Graph Searching (Graph Traversal) Algorithm Design and Analysis 2015 - Week 8 Bibliography: [CLRS] – chap 22.2 –

Slides:



Advertisements
Similar presentations
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.
Advertisements

Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
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.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
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.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
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
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
1 7/2/2015 ITCS 6114 Graph Algorithms. 2 7/2/2015 Graphs ● A graph G = (V, E) ■ V = set of vertices ■ E = set of edges = subset of V  V ■ Thus |E| =
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
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.
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.
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.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Graph Algorithms.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
CSC 413/513: Intro to Algorithms Graph Algorithms.
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/6/2016 CS 332: Algorithms Graph Algorithms.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
Depth-First Search Lecture 21: Graph Traversals
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 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
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
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.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Introduction to Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs-Part II Depth First Search (DFS)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill.
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Advanced Algorithms Analysis and Design
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Text Book: Introduction to algorithms By C L R S
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 – Breadth First Search [CLRS] – chap 22.3 – Depth First Search

Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex (and every edge) –Side-effect: build the subgraph resulting from the trace of this exploration Different methods of exploration => –Different order of vertex discovery –Different shape of the exploration trace subgraph (which can be a spanning tree of the graph) Methods of exploration: –Breadth-First Search –Depth-First Search

Breadth-First Search Explore a graph by following rules: –Pick a source vertex to be the root –Expand frontier of explored vertices across the breadth of the frontier

Breadth-First Search Associate vertex “colors” to guide the algorithm –White vertices have not been discovered All vertices start out white –Grey vertices are discovered but not fully explored They may be adjacent to white vertices –Black vertices are discovered and fully explored They are adjacent only to black and gray vertices Explore vertices by scanning adjacency list of grey vertices

Breadth-First Search Every vertex v will get following attributes: –v.color: (white, grey, black) – represents its exploration status –v.pi represents the “parent” node of v (v has ben reached as a result of exploring adjacencies of pi) –v.d represents the distance (number of edges) from the initial vertex (the root of the BFS)

s=initial vertex (root) Mark all vertexes except the root as WHITE (not yet discovered) Mark root as GREY (start exploration) Use a Queue to store the exploration frontier Push s in Queue While there are nodes in the frontier (Queue) Pop a node u from Queue Discover all nodes v adjacent to u Mark u as fully explored

Example – Applying BFS r stu v wxy

Example ∞ 0 ∞∞ ∞∞∞∞ r stu v wxy Q s

10 ∞∞ ∞ 1 ∞∞ r stu v wxy Q wr

102 ∞ ∞ 12 ∞ r stu v wxy Q rtx

102 ∞ 212 ∞ r stu v wxy Q txv

∞ r stu v wxy Q xvu

r stu v wxy Q vuy

r stu v wxy Q uy

r stu v wxy Q y

r stu v wxy Q

Θ(V) Θ(E) Θ(V+E) Analysis

Analysis of BFS If the graph is implemented using adjacency structures: –The adjacency list of each vertex is scanned only when the vertex is dequeued => every vertex is dequeued only once => the sum of the lengths of all adjacency lists is Θ(E) –The total time for BFS is O(V+E) What if the graph is implemented using adjacency matrix ?

Shortest paths In an unweighted graph, the shortest-path distance δ(s,v) from s to v is the minimum number of edges in any path from s to v. If there is no path from s to v, then δ(s,v)=∞

Properties of BFS If G is a connected graph, then after BFS all its vertices will be BLACK. For every vertex v, v.d is equal with the shortest path from s to v δ(s,v) Proofs !

BFS trees The procedure BFS builds a predecessor subgraph G π as it searches the graph G The predecessor subgraph G π is a breadth-first tree if V π consists of the vertices reachable from s and, for all v in V π, the subgraph G π contains a unique simple path from s to v that is also a shortest path from s to v in G. –A breadth-first tree is indeed a tree, since it is connected and the number of its edges is with 1 smaller than the number of vertices.

Print shortest path from s to v Assuming that BFS has already computed a breadth-first tree with the root s:

