Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Similar documents
Shortest Path Problem

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

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

Directed Graphs (II) Hwansoo Han

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Shortest path problems

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

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.

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

Algorithm Design and Analysis

Announcements. HW3 is graded. Average is 81%

Sample Solutions to Homework #4

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

Minimum Spanning Trees

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).

Konigsberg Bridge Problem

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

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

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

Design and Analysis of Algorithms

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Basic Graph Definitions

Artificial Intelligence

COL 702 : Assignment 1 solutions

Algorithms for Data Science

CSI 604 Elementary Graph Algorithms

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

Graph Representation

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

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

Graph Algorithms: Chapters Part 1: Introductory graph concepts

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

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

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

The Shortest Path Problem

CS350: Data Structures Dijkstra s Shortest Path Alg.

22.3 Depth First Search

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

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

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

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

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

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

DFS & STRONGLY CONNECTED COMPONENTS

Graph: representation and traversal

Data Structures and Algorithms. Chapter 7. Graphs

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

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Graph Algorithms Using Depth First Search

Introduction to Algorithms. Lecture 11

Lecture 11: Analysis of Algorithms (CS ) 1


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

Module 5 Graph Algorithms

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

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

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

Minimum Spanning Trees Ch 23 Traversing graphs

Chapter 22. Elementary Graph Algorithms

Graph Representations and Traversal

Lecture 4: Graph Algorithms

CS521 \ Notes for the Final Exam

Solutions to relevant spring 2000 exam problems

Design and Analysis of Algorithms

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

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

Graph representation

Single Source Shortest Path (SSSP) Problem

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

Algorithm Design (8) Graph Algorithms 1/2

CS 4349 Lecture October 23rd, 2017

Analysis of Algorithms, I

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

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

Parallel Graph Algorithms

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Graph Algorithms (part 3 of CSC 282),

Graph Algorithms. Definition

Representations of Graphs

Chapter 25: All-Pairs Shortest-Paths

Dr. Alexander Souza. Winter term 11/12

Graph Algorithms. 1 Preliminary Denitions. CSci 335 Software Design and Analysis III Chapter 9 Graph Algorithms. Prof.

Design and Analysis of Algorithms 演算法設計與分析. Lecture 13 December 18, 2013 洪國寶

Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis

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

Single Source Shortest Path

The Shortest-Path Problem : G. Dantzig. 4-Shortest Paths Problems. The Shortest-Path Problem : G. Dantzig (continued)

Strongly Connected Components

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

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

Lecture 6 Basic Graph Algorithms

Parallel Graph Algorithms

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

W4231: Analysis of Algorithms

Transcription:

Directed Graphs Definitions. Representations. ADT s. Single Source Shortest Path Problem (Dijkstra, Bellman-Ford, Floyd-Warshall). Traversals for DGs. Parenthesis Lemma. DAGs. Strong Components. Topological Sort DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Directed Graphs. Definitions Directed graph (digraph): G=(V, E) V : set of vertices (nodes, points); n= V E : set of edges (arcs, directed lines); e= E Arc: ordered pair (u, v) arc is from u to v u=tail v=head v is adjacent to u Path from v 1 to v n : v 1, v 2, v 3,, v n such that v 1 v 2, v 2 v 3,, v n-1 v n are arcs Path length: number of edges on the path Simple path: all vertices distinct (except possibly for last and first) Simple cycle: simple path of length 1 that begins and ends at the same node DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 2

Directed Graphs. Representations Example b, d, c, b simple cycle of length 3 Labeled digraph Label: value of any given data type Example: transition graph Representations Adjacency matrix Adjacency list DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 3

Directed Graphs. Representations Adjacency matrix: n n of Booleans (0,1) Labeled adjacency matrix DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 4

Directed Graphs. Representations Adjacency list useful when e << n 2 DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 5

Directed Graph ADT s Typical processing in programs Operations: first(v) returns the index for the first vertex adjacent to v. The index for the null vertex (Λ) is returned if there is no vertex adjacent to v. next(v, i) returns the index after index i for the vertices adjacent to v. Λ is returned if i is the last index for vertices adjacent to v. vertex(v, i) returns the vertex with index i among the vertices adjacent to v. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 6

