The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
Chapter 7: Greedy Algorithms 7.4 Finding the Shortest Path Dijkstra’s Algorithm pp
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
The Shortest Path Problem
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Dijkstra's algorithm.
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.
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Week 4 Single Source Shortest Paths All Pairs Shortest Path Problem.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
Divide-and-Conquer & Dynamic Programming Divide-and-Conquer: Divide a problem to independent subproblems, find the solutions of the subproblems, and then.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Introduction to Algorithms Jiafen Liu Sept
D IJKSTRA ' S ALGORITHM. S INGLE -S OURCE S HORTEST P ATH P ROBLEM Single-Source Shortest Path Problem - The problem of finding shortest paths from a.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Lecture 13 Shortest Path.
Chapter 7: Greedy Algorithms
Shortest Path Problems
Shortest Path Problems
Shortest Path Graph represents highway system Edges have weights
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Minimum-Cost Spanning Tree
ICS 353: Design and Analysis of Algorithms
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Analysis and design of algorithm
CSE 373: Data Structures and Algorithms
Autumn 2015 Lecture 10 Minimum Spanning Trees
Shortest Path Problems
Shortest Path Algorithms
All pairs shortest path problem
ICS 353: Design and Analysis of Algorithms
Shortest Path Problems
Autumn 2016 Lecture 10 Minimum Spanning Trees
Shortest Path Problems
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Lecture 12 Shortest Path.
Winter 2019 Lecture 10 Minimum Spanning Trees
Minimum-Cost Spanning Tree
More Graphs Lecture 19 CS2110 – Fall 2009.
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

The Greedy Approach Chapter 8

The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions which may lead to optimal global one. Walk slowly but surely. At each step choose the best choice & hopefully it will be part of the optimal global solution.

The Single-source Shortest Path Problem Section 8.2

Problem Let G=(V,E) be a directed weighted graph where each edge has a length  0. Let s  V be a certain vertex called the source. For simplicity, let V={1,2,..,n}, s=1 Find all  [u] =distance from s to any vertex u.

Dijkstra’s Solution Divide V into two disjoint sets X and Y where X contains all vertices whose distances from 1have been already determined. So X={1} initially At each step, choose y  Y whose [y] is min where [y]= length of shortest path from 1 to y walking through X only. 1 X Y y [y]

Dijkstra’s Solution Move y to X and remove it from Y Update [w], for all w  Y that is adjacent to y. Claim: when y is moved to X then [y] =  [y] y Y X

Algorithm: Dijkstra Input: weighted graph G=(V,E) Output: distances from 1 to every vertex X  {1}; Y  V - {1}; [1]  0 For y  2 to n if y is adjacent to 1 then [y]  length[1,y] else [1]   end for for i  1 to n-1 let y be the vertex with min [y] in Y X  X  {y}; Y  Y-{y} for each edge (y,w) z  [y] +length[y,w] if (w  Y and z < [w]) then [w]  z end for

Analysis Time =  (m + n 2 ) =  (n 2 ), where m is the number of edges. Notice that finding the min [y] costs  (n) time

Implementation The graph G should be saved as adjacency list. Costs  (m + n) space, where m is the number of edges Use binary arrays to represent the sets X and Y. Notice that Y= X c

Correction When a vertex y is moved to the set X and [y] is finite then [y] =  [y]

Improving Dijkstra The time can be improved to  (m log n) if m=o(n 2 /log n) by using min-heap to find the min [y]. Notice that updating the heap takes O(log n) time there could be at most m updates.