BFS Questions What happens with BFS if G is not connected ? Is it necessary to color nodes in BFS using 3 different colors ?

Depth-First Search Depth-first search is another strategy for exploring a graph –Explore “deeper” in the graph whenever possible –Edges are explored out of the most recently discovered vertex v that still has unexplored edges –When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

Depth-First Search Vertices initially colored white Then colored gray when discovered Then black when their exploration is finished

Depth-First Search Every vertex v will get following attributes: –v.color: (white, grey, black) – represents its exploration status –v.pi represents the “parent” node of v (v has ben reached as a result of exploring adjacencies of pi) –v.d represents the time when the node is discovered –v.f represents the time when the exploration is finished

Example – Applying DFS u vw x yz

u vw x yz 1/

Example – Applying DFS u vw x yz 1/ 2/

Example – Applying DFS u vw x yz 1/ 2/ 3/

Example – Applying DFS u vw x yz 1/ 2/ 3/4/

Example – Applying DFS u vw x yz 1/ 2/ 3/4/5

Example – Applying DFS u vw x yz 1/ 2/ 3/64/5

Example – Applying DFS u vw x yz 1/ 2/7 3/64/5

Example – Applying DFS u vw x yz 1/8 2/7 3/64/5

Example – Applying DFS u vw x yz 1/8 2/7 3/64/5 9/

Example – Applying DFS u vw x yz 1/8 2/7 3/64/5 9/ 10/

Example – Applying DFS u vw x yz 1/8 2/7 3/64/5 9/ 10/11

Example – Applying DFS u vw x yz 1/8 2/7 3/64/5 9/12 10/11

Properties of DFS If G is a connected graph, then after DFS- VISIT all its vertices will be BLACK.

DFS Parenthesis theorem For all vertices u and v, exactly one of the following holds: 1.u.d < u.f < v.d < v.f or v.d < v.f < u.d < u.f (i.e., the intervals [u.d; u.f] and [v.d; v.f] are disjoint) and neither of u and v is a descendant of the other. 2.u.d < v.d < v.f < u.f and v is a descendant of u. 3.v.d < u:d < u.f < v.f and u is a descendant of v. –u.d < v.d < u.f < v.f cannot happen for any vertices !

Example – Parenthesis property u vw x yz 1/8 2/7 3/64/5 9/12 10/ u v y x w z

Classification of edges Tree edges are edges in the depth-first forest G. Edge (u,v) is a tree edge if v was first discovered by exploring edge (u,v) –a tree edge leads towards a WHITE vertex v Back edges are those edges (u,v) connecting a vertex u to an ancestor in a depth-first tree. We consider self-loops, which may occur in directed graphs, to be back edges. –A back edge leads towards a GRAY vertex v Forward edges are those nontree edges (u,v) connecting a vertex u to a descendant in a depth-first tree. –A forward edge leads toward a BLACK vertex v Cross edges are all other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different depth-first trees. –A cross edge leads toward a BLACK vertex v

Example – Types of edges u vw x yz 1/8 2/7 3/64/5 9/12 10/11 F B C B

Theorem In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge.

BFS and DFS - Conclusions Methodically explore every vertex (and every edge) of a directed or undirected graph, build predecessor spanning trees BFS and DFS have interesting properties, that will be useful in many applications: –Testing connectivity (connected components, articulation points, bridges, biconnected components, strongly connected components) –Testing existence of cycles, topological sorting

Priority-first search All the graph-search methods are actually the same algorithm! –Maintain a set S of explored vertices (the black vertices) –Grow S by exploring edges with exactly one endpoint leaving S (the frontier of S – the grey vertices). –The difference: which vertex from the frontier gets chosen ? DFS. Take vertex which was discovered most recently. BFS. Take vertex which was discovered least recently. Prim. Take vertex connected by edge of minimum weight Dijkstra. Take vertex that is closest to the source. –All 4 algorithms can be implemented with a PriorityQueue; only difference is the expression of the priority value !