Пример #1
0
        public static void DijkstraAlgorithmTest()
        {
            var graph = new UndirectedGenericGraph <int>();

            var one   = new Vertex <int>(1);
            var two   = new Vertex <int>(2);
            var three = new Vertex <int>(3);
            var four  = new Vertex <int>(4);
            var five  = new Vertex <int>(5);
            var six   = new Vertex <int>(6);

//            graph.AddPair(one, two, 6);
//            graph.AddPair(two, three, 6);
//            graph.AddPair(three, four, 6);
//            graph.AddPair(one, five, 6);
//            graph.AddToList(six);

            //graph.AddPair(one, two, 5);
            //graph.AddPair(two, three, 6);
            //graph.AddPair(three, four, 2);
            //graph.AddPair(one, three, 15);
            //graph.AddToList(five);

            graph.AddPair(one, two, 24);
            graph.AddPair(three, one, 3);
            graph.AddPair(one, four, 20);
            graph.AddPair(four, three, 12);

            var start     = one;
            var distances = DijkstraAlgorithm(graph, start);
        }
Пример #2
0
        public static void BfsShortestReachInaGraph(Vertex <int> start)
        {
            var graph = new UndirectedGenericGraph <int>();

            var one   = new Vertex <int>(1);
            var two   = new Vertex <int>(2);
            var three = new Vertex <int>(3);
            var four  = new Vertex <int>(4);
            var five  = new Vertex <int>(5);
            var six   = new Vertex <int>(6);

            graph.AddPair(one, two, 6);
            graph.AddPair(two, three, 6);
            graph.AddPair(three, four, 6);
            graph.AddPair(one, five, 6);
            graph.AddToList(six);

            var dict = graph.BreadthFirstSearchWeighted(graph.Vertices.FirstOrDefault(v => v.Value == start.Value));
            var res3 = graph.Result;

            //for (int i = 0; i < 6; i++)
            //{
            //    graph.BreadthFirstSearch(graph.Vertices[i]);
            //    var res3 = graph.Result;
            //}
        }
Пример #3
0
        public static void MinSpanTreeTest()
        {
            var graph = new UndirectedGenericGraph <int>();

            var one   = new Vertex <int>(1);
            var two   = new Vertex <int>(2);
            var three = new Vertex <int>(3);
            var four  = new Vertex <int>(4);
            var five  = new Vertex <int>(5);

            graph.AddPair(one, two, 5);
            graph.AddPair(one, three, 3);
            graph.AddPair(four, one, 6);
            graph.AddPair(two, four, 7);
            graph.AddPair(three, two, 4);
            graph.AddPair(three, four, 5);

            //graph.AddPair(one, two, 20);
            //graph.AddPair(one, three, 50);
            //graph.AddPair(one, four, 70);
            //graph.AddPair(one, five, 90);
            //graph.AddPair(two, three, 30);
            //graph.AddPair(three, four, 40);
            //graph.AddPair(four, five, 60);

            var cost  = KruskalAlgorithm(graph);
            var cost2 = PrimeAlgorithm(graph);
            var cost3 = DijkstraAlgorithmMinSpanTree(graph);//To be tested
        }
Пример #4
0
        public static void CheckVertices()
        {
            var la = new Vertex <string>("Los Angeles");
            var sf = new Vertex <string>("San Francisco");
            var lv = new Vertex <string>("Las Vegas");
            var se = new Vertex <string>("Seattle");
            var au = new Vertex <string>("Austin");
            var po = new Vertex <string>("Portland");

            var aa = new Vertex <string>("Addis Ababa");
            var ad = new Vertex <string>("Adama");

            var testGraph = new UndirectedGenericGraph <string>();

            // la <=> sf, lv, po
            testGraph.AddPair(la, sf);
            testGraph.AddPair(la, lv);
            testGraph.AddPair(la, po);

            // sf <=> se, po
            testGraph.AddPair(sf, se);
            testGraph.AddPair(sf, po);

            // lv <=> au
            testGraph.AddPair(lv, au);

            // se <=> po
            testGraph.AddPair(se, po);

            // aa <=> ad
            testGraph.AddToList(aa);
            testGraph.AddToList(ad);

            // Check to see that all neighbors are properly set up
            foreach (var vertex in testGraph.Vertices)
            {
                Console.WriteLine(vertex.ToString());
                //System.Diagnostics.Debug.WriteLine(vertex.ToString());
            }
            var resu = "";

            for (int i = 0; i < 8; i++)
            {
                //if (!testGraph.Vertices[i].IsVisited)
                //{
                //    testGraph.DepthFirstSearch(testGraph.Vertices[i]); //.FirstOrDefault(s=>s.Value=="Las Vegas"));
                //    var count = testGraph.Count;
                //    resu = testGraph.Result;
                //    testGraph.Result = "";
                //    testGraph.Count = 0;

                //}

                if (!testGraph.Vertices[i].IsVisited)
                {
                    testGraph.DepthFirstSearchStack(testGraph.Vertices[i]);
                    var res3 = testGraph.Result;
                }
            }
            testGraph.UnvisitAll();
            var resu2 = "";

            for (int i = 0; i < 8; i++)
            {
                if (!testGraph.Vertices[i].IsVisited)
                {
                    testGraph.BreadthFirstSearch(testGraph.Vertices[i]); //.FirstOrDefault(s=>s.Value=="Las Vegas"));
                    //var count = testGraph.Count;
                    resu2            = testGraph.Result;
                    testGraph.Result = "";
                    //testGraph.Count = 0;
                }
            }
        }