Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Knowledge Representation
Advertisements

Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
CS16: Introduction to Data Structures & Algorithms
Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
ABC Technology Project
Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
Chapter 5 Plan-Space Planning.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
1 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I.
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
© 2012 National Heart Foundation of Australia. Slide 2.
25 seconds left…...
When do I need an invariant? CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
We will resume in: 25 Minutes.
CS203 Lecture 15.
Binary Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Introduction to Recursion and Recursive Algorithms
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
CPSC 322, Lecture 5Slide 1 Uninformed Search Computer Science cpsc322, Lecture 5 (Textbook Chpt 3.5) Sept, 14, 2012.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Reviewing your Program CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4 © Mitchell Wand, This work is licensed under a Creative Commons.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Foldr and Foldl CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
Introduction to Invariants
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
Case Study: Undefined Variables
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
Rewriting your function using map and foldr
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
Presentation transcript:

Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License 1

Introduction In this lesson, we'll return to the problem of searching in a graph. When we're searching for all the nodes reachable from a given node, the order in which we search doesn't matter– we have to search everything anyway. But if we're searching for a specific node or set of nodes, then the order may make a big difference in running time. 2

Outline In this lesson, we'll write two variations on searching in a graph: – depth-first search – breadth-first search We'll see how the invariants help us keep track of the different variables in our calls. 3

We'll start with path? from reachability.rkt (define (path? graph src tgt) (local ((define (reachable-from? newest nodes) ;; RETURNS: true iff there is a path from src to tgt in graph ;; INVARIANT: newest is a subset of nodes ;; AND: ;; (there is a path from src to tgt in graph) ;; iff (there is a path from newest to tgt) ;; STRATEGY: general recursion ;; HALTING MEASURE: the number of graph nodes _not_ in 'nodes' (cond [(member tgt newest) true] [else (local ((define candidates (set-diff (all-successors newest graph) nodes))) (cond [(empty? candidates) false] [else (reachable-from? candidates (append candidates nodes))]))]))) (reachable-from? (list src) (list src)))) 4

Our first step is to break out the inner function This makes the inner function a little less scary We will have to pass more arguments, so the purpose statement will get a little larger. But don't worry, we haven't really changed anything. 5

Contract and Purpose Statement ;; ListOfNodes ListOfNodes Node Graph -> Boolean ;; GIVEN: ;; 1. The list 'nodes' of all the nodes we've seen ;; 2. The list of nodes whose successors we haven't taken ;; 3. The target node 'tgt' that we are trying to reach ;; 4. The graph we are searching ;; RETURNS: Is tgt reachable from any of the nodes in ;; 'nodes'? ;; INVARIANT: newest is a subset of nodes ;; AND: ;; (there is a path from src to tgt in graph) ;; iff (there is a path from newest to tgt) ;; HALTING MEASURE: the number of graph nodes _not_ in ;; 'nodes' 6

reachable-from? (define (reachable-from? newest nodes tgt graph) (cond [(member tgt newest) true] [else (local ((define candidates (set-diff (all-successors newest graph) nodes))) (cond [(empty? candidates) false] [else (reachable-from? candidates (append candidates nodes) tgt graph)]))])) 7

Defining path? in terms of reachable- from? ;; Strategy: Function Combination (define (path?.v1 graph src tgt) (reachable-from? (list src) (list src) tgt graph)) 8

Refining reachable-from? In order to control the order in which nodes are explored, we'll stop using (all-successors newest) and take the successors of each node in newest one at a time. We'll call our new function reachable-from- dfs?. (We'll explain the name later) What are the possibilities? 9

Possibilities #1-2 tgt is already in newest. In that case we've found the node that we're looking for, and the answer is true. newest is empty. In that case, there are no nodes left to explore, so the answer must be false. 10

What else could happen? Otherwise, we'll let candidates be the successors of (first newest) that are not already in nodes. (set-diff (successors (first newest) graph) nodes) This guarantees that none of the nodes in candidates are already in nodes. And since newest is a subset of nodes, it means that none of the nodes in candidates are in newest, either. Now what? 11

Possibility #3 candidates is empty – in that case, we know that tgt is not reachable from (first newest). – so if tgt is reachable from newest, it must be reachable from (rest newest). So we add a cond-line that says [(empty? candidates) (reachable-from-dfs? (rest newest) nodes tgt graph)] 12

Possibility #4 candidates is non-empty. So we need to add candidates to our list newest of nodes to explore. We also need to remove (first newest), since we've explored it. We also need to add candidates to nodes, in order to maintain the invariant that newest is a subset of nodes. 13

Possibility #4 (cont'd) So our cond line will be: [else (reachable-from-dfs? (append candidates (rest newest)) (append candidates nodes) tgt graph)] Get the next value of newest by removing (first newest) and adding candidates. Add candidates to nodes to maintain the invariant 14

Top Level (define (path-dfs? graph src tgt) (reachable-from-dfs? (list src) (list src) tgt graph)) Mini-exercise: Convince yourself that this call to reachable-from-dfs? satisfies its invariant. 15

Why did we call this dfs? We add the newly-discovered nodes candidates to the front of the list of nodes to be explored. So the nodes that we just discovered get explored first. This is called depth-first search. If you don't remember depth-first search from your undergraduate data structures or algorithms class, go look it up now. 16

Let's see this in action Here is a tree, with the nodes numbered in the order this function will discover them

Alas, this is only “almost” DFS The order in which this algorithm finds the nodes Real depth-first search would find the node labelled 3 here as the left son of 2, not as the third son of 1. See 09-3a-reachability.rkt, which contains a detailed discussion of dfs.

Breadth-First search The other possibility is to put the new nodes at the END of the worklist (the list newest) This explores nodes strictly in the order of their distance from the starting nodes. This is called breadth-first search. 19

reachable-from-bfs? (define (reachable-from-bfs? newest nodes tgt graph) (cond [(member tgt newest) true] [(empty? newest) false] [else (local ((define candidates (set-diff (successors (first newest) graph) nodes))) (cond [(empty? candidates) (reachable-from-dfs? (rest newest) nodes tgt graph)] [else (reachable-from-dfs? (append (rest newest) candidates) (append candidates nodes) tgt graph)]))])) Only difference: put the candidates at the END of the list to be explored 20

The same tree, in bfs order

What if there were cycles? This edge creates a cycle NO PROBLEM: When we discover 2 in (successors 6), it will already be in nodes, so it will be filtered out by the set-diff. The halting measure assures us that the number of nodes not in nodes is strictly decreasing, so there can't possibly be an infinite loop. 22

Choosing between bfs and dfs If you know that the solution is close to the root, then bfs is better. If your tree is very broad, then maybe dfs is better. Go look at an algorithms book for more examples. 23

Variations If you knew more about your problem, you could call a function to choose the order in which to explore your nodes So you might write something like: (reachable-from-bfs? (reorder-candidates (rest newest) candidates) (append candidates nodes) tgt graph) 24

Variations (2) If you had a set of targets instead of a single target, you could return the target you actually found. Or you could keep track not just of the nodes you've reached, but the path you took to get to each of them. 25

Summary We've written two variations on searching in a graph: – depth-first search – breadth-first search We've seen how the invariants help us keep track of the different variables in our calls. 26

Next Steps If you have questions about this lesson, ask them on the Discussion Board Study: – 09-3-paths.rkt, which contains the code in this slide deck – 09-3a-reachbility.rkt, which contains a better treatment, including real dfs. Do Guided Practice 9.1 Go on to the next lesson 27