forked from srgnk/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctci-bfs-shortest-reach.py
54 lines (45 loc) · 1.46 KB
/
ctci-bfs-shortest-reach.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# TESTING
import queue
class Graph:
def __init__(self, n):
self.size = n
self.vert = dict.fromkeys([n for n in range(n)])
for node in self.vert.keys():
self.vert[node] = []
def print_graph(self):
print(self.vert)
def connect(self, x, y):
self.vert[x].append(y)
self.vert[y].append(x)
def find_shortest(self, start):
next_to_visit = queue.Queue()
visited = []
node = start
next_to_visit.put(node)
distances = [-1] * self.size
distances[node] = 0
while not next_to_visit.empty():
node = next_to_visit.get()
height = distances[node]
for adj in self.vert[node]:
if not adj in visited:
distances[adj] = height + 6
next_to_visit.put(adj)
visited.append(adj)
return distances
def find_all_distances(self, start):
result = self.find_shortest(start)
for ind, node in enumerate(result):
if ind != start:
print("{} ".format(node), end='')
print()
t = int(input())
for i in range(t):
n,m = [int(value) for value in input().split()]
graph = Graph(n)
for i in range(m):
x,y = [int(x) for x in input().split()]
graph.connect(x-1,y-1)
s = int(input())
#graph.print_graph()
graph.find_all_distances(s-1)