Пример #1
0
        public static int KruskalAlgorithm(UndirectedGenericGraph <int> graph)
        {
            var tree  = new List <Vertex <int> >();
            var edges = graph.Vertices.SelectMany(s => s.WeightedEdges).Distinct().OrderBy(w => w.Weight).ToList();

            tree.Add(edges[0].StartV);
            tree.Add(edges[0].EndV);
            var cost = edges[0].Weight;

            foreach (var weightedEdge in edges.Skip(1))
            {
                bool isfound = false;
                if (!tree.Contains(weightedEdge.StartV))
                {
                    tree.Add(weightedEdge.StartV);
                    isfound = true;
                }
                if (!tree.Contains(weightedEdge.EndV))
                {
                    tree.Add(weightedEdge.EndV);
                    isfound = true;
                }
                if (isfound)
                {
                    cost += weightedEdge.Weight;
                }
            }

            return(cost);
        }
Пример #2
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
        }
Пример #3
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);
        }
Пример #4
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;
            //}
        }
Пример #5
0
        public static List <int> DijkstraAlgorithm(UndirectedGenericGraph <int> graph, Vertex <int> start)
        {
            var vertices = graph.Vertices;
            var n        = vertices.Count;
            var currDist = new Dictionary <Vertex <int>, int>(n);

            foreach (var v in vertices)
            {
                currDist.Add(v, Int32.MaxValue);
            }

            currDist[start] = 0;

            var toBeChecked = new List <Vertex <int> >(vertices);

            while (toBeChecked.Count > 0)
            {
                var v = GetMinCurrDist(currDist, toBeChecked);

                toBeChecked.Remove(v);

                foreach (var u in vertices[v.Value - 1].Neighbors)
                {
                    if (toBeChecked.Contains(u))
                    {
                        var edgevu = v.WeightedEdges.FirstOrDefault(w => (w.StartV == v && w.EndV == u) || (w.StartV == u && w.EndV == v));

                        if (edgevu != null)
                        {
                            var weightuv = edgevu.Weight;
                            if (currDist[u] > currDist[v] + weightuv)
                            {
                                currDist[u] = currDist[v] + weightuv;
                            }
                        }
                    }
                }
            }

            //var count = toBeChecked.Count;
            var currDict = currDist.OrderBy(s => s.Key.Value).ToList();

            return(currDict.Select(s => s.Value).ToList());
            //return null;// currDict.Values.ToList().Skip(1).ToList();
        }
Пример #6
0
        public static int DijkstraAlgorithmMinSpanTree(UndirectedGenericGraph <int> graph)
        {
            var tree       = new List <Vertex <int> >();
            var weightTree = new List <WeightedEdge <int> >();
            var edges      = graph.Vertices.SelectMany(s => s.WeightedEdges).Distinct().ToList();//.OrderBy(w => w.Weight).ToList();

            weightTree.Add(edges[0]);
            weightTree.Add(edges[1]);

            foreach (var weightedEdge in edges.Skip(2))
            {
                weightTree.Add(weightedEdge);

                var wt = ThereIsCycleInTheTree(weightTree);
                if (wt != null)
                {
                    weightTree.Remove(wt);
                }
            }

            return(weightTree.Sum(s => s.Weight));
        }
Пример #7
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;
                }
            }
        }