SlideShare a Scribd company logo
Design and Analyses of Algorithms
LECTURE 6
Greedy Algorithms (and
Graphs)
• Graph representation
• Minimum spanning trees
• Optimal substructure
• Greedy choice
• Prim’s greedy MST
algorithm
Graphs
Definition. A directed graph (digraph)
G = (V, E) is an ordered pair consisting of
• a set V of vertices (singular: vertex),
• a set E  V  V of edges.
In an undirected graph G = (V, E), the edge
set E consists of unordered pairs of vertices.
In either case, we have |E| = O(V2). Moreover,
if G is connected, then | E |  | V | – 1.
Adjacency-matrix
representation
A[i, j] =
The adjacency matrix of a graph G = (V, E), where
V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n]
given by
1 if (i, j)  E,
0 if (i, j)  E.
Adjacency-matrix
representation
A[i, j] =
The adjacency matrix of a graph G = (V, E), where
V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n]
given by
1 if (i, j)  E,
0 if (i, j)  E.
2 1
3 4
A 1 2 3 4
1 0 1 1 0
2 0 0 1 0
3 0 0 0 0
4 0 0 1 0
(V 2)storage
 dense
representation.
Adjacency-list representation
An adjacency list of a vertex v  V is the list Adj[v]
of vertices adjacent to v.
Adj[1] = {2, 3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] = {3}
2 1
3 4
Adjacency-list representation
An adjacency list of a vertex v  V is the list Adj[v]
of vertices adjacent to v.
Adj[1] = {2, 3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] = {3}
2 1
3 4
For undirected graphs, |Adj[v]| = degree(v).
For digraphs, |Adj[v]| = out-degree(v).
Adjacency-list representation
An adjacency list of a vertex v  V is the list Adj[v]
of vertices adjacent to v.
Adj[1] = {2, 3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] = {3}
2 1
3 4
For undirected graphs, |Adj[v]| = degree(v).
For digraphs, |Adj[v]| = out-degree(v).
Handshaking Lemma: vV = 2 |E| for undirected
graphs  adjacency lists use (V + E) storage —
a sparse representation (for either type of graph).
Minimum spanning trees
Input: A connected, undirected graph G = (V, E)
with weight function w : E  R.
• For simplicity, assume that all edge weights are
distinct.
Minimum spanning trees
w(T ) w(u,v).
(u,v)T
Input: A connected, undirected graph G = (V, E)
with weight function w : E  R.
• For simplicity, assume that all edge weights are
distinct.
Output: A spanning tree T — a tree that connects
all vertices — of minimum weight:
Example of MST
6 12
5
14
3
8
10
9
15
7
Example of MST
6 12
5
14
3
8
10
15
9
7
Optimal substructure
MST T:
(Other edges of G
are not shown.)
Optimal substructure
u
v
MST T:
(Other edges of G
are not shown.)
Remove any edge (u, v)  T.
Optimal substructure
u
v
MST T:
(Other edges of G
are not shown.)
Remove any edge (u, v)  T.
Remove any edge (u, v)  T. Then, T is partitioned
into two subtrees T1 and T2.
T1
T2
u
v
Optimal substructure
MST T:
(Other edges of G
are not shown.)
T1
T2
u
v
Optimal substructure
MST T:
(Other edges of G
are not shown.)
Remove any edge (u, v)  T. Then, T is partitioned
into two subtrees T1 and T2.
Theorem. The subtree T1 is an MST of G1 = (V1, E1),
the subgraph of G induced by the vertices of T1:
V1 = vertices of T1,
E1 = {(x, y)  E : x, y  V1 }.
Similarly for T2.
Proof of optimal substructure
Proof. Cut and paste:
w(T) = w(u, v) + w(T1) + w(T2).
If T
1
were a lower-weight spanning tree than T1 for
G1, then T = {(u, v)}  T
1
 T2 would bea
lower-weight spanning tree than T for G.
Proof of optimal substructure
Proof. Cut and paste:
w(T) = w(u, v) + w(T1) + w(T2).
If T
1
were a lower-weight spanning tree than T1 for
G1, then T = {(u, v)}  T
1
 T2 would bea
lower-weight spanning tree than T for G.
Do we also have overlapping subproblems?
•Yes.
Proof of optimal substructure
Proof. Cut and paste:
w(T) = w(u, v) + w(T1) + w(T2).
If T
1
were a lower-weight spanning tree than T1 for
G1, then T = {(u, v)}  T
1
 T2 would bea
lower-weight spanning tree than T for G.
Do we also have overlapping subproblems?
•Yes.
Great, then dynamic programming may work!
•Yes, but MST exhibits another powerful property
which leads to an even more efficient algorithm.
Hallmark for “greedy”
algorithms
Greedy-choice property
A locally optimal choice
is globally optimal.
Hallmark for “greedy”
algorithms
Greedy-choice property
A locally optimal choice
is globally optimal.
Theorem. Let T be the MST of G = (V, E),
and let A  V. Suppose that (u, v)  E is the
least-weight edge connecting A to V – A.
Then, (u, v)  T.
Proof of theorem
 A
 V – A
u
(u, v) = least-weight edge
connecting A to V – A
Proof. Suppose (u, v)  T. Cut and paste.
T: v
Proof of theorem
 A
 V – A
u
(u, v) = least-weight edge
connecting A to V – A
Proof. Suppose (u, v)  T. Cut and paste.
T: v
Consider the unique simple path from u to v in T.
Proof of theorem
 A
 V – A
u
(u, v) = least-weight edge
connecting A to V – A
Proof. Suppose (u, v)  T. Cut and paste.
T: v
Consider the unique simple path from u to v in T.
Swap (u, v) with the first edge on this path that
connects a vertex in A to a vertex in V – A.
Proof of theorem
Proof. Suppose (u, v)  T. Cut and paste.
u
(u, v) = least-weight edge
connecting A to V – A
v
T
:
 A
 V – A
Consider the unique simple path from u to v in T.
Swap (u, v) with the first edge on this path that
connects a vertex in A to a vertex in V – A.
A lighter-weight spanning tree than T results.
Prim’s algorithm
IDEA: Maintain V – A as a priority queue Q. Key
each vertex in Q with the weight of the least-
weight edge connecting it to a vertex in A.
Q  V
key[v]   for all v  V
key[s]  0 for some arbitrary s  V
while Q  
do u  EXTRACT-MIN(Q)
for each v  Adj[u]
do if v  Q and w(u, v) < key[v]
⊳
DECREASE-KEY
then key[v]  w(u, v)
[v]  u
At the end, {(v, [v])} forms the MST.
Example of Prim’s algorithm
 A
 V – A

 




6 12
5
14
3
8
10
9
0
15
7
Example of Prim’s algorithm
 A
 V – A

 




6 12
5
14
3
8
10
9
0
15
7
Example of Prim’s algorithm
 A
 V – A

 
 0
0



6 12
5
14
3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A

 
 0
0



6 12
5
14
3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
 0
0



6 12

5
14
3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
 0
0



6 12

5
14
3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
0
0



6 12

5
14

3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
0
0



6 12

5
14

3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
0
0



6 12

5
14

3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
 0
0



6 12

5
14
3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
 0
0



6 12

5
14
3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
 0
0



6 12

5
14
3
8
10
15
9
7
Example of Prim’s algorithm
 A
 V – A
 
 0
0


6 12

5
14
3
8
10
15

9
7
Analysis of Prim
Q  V
key[v]   for all v  V
key[s]  0 for some arbitrary s  V
while Q  
do u  EXTRACT-MIN(Q)
for each v  Adj[u]
do if v  Q and w(u, v) < key[v]
then key[v]  w(u, v)
[v]  u
Analysis of Prim
key[v]   for all v  V
key[s]  0 for some arbitrary s  V
while Q  
do u  EXTRACT-MIN(Q)
for each v  Adj[u]
do if v  Q and w(u, v) < key[v]
then key[v]  w(u, v)
[v]  u
Q  V
(V)
total
Analysis of Prim
key[v]   for all v  V
key[s]  0 for some arbitrary s  V
while Q  
do u  EXTRACT-MIN(Q)
for each v  Adj[u]
do if v  Q and w(u, v) < key[v]
then key[v]  w(u, v)
[v]  u
Q  V
(V)
total
|V|
times
Analysis of Prim
key[v]   for all v  V
key[s]  0 for some arbitrary s  V
while Q  
do u  EXTRACT-MIN(Q)
for each v  Adj[u]
do if v  Q and w(u, v) < key[v]
then key[v]  w(u, v)
[v]  u
degree(u)
times
|V|
times
Q  V
(V)
total
Analysis of Prim
Handshaking Lemma  (E) implicit DECREASE-KEY’s.
key[v]   for all v  V
key[s]  0 for some arbitrary s  V
while Q  
do u  EXTRACT-MIN(Q)
for each v  Adj[u]
do if v  Q and w(u, v) < key[v]
then key[v]  w(u, v)
[v]  u
degree(u)
times
|V|
times
Q  V
(V)
total
Analysis of Prim
key[v]   for all v  V
key[s]  0 for some arbitrary s  V
while Q  
do u  EXTRACT-MIN(Q)
for each v  Adj[u]
do if v  Q and w(u, v) < key[v]
then key[v]  w(u, v)
[v]  u
degree(u)
times
|V|
times
Q  V
(V)
total
Handshaking Lemma  (E) implicit DECREASE-KEY’s.
Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
MST algorithms
Kruskal’s algorithm
• Uses the disjoint-set data structure.
• Running time = O(E lgV).
Best to date:
• Karger, Klein, and Tarjan [1993].
• Randomized algorithm.
• O(V + E) expected time.

More Related Content

Similar to lec6.pptx

Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
Traian Rebedea
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
Aliul Kadir Akib
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
Ayesha Tahir
 
Graphs
GraphsGraphs
Graphs
Ghaffar Khan
 
presentasi
presentasipresentasi
presentasi
Deddy Rahmadi
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
Sikder Tahsin Al-Amin
 
Dijksatra
DijksatraDijksatra
Dijksatra
Tanmay Baranwal
 
Bellman ford
Bellman fordBellman ford
Bellman ford
Kiran K
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
SumitSamanta16
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
LEC 1.pptx
LEC 1.pptxLEC 1.pptx
LEC 1.pptx
ssuserc1e786
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
ARVIND SARDAR
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
Ibrahim Alfayoumi
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
Core Condor
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
ARVIND SARDAR
 
Introduction to Treewidth
Introduction to TreewidthIntroduction to Treewidth
Introduction to Treewidth
ASPAK2014
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
Traian Rebedea
 
Graph algorithms
Graph algorithmsGraph algorithms
Graph algorithms
University of Haripur
 
DATA STRUCTURE PRESENTATION.pptx
DATA STRUCTURE  PRESENTATION.pptxDATA STRUCTURE  PRESENTATION.pptx
DATA STRUCTURE PRESENTATION.pptx
MdMinhajulAbedin1711
 

Similar to lec6.pptx (20)

Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
 
Graphs
GraphsGraphs
Graphs
 
presentasi
presentasipresentasi
presentasi
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
Bellman ford
Bellman fordBellman ford
Bellman ford
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
LEC 1.pptx
LEC 1.pptxLEC 1.pptx
LEC 1.pptx
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
 
Weighted graphs
Weighted graphsWeighted graphs
Weighted graphs
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Introduction to Treewidth
Introduction to TreewidthIntroduction to Treewidth
Introduction to Treewidth
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Graph algorithms
Graph algorithmsGraph algorithms
Graph algorithms
 
DATA STRUCTURE PRESENTATION.pptx
DATA STRUCTURE  PRESENTATION.pptxDATA STRUCTURE  PRESENTATION.pptx
DATA STRUCTURE PRESENTATION.pptx
 

Recently uploaded

一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
AndrzejJarynowski
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
kuntobimo2016
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
g4dpvqap0
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
Bill641377
 
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
74nqk8xf
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
mzpolocfi
 
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
zsjl4mimo
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
u86oixdj
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
Lars Albertsson
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 

Recently uploaded (20)

一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
 
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
 
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
一比一原版(Harvard毕业证书)哈佛大学毕业证如何办理
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 

