Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.

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

Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Algorithms and Data Structures
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
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.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs.
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.
Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
Tree Searching Breadth First Search Dept First Search.
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.
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.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
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,
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
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.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
SEARCHING GRAPHS Chapter INTRO  in our tree discussion, learned three ways of traversing a graph: –Preorder (prefix) –Inorder (infix) –Postorder.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CSC 172 DATA STRUCTURES.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs Breadth First Search & Depth First Search
Graphs Chapter 20.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Csc 2720 Instructor: Zhuojun Duan
CC 215 Data Structures Graph Searching
Graphs Breadth First Search & Depth First Search
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs Graph transversals.
Finding Shortest Paths
Graphs Representation, BFS, DFS
Search Related Algorithms
Graph Representation (23.1/22.1)
Elementary Graph Algorithms
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
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
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms: Design and Analysis
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
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:

Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation 1

Traversing a Graph Breadth First Search (BFS) Depth First Search (DFS) 2

Traversing a Graph Many application of graph requires a structured system to examine the vertices and edges of a graph G That is a graph traversal, which means visiting all the nodes of the graph There are two graph traversal methods (a) Breadth First Search (BFS) Think Queue (b) Depth First Search (DFS) Think Stack 3

Overview Goal To systematically visit the nodes of a graph A tree is a directed, acyclic, graph (DAG) If the graph is a tree, DFS is exhibited by preorder, postorder, and (for binary trees) inorder traversals BFS is exhibited by level-order traversal 4

Example Policy: Visit adjacent nodes in increasing index order 5

Preorder DFS: Start with Node

Preorder DFS: Start with Node Push 5 7

Preorder DFS: Start with Node Pop/Visit/Mark 5 8

Preorder DFS: Start with Node Push 2, Push 1 9

Preorder DFS: Start with Node Pop/Visit/Mark 1 10

Preorder DFS: Start with Node Push 4, Push 2, Push 0 11

Preorder DFS: Start with Node Pop/Visit/Mark 0 12

Preorder DFS: Start with Node Push 7, Push 3 13

Preorder DFS: Start with Node Pop/Visit/Mark 3 14

Preorder DFS: Start with Node Push 2 15

Preorder DFS: Start with Node Pop/Mark/Visit 2 16

Preorder DFS: Start with Node Pop/Mark/Visit 7 17

Preorder DFS: Start with Node Push 6 18

Preorder DFS: Start with Node Pop/Mark/Visit 6 19

Preorder DFS: Start with Node Pop (don’t visit) 2 20

Preorder DFS: Start with Node Pop/Mark/Visit 4 21

Preorder DFS: Start with Node Pop (don’t visit) 2 22

Preorder DFS: Start with Node Done 23

Breadth-first Search Ripples in a pond Visit designated node Then visited unvisited nodes a distance i away, where i = 1, 2, 3, etc. For nodes the same distance away, visit nodes in systematic manner (eg. increasing index order) 24

BFS: Start with Node

BFS: Start with Node

BFS: Node one-away

BFS: Visit 1 and

BFS: Nodes two-away

BFS: Visit 0 and

BFS: Nodes three-away

BFS: Visit nodes 3 and

BFS: Node four-away

BFS: Visit

Tree searches A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem) For some problems, any goal node is acceptable ( N or J ); for other problems, you want a minimum- depth goal node, that is, a goal node nearest the root (only J ) LM N OP G Q H J IK FED BC A Goal nodes 35

Depth-first searching A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path For example, after searching A, then B, then D, the search backtracks and tries another path from B Node are explored in the order A B D E H L M N I O P C F G J K Q N will be found before J LM N OP G Q H J IK FED BC A 36

Breadth-first searching A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away For example, after searching A, then B, then C, the search proceeds with D, E, F, G Node are explored in the order A B C D E F G H I J K L M N O P Q J will be found before N LM N OP G Q H J IK FED BC A 37

