Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China www.jiahenglu.net.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms 6.046J/18.401J/SMA5503
Advertisements

Introduction to Algorithms Spanning Trees
Fibonacci Numbers F n = F n-1 + F n-2 F 0 =0, F 1 =1 – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 … Straightforward recursive procedure is slow! Why? How slow? Lets.
Chapter 23 Minimum Spanning Tree
Single Source Shortest Paths
Greed is good. (Some of the time)
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Introduction to Algorithms 6.046J/18.401J/SMA5503
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 7-MST Minimal Spanning Trees Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Shortest Paths Definitions Single Source Algorithms
1 Graph Algorithms Single source shortest paths problem Dana Shapira.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Topological Sorting and Least-cost Path Algorithms.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
Lecture 13 Algorithm Analysis
Minimum- Spanning Trees
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Shortest Paths and Minimum Spanning Trees
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
CS200: Algorithm Analysis
Minimum Spanning Trees
Many slides here are based on D. Luebke slides
CS 332: Algorithms Dijkstra’s Algorithm Continued Disjoint-Set Union
CS6045: Advanced Algorithms
Algorithms and Data Structures Lecture XII
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Paths and Minimum Spanning Trees
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CS 332: Algorithms Amortized Analysis Continued
CS 3013: DS & Algorithms Shortest Paths.
Review of MST Algorithms Disjoint-Set Union Amortized Analysis
Presentation transcript:

Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China

Review: Single-Source Shortest Path Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v  “ Shortest-path ” = minimum weight  Weight of path is sum of edges  E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville?

Review: Shortest Path Properties Optimal substructure: the shortest path consists of shortest subpaths Let  (u,v) be the weight of the shortest path from u to v. Shortest paths satisfy the triangle inequality:  (u,v)   (u,x) +  (x,v) In graphs with negative weight cycles, some shortest paths will not exist

Review: Relaxation Key technique: relaxation  Maintain upper bound d[v] on  (s,v): Relax(u,v,w) { if (d[v] > d[u]+w) then d[v]=d[u]+w; } Relax

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: 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 What will be the running time?

Review: Bellman-Ford Running time: O(VE)  Not so good for large dense graphs  But a very practical algorithm in many ways Note that order in which edges are processed affects how quickly it converges (show example)

DAG Shortest Paths Problem: finding shortest paths in DAG  Bellman-Ford takes O(VE) time.  How can we do better?  Idea: use topological sort. How does it work again? If were lucky and processes vertices on each shortest path from left to right, 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. What will be the running time?

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 DA Ex: run the algorithm

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 DA 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 p2p2 p2p2

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 p2p2 p2p2

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)); } 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)); } 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)); } 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)); } ? 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)); } 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? 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)); } 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)); } ? 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)); } 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)); } ? 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)); } 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)); } ? 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)); } 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)); } ? 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)); } 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)); } ? 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)); } 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)); } ? 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? 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)); } ? 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)); } ? 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)); } 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)); } 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 timeresulting 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