SlideShare a Scribd company logo
CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union
Homework 5 Check web page this afternoon…
Review: Bellman-Ford Algorithm BellmanFord() for each v    V d[v] =   ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)    E Relax(u,v, w(u,v)); for each edge (u,v)    E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w Initialize d[], which will converge to  shortest-path value   Relaxation:  Make |V|-1 passes,  relaxing each edge Test for solution: have we converged yet? Ie,    negative cycle?
Review: DAG Shortest Paths Problem: finding shortest paths in DAG Bellman-Ford takes O(VE) time.  Do better using topological sort.  Idea: if were lucky and processes vertices on each shortest path in order, B-F would be done in one pass Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert).  Thus: just one pass.  Running time: O(V+E)
Review: Dijkstra’s Algorithm If no negative edge weights, we can beat BF Similar to breadth-first search Grow a tree gradually, advancing from vertices taken from a queue Also similar to Prim’s algorithm for MST Use a priority queue keyed on d[v]
Dijkstra’s Algorithm Dijkstra(G) for each v    V d[v] =   ; d[s] = 0; S =   ; Q = V; while (Q      ) u = ExtractMin(Q); S = S  U  {u}; for each v    u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Relaxation Step Note: this is really a  call to Q->DecreaseKey() B C D A 10 4 3 2 1 5 Ex: run the algorithm
Dijkstra’s Algorithm Dijkstra(G) for each v    V d[v] =   ; d[s] = 0; S =   ; Q = V; while (Q      ) u = ExtractMin(Q); S = S  U  {u}; for each v    u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); How many times is  ExtractMin() called? How many times is  DecreaseKey() called? What will be the total running time?
Dijkstra’s Algorithm Dijkstra(G) for each v    V d[v] =   ; d[s] = 0; S =   ; Q = V; while (Q      ) u = ExtractMin(Q); S = S  U  {u}; for each v    u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); How many times is  ExtractMin() called? How many times is  DecraseKey() called? A: O(E lg V) using binary heap for Q Can acheive O(V lg V + E) with Fibonacci heaps
Dijkstra’s Algorithm Dijkstra(G) for each v    V d[v] =   ; d[s] = 0; S =   ; Q = V; while (Q      ) u = ExtractMin(Q); S = S  U {u}; for each v    u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Correctness: we must show that when u is  removed from Q, it has already converged
Correctness Of Dijkstra's Algorithm Note that d[v]      (s,v)   v  Let u be first vertex picked s.t.    shorter path than d[u]   d[u] >   (s,u) Let y be first vertex   V-S on actual shortest path from s  u    d[y] =   (s,y) Because d[x] is set correctly for y's predecessor x    S on the shortest path, and When we put x into S, we relaxed (x,y), giving d[y] the correct value s x y u p 2 p 2
Correctness Of Dijkstra's Algorithm Note that d[v]      (s,v)   v  Let u be first vertex picked s.t.    shorter path than d[u]   d[u] >   (s,u) Let y be first vertex   V-S on actual shortest path from s  u    d[y] =   (s,y) d[u] >   (s,u) =   (s,y) +   (y,u)  ( Why? ) = d[y] +   (y,u)   d[y] But if d[u] > d[y], wouldn't have chosen u.  Contradiction. s x y u p 2 p 2
Disjoint-Set Union Problem Want a data structure to support disjoint sets  Collection of disjoint sets  S  = {S i }, S i   ∩  S j  =   Need to support following operations: MakeSet(x):  S  =  S   U  {{x}} Union(S i , S j ):  S = S  - {S i , S j }  U  {S i   U  S j } FindSet(X): return S i      S  such that x    S i Before discussing implementation details, we look at example application: MSTs
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); }
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1? 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2? 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5? 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8? 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9? 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13? 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14? 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17? 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19? 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21? Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25? 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Kruskal’s Algorithm Kruskal() {  T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
Correctness Of Kruskal’s Algorithm Sketch of a proof that this algorithm produces an MST for  T : Assume algorithm is wrong: result is not an MST Then algorithm adds a wrong edge at some point If it adds a wrong edge, there must be a lower weight edge (cut and paste argument) But algorithm chooses lowest weight edge at each step.  Contradiction Again, important to be comfortable with cut and paste arguments
Kruskal’s Algorithm Kruskal() { T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } What will affect the running time?
Kruskal’s Algorithm Kruskal() { T =   ; for each v    V MakeSet(v); sort E by increasing edge weight w for each (u,v)    E (in sorted order) if FindSet(u)    FindSet(v) T = T  U  {{u,v}}; Union(FindSet(u), FindSet(v)); } What will affect the running time? 1 Sort O(V) MakeSet() calls O(E) FindSet() calls O(V) Union() calls  ( Exactly how many Union()s? )
Kruskal’s Algorithm: Running Time To summarize:  Sort edges: O(E lg E)  O(V) MakeSet()’s O(E) FindSet()’s O(V) Union()’s  Upshot:  Best disjoint-set union algorithm makes above 3 operations take O(E  (E,V)),    almost constant Overall thus O(E lg E), almost linear w/o sorting
Disjoint Set Union So how do we implement disjoint-set union? Naïve implementation: use a linked list to represent each set: MakeSet():  ???  time FindSet():  ???  time Union(A,B): “copy” elements of A into B:  ???  time
Disjoint Set Union So how do we implement disjoint-set union? Naïve implementation: use a linked list to represent each set: MakeSet(): O(1) time FindSet(): O(1) time Union(A,B): “copy” elements of A into B: O(A) time How long can a single Union() take? How long will n Union()’s take?
Disjoint Set Union: Analysis Worst-case analysis: O(n 2 ) time for n Union’s Union(S 1 , S 2 ) “copy”  1 element Union(S 2 , S 3 ) “copy” 2 elements … Union(S n-1 , S n ) “copy” n-1 elements O(n 2 ) Improvement: always copy smaller into larger Why will this make things better? What is the worst-case time of Union()? But now n Union’s take only O(n lg n) time!
Amortized Analysis of Disjoint Sets Amortized analysis  computes average times without using probability With our new Union(), any individual element is copied at most  lg n  times when forming the complete set from 1-element sets Worst case: Each time copied, element in smaller set 1st time resulting set size   2 2nd time   4 … (lg n)th time   n
Amortized Analysis of Disjoint Sets Since we have n elements each copied at most lg n times, n Union()’s takes O(n lg n) time We say that each Union() takes O(lg n)  amortized time Financial term: imagine paying $(lg n) per Union At first we are overpaying; initial Union $O(1) But we accumulate enough $ in bank to pay for later expensive O(n) operation.  Important: amount in bank never goes negative
The End
Ad

More Related Content

What's hot (20)

Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
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
 
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt AlgorithmString Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
Kiran K
 
Proyecto parcial iii_ proyecciones lineales
Proyecto parcial iii_ proyecciones linealesProyecto parcial iii_ proyecciones lineales
Proyecto parcial iii_ proyecciones lineales
JOSUESANTIAGOPILLAJO
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
Pankaj Thakur
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
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
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
Ayesha Tahir
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Topological sorting
Topological sortingTopological sorting
Topological sorting
Amit Kumar Rathi
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
Tareq Hasan
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
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
 
Shortest path
Shortest pathShortest path
Shortest path
Farah Shaikh
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
Roshan Tailor
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
Traian Rebedea
 
Dijksatra
DijksatraDijksatra
Dijksatra
Tanmay Baranwal
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
Traian Rebedea
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpath
Carlostheran
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
Krish_ver2
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
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
 
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt AlgorithmString Matching with Finite Automata and Knuth Morris Pratt Algorithm
String Matching with Finite Automata and Knuth Morris Pratt Algorithm
Kiran K
 
Proyecto parcial iii_ proyecciones lineales
Proyecto parcial iii_ proyecciones linealesProyecto parcial iii_ proyecciones lineales
Proyecto parcial iii_ proyecciones lineales
JOSUESANTIAGOPILLAJO
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
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
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
Ayesha Tahir
 
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
 
Single source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstraSingle source stortest path bellman ford and dijkstra
Single source stortest path bellman ford and dijkstra
Roshan Tailor
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
Traian Rebedea
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
Traian Rebedea
 
My presentation all shortestpath
My presentation all shortestpathMy presentation all shortestpath
My presentation all shortestpath
Carlostheran
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
Krish_ver2
 

Viewers also liked (9)

Review and evaluations of shortest path algorithms
Review and evaluations of shortest path algorithmsReview and evaluations of shortest path algorithms
Review and evaluations of shortest path algorithms
Pawan Kumar Tiwari
 
COMPARISON BETWEEN THE GENETIC ALGORITHMS OPTIMIZATION AND PARTICLE SWARM OPT...
COMPARISON BETWEEN THE GENETIC ALGORITHMS OPTIMIZATION AND PARTICLE SWARM OPT...COMPARISON BETWEEN THE GENETIC ALGORITHMS OPTIMIZATION AND PARTICLE SWARM OPT...
COMPARISON BETWEEN THE GENETIC ALGORITHMS OPTIMIZATION AND PARTICLE SWARM OPT...
IAEME Publication
 
Major 2 final prsesentation
Major 2 final prsesentationMajor 2 final prsesentation
Major 2 final prsesentation
Siddharth Bharadwaj
 
1996 reliability optimization of series-parallel systems using a genetic algo...
1996 reliability optimization of series-parallel systems using a genetic algo...1996 reliability optimization of series-parallel systems using a genetic algo...
1996 reliability optimization of series-parallel systems using a genetic algo...
jiing deng
 
Second Project PPT
Second Project PPTSecond Project PPT
Second Project PPT
Amar Dhillon
 
B tree
B treeB tree
B tree
Tech_MX
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Anuj Modi
 
Genetic Algorithm by Example
Genetic Algorithm by ExampleGenetic Algorithm by Example
Genetic Algorithm by Example
Nobal Niraula
 
Review and evaluations of shortest path algorithms
Review and evaluations of shortest path algorithmsReview and evaluations of shortest path algorithms
Review and evaluations of shortest path algorithms
Pawan Kumar Tiwari
 
COMPARISON BETWEEN THE GENETIC ALGORITHMS OPTIMIZATION AND PARTICLE SWARM OPT...
COMPARISON BETWEEN THE GENETIC ALGORITHMS OPTIMIZATION AND PARTICLE SWARM OPT...COMPARISON BETWEEN THE GENETIC ALGORITHMS OPTIMIZATION AND PARTICLE SWARM OPT...
COMPARISON BETWEEN THE GENETIC ALGORITHMS OPTIMIZATION AND PARTICLE SWARM OPT...
IAEME Publication
 
1996 reliability optimization of series-parallel systems using a genetic algo...
1996 reliability optimization of series-parallel systems using a genetic algo...1996 reliability optimization of series-parallel systems using a genetic algo...
1996 reliability optimization of series-parallel systems using a genetic algo...
jiing deng
 
Second Project PPT
Second Project PPTSecond Project PPT
Second Project PPT
Amar Dhillon
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
Anuj Modi
 
Genetic Algorithm by Example
Genetic Algorithm by ExampleGenetic Algorithm by Example
Genetic Algorithm by Example
Nobal Niraula
 
Ad

Similar to lecture 22 (20)

lec6.pptx
lec6.pptxlec6.pptx
lec6.pptx
nuredinabdellah2
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
prithivr1
 
Independent domination in finitely defined classes of graphs polynomial algor...
Independent domination in finitely defined classes of graphs polynomial algor...Independent domination in finitely defined classes of graphs polynomial algor...
Independent domination in finitely defined classes of graphs polynomial algor...
政謙 陳
 
Kruskal's algorithm
Kruskal's algorithmKruskal's algorithm
Kruskal's algorithm
Raj Kumar Ranabhat
 
kruskal Algorithm in Algorithm for CS St
kruskal Algorithm in Algorithm for CS Stkruskal Algorithm in Algorithm for CS St
kruskal Algorithm in Algorithm for CS St
DhrubajyotiDas70
 
minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
sachin varun
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Amit Kumar Rathi
 
Lec-35Graph - Copy Graph therory in Data strucure
Lec-35Graph - Copy Graph therory in Data strucureLec-35Graph - Copy Graph therory in Data strucure
Lec-35Graph - Copy Graph therory in Data strucure
Anil Yadav
 
Single Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.pptSingle Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.ppt
RAJASEKARAN G
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clustering
ASPAK2014
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
Yi-Lung Tsai
 
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
JamesMa54
 
Dijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.pptDijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
RAJASEKARAN G
 
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra algorithm  ds 57612334t4t44.pptDijkstra algorithm  ds 57612334t4t44.ppt
Dijkstra algorithm ds 57612334t4t44.ppt
ssuser7b9bda1
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
B.Kirron Reddi
 
Introduction to Treewidth
Introduction to TreewidthIntroduction to Treewidth
Introduction to Treewidth
ASPAK2014
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
2020 preTEST5A
2020 preTEST5A2020 preTEST5A
2020 preTEST5A
A Jorge Garcia
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysislecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
Chap10alg
Chap10algChap10alg
Chap10alg
Munkhchimeg
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
prithivr1
 
Independent domination in finitely defined classes of graphs polynomial algor...
Independent domination in finitely defined classes of graphs polynomial algor...Independent domination in finitely defined classes of graphs polynomial algor...
Independent domination in finitely defined classes of graphs polynomial algor...
政謙 陳
 
kruskal Algorithm in Algorithm for CS St
kruskal Algorithm in Algorithm for CS Stkruskal Algorithm in Algorithm for CS St
kruskal Algorithm in Algorithm for CS St
DhrubajyotiDas70
 
minimum spanning trees Algorithm
minimum spanning trees Algorithm minimum spanning trees Algorithm
minimum spanning trees Algorithm
sachin varun
 
Lec-35Graph - Copy Graph therory in Data strucure
Lec-35Graph - Copy Graph therory in Data strucureLec-35Graph - Copy Graph therory in Data strucure
Lec-35Graph - Copy Graph therory in Data strucure
Anil Yadav
 
Single Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.pptSingle Source Shortest Path Algorithm.ppt
Single Source Shortest Path Algorithm.ppt
RAJASEKARAN G
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clustering
ASPAK2014
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
Yi-Lung Tsai
 
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...Fast and efficient exact synthesis of single qubit unitaries generated by cli...
Fast and efficient exact synthesis of single qubit unitaries generated by cli...
JamesMa54
 
Dijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.pptDijkstra Shortest Path Algorithm in Network.ppt
Dijkstra Shortest Path Algorithm in Network.ppt
RAJASEKARAN G
 
Dijkstra algorithm ds 57612334t4t44.ppt
Dijkstra algorithm  ds 57612334t4t44.pptDijkstra algorithm  ds 57612334t4t44.ppt
Dijkstra algorithm ds 57612334t4t44.ppt
ssuser7b9bda1
 
Introduction to Treewidth
Introduction to TreewidthIntroduction to Treewidth
Introduction to Treewidth
ASPAK2014
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
lecture 23 algorithm design and analysis
lecture 23 algorithm design and analysislecture 23 algorithm design and analysis
lecture 23 algorithm design and analysis
bluebirdrish666
 
Ad

More from sajinsc (20)

lecture 30
lecture 30lecture 30
lecture 30
sajinsc
 
lecture 29
lecture 29lecture 29
lecture 29
sajinsc
 
lecture 28
lecture 28lecture 28
lecture 28
sajinsc
 
lecture 27
lecture 27lecture 27
lecture 27
sajinsc
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
lecture 25
lecture 25lecture 25
lecture 25
sajinsc
 
lecture 24
lecture 24lecture 24
lecture 24
sajinsc
 
lecture 23
lecture 23lecture 23
lecture 23
sajinsc
 
lecture 19
lecture 19lecture 19
lecture 19
sajinsc
 
lecture 18
lecture 18lecture 18
lecture 18
sajinsc
 
lecture 17
lecture 17lecture 17
lecture 17
sajinsc
 
lecture 16
lecture 16lecture 16
lecture 16
sajinsc
 
lecture 15
lecture 15lecture 15
lecture 15
sajinsc
 
lecture 14
lecture 14lecture 14
lecture 14
sajinsc
 
lecture 13
lecture 13lecture 13
lecture 13
sajinsc
 
lecture 12
lecture 12lecture 12
lecture 12
sajinsc
 
lecture 11
lecture 11lecture 11
lecture 11
sajinsc
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 
lecture 9
lecture 9lecture 9
lecture 9
sajinsc
 
lecture 8
lecture 8lecture 8
lecture 8
sajinsc
 
lecture 30
lecture 30lecture 30
lecture 30
sajinsc
 
lecture 29
lecture 29lecture 29
lecture 29
sajinsc
 
lecture 28
lecture 28lecture 28
lecture 28
sajinsc
 
lecture 27
lecture 27lecture 27
lecture 27
sajinsc
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
lecture 25
lecture 25lecture 25
lecture 25
sajinsc
 
lecture 24
lecture 24lecture 24
lecture 24
sajinsc
 
lecture 23
lecture 23lecture 23
lecture 23
sajinsc
 
lecture 19
lecture 19lecture 19
lecture 19
sajinsc
 
lecture 18
lecture 18lecture 18
lecture 18
sajinsc
 
lecture 17
lecture 17lecture 17
lecture 17
sajinsc
 
lecture 16
lecture 16lecture 16
lecture 16
sajinsc
 
lecture 15
lecture 15lecture 15
lecture 15
sajinsc
 
lecture 14
lecture 14lecture 14
lecture 14
sajinsc
 
lecture 13
lecture 13lecture 13
lecture 13
sajinsc
 
lecture 12
lecture 12lecture 12
lecture 12
sajinsc
 
lecture 11
lecture 11lecture 11
lecture 11
sajinsc
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 
lecture 9
lecture 9lecture 9
lecture 9
sajinsc
 
lecture 8
lecture 8lecture 8
lecture 8
sajinsc
 

Recently uploaded (20)

2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo SlidesHow to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo Slides
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
"Heraldry Detective Project"- Coats of Arms and Mottos of "Ivanhoe" in Ivanho...
"Heraldry Detective Project"- Coats of Arms and Mottos of "Ivanhoe" in Ivanho..."Heraldry Detective Project"- Coats of Arms and Mottos of "Ivanhoe" in Ivanho...
"Heraldry Detective Project"- Coats of Arms and Mottos of "Ivanhoe" in Ivanho...
ruslana1975
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
How to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo SlidesHow to Add Button in Chatter in Odoo 18 - Odoo Slides
How to Add Button in Chatter in Odoo 18 - Odoo Slides
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptxUnit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Unit 5 ACUTE, SUBACUTE,CHRONIC TOXICITY.pptx
Mayuri Chavan
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
"Heraldry Detective Project"- Coats of Arms and Mottos of "Ivanhoe" in Ivanho...
"Heraldry Detective Project"- Coats of Arms and Mottos of "Ivanhoe" in Ivanho..."Heraldry Detective Project"- Coats of Arms and Mottos of "Ivanhoe" in Ivanho...
"Heraldry Detective Project"- Coats of Arms and Mottos of "Ivanhoe" in Ivanho...
ruslana1975
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 

lecture 22

  • 1. CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union
  • 2. Homework 5 Check web page this afternoon…
  • 3. Review: Bellman-Ford Algorithm BellmanFord() for each v  V d[v] =  ; d[s] = 0; for i=1 to |V|-1 for each edge (u,v)  E Relax(u,v, w(u,v)); for each edge (u,v)  E if (d[v] > d[u] + w(u,v)) return “no solution”; Relax(u,v,w): if (d[v] > d[u]+w) then d[v]=d[u]+w Initialize d[], which will converge to shortest-path value  Relaxation: Make |V|-1 passes, relaxing each edge Test for solution: have we converged yet? Ie,  negative cycle?
  • 4. Review: DAG Shortest Paths Problem: finding shortest paths in DAG Bellman-Ford takes O(VE) time. Do better using topological sort. Idea: if were lucky and processes vertices on each shortest path in order, B-F would be done in one pass Every path in a dag is subsequence of topologically sorted vertex order, so processing verts in that order, we will do each path in forward order (will never relax edges out of vert before doing all edges into vert). Thus: just one pass. Running time: O(V+E)
  • 5. Review: Dijkstra’s Algorithm If no negative edge weights, we can beat BF Similar to breadth-first search Grow a tree gradually, advancing from vertices taken from a queue Also similar to Prim’s algorithm for MST Use a priority queue keyed on d[v]
  • 6. Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Relaxation Step Note: this is really a call to Q->DecreaseKey() B C D A 10 4 3 2 1 5 Ex: run the algorithm
  • 7. Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); How many times is ExtractMin() called? How many times is DecreaseKey() called? What will be the total running time?
  • 8. Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); How many times is ExtractMin() called? How many times is DecraseKey() called? A: O(E lg V) using binary heap for Q Can acheive O(V lg V + E) with Fibonacci heaps
  • 9. Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Correctness: we must show that when u is removed from Q, it has already converged
  • 10. Correctness Of Dijkstra's Algorithm Note that d[v]   (s,v)  v Let u be first vertex picked s.t.  shorter path than d[u]  d[u] >  (s,u) Let y be first vertex  V-S on actual shortest path from s  u  d[y] =  (s,y) Because d[x] is set correctly for y's predecessor x  S on the shortest path, and When we put x into S, we relaxed (x,y), giving d[y] the correct value s x y u p 2 p 2
  • 11. Correctness Of Dijkstra's Algorithm Note that d[v]   (s,v)  v Let u be first vertex picked s.t.  shorter path than d[u]  d[u] >  (s,u) Let y be first vertex  V-S on actual shortest path from s  u  d[y] =  (s,y) d[u] >  (s,u) =  (s,y) +  (y,u) ( Why? ) = d[y] +  (y,u)  d[y] But if d[u] > d[y], wouldn't have chosen u. Contradiction. s x y u p 2 p 2
  • 12. Disjoint-Set Union Problem Want a data structure to support disjoint sets Collection of disjoint sets S = {S i }, S i ∩ S j =  Need to support following operations: MakeSet(x): S = S U {{x}} Union(S i , S j ): S = S - {S i , S j } U {S i U S j } FindSet(X): return S i  S such that x  S i Before discussing implementation details, we look at example application: MSTs
  • 13. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); }
  • 14. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 15. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 16. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 17. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1? 5 13 17 25 14 8 21 Run the algorithm:
  • 18. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 19. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2? 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 20. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 21. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5? 13 17 25 14 8 21 Run the algorithm:
  • 22. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 23. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8? 21 Run the algorithm:
  • 24. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 25. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9? 1 5 13 17 25 14 8 21 Run the algorithm:
  • 26. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 27. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13? 17 25 14 8 21 Run the algorithm:
  • 28. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 29. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14? 8 21 Run the algorithm:
  • 30. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 31. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17? 25 14 8 21 Run the algorithm:
  • 32. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19? 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 33. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21? Run the algorithm:
  • 34. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25? 14 8 21 Run the algorithm:
  • 35. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 36. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19 9 1 5 13 17 25 14 8 21 Run the algorithm:
  • 37. Correctness Of Kruskal’s Algorithm Sketch of a proof that this algorithm produces an MST for T : Assume algorithm is wrong: result is not an MST Then algorithm adds a wrong edge at some point If it adds a wrong edge, there must be a lower weight edge (cut and paste argument) But algorithm chooses lowest weight edge at each step. Contradiction Again, important to be comfortable with cut and paste arguments
  • 38. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } What will affect the running time?
  • 39. Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } What will affect the running time? 1 Sort O(V) MakeSet() calls O(E) FindSet() calls O(V) Union() calls ( Exactly how many Union()s? )
  • 40. Kruskal’s Algorithm: Running Time To summarize: Sort edges: O(E lg E) O(V) MakeSet()’s O(E) FindSet()’s O(V) Union()’s Upshot: Best disjoint-set union algorithm makes above 3 operations take O(E  (E,V)),  almost constant Overall thus O(E lg E), almost linear w/o sorting
  • 41. Disjoint Set Union So how do we implement disjoint-set union? Naïve implementation: use a linked list to represent each set: MakeSet(): ??? time FindSet(): ??? time Union(A,B): “copy” elements of A into B: ??? time
  • 42. Disjoint Set Union So how do we implement disjoint-set union? Naïve implementation: use a linked list to represent each set: MakeSet(): O(1) time FindSet(): O(1) time Union(A,B): “copy” elements of A into B: O(A) time How long can a single Union() take? How long will n Union()’s take?
  • 43. Disjoint Set Union: Analysis Worst-case analysis: O(n 2 ) time for n Union’s Union(S 1 , S 2 ) “copy” 1 element Union(S 2 , S 3 ) “copy” 2 elements … Union(S n-1 , S n ) “copy” n-1 elements O(n 2 ) Improvement: always copy smaller into larger Why will this make things better? What is the worst-case time of Union()? But now n Union’s take only O(n lg n) time!
  • 44. Amortized Analysis of Disjoint Sets Amortized analysis computes average times without using probability With our new Union(), any individual element is copied at most lg n times when forming the complete set from 1-element sets Worst case: Each time copied, element in smaller set 1st time resulting set size  2 2nd time  4 … (lg n)th time  n
  • 45. Amortized Analysis of Disjoint Sets Since we have n elements each copied at most lg n times, n Union()’s takes O(n lg n) time We say that each Union() takes O(lg n) amortized time Financial term: imagine paying $(lg n) per Union At first we are overpaying; initial Union $O(1) But we accumulate enough $ in bank to pay for later expensive O(n) operation. Important: amount in bank never goes negative
  翻译: