-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopsort (dfs).py
More file actions
48 lines (40 loc) · 865 Bytes
/
Copy pathtopsort (dfs).py
File metadata and controls
48 lines (40 loc) · 865 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
41
42
43
44
45
46
47
48
import networkx as nx
g = {
0:[2],
1:[0,2,7],
2:[5,6],
3:[0,4],
4:[2],
5:[10],
6:[9,10],
7:[8,9],
8:[9],
9:[11,12],
10:[11],
11:[],
12:[]
}
G =nx.DiGraph(g)
print(G)
def topsort(G):
N = G.number_of_nodes()
V = [False] * N
ordering = [0] * N
i = N-1
for at in range(N):
if V[at] == False:
visited_nodes =[]
DFS(at, V, visited_nodes, G)
for node_id in visited_nodes:
ordering[i] = node_id
i -= 1
return ordering
def DFS(at, V, visited_nodes, G):
V[at] = True
edges = list(G.edges())
for edge in edges:
if(V[edge[1]]==False):
DFS(edge[1], V, visited_nodes, G)
visited_nodes.append(at)
print(list(G.edges()))
print(topsort(G))