Data Structures and Algorithms Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 12 Application of Stack and Queues Application of Stack and.

Slides:



Advertisements
Similar presentations
Chapter 1: INTRODUCTION TO DATA STRUCTURE
Advertisements

AI Pathfinding Representing the Search Space
Lecture 24 MAS 714 Hartmut Klauck
Lecture 19: Parallel Algorithms
Recursion CS 367 – Introduction to Data Structures.
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
The N-Queens Problem lab01.
CS 61B Data Structures and Programming Methodology July 31, 2008 David Sun.
Search: Representation and General Search Procedure Jim Little UBC CS 322 – Search 1 September 10, 2014 Textbook § 3.0 –
Backtracking What is backtracking?
Introduction to Computability Theory
Search: Representation and General Search Procedure CPSC 322 – Search 1 January 12, 2011 Textbook § 3.0 –
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
1 Lecture 25: Parallel Algorithms II Topics: matrix, graph, and sort algorithms Tuesday presentations:  Each group: 10 minutes  Describe the problem,
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
General Computer Science for Engineers CISC 106 Lecture 25 Dr. John Cavazos Computer and Information Sciences 04/20/2009.
1 Parallel Algorithms III Topics: graph and sort algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Murali Sitaraman
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
1 State Space of a Problem Lecture 03 ITS033 – Programming & Algorithms Asst. Prof.
Graph Theory Topics to be covered:
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
CS 312: Algorithm Analysis Lecture #32: Intro. to State-Space Search This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
Lecture 7 All-Pairs Shortest Paths. All-Pairs Shortest Paths.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Seminar on random walks on graphs Lecture No. 2 Mille Gandelsman,
Optimal Path Planning Using the Minimum-Time Criterion by James Bobrow Guha Jayachandran April 29, 2002.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Search in State Spaces Problem solving as search Search consists of –state space –operators –start state –goal states A Search Tree is an efficient way.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
State space search Represented by a four-tuple [N,A,S,GD], where: N is the problem space A is the set of arcs (or links) between nodes. These correspond.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
TU/e Algorithms (2IL15) – Lecture 3 1 DYNAMIC PROGRAMMING
Simulation Examples And General Principles Part 2
Lecture 18 March 29, 2011 Formal Methods 3 CS 315 Spring Adapted from slides provided by Jason Hallstrom and Murali Sitaraman (Clemson)
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
CSG3F3/ Desain dan Analisis Algoritma
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Matrices Rules & Operations.
CSCI 104 Backtracking Search
Java Software Structures: John Lewis & Joseph Chase
Using a Stack Chapter 6 introduces the stack data type.
Recursion Copyright (c) Pearson All rights reserved.
Data Structures and Algorithms for Information Processing
Chapter 12 Recursion (methods calling themselves)
Enumerating Distances Using Spanners of Bounded Degree
Graphs Representation, BFS, DFS
Lecture 7 All-Pairs Shortest Paths
Analysis and design of algorithm
Using a Stack Chapter 6 introduces the stack data type.
Chapter 22: Elementary Graph Algorithms I
Outline This topic covers Prim’s algorithm:
Using a Stack Chapter 6 introduces the stack data type.
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
Using a Stack Chapter 6 introduces the stack data type.
Presentation transcript:

Data Structures and Algorithms Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 12 Application of Stack and Queues Application of Stack and Queues Shortest route in a grid with obstacles 8 queen’s problem Lecture 12 Application of Stack and Queues Application of Stack and Queues Shortest route in a grid with obstacles 8 queen’s problem 1

Problem 1 Shortest route in a grid with obstacles 2

Shortest route in a grid From a cell in the grid, we can move to any of its neighboring cell in one step. From top left corner, find shortest route to each cell avoiding obstacles. 3 3 The input grid is given as a Boolean matrix G such that G[i,j] = 0 if (i,j) is an obstacle, and 1 otherwise.

Step 1: Realizing the nontriviality of the problem 4

5 Shortest route in a grid nontriviality of the problem Definition: Distance of a cell c from another cell c’ is the length (number of steps) of the shortest route between c and c’. We shall design algorithm for computing distance of each cell from the start-cell. As an exercise, you should extend it to a data structure for retrieving shortest route.

Get inspiration from nature 6 Did you ever notice the way ripples on the surface of water travel in the presence of obstacles ? The ripples travels along the shortest route ?

Shortest route in a grid nontriviality of the problem 7 Create a ripple at the start cell and trace the path it takes to How to find the shortest route to in the grid ?

Shortest route in a grid propagation of a ripple from the start cell 8

Shortest route in a grid ripple reaches cells at distance 1 after step 1 9

Shortest route in a grid ripple reaches cells at distance 2 after step 2 10

Shortest route in a grid ripple reaches cells at distance 3 after step 3 11

Shortest route in a grid ripple reaches cells at distance 8 after step 8 12 Watch the next few slides carefully.

Shortest route in a grid ripple reaches cells at distance 9 after step 9 13

Shortest route in a grid ripple reaches cells at distance 10 after step 10 14

