0% found this document useful (0 votes)
179 views

Prims and Kruskals Algorithm

The document discusses algorithms for finding minimum spanning trees in graphs. It describes the generic minimum spanning tree algorithm and then provides details on Kruskal's and Prim's algorithms. Kruskal's algorithm finds a minimum spanning tree by adding edges in order of increasing weight, ensuring the added edges do not create cycles. Prim's algorithm grows a tree by progressively adding adjacent vertices with the minimum weight edges. Both algorithms run in O(E log V) time where E is the number of edges and V is the number of vertices.

Uploaded by

BharadwajaGll
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

Prims and Kruskals Algorithm

The document discusses algorithms for finding minimum spanning trees in graphs. It describes the generic minimum spanning tree algorithm and then provides details on Kruskal's and Prim's algorithms. Kruskal's algorithm finds a minimum spanning tree by adding edges in order of increasing weight, ensuring the added edges do not create cycles. Prim's algorithm grows a tree by progressively adding adjacent vertices with the minimum weight edges. Both algorithms run in O(E log V) time where E is the number of edges and V is the number of vertices.

Uploaded by

BharadwajaGll
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

1

Topics

Graphs

Minimum Spanning Trees
Kruskal
Prim


2
Minimum Spanning Trees
Undirected, connected graph
G = (V,E)
Weight function W: E R
(assigning cost or length or
other values to edges)
( , )
( ) ( , )
u v T
w T w u v
e
=

Spanning tree: tree that connects all the vertices
(above?)
Minimum spanning tree: tree that connects all
the vertices and minimizes
3
Generic MST Algorithm
Generic-MST(G, w)
1 AC // Contains edges that belong to a MST
2 while A does not form a spanning tree do
3 Find an edge (u,v) that is safe for A
4 AA{(u,v)}
5 return A
Safe edge edge that does not destroy As property
The algorithm manages a set of edges A maintaining
the following loop invariant
Prior to each iteration, A is a subset of some
minimum spanning tree.
At each step, an edge is determined that can be added
to A without violating this invariant. Such an edge
is called a Safe Edge.
4
Kruskal's Algorithm
Edge based algorithm
Add the edges one at a time, in increasing
weight order
The algorithm maintains A a forest of trees.
An edge is accepted it if connects vertices of
distinct trees
We need a data structure that maintains a
partition, i.e.,a collection of disjoint sets
MakeSet(S,x): S S {{x}}
Union(S
i
,S
j
): S S {S
i
,S
j
} {S
i
S
j
}
FindSet(S, x): returns unique S
i
e S, where x e S
i
5
Kruskal's Algorithm
The algorithm adds the cheapest edge that
connects two trees of the forest
MST-Kruskal(G,w)
01 A C
02 for each vertex v e V[G] do
03 Make-Set(v)
04 sort the edges of E by non-decreasing weight w
05 for each edge (u,v) e E, in order by non-
decreasing weight do
06 if Find-Set(u) = Find-Set(v) then
07 A A {(u,v)}
08 Union(u,v)
09 return A
6
Kruskal Example
7
Kruskal Example (2)
8
Kruskal Example (3)
9
Kruskal Example (4)
10
Kruskal Running Time
Initialization O(V) time
Sorting the edges O(E lg E) = O(E lg V) (why?)
O(E) calls to FindSet
Union costs
Let t(v) the number of times v is moved to a new
cluster
Each time a vertex is moved to a new cluster the size
of the cluster containing the vertex at least doubles:
t(v) s log V
Total time spent doing Union
Total time: O(E lg V)

( ) log
v V
t v V V
e
s

11
Prim-Jarnik Algorithm
Vertex based algorithm
Grows one tree T, one vertex at a time
A cloud covering the portion of T already
computed
Label the vertices v outside the cloud with key[v]
the minimum weigth of an edge connecting v
to a vertex in the cloud, key[v] = , if no such
edge exists
12
MST-Prim(G,w,r)
01 Q V[G] // Q vertices out of T
02 for each u e Q
03 key[u]
04 key[r] 0
05 t[r] NIL
06 while Q = C do
07 u ExtractMin(Q) // making u part of T
08 for each v e Adj[u] do
09 if v e Q and w(u,v) < key[v] then
10 t[v] u
11 key[v] w(u,v)
Prim-Jarnik Algorithm (2)
13
Prim Example
14
Prim Example (2)
15
Prim Example (3)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy