1
14 package demo.algo;
15
16 import y.base.Graph;
17 import y.base.Edge;
18 import y.base.Node;
19 import y.base.EdgeList;
20 import y.base.EdgeCursor;
21 import y.base.EdgeMap;
22 import y.base.NodeMap;
23 import y.util.D;
24 import y.util.YRandom;
25
26 import demo.base.RandomGraphGenerator;
27
28 import y.algo.ShortestPaths;
29
30
31
32
38 public class ShortestPathDemo
39 {
40
50 public static void main(String[] args)
51 {
52 int nodeCount = 30;
53 int edgeCount = 60;
54
55 if(args.length == 2) {
56 try {
57 nodeCount = Integer.parseInt(args[0]);
58 edgeCount = Integer.parseInt(args[1]);
59 } catch(NumberFormatException ex) {
60 usage();
61 }
62 }
63
64 RandomGraphGenerator randomGraph = new RandomGraphGenerator(0L);
66 randomGraph.setNodeCount(nodeCount);
67 randomGraph.setEdgeCount(edgeCount);
68 Graph graph = randomGraph.generate();
69
70 EdgeMap weightMap = graph.createEdgeMap();
73 YRandom random = new YRandom(0L);
74 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
75 {
76 Edge e = ec.edge();
77 weightMap.setDouble(e,100.0*random.nextDouble());
78 }
79
80
81
82 if(!graph.isEmpty())
85 {
86
87 Node from = graph.firstNode();
88 Node to = graph.lastNode();
89 EdgeList path;
90 double sum = 0.0;
91
92
95 path = ShortestPaths.singleSourceSingleSink(graph, from, to, false, weightMap);
96 for(EdgeCursor ec = path.edges(); ec.ok(); ec.next())
97 {
98 Edge e = ec.edge();
99 double weight = weightMap.getDouble( e );
100 D.bug( e + " weight = " + weight );
101 sum += weight;
102 }
103 if(sum == 0.0)
104 D.bug("NO UNDIRECTED PATH");
105 else
106 D.bug("UNDIRECTED PATH LENGTH = " + sum);
107
108
109
114 path = ShortestPaths.singleSourceSingleSink(graph, from, to, true, weightMap );
115 sum = 0.0;
116 for(EdgeCursor ec = path.edges(); ec.ok(); ec.next())
117 {
118 Edge e = ec.edge();
119 double weight = weightMap.getDouble( e );
120 D.bug( e + " weight = " + weight );
121 sum += weight;
122 }
123 if(sum == 0.0)
124 D.bug("NO DIRECTED PATH");
125 else
126 D.bug("DIRECTED PATH LENGTH = " + sum);
127
128
129 D.bug("\nAuxiliary distance test\n");
130
131 NodeMap distanceMap = graph.createNodeMap();
132 NodeMap predMap = graph.createNodeMap();
133 ShortestPaths.singleSource(graph, from, true, weightMap, distanceMap, predMap);
134 if(distanceMap.getDouble(to) == Double.POSITIVE_INFINITY)
135 D.bug("Distance from first to last node is infinite");
136 else
137 D.bug("Distance from first to last node is " + distanceMap.getDouble(to));
138
139 graph.disposeNodeMap(distanceMap);
141
142 }
143
144 graph.disposeEdgeMap( weightMap );
146
147 }
148
149 static void usage()
150 {
151 System.err.println("Usage: java demo.algo.ShortestPathDemo <nodeCount> <edgeCount>");
152 System.err.println("Usage: Both <nodeCount> and <edgeCount> must be integral values.");
153 System.exit(1);
154 }
155
156 }
157