Directed Graph ADT s Processing adjacency for a vertex DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 7

The Single Source Shortest Path Problem Given: directed graph G = (V,E) in which each arc has a nonnegative label, and one vertex is specified as the source Determine: the cost of the shortest path from the source to every other vertex in V Weighted graph: arcs are labeled with costs One solution: Dijkstra s Set S of vertices whose shortest distance from the source is already known. At each step we add to S a remaining vertex v whose distance from the source is as short as possible. We can always find a shortest path from the source to v that passes only through vertices in S (special path). Use an array D to record the length of the shortest special path to each vertex. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 8

Dijkstra s algorithm G = (V,E), where V = {1, 2,..., n} and vertex 1 is the source loop at lines 7 8 takes O(n) time and it is executed n 1 times, resulting in a total time of O(n 2 ) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 9

Running Time of Dijkstra s Algorithm loop at lines 7 8 takes O(n) time and it is executed n 1 times, resulting in a total time of O(n 2 ) If e<<n 2 an adjacency list is a better choice use priority queue implemented as a partially ordered tree (heap) for V - S. loop at lines 7 8 can be implemented by going down the adjacency list for w and updating the distances in the priority queue e updates of O(log n): total O(e log n) (if e<<n 2 ) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 10

Correctness of Dijkstra s Algorithm (1) If length(s x w ) < length(s w), then s x = special path < shortest special path to w. In that case when we made the selection we would have chosen x instead. Hypothetical shorter path (1) Impossible shorter path (2) (2) x S before w => the shortest of all paths from the source to x runs through old S alone. Therefore, length(s w x) length(s x) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 11

Another Form of Dijkstra s Algorithm Assume each vertex v in the graph stores two values, which describe a tentative shortest path from a source vertex, s, to v: dist(v) the length of the tentative shortest path from s to v. pred(v) the predecessor of v in the tentative shortest path from s to v. Predecessor pointers automatically define a tentative shortest path tree dist(s)=0 and pred(s)=nil. For every vertex v s, we initially set dist(v) =1 and pred(v)=nil (do not know any path from s to v) We call an edge u v tense if dist(u)+w(u v) < dist(v) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 12

Another Form of Dijkstra s Algorithm Our generic algorithm repeatedly finds a tense edge and relaxes it: Implementation Uses a priority queue to hold vertices in V \ S Needs cross references to vertices to allow for the DecreaseKey operation DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 13

Dijkstra s algorithm (again) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 14

Dijkstra s algorithm (again) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 15

Animations for Dijkstra s Algorithm http://www.cs.sunysb.edu/~skiena/combinatorica/animations/dijkstra.html http://www.cse.yorku.ca/~aaw/hfhuang/dijkstrastart.html http://www.cs.auckland.ac.nz/software/alganim/dijkstra.html http://www.unf.edu/~wkloster/foundations/dijkstraapplet/dijkstraapplet.ht m DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 16

Bellman-Ford(Moore) Moore in 1957, then independently by Bellman in 1958 -- Bellman used the idea of edge relaxation, first proposed by Ford in 1956 able to deal with negative edge weights, but no negative cost cycles (otherwise we could make the path infinitely short by cycling forever through such a cycle) simply applies relaxation to every edge in the graph, and repeats this process n-1 times running time is Θ(ne): initialization process Θ(n) n-1 passes through the set of edges: Θ(e) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 17

Bellman-Ford(Moore) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 18

Correctness of Bellman-Ford (Moore s) Algorithm any shortest path is a sequence a shortest path will never visit the same vertex twice (why?); path consists of at most n - 1 edges. true shortest path satisfies after the i th pass of the loop 9 12 prior to pass i, after pass i, we have DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 19

Bellman-Ford (Moore s) Algorithm Animation http://www.laynetworks.com/simulation%20of%20bellman% 20Algorithm.htm Local applet (Demos\BellManFord\BellmanFord.html) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 20

Floyd-Warshall algorithm The All-Pairs Shortest Paths Problem DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 21

Floyd Warshall formulation DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 22

