Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:

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

1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
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.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graph Searching CSE 373 Data Structures Lecture 20.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
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?
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
Breadth-First and Depth-First Search
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 11 DFS Properties of DFS Topological sort.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
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)
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 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.
 Last lesson  Graphs  Today  Graphs (Implementation, Traversal)
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
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”
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
1  Introduction to Graph Data Structure  Applications  Graph Searching  Minimum Spanning Trees  Shortest Path problems.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
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.
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.
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|
Graph theory Prof Amir Geva Eitan Netzer.
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)
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.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Elementary Graph Algorithms
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)
Finding Shortest Paths
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.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Graphs Searching

Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately: build a tree on the graph  Pick a vertex as the root  Choose certain edges to produce a tree  Note: might also build a forest if graph is not connected

Breadth-First Search Given a G=(V,E) and distinguished source vertex s, BFS systematically explores the edges of G to “discover” every vertex reachable from s. Creates a BFS tree rooted at s that contains all such vertices. Expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier. The algorithm discovers all vertices at distance k from s before discovering any vertices at distance k+1

will 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 and represent the frontier between the discovered and the undiscovered.  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

BFS(G, s) { initialize vertices; Q = {s};// Q is a queue initialize to s while (Q not empty) { u = Dequeue(Q); for each v  u->adj { if (v->color == WHITE){ v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } What does v->p represent? What does v->d represent? Breadth-First Search

        rstu vwxy Breadth-First Search: Example

  0      rstu vwxy s Q:

Breadth-First Search: Example 1  0 1     rstu vwxy w Q: r

Breadth-First Search: Example 1    rstu vwxy r Q: tx

Breadth-First Search: Example   rstu vwxy Q: txv

Breadth-First Search: Example  rstu vwxy Q: xvu

Breadth-First Search: Example rstu vwxy Q: vuy

Breadth-First Search: Example rstu vwxy Q: uy

Breadth-First Search: Example rstu vwxy Q: y

Breadth-First Search: Example rstu vwxy Q: Ø

Breadth-First Search: Properties BFS calculates the shortest-path distance to the source node  Shortest-path distance  (s,v) = minimum number of edges from s to v, or  if v not reachable from s BFS builds breadth-first tree, in which paths to root represent shortest paths in G  Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time

Depth First Search Depth-first search: Strategy Go as deep as can visiting un-visited nodes Choose any un-visited vertex when you have a choice When stuck at a dead-end, backtrack as little as possible Back up to where you could go to another unvisited vertex Then continue to go on from that point Eventually you’ll return to where you started

DFS Algorithm DFS(G) for each vertex u  V[G]{ color[u]=white parent[u]=NULL } time=0 for each vertex u  V[G]{ if color[u]=white then DFS-VISIT(u) }

DFS-VISIT(u) color[u]=GRAY time=time+1 d[u]=time for each vertex v  adj[u] { if color[v]=white Then parent[v]=u DFS-VISIT(v) } color[u]=black f[u]=time=time+1