0% found this document useful (0 votes)
9 views15 pages

CS180 S25 L09 MSTs Part2 Annotated-1

The document outlines administrative information and objectives for a CS180 lecture on Minimum Spanning Trees, including details on Dijkstra's Algorithm, Prim's Algorithm, and Kruskal's Algorithm. It provides definitions, properties, and applications of minimum spanning trees, as well as instructions for submitting assignments and accessing office hours. Additionally, it includes links to previous lecture code solutions and encourages students to ask questions anonymously.

Uploaded by

nalox23341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

CS180 S25 L09 MSTs Part2 Annotated-1

The document outlines administrative information and objectives for a CS180 lecture on Minimum Spanning Trees, including details on Dijkstra's Algorithm, Prim's Algorithm, and Kruskal's Algorithm. It provides definitions, properties, and applications of minimum spanning trees, as well as instructions for submitting assignments and accessing office hours. Additionally, it includes links to previous lecture code solutions and encourages students to ask questions anonymously.

Uploaded by

nalox23341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Announcements

Administrative Information

PS4 due 5/2/25

Release and Submission on BruinLearn through Gradescope.


CS180 Lecture 8: Minimum Spanning Trees
Office Hours: M 3-4 pm and Th 6-7 pm, ENGR VI 282 and
Zoom link on BruinLearn

Please ask questions…

Please feel free to ask questions at any time. If


you’d prefer anonymity. Here is slido:

https://app.sli.do/event/aNNu7dWGCTGkJXtSWRx8
tu

3
Objectives for Today Single-pair shortest path problem

Problem. Given a digraph G = (V, E), edge lengths ℓe ≥ 0, source s ∈ V,


and destination t ∈ V, find a shortest directed path from s to t.
1. Dijkstra’s Algorithm
2. Minimum Spanning Trees
3. Prim’s Algorithm
1
4. Kruskal’s Algorithm 1
5
3

5
4 1
source s 0 2 3
8
7 2 9
7

9 6 1
11
5
5
4 13

5 4 20 6

destination t

length of path = 9 + 4 + 1 + 11 = 25 6

Single-source shortest paths problem Shortest path applications

Problem. Given a digraph G = (V, E), edge lengths ℓe ≥ 0, source s ∈ V, ・Map routing.
find a shortest directed path from s to every node. ・Robot navigation.
・Urban traffic planning.
・Routing of telecommunications messages.
・Network routing protocols
1 3
・Optimal truck routing through given traffic congestion pattern.
1
5
5
4 1
source s 0 2 3
8
7 2 9
7

9 6 1
11
5
5
4 13

4 6 Network Flows: Theory, Algorithms, and Applications,


20 by Ahuja, Magnanti, and Orlin, Prentice Hall, 1993.

shortest-paths tree 7 9
Dijkstra′s algorithm (for single-source shortest paths problem) Dijkstra′s algorithm (for single-source shortest paths problem)

Greedy approach. Maintain a set of explored nodes S for which Greedy approach. Maintain a set of explored nodes S for which
algorithm has determined d[u] = length of a shortest s↝u path. algorithm has determined d[u] = length of a shortest s↝u path.
・Initialize S ← { s }, d[s] ← 0. ・Initialize S ← { s }, d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes ・Repeatedly choose unexplored node v ∉ S which minimizes
the length of a shortest path from s the length of a shortest path from s
to some node u in explored part S, to some node u in explored part S,
followed by a single edge e = (u, v) followed by a single edge e = (u, v)
add v to S, and set d[v] ← π(v).
・To recover path, set pred[v] ← e that achieves min.

d[v]
ℓe v ℓe v
d[u] d[u]
u u
S S

s s

10 11

Dijkstra′s algorithm: efficient implementation

Critical optimization 1. For each unexplored node v ∉ S :


explicitly maintain π [ v] instead of computing directly from definition

・For each v ∉ S : π(v) can only decrease (because set S increases).

・More specifically, suppose u is added to S and there is an edge e = (u, v)


leaving u. Then, it suffices to update:

π [v] ← min { π [v], π [u] + ℓe) }

recall: for each u ∈ S,


π [u] = d [u] = length of shortest s↝u path

Critical optimization 2. Use a min-oriented priority queue (PQ)


to choose an unexplored node that minimizes π [v].

12
Dijkstra′s algorithm: proof of correctness

Invariant. For each node u ∈ S : d[u] = length of a shortest s↝u path.


Pf. [ by induction on ⎜S⎟ ]

P′ e y
x

s P

S u
v

16
Previous lecture code solutions

Runtime Analysis L04:


https://colab.research.google.com/drive/1G0W-
sk8yojp3tXlCnNoilEgY1hSSkpXw?usp=sharing

Graph Algorithms L05:


https://colab.research.google.com/drive/1f932KxyILUvjlXmTm4MD97FoVS-
9O3I0?usp=sharing

19

Objectives for Today Cycles

Def. A path is a sequence of edges which connects a sequence of nodes.

