Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.

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.
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.
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:
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 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?
Tirgul 11 DFS Properties of DFS Topological sort.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
Shortest Path Problems
1 Data Structures DFS, Topological Sort Dana Shapira.
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.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
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.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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.
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.
Elementary Graph Algorithms Comp 122, Fall 2004.
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.
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/6/2016 CS 332: Algorithms Graph Algorithms.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
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
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.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Topological Sort Minimum Spanning Tree
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph Theory and Representation
Graph Representation (23.1/22.1)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Graph traversals / cutler1 Graph traversals Breadth first search Depth first search

Graph traversals / cutler2 Some applications Is G a tree Is G connected? Does G contain a cycle? Find connected components? Topological sorting

Graph traversals / cutler3 Breadth first search Given a graph G=(V,E) and a source vertex s, BFS explores the edges of G to “discover” (visit) each node of G reachable from s. Idea - expand a frontier one step at a time. Frontier is a FIFO queue (O(1) time to update)

Graph traversals / cutler4 Breadth first search Computes the shortest distance (dist) from s to any reachable node. Computes a breadth first tree (of parents) with root s that contains all the reachable vertices from s. To get O(|V|+|E|) we use an adjacency list representation. If we used an adjacency matrix it would be  (|V| 2 )

Graph traversals / cutler5 Coloring the nodes We use colors (white, gray and black) to denote the state of the node during the search. A node is white if it has not been reached (discovered). Discovered nodes are gray or black. Gray nodes are at the frontier of the search. Black nodes are fully explored nodes.

Graph traversals / cutler6 BFS - initialize procedure BFS(G:graph; s:node; var color:carray; dist:iarray; parent:parray); for each vertex u do color[u]:=white; dist[u]:=   (V)  parent[u]:=nil; end for color[s]:=gray; dist[s]:=0; init(Q); enqueue(Q, s);

Graph traversals / cutler7 BFS - main while not (empty(Q)) do u:=head(Q); for each v in adj[u] do if color[v]=white then O(E) color[v]:=gray; dist[v]:=dist[u]+1; parent[v]:=u; enqueue(Q, v); dequeue(Q); color[u]:=black; end BFS

Graph traversals / cutler8 BFS example r s t u r s t u v w x y v w x y r s t u r s t u v w x y       0 sw r 10   1  12 2   r t xt x v  

Graph traversals / cutler9 BFS example r s t u r s t u v w x y v w x y r s t u r s t u v w x y      0.0. x v uv u y 0      u yy  now y is removed from the Q and colored black

Graph traversals / cutler10 Analysis of BFS Initialization is  (|V|). Each node can be added to the queue at most once (it needs to be white), and its adjacency list is searched only once. At most all adjacency lists are searched. If graph is undirected each edge is reached twice, so loop repeated at most 2|E| times. If graph is directed each edge is reached exactly once. So the loop repeated at most |E| times. Worst case time O(|V|+|E|)

Graph traversals / cutler11 Depth First Search Goal - explore every vertex and edge of G We go “deeper” whenever possible. Directed or undirected graph G = (V, E). To get worst case time  (|V|+|E|) we use an adjacency list representation. If we used an adjacency matrix it would be  (|V| 2 )

Graph traversals / cutler12 Depth First Search Until there are no more undiscovered nodes. –Picks an undiscovered node and starts a depth first search from it. –The search proceeds from the most recently discovered node to discover new nodes. –When the last discovered node v is fully explored, backtracks to the node used to discover v. Eventually, the start node is fully explored.

Graph traversals / cutler13 Depth First Search In this version all nodes are discovered even if the graph is directed, or undirected and not connected The algorithm saves: –A depth first forest of the edges used to discover new nodes. –Timestamps for the first time a node u is discovered d[u] and the time when the node is fully explored f[u]

Graph traversals / cutler14 DFS procedure DFS(G:graph; var color:carray; d, f:iarray; parent:parray); for each vertex u do color[u]:=white;  parent[u]:=nil;  (V) end for time:=0; for each vertex u do if color[u]=white then DFS-Visit(u); end if; end for end DFS

Graph traversals / cutler15 DFS-Visit(u) color[u]=:gray; time:=time+1; d[u]:=time for each v in adj[u] do if color[v]=white then parent[v]:=u; DFS-Visit(v); end if; end for; color[u]:=black; time:=time+1; f[u]:=time; end DFS-Visit

Graph traversals / cutler16 DFS example (1) x y z u v w 1/ u v w 1/ x y z 2/ u v w 1/ x y z 2/ 3/ u v w 1/ x y z 2/ 3/4/ B

Graph traversals / cutler17 DFS example (2) u v w x y z u v w x y z 4/5 1/ 2/ 3/ B u v w 4/5 3/6 1/2/ B 4/5 3/6 1/2/7 B

Graph traversals / cutler18 DFS example (3) u v w x y z u v w x y z u v w x y z u v w F 4/5 3/6 1/82/7 B F 4/5 9 3/6 1/82/7 B F 4/5 9 3/6 10 1/82/7 B F 4/5 9 3/6 10/11 1/82/7 B C C C

Graph traversals / cutler19 DFS example (4) x y z F 4/5 9/12 3/6 10/11 1/82/7 B C u v w

Graph traversals / cutler20 Analysis DFS is  (|V|) (excluding the time taken by the DFS-Visits). DFS-Visit is called once for each node v. Its for loop is executed |adj(v)| times. The DFS-Visit calls for all the nodes take  (|E|). Worst case time  (|V|+|E|)

Graph traversals / cutler21 Some applications Is undirected G connected? Do dfsVisit(v). If all vertices are reached return yes, otherwise no. O(V + E) Find connected components. Do DFS. Assign the nodes in a single component a unique component number. Theta(V+E)

Graph traversals / cutler22 Labeling the edges (digraph) Tree edges - those belonging to the forest Back edges - edges from a node to an ancestor in the tree. Forward edges - a non tree edge from a node to a descendant in the tree. Cross edges - the rest of the edges, between trees and subtrees When a graph is undirected its edges are tree or back edges for DFS, tree or cross for BFS

Graph traversals / cutler23 Classifying edges of a digraph (u, v) is: –Tree edge – if v is white –Back edge – if v is gray –Forward or cross - if v is black (u, v) is: –Forward edge – if v is black and d[u] < d[v] (v was discovered after u) –Cross edge – if v is black and d[u] > d[v] (u discovered after v)

Graph traversals / cutler24 More applications Does directed G contain a directed cycle? Do DFS if back edges yes. Time O(V+E). Does undirected G contain a cycle? Same as directed but be careful not to consider (u,v) and (v, u) a cycle. Time O(V) since encounter at most |V| edges (if (u, v) and (v, u) are counted as one edge), before cycle is found. Is undirected G a tree? Do dfsVisit(v). If all vertices are reached and no back edges G is a tree. O(V)

Graph traversals / cutler25 Some applications Shortest distance from s to all the nodes in an acyclic graph – Do topological sort. Then, for every node u in the ordering (starting at s) use each edge (u, v) to compute the shortest distance from s to v, dist[v]. (dist[v] = min(dist[v], dist[u] + w(u, v))

Graph traversals / cutler26 Topological sort Given a DAG G Topological sort is a linear ordering of all the vertices of directed graph G such that if G contains the edge (u, v) u appears before v in the ordering TOPOLOGICAL-SORT(G) 1. Apply DFS(G) to compute f(v) for each vertex v 2. As each vertex is finished insert it at the front of a list 3. return the list