CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
Applications of graph traversals
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
Shortest Path Problems
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
Connected Components, Directed Graphs, Topological Sort COMP171.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Graph Algorithms.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Chapter 22 Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
CSC317 Graph algorithms Why bother?
Topological Sort (an application of DFS)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill.
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Basic Graph Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor (parent) of v. A vertex is discovered at most once. Run time: n = number of vertices m = number of edges Each vertex is enqueued and dequeued at most once (why? They are never white again). Costs? Per vertex 1, n times O(n). Scans adjacency list only when dequeued. Therefore, each adjacency list only at most once. Sum of all adjacency lists depends on number of edges O(m). Total O(n + m) (i.e. linear in number of vertices and edges)

CSC317 2 Why are we doing this? BFS allows us to find shortest paths! Shortest path distance from s to v :  (s,v) … minimum number of edges from any path from vertex s to v ( ∞ if no paths exist) Theorem: Let G = (V,E) be a (un)directed graph. Let be an abitrary vertex. Then, for any edge. How are we going to proof this? If u is reachable from s, then so is v. In this case the shortest path from s to v can not be longer than the shortest path from s to u followed by the edge (u,v). So, the inequality holds. (Forgot something?) If u is not reachable from s then  (s,u)=∞ and the inequality holds.

CSC317 3 Main Theorem: Let G = (V,E) be a (un)directed graph and suppose that BFS is run on G from a given source vertex. Upon termination, for each vertex the value v.d computed by BFS satisfies v.d... Is the shortest path, shortes number of edges from source s to v. Proof: We use induction on the number of ENQUEUE operations. Our inductive hypothesis is for all. The basis of the induction is the situation immediately after enqueuing s in line 9 of BFS. Does the inductive hypothesis in the beginning hold? and for all. For the inductive step, consider a white vertex v that is discovered during the search for vertex u, implying that. From the above theorem (and assignment in line 15) we get

CSC317 4 What does that mean? Vertex v is then enqueued, and it is never enqueued again. I.e. clause of lines 14–17 is executed only for white vertices. Thus, the value of v.d never changes again!

CSC317 5 Graph algorithms part 2 Depth First Search (DFS) Search deeply first... (What’s the difference to BFS?) Explores edges out of most recently discovered vertex so long as there are still unexplored edges leaving it... and then backtracks (like searching in a maze). Run time as in BFS will be linear in number of vertices and edges O(n+m) Applications: -topological sort in directed acyclic graphs (sequences of events where one comes before another; later); -strongly connected components (later).

CSC317 6 Simple example of DFS ordering showing main idea and distinguishing from BFS: s a b c d e f The predecessor subgraph formed by DFS is composed of several trees because the search may repeat from multiple sources.

CSC One way to implement: Stack instead of queue. Last in first out (LIFO). But often implemented recursively.

CSC317 8 Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first search. // start time // we discovered u // finish time we are finished going through all of u’s neighbors

CSC317 9

10 Run time DFS: O(n + m) with n number vertices and m number edges. Procedure DFS-Visit is called exactly once for each vertex (only if white and then changed). During the DFS-Visit all adjacent nodes of each vertex are used, so overall includes all edges. Some properties of DFS: 1.) Every vertex v that gets discovered between start of u and end of u, is a child of u in the DFS tree. The v will also finish before u does (see path from u to v to y to x) This can be seen in recursion: DFS-Visit( u ) called first; and then calls DFSvisit( v ), which finishes first (last in first out, as in a stack) 2.) There is never an edge from a black to a white node (because if a node is black, we already visited all its neighbors). 3.) White path theorem: vertex v is a descendant of vertex u if and only if when the search discovers u, there is a path from u to v consisting entirely of white vertices.

CSC ) Disjoint trees, contained trees, and parenthesis theorem: Consider vertices u and v in a graph G(V,E). Then one of the following conditions hold: intervals [u.d, u.f] and [v.d, v.f] are entirely disjoint, and neither u nor v is a descendant of the other in the depth-first forest, interval [u.d, u.f] is contained entirely within [v.d, v.f], and u is a descendant of v (which means u was discovered after v ), or interval [v.d, v.f] is contained entirely within [u.d, u.f], and v is a descendant of u (which means v was discovered after u ) (vice versa)

CSC Application DFS: topological sort: A directed edge from u to v, means that v happens after u. Topological order: All directed edges only go forward (needs to be acyclic graph DAG). Useful for sorting problems with tasks where one needs to be done before another. Again linear time in vertices and edges O(n+m), since doing DFS. Definitions: A topological sort of a dag G = (V,E) is a linear ordering of all its vertices such that if G contains an edge (u,v) then u appears before v in the ordering. (If the graph contains a cycle, then no linear ordering is possible.)

CSC317 13

CSC How can we do that? We can perform topological sort in time Θ (V+E) since depth-first search takes Θ (V+E) time and it takes O(1) time to insert each of the |V| vertices onto the front of the linked list.

CSC Application DFS: strongly connected components: