Teaching Kids Programming – Introduction to Kruskal’s Minimum Spanning Tree (Graph Algorithm)


Teaching Kids Programming: Videos on Data Structures and Algorithms

What is a Minimum Spanning Tree?

A Graph (G) is a collection of vertex V and edges E that is tex_8b33ec76ae3a9520de7d175e47ea8da5 Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video . We want to build a tree T with all vertex tex_67d4f6802882a8d0ef6bc421d4d1e47c Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video where T is a subset of G i.e. tex_a28fd001e84df186bfd6509d4c85fd29 Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video

A tree does not have a cycles, and therefore, with N vertex, we need N-1 edges. The total weights by picking up N-1 edges are:

tex_a5fa5630e4699fa9109c958ae9f13afd Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video

Kruskal’s Minimum Spanning Tree Algorithm

With Kruska, we can pick N-1 edges to build the Minimum Spanning Tree (MST):

The steps of applying the Kruskal’s MST Algorithm:

  • First, we sort the edges tex_6a52f3c2e267c687da05a76a548cd96e Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video in the ascending order of weights.
  • Build a forest F with all vertex only (no edges yet – each vertex is a separate tree)
  • While F is not connected:
  • – Then, we start picking the smallest edge from the S and that does not form a cycle in the F
  • – Add it to F and remove it from S.
  • Last, when we have N-1 edges, we have the minimum spanning tree (MST).
minimum-spanning-tree Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video

minimum-spanning-tree

The animation process for Kruskal MST algorithm:

mst_kruskal Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video

The Psuedo code for Kruskal MST algorithm:

1
2
3
4
5
6
7
8
9
10
11
def kruskal(V, E):
    F = ∅
    for e in E:
        F.add({e})
    # sort by weight in ascending order
    E.sort(lambda key: key.weight) 
    for u, v in E:
        if findParent(u) != findParent(v):
            F:= F ∪ {(u, v)}{(v, u)}
            merge(findParent(u), findParent(v))
    return F
def kruskal(V, E):
    F = ∅
    for e in E:
        F.add({e})
    # sort by weight in ascending order
    E.sort(lambda key: key.weight) 
    for u, v in E:
        if findParent(u) != findParent(v):
            F:= F ∪ {(u, v)} ∪ {(v, u)}
            merge(findParent(u), findParent(v))
    return F

The time complexity for Kruskal MST algorithm is tex_f2c3e1d69e0b49a37a33bcdc7a85eca0 Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video as we need to sort the E edges. To check if a edge causes a cycle, we can use the Disjoint Set – and by the compression of path, the time complexity to find parent, and merge two groups is trivial.

As there could be at most tex_3c90faf092268b54d56584eb1a933632 Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video edges, and thus tex_45982932462e39ef40ae315e3d0d6c23 Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video Thus the time complexity for Kruskal MST algorithm can also be represented by tex_6cb6a5c76fc9f7e6210c9cf289761602 Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video .

The space complexity is V as we need to use Disjoint Set.

Correctness Proof for the Kruskal’s MST Algorithm

We are choosing the smallest edge E to connect two components – let’s say this is not part of the MST, and the correct edge is E’. So E’ will form a cycle and then we can replace E’ with E as it is the smallest. Thus, E should be part of the MST – and Tree T we obtain by picking the edges form a minimum spanning tree.

We can also build a MST by Prim’s algorithm: Teaching Kids Programming – Introduction to Prim’s Minimum Spanning Tree (Graph Algorithm)

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
985 words
Last Post: Teaching Kids Programming - Silver Ratio and Pell Numbers (Metal Quadratic Equation)
Next Post: Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm)

The Permanent URL is: Teaching Kids Programming – Introduction to Kruskal’s Minimum Spanning Tree (Graph Algorithm)

Leave a Reply