-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtest_BellmanFordSP.py
64 lines (60 loc) · 2.26 KB
/
test_BellmanFordSP.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
#!/usr/bin/env python
# TBD: Finish Python port
#*****************************************************************************
# Compilation: javac BellmanFordSP.java
# Execution: java BellmanFordSP filename.txt s
# Dependencies: EdgeWeightedDigraph.java DirectedEdge.java Queue.java
# EdgeWeightedDirectedCycle.java
# Data files: http://algs4.cs.princeton.edu/44sp/tinyEWDn.txt
# http://algs4.cs.princeton.edu/44sp/mediumEWDnc.txt
#
# Bellman-Ford shortest path algorithm. Computes the shortest path tree in
# edge-weighted digraph G from vertex s, or finds a negative cost cycle
# reachable from s.
#
# % java BellmanFordSP tinyEWDn.txt 0
# 0 to 0 ( 0.00)
# 0 to 1 ( 0.93) 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52 6->4 -1.25 4->5 0.35 5->1 0.32
# 0 to 2 ( 0.26) 0->2 0.26
# 0 to 3 ( 0.99) 0->2 0.26 2->7 0.34 7->3 0.39
# 0 to 4 ( 0.26) 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52 6->4 -1.25
# 0 to 5 ( 0.61) 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52 6->4 -1.25 4->5 0.35
# 0 to 6 ( 1.51) 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52
# 0 to 7 ( 0.60) 0->2 0.26 2->7 0.34
#
# % java BellmanFordSP tinyEWDnc.txt 0
# 4->5 0.35
# 5->4 -0.66
#
#
#*****************************************************************************/
#from AlgsSedgewickWayne.EdgeWeightedDigraph import EdgeWeightedDigraph
#from AlgsSedgewickWayne.BellmanFordSP import BellmanFordSP
import sys
# TBD
##def main(prt=sys.stdout):
## """Unit tests the BellmanFordSP data type."""
## In in = new In(args[0])
## s = Integer.parseInt(args[1])
## EdgeWeightedDigraph G = new EdgeWeightedDigraph(in)
##
## BellmanFordSP sp = new BellmanFordSP(G, s)
##
## # print negative cycle
## if sp.hasNegativeCycle()):
## for (DirectedEdge e : sp.negativeCycle())
## prt.write(e)
##
## # print shortest paths
## else:
## for (int v = 0; v < G.V(); v += 1):
## if sp.hasPathTo(v)):
## prt.write("{} to {} ({:5.2f}) ".format(s, v, sp.distTo(v)))
## for e in sp.pathTo(v):
## prt.write("{} ".format(e))
## prt.write("\n")
## else:
## prt.write("{} to {} no path\n".format(s, v))
##
##if __name__ == '__main__':
## main()