November 13, 20031 Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005.
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.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
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.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Analysis of Algorithms
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
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.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
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 and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Algorithm Analysis Fall 2017 CS 4306/03
Introduction to Algorithms
Minimum Spanning Trees
Lecture 22 Minimum Spanning Tree
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
CS 583 Analysis of Algorithms
Algorithms and Data Structures Lecture XIII
Lecture 12 Algorithm Analysis
Greedy Algorithms Comp 122, Spring 2004.
Introduction to Algorithms: Greedy Algorithms (and Graphs)
Graph Theory Dijkstra's Algorithm.
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University

November 13, This Lecture Weighted Graphs Minimum Spanning Trees Greedy Choice Theorem Kruskal’s Algorithm Prim’s Algorithm

November 13, Spanning Tree A spanning tree of G is a subgraph which is a tree contains all vertices of G How many edges are there in a spanning tree, if there are V vertices?

November 13, Minimum Spanning Trees Undirected, connected graph G = (V,E) Weight function W: E  R (assigning cost or length or other values to edges) Spanning tree: tree that connects all the vertices Optimization problem – Minimum spanning tree (MST): tree T that connects all the vertices and minimizes

November 13, Optimal Substructure ”Cut and paste” argument If G’ would have a cheaper ST T’, then we would get a cheaper ST of G: T’ + (u, v) MST(G) = T u v ”u+v” MST(G’) = T – (u,v)

November 13, Idea for an Algorithm We have to make V – 1 choices (edges of the MST) to arrive at the optimization goal After each choice we have a sub-problem one vertex smaller than the original Dynamic programming algorithm, at each stage, would consider all possible choices (edges) If only we could always guess the correct choice – an edge that definitely belongs to an MST

November 13, Greedy Choice Greedy choice property: locally optimal (greedy) choice yields a globally optimal solution Theorem Let G=(V, E), and let S  V and let (u,v) be min-weight edge in G connecting S to V – S : a light edge crossing a cut Then (u,v)  T – some MST of G

November 13, Greedy Choice (2) Proof Suppose (u,v) is light, but (u,v)  any MST look at path from u to v in some MST T Let (x, y) – the first edge on path from u to v in T that crosses from S to V – S. Swap (x, y) with (u,v) in T. this improves T – contradiction (T supposed to be an MST) u v x y S V-S

November 13, Generic MST Algorithm Generic-MST(G, w) 1 A  // Contains edges that belong to a MST 2 while A does not form a spanning tree do 3 Find an edge (u,v) that is safe for A 4 A  A  {(u,v)} 5 return A Generic-MST(G, w) 1 A  // Contains edges that belong to a MST 2 while A does not form a spanning tree do 3 Find an edge (u,v) that is safe for A 4 A  A  {(u,v)} 5 return A Safe edge – edge that does not destroy A’s property MoreSpecific-MST(G, w) 1 A  // Contains edges that belong to a MST 2 while A does not form a spanning tree do 3.1 Make a cut (S, V-S) of G that respects A 3.2 Take the min-weight edge (u,v) connecting S to V-S 4 A  A  {(u,v)} 5 return A MoreSpecific-MST(G, w) 1 A  // Contains edges that belong to a MST 2 while A does not form a spanning tree do 3.1 Make a cut (S, V-S) of G that respects A 3.2 Take the min-weight edge (u,v) connecting S to V-S 4 A  A  {(u,v)} 5 return A

November 13, Pseudocode assumptions Graph ADT with an operation V():VertexSet E(): EdgeSet w(u:Vertex, v:Vertex):int – returns a weight of (u,v) A looping construct “for each v  V ”, where V is of a type VertexSet, and v is of a type Vertex Vertex ADT with operations: adjacent():VertexSet key():int and setkey(k:int) parent():Vertex and setparent(p:Vertex)

November 13, Prim-Jarnik Algorithm Vertex based algorithm Grows one tree T, one vertex at a time A set A covering the portion of T already computed Label the vertices v outside of the set A with v.key() – the minimum weigth of an edge connecting v to a vertex in A, v.key() = , if no such edge exists

