CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s...
Transcript of CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s...
![Page 1: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/1.jpg)
CS 561, Lecture 9, Minimum Spanning
Trees
Jared Saia
University of New Mexico
![Page 2: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/2.jpg)
Today’s Outline
• Minimum Spanning Trees
• Safe Edge Theorem
• Kruskal and Prim’s algorithms
• Graph Representation
1
![Page 3: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/3.jpg)
Graph Definition
• A graph is a pair of sets (V,E).
• We call V the vertices of the graph
• E is a set of vertex pairs which we call the edges of the
graph.
• In an undirected graph, the edges are unordered pairs of
vertices and in a directed graph, the edges are ordered pairs.
• We assume that there is never an edge from a vertex to itself
(no self-loops) and that there is at most one edge from any
vertex to any other (no multi-edges)
• |V | is the number of vertices in the graph and |E| is the
number of edges
2
![Page 4: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/4.jpg)
Graph Defns
• A graph G′ = (V ′, E′) is a subgraph of G = (V,E) if V ′ ⊆ V
and E′ ⊆ E
• If (u, v) is an edge in a graph, then u is a neighbor of v
• For a vertex v, the degree of v, deg(v), is equal to the number
of neighbors of v
• A walk is a sequence of edges, where each successive pair of
edges shares a vertex.
• A path is a walk, where the vertices in the sequence are all
distinct.
• A graph is connected if there is a path from any vertex to
any other vertex
• A disconnected graph consists of several connected compo-
nents which are maximal connected subgraphs
• Two vertices are in the same component if and only if there
is a path between them
3
![Page 5: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/5.jpg)
Graph Defns
For undirected graphs:
• A cycle is a walk that starts and ends at the same vertex and
where all vertices except the last visited are unique.
• A graph is acyclic if no subgraph is a cycle. Acyclic graphs
are also called forests
• A tree is a connected acyclic graph. It’s also a connected
component of a forest.
• A spanning tree of a graph G is a subgraph that is a tree
and also contains every vertex of G. A graph can only have
a spanning tree if it’s connected
• A spanning forest of G is a collection of spanning trees, one
for each connected component of G
4
![Page 6: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/6.jpg)
Minimum Spanning Tree Problem
• Suppose we are given a connected, undirected weighted graph
• That is a graph G = (V,E) together with a function w : E →R that assigns a weight w(e) to each edge e. (We assume
the weights are real numbers)
• Our task is to find the minimum spanning tree of G, i.e., the
spanning tree T minimizing the function
w(T ) =!
e∈Tw(e)
5
![Page 7: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/7.jpg)
Example
Graph Defns
• A cycle is a path that starts and ends at the same vertexand has at least one edge
• A graph is acyclic if no subgraph is a cycle. Acyclic graphsare also called forests
• A tree is a connected acyclic graph. It’s also a connectedcomponent of a forest.
• A spanning tree of a graph G is a subgraph that is a treeand also contains every vertex of G. A graph can only havea spanning tree if it’s connected
• A spanning forest of G is a collection of spanning trees, onefor each connected component of G
4
Minimum Spanning Tree Problem
• Suppose we are given a connected, undirected weighted graph• That is a graph G = (V, E) together with a function w : E !
R that assigns a weight w(e) to each edge e. (We assumethe weights are real numbers)
• Our task is to find the minimum spanning tree of G, i.e., thespanning tree T minimizing the function
w(T ) =!
e!T
w(e)
5
Example
8 5
10
2 3
18 16
12
14
30
4 26
A weighted graph and its minimum spanning tree
6
Applications
• Creating an inexpensive road network to connect cities• Wiring up homes for phone service with the smallest amount
of phone wire• Finding a good approximation to the TSP problem
7
A weighted graph and its minimum spanning tree
6
![Page 8: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/8.jpg)
Applications
• Creating an inexpensive road network to connect cities
• Wiring up homes for phone service with the smallest amount
of wire
• Finding a good approximation to the TSP problem
7
![Page 9: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/9.jpg)
Generic MST Algorithm
Generic-MST(G,w){
A = {};
while (A does not form a spanning tree){
find an edge (u,v) that is safe for A;
A = A union (u,v);
}
return A;
}
8
![Page 10: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/10.jpg)
Safe edges - Definition
• Let A be any set of edges in G that is a subset of some MST
of G
• An edge e is safe for A if A ∪ {e} is also a subset of a MST.
9
![Page 11: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/11.jpg)
Safe edges
• A cut (S, V − S) of a graph G = (V,E) is a partition of V
• An edge (u, v) crosses the cut (S, V −S) if one of its endpoints
is in S and the other is in V − S
• A cut respects a set of edges A if no edge in A crosses the
cut.
• An edge is a light edge crossing a cut if its weight is the
minimum of any edge crossing the cut
10
![Page 12: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/12.jpg)
Theorem
Let G = (V,E) be a connected, undirected graph with a real-
valued weight function w defined on E. Let A be a subset of
E that is included in some minimum spanning tree for G. Let
(S, V − S) be any cut of G that respects A and let (u, v) be a
light edge crossing (S, V − S). Then edge (u, v) is safe for A
11
![Page 13: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/13.jpg)
Proof
• Let T be a MST that includes some set of edges A
• Assume that T does not contain the light edge e = (u, v)
• Since T is connected, it contains a unique path from u to v
and at least one edge e′ on this path crosses the cut that
respects A
• Note that w(e) ≤ w(e′) by assumption
• Removing e′ from the MST and adding e gives us a new
spanning tree T ′
• T ′ has total weight no more than T and this T ′ must also be
a MST. QED.
12
![Page 14: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/14.jpg)
Example
Proof
• Let T be a minimum spanning tree that includes some set ofedges A
• Assume that T does not contain the light edge e = (u, v)• Since T is connected, it contains a unique path from u to v,
and at least one edge e! on this path crosses the cut• Note that w(e) ! w(e!) by assumption• Removing e! from the minimum spanning tree and adding e
gives us a new spanning tree, T !
• T ! has total weight no more than T .• Thus the edge e is in fact contained in some MST.
12
Example
u
v
e
e’
Proving that every safe edge is some minimum spanning tree.
13
Corollary
Let G = (V, E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset ofE that is included in some minimum spanning tree for G, andlet C = (Vc, Ec) be a connected component (tree) in the forestGA = (V, A). If (u, v) is a light edge connecting C to some othercomponent in GA, then (u, v) is safe for A
Proof: The cut (VC, V "VC) respects A, and (u, v) is a light edgefor this cut. Therefore (u, v) is safe for A.
14
Two MST algorithms
• There are two major MST algorithms, Kruskal’s and Prim’s• In Kruskal’s algorithm, the set A is a forest. The safe edge
added to A is always a least-weighted edge in the graph thatconnects two distinct components
• In Prim’s algorithm, the set A forms a single tree. The safeedge added to A is always a least-weighted edge connectingthe tree to a vertex not in the tree
15
Proof that every safe edge is in some MST. The red edges are
the set A.
13
![Page 15: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/15.jpg)
Corollary
Let G = (V,E) be a connected, undirected graph with a real-
valued weight function w defined on E. Let A be a subset of
E that is included in some minimum spanning tree for G, and
let C = (Vc, Ec) be a connected component (tree) in the forest
GA = (V,A). If (u, v) is a light edge connecting C to some other
component in GA, then (u, v) is safe for A
Proof: The cut (VC, V −VC) respects A, and (u, v) is a light edge
for this cut. Therefore (u, v) is safe for A.
14
![Page 16: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/16.jpg)
Two MST algorithms
• There are two major MST algorithms, Kruskal’s and Prim’s
• In Kruskal’s algorithm, the set A is a forest. The safe edge
added to A is always a least-weighted edge in the graph that
connects two distinct components
• In Prim’s algorithm, the set A forms a single tree. The safe
edge added to A is always a least-weighted edge connecting
the tree to a vertex not in the tree
15
![Page 17: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/17.jpg)
Kruskal’s Algorithm
• Q: In Kruskal’s algorithm, how do we determine whether or
not an edge connects two distinct connected components?
• A: We need some way to keep track of the sets of vertices
that are in each connected components and a way to take
the union of these sets when adding a new edge to A merges
two connected components
• What we need is the data structure for maintaining disjoint
sets (aka Union-Find) that we discussed last week
16
![Page 18: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/18.jpg)
Kruskal’s Algorithm
MST-Kruskal(G,w){
for (each vertex v in V)
Make-Set(v);
sort the edges of E into nondecreasing order by weight;
for (each edge (u,v) in E taken in nondecreasing order){
if(Find-Set(u)!=Find-Set(v)){
A = A union (u,v);
Set-Union(u,v);
}
}
return A;
}
17
![Page 19: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/19.jpg)
Example RunKruskal’s Algorithm
• Q: In Kruskal’s algorithm, how do we determine whether ornot an edge connects two distinct connected components?
• A: We need some way to keep track of the sets of verticesthat are in each connected components and a way to takethe union of these sets when adding a new edge to A mergestwo connected components
• What we need is the data structure for maintaining disjointsets (aka Union-Find) that we discussed last week
16
Kruskal’s Algorithm
MST-Kruskal(G,w){
for (each vertex v in V)
Make-Set(v);
sort the edges of E into nondecreasing order by weight;
for (each edge (u,v) in E taken in nondecreasing order){
if(Find-Set(u)!=Find-Set(v)){
A = A union (u,v);
Set-Union(u,v);
}
}
return A;
}
17
Example Run
8 5
10
2 3
18 16
12
14
30
4 26
8 5
10
3
18 16
12
14
30
4 26
8 5
10
18 16
12
14
30
4 26
8 5
10
18 16
12
14
30
26
18
8
10
16
12
14
30
26
10
16
12
14
30
26
16
12
14
30
26
181816
14
30
26
1816
30
26
18
30
26
18
30
26
30
Kruskal’s algorithm run on the example graph. Thick edges are in A.Dashed edges are useless.
18
Correctness?
• Correctness of Kruskal’s algorithm follows immediately fromthe corollary
• Each time we add the lightest weight edge that connects twoconnected components, hence this edge must be safe for A
• This implies that at the end of the algorith, A will be a MST
19
Kruskal’s algorithm run on the example graph. Thick edges are in A.Dashed edges are useless.
18
![Page 20: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/20.jpg)
Correctness?
• Correctness of Kruskal’s algorithm follows immediately from
the corollary
• Each time we add the lightest weight edge that connects two
connected components, hence this edge must be safe for A
• This implies that at the end of the algorith, A will be a MST
19
![Page 21: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/21.jpg)
Runtime?
• The runtime for Kruskal’s alg. will depend on the implemen-
tation of the disjoint-set data structure. We’ll assume the im-
plementation with union-by-rank and path-compression which
we showed has amortized cost of log∗ n
20
![Page 22: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/22.jpg)
Runtime?
• Time to sort the edges is O(|E| log |E|)• Total amount of time for the |V | calls to Make-Set; and
O(|E|) calls to Find-Set and Set-Union is O((|V |+|E|) log∗ |V |)• Since G is connected, |E| ≥ |V |−1 and so O((|V |+|E|) log∗ |V |) =
O(|E| log∗ |V |) = O(|E| log |E|)• Total amount of additional work done in the for loop is just
O(E)
• Thus total runtime of the algorithm is O(|E| log |E|)• Since |E| ≤ |V |2, we can rewrite this as O(|E| log |V |)
21
![Page 23: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/23.jpg)
Prim’s Algorithm
• In Prim’s algorithm, the set A maintained by the algorithm
forms a single tree.
• The tree starts from an arbitrary root vertex and grows until
it spans all the vertices in V
• At each step, a light edge is added to the tree A which
connects A to an isolated vertex of GA = (V,A)
• By our Corollary, this rule adds only safe edges to A, so when
the algorithm terminates, it will return a MST
22
![Page 24: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/24.jpg)
Example Run
Runtime?
• The runtime fo the Kruskal’s alg. will depend on the imple-mentation of the disjoint-set data structure. We’ll assumethe implementation with union-by-rank and path-compressionwhich we showed has amortized cost of log! n
20
Runtime?
• Time to sort the edges is O(|E| log |E|)• Total amount of time for the |V | Make-Sets and up to |E|
Set-Unions is O((|V | + |E|) log! |V |)• Since G is connected, |E| ! |V |"1 and so O((|V |+|E|) log! |V |) =
O(|E| log! |V |) = O(|E| log |E|)• Total amount of additional work done in the for loop is just
O(E)• Thus total runtime of the algorithm is O(|E| log |E|)• Since |E| # |V |2, we can rewrite this as O(|E| log |V |)
21
Prim’s Algorithm
• In Prim’s algorithm, the set A maintained by the algorithmforms a single tree.
• The tree starts from an arbitrary root vertex and grows untilit spans all the vertices in V
• At each step, a light edge is added to the tree A whichconnects A to an isolated vertex of GA = (V, A)
• By our Corollary, this rule adds only safe edges to A, so whenthe algorithm terminates, it will return a MST
22
Example Run
8 5
10
2 3
18 16
12
14
30
4 26
18
8 5
10
2 3
16
12
14
30
26
8 5
10
2 3
18 16
30
26
8 5
10
3
16
30
26
8 5
16
30
26
16
30
26
Prim’s algorithm run on the example graph, starting with thebottom vertex.
At each stage, thick edges are in A, an arrow points along A’ssafe edge, and dashed edges are useless.
23
Prim’s algorithm run on the example graph, starting with the
bottom vertex.
At each stage, thick edges are in A, an arrow points along A’s
safe edge, and dashed edges are useless.
23
![Page 25: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/25.jpg)
An Implementation
• To implement Prim’s algorithm, we keep all edges adjacent
to A in a heap
• When we pull the minimum-weight edge off the heap, we
first check to see if both its endpoints are in A
• If not, we add the edge to A and then add the neighboring
edges to the heap
• If we implement Prim’s algorithm this way, its running time
is O(|E| log |E|) = O(|E| log |V |)• However, we can do better
24
![Page 26: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/26.jpg)
Prim’s Algorithm
• We can speed things up by noticing that the algorithm visits
each vertex only once
• Rather than keeping the edges in the heap, we will keep a
heap of vertices, where the key of each vertex v is the weight
of the minimum-weight edge between v and A (or infinity if
there is no such edge)
• Each time we add a new edge to A, we may need to decrease
the key of some neighboring vertices
25
![Page 27: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/27.jpg)
Prim’s
We will break up the algorithm into two parts, Prim-Init and
Prim-Loop
Prim(V,E,s){
Prim-Init(V,E,s);
Prim-Loop(V,E,s);
}
26
![Page 28: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/28.jpg)
Prim-Init
Prim-Init(V,E,s){
for each vertex v in V - {s}{
if ((v,s) is in E){
edge(v) = (v,s);
key(v) = w((v,s));
}else{
edge(v) = NULL;
key(v) = infinity;
}
Heap-Insert(v);
}
Heap-Insert(s);
}
27
![Page 29: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/29.jpg)
Prim-Loop
Prim-Loop(V,E,s){
A = {};
for (i = 1 to |V| - 1){
v = Heap-ExtractMin();
add edge(v) to A;
for (each edge (u,v) in E){
if ((u,v) is not in A AND key(u) > w(u,v)){
edge(u) = (u,v);
Heap-DecreaseKey(u,w(u,v));
}
}
}
return A;
}
28
![Page 30: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/30.jpg)
Runtime?
• The runtime of Prim’s is dominated by the cost of the heap
operations Insert, ExtractMin and DecreaseKey
• Insert and ExtractMin are each called O(|V |) times
• DecreaseKey is called O(|E|) times, at most twice for each
edge
• If we use a Fibonacci Heap, the amortized costs of Insert and
DecreaseKey is O(1) and the amortized cost of ExtractMin
is O(log |V |)• Thus the overall run time of Prim’s is O(|E|+ |V | log |V |)• This is faster than Kruskal’s unless E = O(|V |)
29
![Page 31: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/31.jpg)
Note
• This analysis assumes that it is fast to find all the edges that
are incident to a given vertex
• We have not yet discussed how we can do this
• This brings us to a discussion of how to represent a graph in
a computer
30
![Page 32: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/32.jpg)
Graph Representation
There are two common data structures used to explicity repre-
sent graphs
• Adjacency Matrices
• Adjacency Lists
31
![Page 33: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/33.jpg)
Adjacency Matrix
• The adjacency matrix of a graph G is a |V | × |V | matrix of
0’s and 1’s
• For an adjacency matrix A, the entry A[i, j] is 1 if (i, j) ∈ E
and 0 otherwise
• For undirectd graphs, the adjacency matrix is always sym-
metric: A[i, j] = A[j, i]. Also the diagonal elements A[i, i] are
all zeros
32
![Page 34: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/34.jpg)
Example Graph
Adjacency Matrix
• The adjacency matrix of a graph G is a |V | ! |V | matrix of0’s and 1’s
• For an adjacency matrix A, the entry A[i, j] is 1 if (i, j) " E
and 0 otherwise• For undirectd graphs, the adjacency matrix is always sym-
metric: A[i, j] = A[j, i]. Also the diagonal elements A[i, i] areall zeros
32
Example Graph
a
b
e
d
f g
h
ic
33
Example Representations
a b c d e f g h ia 011000000b 101110000c 110110000d 011011000e 011101000f 000110000g 000000010h 000000101i 000000110
a
b
c
d
e
f
g
h
i
d
d
d
e
e
e
f
f
a
b
b
b
a
d
g
g
h
c
c
c
c
b
e
h
i
i
Adjacency matrix and adjacency list representations for theexample graph.
34
Adjacency Matrix
• Given an adjacency matrix, we can decide in !(1) timewhether two vertices are connected by an edge.
• We can also list all the neighbors of a vertex in !(|V |) timeby scanning the row corresponding to that vertex
• This is optimal in the worst case, however if a vertex has fewneighbors, we still need to examine every entry in the row tofind them all
• Also, adjacency matrices require !(|V |2) space, regardless ofhow many edges the graph has, so it is only space e"cientfor very dense graphs
35
33
![Page 35: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/35.jpg)
Example Representations
a b c d e f g h ia 011000000b 101110000c 110110000d 011011000e 011101000f 000110000g 000000010h 000000101i 000000110
Adjacency matrix and adjacency list representations for the
example graph.
34
![Page 36: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/36.jpg)
Adjacency Matrix
• Given an adjacency matrix, we can decide in Θ(1) time
whether two vertices are connected by an edge.
• We can also list all the neighbors of a vertex in Θ(|V |) time
by scanning the row corresponding to that vertex
• This is optimal in the worst case, however if a vertex has few
neighbors, we still need to examine every entry in the row to
find them all
• Also, adjacency matrices require Θ(|V |2) space, regardless of
how many edges the graph has, so it is only space efficient
for very dense graphs
35
![Page 37: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/37.jpg)
Adjacency Lists
• For sparse graphs — graphs with relatively few edges —
we’re better off with adjacency lists
• An adjacency list is an array of linked lists, one list per vertex
• Each linked list stores the neighbors of the corresponding
vertex
36
![Page 38: CS 561, Lecture 9, Minimum Spanning Treessaia/classes/561-f19/lec/lec-mst.pdf• In Kruskal’s algorithm, the set A is a forest. The safe edge added to A is always a least-weighted](https://reader033.fdocuments.us/reader033/viewer/2022041721/5e4f04555839ee5ef21fbbbd/html5/thumbnails/38.jpg)
Adjacency Lists
• The total space required for an adjacency list is O(|V |+ |E|)• Listing all the neighbors of a node v takes O(1+deg(v)) time
• We can determine if (u, v) is an edge in O(1 + deg(u)) time
by scanning the neighbor list of u
• Note that we can speed things up by storing the neighbors
of a node not in lists but rather in hash tables
• Then we can determine if an edge is in the graph in expected
O(1) time and still list all the neighbors of a node v in O(1+
deg(v)) time
37