/// <summary> /// Main method: /// </summary> public static void Main() { // Create a random graph with the given edge and node count RandomGraphGenerator randomGraph = new RandomGraphGenerator(0); randomGraph.NodeCount = 30; randomGraph.EdgeCount = 60; Graph graph = randomGraph.Generate(); // Create an edgemap and assign random double weights to // the edges of the graph. IEdgeMap weightMap = graph.CreateEdgeMap(); Random random = new Random(0); for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next()) { Edge e = ec.Edge; weightMap.SetDouble(e, 100.0 * random.NextDouble()); } // Calculate the shortest path from the first to the last node // within the graph if (!graph.Empty) { Node from = graph.FirstNode; Node to = graph.LastNode; double sum = 0.0; // The undirected case first, i.e. edges of the graph and the // resulting shortest path are considered to be undirected EdgeList path = ShortestPaths.SingleSourceSingleSink(graph, from, to, false, weightMap); for (IEdgeCursor ec = path.Edges(); ec.Ok; ec.Next()) { Edge e = ec.Edge; double weight = weightMap.GetDouble(e); Console.WriteLine(e + " weight = " + weight); sum += weight; } if (sum == 0.0) { Console.WriteLine("NO UNDIRECTED PATH"); } else { Console.WriteLine("UNDIRECTED PATH LENGTH = " + sum); } // Next the directed case, i.e. edges of the graph and the // resulting shortest path are considered to be directed. // Note that this shortest path can't be shorter then the one // for the undirected case path = ShortestPaths.SingleSourceSingleSink(graph, from, to, true, weightMap); sum = 0.0; for (IEdgeCursor ec = path.Edges(); ec.Ok; ec.Next()) { Edge e = ec.Edge; double weight = weightMap.GetDouble(e); Console.WriteLine(e + " weight = " + weight); sum += weight; } if (sum == 0.0) { Console.WriteLine("NO DIRECTED PATH"); } else { Console.WriteLine("DIRECTED PATH LENGTH = " + sum); } Console.WriteLine("\nAuxiliary distance test\n"); INodeMap distanceMap = graph.CreateNodeMap(); INodeMap predMap = graph.CreateNodeMap(); ShortestPaths.SingleSource(graph, from, true, weightMap, distanceMap, predMap); if (distanceMap.GetDouble(to) == double.PositiveInfinity) { Console.WriteLine("Distance from first to last node is infinite"); } else { Console.WriteLine("Distance from first to last node is " + distanceMap.GetDouble(to)); } // Dispose distanceMap since it is not needed anymore graph.DisposeNodeMap(distanceMap); } // Dispose weightMap since it is not needed anymore graph.DisposeEdgeMap(weightMap); Console.WriteLine("\nPress key to end demo."); Console.ReadKey(); }