Brought to you by Max (ICQ:31252512 TEL:61337706) March 12, 2005 Recursion and Exhaustion.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

The simplex algorithm The simplex algorithm is the classical method for solving linear programs. Its running time is not polynomial in the worst case.
AE1APS Algorithmic Problem Solving John Drake
Chapter 11 Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Dana Nau: Lecture slides for Automated Planning Licensed under the Creative Commons Attribution-NonCommercial-ShareAlike License:
Types of Algorithms.
Lecture 24 Coping with NPC and Unsolvable problems. When a problem is unsolvable, that's generally very bad news: it means there is no general algorithm.
Algorithms + L. Grewe.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
1 NP-Complete Problems. 2 We discuss some hard problems:  how hard? (computational complexity)  what makes them hard?  any solutions? Definitions 
Page 1 Recursion and Exhaustion Yip Lik Yau. Page 2 Why Exhaustion?  Many problems can be solved by brute force in a reasonable amount of time if the.
© The McGraw-Hill Companies, Inc., Chapter 8 The Theory of NP-Completeness.
CHAPTER 13 Recursion. Recursive Solution A recursive solution  solves a problem by solving a smaller instance of the problem. Example  How do we go.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 19: Searching and Sorting Algorithms
Backtracking What is backtracking?
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
The Theory of NP-Completeness
Chapter 11: Limitations of Algorithmic Power
Ch 13 – Backtracking + Branch-and-Bound
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Backtracking.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
2.3 Solving Word Problems. Goals SWBAT solve linear inequalities SWBAT solve linear inequalities SWBAT solve compound inequalities SWBAT solve compound.
Introduction While it may not be efficient to write out the justification for each step when solving equations, it is important to remember that the properties.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Data Structures Using C++ 2E Chapter 6 Recursion.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Scott Perryman Jordan Williams.  NP-completeness is a class of unsolved decision problems in Computer Science.  A decision problem is a YES or NO answer.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
Design of Algorithms by Induction Part 1 Algorithm Design and Analysis Week 3 Bibliography: [Manber]- Chap.
1 Lower Bounds Lower bound: an estimate on a minimum amount of work needed to solve a given problem Examples: b number of comparisons needed to find the.
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.
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek.
CSCI 3160 Design and Analysis of Algorithms Tutorial 10 Chengyu Lin.
Hard Problems Some problems are hard to solve.  No polynomial time algorithm is known.  E.g., NP-hard problems such as machine scheduling, bin packing,
Exhaustive Search. Brute Force Methods  guarantee best fitness value is found  feasible for ‘small’ data sets only.
Algorithm Design Methods (II) Fall 2003 CSE, POSTECH.
Hard Problems Sanghyun Park Fall 2002 CSE, POSTECH.
5 – 2: Solving Quadratic Equations by Factoring Objective: CA 8: Students solve and graph quadratic equations by factoring, completing the square, or using.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Donghyun (David) Kim Department of Mathematics and Computer Science North Carolina Central University 1 Chapter 7 Time Complexity Some slides are in courtesy.
Backtracking & Brute Force Optimization Intro2CS – weeks
Solving the Logic Satisfiability problem Solving the Logic Satisfiability problem Jesus De Loera.
HAWKES LEARNING Students Count. Success Matters. Copyright © 2015 by Hawkes Learning/Quant Systems, Inc. All rights reserved. Section 7.2 Counting Our.
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Copyright © Cengage Learning. All rights reserved. 4 Integrals.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
Hard Problems Some problems are hard to solve.  No polynomial time algorithm is known.  E.g., NP-hard problems such as machine scheduling, bin packing,
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
3.4 – Geometric problems – 5 step approach (p200)
Problem Solving: Brute Force Approaches
The Theory of NP-Completeness
Integer Programming An integer linear program (ILP) is defined exactly as a linear program except that values of variables in a feasible solution have.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Types of Algorithms.
Problem Solving: Brute Force Approaches
Types of Algorithms.
Chapter 11 Limitations of Algorithm Power
Backtracking and Branch-and-Bound
Types of Algorithms.
The Theory of NP-Completeness
Lecture 4: Tree Search Strategies
Presentation transcript:

