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: "); }
static void BellmanFordTestMethod() { Graph <int> g = new Graph <int>(); LoadGraph loader = new LoadGraph(g, @"data\SimpleGraphWithNegativeWeights.txt"); GraphNode <int> start = g.GetVertex(new GraphNode <int>(0)); var alg = new GraphTraversal(); var result = alg.BellmanFordFunction(g, start); Console.WriteLine("Negative Cycle found: " + result.Item1); }
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); }
private static void TSPDynamicPrgTestMethod() { //double[,] adjmatrix = new double[,]{ // {0,20,42}, // {20,0,30}, // {42,30,0} //}; LoadGraph loader = new LoadGraph(); double[,] adjmatrix = loader.LoadAdjacencyMatrixGraph(@"data\tsp.txt"); TSPIterativeBitMask <double> tsp = new TSPIterativeBitMask <double>(adjmatrix); TSPDynamicPrg obj = new TSPDynamicPrg(adjmatrix); Console.WriteLine(obj.tsp()); }
private static void JohnsonFunction() { Graph <int> g = new Graph <int>(); LoadGraph loader = new LoadGraph(g, @"data\GraphWithNoNegativeCycle.txt"); var alg = new GraphTraversal(); var result = alg.JohnsonFunction(g); if (result != null) { foreach (var item in result) { 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: "); }
private static void FloydWarshallFunction() { Graph <int> g = new Graph <int>(); LoadGraph loader = new LoadGraph(g, @"data\SimpleGraphWithNegativeWeights.txt"); GraphNode <int> start = g.GetVertex(new GraphNode <int>(0)); var alg = new GraphTraversal(); var result = alg.FloydWarshallFunction(g); if (result != null) { foreach (var item in result) { 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: "); }