lec6.pptx

  • 1. Design and Analyses of Algorithms LECTURE 6 Greedy Algorithms (and Graphs) • Graph representation • Minimum spanning trees • Optimal substructure • Greedy choice • Prim’s greedy MST algorithm
  • 2. Graphs Definition. A directed graph (digraph) G = (V, E) is an ordered pair consisting of • a set V of vertices (singular: vertex), • a set E  V  V of edges. In an undirected graph G = (V, E), the edge set E consists of unordered pairs of vertices. In either case, we have |E| = O(V2). Moreover, if G is connected, then | E |  | V | – 1.
  • 3. Adjacency-matrix representation A[i, j] = The adjacency matrix of a graph G = (V, E), where V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n] given by 1 if (i, j)  E, 0 if (i, j)  E.
  • 4. Adjacency-matrix representation A[i, j] = The adjacency matrix of a graph G = (V, E), where V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n] given by 1 if (i, j)  E, 0 if (i, j)  E. 2 1 3 4 A 1 2 3 4 1 0 1 1 0 2 0 0 1 0 3 0 0 0 0 4 0 0 1 0 (V 2)storage  dense representation.
  • 5. Adjacency-list representation An adjacency list of a vertex v  V is the list Adj[v] of vertices adjacent to v. Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} 2 1 3 4
  • 6. Adjacency-list representation An adjacency list of a vertex v  V is the list Adj[v] of vertices adjacent to v. Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} 2 1 3 4 For undirected graphs, |Adj[v]| = degree(v). For digraphs, |Adj[v]| = out-degree(v).
  • 7. Adjacency-list representation An adjacency list of a vertex v  V is the list Adj[v] of vertices adjacent to v. Adj[1] = {2, 3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3} 2 1 3 4 For undirected graphs, |Adj[v]| = degree(v). For digraphs, |Adj[v]| = out-degree(v). Handshaking Lemma: vV = 2 |E| for undirected graphs  adjacency lists use (V + E) storage — a sparse representation (for either type of graph).
  • 8. Minimum spanning trees Input: A connected, undirected graph G = (V, E) with weight function w : E  R. • For simplicity, assume that all edge weights are distinct.
  • 9. Minimum spanning trees w(T ) w(u,v). (u,v)T Input: A connected, undirected graph G = (V, E) with weight function w : E  R. • For simplicity, assume that all edge weights are distinct. Output: A spanning tree T — a tree that connects all vertices — of minimum weight:
  • 10. Example of MST 6 12 5 14 3 8 10 9 15 7
  • 11. Example of MST 6 12 5 14 3 8 10 15 9 7
  • 12. Optimal substructure MST T: (Other edges of G are not shown.)
  • 13. Optimal substructure u v MST T: (Other edges of G are not shown.) Remove any edge (u, v)  T.
  • 14. Optimal substructure u v MST T: (Other edges of G are not shown.) Remove any edge (u, v)  T.
  • 15. Remove any edge (u, v)  T. Then, T is partitioned into two subtrees T1 and T2. T1 T2 u v Optimal substructure MST T: (Other edges of G are not shown.)
  • 16. T1 T2 u v Optimal substructure MST T: (Other edges of G are not shown.) Remove any edge (u, v)  T. Then, T is partitioned into two subtrees T1 and T2. Theorem. The subtree T1 is an MST of G1 = (V1, E1), the subgraph of G induced by the vertices of T1: V1 = vertices of T1, E1 = {(x, y)  E : x, y  V1 }. Similarly for T2.
  • 17. Proof of optimal substructure Proof. Cut and paste: w(T) = w(u, v) + w(T1) + w(T2). If T 1 were a lower-weight spanning tree than T1 for G1, then T = {(u, v)}  T 1  T2 would bea lower-weight spanning tree than T for G.
  • 18. Proof of optimal substructure Proof. Cut and paste: w(T) = w(u, v) + w(T1) + w(T2). If T 1 were a lower-weight spanning tree than T1 for G1, then T = {(u, v)}  T 1  T2 would bea lower-weight spanning tree than T for G. Do we also have overlapping subproblems? •Yes.
  • 19. Proof of optimal substructure Proof. Cut and paste: w(T) = w(u, v) + w(T1) + w(T2). If T 1 were a lower-weight spanning tree than T1 for G1, then T = {(u, v)}  T 1  T2 would bea lower-weight spanning tree than T for G. Do we also have overlapping subproblems? •Yes. Great, then dynamic programming may work! •Yes, but MST exhibits another powerful property which leads to an even more efficient algorithm.
  • 20. Hallmark for “greedy” algorithms Greedy-choice property A locally optimal choice is globally optimal.
  • 21. Hallmark for “greedy” algorithms Greedy-choice property A locally optimal choice is globally optimal. Theorem. Let T be the MST of G = (V, E), and let A  V. Suppose that (u, v)  E is the least-weight edge connecting A to V – A. Then, (u, v)  T.
  • 22. Proof of theorem  A  V – A u (u, v) = least-weight edge connecting A to V – A Proof. Suppose (u, v)  T. Cut and paste. T: v
  • 23. Proof of theorem  A  V – A u (u, v) = least-weight edge connecting A to V – A Proof. Suppose (u, v)  T. Cut and paste. T: v Consider the unique simple path from u to v in T.
  • 24. Proof of theorem  A  V – A u (u, v) = least-weight edge connecting A to V – A Proof. Suppose (u, v)  T. Cut and paste. T: v Consider the unique simple path from u to v in T. Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V – A.
  • 25. Proof of theorem Proof. Suppose (u, v)  T. Cut and paste. u (u, v) = least-weight edge connecting A to V – A v T :  A  V – A Consider the unique simple path from u to v in T. Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V – A. A lighter-weight spanning tree than T results.
  • 26. Prim’s algorithm IDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least- weight edge connecting it to a vertex in A. Q  V key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   do u  EXTRACT-MIN(Q) for each v  Adj[u] do if v  Q and w(u, v) < key[v] ⊳ DECREASE-KEY then key[v]  w(u, v) [v]  u At the end, {(v, [v])} forms the MST.
  • 27. Example of Prim’s algorithm  A  V – A        6 12 5 14 3 8 10 9 0 15 7
  • 28. Example of Prim’s algorithm  A  V – A        6 12 5 14 3 8 10 9 0 15 7
  • 29. Example of Prim’s algorithm  A  V – A     0 0    6 12 5 14 3 8 10 15 9 7
  • 30. Example of Prim’s algorithm  A  V – A     0 0    6 12 5 14 3 8 10 15 9 7
  • 31. Example of Prim’s algorithm  A  V – A    0 0    6 12  5 14 3 8 10 15 9 7
  • 32. Example of Prim’s algorithm  A  V – A    0 0    6 12  5 14 3 8 10 15 9 7
  • 33. Example of Prim’s algorithm  A  V – A   0 0    6 12  5 14  3 8 10 15 9 7
  • 34. Example of Prim’s algorithm  A  V – A   0 0    6 12  5 14  3 8 10 15 9 7
  • 35. Example of Prim’s algorithm  A  V – A   0 0    6 12  5 14  3 8 10 15 9 7
  • 36. Example of Prim’s algorithm  A  V – A    0 0    6 12  5 14 3 8 10 15 9 7
  • 37. Example of Prim’s algorithm  A  V – A    0 0    6 12  5 14 3 8 10 15 9 7
  • 38. Example of Prim’s algorithm  A  V – A    0 0    6 12  5 14 3 8 10 15 9 7
  • 39. Example of Prim’s algorithm  A  V – A    0 0   6 12  5 14 3 8 10 15  9 7
  • 40. Analysis of Prim Q  V key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   do u  EXTRACT-MIN(Q) for each v  Adj[u] do if v  Q and w(u, v) < key[v] then key[v]  w(u, v) [v]  u
  • 41. Analysis of Prim key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   do u  EXTRACT-MIN(Q) for each v  Adj[u] do if v  Q and w(u, v) < key[v] then key[v]  w(u, v) [v]  u Q  V (V) total
  • 42. Analysis of Prim key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   do u  EXTRACT-MIN(Q) for each v  Adj[u] do if v  Q and w(u, v) < key[v] then key[v]  w(u, v) [v]  u Q  V (V) total |V| times
  • 43. Analysis of Prim key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   do u  EXTRACT-MIN(Q) for each v  Adj[u] do if v  Q and w(u, v) < key[v] then key[v]  w(u, v) [v]  u degree(u) times |V| times Q  V (V) total
  • 44. Analysis of Prim Handshaking Lemma  (E) implicit DECREASE-KEY’s. key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   do u  EXTRACT-MIN(Q) for each v  Adj[u] do if v  Q and w(u, v) < key[v] then key[v]  w(u, v) [v]  u degree(u) times |V| times Q  V (V) total
  • 45. Analysis of Prim key[v]   for all v  V key[s]  0 for some arbitrary s  V while Q   do u  EXTRACT-MIN(Q) for each v  Adj[u] do if v  Q and w(u, v) < key[v] then key[v]  w(u, v) [v]  u degree(u) times |V| times Q  V (V) total Handshaking Lemma  (E) implicit DECREASE-KEY’s. Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
  • 46. MST algorithms Kruskal’s algorithm • Uses the disjoint-set data structure. • Running time = O(E lgV). Best to date: • Karger, Klein, and Tarjan [1993]. • Randomized algorithm. • O(V + E) expected time.