Brought to you by Max (ICQ: TEL: ) March 12, 2005 Recursion and Exhaustion

Page 2 Why Exhaustion? Many problems can be solved by brute force in a reasonable amount of time if the problem size is small Some problems have no known “fast” algorithm Example: Traveling Salesman, Hamiltonian Path, Graph Isomorphism… Estimating the time needed for brute force let us decide whether to use exhaustion or not Example: O(2 N ) is okay for N~30, O(N!) is okay for N~13 Knowing what types of problems are “hard” prevents you from wasting time in competitions

Page 3 Problem: Generating Permutations Given N, generate all permutations of integers 1..N When N = 3:

Page 4 Problem: Generating Permutations Nested For loop is out of the question We need a more advanced method – recursion Outline of algorithm: var used: array[0..100] of boolean; n: integer; procedure run(stage : integer); begin if (level = n) do_something; else for i = 1 to n do if not used[i] begin used[i] = true; run(stage+1); used[i] = false; end;

Page 5 Problem: Generating Permutations Search Tree

Page 6 General Idea of Recursion To solve a problem, break it into smaller, similar instances Suppose we can solve the smaller instances, then combining these results can solve the original problem For the algorithm to terminate, we also need to know how to solve the base case

Page 7 The N-Queens Problem In a NxN chessboard, place N queens so that no two of them can attack each other (this is wrong XD  ) One brute force algorithm is to find all permutations of 1..N Example for N=3: permutation is 132, then we place the queens at (1,1), (2,3) and (3,2) The do_something at base case is to check whether 2 queens are attacking Optimization: when placing each queen, we can immediately check if it has violated the constraint Cuts off useless branches quickly Q Q Q Q Q Q Q Q

Page 8 Estimating Time Complexity of Exhaustion The time complexity for the previous 2 algorithms are easy First stage has N choices, second stage has N-1 choices, … Total time = N*(N-1)*(N-2)*…*2*1 = N! If still not convinced, look at the search tree How about other problems? Start at the top-left corner, move to the bottom-right corner Each step can go down or right only Maximize sum of numbers in the path

Page 9 Two useful mathematical concepts Permutations (n P r) : number of ways to arrange r out of n objects with ordering 5 books, but only 3 slots on a bookshelf, how many permutations? The first slot has 5 choices The second slot has 4 choices The third slot has 3 choices Total number of permutations = 5*4*3 Formula : n P r = n! / (n-r)!

Page 10 Two useful mathematical concepts Combinations (n C r) : number of ways to choose r out of n objects without ordering A bag of 49 balls, draw 6 of them, how many combinations? The first ball has 49 choices The second ball has 48 choices … Total number of combinations = 49*48*47*46*45*44 However, is just the same as , or any of 6! permutations Therefore need to divide it by 6! Formula : n C r = n! / (n-r)! r! Property : n C r = n C (n-r) Choosing r items is equal to choosing which n-r items to leave out

Page 11 Estimating Time Complexity of Exhaustion For the equation x 1 + x 2 + … + x k = s, where x i >=0, how many possible solutions? Answer = (s+k-1) C (k-1), why? N people sit around a round table, how many permutations? (rotations of a permutation are considered the same) Answer = (N-1)!, why? Return to the previous problem To get from the top-left to bottom-right, we need to make 7 moves (4 right, 3 down) This is equivalent to choosing 4 out of 7 items Answer = 7 C 4 (or generally, (width + height – 2) C height )

Page 12 Lexicographical Ordering The objects we permutate may have some “ordering” Integers : 1 < 2 < 3 < … < N Alphabets : a < b < c < … < z If such an ordering exists, we can extend it to an ordering between permutations 123 < 213 < 321 bdca < cabd < cbad < dcab This is called “lexicographical ordering” or “dictionary ordering”

Page 13 Lexicographical Ordering Three natural questions to ask…… Given a permutation, determine its “next” permutation 231 -> 312 Find the Nth permutation (Deranking) 3 rd permutation of 1,2,3 = 213 Given a permutation, determine its position (Ranking) 132 -> position = 2