예제 #1
0
        private static void testGraph()
        {
            //see graph.png
            Edge<string, float>[] edges = new Edge<string, float>[] 
            {
                new Edge<string, float>("1", "2", 7),
                new Edge<string, float>("1", "3", 9),
                new Edge<string, float>("1", "6", 14),

                new Edge<string, float>("2", "3", 10),
                new Edge<string, float>("2", "4", 15),

                new Edge<string, float>("3", "6", 2),
                new Edge<string, float>("3", "4", 11),

                new Edge<string, float>("4", "5", 6),

                new Edge<string, float>("5", "6", 9)
            };

            var graph = edges.ToGraph<string, Edge<string, float>>();

            IDictionary<Pair<string>, double> costMatrix;
            var paths = graph.FindShortestPaths(x => x.Tag, out costMatrix);

            var vertices = graph.GetVertices<string, Edge<string, float>>();

            Console.WriteLine("Possible paths:");
            foreach (var v1 in vertices)
            {
                foreach (var v2 in vertices)
                {
                    var path = paths.Get(v1, v2);
                    var cost = costMatrix.ContainsKey(v1, v2) ? costMatrix.Get(v1, v2): Double.NaN;

                    Console.Write("{0} => {1}: Cost {2}. Edges:", v1, v2, cost.ToString().PadLeft(3));
                    foreach (var edge in path)
                    {
                        Console.Write("({0} => {1}) ", edge.Source, edge.Destination);
                    }
                    Console.WriteLine();
                }
            }
        }