Esempio n. 1
0
        public void Directed2()
        {
            var graph = new Graph <string>(true);

            var vertex1 = new Vertex <string>("a");
            var vertex2 = new Vertex <string>("b");
            var vertex3 = new Vertex <string>("c");
            var vertex4 = new Vertex <string>("d");
            var vertex5 = new Vertex <string>("e");
            var vertex6 = new Vertex <string>("f");

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);
            graph.AddVertex(vertex4);
            graph.AddVertex(vertex5);
            graph.AddVertex(vertex6);

            graph.AddEdge(vertex1, vertex2, 5);
            graph.AddEdge(vertex1, vertex3, 5);
            graph.AddEdge(vertex1, vertex4, 5);
            graph.AddEdge(vertex1, vertex5, 5);
            graph.AddEdge(vertex1, vertex6, 1);

            graph.AddEdge(vertex2, vertex3, 1);

            graph.AddEdge(vertex3, vertex4, 1);

            graph.AddEdge(vertex5, vertex4, 2);

            graph.AddEdge(vertex6, vertex5, 2);
            graph.AddEdge(vertex6, vertex2, 2);

            var resultGraph = GraphAlgorithms.DijkstrasAlgorithm(graph, vertex1);

            Assert.AreEqual(resultGraph.Vertices.Count, 6);
            Assert.AreEqual(resultGraph.Edges.Count, 5);

            var edges = GetEdgeList(resultGraph.Edges.GetEnumerator());

            foreach (var edge in edges)
            {
                if ((edge.FromVertex.Data == "a") && (edge.ToVertex.Data == "d"))
                {
                    Assert.AreEqual(edge.Weight, 5);
                    Assert.AreEqual(edge.FromVertex.Weight, 0);
                    Assert.AreEqual(edge.ToVertex.Weight, 5);
                }
                else if ((edge.FromVertex.Data == "a") && (edge.ToVertex.Data == "f"))
                {
                    Assert.AreEqual(edge.Weight, 1);
                    Assert.AreEqual(edge.FromVertex.Weight, 0);
                    Assert.AreEqual(edge.ToVertex.Weight, 1);
                }
                else if ((edge.FromVertex.Data == "b") && (edge.ToVertex.Data == "c"))
                {
                    Assert.AreEqual(edge.Weight, 1);
                    Assert.AreEqual(edge.FromVertex.Weight, 3);
                    Assert.AreEqual(edge.ToVertex.Weight, 4);
                }
                else if ((edge.FromVertex.Data == "f") && (edge.ToVertex.Data == "b"))
                {
                    Assert.AreEqual(edge.Weight, 2);
                    Assert.AreEqual(edge.FromVertex.Weight, 1);
                    Assert.AreEqual(edge.ToVertex.Weight, 3);
                }
                else if ((edge.FromVertex.Data == "f") && (edge.ToVertex.Data == "e"))
                {
                    Assert.AreEqual(edge.Weight, 2);
                    Assert.AreEqual(edge.FromVertex.Weight, 1);
                    Assert.AreEqual(edge.ToVertex.Weight, 3);
                }
                else
                {
                    throw new Exception("Incorrect edge found for shortest path.");
                }
            }
        }
Esempio n. 2
0
 public void ExceptionNullVertex()
 {
     GraphAlgorithms.DijkstrasAlgorithm(new Graph <int>(true), null);
 }
Esempio n. 3
0
 public void ExceptionInvalidVertex()
 {
     GraphAlgorithms.DijkstrasAlgorithm(new Graph <int>(true), new Vertex <int>(5));
 }
Esempio n. 4
0
 public void ExceptionNullGraph()
 {
     GraphAlgorithms.DijkstrasAlgorithm(null, new Vertex <int>(5));
 }