forked from LeslieK/Algorithms-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindLongShortPaths.py
More file actions
31 lines (27 loc) · 766 Bytes
/
FindLongShortPaths.py
File metadata and controls
31 lines (27 loc) · 766 Bytes
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
from GraphLib import EdgeWeightedDigraph
from BaseClassLib import GraphBase
from DirectedEdge import DEdge # directed, weighted edge
from LongestPath import AcyclicLP
from ShortestPath import AcyclicSP
with open('tinyEWDAG.txt', 'r') as f:
V = int(f.readline().strip())
E = int(f.readline().strip())
text = f.read()
f.close()
#dag = EdgeWeightedDigraph(V)
class DAG(GraphBase):
pass
dag = DAG.graphfactory(V, directed=True, weighted=True)
lines = text.split('\n')
for line in lines[:-1]: # last line is empty
l = line.split()
v = int(l[0])
w = int(l[1])
weight = float(l[2])
dag.addEdge(DEdge(v, w, weight))
#find the longest path from 0 => 5
d = AcyclicLP(dag, 5)
d.distTo(0)
#find the shortest path from 0 => 5
sh = AcyclicSP(dag, 5)
sh.distTo(0)