-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.py
38 lines (30 loc) · 915 Bytes
/
Graph.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
class Graph:
def __init__(self,gdict=None):
if gdict is None:
gdict = {}
self.gdict = gdict
def addEdge(self,vertex,edge):
'''
Adds Edge to A given Vertex
'''
self.gdict[vertex].append(edge)
#bREATH FIRST TRAVERSAL
def bfs(self, vertex):
visited = [vertex]
queue = [vertex]
while queue:
deVertex = queue.pop(0)
print(deVertex)
for adjacentVertex in self.gdict[deVertex]:
if adjacentVertex not in visited:
visited.append(adjacentVertex)
queue.append(adjacentVertex)
customDict = { "a" : ["b","c"],
"b" : ["a", "d", "e"],
"c" : ["a", "e"],
"d" : ["b", "e", "f"],
"e" : ["d", "f", "c"],
"f" : ["d", "e"]
}
g = Graph(customDict)
g.dfs("a")