1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.

Slides:



Advertisements
Similar presentations
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Advertisements

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
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
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.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
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.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
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.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Graph (II) Shortest path, Minimum spanning tree GGuy
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 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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
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)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
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.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Introduction to Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Challenging Problem 2: Challenging problem 2 will be given on the website (week 4) at 9:30 am on Saturday (Oct 1, 2005). 2. The due time is Sunday (Oct.
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Tree.
Announcement 2: A 2 hour midterm (open book) will be given on March (Tuesday) during the lecture time. 2018/12/4.
Page 620 Single-Source Shortest Paths
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
Minimum-Cost Spanning Tree
2018/12/27 chapter25.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Minimum Spanning Tree.
Lecture 12 Algorithm Analysis
Single-Source Shortest Path & Minimum Spanning Trees
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Chapter 9: Graphs Spanning Trees
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Presentation transcript:

1 Greedy 2 Jose Rolim University of Geneva

Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra

Algorithmique Greedy 2Jose Rolim3 Definition MST  Given a connected graph G = (V, E), with weight function w : E --> R  Min-weight connected subgraph  Spanning tree T:  A tree that includes all nodes from V  T = (V, E’), where E’  E  Weight of T: W( T ) =  w(e)  Minimum spanning tree (MST):  A tree with minimum weight among all spanning trees

Algorithmique Greedy 2Jose Rolim4 Example:

Algorithmique Greedy 2Jose Rolim5 MST:  MST for given G may not be unique  Since MST is a spanning tree:  # edges : |V| - 1  If the graph is unweighted:  All spanning trees have same weight

Algorithmique Greedy 2Jose Rolim6 Kruskal’s algorithm (Greedy)  Start with A empty, and each vertex being its own connected component  Repeatedly merge two components by connecting them with a light edge crossing them  Two issues:  Maintain sets of components  Choose light edges

Algorithmique Greedy 2Jose Rolim7 Pseudo-code

Algorithmique Greedy 2Jose Rolim8 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim9 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim10 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim11 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim12 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim13 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim14 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim15 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim16 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim17 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim18 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim19 The Greedy Algorithm in Action

Algorithmique Greedy 2Jose Rolim20 The Greedy Algorithm in Action

The Greedy Algorithm in Action Node First root node

The Greedy Algorithm in Action Node First

The Greedy Algorithm in Action Node First

The Greedy Algorithm in Action Node First

The Greedy Algorithm in Action Node First

The Greedy Algorithm in Action Node First

Algorithmique Greedy 2Jose Rolim27 Complexity  Time complexity:  #make-set, find-set and union operations: O(|V| + |E|) O( (|V| + |E|)  (|V| + |E|))  Sorting: O(|E| log |E|) = O(|E| log |V|)  Total: O(|E| log |V|)

Algorithmique Greedy 2Jose Rolim28 Prim’s algorithm (also Greedy)  Start with an arbitrary node from V  Instead of maintaining a forest, grow a MST  At any time, maintain a MST for V’  V  At any moment, find a light edge connecting V’ with (V-V’)  I.e., the edge with smallest weight connecting some vertex in V’ with some vertex in V-V’ !

Algorithmique Greedy 2Jose Rolim29 Issues:  Again two issues:  Maintain the tree already build at any moment Easy: simply a tree rooted at r : the starting node  Find the next light edge efficiently For v  V - V’, define key(v) = the min distance between v and some node from V’ At any moment, find the node with min key.

Algorithmique Greedy 2Jose Rolim30 Pseudo-code

Algorithmique Greedy 2Jose Rolim31 Prim’s Algorithm in Action The minimum cost arc from yellow nodes to green nodes can be found by placing arc values in a priority queue.

Algorithmique Greedy 2Jose Rolim32 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim33 20 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim34 20 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim35 20 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim36 20 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim37 20 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim38 20 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim39 20 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim40 20 Prim’s Algorithm in Action

Algorithmique Greedy 2Jose Rolim41 Complexity  Time complexity  # insert: O(|V|)  # Decrease-Key: O( |E|)  # Extract-Min O( |V| )  Using heap for priority queue:  Each operation is O ( log |V| )  Total time complexity: O (|E| log |V|)

Algorithmique Greedy 2Jose Rolim42 Single-Source Shortest Paths  Problem Definition  Shortest paths  Dijkstra’s algorithm (can be viewed as a greedy algorithm)

Algorithmique Greedy 2Jose Rolim43 Problem Definition:  Real problem: A motorist wishes to find the shortest possible route from Chicago to Boston.Given a road map of the United States on which the distance between each pair of adjacent intersections is marked, how can we determine this shortest route?  Formal definition: Given a graph G=(V, E, W), where each edge has a weight, find a shortest path from s to v for some interesting vertices s and v.  s—source  v—destination.

Algorithmique Greedy 2Jose Rolim44 Shortest path:  The weight of path p= is the sum of the weights of its constituent edges: The cost of the shortest path from s to v is denoted as  (s, v).

Algorithmique Greedy 2Jose Rolim45 Representing shortest paths:  we maintain for each vertex v  V, a predecessor [ v] that is the vertex in the shortest path right before v.  With the values of, a backtracking process can give the shortest path. (We will discuss that after the algorithm is given)

Algorithmique Greedy 2Jose Rolim46 Dijkstra’s Algorithm:  Dijkstra’s algorithm assumes that w(e)  0 for each e in the graph.  maintain a set S of vertices such that  Every vertex v  S, d[v]=  (s, v), i.e., the shortest-path from s to v has been found. (Intial values: S=empty, d[s]=0 and d[v]=  )  (a) select the vertex u  V-S such that d[u]=min {d[x]|x  V-S}. Set S=S  {u} (b) for each node v adjacent to u do RELAX(u, v, w).  Repeat step (a) and (b) until S=V.

Algorithmique Greedy 2Jose Rolim47 Implementation:  a priority queue Q stores vertices in V-S, keyed by their d[] values.  the graph G is represented by adjacency lists.

Algorithmique Greedy 2Jose Rolim s uv x y (a)

Algorithmique Greedy 2Jose Rolim49 0 5/s 10/s s uv x y 8 8 (b) (s,x) is the shortest path using one edge. It is also the shortest path from s to x.

Algorithmique Greedy 2Jose Rolim50 0 7/x 14/x 5/s 8/x s uv x y (c)

Algorithmique Greedy 2Jose Rolim51 0 7/x 13/y 5/s 8/x s uv x y (d)

Algorithmique Greedy 2Jose Rolim52 0 7/x 9/u 5/s 8/x s uv x y (e)

Algorithmique Greedy 2Jose Rolim53 Time complexity of Dijkstra’s Algorithm:  Time complexity:  #Extract-min: O(|V|)  #Decrease-key = #Relax: O(|E|)  Use binary heap for priority heap: Time complexity: O((|V| + |E|) log |V|)  Use Fibonacci heap for priority heap: Amortized time complexity: O(|V| log |V| + |E|)  Greedy algorithm:  Any moment, select the lightest vertex in V-S to add to set S