csci 210: Data Structures Stacks and Queues in Solution Searching

Similar documents
Definitions. You can represent a point by a dot and name it by a capital letter.

Lecture 6: Recursion RECURSION

Euclid. Father of Geometry Euclidean Geometry Euclid s Elements

RECURSION (CONTINUED)

csci 210: Data Structures Stacks and Queues

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 616 MIDTERM WINTER 2017

Announcements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements.

RECURSION (CONTINUED)

Using Recursion Goodrich, Tamassia. Using Recursion 1

MST & Shortest Path -Prim s -Djikstra s

CS213d Data Structures and Algorithms

Data Structures and Algorithms

CS 2230 CS II: Data structures. Limits of comparison sorting, beyond comparison sorting Brandon Myers University of Iowa

CSCI 220: Computer Architecture I Instructor: Pranava K. Jha. Simplification of Boolean Functions using a Karnaugh Map

Data Structures and Algorithms " Recursion!

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

Recursion. The Recursion Pattern. Recursion 3/16/14

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 14 Component-I (A) - Personal Details

Discrete Mathematics Introduction

Geometry Ch 4 Practice Exam

1. For each part (a) through (d) below, state which of the three triangles, if any, are similar and why. a.

CIS-331 Exam 2 Fall 2015 Total of 105 Points Version 1

CIS-331 Fall 2014 Exam 1 Name: Total of 109 Points Version 1

CS302 - Data Structures using C++

csci 210: Data Structures Graph Traversals

IT 201 Digital System Design Module II Notes

Geometry 1-1. Non-collinear Points not on the same line. Need at least 3 points to be non-collinear since two points are always collinear

Solving Problems by Searching

GEOMETRY BASIC GEOMETRICAL IDEAS. 3) A point has no dimensions (length, breadth or thickness).

Math-2 Lesson 8-6 Unit 5 review -midpoint, -distance, -angles, -Parallel lines, -triangle congruence -triangle similarity -properties of

Life is what you make it. Mr. H s dad

Graphs: Graph Data Structure:

CIS-331 Fall 2013 Exam 1 Name: Total of 120 Points Version 1

Transactions in Euclidean Geometry

ARML Practice Problems Arvind Thiagarajan, May 7, 2006

4. Specifications and Additional Information

2. In general, dilations do not preserve distance so they are not rigid transformations. Dilations cause the size of the shape to change.

Lexical Analysis - 1. A. Overview A.a) Role of Lexical Analyzer

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Vocabulary Point- Line- Plane- Ray- Line segment- Congruent-

Motivation of Sorting

Algorithm Design and Recursion. Search and Sort Algorithms

Your friends the Trees. Data Structures

PLANE GEOMETRY SKILL BUILDER ELEVEN

4 Triangles and Congruence

Uninformed Search strategies. AIMA sections 3.4,3.5

Day 6: Triangle Congruence, Correspondence and Styles of Proof HOMEWORK

Problem Solving Agents

For all questions, E. NOTA means none of the above answers is correct. Diagrams are NOT drawn to scale.

Lecture 25 Spanning Trees

Lesson 23: Base Angles of Isosceles Triangles Day 1

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

CS Data Structures and Algorithm Analysis

Proving Triangles and Quadrilaterals Satisfy Transformational Definitions

DE to a line parallel to Therefore

CIS-331 Final Exam Spring 2016 Total of 120 Points. Version 1

GRAPHS Lecture 17 CS2110 Spring 2014

Finite Math - J-term Homework. Section Inverse of a Square Matrix

CSE 373 Lecture 19: Wrap-Up of Sorting

Introduction to Algorithms

Goal-Based Agents Problem solving as search. Outline

DATA MINING II - 1DL460

CSPs: Search and Arc Consistency

Complementary, Supplementary, & Vertical Angles

CIS-331 Exam 2 Fall 2014 Total of 105 Points. Version 1

How to find a minimum spanning tree

Fundamentals of Cryptography

1a. [4 marks] A surveyor has to calculate the area of a triangular piece of land, DCE.

Lecture P6: Recursion

Lesson 3.6 Overlapping Triangles

Congruence. CK-12 Kaitlyn Spong. Say Thanks to the Authors Click (No sign in required)

csci 210: Data Structures Graph Traversals