1. Dijkstra’s Algorithm
Def. A cycle is a path with no repeated nodes or edges other than the
2. Minimum Spanning Trees starting and ending nodes.
3. Prim’s Algorithm
2 3
4. Kruskal’s Algorithm

1 6 5 4

7 8

path P = { (1, 2), (2, 3), (3, 4), (4, 5), (5, 6) }
20
cycle C = { (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1) }

21
Cuts

Def. A cut is a partition of the nodes into two nonempty subsets S and V – S.

Def. The cutset of a cut S is the set of edges with exactly one endpoint in S.

2 3

1 6 5 4

7 8

cut S = { 4, 5, 8 }
cutset D = { (3, 4), (3, 5), (5, 6), (5, 7), (8, 7) }

22

Spanning Trees

In an undirected connected graph G, a spanning tree is a subgraph that is a tree and that
contains all vertices of G.

DFS and BFS on connected graphs both produce spanning trees of the graphs.

9
7
8

5 4 6
Spanning tree definition

Def. Let H = (V, T ) be a subgraph of an undirected graph G = (V, E).


H is a spanning tree of G if H is both acyclic and connected.

graph G = (V, E)
spanning tree H = (V, T)

26

Spanning tree properties

Proposition. Let H = (V, T ) be a subgraph of an undirected graph G = (V, E).


Then, the following are equivalent:
・H is a spanning tree of G.
・H is acyclic and connected.
・H is connected and has V⎟ – 1 edges.

・H is acyclic and has V – 1 edges.


⎜ ⎟

・H is minimally connected: removal of any edge disconnects it.


・H is maximally acyclic: addition of any edge creates a cycle.

graph G = (V, E)
spanning tree H = (V, T) 28
Minimum Spanning Trees Minimum spanning tree (MST)

Def. Given a connected, undirected graph G = (V, E) with edge costs ce,
a minimum spanning tree (V, T ) is a spanning tree of G such that the sum
Given a connected undirected graph, G= (V,E) with positive edge weights, find the spanning tree
with the minimum sum of the edge weights. This is the minimum spanning tree (MST). of the edge costs in T is minimized.
The sum of the edge weights is the cost of the MST.
24
4
Let’s see how greedy algorithms can work for this.
6 23 9
18
10
9 16 5
11
7 7 Example for a shipping company: 8
7
8 Let vertices can be the factories, 10 14
17
11 edges connect factories if they are on a shipping route,
21
12 and the edge weight is the distance between the factories.

How can the shipping company visit all the factories, but in
the least distance traveled?
5 4 6 MST cost = 50 = 4 + 6 + 8 + 5 + 11 + 9 + 7
5 4

31

Applications

MST is fundamental problem with diverse applications.

・Cluster analysis.
・Max bottleneck paths.
・Real-time face verification.
・Find road networks in satellite and aerial imagery.
・Reducing data storage in sequencing amino acids in a protein.
・Autoconfig protocol for Ethernet bridging to avoid cycles in a network.
・Approximation algorithms for NP-hard problems (e.g., TSP, Steiner tree).
・Network design (communication, electrical, hydraulic, computer, road).

Network Flows: Theory, Algorithms, and Applications,


by Ahuja, Magnanti, and Orlin, Prentice Hall, 1993.

32
Cut Property of MST Proof Exercise: Cycle Property of MST Proof

The cut property of MST: Let S be any subset of vertices S that is non-empty and not equal to V. If The cycle property of MST: For any cycle in the graph, G, if e is the maximum cost edge in the
e is the minimum cost edge between S and V-S, then every MST contains e. cycle, then e is not in any MST of G.

Proof by contradiction: Proof by contradiction:

36 37
Objectives for Today
Exercise: Cycle Property of MST Proof

The cycle property of MST: For any cycle in the graph, G, if e is the maximum cost edge in the
cycle, then e is not in any MST of G.
1. Dijkstra’s Algorithm
Proof by contradiction:
2. Minimum Spanning Trees
3. Prim’s Algorithm
4. Kruskal’s Algorithm

38

39

Prim’s Algorithm

Given a connected graph, G=(V,E), with positive weights on E


1. Choose any arbitrary vertex, s, to be the starting vertex in the tree, T*.
2. At each iteration, add the lowest cost edge connecting vertex in T* to vertex not in T*.
(The cut is the vertices in T* and those vertices not in T* yet.)
3. Stop when all vertices are included in T*

Claim: Prim’s algorithm produces T*, an MST for G.

1
B C

4 2
3 40
6
A E

7
10
5
D
F
9
Prim′s algorithm Prim′s proof of correctness

Initialize S = { s } for any node s, T = ∅. Loop invariant: T is a MST of the vertices in S.


Repeat n – 1 times:
・Add to T a min-cost edge with exactly one endpoint in S. Induction over the number of iterations:
・Add the other endpoint to S. Base case: S={s} and T is empty. That is a MST containing only s.

Assume invariant holds after i-th iteration, so T is a MST on i+1 nodes in S


