CSE 373 Data Structures Lecture 16

Slides:



Advertisements
Similar presentations
Graph Searching CSE 373 Data Structures Lecture 20.
Advertisements

Chapter 8, Part I Graph Algorithms.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
CSE 589 Applied Algorithms Spring 1999 Course Introduction Depth First Search.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 589 Applied Algorithms Course Introduction. CSE Lecture 1 - Spring Instructors Instructor –Richard Ladner –206.
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.
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
GRAPH ALGORITHM. 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,5),(6,7)
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,
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
CSC 172 DATA STRUCTURES.
Graphs A New Data Structure
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
Data Structures, Algorithms & Complexity
Lecture 11 Graph Algorithms
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Ellen Walker CPSC 201 Data Structures Hiram College
CC 215 Data Structures Graph Searching
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph.
Graph Algorithm.
Graph Search Lecture 17 CS 2110 Spring 2018.
CSE 421: Introduction to Algorithms
Elementary Graph Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Graphs Representation, BFS, DFS
Spanning Trees.
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Lectures on Graph Algorithms: searching, testing and sorting
Elementary Graph Algorithms
Chapter 11 Graphs.
Depth-First Search Graph Traversals Depth-First Search DFS.
CSE 421: Introduction to Algorithms
Spanning Trees Longin Jan Latecki Temple University based on slides by
Richard Anderson Autumn 2016 Lecture 5
Subgraphs, Connected Components, Spanning Trees
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Richard Anderson Winter 2009 Lecture 6
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Graphs Chapter 7 Visit for more Learning Resources.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Chapter 16 1 – Graphs Graph Categories Strong Components
Spanning Trees Longin Jan Latecki Temple University based on slides by
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Lecture 10 Graph Algorithms
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:

CSE 373 Data Structures Lecture 16 Graph Searching CSE 373 Data Structures Lecture 16

Graph Searching - Lecture 16 Readings Reading Sections 9.5 and 9.6 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Find Properties of Graphs Spanning trees Connected components Bipartite structure Biconnected components Applications Finding the web graph – used by Google and others Garbage collection – used in Java run time system Alternating paths for matching 5/10/03 Graph Searching - Lecture 16

Graph Searching Methodology 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 5/10/03 Graph Searching - Lecture 16

Graph Searching Methodology Depth-First Search (DFS) Searches down one path as deep as possible When no nodes available, it backtracks When backtracking, it explores side-paths that were not taken Uses a stack (instead of a queue in BFS) Allows an easy recursive implementation 5/10/03 Graph Searching - Lecture 16

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 5/10/03 Graph Searching - Lecture 16

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/10/03 Graph Searching - Lecture 16

Example of DFS: Graph connectivity and spanning tree 2 DFS(1) 1 7 3 5 6 4 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Example Step 2 2 DFS(1) DFS(2) 1 7 3 5 6 4 Red links will define the spanning tree if the graph is connected 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Example Step 5 2 DFS(1) DFS(2) DFS(3) DFS(4) DFS(5) 1 7 3 5 6 4 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Example Steps 6 and 7 2 DFS(1) DFS(2) DFS(3) DFS(4) DFS(5) DFS(7) 1 7 3 5 6 4 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Example Steps 8 and 9 2 DFS(1) DFS(2) DFS(3) DFS(4) DFS(5) DFS(7) 1 7 3 5 6 Now back up. 4 5/10/03 Graph Searching - Lecture 16

Example Step 10 (backtrack) 2 DFS(1) DFS(2) DFS(3) DFS(4) DFS(5) 1 7 3 5 6 Back to 5, but it has no more neighbors. 4 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Example Step 12 2 DFS(1) DFS(2) DFS(3) DFS(4) DFS(6) 1 7 3 5 6 Back up to 4. From 4 we can get to 6. 4 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Example Step 13 2 DFS(1) DFS(2) DFS(3) DFS(4) DFS(6) 1 7 3 5 6 From 6 there is nowhere new to go. Back up. 4 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Example Step 14 2 DFS(1) DFS(2) DFS(3) DFS(4) 1 7 3 5 Back to 4. Keep backing up. 6 4 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Example Step 17 2 DFS(1) 1 7 All the way back to 1. Done. 3 5 6 4 All nodes are marked so graph is connected; red links define a spanning tree 5/10/03 Graph Searching - Lecture 16

Adjacency List Implementation Adjacency lists 2 M G 1 7 1 2 3 4 5 6 7 2 4 6 3 1 7 3 4 5 5 6 1 3 5 3 7 4 1 4 6 5 2 4 Index next 5/10/03 Graph Searching - Lecture 16

Another Use for Depth First Search: Connected Components 2 1 3 7 10 4 9 5 6 8 11 3 connected components 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Connected Components 2 1 3 7 10 4 9 5 6 8 11 3 connected components are labeled 5/10/03 Graph Searching - Lecture 16

Depth-first Search for Labeling Connected components Main { i : integer for i = 1 to n do M[i] := 0; label := 1; for i = 1 to n do if M[i] = 0 then DFS(G,M,i,label); 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 if M[v.index] = 0 then DFS(G,M,v.index,label); v := v.next; // next neighbor // 5/10/03 Graph Searching - Lecture 16

Connected Components for Image Analysis 4 1 2 3 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Performance DFS n vertices and m edges Storage complexity O(n + m) Time complexity O(n + m) Linear Time! 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 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} 5/10/03 Graph Searching - Lecture 16

Can do Connectivity using BFS Uses a queue to order search 2 1 7 3 5 6 4 Queue = 1 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Beginning of example 2 1 7 3 5 6 4 Mark while on queue to avoid putting in queue more than once Queue = 2,4,6 5/10/03 Graph Searching - Lecture 16

Depth-First vs Breadth-First Stack or recursion Many applications Breadth-First Queue (recursion no help) Can be used to find shortest paths from the start vertex Can be used to find short alternating paths for matching 5/10/03 Graph Searching - Lecture 16

Graph Searching - Lecture 16 Minimum Spanning Tree Edges are weighted: find minimum cost spanning tree Applications Find cheapest way to wire your house Find minimum cost to wire a message on the Internet 5/10/03 Graph Searching - Lecture 16