Graph Traversal Problem: Search for a certain node or traverse all nodes in the graph Depth First Search Once a possible path is found, continue the search until the end of the path Breadth First Search Start several paths at a time, and advance in each one step at a time 38

Exploring a Labyrinth Without Getting Lost A Depth-First Search (DFS) in an undirected graph G is like wandering in a labyrinth with a string and a can of red paint without getting lost. We start at vertex s, tying the end of our string to the point and painting s “visited”. Next we label s as our current vertex called u. Now we travel along an arbitrary edge (u, v). If edge (u, v) leads us to an already visited vertex v we return to u. If vertex v is unvisited, we unroll our string and move to v, paint v “visited”, set v as our current vertex, and repeat the previous steps. 39

Depth-First Search 40

41 Breadth-First Search Like DFS, a Breadth-First Search (BFS) traverses a connected component of a graph, and in doing so defines a spanning tree with several useful properties. The starting vertex s has level 0, and, as in DFS, defines that point as an “anchor.” In the first round, the string is unrolled the length of one edge, and all of the edges that are only one edge away from the anchor are visited. These edges are placed into level 1

42 Breadth-First Search In the second round, all the new edges that can be reached by unrolling the string 2 edges are visited and placed in level 2. This continues until every vertex has been assigned a level. The label of any vertex v corresponds to the length of the shortest path from s to v.

43 BFS - A Graphical Representation d) c) b)a)a)

More BFS 44

Applications: Finding a Path Find path from source vertex s to destination vertex d Use graph search starting at s and terminating as soon as we reach d Need to remember edges traversed Use depth – first search ? Use breath – first search? 45

DFS vs. BFS E F G B C D A start destination A DFS on A A DFS on B B A DFS on C B C A B Return to call on B D Call DFS on D A B D Call DFS on G G found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G DFS Process 46

DFS vs. BFS E F G B C D A start destination BFS Process A Initial call to BFS on A Add A to queue B Dequeue A Add B frontrearfrontrear C Dequeue B Add C, D frontrear DD Dequeue C Nothing to add frontrear G Dequeue D Add G frontrear found destination - done! Path must be stored separately 47

Example In DFS, each vertex has three possible colors representing its state: white: vertex is unvisited; gray: vertex is in progress black: DFS has finished processing the vertex. 48

Example Source graph 49

Mark a vertex 1 as visited. Print the vertex. 50

There is an edge (1, 4) and a vertex 4 is unvisited. Go there. 51

Mark the vertex 4 as visited. Print the vertex 52

There is an edge (4, 2) and vertex a 2 is unvisited. Go there. 53

Mark the vertex 2 as visited. Print the vertex. 54

There is an edge (2, 5) and a vertex 5 is unvisited. Go there. 55

Mark the vertex 5 as visited. Print the vertex. 56

There is an edge (5, 3) and a vertex 3 is unvisited. Go there. 57

Mark the vertex 3 as visited. Print the vertex. 58

There are no ways to go from the vertex 3. So, backtrack to the vertex 5 to check other adjacent vertices of the vertex 5. 59

There is an edge (5, 4), but the vertex 4 is already visited. So do not go to the vertex 4. Continue with other adjacent vertices of vertex 5. 60

There are no ways to go from the vertex 5. So, backtrack to the vertex 2 to check other adjacent vertices of the vertex 2. 61

There are no more edges, adjacent to vertex 2. So, backtrack to the vertex 4 to check other adjacent vertices of the vertex 4. 62

There is an edge (4, 5), but the vertex 5 is already visited. So do not go to the vertex 5. Continue with other adjacent vertices of vertex 4. 63

There are no more edges, adjacent to the vertex 4. So, backtrack to the vertex 1 to check other adjacent vertices of the vertex 1. 64

There are no more edges, adjacent to the vertex 1. DFS is over. 65

A depth first traversal method tends to traverse very long, narrow trees; whereas breadth first traversal method tends to traverse very wide, short trees. 66

Summary Breadth First Search (BFS) Depth First Search (DFS) 67