Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

Graph Traversals and Minimum Spanning Trees. Graph Terminology.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
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
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
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.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
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,
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 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
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,
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
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.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
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.
Graph Search Applications, Minimum Spanning Tree
Graphs Chapter 20.
A vertex u is reachable from vertex v iff there is a path from v to u.
Discrete Mathematicsq
Connected Components Minimum Spanning Tree
Minimum Spanning Trees
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs Chapter 7 Visit for more Learning Resources.
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
Presentation transcript:

Graph Traversal BFS & DFS

Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi j

Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u

Graph Search Methods (traversal) A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v

Graph Search Methods Many graph problems solved using a search method.  Finding Path(shortest) from one vertex to another.  Check if the graph is connected?  Find a (minimum) spanning tree. Commonly used search (traversal) methods:  Breadth-first search.  Depth-first search.

Breadth-First Search Visit start vertex (selected by user) and put into a FIFO queue. Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue. BFS(G,1)

Breadth-First Search Example Start search at vertex

Breadth-First Search Example Visit/mark/label start vertex and put in a FIFO queue FIFO Queue 1

Breadth-First Search Example Remove 1 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue 1 Blue vertex is visited

Breadth-First Search Example Remove 1 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 2 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 2 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 4 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 4 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 3 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 5 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 7 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 9 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 9 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Remove 8 from Q; visit adjacent unvisited vertices; put in Q FIFO Queue

Breadth-First Search Example Queue is empty. Search terminates FIFO Queue

Example start from 2, 7 and

Breadth-First Search Property All vertices reachable from the start vertex (including the start vertex) are visited.

Breadth-first-search

Example start at

Example start at 1

Example start at

Example start at

Finding a Path From Vertex v To Vertex u Start a breadth-first search at vertex v. Terminate when vertex u is visited or when Q becomes empty (whichever occurs first).

Is The Graph Connected? Start a breadth-first search at any vertex of the graph. Graph is connected iff all n vertices get visited.

Connected Components Start a breadth-first search at any as yet unvisited vertex of the graph. Newly visited vertices (plus edges between them) define a component. Repeat until all vertices are visited.

Connected Components

Spanning Tree Breadth-first search from vertex Breadth-first spanning tree.

Spanning Tree Start a breadth-first search at any vertex of the graph. If graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree (breadth-first spanning tree).

Depth-First Search depthFirstSearch(v) { Label vertex v as reached. for (each unreached vertex u adjacenct from v) depthFirstSearch(u); }

Depth-First Search Example Start search at vertex Label vertex 1 and do a depth first search from either 2 or Suppose that vertex 2 is selected.

Depth-First Search Example Label vertex 2 and do a depth first search from either 3, 5, or Suppose that vertex 5 is selected.

Depth-First Search Example Label vertex 5 and do a depth first search from either 3, 7, or Suppose that vertex 9 is selected.

Depth-First Search Example Label vertex 9 and do a depth first search from either 6 or Suppose that vertex 8 is selected.

Depth-First Search Example Label vertex 8 and return to vertex From vertex 9 do a dfs(6). 6

Depth-First Search Example Label vertex 6 and do a depth first search from either 4 or Suppose that vertex 4 is selected.

Depth-First Search Example Label vertex 4 and return to From vertex 6 do a dfs(7). 7

Depth-First Search Example Label vertex 7 and return to Return to 9.

Depth-First Search Example Return to 5.

Depth-First Search Example Do a dfs(3). 3

Depth-First Search Example Label 3 and return to Return to 2.

Depth-First Search Example Return to 1.

Depth-First Search Example Return to invoking method.

Example start from 2, 7 and

Depth-First Search Properties Same properties with respect to path finding, connected components, and spanning trees. Edges used to reach unlabeled vertices define a depth-first spanning tree when the graph is connected. There are problems for which bfs is better than dfs and vice versa.

Depth-first-search

54 Graph Algorithms Minimum Spanning Trees

55 Problem: Laying Telephone Wire Central office

56 Wiring: Naïve Approach Central office Expensive!

57 Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

58 Minimum Spanning Tree (MST) (see Weiss, Section ) it is a tree (i.e., it is acyclic) it covers all the vertices V –contains |V| - 1 edges the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique A minimum spanning tree is a subgraph of an undirected weighted graph G, such that

59 Applications of MST Any time you want to visit all vertices in a graph at minimum cost (e.g., wire routing on printed circuit boards, sewer pipe layout, road planning…) Provides a heuristic for traveling salesman problems.

60 How Can We Generate a MST? a c e d b a c e d b

Kruskal’s Algorithm u is a vertex FIND-SET(u) returns a representative element from the set that contains u. We can determine whether two vertices u and v belong to the same tree by testing whether FIND-SET(u) equals FIND-SET(v).

Kruskal’s Algorithm a c e d b

Example 1

Example 2

Homework #3  Implement Kruskal’s Algorithm for finding a minimum spanning tree  Test your program using  Using adjacency matrix to represent the graph  Output the edges  Due date: May 4 by 11:59pm