Esempio n. 1
0
        /// <summary>
        /// Main logic of Kruskal's MST algorithm using disjoint sets
        /// </summary>
        /// <param name="gr">Researched graph</param>
        /// <returns></returns>
        private static List <WeightedEdge> Kruskal_MST_sec(Graph gr)
        {
            List <WeightedEdge> tree = new List <WeightedEdge>();
            List <DJS>          sets = new List <DJS>();
            DJSUtils            ut   = new DJSUtils(sets);

            for (int i = 0; i < gr.Size; i++)
            {
                ut.MAKE_SET(i); //O(V)
            }

            WeightedEdge[] grE = gr.GetSortedEdges(); //O(E logE)

            foreach (WeightedEdge edge in grE)        //O(E)
            {
                //consider edge (u,v)
                int u    = edge.Src;
                int v    = edge.Dest;
                DJS uSet = ut.FIND_SET(u).Set;
                DJS vSet = ut.FIND_SET(v).Set;

                if (uSet != vSet)
                {
                    tree.Add(edge);
                    ut.UNION(uSet, vSet, (s1, s2) => {
                        if (s1.Size > s2.Size)
                        {
                            return(1);
                        }
                        if (s1.Size < s2.Size)
                        {
                            return(-1);
                        }
                        return(0);
                    });
                }
            }

            return(tree);
        }
Esempio n. 2
0
 public Node(DJS set, int vtx, Node nextNode)
 {
     Set      = set;
     Vtx      = vtx;
     NextNode = nextNode;
 }
Esempio n. 3
0
 public Node(DJS set, int vtx)
 {
     Set      = set;
     Vtx      = vtx;
     NextNode = null;
 }