Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Breadth-First and Depth-First Search
Chapter 8, Part I Graph Algorithms.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
CSE 589 Applied Algorithms Spring 1999 Course Introduction Depth First Search.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 9 Instructor: Paul Beame.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CSE 589 Applied Algorithms Course Introduction. CSE Lecture 1 - Spring Instructors Instructor –Richard Ladner –206.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Search Related Algorithms. Graph Code Adjacency List Representation:
Tree Searching Breadth First Search Dept First Search.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
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.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Graph and Digraph Sung Yong Shin TC Lab. CS Dept., KAIST.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Minimum Spanning Trees
CC 215 Data Structures Graph Searching
Spanning Trees Longin Jan Latecki Temple University based on slides by
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Spanning Trees.
Chapter 11 Graphs.
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Minimum Spanning Trees
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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:

Graph Searching CSIT 402 Data Structures II

2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as deep as possible ›Whenpath ends, it backtracks ›When backtracking, it explores side-paths that were not taken ›Uses a stack (instead of a queue in BFS) ›Permits an easy recursive implementation

3 Depth First Search Algorithm Recursive marking algorithm Initially every vertex is unmarked DFS(i: vertex) mark i; for each j adjacent to i do if j is unmarked then DFS(j) end{DFS} Marks all vertices reachable from i i j k DFS(i) DFS(j)

4 DFS Application: Spanning Tree Given a (undirected) graph G(V,E) a spanning tree of G is a graph G’(V’,E’) ›V’ = V, the tree touches all vertices (spans) the graph ›E’ is a subset of E such G’ is connected and there is no cycle in G’ ›A graph is connected if given any two vertices u and v, there is a path from u to v

5 Example of DFS: Graph connectivity and spanning tree DFS(1)

6 Example Step DFS(1) DFS(2) Red links will define the spanning tree if the graph is connected

7 Example Step DFS(1) DFS(2) DFS(3) DFS(4) DFS(5)

8 Example Steps 6 and DFS(1) DFS(2) DFS(3) DFS(4) DFS(5) DFS(3) DFS(7)

9 Example Steps 8 and DFS(1) DFS(2) DFS(3) DFS(4) DFS(5) DFS(7) Now back up.

10 Example Step 10 (backtrack) DFS(1) DFS(2) DFS(3) DFS(4) DFS(5) Back to 5, but it has no more neighbors.

11 Example Step DFS(1) DFS(2) DFS(3) DFS(4) DFS(6) Back up to 4. From 4 we can get to 6.

12 Example Step DFS(1) DFS(2) DFS(3) DFS(4) DFS(6) From 6 there is nowhere new to go. Back up.

13 Example Step DFS(1) DFS(2) DFS(3) DFS(4) Back to 4. Keep backing up.

14 Example Step DFS(1) All nodes are marked so graph is connected; red links define a spanning tree All the way back to 1. Done.

15 Adjacency List Implementation Adjacency lists G Mark Index next

16 Another Use for Depth First Search: Connected Components connected components

17 Connected Components connected components are labeled

18 Depth-first Search for Labeling Connected components Main { i : integer for i = 1 to n do M[i] := 0; // all initially unmarked Label := 1; // Each connected set has different label for i = 1 to n do // if i not labeled call DFS to mark connected vertices if M[i] = 0 then DFS(G,M,i,label); // Found connected vertices; on to next set label := label + 1; } DFS(G[]: node ptr array, M[]: int array, i,label: int) { v : node pointer; M[i] := label; v := G[i]; // first neighbor // while v  null do // recursive call (below) if M[v.index] = 0 then DFS(G,M,v.index,label); v := v.next; // next neighbor // }

19 Performance DFS n vertices and m edges Storage complexity O(n + m) Time complexity O(n + m) Linear Time!

20 Graph Searching Methodology Breadth-First Search (BFS) Breadth-First Search (BFS) ›Use a queue to explore neighbors of source vertex, then neighbors of neighbors etc. ›All nodes at a given distance (in number of edges) are explored before we go further ›Very similar to Dijkstra Shortest Path Algorithm

21 Breadth-First Search BFS Initialize Q to be empty; Enqueue(Q,1) and mark 1; while Q is not empty do i := Dequeue(Q); for each j adjacent to i do if j is not marked then Enqueue(Q,j) and mark j; end{BFS}

22 Can do Connectivity using BFS Uses a queue to order search Queue =

23 Beginning of example Queue = 2,4,6 Mark while on queue to avoid putting in queue more than once

24 Depth-First vs Breadth-First Depth-First ›Stack or recursion ›Many applications, particularly greedy ones Breadth-First ›Queue (recursion no help) ›Can be used to find shortest paths from the start vertex ›Better for exhaustive search

25 Minimum Spanning Tree Edges are weighted: find minimum cost spanning tree Applications ›Find cheapest way to wire your house ›Find minimum cost (distance?) to wire a message on the Internet

26 Strategy for Minimum Spanning Tree For any spanning tree T, inserting an edge e new not in T creates a cycle But ›Removing any edge e old from the cycle gives back a spanning tree ›If e new has a lower cost than e old we have progressed!

27 Strategy Strategy for construction: ›Add an edge of minimum cost that does not create a cycle (greedy algorithm) ›Repeat |V| -1 times ›Correct since if we could replace an edge with one of lower cost, the algorithm would have picked it up

Next: Spanning Trees Prim Kruskal 28