-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKahn's_algorrithm.py
More file actions
67 lines (52 loc) · 1.14 KB
/
Copy pathKahn's_algorrithm.py
File metadata and controls
67 lines (52 loc) · 1.14 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
67
g = {
0:[2, 3],
1:[4],
2:[6],
3:[1,4],
4:[5,8],
5:[],
6:[7,11],
7:[4,12],
8:[],
9:[2,10],
10:[6],
11:[12],
12:[8],
13:[]
}
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return len(self.items) == 0
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
else:
return "Queue is empty"
def find_topological_ordering(g):
n = len(g)
in_degree = [0]*n
for i in g:
for to in g[i]:
in_degree[to] += 1
q = Queue()
for i in g:
if in_degree[i] == 0:
q.enqueue(i)
index = 0
order = []
while q.is_empty() == False:
at = q.dequeue()
order.append(at)
index += 1
for to in g[at]:
in_degree[to] -= 1
if in_degree[to] == 0:
q.enqueue(to)
if index != n:
return None
return order
print(find_topological_ordering(g))