§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the.

Slides:



Advertisements
Similar presentations
Dynamic Programming Introduction Prof. Muhammad Saeed.
Advertisements

Algorithm Design Methodologies Divide & Conquer Dynamic Programming Backtracking.
Traveling Salesperson Problem
Adversarial Search We have experience in search where we assume that we are the only intelligent being and we have explicit control over the “world”. Lets.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
Inexact Matching of Strings General Problem –Input Strings S and T –Questions How distant is S from T? How similar is S to T? Solution Technique –Dynamic.
Dynamic Programming Lets begin by looking at the Fibonacci sequence.
Backtracking What is backtracking?
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
1 Pseudo-polynomial time algorithm (The concept and the terminology are important) Partition Problem: Input: Finite set A=(a 1, a 2, …, a n } and a size.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dynamic Programming Optimization Problems Dynamic Programming Paradigm
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Pseudo-polynomial time algorithm (The concept and the terminology are important) Partition Problem: Input: Finite set A=(a1, a2, …, an} and a size s(a)
Ch 13 – Backtracking + Branch-and-Bound
Dynamic Programming1 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
Daniel Kroening and Ofer Strichman Decision Procedures An Algorithmic Point of View Deciding ILPs with Branch & Bound ILP References: ‘Integer Programming’
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Decision Procedures An Algorithmic Point of View
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
CS 5243: Algorithms Dynamic Programming Dynamic Programming is applicable when sub-problems are dependent! In the case of Divide and Conquer they are.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter Dynamic Programming Objectives: Present the Dynamic Programming paradigm.
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.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Dynamic Programming Louis Siu What is Dynamic Programming (DP)? Not a single algorithm A technique for speeding up algorithms (making use of.
1 Dynamic Programming Andreas Klappenecker [partially based on slides by Prof. Welch]
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Computer Sciences Department1.  Property 1: each node can have up to two successor nodes (children)  The predecessor node of a node is called its.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
CSCE350 Algorithms and Data Structure Lecture 21 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Searching a Linear Subspace Lecture VI. Deriving Subspaces There are several ways to derive the nullspace matrix (or kernel matrix). ◦ The methodology.
An Analysis of the n- Queens problem Saleem Karamali.
1 Chapter 15-2: Dynamic Programming II. 2 Matrix Multiplication Let A be a matrix of dimension p x q and B be a matrix of dimension q x r Then, if we.
CSG3F3/ Desain dan Analisis Algoritma
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Chapter 8 Dynamic Programming
Design and Analysis of Algorithm
Unit-5 Dynamic Programming
Merge Sort 1/12/2019 5:31 PM Dynamic Programming Dynamic Programming.
Dynamic Programming 1/15/2019 8:22 PM Dynamic Programming.
Dynamic Programming Dynamic Programming 1/15/ :41 PM
Dynamic Programming Dynamic Programming 1/18/ :45 AM
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Lecture 8. Paradigm #6 Dynamic Programming
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
Dynamic Programming Steps.
Presentation transcript:

§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the examination of all or some of the candidates, declare the identified answer. Suuuure — if the list is finite and it is possible to identify the answer following the examinations. AND, if there are not too many candidates. Backtracking enables us to eliminate the explicit examination of a large subset of the candidates while still guaranteeing that the answer will be found if the algorithm is run to termination. The basic idea is that suppose we have a partial solution ( x 1,..., x i ) where each x k  S k for 1  k  i < n. First we add x i+1  S i+1 and check if ( x 1,..., x i, x i+1 ) satisfies the constrains. If the answer is “yes” we continue to add the next x, else we delete x i and backtrack to the previous partial solution ( x 1,..., x i  1 ). pruning 1/13

§5 Backtracking Algorithms Find a placement of 8 queens on an 8  8 chessboard such that no two queens attack. Two queens are said to attack iff they are in the same row, column, diagonal, or antidiagonal of the chessboard Q Q Q Q Q Q Q Q Q i ::= queen in the i-th row x i ::= the column index in which Q i is Solution = ( x 1, x 2,..., x 8 ) = ( 4, 6, 8, 2, 7, 1, 3, 5 ) Constrains:  S i = { 1,2,3,4,5,6,7,8 } for 1  i  8 This implies 8 8 candidates in the solution space.  x i  x j if i  j  ( x i  x j ) / (i  j )   1 This implies that the solution must be a permutation of 1, 2,..., 8. Thus the number of candidates in the solution space is reduced to 8!. For the problem with n queens, there are n! candidates in the solution space. 1. Eight Queens 2/13

§5 Backtracking Algorithms Method: Take the problem of 4 queens as an example Step 1: Construct a game tree x1 = 1234 x2 = x3 = x4 = ! leaves Each path from the root to a leaf defines an element of the solution space. 3/13

§5 Backtracking Algorithms x1 = 1234 x2 = x3 = x4 = Step 2: Perform a depth-first search ( post-order traversal ) to examine the paths Q QQ QQ Q Q Q Q Q QQQ Q Q ( 2, 4, 1, 3 ) Note: No tree is actually constructed. The game tree is just an abstract concept. 4/13

§5 Backtracking Algorithms 2. The Turnpike Reconstruction Problem Given N points on the x-axis with coordinates x 1 < x 2 < …< x N. Assume that x 1 = 0. There are N ( N – 1 ) / 2 distances between every pair of points. Given N ( N – 1 ) / 2 distances. Reconstruct a point set from the distances. 〖 Example 〗 Given D = { 1, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8, 10 } Step 1: N ( N – 1 ) / 2 = 15 implies N = ? 6 Step 2: x 1 = 0 and x 6 = 10 x 3 = 6x 2 = 4x 3 = 4x 4 = 6 x 3 = 5 x 4 = 7x 2 = 3 x 5 = 8x 2 = 2 Step 3: find the next largest distance and check ( 0, 3, 5, 6, 8, 10 ) Please read Figure and for details. 5/13 Home work: p Homometric point sets

Research Project 14 Review of Programming Contest Rules (25) Given the ACM-ICPC's rule of scoring, your task is to write a program to find yourself the order of solving the problems which gives you the best chance of winning the contest. Detailed requirements can be downloaded from 6/13

Research Project 15 Good-Bye Party (25) In every summer there are students graduating from school. A lot of graduating students would like to host their parties to say good-bye to their classmates and friends. This kind of activity is called "bg" on our BBS. Attending different parties will give you different feelings. You may assign each party a degree of happiness which is a non-negative integer that shows how you feel about attending to that party. Now you are given a list of parties containing the length of each party and the time on which the host will be leaving school. It would be nice to schedule the parties so that you can obtain maximum of happiness. Since there could be as many as 30 parties on the list, you'll need the computer to help you. Detailed requirements can be downloaded from 7/13

§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N <= 1 ) return 1; else return Fib( N - 1 ) + Fib( N - 2 ); } T(N)  T(N – 1) + T(N – 2) T(N)  F(N) 8/13

