-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouteBetweenNodes.py
48 lines (37 loc) · 1.17 KB
/
RouteBetweenNodes.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
# Created by Elshad Karimov
# Copyright © 2021 AppMillers. All rights reserved.
class Graph:
def __init__(self, gdict=None):
if gdict is None:
gdict = {}
self.gdict = gdict
def addEdge(self, vertex, edge):
self.gdict[vertex].append(edge)
def checkRoute(self, startNode, endNode):
visited = [startNode]
queue = [startNode]
path = False
while queue:
deVertex = queue.pop(0)
for adjacentVertex in self.gdict[deVertex]:
if adjacentVertex not in visited:
if adjacentVertex == endNode:
path = True
break
else:
visited.append(adjacentVertex)
queue.append(adjacentVertex)
return path
customDict = { "a" : ["c","d", "b"],
"b" : ["j"],
"c" : ["g"],
"d" : [],
"e" : ["f", "a"],
"f" : ["i"],
"g" : ["d", "h"],
"h" : [],
"i" : [],
"j" : []
}
g = Graph(customDict)
print(g.checkRoute("a", "j"))