-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtopoOrdering.py
67 lines (45 loc) · 1.13 KB
/
topoOrdering.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
import sys
def topoOrder(graph2):
ordering = []
candidates = incomingEdges(graph2)
candidates = list(set(candidates))
while len(candidates) > 0:
node = candidates[0]
ordering.append(node)
while(True):
if node not in graph2.keys():
break
nextNode = graph2[node][0]
del graph2[node][0]
if anyEdges(nextNode,graph2) == 0:
candidates.append(nextNode)
if len(graph2[node]) == 0:
break
candidates.remove(node)
return ordering
def anyEdges(node,graph3):
count = 0
for values in graph3.values():
for i in range(len(values)):
if node == values[i]:
count += 1
return count
def incomingEdges(graph3):
array = set()
for key in graph3.keys():
array.add(key)
for values in graph3.values():
for i in range(len(values)):
if values[i] in array:
array.discard(values[i])
return array
filename = sys.argv[1]
dictionary = dict()
with open(filename) as file:
for line in file:
toAdd = line.split('->')[0]
toAdd2 = line.split('->')[1]
toAdd2 = toAdd2.split(',')
dictionary[int(toAdd)] = map(int,toAdd2)
result = topoOrder(dictionary)
print(", ".join(str(x) for x in result))