Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 9953a3e

Browse files
authored
Add files via upload
1 parent 0be2a00 commit 9953a3e

9 files changed

+856
-0
lines changed

bfs.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Uses python3
2+
##############################
3+
# @author Daniel Avdar
4+
# @input: #edges, #vertices (one line)
5+
# the graph's edges (following lines)
6+
# the vertices u,v to find the path between them (last line)
7+
# @output: the minimum number of edges in a path from 𝑢 to 𝑣,
8+
# or −1 if there is no path.
9+
# @description: bfs algorithm for finding the shortest path in a graph
10+
#
11+
##############################
12+
13+
import sys
14+
import queue
15+
16+
q = queue.Queue()
17+
from numpy import inf
18+
19+
20+
def generate_DWG_raph(edges, n):
21+
graph = {}
22+
for i in range(1, n + 1):
23+
graph[i] = [False, dict(), False, [inf, i]]
24+
for (a, b) in edges:
25+
graph[a][1][b] = 1
26+
graph[b][1][a] = 1
27+
return graph
28+
29+
30+
def distance(graph, s, t):
31+
qu = queue.Queue()
32+
33+
def set_v(v, w):
34+
graph[v][3][0] = min(w, graph[v][3][0])
35+
36+
set_v(s, 0)
37+
qu.put(graph[s][3])
38+
graph[s][2] = True
39+
while not qu.empty():
40+
w, v = qu.get()
41+
graph[v][0] = True
42+
for i in graph[v][1].keys():
43+
if graph[i][0]:
44+
continue
45+
46+
set_v(i, w + graph[v][1][i])
47+
if graph[i][2]:
48+
continue
49+
graph[i][2] = True
50+
qu.put(graph[i][3])
51+
52+
return graph[t][3][0] if graph[t][3][0] != inf else -1
53+
54+
55+
def main():
56+
input = sys.stdin.read()
57+
data = list(map(int, input.split()))
58+
n, m = data[0:2]
59+
data = data[2:]
60+
edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2]))
61+
g = generate_DWG_raph(edges, n)
62+
if not data:
63+
print(-1)
64+
return
65+
s, t = data[-2], data[-1]
66+
print(distance(g, s, t))
67+
68+
69+
if __name__ == '__main__':
70+
main()
71+
# todo git

dijkstra.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Uses python3
2+
##############################
3+
# @author Daniel Avdar
4+
# @input: #edges, #vertices (one line)
5+
# the graph's edges (following lines)
6+
# the vertices s,t to find the path between them (last line)
7+
# @output: the minimum weight of a path from s to t,
8+
# or −1 if there is no path.
9+
# @description: Dijkstra algorithm for finding the shortest path in a weighted graph
10+
#
11+
##############################
12+
13+
import sys
14+
import heapq
15+
from numpy import inf
16+
17+
heap = []
18+
19+
20+
def generateDWGraph(edges, n):
21+
graph = {}
22+
for i in range(1, n + 1):
23+
graph[i] = [False, dict(), None, [inf, i]]
24+
heap.append(graph[i][3])
25+
for ((a, b), w) in edges:
26+
graph[a][1][b] = w
27+
28+
return graph
29+
30+
31+
def distance(graph, s, t):
32+
heap_ = heap
33+
34+
def set_v(v, w):
35+
graph[v][3][0] = min(w, graph[v][3][0])
36+
37+
set_v(s, 0)
38+
heapq.heapify(heap_)
39+
40+
while heap_:
41+
w, v = heapq.heappop(heap_)
42+
graph[v][0] = True
43+
for i in graph[v][1].keys():
44+
if graph[i][0]:
45+
continue
46+
set_v(i, w + graph[v][1][i])
47+
heapq.heapify(heap_)
48+
return graph[t][3][0] if graph[t][3][0] != inf else -1
49+
50+
51+
def main():
52+
input_ = sys.stdin.read()
53+
data = list(map(int, input_.split()))
54+
n, m = data[0:2]
55+
data = data[2:]
56+
edges = list(zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3]))
57+
58+
if not data:
59+
print(0)
60+
return
61+
62+
s, t = data[-2], data[-1]
63+
g = generateDWGraph(edges, n)
64+
print(distance(g, s, t))
65+
66+
67+
if __name__ == '__main__':
68+
main()
69+
# todo git

