예제 #1
0
        private static void JohnsonFunction()
        {
            string      filename = @"data\SimpleGraph.txt";
            Graph <int> g        = new Graph <int>();
            //LoadGraph loader = new LoadGraph(g, @"data\GraphWithNoNegativeCycle.txt");
            LoadGraph loader = new LoadGraph();

            loader.LoadAdjacencyListGraph(g, filename);
            var alg    = new GraphTraversal();
            var result = alg.JohnsonFunction(g);

            if (result.Item1)
            {
                Console.WriteLine("Negative Path found");
                return;
            }
            if (result.Item2 != null)
            {
                foreach (var item in result.Item2)
                {
                    Tuple <GraphNode <int>, GraphNode <int> > keyset = item.Key;
                    GraphNode <int> nodeFrom = keyset.Item1;
                    GraphNode <int> nodeTo   = keyset.Item2;
                    Console.WriteLine("Shortest distance from Node: " + nodeFrom.Value + " To: " + nodeTo.Value + " = " + item.Value);
                }
            }
            Console.WriteLine("Path found: ");
        }
예제 #2
0
        static void BellmanFordTestMethod()
        {
            Graph <int> g      = new Graph <int>();
            LoadGraph   loader = new LoadGraph();

            loader.LoadAdjacencyListGraph(g, @"data\SimpleGraph.txt");
            GraphNode <int> start  = g.GetVertex(new GraphNode <int>(1));
            var             alg    = new GraphTraversal();
            var             result = alg.BellmanFordFunction(g, start);

            Console.WriteLine("Negative Cycle found: " + result.Item1);
        }
예제 #3
0
        private static void FloydWarshallFunction()
        {
            string      filename = @"data\GraphWithNegativeCycle.txt";
            Graph <int> g        = new Graph <int>();
            //LoadGraph loader = new LoadGraph(g, @"data\SimpleGraphWithNegativeWeights.txt");
            LoadGraph loader = new LoadGraph();

            loader.LoadAdjacencyListGraph(g, filename);
            var       alg = new GraphTraversal();
            Stopwatch sw  = new Stopwatch();

            sw.Start();

            var result = alg.FloydWarshallFunction(g);

            sw.Stop();
            if (result.Item1)
            {
                Console.WriteLine("Negative cycle exists");
                return;
            }

            double minVal = double.MaxValue;

            if (result.Item2 != null)
            {
                foreach (var item in result.Item2)
                {
                    Tuple <GraphNode <int>, GraphNode <int> > keyset = item.Key;
                    GraphNode <int> nodeFrom = keyset.Item1;
                    GraphNode <int> nodeTo   = keyset.Item2;
                    Console.WriteLine("Shortest distance from Node: " + nodeFrom.Value + " To: " + nodeTo.Value + " = " + item.Value);
                    if (minVal > item.Value)
                    {
                        minVal = item.Value;
                    }
                    Console.WriteLine("The minimum cost is " + minVal);
                }
            }
            else
            {
                Console.WriteLine("This graph has a negative cycle");
            }
            Console.WriteLine("Path found: Elapsed time(ms) : " + sw.ElapsedMilliseconds);
        }