SlideShare une entreprise Scribd logo
1  sur  27
Solving Recurrences and the QuickSort Algorithm 1
Advanced Data Structures &
Algorithm Design
Solving Recurrences and the QuickSort
Algorithm
Solving Recurrences and the QuickSort Algorith2
The Search Problem
 A complement to the sorting problem
discussed last week is the searching
problem:
 Given a set of elements A = {a1, a2, …, an} and a
search parameter s, return the location i of s in A
 If A is an ordered set, then the binary search
algorithm can be used to search A for some value
Solving Recurrences and the QuickSort Algorith3
The Binary Search Algorithm
 The binary search algorithm operates as
follows (where A is the input set, and mid is
the index of the element at the midpoint of A):
 If s = Amid, return mid
 If s < Amid, search the left half of A
 If s > Amid, search the right half of A
Solving Recurrences and the QuickSort Algorith4
The Binary Search Algorithm
int BinarySearch(const DataType A[], int first, int
last, const DataType &s)
{
if ( last < first ) return –1; // not found
int mid = (first + last) / 2;
if ( s == A[mid] ) return mid;
if ( s < A[mid] ) // search left
return BinarySearch(A, first, mid-1, s);
else
return BinarySearch(A, mid+1, last, s);
}
int BinarySearch(const DataType A[], int first, int
last, const DataType &s)
{
if ( last < first ) return –1; // not found
int mid = (first + last) / 2;
if ( s == A[mid] ) return mid;
if ( s < A[mid] ) // search left
return BinarySearch(A, first, mid-1, s);
else
return BinarySearch(A, mid+1, last, s);
}
What is the order of growth of the running time of
BinarySearch?
What is the order of growth of the running time of
BinarySearch?
Solving Recurrences and the QuickSort Algorith5
Analysis of Binary Search
 Each execution requires up to 4 constant
operations and 1 recursive function call that
operates on ½ the array, so:



