Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Similar documents
Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Depth-first search in undirected graphs What parts of the graph are reachable from a given vertex?

CSC Intro to Intelligent Robotics, Spring Graphs

CSE 101. Algorithm Design and Analysis Miles Jones and Russell Impagliazzo Miles Office 4208 CSE Building

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 6: BFS and Dijkstra s

Graph Representations and Traversal

Graphs: Connectivity. Jordi Cortadella and Jordi Petit Department of Computer Science

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

CS483 Design and Analysis of Algorithms

Algorithms (I) Introduction. Guoqiang Li. School of Software, Shanghai Jiao Tong University

CS Final - Review material

Design and Analysis of Algorithms (I)

Design and Analysis of Algorithms

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Graph Representation

DFS & STRONGLY CONNECTED COMPONENTS

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

Question Points Score TOTAL 50

Algorithm Design and Analysis

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Lecture 3: Graphs and flows

Graph Algorithms. Definition

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 5: SCC/BFS

Graph. Vertex. edge. Directed Graph. Undirected Graph

Graphs: Connectivity. A graph. Transportation systems. Social networks

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Chapter 22. Elementary Graph Algorithms

Graph: representation and traversal

Depth First Search (DFS)

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

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Graph Algorithms Using Depth First Search

1 Heaps. 1.1 What is a heap? 1.2 Representing a Heap. CS 124 Section #2 Heaps and Graphs 2/5/18

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

Basic Graph Algorithms

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

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

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Graphs: basic concepts and algorithms

Sorting and Searching

Sorting and Searching

Design and Analysis of Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

U.C. Berkeley CS170 : Algorithms Midterm 1 Lecturers: Alessandro Chiesa and Satish Rao September 18, Midterm 1

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Dijkstra s Algorithm and Priority Queue Implementations. CSE 101: Design and Analysis of Algorithms Lecture 5

Algorithm Design and Analysis

Graph representation

Algorithms and Data Structures

Analysis of Algorithms, I

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

1 Heaps. 1.1 What is a heap? 1.2 Representing a Heap. CS 124 Section #2 Heaps and Graphs 2/5/18

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

CS521 \ Notes for the Final Exam

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

Elementary Graph Algorithms

Design and Analysis of Algorithms

COS 423 Lecture14. Robert E. Tarjan 2011

Design and Analysis of Algorithms

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

22.3 Depth First Search

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

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

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 8: Negative Edges

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Copyright 2000, Kevin Wayne 1

Representations of Graphs

COT 6405 Introduction to Theory of Algorithms

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Homework Assignment #3 Graph

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

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Lecture Summary CSC 263H. August 5, 2016

Solutions to relevant spring 2000 exam problems

Announcements. HW3 is graded. Average is 81%

Design and Analysis of Algorithms

CS 561, Lecture 10. Jared Saia University of New Mexico

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Lecture 4: Graph Algorithms

Content. 1 Algorithms. 2 Assignment and Knapsack problem 3 Bin Packing. 4 Scheduling

Graphs. CSE 2320 Algorithms and Data Structures Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

Lecture 2 - Graph Theory Fundamentals - Reachability and Exploration 1

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

Data Structures Brett Bernstein

Quiz 2 CS 3510 October 22, 2004

Unit 2: Algorithmic Graph Theory

Algorithms for Data Science

Lecture 10: Strongly Connected Components, Biconnected Graphs

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

CS350: Data Structures Heaps and Priority Queues

Introduction to Algorithms. Lecture 11

Transcription:

Algorithms (VII) Yijia Chen Shanghai Jiaotong University

Review of the Previous Lecture

Depth-first search in undirected graphs

Exploring graphs explore(g, v) Input: G = (V, E) is a graph; v V Output: visit(u) is set to true for all nodes u reachable from v 1. visited(v) = true 2. previst(v) 3. for each edge (v, u) E do 4. if not visited(u) then explore(u) 5. postvisit(v) Theorem explore(g, v) is correct, i.e., it visits exactly all nodes that are reachable from v.

Edge types Those edges in G that are traversed by explore are tree edges. The rest are back edges.

Depth-first search dfs(g) 1. for all v V do 2. visited(v) = false 3. for all v V do 4. if not visited(v) then explore(v) Theorem DFS has a running time of O( V + E ).

Connectivity in undirected graphs Definition An undirected graph is connected, if there is a path between any pair of vertices. Definition A connected component is a subgraph that is internally connected but has no edges to the remaining vertices. When explore is started at a particular vertex, it identifies precisely the connected component containing that vertex. Each time the DFS outer loop calls explore, a new connected component is picked out.

