Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
19 January 2021

Minimum Spanning Trees: Prim's Algorithm

by mervyn

Motivation

Road construction to connect all cities: How to minimize construction cost? Road construction plan to collect all the cities with the minimum cost.

Spanning Trees

spantree

This spanning tree is not unique. Such a collection of edges is called a tree because if any vertex is taken to be the root, we form a tree by treating the adjacent vertices as children, and so on…

Spanning trees on Weighted Graphs

The weight of a spanning tree is the sum of the weights on all the edges which comprise the spanning tree

Minimum Spanning Tree(MST)

Spanning tree which minimizes the weight

Unweighted Graphs

Observation

Algorithms

Two common algorithms for finding minimum spanning trees are:

Prim’s Algorithm

Suppose we have a known minimum spanning tree on \(k<n\) vertices

Add that edge \(e_{k}\) with least weight that connects this minimum spanning tree to a new vertex \(v_{k+1}\)

Suppose it does not

Because a MST is connected, there must be a path from vertex \(v_{k+1}\) back to our existing MST

Let w be the weight of this MST

Suppose we swap edges and instead choose to include \(e_{k}\), and exclude \(\widetilde{e}\)

Example

Find the MST for the following undirected weighted graph

prim

  1. Set up the appropriate table and initialize it
  2. We could extend the trivial tree containing just the root node by one of the three possible children:

prim2

As we wish to find a minimum spanning tree, it makes sense we add that vertex with a connecting edge with least weight.

  1. The next unvisited vertex with minimum distance is vertex 4
    • Update vertices 2, 7, 8
    • Don’t update vertex 5

We have updated all vertices adjacent to vertex 4.

prim3

  1. We could extend the tree by adding one of the edges (1,5), (4,2), (4,7), or (4,8)
    • When there are no more unvisited vertices, we are done
    • If at any point, all remaining vertices had a distance of \(\infty\), this would indicate that the graph is not connected
      • In this case, the MST would only span one connected sub-graph
  2. Using the parent pointers, we can now construct the minimum spanning tree

Summary

Analysis

The initialization requires \(\Theta(|V|)\) memory and runtime. We iterate \(\lvert V\rvert-1\) times, each time finding the closet vertex

To do better:

Analysis with Priority Queue (Minimum Heap)

The initialization requires \(\Theta(|V|)\) memory and runtime

We iterate \(\lvert V\rvert-1\) times, each time finding the closest vertex

The each edge visited may result in a new edge being pushed to the very top of the heap. Thus, the work required for this is \(O(\lvert E\rvert\lg(\lvert V\rvert))\)

Total runtime: \(O(\lvert V\rvert\lg(\lvert V\rvert)+ \lvert E\rvert\lg(\lvert V\rvert))=O(\lvert E\rvert\lg(\lvert V\rvert))\)

If number of edges are critically larger than vertices, using priority queue could cost more than without it. However, if there are just a moderate number of edges (not extremely more than vertices) priority queue could be a more efficient way.

source “K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 11-1 최소신장트리 문제와 프림 알고리즘 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: