CS Analysis of Recursive Algorithms and Brute Force

Similar documents
CS Non-recursive and Recursive Algorithm Analysis

CS483 Design and Analysis of Algorithms

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

CS483 Design and Analysis of Algorithms

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

CS Asymptotic Notations for Algorithm Analysis

Module 1: Analyzing the Efficiency of Algorithms

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

CS483 Design and Analysis of Algorithms

Divide and Conquer. Recurrence Relations

Algorithm Design Strategies V

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

CPS 616 DIVIDE-AND-CONQUER 6-1

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

Chapter 8 Dynamic Programming

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

Copyright 2000, Kevin Wayne 1

Chapter 8 Dynamic Programming

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

Recurrence Relations

Computational Complexity and Intractability: An Introduction to the Theory of NP. Chapter 9

Question Paper Code :

COMP 382: Reasoning about algorithms

Asymptotic Analysis and Recurrences

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

Recursion: Introduction and Correctness

Practical Session #3 - Recursions

Algorithms Design & Analysis. Analysis of Algorithm

CS Asymptotic Notations for Algorithm Analysis Outline

Analysis of Algorithms - Using Asymptotic Bounds -

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Recursion and Induction

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

CSCI 3110 Assignment 6 Solutions

Massachusetts Institute of Technology Handout J/18.062J: Mathematics for Computer Science May 3, 2000 Professors David Karger and Nancy Lynch

Binomial Coefficient Identities/Complements

Unit 1A: Computational Complexity

(tree searching technique) (Boolean formulas) satisfying assignment: (X 1, X 2 )

CS60007 Algorithm Design and Analysis 2018 Assignment 1

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Linear Selection and Linear Sorting

Lecture 2: Divide and conquer and Dynamic programming

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

What is an integer program? Modelling with Integer Variables. Mixed Integer Program. Let us start with a linear program: max cx s.t.

CS 4349 Lecture August 30th, 2017

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

CS 161 Summer 2009 Homework #2 Sample Solutions

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Chapter 4 Divide-and-Conquer

Analysis of Algorithms

CS 310 Advanced Data Structures and Algorithms

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

1 Sequences and Summation

Week 5: Quicksort, Lower bound, Greedy

Data Structures in Java

CS2223 Algorithms D Term 2009 Exam 3 Solutions

Data Structures in Java

Review Of Topics. Review: Induction

Advanced Counting Techniques. Chapter 8

Dynamic Programming( Weighted Interval Scheduling)

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

AC64/AT64/ AC115/AT115 DESIGN & ANALYSIS OF ALGORITHMS

Recurrence Relations

CS 6901 (Applied Algorithms) Lecture 3

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

CS173 Running Time and Big-O. Tandy Warnow

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Recurrences COMP 215

Advanced Counting Techniques

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

Algorithm efficiency analysis

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Data Structures and Algorithms Chapter 3

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

Outline. 1 NP-Completeness Theory. 2 Limitation of Computation. 3 Examples. 4 Decision Problems. 5 Verification Algorithm

Advanced Analysis of Algorithms - Midterm (Solutions)

Module 1: Analyzing the Efficiency of Algorithms

Reductions, Recursion and Divide and Conquer

Kartsuba s Algorithm and Linear Time Selection

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Limitations of Algorithm Power

CSC 8301 Design & Analysis of Algorithms: Lower Bounds

CS483 Design and Analysis of Algorithms

Divide and Conquer. Andreas Klappenecker

CS325: Analysis of Algorithms, Fall Final Exam

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

CS Data Structures and Algorithm Analysis

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Dynamic Programming: Interval Scheduling and Knapsack

Transcription:

CS483-05 Analysis of Recursive Algorithms and Brute Force Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 4:30pm - 5:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/ lifei/teaching/cs483_fall07/ This lecture note is based on Introduction to The Design and Analysis of Algorithms by Anany Levitin. CS483 Design and Analysis of Algorithms 1 Lecture 05, September 11, 2007

Input: A positive decimal integer n. Example 4: Counting Binary Bits Output: The number of binary digits in n s binary representation. Algorithm 0.1: COUNTBINARYBITS(n) count = 1 while n > 1 count = count + 1 do n = n/2 return (count) CS483 Design and Analysis of Algorithms 2 Lecture 05, September 11, 2007

Outline Analysis of Recursive Algorithms Brute Force Ideas Examples: Selection Sort & Bubble Sort Examples: String Matching Examples: Exhaustive Search CS483 Design and Analysis of Algorithms 3 Lecture 05, September 11, 2007

The iteration method Analysis of Recursive Algorithms Expand (iterate) the recurrence and express it as a summation of terms depending only on n and the initial conditions. The substitution method Master Theorem (To be introduced in Chapter 4.) CS483 Design and Analysis of Algorithms 4 Lecture 05, September 11, 2007

Iteration Method: Examples n! T(n) = T(n 1) + 1 Tower of Hanoi T(n) = 2T(n 1) + 1 CS483 Design and Analysis of Algorithms 5 Lecture 05, September 11, 2007

n! (T(n) = T(n 1) + 1) Iteration: Example T(n) = T(n 1) + 1 = (T(n 2) + 1) + 1 = T(n 2) + 2 = T(n i) + i = T(0) + n = n Tower of Hanoi (T(n) = 2T(n 1) + 1)??? CS483 Design and Analysis of Algorithms 6 Lecture 05, September 11, 2007

Tower of Hanoi (T(n) = 2T(n 1) + 1) T(n) = 2T(n 1) + 1 = 2(2T(n 2) + 1) + 1 = 2 2 T(n 2) + 2 + 1 = 2 i T(n i) + 2 i 1 + + 1 = 2 n 1 T(1) + 2 n 1 + 2 n 1 + + 1 = 2 n 1 T(1) + n 2 i=0 = 2 n 1 + 2 n 1 1 = 2 n 1 2 i CS483 Design and Analysis of Algorithms 7 Lecture 05, September 11, 2007

The iteration method Analysis of Recursive Algorithms Expand (iterate) the recurrence and express it as a summation of terms depending only on n and the initial conditions. The substitution method 1. Guess the form of the solution 2. Use mathematical induction to find the constants Master Theorem CS483 Design and Analysis of Algorithms 8 Lecture 05, September 11, 2007

Substitution Method: Example 1 Count number of bits (T(n) = T( n/2 ) + 1) CS483 Design and Analysis of Algorithms 9 Lecture 05, September 11, 2007

Substitution Method: Example 1 Count number of bits (T(n) = T( n/2 ) + 1) Guess T(n) log n. T(n) = T( n/2 ) + 1 log( n/2 ) + 1 log(n/2) + 1 (log n log 2) + 1 log n 1 + 1 = log n CS483 Design and Analysis of Algorithms 10 Lecture 05, September 11, 2007

Substitution Method: Example 2 Tower of Hanoi (T(n) = 2T(n 1) + 1) CS483 Design and Analysis of Algorithms 11 Lecture 05, September 11, 2007

Substitution Method: Example 2 Tower of Hanoi (T(n) = 2T(n 1) + 1) Guess T(n) 2 n. T(n) = 2T(n 1) + 1 2 2 n 1 + 1 2 n + 1, wrong! CS483 Design and Analysis of Algorithms 12 Lecture 05, September 11, 2007

Substitution Method: Extension F n Tower of Hanoi (T(n) = 2T(n 1) + 1) Guess T(n) 2 n. T(n) = 2T(n 1) + 1 2 2 n 1 + 1 2 n + 1, wrong! Guess T(n) 2 n 1. CS483 Design and Analysis of Algorithms 13 Lecture 05, September 11, 2007

T(n) = 2T(n 1) + 1 2(2 n 1 1) + 1 = 2 n 2 + 1 = 2 n 1, correct! CS483 Design and Analysis of Algorithms 14 Lecture 05, September 11, 2007

Substitution Method: Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) CS483 Design and Analysis of Algorithms 15 Lecture 05, September 11, 2007

Substitution Method: Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) F n 2 < F n 1 < F n, n 1 CS483 Design and Analysis of Algorithms 16 Lecture 05, September 11, 2007

Substitution Method: Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) F n 2 < F n 1 < F n, n 1 Assume 2 n 1 < F n < 2 n Guess F n = c φ n, 1 < φ < 2. CS483 Design and Analysis of Algorithms 17 Lecture 05, September 11, 2007

Substitution Method: Extension F n Fibonacci Numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) F n 2 < F n 1 < F n, n 1 Assume 2 n 1 < F n < 2 n Guess F n = c φ n, 1 < φ < 2. c φ n = c φ n 1 + c φ n 2 φ 2 = φ + 1 φ = 1 ± 5 2 General solution: F n = c 1 φ n 1 + c 2 φ n 2 F 1 = 0, F 2 = 1 CS483 Design and Analysis of Algorithms 18 Lecture 05, September 11, 2007

General solution: F n = c 1 φ n 1 + c 2 φ n 2 F 1 = 0, F 2 = 1 F n = 1 ( 1 + 5 ) n 1 ( 1 5 5 2 5 2 ) n CS483 Design and Analysis of Algorithms 19 Lecture 05, September 11, 2007

Order of growth of functions Summary: Algorithm Analysis Analyze algorithms order of growth (using asymptotic notations). Non-recursive algorithms Recursive algorithms a. The iteration method b. The substitution method c. Master Theorem (to be introduced) (T(n) = at(n/b) + f(n).) CS483 Design and Analysis of Algorithms 20 Lecture 05, September 11, 2007

Outline Analysis of Recursive Algorithms Brute Force Ideas Examples: Selection Sort & Bubble Sort Examples: String Matching Examples: Exhaustive Search CS483 Design and Analysis of Algorithms 21 Lecture 05, September 11, 2007

Brute Force Ideas Brute force is a straightforward approach to solve a problem, usually directly based on the problem statement and definitions of the concepts involved. CS483 Design and Analysis of Algorithms 22 Lecture 05, September 11, 2007

Outline Analysis of Recursive Algorithms Brute Force Ideas Examples: Selection Sort & Bubble Sort Examples: String Matching Examples: Exhaustive Search CS483 Design and Analysis of Algorithms 23 Lecture 05, September 11, 2007

Selection Sort & Bubble Sort Given n orderable items, sort them in non-decreasing order. CS483 Design and Analysis of Algorithms 24 Lecture 05, September 11, 2007

Selection Sort Given n orderable items, sort them in non-decreasing order. Input: An array A[0,...,n 1] of orderable elements. Output: An array A[0,...,n 1] sorted in non-decreasing order. Algorithm 0.2: SELECTIONSORT(A[0, n 1]) for i = 0 to n 2 min = i for j = i + 1 to n 1 if A[j] < A[min] then min = j Swap A[i] and A[min] do do CS483 Design and Analysis of Algorithms 25 Lecture 05, September 11, 2007

Selection Sort Analysis Input size: n. Basic operation: A[j] < A[min] Running time: C(n) = = n 2 i=0 n 1 j=i+1 1 n 2 [(n 1) (i 1) + 1] = i=0 (n 1)n = 2 = Θ(n 2 ) n 2 (n 1 i) i=0 CS483 Design and Analysis of Algorithms 26 Lecture 05, September 11, 2007

Bubble Sort Given n orderable items, sort them in non-decreasing order. Input: An array A[0,...,n 1] of orderable elements. Output: An array A[0,...,n 1] sorted in non-decreasing order. Algorithm 0.3: BUBBLESORT(A[0, n 1]) for i = 0 to n 2 for j = 0 to n 2 i do if A[j + 1] < A[j] do then Swap A[j] and A[j + 1] CS483 Design and Analysis of Algorithms 27 Lecture 05, September 11, 2007

Bubble Sort Analysis Input size: n. Basic operation: A[j + 1] < A[j] Running time: C(n) = = n 2 i=0 n 2 i j=0 1 n 2 [(n 2 i) 0 + 1] = i=0 (n 1)n = 2 = Θ(n 2 ) n 2 (n 1 i) i=0 CS483 Design and Analysis of Algorithms 28 Lecture 05, September 11, 2007

Outline Analysis of Recursive Algorithms Brute Force Ideas Examples: Selection Sort & Bubble Sort Examples: String Matching Examples: Exhaustive Search CS483 Design and Analysis of Algorithms 29 Lecture 05, September 11, 2007

String Matching Given a string of n characters called the text; and a string of m characters called the pattern, find a substring of the text that matches the pattern. Input: An array T[0,...,n 1] of n characters representing a text An array P[0,...,m] characters representing a pattern Output: The index of the first character in the text that starts a matching substring or 1 if the search is unsuccessful Example: Pattern: 001011 Text: 10010101101001100101111010 Pattern: happy Text: It is never too late to have a happy childhood. CS483 Design and Analysis of Algorithms 30 Lecture 05, September 11, 2007

String Matching Algorithm 0.4: STRINGMATCHING(T[0, n 1], P[0,..., m 1]) for i = 0 to n m j = 0 while j < m and P[j] = T[i + j] do do j = j + 1 if j = m return ( 1) then return (i) CS483 Design and Analysis of Algorithms 31 Lecture 05, September 11, 2007

