Algorithms: Design and Analysis

Slides:



Advertisements
Similar presentations
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Advertisements

GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.
Graphs – Depth First Search ORD DFW SFO LAX
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
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.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.
Depth-First Search Lecture 21: Graph Traversals
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Graphs and Paths : Chapter 15 Saurav Karmakar
Discrete Maths 11. Graph Theory Objective
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Graph Search Applications, Minimum Spanning Tree
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
CSE 373 Data Structures and Algorithms
IOI/ACM ICPC Training 4 June 2005.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
Fundamentals, Terminology, Traversal, Algorithms
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Searching Graphs ORD SFO LAX DFW Spring 2007
CSC317 Graph algorithms Why bother?
Csc 2720 Instructor: Zhuojun Duan
Graph Searching.
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Chapter 14 Graph Algorithms
Ellen Walker CPSC 201 Data Structures Hiram College
CC 215 Data Structures Graph Searching
Discrete Maths 10. Trees 242/ , Semester 2, Objective
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
CSE 421: Introduction to Algorithms
Graph & BFS.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Search Related Algorithms
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
CSE 373: Data Structures and Algorithms
Depth-First Search D B A C E Depth-First Search Depth-First Search
Chapter 11 Graphs.
Tree Searching.
Depth-First Search Graph Traversals Depth-First Search DFS.
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Subgraphs, Connected Components, Spanning Trees
Searching Graphs ORD SFO LAX DFW Spring 2007
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMP171 Depth-First Search.
CSE 373: Data Structures and 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.
Copyright © Aiman Hanna All rights reserved
Depth-First Search CSE 2011 Winter April 2019.
Lecture 6 Graph Traversal
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Depth-First Search CSE 2011 Winter April 2019.
A vertex u is reachable from vertex v iff there is a path from v to u.
Spanning Trees Longin Jan Latecki Temple University based on slides by
3.2 Graph Traversal.
Lecture 11 Graph Algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Algorithms: Design and Analysis 240-310, Semester 2, 2018-2019 8. Graph Search Objective describe and compare depth-first and breadth-first graph search

1. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: visit every vertex Often the end result is a tree built over the graph called a spanning tree it visits every vertex, but not necessarily every edge Pick any vertex as the root Choose certain edges to produce a tree Note: we might build a forest if the graph is not connected

Example search then build a spanning tree (or trees)

Two Main Algorithms Depth First Search (DFS) Usually implemented using recursion More natural / commonly used Breadth First Search (BFS) Usually implemented using a queue Can solve shortest paths problem Uses more memory for larger graphs Running time for both: O(V + E)

2. Depth First Search (DFS) DFS is “depth first” because it always fully explores down a path away from a vertex v before it looks at other paths leaving v. Crucial DFS properties: uses recursion: essential for graph structures choice: at a vertex there may be a choice of several edges to follow to the next vertex backtracking: "return to where you came from" avoid cycles by grouping vertices into visited and unvisited

Directed Graph Example DFS works with directed and undirected graphs. b d e f c Graph G

Data Structures enum MARKTYPE {VISITED, UNVISITED}; struct cell { /* adj. list */ NODE nodeName; struct cell *next; }; typedef struct cell *LIST; struct graph { enum MARKTYPE mark; LIST successors; }; typedef struct graph GRAPH[NUMNODES];

The dfs() Function void dfs(NODE u, GRAPH G) // recursively search G, starting from u { LIST p; // runs down adj. list of u NODE v; // node in cell that p points at G[u].mark = VISITED; // visited u p = G[u].successors; while (p != NULL) { // visit u’s succ’s v = p->nodeName; if (G[v].mark == UNVISITED) dfs(v, G); // visit v p = p->next; } }

Calling dfs(a,G) call it d(a) for short Call Visited d(a) {a} d(a)-d(b) {a,b} d(a)-d(b)-d(c) {a,b,c} Skip b, return to d(b) d(a)-d(b)-d(d) {a,b,c,d} Skip c d(a)-d(b)-d(d)-d(e) {a,b,c,d,e} Skip c, return to d(d) continued

d(a)-d(b)-d(d)-d(f). {a,b,c,d,e,f} d(a)-d(b)-d(d)-d(f) {a,b,c,d,e,f} Skip c, return to d(d) d(a)-d(b)-d(d) {a,b,c,d,e,f} Return to d(b) d(a)-d(b) {a,b,c,d,e,f} Return to d(a) d(a) {a,b,c,d,e,f} Skip d, return