§3 Dynamic ProgrammingF6 F2 F1F0 F3 F1 F2 F1F0 F2 F1F0 F3 F1F2 F1F0 F3 F1 F2 F1F0 F4 F5 Trouble-maker : The growth of redundant calculations is explosive. Solution : Record the two most recently computed values to avoid recursive calls. int Fibonacci ( int N ) { int i, Last, NextToLast, Answer; if ( N <= 1 ) return 1; Last = NextToLast = 1; /* F(0) = F(1) = 1 */ for ( i = 2; i <= N; i++ ) { Answer = Last + NextToLast; /* F(i) = F(i-1) + F(i-2) */ NextToLast = Last; Last = Answer; /* update F(i-1) and F(i-2) */ } /* end-for */ return Answer; } T(N) = O(N) 9/13

§3 Dynamic Programming 2. Ordering Matrix Multiplications 〖 Example 〗 Suppose we are to multiply 4 matrices M 1 [ 10  20 ]  M 2 [ 20  50 ]  M 3 [ 50  1 ]  M 4 [ 1  100 ]. If we multiply in the order M 1 [ 10  20 ]  ( M 2 [ 20  50 ]  ( M 3 [ 50  1 ]  M 4 [ 1  100 ] ) ) Then the computing time is 50  1   50   20  100 = 125,000 If we multiply in the order ( M 1 [ 10  20 ]  ( M 2 [ 20  50 ]  M 3 [ 50  1 ] ) )  M 4 [ 1  100 ] Then the computing time is 20  50   20   1  100 = 2,200 Problem: In which order can we compute the product of n matrices with minimal computing time? 10/13

§3 Dynamic Programming Let b n = number of different ways to compute M 1  M 2     M n. Then we have b 2 = 1, b 3 = 2, b 4 = 5,    Let M ij = M i     M j. Then M 1n = M 1     M n = M 1i  M i+1 n bnbn bibi bnibni )( 4 nn n n Ob  /* Catalan number */ Suppose we are to multiply n matrices M 1     M n where M i is an r i  1  r i matrix. Let m ij be the cost of the optimal way to compute M i     M j. Then we have the recurrence equations: There are only O( N 2 ) values of M ij. If j – i = k, then the only values M xy required to compute M ij satisfy y – x < k. 11/13

§3 Dynamic Programming /* r contains number of columns for each of the N matrices */ /* r[ 0 ] is the number of rows in matrix 1 */ /* Minimum number of multiplications is left in M[ 1 ][ N ] */ void OptMatrix( const long r[ ], int N, TwoDimArray M ) { int i, j, k, L; long ThisM; for( i = 1; i <= N; i++ ) M[ i ][ i ] = 0; for( k = 1; k < N; k++ ) /* k = j - i */ for( i = 1; i <= N - k; i++ ) { /* For each position */ j = i + k; M[ i ][ j ] = Infinity; for( L = i; L < j; L++ ) { ThisM = M[ i ][ L ] + M[ L + 1 ][ j ] + r[ i - 1 ] * r[ L ] * r[ j ]; if ( ThisM < M[ i ][ j ] ) /* Update min */ M[ i ][ j ] = ThisM; } /* end for-L */ } /* end for-Left */ } T(N) = O(N 3 ) To record the ordering please refer to Figure on p /13

Research Project 16 Diff (25) In computing, diff is a file comparison utility that outputs the differences between two files. It is typically used to show the changes between a file and a former version of the same file. Diff displays the changes made per line for text files. Now you are supposed to simulate the diff operation. Detailed requirements can be downloaded from 13/13