-
Notifications
You must be signed in to change notification settings - Fork 0
/
16_LongestPath_DAG.py
68 lines (63 loc) · 1.76 KB
/
16_LongestPath_DAG.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
#Python
def importData():
file = open('test.txt', 'r')
m=file.readlines()
list=[]
graph={}
c=0
w={}
for i in m:
i=i.strip()
if c==0:
src=i
c+=1
continue
elif c==1:
sink=i
c+=1
continue
node, vertex = i.split("->")
node=int(node)
vertex, weight =vertex.split(":")
vertex=int(vertex)
weight=int(weight)
if node not in graph:
graph[node] = {}
graph[node][vertex] = weight
return int(src),int(sink),graph
def compute(curr, graph, source, sink, longest_path, longest_path_choice):
if curr == sink:
longest_path[sink] = 0
return 0
if curr in longest_path:
return longest_path[curr]
else:
lp = float('-inf')
lpc = None
if curr not in graph:
longest_path[curr] = float('-inf')
longest_path_choice[curr] = None
return float('-inf')
for node in graph[curr]:
res = graph[curr][node] + compute(node, graph, source, sink, longest_path, longest_path_choice)
if res > lp:
lp = res
lpc = node
longest_path_choice[curr] = lpc
longest_path[curr] = lp
return lp
def output(we,source, sink, lpc):
out=[]
out.append(source)
while source != sink:
source = lpc[source]
out.append(source)
file = open("output.txt", "w+")
file.write(str(we)+"\n")
file.write('->'.join(map(str, out)))
file.close()
source,sink,graph=importData()
lp,lpc= {},{}
we=compute(source, graph, source, sink, lp, lpc)
print lpc
output(we,source, sink, lpc)