Connectivity in undirected graphs (cont d) Thus depth-first search is trivially adapted to check if a graph is connected. More generally, to assign each node v an integer ccnum[v] identifying the connected component to which it belongs. All it takes is previsit(v) ccnum[v] = cc where cc needs to be initialized to zero and to be incremented each time the DFS procedure calls explore.

Previsit and postvisit orderings For each node, we will note down the times of two important events: the moment of first discovery (corresponding to previsit); and the moment of final departure (postvisit). previsit(v) pre[v] = clock clock = clock + 1 postvisit(v) post[v] = clock clock = clock + 1 Lemma For any nodes u and v, the two intervals [pre(u), post(u)] and [pre(u), post(u)] are either disjoint or one is contained within the other.

Depth-first search in directed graphs

Types of edges DFS yields a search tree/forests. root. descendant and ancestor. parent and child. Tree edges are actually part of the DFS forest.. Forward edges lead from a node to a nonchild descendant in the DFS tree. Backedges lead to an ancestor in the DFS tree. Cross edges lead to neither descendant nor ancestor; they therefore lead to a node that has already been completely explored (that is, already postvisited).

Types of edges (cont d) pre/post ordering for (u, v) Edge type [ u [ v ] v ] u Tree/forward [ v [ u ] u ] v Back [ v ] v [ u ] u Cross

Directed acyclic graphs (DAG) Definition A cycle in a directed graph is a circular path v 0 v 1 v 2 v k v 0. Lemma A directed graph has a cycle if and only if its depth-first search reveals a back edge.

Directed acyclic graphs (cont d) Linearization/Topologically Sort: Order the vertices such that every edge goes from a small vertex to a large one. Lemma In a dag, every edge leads to a vertex with a lower post number. Hence there is a linear-time algorithm for ordering the nodes of a dag. Since a dag is linearized by decreasing post numbers, the vertex with the smallest post number comes last in this linearization, and it must be a sink no outgoing edges. Symmetrically, the one with the highest post is a source, a node with no incoming edges. Lemma Every dag has at least one source and at least one sink. The guaranteed existence of a source suggests an alternative approach to linearization: 1. Find a source, output it, and delete it from the graph. 2. Repeat until the graph is empty.

Strongly connected components

Defining connectivity for directed graphs Definition Two nodes u and v of a directed graph are connected if there is a path from u to v and a path from v to u. This relation partitions V into disjoint sets that we call strongly connected components. Lemma Every directed graph is a dag of its strongly connected components.

An efficient algorithm Lemma If the explore subroutine is started at node u, then it will terminate precisely when all nodes reachable from u have been visited. Therefore, if we call explore on a node that lies somewhere in a sink strongly connected component (a strongly connected component that is a sink in the meta-graph), then we will retrieve exactly that component. We have two problems: (A) How do we find a node that we know for sure lies in a sink strongly connected component? (B) How do we continue once this first component has been discovered?

An efficient algorithm (cont d) Lemma The node that receives the highest post number in a depth-first search must lie in a source strongly connected component. Lemma If C and C are strongly connected components, and there is an edge from a node in C to a node in C, then the highest post number in C is bigger than the highest post number in C. Hence the strongly connected components can be linearized by arranging them in decreasing order of their highest post numbers.

Solving problem A Consider the reverse graph G R, the same as G but with all edges reversed. G R has exactly the same strongly connected components as G. So, if we do a depth-first search of G R, the node with the highest post number will come from a source strongly connected component in G R, which is to say a sink strongly connected component in G.

Solving problem B Once we have found the first strongly connected component and deleted it from the graph, the node with the highest post number among those remaining will belong to a sink strongly connected component of whatever remains of G. Therefore we can keep using the post numbering from our initial depth-first search on G R to successively output the second strongly connected component, the third strongly connected component, and so on.

The linear-time algorithm 1. Run depth-first search on G R. 2. Run the undirected connected components algorithm on G, and during the depth-first search, process the vertices in decreasing order of their post numbers from step 1.

Chapter 4. Paths in graphs

Distances DFS does not necessarily find the shortest paths. Definition The distance between two nodes is the length of the shortest path between them.

Breadth-first search

The algorithm bfs(g, s) Input: Output: Graph G = (V, E), directed or undirected; vertex s V For all vertices u reachable from s, dist(u) is set to the distance from s to u. 1. for all u V do 2. dist(u) = 3. dist(s) = 0 4. Q = [s] (queue containing just s) 5. while Q is not empty do 6. u = eject(q) 7. for all edge (u, v) E do 8. if dist(v) = then 9. inject(q, v) 10. dist(v) = dist(u) + 1