String Matching Analysis Input size: n + m. Basic operation: P[j] = T[i + j] Running time (worst-case): C(n + m) = (n m + 1) m = Θ(nm) CS483 Design and Analysis of Algorithms 32 Lecture 05, September 11, 2007

Outline Analysis of Recursive Algorithms Brute Force Ideas Examples: Selection Sort & Bubble Sort Examples: String Matching Examples: Exhaustive Search CS483 Design and Analysis of Algorithms 33 Lecture 05, September 11, 2007

Traveling Salesman Problem TSP: Find the shortest tour through a given set of n cities that visits each city exactly once before returning to the city where it starts. a 2 b 5 8 7 3 c 1 d CS483 Design and Analysis of Algorithms 34 Lecture 05, September 11, 2007

a 2 b 5 7 8 3 c 1 d Tour Cost a b c d a 2 + 3 + 7 + 5 = 17 a b d c a 2 + 4 + 7 + 8 = 21 a c b d a 8 + 3 + 4 + 5 = 20 a c d b a 8 + 7 + 4 + 2 = 21 a d b c a 5 + 4 + 3 + 8 = 20 a d c b a 5 + 7 + 3 + 2 = 17 CS483 Design and Analysis of Algorithms 35 Lecture 05, September 11, 2007

Traveling Salesman Problem Analysis Input size: n (n 1). Running time: C(n) = (n 1)!/2. CS483 Design and Analysis of Algorithms 36 Lecture 05, September 11, 2007

Knapsack Problem Knapsack Problem: Given n objects, each object i has weight w i and value v i, and a knapsack of capacity W, find most valuable items that fit into the knapsack Items are not splittable CS483 Design and Analysis of Algorithms 37 Lecture 05, September 11, 2007

Example: Knapsack capacity W = 16 Item Weight Value 1 2 $20 2 5 $30 3 10 $50 4 5 $10 CS483 Design and Analysis of Algorithms 38 Lecture 05, September 11, 2007

Subset Total weight Total value {1} 2 $20 {2} 5 $30 {3} 10 $50 {4} 5 $10 {1, 2} 7 $50 {1, 3} 12 $70 {1, 4} 7 $30 {2, 3} 15 $80 {2, 4} 10 $40 {3, 4} 15 $60 {1, 2, 3} 17 not feasible {1, 2, 4} 12 $60 {1, 3, 4} 17 not feasible {2, 3, 4} 20 not feasible {1, 2, 3, 4} 22 not feasible CS483 Design and Analysis of Algorithms 39 Lecture 05, September 11, 2007

Knapsack Problem Analysis Input size: n (items). Running time: The number of subsets of an n-element set is 2 n. C(n) = Ω(2 n ). CS483 Design and Analysis of Algorithms 40 Lecture 05, September 11, 2007

Assignment Problem Assignment Problem: There are n people to execute n jobs, one person per job. If ith person is assigned the jth job, the cost is C[i, j], i, j = 1,...,n. Find the assignment with the minimum total cost. Job 1 Job 2 Job 3 Job 4 Person 1 9 2 7 8 Person 2 6 4 3 7 Person 3 5 8 1 8 Person 4 7 6 9 4 CS483 Design and Analysis of Algorithms 41 Lecture 05, September 11, 2007

Assignment Problem Analysis Input size: n. Running time: C(n) = n!. CS483 Design and Analysis of Algorithms 42 Lecture 05, September 11, 2007

Summary for Brute Force Strengths 1. Wide applicability 2. Simplicity 3. Yields reasonable algorithms for some important problems (e.g., matrix multiplication, sorting, searching, string matching) Weaknesses 1. Rarely yields efficient algorithms 2. Some brute-force algorithms are unacceptably slow 3. Not as constructive as some other design techniques CS483 Design and Analysis of Algorithms 43 Lecture 05, September 11, 2007

Summary for Brute Force Exhaustive-search algorithms run in a realistic amount of time only on very small instances In some cases, there are much better alternatives Shortest paths (greedy) Minimum spanning tree (greedy) Assignment problem (iterative improvement) In many cases, exhaustive search or its variation is the only known way to get exact solution CS483 Design and Analysis of Algorithms 44 Lecture 05, September 11, 2007

Summary Read Chap. 3. Next class: Chap. 4 and Master Theorem. CS483 Design and Analysis of Algorithms 45 Lecture 05, September 11, 2007