public static void usageExample1(String graphFilename, String source, String target, int k)
    {
        /* Read graph from file */
        Console.WriteLine("Reading data from file... ");
        Graph graph = new Graph(graphFilename);

        Console.WriteLine("complete.");
        printGraph(graph);
        /* Compute the K shortest paths and record the completion time */
        Console.WriteLine("Computing the " + k + " shortest paths from [" + source + "] to [" + target + "] ");
        Console.WriteLine("using Yen's algorithm... ");
        IList <Path> ksp;
        long         timeStart    = 0;// TODO System.currentTimeMillis();
        Yen          yenAlgorithm = new Yen();

        ksp = yenAlgorithm.Ksp(graph, source, target, k);
        long timeFinish = 0;// TODO SystemJ.currentTimeMillis();

        Console.WriteLine("complete.");
        Console.WriteLine("Operation took " + (timeFinish - timeStart) / 1000.0 + " seconds.");
        /* Output the K shortest paths */
        Console.WriteLine("k) cost: [path]");
        int n = 0;

        foreach (Path p in ksp)
        {
            Console.WriteLine(++n + ") " + p);
        }
    }
Пример #2
0
        protected override IList <P> FindShortestPathHook(
            V startVertex,
            V endVertex,
            int maxNumberOfPaths
            )
        {
            IList <P> paths = new System.Collections.Generic.List <P>();
            IList <edu.ufl.cise.bsmock.graph.util.Path> pathList = yenAlgorithm.Ksp(
                graphAdaptee,
                startVertex.VertexId,
                endVertex.VertexId,
                maxNumberOfPaths
                );

            foreach (edu.ufl.cise.bsmock.graph.util.Path path in pathList)
            {
                java.util.LinkedList <edu.ufl.cise.bsmock.graph.Edge> listOfEdges = path.GetEdges();
                IList <E> edges = new System.Collections.Generic.List <E>();
                foreach (edu.ufl.cise.bsmock.graph.Edge edgeAdaptee in listOfEdges)
                {
                    E edge = GetOriginalEdgeInstance(edgeAdaptee);
                    edges.Add(
                        edge
                        );
                }
                W totalWeight = base.CreateInstanceWithTotalWeight(path.GetTotalCost(), edges);
                paths.Add(base.CreatePath(totalWeight, edges));
            }
            return(new ReadOnlyCollection <P>(paths));
        }
Пример #3
0
        public void YenKShortestPathsTest()
        {
            const double deltaValue = 0.0000001;

            var graph = new Graph();

            graph.AddEdge("A", "B", 5);
            graph.AddEdge("A", "C", 6);
            graph.AddEdge("B", "C", 7);
            graph.AddEdge("B", "D", 8);
            graph.AddEdge("C", "D", 9);

            Yen          yenAlgorithm = new Yen();
            IList <Path> paths        = yenAlgorithm.Ksp(graph, "A", "D", 5);

            Assert.AreEqual(3, paths.Count);

            Path path1 = paths[0];
            Path path2 = paths[1];
            Path path3 = paths[2];

            Assert.AreEqual(13, path1.GetTotalCost(), deltaValue);
            Assert.AreEqual(15, path2.GetTotalCost(), deltaValue);
            Assert.AreEqual(21, path3.GetTotalCost(), deltaValue);

            java.util.LinkedList <String> nodes1 = path1.GetNodes();
            Assert.AreEqual(3, nodes1.size());
            Assert.AreEqual("A", nodes1.get(0));
            Assert.AreEqual("B", nodes1.get(1));
            Assert.AreEqual("D", nodes1.get(2));

            java.util.LinkedList <String> nodes2 = path2.GetNodes();
            Assert.AreEqual(3, nodes2.size());
            Assert.AreEqual("A", nodes2.get(0));
            Assert.AreEqual("C", nodes2.get(1));
            Assert.AreEqual("D", nodes2.get(2));

            java.util.LinkedList <String> nodes3 = path3.GetNodes();
            Assert.AreEqual(4, nodes3.size());
            Assert.AreEqual("A", nodes3.get(0));
            Assert.AreEqual("B", nodes3.get(1));
            Assert.AreEqual("C", nodes3.get(2));
            Assert.AreEqual("D", nodes3.get(3));
        }