Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina 2009.9. CSCE350 Algorithms and Data Structure.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
1 Recursive Algorithm Analysis Dr. Ying Lu RAIK 283: Data Structures & Algorithms September 13, 2012.
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS4413 Divide-and-Conquer
Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
The Design and Analysis of Algorithms
COSC 3100 Brute Force and Exhaustive Search Instructor: Tanvir 1.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Analysis & Design of Algorithms (CSCE 321)
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Homework page 102 questions 1, 4, and 10 page 106 questions 4 and 5 page 111 question 1 page 119 question 9.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Design and Analysis of Algorithms Chapter Analysis of Algorithms Dr. Ying Lu August 28, 2012
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Analysis of Algorithm Efficiency Dr. Yingwu Zhu p5-11, p16-29, p43-53, p93-96.
Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 3 Brute Force. A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples:
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 UNIT-I BRUTE FORCE ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 3:
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Lecture 4 Jianjun Hu Department of Computer Science and Engineerintg University of South Carolina CSCE350 Algorithms and Data Structure.
Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7.
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Exhaustive search Exhaustive search is simply a brute- force approach to combinatorial problems. It suggests generating each and every element of the problem.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
CSG523/ Desain dan Analisis Algoritma
CSG523/ Desain dan Analisis Algoritma
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force II.
Analysis of Algorithms
Analysis of algorithms
Introduction to the Design and Analysis of Algorithms
Analysis of algorithms
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
CSCE350 Algorithms and Data Structure
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 2.
Fundamentals of the Analysis of Algorithm Efficiency
3. Brute Force Selection sort Brute-Force string matching
3. Brute Force Selection sort Brute-Force string matching
At the end of this session, learner will be able to:
Analysis of algorithms
Fundamentals of the Analysis of Algorithm Efficiency
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure

Outline  Review of recursive algorithm running time—cheat sheet  Homogeneous Second-Order Linear Recurrence  Brute Force Strategy for Algorithm Design  The art of lazy algorithm is to count/estimate its running time  Is it doable within given timeframe?

Time Efficiency of Recursive Algorithms Steps in mathematical analysis of recursive algorithms:  Decide on parameter n indicating input size  Identify algorithm’s basic operation  Determine worst, average, and best case for input of size n  Set up a recurrence relation and initial condition(s) for C(n)-the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls).  Solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution (see Appendix B)

Three Recurrence Types We know How to Find the Closed-Form Solution Please related them to the following algorithms we learned in the last class Recursive algorithm for n! Recursive algorithm for Tower of Hanoi Recursive algorithm for finding the number of digits in the binary representation of a decimal integer Recursive algorithm for finding the Fibbonacci numbers

Important Recurrence Types: One (constant) operation reduces problem size by one. T(n) = T(n-1) + c T(1) = d Solution: T(n) = (n-1)c + d linear A pass through input reduces problem size by one. T(n) = T(n-1) + cn T(1) = d Solution: T(n) = [n(n+1)/2 – 1] c + d quadratic One (constant) operation reduces problem size by half. T(n) = T(n/2) + c T(1) = d Solution: T(n) = c lg n + d logarithmic A pass through input reduces problem size by half. T(n) = 2T(n/2) + cn T(1) = d Solution: T(n) = cn lg n + d n n log n

A General Divide-and-Conquer Recurrence: Master Theorem T(n) = aT(n/b) + f (n) where f (n) ∈ Θ(n k ) a < b k T(n) ∈ Θ(n k ) a = b k T(n) ∈ Θ(n k lg n ) a > b k T(n) ∈ Θ( n log b a ) Note: the same results hold with O instead of Θ.

Solutions to a Homogeneous Second-Order Linear Recurrence with Constant Coefficients Characteristic equation  roots Then

Fibonacci numbers The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, … Fibonacci recurrence: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 Another example: A(n) = 3A(n-1) – 2A(n-2) A(0) = 1 A(1) = 3 2nd order linear homogeneous recurrence relation with constant coefficients

How to Find the Constants and Using the initial conditions For example, for Fibonacci numbers F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 Characteristic equation  roots We have Using F(0)=0 and F(1)=1 

Computing Fibonacci numbers Definition based recursive algorithm Nonrecursive brute-force algorithm Explicit formula algorithm Logarithmic algorithm based on formula: F(n-1) F(n) F(n) F(n+1) = n for n≥1, assuming an efficient way of computing matrix powers.

