-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlongestPathDAG.py
99 lines (69 loc) · 1.65 KB
/
longestPathDAG.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
99
import sys
import copy
def topoOrdering(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
def longestPath(graph, weightGraph, source, sink):
weights = dict()
tempGraph = copy.deepcopy(graph)
order = topoOrdering(graph)
for value in order:
weights[value] = float("-inf")
weights['0'] = 0
order.remove(order[0])
for value in order:
if weights[value] < weights[]
filename = sys.argv[1]
file = open(filename, 'r')
data = file.read().splitlines()
array = []
for line in data:
array.append(line)
dictionary = dict()
for element in array:
key = element[0]
value = element[3]
if key not in dictionary:
dictionary[key] = [value]
else:
dictionary[key].append(value)
dictionary2 = dict()
for element in array:
key = element[0]
value = element[3:]
if key not in dictionary2:
dictionary2[key] = [value]
else:
dictionary2[key].append(value)
longestPath(dictionary,dictionary2,0,4)