示例#1
0
        public static void KurskalsAlg(GraphKruskal gk)
        {
            List<Edge> edges = gk.Edges.OrderBy(o => o.cost).ToList<Edge>();    //sort edges by weight/cost
            GraphKruskal mstGraph = new GraphKruskal();
            HashSet<int> verticesInMST = new HashSet<int>();

            int[] parent = new int[gk.Vertices.Count];
            for (int i = 0; i < parent.Length; i++)
                parent[i] = -1;

            foreach (Edge e in edges)
            {
                if(!(verticesInMST.Contains(e.v1) && verticesInMST.Contains(e.v2)))
                {
                    mstGraph.AddEdge(e.v1, e.v2, e.cost);
                    if (!verticesInMST.Contains(e.v1))
                        verticesInMST.Add(e.v1);

                    if (!verticesInMST.Contains(e.v2))
                        verticesInMST.Add(e.v2);
                }
            }
        }
示例#2
0
 private static void buildgraph(GraphKruskal gk)
 {
     gk.AddEdge(0, 1, 4);
     gk.AddEdge(0, 7, 8);
     gk.AddEdge(1, 7, 11);
     gk.AddEdge(1, 2, 8);
     gk.AddEdge(7, 8, 7);
     gk.AddEdge(7, 6, 1);
     gk.AddEdge(6, 8, 6);
     gk.AddEdge(6, 5, 2);
     gk.AddEdge(2, 8, 2);
     gk.AddEdge(2, 3, 7);
     gk.AddEdge(2, 5, 4);
     gk.AddEdge(5, 4, 10);
     gk.AddEdge(5, 3, 14);
     gk.AddEdge(3, 4, 9);
 }