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


Teaching Kids Programming: Videos on Data Structures and Algorithms

Introduction to Minimum Spanning Tree MST

Previously, we have talked about the Kruskal’s MST Algorithm: Teaching Kids Programming – Introduction to Kruskal’s Minimum Spanning Tree (Graph Algorithm).

A Minimum Spanning Tree (MST) is a Tree tex_3ca65799320efe93e56ebdc3f48e6013 Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video that is a subset of a weighted undirectional Graph tex_5134371fedcc8423c46f533342e06148 Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video aka tex_d9ef6e543d338d692acffcae1075a505 Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video . With Kruskal’s algorithm, we start with a forest will all vertice V and then add N-1 edges (N-1 edges to connect N vertex).

The total weights of the MST tex_3ca65799320efe93e56ebdc3f48e6013 Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video is:

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

We need to pick exactly N-1 edges to build a MST with N vertices.

The Prim’s Minimum Spanning Tree (Graph Algorithm)

The Prim’s algorithm works differently: by adding vertices one by one in a way that every time we select the smallest edge that connects to the visited vertices.

The steps of applying the Prim’s MST Algorithm:

  • tex_3f1a0d1f265ff318bea9a110e1a11c7d Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video
  • Pick a random vertex and put it in MST T
  • While T does not have N vertice yet
  • – find the smallest edge (m, n) where tex_cfed270de655ca9d2a914168540299f7 Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video and tex_dcc683d0a0c62569412bf02717b993ff Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video
  • – add the edge (m, n) to T e.g. T = T ∪ {(m, n)}
  • – remove the vertex n from S

The Psuedo code:

1
2
3
4
5
6
7
8
9
10
def Prims(G):
    E =;
    # pick vertex 1 (or another random)
    V = { 1 };
    while V ≠ G.V:
       # find the smallest edge such that m ∈ V and n ∈ G.V - V
       (m, n) = findEdge(V, G.V - V, G.E) 
       E = E ∪ {(m, n)}
       V = V ∪ {n}
    return {V: V, E: E}
def Prims(G):
    E = ∅;
    # pick vertex 1 (or another random)
    V = { 1 };
    while V ≠ G.V:
       # find the smallest edge such that m ∈ V and n ∈ G.V - V
       (m, n) = findEdge(V, G.V - V, G.E) 
       E = E ∪ {(m, n)}
       V = V ∪ {n}
    return {V: V, E: E}

The Prim’s Minimum Spanning Tree Algorithm can be visualized as follows – always pick the smallest edge (Greedy) that connects two vertices one from the unvisited vertices and one from the chosen set of vertices.

prims-mst-algorithm Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video

The time complexity is tex_2f7b038e6821fa1ab6bdb043c41f0eda Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video which is tex_7c409a1f09d2516444594a8fb110da35 Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video . It takes tex_dcd3716d08c3ab30a5fb5255bace7f4b Teaching Kids Programming - Introduction to Prim's Minimum Spanning Tree (Graph Algorithm) algorithms graph math Minimum Spanning Tree teaching kids programming youtube video to insert a vertex into a heap.

Correctness Proof of Prim’s MST Algorithm

We have chosen a list of vertex V, and by choosing a next edge we connect the current Tree (subset of MST T) to another vertex. If the next edge we pick E’ is not the smallest edge E, thus the E’ will form a cycle and can be replaced with E.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
917 words
Last Post: Teaching Kids Programming - Introduction to Kruskal's Minimum Spanning Tree (Graph Algorithm)
Next Post: Teaching Kids Programming - Graph Traversal Algorithms in DFS or BFS (Unlock Rooms with Keys)

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

Leave a Reply