SEARCHING GRAPHS Chapter 16.2. INTRO  in our tree discussion, learned three ways of traversing a graph: –Preorder (prefix) –Inorder (infix) –Postorder.

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

GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.
1 Section 9.4 Spanning Trees. 2 Let G be a simple graph. A spanning subtree of G is a subgraph of G containing every vertex of G –must be connected; contains.
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
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 8, Part I Graph Algorithms.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Alyce Brady CS 510: Computer Algorithms Breadth-First Graph Traversal Algorithm.
Graph Traversals Depth-First Traversal. The Algorithm.
Design and Analysis of Algorithms - Chapter 5
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Applications of Depth-First Search
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
Transforming Infix to Postfix
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
CS261 Data Structures DFS and BFS – Edge List Representation.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
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,
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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.
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.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Now, Chapter 5: Decrease and Conquer Reduce problem instance to smaller instance of the same problem and extend solution Solve smaller instance Extend.
Breadth-first and depth-first traversal CS1114
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
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 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
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.
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.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
COMP108 Algorithmic Foundations Graph Theory
Graphs A New Data Structure
Thinking about Algorithms Abstractly
Breadth First and Depth First
A vertex u is reachable from vertex v iff there is a path from v to u.
Csc 2720 Instructor: Zhuojun Duan
Ellen Walker CPSC 201 Data Structures Hiram College
Graphs.
Graphs Part 2 Adjacency Matrix
Depth-First Search D B A C E Depth-First Search Depth-First Search
COMP171 Depth-First Search.
Depth-First Search CSE 2011 Winter April 2019.
COMP108 Algorithmic Foundations Graph Theory
Graphs Chapter 7 Visit for more Learning Resources.
Depth-First Search CSE 2011 Winter April 2019.
A vertex u is reachable from vertex v iff there is a path from v to u.
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:

SEARCHING GRAPHS Chapter 16.2

INTRO  in our tree discussion, learned three ways of traversing a graph: –Preorder (prefix) –Inorder (infix) –Postorder (postorder)  traversing easy if we begin at root (can get to every other node from there)  in a graph, however, some nodes may not be reachable from a given node, making it impossible to traverse the whole graph  graphs are used to model many real-world systems –ex: towns and connecting railways –some towns may not be reachable on the graph if there is no rail service to that town  two basic search algorithms for graphs: –1) breadth-first –2) depth-first

DEPTH-FIRST SEARCH (TREE)  Consider the tree:  Perform depth-first: –Visit each node –Look at its children –Start at 10: Look at 5 Now children of 5: Look at 2 Child of 2: Look at 1 backtrack to 5 Look at 7 Backtrack to 10 –Repeat for right child of 10 –Handle backtracking with a STACK

DEPTH-FIRST (GRAPH) - same concept as with a tree: - Algorithm: -1) start at a node A -2) go to an adjacent unvisited vertex B: -a) mark it as visited -b) push it onto the stack -3) repeat step 2 until no more adjacent nodes to B remain unvisited -a) pop current node off stack -b) go to next adjacent unvisited vertex, and begin process again -4) if no more unvisited nodes in graph remain, search is done

DEPTH-FIRST (GRAPH) - Example: -Start at A: Visit Stack A B AB F ABF C ABFC BACKTRACK ABF H ABFH Backtrack ABF Backtrack AB Backtrack A D AD A BFH C D E GI

DEPTH-FIRST (cont.) Example (cont.) VisitStack EADE BacktrackAD GADG IADGI BacktrackADG BacktrackAD BacktrackA Backtrackempty A BFH C D E GI

DEPTH-FIRST (cont.) the algorithm can easily be written as a recursive function and by traversing the adjacency matrix/list for the graph from previous example: adjacency list: A-->B-->C-->D-->0 B-->F-->0 C-->B-->0 D-->E-->G-->0 E-->0 F-->C-->H  0 G-->I  0 H-->0 I-->0

DEPTH-FIRST (cont.) - Algorithm (adjacency list): start: mark all nodes as unvisited depthfirstsearch(A): for each vertex u in A's list mark it as visited depthfirstsearch(u) once all A's list has been exhausted, move to next UNVISITED vertex: depthfirstsearch(D) repeat until all nodes have been visited

DEPTH-FIRST (cont.) - Comments: -Disadv: -is possible that some nodes may still not get visited by depth-search (for example, start at B) -Adv: -good algorithm because it can yield lots of information about the structure of the graph

BREADTH-FIRST SEARCH (TREE) - Again consider: - we perform breadth-first by starting at a node, then looking at all children of that node: -Start 10: -Look Look at their children: -Look 2,7,11,14 -And their children: -Look 1, 17 -When no more children, search done -Since we track where we look first, a queue is used

BREADTH-FIRST (GRAPH) again, similar to that of a tree use a queue to store vertices as they are visited Algorithm Mark all vertices as UNVISITED 1) start at a vertex A 2) visit the next UNVISITED vertex that is adjacent to the current one a) Mark it as VISITED b) Insert it into the queue c) repeat 2 until no more unvisited vertices adjacent to current one 3) Remove next vertex from queue and make it the current vertex 4) Repeat step 2 until no more vertices are unvisited 5) If no unvisited vertices remain (queue is empty) you are done

Breath-First (Graph) Again consider: Start at A: Visit Queue A empty B (A->B) B C (A->C) BC D (A->D) BCD no more children of A: remove B CD F CDF no more children of B: remove C DF B already visited, no more children: remove D F A BFH C D E GI

BREADTH-FIRST (cont.) - Example (cont.) Visit Queue E FE G FEG remove F EG visit H EGH remove E GH remove G H I HI remove H I remove I queue empty no children of I Queue Empty - Search Done A BFH C D E GI

BREADTH-FIRST (discussion) - Implementation: -can easily implement this algorithm with an adjacency list by simply searching the linked list for each node in the list (how would you write it?) - Comments: -again, may be situations where not all vertices are searched, depending on where you start (i.e, starting again at B) -still very good algorithm to use due to its ease of implementation

QUESTIONS? MORE GRAPHS P