>+
≤
=
1if)1()2/(
1if)1(
)(
nOnT
nO
nT
 We shall see tonight that this equates to
T(n)=O(log2n)
Solving Recurrences and the QuickSort Algorith6
Solving Recurrences
 Recall our recurrence relation from last week:



>+
=
=
1if)()2/(2
1if)1(
)(
nnOnT
nO
nT
 How do we solve this recurrence to
determine the actual running time?
Solving Recurrences and the QuickSort Algorith7
Solving Recurrences
 There are three methods that we can use:
 The Substitution Method
 We guess a bound, then use mathematical induction to
prove our guess is correct
 The Iteration Method
 Converts the recurrence into a summation, then relies
on techniques for bounding summations
 The Master Method
 Provides bounds for recurrences of the form
T(n)=aT(n/b)+f(n) utilizing a set of rules
Solving Recurrences and the QuickSort Algorith8
The Substitution Method
 Involves guessing the form of the solution,
then using mathematical induction to find the
constants and show that the solution works
 Can only be applied in cases when it is easy
to guess the form of the answer
Solving Recurrences and the QuickSort Algorith9
The Substitution Method: An Example
 We can guess that the solution is of the form
T(n)=O(nlog2n)
 By definition, this means that T(n) has some
upper bound of cnlog2n for some constant c > 0
 We’ll use induction to prove that this bound holds



>+
=
=
1if)2/(2
1if)1(
)(
nnnT
nO
nT
Solving Recurrences and the QuickSort Algorith10
The Substitution Method: An Example
1aslongas-log
log
2loglog
)2/(log
))2/(log]2/[(2)(
:yieldsrecurrencetheintothisngSubstituti
)2/(log)2/()2/(
:isthat,2/forholdsboundthat thisassumingbystartWe
2
2
22
2
2
2
≥≤
+−=
+−=
+≤
+≤
≤
cncn
ncnncn
ncnncn
nncn
nnncnT
nncnT
n
Solving Recurrences and the QuickSort Algorith11
The Substitution Method: An Example
 Now we must show that our solution holds for
the boundary conditions
 This means that we must be able to show that we
can choose the constant c to be large enough so
that the bound holds for all n, including the
boundary conditions
 For example, assume that our only boundary
condition is that T(1)=1
 With our solution, T(1) <= c1log21 = 0, no matter
what c is
Solving Recurrences and the QuickSort Algorith12
The Substitution Method: An Example
 This problem can normally be easily
overcome
 Asymptotic notation only requires us to prove that
T(n)<=cnlog2n for some n >= n0
 We can choose n0 to remove the difficult boundary
condition from consideration
 E.g., choosing n0 to be 2 or 3 removes the difficult
boundary of 1 from consideration
Solving Recurrences and the QuickSort Algorith13
The Iteration Method
 No guesses are required, but significant
algebra is
 The idea is to expand the recurrence and
express it as a summation of terms
dependent only on n
Solving Recurrences and the QuickSort Algorith14
The Iteration Method: An Example
)64/(27)16/(9)4/(3
)))64/(3)16/((3)4/((3
))16/(3)4/((3
)4/(3)(
:followsasititeratecanWe
:)4/(3)(recurrenceheConsider t
nTnnn
nTnnn
nTnn
nTnnT
nnTnT
+++=
+++=
++=
+=
+=
How much iteration is enough?How much iteration is enough?
Solving Recurrences and the QuickSort Algorith15
The Iteration Method: An Example
 The i-th term in the series is 3i
(n/4i
)
 This iteration hits n=1 when (n/4i
)=1 or when
i>log4n
 If we continue the iteration to this point, we
discover a decreasing geometric series:
)(
)(4
2.9)(identity)(
4
3
)1(3...64/2716/94/3)(
0
3log
log
4
4
nO
nOn
nn
nnnnnT
i
i
n
=
+=
Θ+





≤
Θ+++++≤
∑
∞
=
Solving Recurrences and the QuickSort Algorith16
The Iteration Method
 There are two important points to focus on
when utilizing the Iteration Method:
 How many times must the recurrence be iterated
to reach the boundary condition?
 What is the summation of the terms?
 The book describes a technique called
“recurrence trees”, which are helpful in
finding a solution
Solving Recurrences and the QuickSort Algorith17
The Master Method
 Provides a “cookbook” approach to solving
recurrences
 It is used specifically to solve recurrences of the
form T(n)=aT(n/b)+cnk
 Recurrences of this form describe solutions that divide
problems into subproblems of size n/b, where cnk
is the
cost of dividing the problem and combining the results
Solving Recurrences and the QuickSort Algorith18
The Master Method
 To use the master method, you must
memorize three case:





<
=
>
=
k
k
k
k
k
a
ba
ba
ba
nO
nnO
nO
nT
b
if
if
if
)(
)log(
)(
)(
log
 Note that these are simplified versions of the
solutions found in the book
Solving Recurrences and the QuickSort Algorith19
QuickSort
 QuickSort is a typical example of a divide-
and-conquer algorithm:
 Divide – partitions the input around an arbitrary
element
 Conquer – sorts each partition
 Combine – sorting is performed in-place, and so
requires zero work
 The algorithm consists of two functions, the
recursive QuickSort(), which utilizes
Partition() to divide the array
Solving Recurrences and the QuickSort Algorith20
QuickSort
void QuickSort(ArrayType &A, int begin, int end)
{
if ( begin < end )
{
int q = Partition(A, begin, end);
QuickSort(A, begin, q-1);
QuickSort(A, q+1, end);
}
}
void QuickSort(ArrayType &A, int begin, int end)
{
if ( begin < end )
{
int q = Partition(A, begin, end);
QuickSort(A, begin, q-1);
QuickSort(A, q+1, end);
}
}
Solving Recurrences and the QuickSort Algorith21
QuickSort: Partition
int Partition(ArrayType A[], int begin, int end)
{
ArrayType x = A[end];
int i = begin - 1;
for ( int j = begin ; j < end ; ++j )
{
if ( A[j] < x )
{
++i;
swap(A[i], A[j]);
}
}
swap(A[i+1], A[end]);
return i+1;
}
int Partition(ArrayType A[], int begin, int end)
{
ArrayType x = A[end];
int i = begin - 1;
for ( int j = begin ; j < end ; ++j )
{
if ( A[j] < x )
{
++i;
swap(A[i], A[j]);
}
}
swap(A[i+1], A[end]);
return i+1;
}
Solving Recurrences and the QuickSort Algorith22
How Partition Works
9 7 4 5 2 1 6 3
i j
9 7 4 5 2 1 6 3
i j
9 7 4 5 2 1 6 3
i j
97 4 52 1 6 3
i j
Solving Recurrences and the QuickSort Algorith23
How Partition Works
9 7 452 1 63
The final result of the first partitioning:
•The partition element is in it’s final sorted position
•QuickSort now operates on each subarray
independently
Solving Recurrences and the QuickSort Algorith24
Analysis of QuickSort
 In the best case, Partition() divides the array evenly
at every level
 Each problem is divided into two equal subproblems of size
n/2
 Partition() itself is O(n) (it has a single loop that executes
some number of times based on the size of the input)
 The recurrence then is T(n)=2T(n/2)+n
 Using the Master Method:
 a=2, bk
=21
=2, a=bk
 this is Case 2
 T(n)=O(nk
log2n)=O(n1
log2n)=O(nlog2n)
Solving Recurrences and the QuickSort Algorith25
Analysis of QuickSort
 In the worst case, the size of one partition is
1 and the other is n-1
 Observing that T(1)=O(1), then the worst-case
recurrence is:
 T(n)=T(n-1)+O(n)+O(1)
 T(n)=T(n-1)+O(n)
Solving Recurrences and the QuickSort Algorith26
Analysis of QuickSort
 When will this worst-
case occur?
 How likely will it
occur?
 Why does it occur?
 How can we
improve it?
)(
)(
)()1()(
:iterationusecanweHere
2
1
1
n
k
k
nnTnT
n
k
n
k
Θ=






Θ=
Θ=
Θ+−=
∑
∑
=
=
Solving Recurrences and the QuickSort Algorith27
Homework
 Reading: 4.1-4.3; 8.1-8.2; 8.4
 Exercises:
 4.3-1, 2, 3

Contenu connexe

Tendances

Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Deepak John
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1chidabdu
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisAmrinder Arora
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm CaseyHaaland
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notationsjayavignesh86
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 

Tendances (20)

Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
 
Slide2
Slide2Slide2
Slide2
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Grovers Algorithm
Grovers Algorithm Grovers Algorithm
Grovers Algorithm
 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notations
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 

En vedette

lecture 8
lecture 8lecture 8
lecture 8sajinsc
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesWaqas Akram
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relationsIIUM
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5Kumar
 
recurrence relations
 recurrence relations recurrence relations
recurrence relationsAnurag Cheela
 
Recurrences
RecurrencesRecurrences
RecurrencesDEVTYPE
 

En vedette (9)

lecture 8
lecture 8lecture 8
lecture 8
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relation
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5
 
recurrence relations
 recurrence relations recurrence relations
recurrence relations
 
Reccurrence relations
Reccurrence relationsReccurrence relations
Reccurrence relations
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Merge sort
Merge sortMerge sort
Merge sort
 

Similaire à Cis435 week02

Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmMahbubur Rahman
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesNirmalavenkatachalam
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Pramit Kumar
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
Case Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfCase Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfShaistaRiaz4
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...2022cspaawan12556
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 

Similaire à Cis435 week02 (20)

02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
Ada notes
Ada notesAda notes
Ada notes
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Algorithms DM
Algorithms DMAlgorithms DM
Algorithms DM
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Case Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfCase Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdf
 
algo_vc_lecture8.ppt
algo_vc_lecture8.pptalgo_vc_lecture8.ppt
algo_vc_lecture8.ppt
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 

Plus de ashish bansal (13)

Data struters
Data strutersData struters
Data struters
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Cis435 week04
Cis435 week04Cis435 week04
Cis435 week04
 
Cis435 week03
Cis435 week03Cis435 week03
Cis435 week03
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
35 algorithm-types
35 algorithm-types35 algorithm-types
35 algorithm-types
 
32 algorithm-types
32 algorithm-types32 algorithm-types
32 algorithm-types
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
5 searching
5 searching5 searching
5 searching
 
4 recursion
4 recursion4 recursion
4 recursion
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 

Dernier

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Dernier (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Cis435 week02

  • 1. Solving Recurrences and the QuickSort Algorithm 1 Advanced Data Structures & Algorithm Design Solving Recurrences and the QuickSort Algorithm
  • 2. Solving Recurrences and the QuickSort Algorith2 The Search Problem  A complement to the sorting problem discussed last week is the searching problem:  Given a set of elements A = {a1, a2, …, an} and a search parameter s, return the location i of s in A  If A is an ordered set, then the binary search algorithm can be used to search A for some value
  • 3. Solving Recurrences and the QuickSort Algorith3 The Binary Search Algorithm  The binary search algorithm operates as follows (where A is the input set, and mid is the index of the element at the midpoint of A):  If s = Amid, return mid  If s < Amid, search the left half of A  If s > Amid, search the right half of A
  • 4. Solving Recurrences and the QuickSort Algorith4 The Binary Search Algorithm int BinarySearch(const DataType A[], int first, int last, const DataType &s) { if ( last < first ) return –1; // not found int mid = (first + last) / 2; if ( s == A[mid] ) return mid; if ( s < A[mid] ) // search left return BinarySearch(A, first, mid-1, s); else return BinarySearch(A, mid+1, last, s); } int BinarySearch(const DataType A[], int first, int last, const DataType &s) { if ( last < first ) return –1; // not found int mid = (first + last) / 2; if ( s == A[mid] ) return mid; if ( s < A[mid] ) // search left return BinarySearch(A, first, mid-1, s); else return BinarySearch(A, mid+1, last, s); } What is the order of growth of the running time of BinarySearch? What is the order of growth of the running time of BinarySearch?
  • 5. Solving Recurrences and the QuickSort Algorith5 Analysis of Binary Search  Each execution requires up to 4 constant operations and 1 recursive function call that operates on ½ the array, so:    >+ ≤ = 1if)1()2/( 1if)1( )( nOnT nO nT  We shall see tonight that this equates to T(n)=O(log2n)
  • 6. Solving Recurrences and the QuickSort Algorith6 Solving Recurrences  Recall our recurrence relation from last week:    >+ = = 1if)()2/(2 1if)1( )( nnOnT nO nT  How do we solve this recurrence to determine the actual running time?
  • 7. Solving Recurrences and the QuickSort Algorith7 Solving Recurrences  There are three methods that we can use:  The Substitution Method  We guess a bound, then use mathematical induction to prove our guess is correct  The Iteration Method  Converts the recurrence into a summation, then relies on techniques for bounding summations  The Master Method  Provides bounds for recurrences of the form T(n)=aT(n/b)+f(n) utilizing a set of rules
  • 8. Solving Recurrences and the QuickSort Algorith8 The Substitution Method  Involves guessing the form of the solution, then using mathematical induction to find the constants and show that the solution works  Can only be applied in cases when it is easy to guess the form of the answer
  • 9. Solving Recurrences and the QuickSort Algorith9 The Substitution Method: An Example  We can guess that the solution is of the form T(n)=O(nlog2n)  By definition, this means that T(n) has some upper bound of cnlog2n for some constant c > 0  We’ll use induction to prove that this bound holds    >+ = = 1if)2/(2 1if)1( )( nnnT nO nT
  • 10. Solving Recurrences and the QuickSort Algorith10 The Substitution Method: An Example 1aslongas-log log 2loglog )2/(log ))2/(log]2/[(2)( :yieldsrecurrencetheintothisngSubstituti )2/(log)2/()2/( :isthat,2/forholdsboundthat thisassumingbystartWe 2 2 22 2 2 2 ≥≤ +−= +−= +≤ +≤ ≤ cncn ncnncn ncnncn nncn nnncnT nncnT n
  • 11. Solving Recurrences and the QuickSort Algorith11 The Substitution Method: An Example  Now we must show that our solution holds for the boundary conditions  This means that we must be able to show that we can choose the constant c to be large enough so that the bound holds for all n, including the boundary conditions  For example, assume that our only boundary condition is that T(1)=1  With our solution, T(1) <= c1log21 = 0, no matter what c is
  • 12. Solving Recurrences and the QuickSort Algorith12 The Substitution Method: An Example  This problem can normally be easily overcome  Asymptotic notation only requires us to prove that T(n)<=cnlog2n for some n >= n0  We can choose n0 to remove the difficult boundary condition from consideration  E.g., choosing n0 to be 2 or 3 removes the difficult boundary of 1 from consideration
  • 13. Solving Recurrences and the QuickSort Algorith13 The Iteration Method  No guesses are required, but significant algebra is  The idea is to expand the recurrence and express it as a summation of terms dependent only on n
  • 14. Solving Recurrences and the QuickSort Algorith14 The Iteration Method: An Example )64/(27)16/(9)4/(3 )))64/(3)16/((3)4/((3 ))16/(3)4/((3 )4/(3)( :followsasititeratecanWe :)4/(3)(recurrenceheConsider t nTnnn nTnnn nTnn nTnnT nnTnT +++= +++= ++= += += How much iteration is enough?How much iteration is enough?
  • 15. Solving Recurrences and the QuickSort Algorith15 The Iteration Method: An Example  The i-th term in the series is 3i (n/4i )  This iteration hits n=1 when (n/4i )=1 or when i>log4n  If we continue the iteration to this point, we discover a decreasing geometric series: )( )(4 2.9)(identity)( 4 3 )1(3...64/2716/94/3)( 0 3log log 4 4 nO nOn nn nnnnnT i i n = += Θ+      ≤ Θ+++++≤ ∑ ∞ =
  • 16. Solving Recurrences and the QuickSort Algorith16 The Iteration Method  There are two important points to focus on when utilizing the Iteration Method:  How many times must the recurrence be iterated to reach the boundary condition?  What is the summation of the terms?  The book describes a technique called “recurrence trees”, which are helpful in finding a solution
  • 17. Solving Recurrences and the QuickSort Algorith17 The Master Method  Provides a “cookbook” approach to solving recurrences  It is used specifically to solve recurrences of the form T(n)=aT(n/b)+cnk  Recurrences of this form describe solutions that divide problems into subproblems of size n/b, where cnk is the cost of dividing the problem and combining the results
  • 18. Solving Recurrences and the QuickSort Algorith18 The Master Method  To use the master method, you must memorize three case:      < = > = k k k k k a ba ba ba nO nnO nO nT b if if if )( )log( )( )( log  Note that these are simplified versions of the solutions found in the book
  • 19. Solving Recurrences and the QuickSort Algorith19 QuickSort  QuickSort is a typical example of a divide- and-conquer algorithm:  Divide – partitions the input around an arbitrary element  Conquer – sorts each partition  Combine – sorting is performed in-place, and so requires zero work  The algorithm consists of two functions, the recursive QuickSort(), which utilizes Partition() to divide the array
  • 20. Solving Recurrences and the QuickSort Algorith20 QuickSort void QuickSort(ArrayType &A, int begin, int end) { if ( begin < end ) { int q = Partition(A, begin, end); QuickSort(A, begin, q-1); QuickSort(A, q+1, end); } } void QuickSort(ArrayType &A, int begin, int end) { if ( begin < end ) { int q = Partition(A, begin, end); QuickSort(A, begin, q-1); QuickSort(A, q+1, end); } }
  • 21. Solving Recurrences and the QuickSort Algorith21 QuickSort: Partition int Partition(ArrayType A[], int begin, int end) { ArrayType x = A[end]; int i = begin - 1; for ( int j = begin ; j < end ; ++j ) { if ( A[j] < x ) { ++i; swap(A[i], A[j]); } } swap(A[i+1], A[end]); return i+1; } int Partition(ArrayType A[], int begin, int end) { ArrayType x = A[end]; int i = begin - 1; for ( int j = begin ; j < end ; ++j ) { if ( A[j] < x ) { ++i; swap(A[i], A[j]); } } swap(A[i+1], A[end]); return i+1; }
  • 22. Solving Recurrences and the QuickSort Algorith22 How Partition Works 9 7 4 5 2 1 6 3 i j 9 7 4 5 2 1 6 3 i j 9 7 4 5 2 1 6 3 i j 97 4 52 1 6 3 i j
  • 23. Solving Recurrences and the QuickSort Algorith23 How Partition Works 9 7 452 1 63 The final result of the first partitioning: •The partition element is in it’s final sorted position •QuickSort now operates on each subarray independently
  • 24. Solving Recurrences and the QuickSort Algorith24 Analysis of QuickSort  In the best case, Partition() divides the array evenly at every level  Each problem is divided into two equal subproblems of size n/2  Partition() itself is O(n) (it has a single loop that executes some number of times based on the size of the input)  The recurrence then is T(n)=2T(n/2)+n  Using the Master Method:  a=2, bk =21 =2, a=bk  this is Case 2  T(n)=O(nk log2n)=O(n1 log2n)=O(nlog2n)
  • 25. Solving Recurrences and the QuickSort Algorith25 Analysis of QuickSort  In the worst case, the size of one partition is 1 and the other is n-1  Observing that T(1)=O(1), then the worst-case recurrence is:  T(n)=T(n-1)+O(n)+O(1)  T(n)=T(n-1)+O(n)
  • 26. Solving Recurrences and the QuickSort Algorith26 Analysis of QuickSort  When will this worst- case occur?  How likely will it occur?  Why does it occur?  How can we improve it? )( )( )()1()( :iterationusecanweHere 2 1 1 n k k nnTnT n k n k Θ=       Θ= Θ= Θ+−= ∑ ∑ = =
  • 27. Solving Recurrences and the QuickSort Algorith27 Homework  Reading: 4.1-4.3; 8.1-8.2; 8.4  Exercises:  4.3-1, 2, 3