Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Similar documents
Minimum Spanning Trees

Introduction to Algorithms. Lecture 11

Graph Representation

Minimum Spanning Trees Ch 23 Traversing graphs

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Unit 2: Algorithmic Graph Theory

DFS & STRONGLY CONNECTED COMPONENTS

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Chapter 22. Elementary Graph Algorithms

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Graph Algorithms. Definition

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Elementary Graph Algorithms

Representations of Graphs

Single Source Shortest Path

Basic Graph Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Minimum Spanning Trees

Graph representation

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Design and Analysis of Algorithms

Elementary Graph Algorithms

Graph: representation and traversal

Single Source Shortest Paths

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

22 Elementary Graph Algorithms. There are two standard ways to represent a

Shortest Path Problem

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Lecture 6 Basic Graph Algorithms

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Minimum spanning trees

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Lecture 11: Analysis of Algorithms (CS ) 1

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Minimum Spanning Trees

Graph Algorithms. Andreas Klappenecker. [based on slides by Prof. Welch]

Lecture Notes for Chapter 23: Minimum Spanning Trees

Introduction. I Given a weighted, directed graph G =(V, E) with weight function

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

Minimum Spanning Trees

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

Graph Algorithms: Chapters Part 1: Introductory graph concepts

22 Elementary Graph Algorithms. There are two standard ways to represent a

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

Minimum Spanning Tree

Outlines: Graphs Part-2

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Chapter 23. Minimum Spanning Trees

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Week 12: Minimum Spanning trees and Shortest Paths

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

COT 6405 Introduction to Theory of Algorithms

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

CSI 604 Elementary Graph Algorithms

ECE250: Algorithms and Data Structures Single Source Shortest Paths Bellman-Ford Algorithm

Algorithm Design and Analysis

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Shortest path problems

Strongly Connected Components

Graph: representation and traversal

Outline. Graphs. Divide and Conquer.

Announcements. HW3 is graded. Average is 81%

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Single Source Shortest Path (SSSP) Problem

Lecture 10: Analysis of Algorithms (CS ) 1

Minimum Spanning Trees My T. UF

COMP251: Single source shortest paths

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Design and Analysis of Algorithms

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4.

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Basic Graph Algorithms (CLRS B.4-B.5, )

Minimum Spanning Trees

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Graph Search. Adnan Aziz

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Week 11: Minimum Spanning trees

Minimum Spanning Trees Ch 23 Traversing graphs

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Shortest Path Algorithms

Shortest Paths. Shortest Path. Applications. CSE 680 Prof. Roger Crawfis. Given a weighted directed graph, one common problem is finding the shortest

Solutions to relevant spring 2000 exam problems

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Transcription:

Part VI Graph algorithms Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths 1

Chapter 22 Elementary Graph Algorithms Representations of graphs breadth-first-search (BFS) depth-first-search (DFS) applications: (1) topological sort (2) strongly connected components 2

