-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSTopo.py
More file actions
66 lines (54 loc) · 1.97 KB
/
DFSTopo.py
File metadata and controls
66 lines (54 loc) · 1.97 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
from collections import deque, defaultdict
class Graph():
def __init__(self):
self.vertices = 0
self.graph = defaultdict(list)
self.topoOrder = deque()
self.cycleExists = False
self.backEdge = deque()
def addEdgesfromFile(self, filename):
with open(filename) as f:
for line in f:
templist = []
templist = line.split(":")
temp2 = []
temp2 = line.split()
temp2 = temp2[1:]
if len(temp2) == 0:
temp2.append(0)
for i in range(0, len(temp2)):
self.graph[int(templist[0])].append(int(temp2[i]))
f.close()
self.vertices = len(self.graph)
def DFS(self):
color = ["White"] * (self.vertices + 1)
for i in range(1 , self.vertices + 1):
if color[i] == "White":
self.DFSVisit(i, color)
if self.cycleExists == False:
self.topoSort()
elif self.cycleExists == True:
print("Cycle exists, Back Edges occur at:")
while(len(self.backEdge) > 0):
print(self.backEdge.popleft())
def DFSVisit(self, u, color):
color[u] = "Gray"
for v in self.graph[u]:
if color[v] == "White":
self.DFSVisit(v, color)
if color[v] == "Gray":
self.backEdge.appendleft((u, v))
self.cycleExists = True
color[u] = "Black"
self.topoOrder.appendleft(u)
def topoSort(self):
self.topoOrder.remove(0)
print("Topological Order")
while(len(self.topoOrder) > 0):
print(self.topoOrder.popleft())
if __name__ == "__main__":
filename = input("Enter the name of the graph file, ex. graphin-c1.txt: ")
graph = Graph()
graph.addEdgesfromFile(filename)
print("Graph Created, Performing DFS Topo Sort")
graph.DFS()