DKT 122/3 DIGITAL SYSTEM 1

Algorithms: Lecture 10. Chalmers University of Technology

Transactions in Euclidean Geometry

Compiler Construction

UAB MATH TALENT SEARCH

WYSE Academic Challenge Regional Computer Science 2008 Solution Set

no triangle can have more than one right angle or obtuse angle.

CS 112 Final May 8, 2008 (Lightly edited for 2011 Practice) Name: BU ID: Instructions GOOD LUCK!

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

CHAPTER TWO LANGUAGES. Dr Zalmiyah Zakaria

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Search. CS 3793/5233 Artificial Intelligence Search 1

9/7/2015. Outline for today s lecture. Problem Solving Agents & Problem Formulation. Task environments

CSCI 237 Sample Final Exam

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Unit 8: Similarity Analysis

Math 308, Section 101 Solutions to Study Questions for Final Exam (Thursday, December 16, 2004)

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

NCSS Statistical Software. Design Generator

Sets 1. The things in a set are called the elements of it. If x is an element of the set S, we say

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

Theory of Computations III. WMU CS-6800 Spring -2014

COMP 182 Algorithmic Thinking. Breadth-first Search. Luay Nakhleh Computer Science Rice University

Uninformed Search. Day 1 & 2 of Search. Russel & Norvig Chap. 3. Material in part from

1. Each interior angle of a polygon is 135. How many sides does it have? askiitians

not to be republishe NCERT CHAPTER 8 QUADRILATERALS 8.1 Introduction

Transcription:

csci 210: Data Structures Stacks and Queues in Solution Searching 1

Summary Topics Using Stacks and Queues in searching Applications: In-class problem: missionary and cannibals In-class problem: finding way out of a maze Searching a solution space: Depth-first and breadth-first search (DFS, BFS) READING: GT textbook chapter 5 2

Searching in a Solution Space Remember the problems: Permutations: Write a function to print all permutations of a given string. Subsets: Write a function to enumerate all subsets of a given string Subset sum: Given an array of numbers and a target value, find whether there exists a subset of those numbers that sum up to the target value. We saw how to solve them recursively. Idea: A recursive solution takes as parameters the partial solution so far. Given this partial solution, it finds all possible ways to build new solutions. 3

Recursive Permute void recpermute(string sofar, String remaining) { } //base case if (remaining.length() == 0) else { } System.out.println(soFar); for (int i=0; i< remaining.length(); i++) { } String nextsofar = sofar + remaining[i]; String nextremaining = remaining.substring(0,i) + remaining.substring(i+1); recpermute(nextsofar, nextremaining) 4

Tree of recursive calls, abc a, bc b, ac c, ab ab, c ac, b ba, c bc, a ca, b cb, a abc acb bac bca cab cba 5

Searching in a Solution Space Permutations: Write a function to print all permutations of a given string. Subsets: Write a function to enumerate all subsets of a given string Subset sum: Given an array of numbers and a target value, find whether there exists a subset of those numbers that sum up to the target value. We saw how to solve them recursively. Idea: A recursive solution takes as parameters the partial solution so far. Given this partial solution, it finds all possible ways to build new solutions. Another way to look at it: let S = the set of all possible partial solutions so far. e.g. S = {a, b, c, d } //all possible partial solutions of one letter for each partial solution p in S move one step forward and find all possible next solutions from p. Add all these to a new set S. e.g. partial solution p = a gives 3 new solutions: ab, ac, ad repeat with S = S e.g. S = {ab, ac, ad, ba, bc, bd, ca, cb, cd, da, db, dc} 6

Permutations Recursive permute: recpermute(sofar, remaining) the function knows about the current partial solution the system keeps track of the active calls---the tree of recursive calls corresponds to all partial solutions, abc S = { } a, bc b, ac c, ab S = {a, b, c} ab, c ac, b ba, c bc, a ca, b cb, a S = {ab, ac, bc, ba, cb, ca} abc acb bac bca cab cba S = {abc, acb, bca, bac, cba, cab} Non-recursive permute construct explicitly the set of partial solutions 7

Building a Solution Imagine that we encode the partial solution to a problem in some way for e.g. for permutations a partial solution could be a tuple s = <sofar, remaining> //S denotes the set of partial solutions S = empty set //create the initial state S = { initial-state} while S is not empty S = {} go through all partial solution s from S for each s generate all possible next solutions from s and add them to S S = S Think of S as the (partial) solution space. Our algorithm will construct it. 8

