greedy algorithm 1
greedy algorithm 1
Jianxi Gao
Notation. G = (V,E)
V = nodes (or vertices).
E = edges (or arcs) between pairs of nodes
Captures pairwise relationship between objects.
Network size parameters: n = |V |, m = |E|.
L
A
F M V = {A,B,C,D,E,F,G,H,I, L,M}
I
E {A-D,B-B,B-D,C-D,C-D,D-F,F-G,G-H,H-I,L-M}
D
B G
H
A-D is the same as D-A
C
n = 10, m = 10.
Tree
A tree is an undirected,
acyclic, connected graph.
Minimum Spanning Tree
Human Brain
has between
10-100 billion
neurons.
Examples of Networks: BRAIN Brain
https://www.nytimes.com/interactive/2021/02/16/us/winter-storm-texas-power-outage-map.html
Example of Power Grid
Example of Power Grid
Example of
Road network
• Minimum Spanning Tree of
North Seattle
Minimum Spanning Tree
on Surface of Sphere
5000 Vertices
Minimum Spanning Tree
on Surface of Torus
5000 Vertices
Recursive Tree
Examples of trees
remove
Remove
Add a link
Which of the following properties are true for all spanning tree
H(V, T) of a graph G(V, E)?
A. Contains exactly |V|-1 edges
B. May contain |V| edges 10 mins
C. It is possible to contain less than |V| nodes
D. The removal of any edge disconnects it.
E. The addition of any edge creates a cycle.
F. There exist multiple shortest paths between a pair of nodes in H.
Greedy Algorithms
• Prim's algorithm. Start with some root node s and greedily grow a tree
T from s outward. At each step, add the cheapest edge e to T that has
exactly one endpoint in T.
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
7 4
D C 1 10 F H
11
3 A 12 9
E B 5 2 G I
6 8
Finding MST: Prim’s Algorithm
4
D C 1 F H
3 A 9
E B 5 2 G I
6 8
Finding MST: Prim’s Algorithm
10 mins 7 7
5
D H
5 6
5 6
4 7
B E I
1
4 6 2
4 3
A C F J
3 6 4
Finding MST: Prim’s Algorithm
Interchangeable pairs
G
BE→EF
7 7
BD→DE
5
D H
GD→GH
5 6
5 6
4 7
B E I
1
4 6 2
4 3
A C F J
3 6 4
Kruskal’s Algorithm
A C F J
3 4
Kruskal’s Algorithm
A C F J
3 4
Kruskal’s Algorithm
G
7 7
5
D H
5
4 7
B E I
1
3 2
A C F J
3 4
Kruskal’s Algorithm
G
7
5
D H
5
4
B E I
1
3 2
A C F J
3 4
Cuts
7 8
Cut S = { 4, 5, 8 }
Cutset D = 5-6, 5-7, 3-4, 3-5, 7-8
Cut Property
S V-S
e
Minimum spanning tree
Consider the cut S = {1,4,7,8}, Select all the edges in the cutset of S.
5 mins A. 1-2
B. 2-3
2 3 C. 1-7
1
D. 5-6
6 4
E. 1-6
5
F. 5-7
7 8 G. 4-8
H. 6-7
I. 3-4
J. 5-8
K. 3-5
L. 4-5
M. 3-6
Dijkstra’s algorithm
Procedure Dijkstra(G,l,s)
Input: Graph G = (V,E), positive edge lengths 𝑙𝑒 : 𝑒 ∈ E; 𝑠 ∈ 𝑉.
Output: For all nodes reachable from 𝑠, 𝑑𝑖𝑠𝑡(𝑢) is set to the distance from 𝑠 to 𝑢.
For all u∈ 𝑉
𝑑𝑖𝑠𝑡 𝑢 = ∞,
Prev(u) = nil; (the node immediately before u on the shortest path.)
𝑑𝑖𝑠𝑡 𝑠 = 0,
𝐻 = 𝑚𝑎𝑘𝑒𝑞𝑢𝑒𝑢𝑒 𝑉 , (using distance values as keys, Priority queue)
While H is not empty
u = deletemin(H) (return the element with the smallest key, and remove it)
for all edges 𝑢, 𝑣 ∈ 𝐸:
if dist(u) +l(u,v) < dist(v)
dist(v) = dist(u) +l(u,v).
prev(v) = u;
Decreasekey(H,v); (notify that a certain key values has been decreased.)
Prim algorithm
Procedure Dijkstra(G,l,s)
Input: Graph G = (V,E), positive edge lengths 𝑙𝑒 : 𝑒 ∈ E; 𝑠 ∈ 𝑉.
Output: Minimum spanning tree
For all u∈ 𝑉
𝑑𝑖𝑠𝑡 𝑢 = ∞,
Prev(u) = nil; (the node immediately before u on the shortest path.)
𝑑𝑖𝑠𝑡 𝑠 = 0,
𝐻 = 𝑚𝑎𝑘𝑒𝑞𝑢𝑒𝑢𝑒 𝑉 , (using distance values as keys, Priority queue)
While H is not empty
u = deletemin(H) (return the element with the smallest key, and remove it)
for all edges 𝑢, 𝑣 ∈ 𝐸:
if dist(u) +l(u,v) < dist(v)
dist(v) = dist(u) +l(u,v).
prev(v) = u;
Decreasekey(H,v); (notify that a certain key values has been decreased.)
MST Algorithms: Theory
10 mins 1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1
2 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1 2
A B C
3 2 1 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1
A B C
3 2 1 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1
A B C
3 2 1 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1
A B C
3 2 1 1
1 4
D E F
3
2 4
2
G H
1
Kruskal’s Algorithm example
1
A B C
2 1 1
1
D E F
G H
1