Another Example: calculating a^n Construct an algorithm for computing a n, where a>0 is a constant and the integer n>=0 is the input size. Then analyze its time efficiency Basic operation  a multiplication of two float numbers Two choices Nonrecursive algorithm Recursive algorithm Any idea to make it faster, e.g., with a better time efficiency? Can we solve this problem in time?

Empirical Analysis of Algorithm Time Efficiency  Why?  Mathematical analysis is difficult for many algorithms  Difficult for average case analysis  Difficult for special type of data distribution  How?  Purpose: compare efficiency  Efficiency measures M: operation counts or time unit  Determine characteristics of typical sample input  Implement the algorithm and run it  Different sample sizes, multiple samples for each size  Profiling tools: measuring time spent on different segments of program can pinpoint bottleneck in a program..

Empirical Analysis of Algorithm Time Efficiency Data analysis for the empirical data Scatter Plot

Now We Get to Chapter 3 -- Brute Force From now on, we are going to learn some basic and general strategies in designing algorithms to solve some typical computing problems. We will analyze the efficiency of these algorithms using the tools learned in the past several classes We will learn how to design algorithms with better efficiency First, let’s talk about the Brute Force strategies– the simplest

Brute Force Strategy for Algorithm Design Brute Force is a straightforward approach to solving a problem, usually directly based on the problem’s statement and definitions of the concepts involved In many cases, Brute Force does not provide you a very efficient solution Brute Force may be enough for moderate size problems with current computers….

Sorting Algorithm We have discussed two sorting algorithms: Selection Sort and Insertion Sort What are the basic idea behind the Selection-Sort algorithm? Scanning the entire given list to find its smallest element and swap it with the first element This is a straightforward solution – Brute Force strategy What is its time efficiency – An example: sorting the numbers [ ] See pseudocode in Section 3.1

Selection sort

Insertion Sort

Another Brute-Force Application: Bubble Sort Compare adjacent elements and exchange them if they are out of order This the result after the first pass, which moves the largest as the rightmost element

Algorithm in Pseudocode What is the time efficiency?

Sequential Search – Brute Force Find whether a search key is present in an array What is time efficiency of this algorithm?

Brute-Force String Matching Find a pattern in the text: Pattern – ‘NOT’, text – ‘NOBODY_NOTICED_HIM’ Typical Applications – ‘find’ function in the text editor, e.g., MS-Word, Google search What is the time efficiency of this algorithm?

Closest-Pair and Convex Hull Problems by Brute Force Closest-Pair problem Given n points in a plane, find the closest pair How to solve this problem and what is the time efficiency of this algorithm? Convex-Hull problem Convex hull is the tightest convex polygon that bounds a set of n points in a plane Convex polygon – any two points in this polygon results in the inclusion of the segment that links these two points also in this polygon

Convex/NonConvex Polygons

Convex Hull Imagine a rubber band around a set of nails Nails touched by the band  extreme points

Solve Convex-Hull Problem  Connect any pair of points by a line segment.  Each line segment partitions the plane to the two half planes  If all the n points are on the same side of this line segment Such a line segment is an edge of the convex-hull polygon  What is the time efficiency of this Brute-Force algorithm?  For each possible pair of points, we need to check whether all the remaining n-2 points are on the same side of the line segment that connects these pair of points.  For Sorting, String Matching, and Convex-Hull problems, we will revisit them by designing more efficient algorithms.

Exhaustive Search A brute-force approach to combinatorial problem Generate each and every element of the problem’s domain Then compare and select the desirable element that satisfies the set constraints Involve combinatorial objects such as permutations, combinations, and subsets of a given set The time efficiency is usually bad – usually the complexity grows exponentially with the input size Three examples Traveling salesman problem Knapsack problem Assignment problem

Traveling Salesman Problem Find the shortest tour through a given n cities that visits each city exactly once before returning to the starting city Using graph model: city  vertex, road  edge, length of the road  edge weight. TSP  shortest Hamiltonian Circuit – a cycle that passes through all the vertices of the graph exactly once Exhaustive search: List all the possible Hamiltonian circuits (starting from any vertex) Ignore the direction How many candidate circuits do we have?  (n-1)!/2 Very high complexity

TSP Example