public void DoTest()
        {
            DirectedWeightedSparseGraph <string> graph = new DirectedWeightedSparseGraph <string>();

            // Add vertices
            var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" };

            graph.AddVertices(verticesSet1);

            // Add edges
            graph.AddEdge("a", "s", 1);
            graph.AddEdge("a", "z", 1);
            graph.AddEdge("s", "x", 1);
            graph.AddEdge("x", "d", 1);
            graph.AddEdge("x", "c", 1);
            graph.AddEdge("d", "f", 1);
            graph.AddEdge("d", "c", 1);
            graph.AddEdge("c", "f", 1);
            graph.AddEdge("c", "v", 1);
            graph.AddEdge("v", "f", 1);

            // Print the nodes in graph
            Console.WriteLine(" [*] DFS PrintAll: ");
            DepthFirstSearcher.PrintAll(graph, "a");
            Console.WriteLine("\r\n");

            var list = DepthFirstSearcher.FindAllPaths(graph, "a", "f");

            foreach (var path in list)
            {
                Console.WriteLine(string.Join(",", path));
            }

            Assert.AreEqual(5, list.Count);
        }
Ejemplo n.º 2
0
        public void ShortestPathTo_FindShortestPath_WhenEdgeHaveDifferentWeight(ShortestPathAlgorithm alg)
        {
            var vertices = new[] { "r", "s", "t", "x", "y", "z" };
            var graph    = new DirectedWeightedSparseGraph <string>();

            graph.AddVertices(vertices);

            graph.AddEdge("r", "s", 7);
            graph.AddEdge("r", "t", 6);
            graph.AddEdge("s", "t", 5);
            graph.AddEdge("s", "x", 9);
            graph.AddEdge("t", "x", 10);
            graph.AddEdge("t", "y", 7);
            graph.AddEdge("t", "z", 5);
            graph.AddEdge("x", "y", 2);
            graph.AddEdge("x", "z", 4);
            graph.AddEdge("y", "z", 1);

            var algorithm   = CreateAlgorithm(alg, graph, "s");
            var shortestToZ = algorithm.ShortestPathTo("z");

            Assert.NotNull(shortestToZ);
            Assert.Equal(3, shortestToZ.Count());
            Assert.Contains("s", shortestToZ);
            Assert.Contains("t", shortestToZ);
            Assert.Contains("z", shortestToZ);
            Assert.Equal(10, algorithm.DistanceTo("z"));

            var shortestToY = algorithm.ShortestPathTo("y");

            Assert.NotNull(shortestToY);
            Assert.Equal(3, shortestToY.Count());
            Assert.Contains("s", shortestToY);
            Assert.Contains("x", shortestToY);
            Assert.Contains("y", shortestToY);
            Assert.Equal(11, algorithm.DistanceTo("y"));
        }
        public void ShortestPathTo_FindShortestPath_WhenEdgeHaveDifferentWeight()
        {
            var vertices = new[] { "r", "s", "t", "x", "y", "z" };
            var graph    = new DirectedWeightedSparseGraph <string>();

            graph.AddVertices(vertices);

            graph.AddEdge("r", "s", 7);
            graph.AddEdge("r", "t", 6);
            graph.AddEdge("s", "t", 5);
            graph.AddEdge("s", "x", 9);
            graph.AddEdge("t", "x", 10);
            graph.AddEdge("t", "y", 7);
            graph.AddEdge("t", "z", 5);
            graph.AddEdge("x", "y", 2);
            graph.AddEdge("x", "z", 4);
            graph.AddEdge("y", "z", 1);

            var dijkstra    = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "s");
            var shortestToZ = dijkstra.ShortestPathTo("z");

            Assert.NotNull(shortestToZ);
            Assert.Equal(3, shortestToZ.Count());
            Assert.Contains("s", shortestToZ);
            Assert.Contains("t", shortestToZ);
            Assert.Contains("z", shortestToZ);
            Assert.Equal(10, dijkstra.DistanceTo("z"));

            var shortestToY = dijkstra.ShortestPathTo("y");

            Assert.NotNull(shortestToY);
            Assert.Equal(3, shortestToY.Count());
            Assert.Contains("s", shortestToY);
            Assert.Contains("x", shortestToY);
            Assert.Contains("y", shortestToY);
            Assert.Equal(11, dijkstra.DistanceTo("y"));
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            string result = string.Empty;

            string[] V;
            IEnumerable <WeightedEdge <string> > E;
            DirectedWeightedSparseGraph <string> graph;
            DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string> dijkstra;

            // Init graph object
            graph = new DirectedWeightedSparseGraph <string>();

            // Init V
            V = new string[6] {
                "r", "s", "t", "x", "y", "z"
            };
            result = result + "Initial Verrtices: 'r', 's', 't', 'x', 'y', 'z'" + "\n";

            // Insert V
            graph.AddVertices(V);
            result = result + "# of vertices: " + V.Length + "\n";

            // Insert E
            var status = graph.AddEdge("r", "s", 7);

            status = graph.AddEdge("r", "t", 6);
            status = graph.AddEdge("s", "t", 5);
            status = graph.AddEdge("s", "x", 9);
            status = graph.AddEdge("t", "x", 10);
            status = graph.AddEdge("t", "y", 7);
            status = graph.AddEdge("t", "z", 5);
            status = graph.AddEdge("x", "y", 2);
            status = graph.AddEdge("x", "z", 4);
            status = graph.AddEdge("y", "z", 1);

            // Get E
            E = graph.Edges;
            //Debug.Assert(graph.EdgesCount == 10, "Wrong Edges Count.");
            result = result + "# of edges: " + graph.EdgesCount + "\n\n";

            //
            // PRINT THE GRAPH
            result = result + "[*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH:\r\n";

            result = result + "Graph representation:";
            result = result + graph.ToReadable() + "\r\n\n";

            // Init DIJKSTRA
            dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "s");

            result = result + "dijkstra.HasPathTo('r') is " + dijkstra.HasPathTo("r") + "\n";
            result = result + "dijkstra.HasPathTo('z') is " + dijkstra.HasPathTo("z") + "\n\n";

            // Get shortest path to Z
            var pathToZ = string.Empty;

            foreach (var node in dijkstra.ShortestPathTo("z"))
            {
                pathToZ = String.Format("{0}({1}) -> ", pathToZ, node);
            }
            pathToZ = pathToZ.TrimEnd(new char[] { ' ', '-', '>' });

            result = result + "Shortest path to node 'z': " + pathToZ + "\r\n";

            var pathToY = string.Empty;

            foreach (var node in dijkstra.ShortestPathTo("y"))
            {
                pathToY = String.Format("{0}({1}) -> ", pathToY, node);
            }
            pathToY = pathToY.TrimEnd(new char[] { ' ', '-', '>' });

            result = result + "Shortest path to node 'y': " + pathToY + "\r\n\n";


            ///***************************************************************************************/


            //// Clear the graph and insert new V and E to the instance
            //graph.Clear();

            //V = new string[] { "A", "B", "C", "D", "E" };

            //// Insert new values of V
            //graph.AddVertices(V);
            //Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            //// Insert new value for edges
            //status = graph.AddEdge("A", "C", 7);
            //Debug.Assert(status == true);
            //status = graph.AddEdge("B", "A", 19);
            //Debug.Assert(status == true);
            //status = graph.AddEdge("B", "C", 11);
            //Debug.Assert(status == true);
            //status = graph.AddEdge("C", "E", 5);
            //Debug.Assert(status == true);
            //status = graph.AddEdge("C", "D", 15);
            //Debug.Assert(status == true);
            //status = graph.AddEdge("D", "B", 4);
            //Debug.Assert(status == true);
            //status = graph.AddEdge("E", "D", 13);
            //Debug.Assert(status == true);

            //Debug.Assert(graph.EdgesCount == 7, "Wrong Edges Count.");

            ////
            //// PRINT THE GRAPH
            //Console.Write("[*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            //Console.WriteLine("Graph representation:");
            //Console.WriteLine(graph.ToReadable() + "\r\n");

            //// Init DIJKSTRA
            //dijkstra = new DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "A");

            //var pathToD = string.Empty;
            //foreach (var node in dijkstra.ShortestPathTo("D"))
            //    pathToD = String.Format("{0}({1}) -> ", pathToD, node);
            //pathToD = pathToD.TrimEnd(new char[] { ' ', '-', '>' });

            //Console.WriteLine("Shortest path from 'A' to 'D': " + pathToD + "\r\n");

            //Console.WriteLine("*********************************************\r\n");


            ///***************************************************************************************/


            var dijkstraAllPairs = new DijkstraAllPairsShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph);

            var vertices = graph.Vertices;

            result = result + "Dijkstra All Pairs Shortest Paths: \r\n";

            foreach (var source in vertices)
            {
                foreach (var destination in vertices)
                {
                    var shortestPath = string.Empty;
                    if (dijkstraAllPairs.ShortestPath(source, destination) != null)
                    {
                        foreach (var node in dijkstraAllPairs.ShortestPath(source, destination))
                        {
                            shortestPath = String.Format("{0}({1}) -> ", shortestPath, node);
                        }


                        shortestPath = shortestPath.TrimEnd(new char[] { ' ', '-', '>' });

                        result = result + "Shortest path from '" + source + "' to '" + destination + "' is: " + shortestPath + "\r\n";
                    }
                }
            }

            //Console.ReadLine();


            HtmlString html = StringHelper.GetHtmlString(result);

            return(View(html));
        }
        public static void DoTest()
        {
            string[] V;
            IEnumerable <WeightedEdge <string> > E;
            DirectedWeightedSparseGraph <string> graph;
            DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string> dijkstra;

            // Init graph object
            graph = new DirectedWeightedSparseGraph <string>();

            // Init V
            V = new string[6] {
                "r", "s", "t", "x", "y", "z"
            };

            // Insert V
            graph.AddVertices(V);
            Assert.True(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert E
            var status = graph.AddEdge("r", "s", 7);

            Assert.True(status == true);
            status = graph.AddEdge("r", "t", 6);
            Assert.True(status == true);
            status = graph.AddEdge("s", "t", 5);
            Assert.True(status == true);
            status = graph.AddEdge("s", "x", 9);
            Assert.True(status == true);
            status = graph.AddEdge("t", "x", 10);
            Assert.True(status == true);
            status = graph.AddEdge("t", "y", 7);
            Assert.True(status == true);
            status = graph.AddEdge("t", "z", 5);
            Assert.True(status == true);
            status = graph.AddEdge("x", "y", 2);
            Assert.True(status == true);
            status = graph.AddEdge("x", "z", 4);
            Assert.True(status == true);
            status = graph.AddEdge("y", "z", 1);
            Assert.True(status == true);

            // Get E
            E = graph.Edges;
            Assert.True(graph.EdgesCount == 10, "Wrong Edges Count.");

            // PRINT THE GRAPH
            // [*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH - TEST 01:

            // Graph representation:
            // Init DIJKSTRA
            dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "s");

            Assert.True(dijkstra.HasPathTo("r") == false);
            Assert.True(dijkstra.HasPathTo("z") == true);

            // Get shortest path to Z
            var pathToZ = string.Empty;

            foreach (var node in dijkstra.ShortestPathTo("z"))
            {
                pathToZ = String.Format("{0}({1}) -> ", pathToZ, node);
            }

            pathToZ = pathToZ.TrimEnd(new char[] { ' ', '-', '>' });

            var pathToY = string.Empty;

            foreach (var node in dijkstra.ShortestPathTo("y"))
            {
                pathToY = String.Format("{0}({1}) -> ", pathToY, node);
            }

            pathToY = pathToY.TrimEnd(new char[] { ' ', '-', '>' });

            // Console.WriteLine("Shortest path to node 'y': " + pathToY + "\r\n");

            // Clear the graph and insert new V and E to the instance
            graph.Clear();

            V = new string[] { "A", "B", "C", "D", "E" };

            // Insert new values of V
            graph.AddVertices(V);
            Assert.True(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert new value for edges
            status = graph.AddEdge("A", "C", 7);
            Assert.True(status == true);
            status = graph.AddEdge("B", "A", 19);
            Assert.True(status == true);
            status = graph.AddEdge("B", "C", 11);
            Assert.True(status == true);
            status = graph.AddEdge("C", "E", 5);
            Assert.True(status == true);
            status = graph.AddEdge("C", "D", 15);
            Assert.True(status == true);
            status = graph.AddEdge("D", "B", 4);
            Assert.True(status == true);
            status = graph.AddEdge("E", "D", 13);
            Assert.True(status == true);

            Assert.True(graph.EdgesCount == 7, "Wrong Edges Count.");

            // [*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH - TEST 01:

            // Graph representation:
            // Init DIJKSTRA
            dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "A");

            var pathToD = string.Empty;

            foreach (var node in dijkstra.ShortestPathTo("D"))
            {
                pathToD = String.Format("{0}({1}) -> ", pathToD, node);
            }

            pathToD = pathToD.TrimEnd(new char[] { ' ', '-', '>' });

            var vertices         = graph.Vertices;
            var dijkstraAllPairs = new DijkstraAllPairsShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph);

            // Dijkstra All Pairs Shortest Paths:
            foreach (var source in vertices)
            {
                foreach (var destination in vertices)
                {
                    var shortestPath = string.Empty;
                    foreach (var node in dijkstraAllPairs.ShortestPath(source, destination))
                    {
                        shortestPath = String.Format("{0}({1}) -> ", shortestPath, node);
                    }

                    shortestPath = shortestPath.TrimEnd(new char[] { ' ', '-', '>' });

                    // Console.WriteLine("Shortest path from '" + source + "' to '" + destination + "' is: " + shortestPath + "\r\n");
                }
            }
        }
        public static void DoTest()
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" };

            graph.AddVertices(verticesSet1);

            graph.AddEdge("a", "s", 1);
            graph.AddEdge("a", "z", 2);
            graph.AddEdge("s", "x", 3);
            graph.AddEdge("x", "d", 1);
            graph.AddEdge("x", "c", 2);
            graph.AddEdge("x", "a", 3);
            graph.AddEdge("d", "f", 1);
            graph.AddEdge("d", "c", 2);
            graph.AddEdge("d", "s", 3);
            graph.AddEdge("c", "f", 1);
            graph.AddEdge("c", "v", 2);
            graph.AddEdge("c", "d", 3);
            graph.AddEdge("v", "f", 1);
            graph.AddEdge("f", "c", 2);

            var allEdges = graph.Edges.ToList();

            Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count.");
            Debug.Assert(graph.EdgesCount == 14, "Wrong edges count.");
            Debug.Assert(graph.EdgesCount == allEdges.Count, "Wrong edges count.");

            Debug.Assert(graph.OutgoingEdges("a").ToList().Count == 2, "Wrong outgoing edges from 'a'.");
            Debug.Assert(graph.OutgoingEdges("s").ToList().Count == 1, "Wrong outgoing edges from 's'.");
            Debug.Assert(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'.");
            Debug.Assert(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'.");
            Debug.Assert(graph.OutgoingEdges("c").ToList().Count == 3, "Wrong outgoing edges from 'c'.");
            Debug.Assert(graph.OutgoingEdges("v").ToList().Count == 1, "Wrong outgoing edges from 'v'.");
            Debug.Assert(graph.OutgoingEdges("f").ToList().Count == 1, "Wrong outgoing edges from 'f'.");
            Debug.Assert(graph.OutgoingEdges("z").ToList().Count == 0, "Wrong outgoing edges from 'z'.");

            Debug.Assert(graph.IncomingEdges("a").ToList().Count == 1, "Wrong incoming edges from 'a'.");
            Debug.Assert(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'.");
            Debug.Assert(graph.IncomingEdges("d").ToList().Count == 2, "Wrong incoming edges from 'd'.");
            Debug.Assert(graph.IncomingEdges("x").ToList().Count == 1, "Wrong incoming edges from 'x'.");
            Debug.Assert(graph.IncomingEdges("c").ToList().Count == 3, "Wrong incoming edges from 'c'.");
            Debug.Assert(graph.IncomingEdges("v").ToList().Count == 1, "Wrong incoming edges from 'v'.");
            Debug.Assert(graph.IncomingEdges("f").ToList().Count == 3, "Wrong incoming edges from 'f'.");
            Debug.Assert(graph.IncomingEdges("z").ToList().Count == 1, "Wrong incoming edges from 'z'.");

            Console.WriteLine("[*] Directed Weighted Sparse Graph:");
            Console.WriteLine("Graph nodes and edges:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // ASSERT RANDOMLY SELECTED EDGES
            var f_to_c        = graph.HasEdge("f", "c");
            var f_to_c_weight = graph.GetEdgeWeight("f", "c");

            Debug.Assert(f_to_c == true, "Edge f->c doesn't exist.");
            Debug.Assert(f_to_c_weight == 2, "Edge f->c must have a weight of 2.");
            Console.WriteLine("Is there an edge from f to c? " + f_to_c + ". If yes it's weight is: " + f_to_c_weight + ".");

            // ASSERT RANDOMLY SELECTED EDGES
            var d_to_s        = graph.HasEdge("d", "s");
            var d_to_s_weight = graph.GetEdgeWeight("d", "s");

            Debug.Assert(d_to_s == true, "Edge d->s doesn't exist.");
            Debug.Assert(d_to_s_weight == 3, "Edge d->s must have a weight of 3.");
            Console.WriteLine("Is there an edge from d to d? " + d_to_s + ". If yes it's weight is: " + d_to_s_weight + ".");

            Console.WriteLine();

            // TRY ADDING DUPLICATE EDGES BUT WITH DIFFERENT WEIGHTS
            var add_d_to_s_status = graph.AddEdge("d", "s", 6);

            Debug.Assert(add_d_to_s_status == false, "Error! Added a duplicate edge.");

            var add_c_to_f_status = graph.AddEdge("c", "f", 12);

            Debug.Assert(add_c_to_f_status == false, "Error! Added a duplicate edge.");

            var add_s_to_x_status = graph.AddEdge("s", "x", 123);

            Debug.Assert(add_s_to_x_status == false, "Error! Added a duplicate edge.");

            var add_x_to_d_status = graph.AddEdge("x", "d", 34);

            Debug.Assert(add_x_to_d_status == false, "Error! Added a duplicate edge.");

            // TEST DELETING EDGES
            graph.RemoveEdge("d", "c");
            Debug.Assert(graph.HasEdge("d", "c") == false, "Error! The edge d->c was deleted.");

            graph.RemoveEdge("c", "v");
            Debug.Assert(graph.HasEdge("c", "v") == false, "Error! The edge c->v was deleted.");

            graph.RemoveEdge("a", "z");
            Debug.Assert(graph.HasEdge("a", "z") == false, "Error! The edge a->z was deleted.");

            // ASSERT VERTICES AND EDGES COUNT
            Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count.");
            Debug.Assert(graph.EdgesCount == 11, "Wrong edges count.");

            Console.WriteLine("After removing edges (d-c), (c-v), (a-z):");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // TEST DELETING VERTICES
            graph.RemoveVertex("x");
            Debug.Assert(graph.HasEdge("x", "a") == false, "Error! The edge x->a was deleted because vertex x was deleted.");

            // ASSERT VERTICES AND EDGES COUNT
            Debug.Assert(graph.VerticesCount == 7, "Wrong vertices count.");
            Debug.Assert(graph.EdgesCount == 7, "Wrong edges count.");

            Console.WriteLine("After removing node (x):");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            graph.AddVertex("x");
            graph.AddEdge("s", "x", 3);
            graph.AddEdge("x", "d", 1);
            graph.AddEdge("x", "c", 2);
            graph.AddEdge("x", "a", 3);
            graph.AddEdge("d", "c", 2);
            graph.AddEdge("c", "v", 2);
            graph.AddEdge("a", "z", 2);
            Console.WriteLine("Re-added the deleted vertices and edges to the graph.");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // BFS from A
            Console.WriteLine("Walk the graph using BFS from A:");
            var bfsWalk = graph.BreadthFirstWalk("a");          // output: (s) (a) (x) (z) (d) (c) (f) (v)

            foreach (var node in bfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }
            Console.WriteLine("\r\n");

            // DFS from A
            Console.WriteLine("Walk the graph using DFS from A:");
            var dfsWalk = graph.DepthFirstWalk("a");            // output: (s) (a) (x) (z) (d) (c) (f) (v)

            foreach (var node in dfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }
            Console.WriteLine("\r\n");

            // BFS from F
            Console.WriteLine("Walk the graph using BFS from F:");
            bfsWalk = graph.BreadthFirstWalk("f");              // output: (s) (a) (x) (z) (d) (c) (f) (v)
            foreach (var node in bfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }
            Console.WriteLine("\r\n");

            // DFS from F
            Console.WriteLine("Walk the graph using DFS from F:");
            dfsWalk = graph.DepthFirstWalk("f");                // output: (s) (a) (x) (z) (d) (c) (f) (v)
            foreach (var node in dfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }
            Console.WriteLine("\r\n");

            Console.ReadLine();


            /********************************************************************/


            Console.WriteLine("***************************************************\r\n");

            graph.Clear();
            Console.WriteLine("Cleared the graph from all vertices and edges.\r\n");

            var verticesSet2 = new string[] { "a", "b", "c", "d", "e", "f" };

            graph.AddVertices(verticesSet2);

            graph.AddEdge("a", "b", 1);
            graph.AddEdge("a", "d", 2);
            graph.AddEdge("b", "e", 3);
            graph.AddEdge("d", "b", 1);
            graph.AddEdge("d", "e", 2);
            graph.AddEdge("e", "c", 3);
            graph.AddEdge("c", "f", 1);
            graph.AddEdge("f", "f", 1);

            Debug.Assert(graph.VerticesCount == 6, "Wrong vertices count.");
            Debug.Assert(graph.EdgesCount == 8, "Wrong edges count.");

            Console.WriteLine("[*] NEW Directed Weighted Sparse Graph:");
            Console.WriteLine("Graph nodes and edges:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            Console.WriteLine("Walk the graph using DFS:");
            dfsWalk = graph.DepthFirstWalk();           // output: (a) (b) (e) (d) (c) (f)
            foreach (var node in dfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }

            Console.ReadLine();
        }
Ejemplo n.º 7
0
        public static void DoTest()
        {
            string[] V;
            IEnumerable <WeightedEdge <string> > E;
            DirectedWeightedSparseGraph <string> graph;
            BellmanFordShortestPaths <DirectedWeightedSparseGraph <string>, string> BellmanFord;

            // Init graph object
            graph = new DirectedWeightedSparseGraph <string>();

            // Init V
            V = new string[6] {
                "r", "s", "t", "x", "y", "z"
            };

            // Insert V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert E
            var status = graph.AddEdge("r", "s", -3);

            Debug.Assert(status == true);

            status = graph.AddEdge("s", "t", 4);
            Debug.Assert(status == true);

            status = graph.AddEdge("t", "x", -1);
            Debug.Assert(status == true);

            status = graph.AddEdge("x", "y", 6);
            Debug.Assert(status == true);

            status = graph.AddEdge("y", "z", -1);
            Debug.Assert(status == true);

            status = graph.AddEdge("r", "t", 1);
            Debug.Assert(status == true);

            status = graph.AddEdge("s", "x", 7);
            Debug.Assert(status == true);

            status = graph.AddEdge("t", "y", 4);
            Debug.Assert(status == true);

            status = graph.AddEdge("t", "z", 3);
            Debug.Assert(status == true);

            status = graph.AddEdge("x", "z", 2);
            Debug.Assert(status == true);

            // NEGATIVE CYCLE (BACK-EDGE)
            status = graph.AddEdge("y", "t", -7);
            Debug.Assert(status == true);


            // Get E
            E = graph.Edges;
            Debug.Assert(graph.EdgesCount == 11, "Wrong Edges Count.");

            //
            // PRINT THE GRAPH
            Console.Write("[*] BELLMAN-FORD ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // WILL THROW EXCEPTION
            try
            {
                BellmanFord = new BellmanFordShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "s");
            }
            catch (Exception ex)
            {
                status = graph.RemoveEdge("y", "t");
                //Debug.Assert(status == true, "Error! Edge was not deleted.");

                BellmanFord = new BellmanFordShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "s");
            }

            //Debug.Assert(graph.HasEdge("y", "t") == false, "Wrong, edge y-t must have been deleted.");

            Debug.Assert(BellmanFord.HasPathTo("r") == false);
            Debug.Assert(BellmanFord.HasPathTo("z") == true);

            // Get shortest path to Z
            var pathToZ = string.Empty;

            foreach (var node in BellmanFord.ShortestPathTo("z"))
            {
                pathToZ = String.Format("{0}({1}) -> ", pathToZ, node);
            }
            pathToZ = pathToZ.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path to node 'z': " + pathToZ + "\r\n");

            var pathToY = string.Empty;

            foreach (var node in BellmanFord.ShortestPathTo("y"))
            {
                pathToY = String.Format("{0}({1}) -> ", pathToY, node);
            }
            pathToY = pathToY.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path to node 'y': " + pathToY + "\r\n");

            Console.WriteLine("*********************************************\r\n");


            /***************************************************************************************/


            // Clear the graph and insert new V and E to the instance
            graph.Clear();

            V = new string[] { "s", "a", "b", "c", "d" };

            // Insert new values of V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert new value for edges
            status = graph.AddEdge("s", "a", 1);
            Debug.Assert(status == true);
            status = graph.AddEdge("a", "b", 1);
            Debug.Assert(status == true);
            status = graph.AddEdge("b", "c", 2);
            Debug.Assert(status == true);
            status = graph.AddEdge("c", "a", -5);
            Debug.Assert(status == true);
            status = graph.AddEdge("c", "d", 2);
            Debug.Assert(status == true);

            Debug.Assert(graph.EdgesCount == 5, "Wrong Edges Count.");

            // PRINT THE GRAPH
            Console.Write("[*] BELLMAN-FORD ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // WILL THROW EXCEPTION
            try
            {
                BellmanFord = new BellmanFordShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "b");
            }
            catch (Exception ex)
            {
                status = graph.RemoveEdge("c", "a");
                //Debug.Assert(status == true, "Error! Edge was not deleted.");

                BellmanFord = new BellmanFordShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "b");
            }

            Console.WriteLine("*********************************************\r\n");


            /***************************************************************************************/


            // Clear the graph and insert new V and E to the instance
            graph.Clear();

            V = new string[] { "A", "B", "C", "D", "E" };

            // Insert new values of V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert new value for edges
            status = graph.AddEdge("A", "C", 7);
            Debug.Assert(status == true);
            status = graph.AddEdge("B", "A", 19);
            Debug.Assert(status == true);
            status = graph.AddEdge("B", "C", 11);
            Debug.Assert(status == true);
            status = graph.AddEdge("C", "E", 5);
            Debug.Assert(status == true);
            status = graph.AddEdge("C", "D", 15);
            Debug.Assert(status == true);
            status = graph.AddEdge("D", "B", 4);
            Debug.Assert(status == true);
            status = graph.AddEdge("E", "D", 13);
            Debug.Assert(status == true);

            Debug.Assert(graph.EdgesCount == 7, "Wrong Edges Count.");

            //
            // PRINT THE GRAPH
            Console.Write("[*] BELLMAN-FORD ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // Init BELLMAN-FORD
            BellmanFord = new BellmanFordShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "A");

            var pathToD = string.Empty;

            foreach (var node in BellmanFord.ShortestPathTo("D"))
            {
                pathToD = String.Format("{0}({1}) -> ", pathToD, node);
            }
            pathToD = pathToD.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path from 'A' to 'D': " + pathToD + "\r\n");

            Console.WriteLine("*********************************************\r\n");


            /***************************************************************************************/


            //var dijkstraAllPairs = new DijkstraAllPairsShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph);

            //var vertices = graph.Vertices;

            //Console.WriteLine("Dijkstra All Pairs Shortest Paths: \r\n");

            //foreach (var source in vertices)
            //{
            //    foreach (var destination in vertices)
            //    {
            //        var shortestPath = string.Empty;
            //        foreach (var node in dijkstraAllPairs.ShortestPath(source, destination))
            //            shortestPath = String.Format("{0}({1}) -> ", shortestPath, node);

            //        shortestPath = shortestPath.TrimEnd(new char[] { ' ', '-', '>' });

            //        Console.WriteLine("Shortest path from '" + source + "' to '" + destination + "' is: " + shortestPath + "\r\n");
            //    }
            //}

            Console.ReadLine();
        }
        public static void DoTest()
        {
            string[] V;
            IEnumerable<WeightedEdge<string>> E;
            DirectedWeightedSparseGraph<string> graph;
            DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string> dijkstra;

            // Init graph object
            graph = new DirectedWeightedSparseGraph<string>();

            // Init V
            V = new string[6] { "r", "s", "t", "x", "y", "z" };

            // Insert V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert E
            var status = graph.AddEdge("r", "s", 7);
            Debug.Assert(status == true);
            status = graph.AddEdge("r", "t", 6);
            Debug.Assert(status == true);
            status = graph.AddEdge("s", "t", 5);
            Debug.Assert(status == true);
            status = graph.AddEdge("s", "x", 9);
            Debug.Assert(status == true);
            status = graph.AddEdge("t", "x", 10);
            Debug.Assert(status == true);
            status = graph.AddEdge("t", "y", 7);
            Debug.Assert(status == true);
            status = graph.AddEdge("t", "z", 5);
            Debug.Assert(status == true);
            status = graph.AddEdge("x", "y", 2);
            Debug.Assert(status == true);
            status = graph.AddEdge("x", "z", 4);
            Debug.Assert(status == true);
            status = graph.AddEdge("y", "z", 1);
            Debug.Assert(status == true);

            // Get E
            E = graph.Edges;
            Debug.Assert(graph.EdgesCount == 10, "Wrong Edges Count.");

            //
            // PRINT THE GRAPH
            Console.Write("[*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // Init DIJKSTRA
            dijkstra = new DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "s");

            Debug.Assert(dijkstra.HasPathTo("r") == false);
            Debug.Assert(dijkstra.HasPathTo("z") == true);

            // Get shortest path to Z
            var pathToZ = string.Empty;
            foreach (var node in dijkstra.ShortestPathTo("z"))
                pathToZ = String.Format("{0}({1}) -> ", pathToZ, node);
            pathToZ = pathToZ.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path to node 'z': " + pathToZ + "\r\n");

            var pathToY = string.Empty;
            foreach (var node in dijkstra.ShortestPathTo("y"))
                pathToY = String.Format("{0}({1}) -> ", pathToY, node);
            pathToY = pathToY.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path to node 'y': " + pathToY + "\r\n");

            Console.WriteLine("*********************************************\r\n");

            /***************************************************************************************/

            // Clear the graph and insert new V and E to the instance
            graph.Clear();

            V = new string[] { "A", "B", "C", "D", "E" };

            // Insert new values of V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert new value for edges
            status = graph.AddEdge("A", "C", 7);
            Debug.Assert(status == true);
            status = graph.AddEdge("B", "A", 19);
            Debug.Assert(status == true);
            status = graph.AddEdge("B", "C", 11);
            Debug.Assert(status == true);
            status = graph.AddEdge("C", "E", 5);
            Debug.Assert(status == true);
            status = graph.AddEdge("C", "D", 15);
            Debug.Assert(status == true);
            status = graph.AddEdge("D", "B", 4);
            Debug.Assert(status == true);
            status = graph.AddEdge("E", "D", 13);
            Debug.Assert(status == true);

            Debug.Assert(graph.EdgesCount == 7, "Wrong Edges Count.");

            //
            // PRINT THE GRAPH
            Console.Write("[*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // Init DIJKSTRA
            dijkstra = new DijkstraShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "A");

            var pathToD = string.Empty;
            foreach (var node in dijkstra.ShortestPathTo("D"))
                pathToD = String.Format("{0}({1}) -> ", pathToD, node);
            pathToD = pathToD.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path from 'A' to 'D': " + pathToD + "\r\n");

            Console.ReadLine();
        }
        public static void DoTest()
        {
            var graph = new DirectedWeightedSparseGraph<string>();

            var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" };

            graph.AddVertices(verticesSet1);

            graph.AddEdge("a", "s", 1);
            graph.AddEdge("a", "z", 2);
            graph.AddEdge("s", "x", 3);
            graph.AddEdge("x", "d", 1);
            graph.AddEdge("x", "c", 2);
            graph.AddEdge("x", "a", 3);
            graph.AddEdge("d", "f", 1);
            graph.AddEdge("d", "c", 2);
            graph.AddEdge("d", "s", 3);
            graph.AddEdge("c", "f", 1);
            graph.AddEdge("c", "v", 2);
            graph.AddEdge("c", "d", 3);
            graph.AddEdge("v", "f", 1);
            graph.AddEdge("f", "c", 2);

            var allEdges = graph.Edges.ToList();

            Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count.");
            Debug.Assert(graph.EdgesCount == 14, "Wrong edges count.");
            Debug.Assert(graph.EdgesCount == allEdges.Count, "Wrong edges count.");

            Debug.Assert(graph.OutgoingEdges("a").ToList().Count == 2, "Wrong outgoing edges from 'a'.");
            Debug.Assert(graph.OutgoingEdges("s").ToList().Count == 1, "Wrong outgoing edges from 's'.");
            Debug.Assert(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'.");
            Debug.Assert(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'.");
            Debug.Assert(graph.OutgoingEdges("c").ToList().Count == 3, "Wrong outgoing edges from 'c'.");
            Debug.Assert(graph.OutgoingEdges("v").ToList().Count == 1, "Wrong outgoing edges from 'v'.");
            Debug.Assert(graph.OutgoingEdges("f").ToList().Count == 1, "Wrong outgoing edges from 'f'.");
            Debug.Assert(graph.OutgoingEdges("z").ToList().Count == 0, "Wrong outgoing edges from 'z'.");

            Debug.Assert(graph.IncomingEdges("a").ToList().Count == 1, "Wrong incoming edges from 'a'.");
            Debug.Assert(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'.");
            Debug.Assert(graph.IncomingEdges("d").ToList().Count == 2, "Wrong incoming edges from 'd'.");
            Debug.Assert(graph.IncomingEdges("x").ToList().Count == 1, "Wrong incoming edges from 'x'.");
            Debug.Assert(graph.IncomingEdges("c").ToList().Count == 3, "Wrong incoming edges from 'c'.");
            Debug.Assert(graph.IncomingEdges("v").ToList().Count == 1, "Wrong incoming edges from 'v'.");
            Debug.Assert(graph.IncomingEdges("f").ToList().Count == 3, "Wrong incoming edges from 'f'.");
            Debug.Assert(graph.IncomingEdges("z").ToList().Count == 1, "Wrong incoming edges from 'z'.");

            Console.WriteLine("[*] Directed Weighted Sparse Graph:");
            Console.WriteLine("Graph nodes and edges:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // ASSERT RANDOMLY SELECTED EDGES
            var f_to_c = graph.HasEdge("f", "c");
            var f_to_c_weight = graph.GetEdgeWeight("f", "c");
            Debug.Assert(f_to_c == true, "Edge f->c doesn't exist.");
            Debug.Assert(f_to_c_weight == 2, "Edge f->c must have a weight of 2.");
            Console.WriteLine("Is there an edge from f to c? " + f_to_c + ". If yes it's weight is: " + f_to_c_weight + ".");

            // ASSERT RANDOMLY SELECTED EDGES
            var d_to_s = graph.HasEdge("d", "s");
            var d_to_s_weight = graph.GetEdgeWeight("d", "s");
            Debug.Assert(d_to_s == true, "Edge d->s doesn't exist.");
            Debug.Assert(d_to_s_weight == 3, "Edge d->s must have a weight of 3.");
            Console.WriteLine("Is there an edge from d to d? " + d_to_s + ". If yes it's weight is: " + d_to_s_weight + ".");

            Console.WriteLine();

            // TRY ADDING DUPLICATE EDGES BUT WITH DIFFERENT WEIGHTS
            var add_d_to_s_status = graph.AddEdge("d", "s", 6);
            Debug.Assert(add_d_to_s_status == false, "Error! Added a duplicate edge.");

            var add_c_to_f_status = graph.AddEdge("c", "f", 12);
            Debug.Assert(add_c_to_f_status == false, "Error! Added a duplicate edge.");

            var add_s_to_x_status = graph.AddEdge("s", "x", 123);
            Debug.Assert(add_s_to_x_status == false, "Error! Added a duplicate edge.");

            var add_x_to_d_status = graph.AddEdge("x", "d", 34);
            Debug.Assert(add_x_to_d_status == false, "Error! Added a duplicate edge.");

            // TEST DELETING EDGES
            graph.RemoveEdge("d", "c");
            Debug.Assert(graph.HasEdge("d", "c") == false, "Error! The edge d->c was deleted.");
            
            graph.RemoveEdge("c", "v");
            Debug.Assert(graph.HasEdge("c", "v") == false, "Error! The edge c->v was deleted.");
            
            graph.RemoveEdge("a", "z");
            Debug.Assert(graph.HasEdge("a", "z") == false, "Error! The edge a->z was deleted.");

            // ASSERT VERTICES AND EDGES COUNT
            Debug.Assert(graph.VerticesCount == 8, "Wrong vertices count.");
            Debug.Assert(graph.EdgesCount == 11, "Wrong edges count.");

            Console.WriteLine("After removing edges (d-c), (c-v), (a-z):");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // TEST DELETING VERTICES
            graph.RemoveVertex("x");
            Debug.Assert(graph.HasEdge("x", "a") == false, "Error! The edge x->a was deleted because vertex x was deleted.");

            // ASSERT VERTICES AND EDGES COUNT
            Debug.Assert(graph.VerticesCount == 7, "Wrong vertices count.");
            Debug.Assert(graph.EdgesCount == 7, "Wrong edges count.");

            Console.WriteLine("After removing node (x):");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            graph.AddVertex("x");
            graph.AddEdge("s", "x", 3);
            graph.AddEdge("x", "d", 1);
            graph.AddEdge("x", "c", 2);
            graph.AddEdge("x", "a", 3);
            graph.AddEdge("d", "c", 2);
            graph.AddEdge("c", "v", 2);
            graph.AddEdge("a", "z", 2);
            Console.WriteLine("Re-added the deleted vertices and edges to the graph.");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // BFS from A
            Console.WriteLine("Walk the graph using BFS from A:");
            var bfsWalk = graph.BreadthFirstWalk("a");		// output: (s) (a) (x) (z) (d) (c) (f) (v)
            foreach (var node in bfsWalk) Console.Write(String.Format("({0})", node));
            Console.WriteLine("\r\n");

            // DFS from A
            Console.WriteLine("Walk the graph using DFS from A:");
            var dfsWalk = graph.DepthFirstWalk("a");		// output: (s) (a) (x) (z) (d) (c) (f) (v)
            foreach (var node in dfsWalk) Console.Write(String.Format("({0})", node));
            Console.WriteLine("\r\n");

            // BFS from F
            Console.WriteLine("Walk the graph using BFS from F:");
            bfsWalk = graph.BreadthFirstWalk("f");		// output: (s) (a) (x) (z) (d) (c) (f) (v)
            foreach (var node in bfsWalk) Console.Write(String.Format("({0})", node));
            Console.WriteLine("\r\n");

            // DFS from F
            Console.WriteLine("Walk the graph using DFS from F:");
            dfsWalk = graph.DepthFirstWalk("f");		// output: (s) (a) (x) (z) (d) (c) (f) (v)
            foreach (var node in dfsWalk) Console.Write(String.Format("({0})", node));
            Console.WriteLine("\r\n");

            Console.ReadLine();


            /********************************************************************/


            Console.WriteLine("***************************************************\r\n");

            graph.Clear();
            Console.WriteLine("Cleared the graph from all vertices and edges.\r\n");

            var verticesSet2 = new string[] { "a", "b", "c", "d", "e", "f" };

            graph.AddVertices(verticesSet2);

            graph.AddEdge("a", "b", 1);
            graph.AddEdge("a", "d", 2);
            graph.AddEdge("b", "e", 3);
            graph.AddEdge("d", "b", 1);
            graph.AddEdge("d", "e", 2);
            graph.AddEdge("e", "c", 3);
            graph.AddEdge("c", "f", 1);
            graph.AddEdge("f", "f", 1);

            Debug.Assert(graph.VerticesCount == 6, "Wrong vertices count.");
            Debug.Assert(graph.EdgesCount == 8, "Wrong edges count.");

            Console.WriteLine("[*] NEW Directed Weighted Sparse Graph:");
            Console.WriteLine("Graph nodes and edges:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            Console.WriteLine("Walk the graph using DFS:");
            dfsWalk = graph.DepthFirstWalk();		// output: (a) (b) (e) (d) (c) (f) 
            foreach (var node in dfsWalk) Console.Write(String.Format("({0})", node));

            Console.ReadLine();

        }
        public static void DoTest()
        {
            string[] V;
            IEnumerable<WeightedEdge<string>> E;
            DirectedWeightedSparseGraph<string> graph;
            BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string> BellmanFord;

            // Init graph object
            graph = new DirectedWeightedSparseGraph<string>();

            // Init V
            V = new string[6] { "r", "s", "t", "x", "y", "z" };

            // Insert V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert E
            var status = graph.AddEdge("r", "s", -3); 
            Debug.Assert(status == true);

            status = graph.AddEdge("s", "t", 4);
            Debug.Assert(status == true);

            status = graph.AddEdge("t", "x", -1);
            Debug.Assert(status == true);

            status = graph.AddEdge("x", "y", 6);
            Debug.Assert(status == true);

            status = graph.AddEdge("y", "z", -1);
            Debug.Assert(status == true);

            status = graph.AddEdge("r", "t", 1);
            Debug.Assert(status == true);

            status = graph.AddEdge("s", "x", 7);
            Debug.Assert(status == true);

            status = graph.AddEdge("t", "y", 4);
            Debug.Assert(status == true);

            status = graph.AddEdge("t", "z", 3);
            Debug.Assert(status == true);

            status = graph.AddEdge("x", "z", 2);
            Debug.Assert(status == true);

            // NEGATIVE CYCLE (BACK-EDGE)
            status = graph.AddEdge("y", "t", -7);
            Debug.Assert(status == true);


            // Get E
            E = graph.Edges;
            Debug.Assert(graph.EdgesCount == 11, "Wrong Edges Count.");

            //
            // PRINT THE GRAPH
            Console.Write("[*] BELLMAN-FORD ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // WILL THROW EXCEPTION
            try
            {
                BellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "s");
            }
            catch(Exception ex)
            {
                status = graph.RemoveEdge("y", "t");
                //Debug.Assert(status == true, "Error! Edge was not deleted.");

                BellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "s");
            }

            //Debug.Assert(graph.HasEdge("y", "t") == false, "Wrong, edge y-t must have been deleted.");

            Debug.Assert(BellmanFord.HasPathTo("r") == false);
            Debug.Assert(BellmanFord.HasPathTo("z") == true);

            // Get shortest path to Z
            var pathToZ = string.Empty;
            foreach (var node in BellmanFord.ShortestPathTo("z"))
                pathToZ = String.Format("{0}({1}) -> ", pathToZ, node);
            pathToZ = pathToZ.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path to node 'z': " + pathToZ + "\r\n");

            var pathToY = string.Empty;
            foreach (var node in BellmanFord.ShortestPathTo("y"))
                pathToY = String.Format("{0}({1}) -> ", pathToY, node);
            pathToY = pathToY.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path to node 'y': " + pathToY + "\r\n");

            Console.WriteLine("*********************************************\r\n");


            /***************************************************************************************/


            // Clear the graph and insert new V and E to the instance
            graph.Clear();

            V = new string[] { "s", "a", "b", "c", "d" };

            // Insert new values of V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert new value for edges
            status = graph.AddEdge("s", "a", 1);
            Debug.Assert(status == true);
            status = graph.AddEdge("a", "b", 1);
            Debug.Assert(status == true);
            status = graph.AddEdge("b", "c", 2);
            Debug.Assert(status == true);
            status = graph.AddEdge("c", "a", -5);
            Debug.Assert(status == true);
            status = graph.AddEdge("c", "d", 2);
            Debug.Assert(status == true);

            Debug.Assert(graph.EdgesCount == 5, "Wrong Edges Count.");

            // PRINT THE GRAPH
            Console.Write("[*] BELLMAN-FORD ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // WILL THROW EXCEPTION
            try
            {
                BellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "b");
            }
            catch (Exception ex)
            {
                status = graph.RemoveEdge("c", "a");
                //Debug.Assert(status == true, "Error! Edge was not deleted.");

                BellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "b");
            }

            Console.WriteLine("*********************************************\r\n");


            /***************************************************************************************/
            
            
            // Clear the graph and insert new V and E to the instance
            graph.Clear();

            V = new string[] { "A", "B", "C", "D", "E" };

            // Insert new values of V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert new value for edges
            status = graph.AddEdge("A", "C", 7);
            Debug.Assert(status == true);
            status = graph.AddEdge("B", "A", 19);
            Debug.Assert(status == true);
            status = graph.AddEdge("B", "C", 11);
            Debug.Assert(status == true);
            status = graph.AddEdge("C", "E", 5);
            Debug.Assert(status == true);
            status = graph.AddEdge("C", "D", 15);
            Debug.Assert(status == true);
            status = graph.AddEdge("D", "B", 4);
            Debug.Assert(status == true);
            status = graph.AddEdge("E", "D", 13);
            Debug.Assert(status == true);

            Debug.Assert(graph.EdgesCount == 7, "Wrong Edges Count.");

            //
            // PRINT THE GRAPH
            Console.Write("[*] BELLMAN-FORD ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // Init BELLMAN-FORD
            BellmanFord = new BellmanFordShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph, "A");

            var pathToD = string.Empty;
            foreach (var node in BellmanFord.ShortestPathTo("D"))
                pathToD = String.Format("{0}({1}) -> ", pathToD, node);
            pathToD = pathToD.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path from 'A' to 'D': " + pathToD + "\r\n");

            Console.WriteLine("*********************************************\r\n");


            /***************************************************************************************/


            //var dijkstraAllPairs = new DijkstraAllPairsShortestPaths<DirectedWeightedSparseGraph<string>, string>(graph);

            //var vertices = graph.Vertices;

            //Console.WriteLine("Dijkstra All Pairs Shortest Paths: \r\n");

            //foreach (var source in vertices)
            //{
            //    foreach (var destination in vertices)
            //    {
            //        var shortestPath = string.Empty;
            //        foreach (var node in dijkstraAllPairs.ShortestPath(source, destination))
            //            shortestPath = String.Format("{0}({1}) -> ", shortestPath, node);

            //        shortestPath = shortestPath.TrimEnd(new char[] { ' ', '-', '>' });

            //        Console.WriteLine("Shortest path from '" + source + "' to '" + destination + "' is: " + shortestPath + "\r\n");
            //    }
            //}

            Console.ReadLine();
        }
        public static void DoTest()
        {
            string[] V;
            IEnumerable <WeightedEdge <string> > E;
            DirectedWeightedSparseGraph <string> graph;
            DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string> dijkstra;

            // Init graph object
            graph = new DirectedWeightedSparseGraph <string>();

            // Init V
            V = new string[6] {
                "r", "s", "t", "x", "y", "z"
            };

            // Insert V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert E
            var status = graph.AddEdge("r", "s", 7);

            Debug.Assert(status == true);
            status = graph.AddEdge("r", "t", 6);
            Debug.Assert(status == true);
            status = graph.AddEdge("s", "t", 5);
            Debug.Assert(status == true);
            status = graph.AddEdge("s", "x", 9);
            Debug.Assert(status == true);
            status = graph.AddEdge("t", "x", 10);
            Debug.Assert(status == true);
            status = graph.AddEdge("t", "y", 7);
            Debug.Assert(status == true);
            status = graph.AddEdge("t", "z", 5);
            Debug.Assert(status == true);
            status = graph.AddEdge("x", "y", 2);
            Debug.Assert(status == true);
            status = graph.AddEdge("x", "z", 4);
            Debug.Assert(status == true);
            status = graph.AddEdge("y", "z", 1);
            Debug.Assert(status == true);

            // Get E
            E = graph.Edges;
            Debug.Assert(graph.EdgesCount == 10, "Wrong Edges Count.");

            //
            // PRINT THE GRAPH
            Console.Write("[*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // Init DIJKSTRA
            dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "s");

            Debug.Assert(dijkstra.HasPathTo("r") == false);
            Debug.Assert(dijkstra.HasPathTo("z") == true);

            // Get shortest path to Z
            var pathToZ = string.Empty;

            foreach (var node in dijkstra.ShortestPathTo("z"))
            {
                pathToZ = String.Format("{0}({1}) -> ", pathToZ, node);
            }
            pathToZ = pathToZ.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path to node 'z': " + pathToZ + "\r\n");

            var pathToY = string.Empty;

            foreach (var node in dijkstra.ShortestPathTo("y"))
            {
                pathToY = String.Format("{0}({1}) -> ", pathToY, node);
            }
            pathToY = pathToY.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path to node 'y': " + pathToY + "\r\n");

            Console.WriteLine("*********************************************\r\n");


            /***************************************************************************************/


            // Clear the graph and insert new V and E to the instance
            graph.Clear();

            V = new string[] { "A", "B", "C", "D", "E" };

            // Insert new values of V
            graph.AddVertices(V);
            Debug.Assert(graph.VerticesCount == V.Length, "Wrong Vertices Count.");

            // Insert new value for edges
            status = graph.AddEdge("A", "C", 7);
            Debug.Assert(status == true);
            status = graph.AddEdge("B", "A", 19);
            Debug.Assert(status == true);
            status = graph.AddEdge("B", "C", 11);
            Debug.Assert(status == true);
            status = graph.AddEdge("C", "E", 5);
            Debug.Assert(status == true);
            status = graph.AddEdge("C", "D", 15);
            Debug.Assert(status == true);
            status = graph.AddEdge("D", "B", 4);
            Debug.Assert(status == true);
            status = graph.AddEdge("E", "D", 13);
            Debug.Assert(status == true);

            Debug.Assert(graph.EdgesCount == 7, "Wrong Edges Count.");

            //
            // PRINT THE GRAPH
            Console.Write("[*] DIJKSTRA ON DIRECTED WEIGHTED GRAPH - TEST 01:\r\n");

            Console.WriteLine("Graph representation:");
            Console.WriteLine(graph.ToReadable() + "\r\n");

            // Init DIJKSTRA
            dijkstra = new DijkstraShortestPaths <DirectedWeightedSparseGraph <string>, string>(graph, "A");

            var pathToD = string.Empty;

            foreach (var node in dijkstra.ShortestPathTo("D"))
            {
                pathToD = String.Format("{0}({1}) -> ", pathToD, node);
            }
            pathToD = pathToD.TrimEnd(new char[] { ' ', '-', '>' });

            Console.WriteLine("Shortest path from 'A' to 'D': " + pathToD + "\r\n");

            Console.ReadLine();
        }
        public static void DoTest()
        {
            var graph = new DirectedWeightedSparseGraph <string>();

            var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" };

            graph.AddVertices(verticesSet1);

            graph.AddEdge("a", "s", 1);
            graph.AddEdge("a", "z", 2);
            graph.AddEdge("s", "x", 3);
            graph.AddEdge("x", "d", 1);
            graph.AddEdge("x", "c", 2);
            graph.AddEdge("x", "a", 3);
            graph.AddEdge("d", "f", 1);
            graph.AddEdge("d", "c", 2);
            graph.AddEdge("d", "s", 3);
            graph.AddEdge("c", "f", 1);
            graph.AddEdge("c", "v", 2);
            graph.AddEdge("c", "d", 3);
            graph.AddEdge("v", "f", 1);
            graph.AddEdge("f", "c", 2);

            var allEdges = graph.Edges.ToList();

            Assert.True(graph.VerticesCount == 8, "Wrong vertices count.");
            Assert.True(graph.EdgesCount == 14, "Wrong edges count.");
            Assert.True(graph.EdgesCount == allEdges.Count, "Wrong edges count.");

            Assert.True(graph.OutgoingEdges("a").ToList().Count == 2, "Wrong outgoing edges from 'a'.");
            Assert.True(graph.OutgoingEdges("s").ToList().Count == 1, "Wrong outgoing edges from 's'.");
            Assert.True(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'.");
            Assert.True(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'.");
            Assert.True(graph.OutgoingEdges("c").ToList().Count == 3, "Wrong outgoing edges from 'c'.");
            Assert.True(graph.OutgoingEdges("v").ToList().Count == 1, "Wrong outgoing edges from 'v'.");
            Assert.True(graph.OutgoingEdges("f").ToList().Count == 1, "Wrong outgoing edges from 'f'.");
            Assert.True(graph.OutgoingEdges("z").ToList().Count == 0, "Wrong outgoing edges from 'z'.");

            Assert.True(graph.IncomingEdges("a").ToList().Count == 1, "Wrong incoming edges from 'a'.");
            Assert.True(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'.");
            Assert.True(graph.IncomingEdges("d").ToList().Count == 2, "Wrong incoming edges from 'd'.");
            Assert.True(graph.IncomingEdges("x").ToList().Count == 1, "Wrong incoming edges from 'x'.");
            Assert.True(graph.IncomingEdges("c").ToList().Count == 3, "Wrong incoming edges from 'c'.");
            Assert.True(graph.IncomingEdges("v").ToList().Count == 1, "Wrong incoming edges from 'v'.");
            Assert.True(graph.IncomingEdges("f").ToList().Count == 3, "Wrong incoming edges from 'f'.");
            Assert.True(graph.IncomingEdges("z").ToList().Count == 1, "Wrong incoming edges from 'z'.");

            // ASSERT RANDOMLY SELECTED EDGES
            var f_to_c        = graph.HasEdge("f", "c");
            var f_to_c_weight = graph.GetEdgeWeight("f", "c");

            Assert.True(f_to_c == true, "Edge f->c doesn't exist.");
            Assert.True(f_to_c_weight == 2, "Edge f->c must have a weight of 2.");

            // ASSERT RANDOMLY SELECTED EDGES
            var d_to_s        = graph.HasEdge("d", "s");
            var d_to_s_weight = graph.GetEdgeWeight("d", "s");

            Assert.True(d_to_s == true, "Edge d->s doesn't exist.");
            Assert.True(d_to_s_weight == 3, "Edge d->s must have a weight of 3.");

            // TRY ADDING DUPLICATE EDGES BUT WITH DIFFERENT WEIGHTS
            var add_d_to_s_status = graph.AddEdge("d", "s", 6);

            Assert.True(add_d_to_s_status == false, "Error! Added a duplicate edge.");

            var add_c_to_f_status = graph.AddEdge("c", "f", 12);

            Assert.True(add_c_to_f_status == false, "Error! Added a duplicate edge.");

            var add_s_to_x_status = graph.AddEdge("s", "x", 123);

            Assert.True(add_s_to_x_status == false, "Error! Added a duplicate edge.");

            var add_x_to_d_status = graph.AddEdge("x", "d", 34);

            Assert.True(add_x_to_d_status == false, "Error! Added a duplicate edge.");

            // TEST DELETING EDGES
            graph.RemoveEdge("d", "c");
            Assert.True(graph.HasEdge("d", "c") == false, "Error! The edge d->c was deleted.");

            graph.RemoveEdge("c", "v");
            Assert.True(graph.HasEdge("c", "v") == false, "Error! The edge c->v was deleted.");

            graph.RemoveEdge("a", "z");
            Assert.True(graph.HasEdge("a", "z") == false, "Error! The edge a->z was deleted.");

            // ASSERT VERTICES AND EDGES COUNT
            Assert.True(graph.VerticesCount == 8, "Wrong vertices count.");
            Assert.True(graph.EdgesCount == 11, "Wrong edges count.");

            // TEST DELETING VERTICES
            graph.RemoveVertex("x");
            Assert.True(graph.HasEdge("x", "a") == false, "Error! The edge x->a was deleted because vertex x was deleted.");

            // ASSERT VERTICES AND EDGES COUNT
            Assert.True(graph.VerticesCount == 7, "Wrong vertices count.");
            Assert.True(graph.EdgesCount == 7, "Wrong edges count.");

            graph.AddVertex("x");
            graph.AddEdge("s", "x", 3);
            graph.AddEdge("x", "d", 1);
            graph.AddEdge("x", "c", 2);
            graph.AddEdge("x", "a", 3);
            graph.AddEdge("d", "c", 2);
            graph.AddEdge("c", "v", 2);
            graph.AddEdge("a", "z", 2);

            // BFS from A
            // Walk the graph using BFS from A:
            var bfsWalk = graph.BreadthFirstWalk("a");          // output: (s) (a) (x) (z) (d) (c) (f) (v)

            foreach (var node in bfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }

            // DFS from A
            // Walk the graph using DFS from A:
            var dfsWalk = graph.DepthFirstWalk("a");            // output: (s) (a) (x) (z) (d) (c) (f) (v)

            foreach (var node in dfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }

            // BFS from F
            // Walk the graph using BFS from F:
            bfsWalk = graph.BreadthFirstWalk("f");              // output: (s) (a) (x) (z) (d) (c) (f) (v)
            foreach (var node in bfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }

            // DFS from F
            Console.WriteLine("Walk the graph using DFS from F:");
            dfsWalk = graph.DepthFirstWalk("f");                // output: (s) (a) (x) (z) (d) (c) (f) (v)
            foreach (var node in dfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }

            /********************************************************************/

            graph.Clear();

            var verticesSet2 = new string[] { "a", "b", "c", "d", "e", "f" };

            graph.AddVertices(verticesSet2);

            graph.AddEdge("a", "b", 1);
            graph.AddEdge("a", "d", 2);
            graph.AddEdge("b", "e", 3);
            graph.AddEdge("d", "b", 1);
            graph.AddEdge("d", "e", 2);
            graph.AddEdge("e", "c", 3);
            graph.AddEdge("c", "f", 1);
            graph.AddEdge("f", "f", 1);

            Assert.True(graph.VerticesCount == 6, "Wrong vertices count.");
            Assert.True(graph.EdgesCount == 8, "Wrong edges count.");

            // Walk the graph using DFS:
            dfsWalk = graph.DepthFirstWalk();           // output: (a) (b) (e) (d) (c) (f)
            foreach (var node in dfsWalk)
            {
                Console.Write(String.Format("({0})", node));
            }
        }