Theorem. Prim’s algorithm computes an MST.
What is the loop invariant of Prim’s algorithm?

S S

42 43

Prim′s proof of correctness Prim′s proof of correctness

Loop invariant: T is a MST of the vertices in S. Loop invariant: T is a MST of the vertices in S.
Inductive step:
What happens on completion of the algorithm?

S S

44 45
Prim′s algorithm: implementation Objectives for Today
Theorem. Prim’s algorithm can be implemented to run in O(m log n) time.
Pf. Implementation almost identical to Dijkstra’s algorithm.
1. Dijkstra’s Algorithm
2. Minimum Spanning Trees
PRIM (V, E, c)
3. Prim’s Algorithm
__
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_

S ← , T ← 
s ← any node in V. 4. Kruskal’s Algorithm
FOREACH v ≠ s : π [v] ← ∞, pred[v] ← null; π [s] ← 0.
Create an empty priority queue pq.
FOREACH v ∈ V : INSERT(pq, v, π[v]).
WHILE (IS-NOT-EMPTY(pq)) π[v] = cost of cheapest
u ← DEL-MIN(pq). known edge between v and S

S ← S ∪ { u }, T ← T ∪ { pred[u] }.
FOREACH edge e = (u, v) ∈ E with v ∉ S :
IF (ce < π [v])
47
DECREASE-KEY(pq, v, ce).
π [v] ← ce; pred[v] ← e.
46

Kruskal’s Algorithm

Given a connected graph, G=(V,E), with positive weights on E


1. Sort the edge weights of E.
2. At each iteration, add the minimum cost edge to T* that does not create a cycle.
3. Stop when all vertices are included in T*

Claim: Kruskal’s algorithm produces T*, an MST for G.

1
B C

4 2
3 48
6
A E

7
10
5
D
F
9
Kruskal′s algorithm Kruskal′s algorithm

Consider edges in ascending order of cost: Consider edges in ascending order of cost:
・Add to tree unless it would create a cycle. ・Add to tree unless it would create a cycle.
Theorem. Kruskal’s algorithm computes an MST. Theorem. Kruskal’s algorithm computes an MST.
Pf. One each iteration Kruskal’s consider adding some edge (u,v) to T Pf. One each iteration Kruskal’s consider adding some edge (u,v) to T
・Case 1: If u and v are already part of T ・Case 2: v or u (or both) are not in T yet
・Consider S the set of nodes with path to v

50 51

Kruskal′s algorithm Union–find data structure

Consider edges in ascending order of cost:


・Add to tree unless it would create a cycle.
Theorem. Kruskal’s algorithm computes an MST.
Pf. Why spanning tree upon completion?

To keep track of n elements in subsets:


Each element is in a record with its value and pointer to set to which it belongs along with
set size. Initially this can be null pointer or to itself. Make-Set is O(n)

To create union sets, set the pointer to the element for larger set: Union is O(1)

To find set of element, traverse pointers. Since set doubles in size on union and cannot
have size more than n: Find-Set is O(log n)
52 53
Kruskal′s algorithm: implementation MINIMUM BOTTLENECK SPANNING TREE
Theorem. Kruskal’s algorithm can be implemented to run in O(m log m) time.
・Sort edges by cost. Problem. Given a connected graph G with positive edge costs, find a
・Use union–find data structure to dynamically maintain connected spanning tree that minimizes the most expensive edge.
components.
Goal. O(m log m) time or better.
KRUSKAL (V, E, c)
__
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
___
_____
___
___
___
___
___
___
___
___
___
___
___

SORT m edges by cost and renumber so that c(e1) ≤ c(e2) ≤ … ≤ c(em).


T ←  24

FOREACH v ∈ V : MAKE-SET(v).
FOR i = 1 TO m 4 10 11 9

(u, v) ← ei.
6 5 9
IF (FIND-SET(u) ≠ FIND-SET(v)) are u and v in
same component?
T ← T ∪ { ei }. 3 8 7 14 7

UNION(u, v). make u and v in


same component
RETURN T. Note: not necessarily a MST 21

54 minimum bottleneck spanning tree T (bottleneck = 9)


55

MINIMUM BOTTLENECK SPANNING TREE MINIMUM BOTTLENECK SPANNING TREE

Solution 1. Compute a MST T*. It is also a MBST. Solution 2.


Pf. Suppose for sake of contradiction that T* is not a MBST. ・Sort the edges in increasing order of cost e ≤ e ≤ … ≤ e . 1 2 m

・Let e ∈ T* have cost strictly larger than min bottleneck cost. ・Define E = { e ∈ E : c ≤ x } and G = (V, E ).
x e x x

・Consider cut formed by deleting e from T*. ・Use binary search to find smallest value of x for which G is connected. x

・MBST contains at least one edge f in cutset.


・T* ∪ { f } – { e } yields better MST. ※

minimum spanning tree T*


56 57
Objectives for Today

1. Dijkstra’s Algorithm
2. Minimum Spanning Trees
3. Prim’s Algorithm
4. Kruskal’s Algorithm

58

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