-
Notifications
You must be signed in to change notification settings - Fork 14
/
10305 - Ordering Tasks.py
98 lines (82 loc) · 2.32 KB
/
10305 - Ordering Tasks.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from collections import defaultdict
from collections import deque
from pprint import pprint
visited = {}
result = deque()
graph = defaultdict(list)
def topoVisit(u):
visited[u] = 1
for v in graph[u]:
if not visited[v]:
topoVisit(v)
result.append(u)
def topoSort(nodes, graph):
for v in nodes:
visited[v] = 0
for v in nodes:
if not visited[v]:
topoVisit(v)
result.reverse()
# return result
print(' '.join([str(e) for e in result]))
def topolgical_sort(graph_unsorted):
graph_sorted = []
graph_unsorted = dict(graph_unsorted)
while graph_unsorted:
acyclic = False
for node, edges in graph_unsorted.items():
for edge in edges:
if edge in graph_unsorted:
break
else:
acyclic = True
del graph_unsorted[node]
graph_sorted.append((node, edges))
if not acyclic:
raise RuntimeError("A cyclic dependency occurred")
return graph_sorted
def dfs(i, countrd, ans):
ans = []
ans.append(i)
countrd[i] -= 1
for j in i.neighbors:
countrd[j] = countrd[j] - 1
if countrd[j] == 0:
dfs(j, countrd, ans)
def topSort1(graph):
# write your code here
countrd = {}
for x in graph:
countrd[x] = 0
for i in graph:
for j in i.neighbors:
countrd[j] = countrd[j] + 1
ans = []
for i in graph:
if countrd[i] == 0:
dfs(i, countrd, ans)
return ans
if __name__ == '__main__':
# while True:
# n,m = map(int,input().split())
# if n == 0 and m == 0:
# break
#
# # nodes = list(range(1,n+1))
# for i in range(m):
# v1,v2 = map(int,input().split())
# graph[v1].append(v2)
graph= [(2, []),
(5, [11]),
(11, [2, 9, 10]),
(7, [11, 8]),
(9, []),
(10, []),
(8, [9]),
(3, [10, 8])]
# m = 4
# for i in range(m):
# v1,v2 = map(int,input().split())
# graph[v1].append(v2)
pprint(topSort1(graph))
# print(*A)