DFS Spanning Tree Since nodes are marked, the graph is searched as if it were a tree: A spanning tree is a subgraph of a graph G which contains all the verticies of G. a/1 b/2 d/4 c/3 e/5 f/6 c

Example 2 a b a b c d c d e f e f DFS h h g g the tree generated by DFS is drawn with thick lines

dfs() Running Time The time taken to search from a node is proportional to the no. of successors of that node. Total search time for all nodes = O(|V|) Total search time for all successors = time to search all edges = O(|E|) Total running time is O(V + E) continued

If the graph is dense, E >> V (E approaches V2) then the O(V) term can be ignored in that case, the total running time = O(E) or O(V2)

3. Uses of DFS Finding cycles in a graph e.g. for finding recursion in a call graph Searching complex locations, such as mazes Reachability detection i.e. can a vertex v be reached from vertex u? useful for e-mail routing; path finding Strong connectivity Topological sorting continued

Maze Traversal The DFS algorithm is similar to a classic strategy for exploring a maze mark each intersection, corner and dead end (vertex) as visited mark each corridor (edge ) traversed keep track of the path back to the previous branch points Graphs

Reachability DFS tree rooted at v: what are the vertices reachable from v via directed paths? E D C start at C E D A C F E D A B C F A B start at B

Strong Connectivity Each vertex can reach all other vertices a g c d e b e f g Graphs

Strong Connectivity Algorithm Pick a vertex v in G. Perform a DFS from v in G. If there’s a vertex not visited, print “no”. Let G’ be G with edges reversed. Perform a DFS from v in G’. If there’s a vertex not visited, print “no” If the algorithm gets here, print “yes”. Running time: O(V+E). a G: g c d e b f a G’: g c d e b f

Strongly Connected Components List all the subgraphs where each vertex can reach all the other vertices in that subgraph. Can also be done in O(V+E) time using DFS. a d c b e f g { a , c , g } { f , d , e , b }

4. Breadth-first Search (BFS) Process all the verticies at a given level before moving to the next level. Example graph G (again): a b c d e f h g

Informal Algorithm 1) Put the verticies into an ordering e.g. {a, b, c, d, e, f, g, h} 2) Select a vertex, add it to the spanning tree T: e.g. a 3) Add to T all edges (a,X) and X verticies that do not create a cycle in T i.e. (a,b), (a,c), (a,g) T = {a, b, c, g} a b c g continued

Repeat step 3 on the verticies just added, these are on level 1 i.e. b: add (b,d) c: add (c,e) g: nothing T = {a,b,c,d,e} Repeat step 3 on the verticies just added, these are on level 2 i.e. d: add (d,f) e: nothing T = {a,b,c,d,e,f} a level 1 b c g d e a b c g level 2 d e f continued

Repeat step 3 on the verticies just added, these are on level 3 i.e. f: add (f,h) T = {a,b,c,d,e,f,h} Repeat step 3 on the verticies just added, these are on level 4 i.e. h: nothing, so stop b c g d e level 3 f h continued

Resulting spanning tree: b a different spanning tree from the earlier solution c d e f h g

Example 2

Algorithm Graphically pre-built adjency list start node

BFS Code boolean marked[]; // visited this vertex? int edgeTo[]; // vertex number going to this vertex void bfs(Graph graph, int start) { Queue q = new Queue(); marked[start] = true; q.add(start); // add to end of queue while (!q.isEmpty()) { int v = q.remove(); // get from start of queue for (int w : graph.adjacentTo(v)) // v --> w if (!marked[w]) { edgeTo[w] = v; // save last edge on a shortest path marked[w] = true; q.add(w); // add to end of queue } } // end of bfs()

5. DFS vs. BFS Applications DFS BFS Spanning forest, connected components, paths, cycles  Shortest paths Biconnected components

DFS and BFS as Maze Explorers DFS is like one person exploring a maze do down a path to the end, get to a dead-end, backtrack, and try a different path BFS is like a group of searchers fanning out in all directions, each unrolling a ball of string. at a branch point, the searchers split up to explore all the branches at once if two groups meet up, they join forces (using the ball of string of the group that got there first) the group that gets to the exit first has found the shortest path

BFS Maze Graphically Also called flood filling; used in paint software.

Sequential / Parallel The BFS "fanning out" algorithm is best implemented in a parallel language, where each "group of explorers" is a separate thread of execution. e.g. use fork and join in Java The earlier implementation uses a queue to implement the fanning out as a sequential algorithm. DFS is inherently a sequential algorithm.