CSE 417: Algorithms and Computational Complexity

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
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.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
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.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Graph Algorithms BFS, DFS, Dijkstra’s.
Introduction to Graphs
Lecture 23 CSE 331 Oct 26, 2016.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
More Graph Algorithms.
Graph Algorithm.
Data Structures and Algorithms CMPSC 465
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
Shortest Paths C B A E D F Shortest Paths
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
Algorithms and Data Structures Lecture XII
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Lecture 23 CSE 331 Oct 25, 2017.
Lecture 24 CSE 331 Oct 29, 2012.
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Chapter 11 Graphs.
Autumn 2015 Lecture 10 Minimum Spanning Trees
Lecture 13 Algorithm Analysis
Lecture 16 CSE 331 Oct 8, 2012.
Lecture 13 Algorithm Analysis
Richard Anderson Winter 2009 Lecture 6
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Graph Algorithms: Shortest Path
CSE 373: Data Structures and Algorithms
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Richard Anderson Lecture 5 Graph Theory
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Lecture 11 Graph Algorithms
Topological Sorting Minimum Spanning Trees Shortest Path
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 12 Instructor: Paul Beame

Single-source shortest paths Given an (un)directed graph G=(V,E) with each edge e having a weight w(e) and a vertex v Find length of shortest paths from v to each vertex in G - if there is a (directed) cycle with negative weight go around the cycle over and over Assume first that there are no negative cost edges

A greedy algorithm Dijkstra’s Algorithm: Maintain a set S of vertices whose shortest paths are known initially S={v} Maintaining current best lengths of paths that only go through S to each of the vertices in G path-lengths to elements of S will be right, to V-S they might not be right Repeatedly add vertex u to S that has the shortest path-length of any vertex in V-S update path lengths based on new paths through u

Dijkstra’s Algorithm 4 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4     4 2 5 7 2 4 1  3 3 4  6  5   8 1 9 8  4    5 2 10 

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1  3 3 4  6  5   8 1 9 8  4    5 2 10 

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4  6  5   8 1 9 8  4    5 2 10 

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4  6  5   8 1 9 8  4    5 2 10 

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5  8 8 1 9 8  4    5 2 10 

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5  8 8 1 9 8  4    5 2 10 

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5  8 8 1 9 8 13 4    5 2 10 

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5  8 8 1 9 8 13 4    5 2 10 

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5  8 8 1 9 8 13 4    5 2 10 

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5  8 8 1 9 8 13 4    5 2 10 

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5  8 8 1 9 8 13 4  16  5 2 10 

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5  8 8 1 9 8 13 4  16  5 2 10 

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4  16 10 5 2 10 

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4  16 10 5 2 10 

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 20

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 20

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 20

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 20

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 19

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 19

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 19

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 19

Dijkstra’s Algorithm 4 Update distances 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 4 Update distances 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 18

Dijkstra’s Algorithm 4 Add to S 2 5 7 1 3 3 4 6 5 8 1 9 8 4 5 2 10 2 4 4 Add to S 2 5 7 2 4 1 9 3 3 4 7 6 5 5 15 8 8 1 9 8 13 4 14 16 10 5 2 10 18

Dijkstra’s Algorithm Correctness Suppose all distances to vertices in S are correct and u has smallest current value in V-S distance value of vertex in V-S=length of shortest path from v with only last edge leaving S S Suppose some other path to u and x= first vertex on this path not in S x v d(u) d(x) x-u path length  0 u  other path is longer Therefore adding u to S keeps correct distances

Dijkstra’s Algorithm Algorithm also produces a tree of shortest paths to v From w follow its ancestors in the tree back to v If all you care about is the shortest path from v to w simply stop the algorithm when w is added to S

Implementing Dijkstra’s Algorithm Need to keep current distance values for nodes in V-S find minimum current distance value reduce distances when vertex moved to S Same operations as priority queue version of Prim’s Algorithm only difference is rule for updating values node value + edge-weight vs edge-weight alone same run-times as Prim’s Algorithm O(m log n)

Topological Sort Applications Given: a directed acyclic graph (DAG) G=(V,E) Output: numbering of the vertices of G with distinct numbers from 1 to n so edges only go from lower number to higher numbered vertices Applications nodes represent tasks edges represent precedence between tasks topological sort gives a sequential schedule for solving them

Directed Acyclic Graph

Topological Sort Can do using DFS (see book) Alternative simpler idea: Any vertex of in-degree 0 can be given number 1 to start Remove it from the graph and then give a vertex of in-degree 0 number 2, etc.

Topological Sort 1

Topological Sort 1 2

Topological Sort 1 2 3

Topological Sort 1 2 4 3

Topological Sort 1 2 4 3 5

Topological Sort 1 2 4 3 5 6

Topological Sort 1 2 4 3 5 6 7

Topological Sort 1 2 4 3 8 5 6 7

Topological Sort 1 2 4 3 8 5 6 9 7

Topological Sort 1 2 4 3 8 5 6 9 10 7

Topological Sort 1 2 4 3 8 5 6 9 10 7 11

Topological Sort 1 2 4 3 8 5 6 9 10 7 11 12

Topological Sort 1 2 4 3 8 5 6 9 10 7 11 12 13

Topological Sort 1 2 4 3 8 5 6 9 10 7 11 12 13 14

Implementing Topological Sort Go through all edges, computing in-degree for each vertex O(m+n) Maintain a queue (or stack) of vertices of in-degree 0 Remove any vertex in queue and number it When a vertex is removed, decrease in-degree of each of its neighbors by 1 and add them to the queue if their degree drops to 0 Total cost O(m+n)