Floyd-Warshall formulation Compute d ij (k) knowing matrix at previous step D (k-1) Two cases Don't go through k. The shortest path passes only through intermediate vertices {1, 2,, k-1} resulting in a path length of d ij (k-1) (discovered at the previous step) Do go through k. We can assume there is exactly one pass through vertex k (if there are no negative cost cycles): go from i to k and from k to j to get an overall minimum, take the shortest path i k, and the shortest path k j. this path uses only the intermediate vertices in {1, 2,, k-1}, the length of the path is d ik (k-1) + d kj (k-1). DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 23

Floyd-Warshall DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 24

Floyd-Warshall trace DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 25

Floyd-Warshall Algorithm Animation http://students.ceid.upatras.gr/~papagel/project/kef 5_7_2.htm http://www.cs.man.ac.uk/~graham/cs2022/dynamic/ floydwarshall/ http://www.pms.ifi.lmu.de/lehre/compgeometry/gos per/shortest_path/shortest_path.html#visualization DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 26

Transitive closure Graph G=(V, E). Transitive closure: graph G*=(V,E*) Solutions: Floyd Warshall with weights 1 or use recurrence: DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 27

Transitive closure DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 28

Traversals. Depth first search DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 29

Depth first search example DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 30

Analysis of DFS If we ignore the time spent in the recursive calls DFS takes O(n) time Each vertex is visited exactly once in the search=>call to DFSVisit is made exactly once for each vertex Let outdeg(u) be the number of outgoing arcs from u Each vertex u can be processed in O(1 + outdeg(u)) time Total time is DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 31

DFS Animations http://maven.smith.edu/~thiebaut/java/graph/welco me.html http://sziami.cs.bme.hu/~gsala/alg_anims/3/graphe.html DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 32

DFS edges classification Edge classification by DFS Tree edges: lead to unvisited vertices Back edges: (u, v) v is a (not necessarily proper) ancestor of u in the tree. (A self-loop is considered to be a back edge). Forward edges: (u, v) v is a proper descendent of u in the tree. Cross edges: (u, v) u and v are neither ancestors nor descendants of one another (the edge may go between different trees of the forest) DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 33

Parenthesis Lemma Parenthesis Lemma. Given a digraph G = (V, E), and any DFS tree for G and any two vertices u, v V, exactly one of the following three conditions hold: u is a descendent of v if and only if [d[u], f [u]] [d[v], f [v]]. u is an ancestor of v if and only if [d[u], f [u]] [d[v], f [v]]. u is unrelated to v if and only [d[u], f [u]] [d[v], f [v]]. =. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 34

Illustration of parenthesis lemma DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 35

Directed acyclic graphs (DAGs) Directed acyclic graph (dag) = directed graph with no cycles more general than trees but less general than arbitrary digraphs also useful in representing arithmetic expressions, partial orders, DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 36

Test for acyclicity Can use DFS for testing If a directed graph has a cycle, then a back arc will always be encountered in any depth-first search of the graph Proof suppose G is cyclic; do a DFS there will be one vertex v having the lowest depth-first search number (d[..]) of any vertex on a cycle consider an arc u v on some cycle containing v u is on the cycle, u must be a descendant of v in DFS; u v cannot be a cross arc d[u] > d[v], u v cannot be a tree arc or a forward arc. It s a back arc DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 37

Topological sort Situations where activities must be completed in a certain order Use dags to represent this Topological sort: a process of assigning a linear ordering to the vertices of a dag so that if there is an arc i j, then i appears before j in the linear ordering in general, there may be many legal topological orders for a given DAG DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 38

Topological sort DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 39

Strong Components Strongly connected component of a digraph: a maximal set of vertices in which there is a path from any one vertex in the set to any other vertex in the set. Can use dfs to determine strong components A graph Strong components Reduced graph. Represents connections between components DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 40

Determining strong components DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 41

Reading AHU, chapter 6 Preiss, chapter: Graphs and Graph Algorithms CLR, chapter 23, sections 3-5, chapter 25, sections 1-3, chapter 26, sections 1-3 CLRS chapter 22, section 1, 3, chapter 24 sections 1-3, chapter 25 section 2 Notes DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 42