Shortest route in a grid ripple reaches cells at distance 11 after step 11 15

Shortest route in a grid ripple reaches cells at distance 12 after step 12 16

Shortest route in a grid ripple reaches cells at distance 13 after step 13 17

Shortest route in a grid ripple reaches cells at distance 14 after step 14 18

Shortest route in a grid ripple reaches cells at distance 15 after step 15 19

Step 2: Designing algorithm for distances in grid (using an insight into propagation of ripple) 20

Shortest route in a grid A snapshot of ripple after i steps 21

Shortest route in a grid A snapshot of the ripple after i+1 steps 22

23 ∞

24

The first (not so elegant) algorithm (to compute distance to all cells in the grid) 25

How to transform the algorithm to an elegant algorithm ? Key points we have observed: We can compute cells at distance i+1 if we know all cells up to distance i. Therefore, we need a mechanism to enumerate the cells of the grid in non-decreasing order of distances from the start cell. 26 How to design such a mechanism ?

Keep a queue Q 27 Q

An elegant algorithm (to compute distance to all cells in the grid) 28 Not IsEmptyQueue(Q) Distance(c) +1 Enqueue(b, Q);

Proof of correctness of algorithm Question: What is to be proved ? Answer: At the end of the algorithm, Distance[c]= the distance of cell c from the starting cell in the grid. Question: How to prove ? Answer: By the principle of mathematical induction on the distance from the starting cell. Inductive assertion: P(i): The algorithm correctly computes distance to all vertices at distance i from the starting cell. As an exercise, try to prove P(i) by induction on i. 29

Problem 2 Placing 8 queens safely on a chess board 30 It was explained on the black board. However, for a better understanding, the slides are also provided here.

8 queen problem Find a way to place 8 queens on a chess board so that no two of them attack each other. 31

First some notations and definitions are provided in order to have a better insight into the problem. to describe the idea underlying the algorithm compactly. to describe the algorithm in a formal manner. 32

A Configuration of 8 queens on chess board where each row has exactly one queen 33 Q Q Q Q Q Q Q Q

Configuration of 8 queens on chess board where each row has exactly one queen 34 <1,1,3,4,6,4,8,7><1,1,3,4,6,4,8,7> Most significant digit Least significant digit

To compute a safe configuration of 8 queens 35

A clever approach to search for safe configuration An overview Place queen of the 1 st row in 1 st column. It is unsafe to place second queen at 1 st column or 2 nd column of 2 nd row.  No need to generate configurations or. So we proceed with. All configurations with are unsafe for k<5.  No need to generate these unsafe configurations. So we proceed with. … and so on … Q Q Q

A clever approach to search for safe configuration An overview of general step safe Because we are enumerating configurations in lexicographic order

Implementation of the clever approach An important terminology: Given a partial configuration of i queens placed on first i rows of the chess, A queen is said to be safe if it is not attacked by any other queen lying in any row below it. A queen is said to be unsafe if it is attacked by at least one queen lying in any row below it. A queen is uncertain if it is not known yet whether it is safe or unsafe. Representation of a partial configuration: Each queen in a partial configuration will be specified by a triplet, where r and c are the row and column of the cell where the queen is placed. Flag is a variable which takes one of the values from {safe, unsafe, uncertain } as explained above. 38

Implementation of the clever approach A snapshot of the algorithm 39

Implementation of the clever approach A snapshot of the algorithm 40 safe safe/ Unsafe/ uncertain stack S For queens

A single step of the algorithm 41 We determine if it is attacked by any existing queen and change its status as safe or unsafe accordingly, and push it back into stack.

Finally, The following slide has a simple and elegant implementation of the clever approach we discussed… 42

An elegant algorithm for 8 queens problem CreateEmptyStack(S); Push((1,1,safe), S); While (Not IsEmptyStack(S)) do { (r,c,flag)  Top(S); Pop(S); 3 Cases: { Flag is uncertain : If ((r,c) does not attack any existing queen ) Push((r,c,safe), S); else Push((r,c,unsafe), S); Flag is safe : If (r = 8) { // we reached a safe configuration of 8 queens print the solution and empty the stack S; } else { Push((r,c,safe), S); Push((r+1,1,uncertain), S)}; Flag is unsafe : If (c = 8) { (r’,c’,flag’)  Top(S); Pop(S); Push((r’,c’,unsafe), S); } else Push((r,c+1, uncertain), S); } 43

Homework exercises 1.Now that you know the clever algorithm, give reason for pursuing lexicographic approach in the search of a safe configuration by it. 2.Extend the algorithm for computing a safe configuration for n queens on n by n chess board for any n. 3.The current algorithm finds just one safe configuration. Modify it to enumerate all safe configurations. 4.If you love programming, implement the clever algorithm and trivial algorithm and see if the clever algorithm is indeed mush faster than the trivial algorithm. 5.We have given an iterative implementation of the clever algorithm. Can you design a recursive implementation as well ? Which of them will be faster in practical implementation, and why ? 44