representations of graphs adjacency-list adjacency-matrix incidence matrix for directed graphs (The incidence matrix of a directed graph D is a p q matrix [b ij ] where p and q are the number of vertices and edges respectively, such that b[i,j]=1 if the edge x j leaves vertex v i, 1 if it enters vertex v i and 0 otherwise. (Note that many authors use the opposite sign convention.) examples 3

BFS The idea of a breadth first search: closest nodes are visited first data structure to use: queue example: 4

BFS(G, s) 1. for each u in V[G] - s 2 do visit[u] <-- unvisited 3. parent[u] <-- null 4. visit[s] <-- visited 5. parent[s] <-- null 6. Q <-- MakeEmptyQueue() 7. Enqueue(Q, s) 8. while Not IsEmptyQueue(Q) 9. do u <-- Dequeue(Q) 10. for each v in Adj[u] 11. do if visit[v] = unvisited 12. then visit[v] <-- visited 13. parent[v] <-- u 14. Enqueue(Q, v) 15 return parent 5

BFS tree cost of BFS O( V + E ) BFS can find a shortest path from s to all other nodes (without weight). But why? 6

DFS The idea of a depth first search: deepest nodes are visited first data structure to use: stack 7

DFS(G, s) 1. for each u in V[G] - s 2 do visit[u] <-- unvisited 3. parent[u] <-- null 4. visit[s] <-- visited 6. S <-- MakeEmptyStack() 7. Push(S, s) 8. while Not IsEmptyStack(S) 9. do u <-- Pop(S) 10. visit[u] = visited 11. for each v in Adj[u] 12. if visit[v] = unvisited 13. then parent[v] <-- u 14. Push(S, v) 15. return parent 8

a recursive DFS algorithm (which also generates timestamps) DFS(G) 1. for each node u in V[G] 2. do parent[u] <-- null 3. time <-- 0 4. for each node u in V[G] 5. do if visit[u] = unvisited 6. then DFS-VISIT(u) 9

DFS-VISIT(u) 1. time <-- time + 1 2. discover[u] <-- time 3. visit[u] <-- visited 4. for each v in Adj[u] 5. do if visit[v] = unvisited 6. then parent[v] <-- u 7. DFS-VISIT(v) 8 time <-- time + 1 8. finish[u] <-- time running time? 10

Properties of depth-first-search (1) u = parent[v] iff DFS-VISIT(v) is called (2) parenthesis structure: for any u, v exactly one of the following three conditions holds: (a) [discover[u], finish[u]] and [discover[v], finish[v]] are entirely disjoint, and neither u nor v is a descendant of the other in the search tree. (b) [discover[u], finish[u] is contained entirely within [discover[v], finish[v]] and u is a descendant of v, OR (c) [discover[v], finish[v] is contained entired within [discover[u], finish[u]] and v is a descendant of u. 11

White-Path Theorem: v is a descendant of u if and only at time discover[u] that the search discovers u, node v can be reached from u along a path consisting entirely of white ( unvisited ) nodes. 12

Classification of edges (for directed graphs) (1) tree edges: those in the search tree (forest) (2) back edges: those connecting a vertex to an ancester (3) forward edges: those connecting a vertex to a descendant (4) cross edges: all other edges Theorem 22.10 In a depth-first-search of an undirected graph G, every edge of G is either a tree edge or a back edge. 13

Topological sorting DAG: directed acyclic graphs example: edges R(prerequisite, course) reverse order of their finish time 14

Strongly connected components Let G = (V, E) be a di-graph. A strongly connected component is a maximal subgraph H = (V H, E H ) of G such that for every two nodes v, u V H, there is a path consisting of edges in E H from v to u and there is a path consisting of edges in E H from u to v. Algorithm STRONLY-CONNECTED-COMPONENTS(G) 1. call DFS(G) to compute finish[u] for each u in V[G] 2. compute GT = transpose of G 3. call DFS(GT) (in which vertices are considered in order of decreasing finish[u] as computed in step 1.) 4. output the vertices of each tree in the depth-first forest produced by step 3. 15

Properties: (1) Component graph: G SCC = (V SCC, E SCC ). G SCC is a dag. Let C be a SCC, define finish(c) = max u C finish[u]. (2) Lemma 22.14: Let C and C be distinct strongly connected components for G. If (u, v) E, where u C and v C, then f(c) > f(c ). (3) Theorem 22.16: STRONLY-CONNECTED-COMPONENTS(G) correctly computes the strongly connected components for a directed graph G. 16

Others Algorithm for computing connected components in undirected graphs. Reachability problem: given G = (V, E), and u, v V, is there a path from u to v? [Is there an SQL program that can solve Reachability problem?] 17

Chapter 23. Minimum Spanning Trees spanning trees (MST) MST: given a connected, undirected graph G = (V, E) with w : E R, find a spanning tree T such that W (T ) = w(u, v) is the minimum (u,v) T Two greedy algorithms: (1) Kruskal s and (2) Prim s based on a generic MST algorithm. The idea: growing an MST by adding one edge to A at a time until A forms a spanning tree. But which edge to add?? 18

Growing an MST GENERIC-MST(G,w) 1. A <-- empty 2. while A does not form a spanning tree 3. do find an edge (u, v) that is safe for A 4. A <-- A U {(u, v)} 5. return A loop invariant: A is a subset of some MST safe edge: one does not cause a cycle while maintaining the invariant 19

cut: (S, V S) is a partition of V crossing: (u, v) crosses cut (S, V S) if u and v are in S and V S respectively (or if v and u are in S and V S respectively) respecting: a cut respects a set A of edges if no edge in A crosses the cut. light edge: an edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut. Theorem 23.1 Let G = (V, E) Let A be a subset of E that is included in some MST for G, let (S, V S) be any cut of G that respect A, and let (u, v) be a light edge crossing the cut. Then edge (u, v) is safe for A. Proof: (1) does not form a cycle; (2) A is still a subset of some MST 20

Kruskal s algorithm for MST MST-KRUSKAL(G,w) 1. A <-- empty 2. for each vertex v in V[G] 3 do MAKE-SET(v) 4. sort E into nondecreasing order by weight w 5. for each edge (u, v) in E, taken in order 6. do if FIND-SET(u) <> FIND-SET(v) 7. then A <-- A U {(u, v)} 8. UNION(u, v) 9 return A disjoint-set data structure and operations: MAKE-SET, FIND-SET and UNION 21

running time: O( E log V ) 22

Prim s algorithm for MST MST-PTIM(G, w, r) 1. for each u in V[G] 2. do key[u] <-- infinite 3. parent[u] <-- NULL 4. key[r] <-- 0 5. Q <-- V[G] 6. while Q is not empty 7. do u <-- EXTRACT-MIN(Q) 8. for each v in Adj[u] 9. do if v in Q and w(u,v) < key[v] 10. then parent[v] <-- u 11. key[v] <-- w(u, v) 12. return parent 23

Priority queue: Q running time? O( E log V ). 24

UNKNOWN(G, w, r) 1. for each u in V[G] 2. do key[u] <-- infinite 3. parent[u] <-- NULL 4. key[r] <-- 0 5. Q <-- V[G] 6. while Q is not empty 7. do u <-- EXTRACT-MIN(Q) 8. for each v in Adj[u] 9. do if w(u,v) + key[u] < key[v] 10. then parent[v] <-- u 11. key[v] <-- w(u, v) + key[u] 12. return parent 25

Chapter 24. Single-source shortest paths single source: s weighted edges: w(u, v) R path weight: w(p) = k i=1 w(v i 1, v i ) shortest path weight: δ(u, v) = min{w(p) : p is a path from u to v} Single-source shortest paths: from s to each vertex v V Single-destination shortest paths: to vertex t from each vertex v V Single-pair shortest path: from s to t All-pairs shortest paths: from s to t for all pairs s, t V. 26

Lemma 24.1 (subpaths of shortest paths are shortest paths) Given a weighted directed graph G = (V < E) with weight function w. Let p = (v 1, v 2,, v k ) be a shortest path from v 1 to v k. Then p i,j = (v i,, v j ) is a shorest path from v i to v j. negative weights: cycles: negative weight cycles, positive weight cycles, 0 weight cycles representing shortest paths: predecessor π shortest path tree: 27

Technique: relaxation Let d[v] be an upper bound on the weight of a shortest path from s to v, initialized. The process of relaxing edge (u, v): improve d[v] so far by going through u, and update d[v] and π[v]. 28

Bellman-Ford algorithm BELLMAN-FORD((G, w, s) 1. for each vertex v in V[G] {initialization} 2. do d[v] <-- infinite 3. pi[v] <-- null 4. d[s] <-- 0 5. for i <-- 1 to V -1 { relaxation} 6. do for each edge (u, v) in E[G] 7. do if d[v] > d[u] + w(u, v) 8. then d[v] <-- d[u] + w(u, v) 9. pi[v] <-- u 10. for each edge (u, v) in E[G] {checking negative} 11. do if d[v] > d[u] + w(u, v) {weight cycle} 12. then return FALSE 29

13. return TRUE running time : O( V E ) Lemma 24.2 Let G = (V, E) be a weighted, directed graph with source s and weight function w : E R and assume that G contains no negative weight cycles that can be reached from s. Then after V 1 iterations of line 5 in the algorithm, d[v] = δ(s, v) for all vertices v that are reachable from s. 30

Theorem 24.4 Bellman-Ford algorithm is correct on weighted, directed graphs. (1) Lemma 24.2 shows the correctness on weighted, directed graphs without negative weight cycles. (2) we need to show, when G contains a negative weight cycle reachable from s, the algorithm returns FALSE assume the cycle to be c = (v 0, v 1,, v k ), where v 0 = v k and k i=1 w(v i 1, v i ) < 0 Then because d[v i ] d[v i 1 ] + w(v i 1, v i ) k i=1 d[v i ] k i=1 (d[v i 1 ] + w(v i 1, v i )) 31

k i=1 d[v i 1 ] + k i=1 w(v i 1, v i ) But implying k i=1 k i=1 w(v i 1, v i ) 0. d[v i ] = k i=1 d[v i 1 ] 32

Single-source shorest paths on DAG DAG-SHORTEST PATHS(G, w, s) 1. topologically sort the vertices of G 2. for each vertex v in V[G] {initialization} 3. do d[v] <-- infinite 4. pi[v] <-- null 5. d[s] <-- 0 6. for each u in V[G], in topologically sorted order 7. do for each vertex v in Adj[u] 8. do if d[v] > d[u] + w(u, v) 9. then d[v] <-- d[u] + w(u, v) 10. pi[v] <-- u 11. return d and pi running time: O(V + E) 33

Dijkstra s algorithm On weighted, directed graphs in which each edge has non-negative weight. DIJKSTRA(G, w, s) 1. for each vertex v in V[G] {initialization} 2. do d[v] <-- infinite 3. pi[v] <-- null 4. d[s] <-- 0 5. S <-- empty 6. Q <-- V[G] 7. while Q is not empty 8. do u <-- EXTRACT-MIN(Q) 9. S <-- S U {u} 10. for each vertex v in Adj[u] 11. do if d[v] > d[u] + w(u, v) 12. then d[v] <-- d[u] + w(u, v) 34

13. pi[v] <-- u running time: O((V + E)lgV ) Correctness of the algorithm: Theorem 24.6, proof by the use of following loop invariant for the while loop: d[v] = δ(s, v) for each v S. Can it deal with negative weight edges? 35