eulerian_path.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# python3
2+
##############################
3+
#
4+
# @author Daniel Avdar
5+
#
6+
# @description: Finds the Eulerian cycles in the graph and returns an Eulerian Path.
7+
# That is used for a (circular) genome, that spells Eulerian cycles in
8+
# the de Bruijn graph constructed on all k-mers of the genome.
9+
#
10+
# @input:The first line contains integers n and m— the number of vertices
11+
# and the number of edges,respectively.
12+
# Each of the following m lines specifies an edge in the format “u
13+
# v”. (As usual, we assume that the vertices of the graph
14+
# are {1,2,...,n}.) The graph may contain self-loops
15+
# (that is, edges of the form(v,v)) and parallel edges
16+
# (that is, several copies of the same edge).
17+
# It is guaranteed that the graph is strongly connected.
18+
#
19+
# @output:If the graph has no Eulerian cycle, output 0.
20+
# Otherwise output 1 in the first line and a sequence v1,v2,...,vm
21+
# of vertices in the second line - the given path.
22+
#
23+
##############################
24+
import threading
25+
import collections
26+
import sys
27+
28+
sys.setrecursionlimit(8 ** 10)
29+
threading.stack_size(2 ** 27)
30+
31+
Vertex = collections.namedtuple('Vertex',
32+
["visited", "neighbors", "self", "num_of_outs", "num_of_ins"]) # "back_neighbors"
33+
34+
35+
def dfs(graph, vertex, func):
36+
while graph[vertex].num_of_outs > 0:
37+
neighbor_ind = len(graph[vertex].neighbors) - graph[vertex].num_of_outs
38+
neighbor = graph[graph[vertex].neighbors[neighbor_ind]].self
39+
graph[vertex] = graph[vertex]._replace(num_of_outs=graph[vertex].num_of_outs - 1)
40+
dfs(graph, neighbor, func)
41+
func(vertex)
42+
43+
44+
def find_start(graph):
45+
start_v = 1
46+
for i in graph:
47+
outs = graph[i].num_of_outs
48+
ins = graph[i].num_of_ins
49+
if outs != ins:
50+
return 0
51+
return start_v
52+
53+
54+
def build_graph(edges, max_v_num):
55+
graph = dict()
56+
for i in range(1, max_v_num + 1):
57+
graph[i] = Vertex._make([False, list(), i, 0, 0])
58+
for e in edges:
59+
graph[e[0]].neighbors.append(e[1])
60+
graph[e[0]] = graph[e[0]]._replace(num_of_outs=graph[e[0]].num_of_outs + 1)
61+
graph[e[1]] = graph[e[1]]._replace(num_of_ins=graph[e[1]].num_of_ins + 1)
62+
start_v = find_start(graph)
63+
if start_v == 0:
64+
return None
65+
return graph
66+
67+
68+
def inputer():
69+
data = sys.stdin.read().split()
70+
data = list(map(lambda i: (int(data[i]), int(data[i + 1])), range(0, len(data) - 1, 2)))
71+
n, edges_num = data[0]
72+
return n, edges_num, data[1:]
73+
74+
75+
def find_eulerian_path(n, data):
76+
graph = build_graph(data, n)
77+
if graph == None:
78+
return 0
79+
tmp_res = []
80+
81+
def func(v):
82+
tmp_res.append(v)
83+
84+
dfs(graph, graph[1].self, func)
85+
tmp_res = tmp_res[:len(tmp_res) - 1][::-1]
86+
return 1, tmp_res
87+
88+
89+
def test(filename):
90+
"""
91+
>>> test("01")
92+
1
93+
>>> test("02")
94+
1
95+
>>> test("03")
96+
1
97+
>>> test("04")
98+
1
99+
>>> test("05")
100+
1
101+
>>> test("06")
102+
1
103+
>>> test("07")
104+
"""
105+
f = open(filename)
106+
107+
lines = f.readlines()
108+
109+
n, m = map(int, lines[0].split())
110+
111+
adj_matrix = [list(map(int, i.split())) for i in lines[1:]]
112+
113+
f.close()
114+
f = open(filename + ".a")
115+
lines = f.readlines()
116+
expected1 = list(map(int, lines[0].split()))[0]
117+
expected2 = None
118+
if expected1:
119+
expected2 = list(map(int, lines[1].split()))
120+
f.close()
121+
exist = find_eulerian_path(n, adj_matrix)
122+
res = None
123+
if exist is not 0:
124+
exist, res = exist
125+
126+
pass_ = exist == expected1 and res == expected2
127+
return 1 if pass_ else res
128+
129+
130+
def main():
131+
n, Edges_num, data = inputer()
132+
res = find_eulerian_path(n, data)
133+
if res == 0:
134+
print(res)
135+
return
136+
print(res[0])
137+
for i in res[1]:
138+
print(i, end=' ')
139+
140+
141+
threading.Thread(target=main).start()
142+
# todo git

0 commit comments

Comments
 (0)