Correctness and efficiency Lemma For each d = 0, 1, 2,..., there is a moment at which (1) all nodes at distance d from s have their distances correctly set; (2) all other nodes have their distances set to ; and (3) the queue contains exactly the nodes at distance d. Lemma BFS has a running time of O( V + E ).

Lengths on edges BFS treats all edges as having the same length, which is rarely true in applications where shortest paths are to be found. Every edge e E with a length l e. If e = (u, v), we will sometimes also write l(u, v) or l uv.

Dijkstra s algorithm

An adaption of breadth-first search BFS finds shortest paths in any graph whose edges have unit length. Can we adapt it to a more general graph G = (V, E) whose edge lengths l e are positive integers? A simple trick: For any edge e = (u, v) of E, replace it by l e edges of length 1, by adding l e 1 dummy nodes between u and v. It might take time ( O V + l e ), e E which is bad in case we have edges with high length.

Alarm clocks Set an alarm clock for node s at time 0. Repeat until there are no more alarms: Say the next alarm goes off at time T, for node y. Then: The distance from s to u is T. For each neighbor v of u in G: If there is no alarm yet for v, set one for time T + l(u, v). If v s alarm is set for later than T + l(u, v), then reset it to this earlier time.

Priority queue Priority queue is a data structure usually implemented by heap. Insert. Add a new element to the set. Decrease-key. Accommodate the decrease in key value of a particular element. Delete-min. Return the element with the smallest key, and remove it from the set. Make-queue. Build a priority queue out of the given elements, with the given key values. (In many implementations, this is significantly faster than inserting the elements one by one.)

Dijkstra s shortest-path algorithm dijkstra(g, l, s) Input: Graph G = (V, E), directed or undirected; positive edge length {l e e E}; vertex s V Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u. 1. for all u V do 2. dist(u) = 3. prev(u) = nil 4. dist(s) = 0 5. H = makequeue(v ) (using dist-values as keys) 6. while H is not empty do 7. u = deletemin(h) 8. for all edge (u, v) E do 9. if dist(v) > dist(u) + l(u, v) then 10. dist(v) = dist(u) + l(u, v) 11. prev(v) = u 12. decreasekey(h, v)

An alternative derivation 1. Initialize dist(s) = 0, other dist( ) to 2. R = { } (the known region ) 3. while R V do 4. Pick the node v / R with smallest dist( ) 5. Add v to R 6. for all edge (v, z) E do 7. if dist(z) > dist(v) + l(v, z) then 8. dist(z) = dist(v) + l(v, z)

Key property At the end of each iteration of the while loop, the following conditions hold: (1) there is a value d such that all nodes in R are at distance d from s and all nodes outside R are at distance d from s; (2) for every node u, the value dist(u) is the length of the shortest path from s to u whose intermediate nodes are constrained to be in R (if no such path exists, the value is ).

Running time Since makequeue takes at most as long as V insert operations, we get a total of V deletemin and V + E insert/decreasekey operations. The time needed for these varies by implementation; for instance, a binary heap gives an overall running time of O(( V + E ) log V ).

Which heap is best? Implementation deletemin insert/ V deletemin+ decreasekey ( V + E ) insert Array O( V ) O(1) O( V 2 ) Binary heap O(log V ) O(log V ) O(( V + E ) log V ) d log V log V (d V + E ) log V d-ary heap O( ) O( ) O( ) log d log d log d Fibonacci heap O(log V ) O(1) (amortized) O( V log V + E )

Priority queue implementations

Array The simplest implementation of a priority queue is as an unordered array of key values for all potential elements (the vertices of the graph, in the case of Dijkstra s algorithm). Initially, these values are set to. An insert or decreasekey is fast, because it just involves adjusting a key value, an O(1) operation. To deletemin, on the other hand, requires a linear-time scan of the list.

Binary heap Here elements are stored in a complete binary tree. In addition, a special ordering constraint is enforced: the key value of any node of the tree is less than or equal to that of its children. In particular, therefore, the root always contains the smallest element. To insert, place the new element at the bottom of the tree (in the first available position), and let it bubble up. The number of swaps is at most the height of the tree log 2 n, when there are n elements. A decreasekey is similar, except the element is already in the tree, so we let it bubble up from its current position. To deletemin, return the root value. Then remove this element from the heap, take the last node in the tree (in the rightmost position in the bottom row) and place it at the root. Then let it sift down. Again this takes O(log n) time.

d-ary heap A d-ary heap is identical to a binary heap, except that nodes have d children. This reduces the height of a tree with n elements to Θ(log d n) = Θ((log n)/(log d)) Inserts are therefore speeded up by a factor of Θ(log d). Deletemin operations, however, take a little longer, namely O(d log d n).