Graph Searching CSE 373 Data Structures Lecture 20.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
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.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan Mohammad Reza Akhavan.
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
Breadth-First and Depth-First Search
Chapter 8, Part I Graph Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
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)
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:
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
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?
CSE 589 Applied Algorithms Spring 1999 Course Introduction Depth First Search.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph & BFS.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Graph Traversals CSC 172 SPRING 2002 LECTURE 26. Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like.
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.
Search Related Algorithms. Graph Code Adjacency List Representation:
Tree Searching Breadth First Search Dept First Search.
Spring 2015 Lecture 10: Elementary Graph Algorithms
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,
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.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
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|
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,
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Depth-First Search Lecture 21: Graph Traversals
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
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.
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.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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)
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
CC 215 Data Structures Graph Searching
Elementary Graph Algorithms
CSE 373 Data Structures Lecture 16
Depth-First Search D B A C E Depth-First Search Depth-First Search
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Graph Searching CSE 373 Data Structures Lecture 20

3/7/03Graph Searching - Lecture 202 Readings Reading ›Sections 9.5 and 9.6

3/7/03Graph Searching - Lecture 203 Graph Searching 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

3/7/03Graph Searching - Lecture 204 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

3/7/03Graph Searching - Lecture 205 Graph Searching Methodology Depth-First Search (DFS) 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

3/7/03Graph Searching - Lecture 206 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

3/7/03Graph Searching - Lecture 207 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

3/7/03Graph Searching - Lecture 208 Example of DFS: Graph connectivity and spanning tree DFS(1)

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

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

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

3/7/03Graph Searching - Lecture 2012 Example Steps 8 and DFS(1) DFS(2) DFS(3) DFS(4) DFS(5) DFS(7)

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

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

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

3/7/03Graph Searching - Lecture 2016 Example Step DFS(1) DFS(2) DFS(3) DFS(4)

3/7/03Graph Searching - Lecture 2017 Example Step DFS(1) All nodes are marked so graph is connected; red links define a spanning tree

3/7/03Graph Searching - Lecture 2018 Adjacency List Implementation Adjacency lists G M Index next

3/7/03Graph Searching - Lecture 2019 Connected Components connected components

3/7/03Graph Searching - Lecture 2020 Connected Components connected components are labeled

3/7/03Graph Searching - Lecture 2021 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]; while v  null do if M[v.index] = 0 then DFS(G,M,v.index,label); v := v.next; }

3/7/03Graph Searching - Lecture 2022 Performance DFS n vertices and m edges Storage complexity O(n + m) Time complexity O(n + m) Linear Time!

3/7/03Graph Searching - Lecture 2023 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}

3/7/03Graph Searching - Lecture 2024 Can do Connectivity using BFS Uses a queue to order search Queue =

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

3/7/03Graph Searching - Lecture 2026 Depth-First vs Breadth-First Depth-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

3/7/03Graph Searching - Lecture 2027 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