Step 1 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t 0 1 2 3 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Kruskal's algorithm on the same 6 6 6 -node graph from example h 11 h11 h 11 . Sort every edge by weight, then scan from cheapest to most expensive. Accept an edge if its two endpoints sit in different DSU components; reject it if both are already connected (a cycle would form). A spanning tree on 6 6 6 nodes needs exactly 5 5 5 edges. Initial DSU: { A } , { B } , { C } , { D } , { E } , { F } \{A\}, \{B\}, \{C\}, \{D\}, \{E\}, \{F\} { A } , { B } , { C } , { D } , { E } , { F } .
Step 2 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 1 2 3 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 1 1 1 : B − C B\!-\!C B − C weight 1 1 1 . f i n d ( B ) = B find(B) = B f in d ( B ) = B , f i n d ( C ) = C find(C) = C f in d ( C ) = C – different components. Accept. Union → { B , C } \to \{B, C\} → { B , C } . MST weight so far = 1 = 1 = 1 .
Step 3 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 2 3 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 2 2 2 : A − C A\!-\!C A − C weight 2 2 2 . f i n d ( A ) = A find(A) = A f in d ( A ) = A vs f i n d ( C ) = { B , C } find(C) = \{B,C\} f in d ( C ) = { B , C } – different. Accept. Union → { A , B , C } \to \{A, B, C\} → { A , B , C } . MST weight = 3 = 3 = 3 .
Step 4 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 DE 2 3 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 3 3 3 : D − E D\!-\!E D − E weight 2 2 2 . f i n d ( D ) = D find(D) = D f in d ( D ) = D vs f i n d ( E ) = E find(E) = E f in d ( E ) = E – different. Accept. Union → { D , E } \to \{D, E\} → { D , E } . MST weight = 5 = 5 = 5 . Components: { A , B , C } , { D , E } , { F } \{A,B,C\}, \{D,E\}, \{F\} { A , B , C } , { D , E } , { F } .
Step 5 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 DE 2 EF 3 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 4 4 4 : E − F E\!-\!F E − F weight 3 3 3 . f i n d ( E ) = { D , E } find(E) = \{D,E\} f in d ( E ) = { D , E } vs f i n d ( F ) = F find(F) = F f in d ( F ) = F – different. Accept. Union → { D , E , F } \to \{D, E, F\} → { D , E , F } . MST weight = 8 = 8 = 8 . Four edges picked; one more needed to merge { A , B , C } \{A,B,C\} { A , B , C } with { D , E , F } \{D,E,F\} { D , E , F } .
Step 6 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F Arrow from G.node[A] to G.node[B]: w=4 cycle! w=4 cycle! BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 DE 2 EF 3 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 5 5 5 : A − B A\!-\!B A − B weight 4 4 4 . f i n d ( A ) = f i n d ( B ) = { A , B , C } find(A) = find(B) = \{A,B,C\} f in d ( A ) = f in d ( B ) = { A , B , C } – same component. Reject – adding A − B A\!-\!B A − B would close the cycle A − C − B − A A\!-\!C\!-\!B\!-\!A A − C − B − A . MST unchanged.
Step 7 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F Arrow from G.node[A] to G.node[B]: w=4 cycle! w=4 cycle! Arrow from G.node[B] to G.node[D]: w=5 bridge w=5 bridge BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 DE 2 EF 3 BD 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 6 6 6 : B − D B\!-\!D B − D weight 5 5 5 . f i n d ( B ) = { A , B , C } find(B) = \{A,B,C\} f in d ( B ) = { A , B , C } vs f i n d ( D ) = { D , E , F } find(D) = \{D,E,F\} f in d ( D ) = { D , E , F } – different. Accept. Union → { A , B , C , D , E , F } \to \{A,B,C,D,E,F\} → { A , B , C , D , E , F } . MST weight = 13 = 13 = 13 . Five edges on six nodes – the spanning tree is complete. The remaining edges will all form cycles, but we finish the scan for completeness.
Step 8 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F Arrow from G.node[A] to G.node[B]: w=4 cycle! w=4 cycle! Arrow from G.node[B] to G.node[D]: w=5 bridge w=5 bridge Arrow from G.node[D] to G.node[F]: w=6 cycle! w=6 cycle! BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 DE 2 EF 3 BD 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 7 7 7 : D − F D\!-\!F D − F weight 6 6 6 . Both endpoints already in the single component. Reject – would close the cycle D − E − F − D D\!-\!E\!-\!F\!-\!D D − E − F − D .
Step 9 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F Arrow from G.node[A] to G.node[B]: w=4 cycle! w=4 cycle! Arrow from G.node[B] to G.node[D]: w=5 bridge w=5 bridge Arrow from G.node[D] to G.node[F]: w=6 cycle! w=6 cycle! BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 DE 2 EF 3 BD 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 8 8 8 : C − D C\!-\!D C − D weight 8 8 8 . Same component. Reject – would close the cycle C − B − D − C C\!-\!B\!-\!D\!-\!C C − B − D − C inside the MST.
Step 10 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F Arrow from G.node[A] to G.node[B]: w=4 cycle! w=4 cycle! Arrow from G.node[B] to G.node[D]: w=5 bridge w=5 bridge Arrow from G.node[D] to G.node[F]: w=6 cycle! w=6 cycle! BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 DE 2 EF 3 BD 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Edge 9 9 9 : C − E C\!-\!E C − E weight 10 10 10 . Same component. Reject – the cycle C − B − D − E − C C\!-\!B\!-\!D\!-\!E\!-\!C C − B − D − E − C would form.
Step 11 / 11 Edge from node B to node C 1 Edge from node A to node C 2 Edge from node D to node E 2 Edge from node E to node F 3 Edge from node A to node B 4 Edge from node B to node D 5 Edge from node D to node F 6 Edge from node C to node D 8 Edge from node C to node E 10 A B C D E F Arrow from G.node[A] to G.node[B]: w=4 cycle! w=4 cycle! Arrow from G.node[B] to G.node[D]: w=5 bridge w=5 bridge Arrow from G.node[D] to G.node[F]: w=6 cycle! w=6 cycle! BC:1 0 AC:2 1 DE:2 2 EF:3 3 AB:4 4 BD:5 5 DF:6 6 CD:8 7 CE:10 8 e d g e s s o r t e d b y w e i g h t edges\;sorted\;by\;weight e d g es sor t e d b y w e i g h t BC 0 AC 1 DE 2 EF 3 BD 4 M S T ( 5 e d g e s ) MST\;(5\;edges) MST ( 5 e d g es ) Kruskal terminates. MST = { B C , A C , D E , E F , B D } = \{BC, AC, DE, EF, BD\} = { BC , A C , D E , EF , B D } with total weight 1 + 2 + 2 + 3 + 5 = 13 1 + 2 + 2 + 3 + 5 = 13 1 + 2 + 2 + 3 + 5 = 13 . Four edges were rejected because they would close a cycle inside an already-connected component. Compare to Dijkstra in h 11 h11 h 11 : that example builds a shortest-path tree rooted at A A A (sum of d i s t dist d i s t values = 36 36 36 ), whereas Kruskal builds a minimum-weight spanning tree (13 13 13 ) – same nodes and weights, different optimisation objective.