Title: CS2420: Lecture 40
1CS2420 Lecture 40
- Vladimir Kulyukin
- Computer Science Department
- Utah State University
2Outline
- Graph Algorithms (Chapter 9)
3Spanning Trees
- An edge e in G is redundant if, after the removal
of e, G remains connected. - A spanning tree of G is a tree (acyclic graph)
that contains all vertices of G and preserves Gs
connectivity without any redundant edges.
4Spanning Tree
- A spanning tree of a connected graph is its
connected acyclic sub-graph (a tree) that
contains all vertices of the graph and preserves
the graphs connectivity. - In a weighted connected graph, the weight of a
spanning tree is defined as the sum of the trees
edges. - A minimal (minimum) spanning tree of a weighted
connected graph is a spanning tree of the
smallest weight.
5Spanning Tree Example
A
A
B
B
C
C
D
D
6Spanning Trees Examples
1
1
1
A
B
A
B
A
B
2
2
5
5
3
3
3
D
C
C
D
C
D
Tree T1 Weight(T1) 6
Tree T2 Weight(T2) 9
Original Graph
1
A
B
2
5
C
D
Tree T3 Weight(T3) 8
7Min Spanning Tree Construction
- Construct a minimum spanning tree by expanding it
with one vertex at each iteration. - The initial tree consists of one vertex, i.e. the
root. - On each iteration, the tree so far is expanded by
attaching to it the nearest vertex. - Stop when all the vertices are included.
8Min Spanning Tree Construction Example
1
1
1
A
B
A
B
A
B
2
2
5
Step 1
3
D
C
D
Step 2
Original Graph
1
A
B
2
3
C
D
Step 3
9Min Spanning Tree Construction Prims Algorithm
- G (V, E) is a weighted connected graph
- Input G (V, E).
- Output ET, the set of edges composing a minimum
spanning tree.
10Prims Algorithm Insight
u is the vertex we have reached next we choose
the vertex closest to u by comparing the
weights w1, w2, w3, and w4.
v1
w1
v2
w2
u
w3
v3
w4
v4
11Prims Algorithm Update
- After v is reached, two operations must be
performed - Mark v as visited, i.e. move it into VT
- For each remaining vertex v not in VT and
connected to u by a shorter edge than vs current
weight label, update vs weight label (V.Wght) by
the weight of (u, v) and set vs previous label
(V.Prev) to u.
12Prims Algorithm Example
Tree Remaining Vertices NULL ltA, _, INFgt, ltB,
_, INFgt, ltC, _, INFgt, ltD, _, INFgt, ltE, _,
INFgt, ltF, _, INFgt ltA, _, 0gt ltB, A, 3gt ,
ltC,_,INFgt, ltD, _, INFgt, ltE, A, 6gt , ltF, A,
5gt ltB, A, 3gt ltC, B, 1gt, ltD, _, INFgt, ltE, A,
6gt, ltF, B, 4gt ltC, B, 1gt ltD, C, 6gt, ltE, A,
6gt, ltF, B, 4gt ltF, B, 4gt ltD, F, 5gt , ltE, F,
2gt ltE, F, 2gt ltD, F, 5gt ltD, F, 5gt
1
B
C
3
6
4
4
5
5
A
D
F
2
6
8
E
13Prims Algorithm Example
1
B
C
6
3
4
4
5
5
D
A
F
2
6
8
E
14Prims Algorithm Example
1
B
C
6
3
4
4
5
5
D
A
F
2
6
8
E
15Prims Algorithm Example
1
B
C
6
3
4
4
5
5
D
A
F
2
6
8
E
16Prims Algorithm Example
1
B
C
6
3
4
4
5
5
D
A
F
2
6
8
E
17Prims Algorithm Example
1
B
C
6
3
4
4
5
5
D
A
F
2
6
8
E
18Prims Algorithm Result Spanning Tree
1
B
C
3
4
5
D
A
F
2
E
19Prims Algorithm Implementation
- A vertex v has four labels
- 1) The vertexs name, i.e., V
- 2) The name of the vertex U from which V was
reached (V.Prev) - 3) The weight of (u, v) (V.Wght). Remember V.Wght
weight(u,v) - 4) Visitation status of V (V.Visisted) this is a
boolean variable, initially false, and set to
true when V is visited. - Unreached vertices have NULL as the value of
V.Prev and INF as the value of V.Wght.
20Prims Algorithm Implementation
- We assume that we have implemented a dynamic
priority queue data structure (DynamicPriorityQueu
e). - If Q is a dynamic priority queue, then we can
perform the following operations on it - Q.Insert(v) // inserts an element v into Q
- Q.UpdatePriority(v) // moves v up or down the
minimal heap - Q.Pop() // removes the minimal element
- Q.Push(v) // inserts v into Q.