Building a Solution We do not need both S and S Think of S as the (partial) solution space. Our algorithm will construct it. S = empty set //create the initial state S = { initial-state} while S is not empty delete the next partial solution s from S generate all possible next solutions from s and add them to S 9

The solution space Each solution is a state Each solution generates new solutions, abc a, bc b, ac c, ab ab, c ac, b ba, c bc, a ca, b cb, a abc acb bac bca cab cba 10

Building a Solution Think of S as the solution space. Our algorithm will construct it. S = empty set //create the initial state S = { initial-state} while S is not empty delete the next partial solution s from S generate all possible next solutions from s and add them to S S is a set of states. How to store S? Keep S as a queue delete next solution from the front add new solutions to the end of queue Keep S as a stack delete next solution from the top add new solutions to the top 11

S = empty set //create the initial state S = { initial-state} while S is not empty delete the next partial solution s from S generate all possible next solutions from s and add them to S S as a queue S = { <, abc >} partial solution s = <, abc> generates 3 new solutions <a, bc>, <b, ac>, <c, ab> they are all put in S: S = {<a, bc>, <b, ac>, <c, ab>} partial solution s=<a,bc> generates 2 new solutions <ab,c> and <ac,b>; they are put in S S = { <b, ac>, <c, ab>, <ab,c>, <ac,b>} S = { <c, ab>, <ab,c>, <ac,b>, <ba,c>, <bc, a>} S = { <ab,c>, <ac,b>, <ba,c>, <bc, a>, <ca,b>, <cb, a>}... S = { <abc, >, <acb, >, <bac, >, <bca, >, <cab, >, <cba, >} S = {} 12

The solution space How does the algorithm traverse and construct the solution space when S is a queue?, abc a, bc b, ac c, ab ab, c ac, b ba, c bc, a ca, b cb, a abc acb bac bca cab cba 13

S = empty set //create the initial state S = { initial-state} while S is not empty delete the next partial solution s from S generate all possible next solutions from s and add them to S S as a stack S = { <, abc >} partial solution s = <, abc> generates 3 new solutions <a, bc>, <b, ac>, <c, ab> they are all put in S: S = {<c, ab>, <b,ac>, <a,bc>} partial solution s=<c,ab> generates 2 new solutions <ca,b> and <cb,a>; they are put in S S = {<cb, a>, <ca,b>, <b,ac>, <a,bc>}... 14

The solution space How does the algorithm traverse and construct the solution space when S is a stack?, abc a, bc b, ac c, ab ab, c ac, b ba, c bc, a ca, b cb, a abc acb bac bca cab cba 15

The solution space Using a stack mimics recursion <----- goes depth first depth-first search (DFS) Using a queue goes level by level <----- goes breadth first breadth-first search (BFS), abc a, bc b, ac c, ab ab, c ac, b ba, c bc, a ca, b cb, a abc acb bac bca cab cba 16

Example: The missionary and cannibal problem You have 3 missionaries, 3 cannibals and a boat sitting on, say, the left side of a river. They all need to cross to the other side. Find a set of moves that brings all 6 people on the other side safely. The boat can take at most two people at a time (and at least one). Anybody can row If at any point there are more cannibals than missionaries, the missionaries get eaten. 17

Missionaries and Cannibals We want to frame it as a search in a solution space and use the previous skeleton How to encode a state? write a class MCState What s the initial state? What s the final state? write MCState:isFinal() When is a state valid? write MCState:isValid() Given a state, what are the moves you can make? What will the set S contain? 18

Missionaries and Cannibals Queue<MCState> s = new Queue<MCState>(); //add initial state s.insert(newmcstate(3,3,0,0,1)); while (!s.isempty())) { MCState crt = s.delete(); if (crt.isfinal()) { //this is the goal state; break;} //generate all possible next states and call s.insert() to add them to s... } //crt must be the final state; print it Are there duplicate states in S? Can a state be inserted in S several times? (This would correspond to a loop --- we go back to a state that we already explored). Why is this not a problem? The skeleton above uses a Queue for S. Would a Stack work? Why (not)? 19