Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms.

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Advertisements

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.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
Breadth-First Search Text Read Weiss, § 9.3 (pp ) Breadth-First Search Algorithms.
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
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 8, Part I Graph Algorithms.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Chapter 5 Trees PROPERTIES OF TREES 3 4.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graph Traversals Depth-First Traversal. The Algorithm.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
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,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
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.
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 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.
Breadth-first and depth-first traversal CS1114
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
Best-first search is a search algorithm which explores a graph by expanding the most promising node chosen according to a specified rule.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
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.
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 2720 Instructor: Zhuojun Duan
CS120 Graphs.
Comp 245 Data Structures Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs Representation, BFS, DFS
Graphs.
Graphs Part 2 Adjacency Matrix
Depth-First Search Graph Traversals Depth-First Search DFS.
Binary Tree Traversals
COMP171 Depth-First Search.
Depth-First Search CSE 2011 Winter April 2019.
Analysis and design of algorithm
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:

Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms

Requirements Also called Depth-First Search Can be used to attempt to visit all nodes of a graph in a systematic manner Works with directed and undirected graphs Works with weighted and unweighted graphs

A H B F E D C G Walk-Through Visited Array A B C D E F G H Task: Conduct a depth-first search of the graph starting with node D

A H B F E D C G Walk-Through Visited Array A B C D √ E F G H Visit D D The order nodes are visited: D

A H B F E D C G Walk-Through Visited Array A B C D √ E F G H Consider nodes adjacent to D, decide to visit C first (Rule: visit adjacent nodes in alphabetical order) D The order nodes are visited: D

A H B F E D C G Walk-Through Visited Array A B C √ D √ E F G H Visit C CDCD The order nodes are visited: D, C

A H B F E D C G Walk-Through Visited Array A B C √ D √ E F G H No nodes adjacent to C; cannot continue  backtrack, i.e., pop stack and restore previous state CDCD The order nodes are visited: D, C

A H B F E D C G Walk-Through Visited Array A B C √ D √ E F G H Back to D – C has been visited, decide to visit E next D The order nodes are visited: D, C

A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G H Back to D – C has been visited, decide to visit E next EDED The order nodes are visited: D, C, E

A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G H Only G is adjacent to E EDED The order nodes are visited: D, C, E

A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G √ H Visit G GEDGED The order nodes are visited: D, C, E, G

A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G √ H Nodes D and H are adjacent to G. D has already been visited. Decide to visit H. GEDGED The order nodes are visited: D, C, E, G

A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G √ H √ Visit H HGEDHGED The order nodes are visited: D, C, E, G, H

A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G √ H √ Nodes A and B are adjacent to F. Decide to visit A next. HGEDHGED The order nodes are visited: D, C, E, G, H

A H B F E D C G Walk-Through Visited Array A √ B C √ D √ E √ F G √ H √ Visit A AHGEDAHGED The order nodes are visited: D, C, E, G, H, A

A H B F E D C G Walk-Through Visited Array A √ B C √ D √ E √ F G √ H √ Only Node B is adjacent to A. Decide to visit B next. AHGEDAHGED The order nodes are visited: D, C, E, G, H, A

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ Visit B BAHGEDBAHGED The order nodes are visited: D, C, E, G, H, A, B

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to B. Backtrack (pop the stack). AHGEDAHGED The order nodes are visited: D, C, E, G, H, A, B

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to A. Backtrack (pop the stack). HGEDHGED The order nodes are visited: D, C, E, G, H, A, B

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to H. Backtrack (pop the stack). GEDGED The order nodes are visited: D, C, E, G, H, A, B

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to G. Backtrack (pop the stack). EDED The order nodes are visited: D, C, E, G, H, A, B

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to E. Backtrack (pop the stack). D The order nodes are visited: D, C, E, G, H, A, B

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ F is unvisited and is adjacent to D. Decide to visit F next. D The order nodes are visited: D, C, E, G, H, A, B

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F √ G √ H √ Visit F FDFD The order nodes are visited: D, C, E, G, H, A, B, F

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F √ G √ H √ No unvisited nodes adjacent to F. Backtrack. D The order nodes are visited: D, C, E, G, H, A, B, F

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F √ G √ H √ No unvisited nodes adjacent to D. Backtrack. The order nodes are visited: D, C, E, G, H, A, B, F

A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F √ G √ H √ Stack is empty. Depth-first traversal is done. The order nodes are visited: D, C, E, G, H, A, B, F

Consider Trees 1.What depth-first traversals do you know? 2.How do the traversals differ? 3.In the walk-through, we visited a node just as we pushed the node onto the stack. Is there another time at which you can visit the node? 4.Conduct a depth-first search of the same graph using the strategy you came up with in #3.