November 13, MST-Prim(G,r) 01 for each vertex u  G.V() 02 u.setkey(  03 u.setparent(NIL) 04 r.setkey(0) 05 Q.init(G.V()) // Q is a priority queue ADT 06 while not Q.isEmpty() 07 u  Q.extractMin() // making u part of T 08 for each v  u.adjacent() do 09 if v  Q and G.w(u,v) < v.key() then 10 v.setkey(G.w(u,v)) 11 Q.modifyKey(v) 12 v.setparent(u) Prim-Jarnik Algorithm (2) updating keys

November 13, Prim-Jarnik’s Example Let’s do an example: HBCIADGFE Let’s keep track: What is set A, which vertices are in Q, what cuts are we making, what are the key values

November 13, MST-Prim(G,r) 01 for each vertex u  G.V() 02 u.setkey(  03 u.setparent(NIL) 04 r.setkey(0) 05 Q.init(G.V()) // Q is a priority queue ADT 06 while not Q.isEmpty() 07 u  Q.extractMin() // making u part of T 08 for each v  u.adjacent() do 09 if v  Q and G.w(u,v) < v.key() then 10 v.setkey(G.w(u,v)) 11 Q.modifyKey(v) 12 v.setparent(u) Implementation issues

November 13, Priority Queues A priority queue is an ADT for maintaining a set S of elements, each with an associated value called key We need PQ to support the following operations init(S:VertexSet) extractMin():Vertex modifyKey(v:Vertex) To choose how to implement a PQ, we need to count how many times the operations are performed: init is performed just once and runs in O(n)

November 13, Prim-Jarnik’s Running Time Time = |V|T(extractMin) + O(E)T(modifyKey) Binary heap implementation: Time = O(V lgV + E lgV) = O(E lgV) QT(extractMin)T(modifyKey)Total arrayO(V)O(V)O(1)O(V 2 ) binary heapO(lg V) O(E lgV ) Fibonacci heap O(lg V)O(1) amortizedO(V lgV +E )

November 13, Greedy Algorithms One more algorithm design technique Greedy algorithms can be used to solve optimization problems, if: There is an optimal substructure We can prove that a greedy choice at each iteration leads to an optimal solution.

November 13, Kruskal's Algorithm Edge based algorithm Add the edges one at a time, in increasing weight order The algorithm maintains A – a forest of trees. An edge is accepted it if connects vertices of distinct trees (the cut respects A)

November 13, Disjoint-Set ADT We need a Disjoint-Set ADT that maintains a partition, i.e., a collection S of disjoint sets. Operations: makeSet(x:Vertex): SetID S  S  {{x}} union(x:Vertex, y:Vertex) – X  S.findSet(x) Y  S.findSet(y) S  S – {X, Y}  {X  Y} findSet(x:Vertex): SetID returns unique X  S, where x  X

November 13, Kruskal's Algorithm The algorithm keeps adding the cheapest edge that connects two trees of the forest MST-Kruskal(G) 01 A   02 S.init() // Init disjoint-set ADT 03 for each vertex v  G.V() 04 S.makeSet(v) 05 sort the edges of G.E() by non-decreasing G.w(u,v) 06 for each edge (u,v)  E, in sorted order do 07 if S.findSet(u)  S.findSet(v) then 08 A  A  {(u,v)} 09 S.union(u,v) 10 return A

November 13, Kruskal’s Example Let’s do an example: HBCIADGFE Let’s keep track: What is set A, what is a collection S, what cuts are we making

November 13, Disjoint Sets as Lists Each set – a list of elements identified by the first element, all elements in the list point to the first element Union – add a smaller list to a larger one FindSet: O(1), Union(u,v): O(min{|C(u)|, |C(v)|}) 123 ABC  4  123 ABC  4

November 13, Kruskal Running Time Initialization O(V) time Sorting the edges  (E lg E) =  (E lg V) (why?) O(E) calls to FindSet Union costs Let t(v) be the number of times v is moved to a new cluster Each time a vertex is moved to a new cluster the size of the cluster containing the vertex at least doubles: t(v)  log V Total time spent doing Union Total time: O(E lg V)

November 13, Next Lecture Shortest Paths in Weighted Graphs