-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph learning DFS.py
More file actions
40 lines (34 loc) · 1004 Bytes
/
graph learning DFS.py
File metadata and controls
40 lines (34 loc) · 1004 Bytes
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
#graph learning DFS
class myGraph:
def __init__(self, edges):
self.vertices = dict.fromkeys((sum(edges,[])),False)
self.graph = {}
for fro, to in edges:
if fro not in self.graph:
self.graph[fro] = [to]
else:
self.graph[fro].append(to)
print(self.vertices)
print()
print(self.graph)
def DFS_Traverse(self, head):
self.vertices[head] = True
print(head)
if head in self.graph:
for vertex in self.graph[head]:
if self.vertices[vertex] == False:
self.DFS_Traverse(vertex)
def print_traveled(self):
print(self.vertices)
print(list(dict.values(self.vertices)).count(False))
return list(dict.values(self.vertices)).count(False)
# entries = int(input())
# edges = []
# for i in range(entries):
# edge = input()
# edges.append(edge.split(' '))
edges = [['8', '1'],['8', '3'],['7', '4'],['7', '5'],['2', '6'],
['10', '7'],['2', '8'],['10', '9'],['2', '10'],['5', '10']]
newGraph = myGraph(edges)
newGraph.DFS_Traverse('2')
newGraph.print_traveled()