SlideShare a Scribd company logo
Computer Science 385
                                     Analysis of Algorithms
                                     Siena College
                                     Spring 2011


                  Topic Notes: Decrease and Conquer
Our next class of algorithms are the decrease-and-conquer group.
The idea here:

   1. Reduce the problem to a smaller instance

   2. Solve the smaller instance

   3. Modify the smaller instance solution to be a solution to the original

The main variations:

   • Decrease by a constant (often by 1)

   • Decrease by a constant factor (often by half)

   • Variable size decrease

For example, consider variations on how to compute the value an .
The brute-force approach would involve applying the definition and multiplying a by itself, n
times.
A decrease-by-one approach would reduce the problem to computing the result for the problem of
size n − 1 (computing an−1 ) then modifying that to be a solution to the original (by multiplying
that result by a).
                                                                                    n
A decrease-by-constant-factor (2, in this case) approach would involve computing a 2 and multi-
plying that result by itself to compute the answer. The only complication here is that we have to
treat odd exponents specially, leading to a rule:

                                    (a 2 )2
                                       n
                                   
                                                    if n is even
                               n               2
                              a =  (a   n−1
                                          2    ) · a if n > 1 and odd
                                    a                if n = 1
                                  
                                  


This approach will lead to O(log n) multiplications.


Insertion Sort
CS 385                                    Analysis of Algorithms                       Spring 2011



Our decrease-by-one approach to sorting is the insertion sort.
The insertion sort sorts an array of n elements by first sorting the first n−1 elements, then inserting
the last element into its correct position in the array.

insertion_sort(A[0..n-1]) {
  for (i=0 to n-1) {
    v = A[i]
    j = i-1
    while (j >= 0 and A[j] > v) {
      A[j+1] = A[j]
      j--
    }
    A[j+1] = v

This is an in-place sort and is stable.
Our basic operation for this algorithm is the comparison of keys in the while loop.
We do have differences in worst, average, and best case behavior. In the worst case, the while loop
always executes as many times as possible. This occurs when each element needs to go all the way
at the start of the sorted portion of the array – exactly when the starting array is in reverse sorted
order.
The worst case number of comparisons:

                                       n−1 i−1         n−1
                                                                  n(n − 1)
                        Cworst (n) =             1=          i=            ∈ Θ(n2 )
                                       i=1 j=0         i=1           2

In the best case, the inner loop needs to do just one comparison, determining that the element is
already in its correct position. This happens when the algorithm is presented with already-sorted
input. Here, the number of comparisons:

                                                 n−1
                                  Cbest (n) =          1 = n − 1 ∈ Θ(n)
                                                 i=1


This behavior is unusual – after all, how often do we attempt to sort an already-sorted array?
However, we come close in some very important cases. If we have nearly-sorted data, we have
nearly this same performance.
A careful analysis of the average case would result in:

                                                        n2
                                          Cavg (n) ≈       ∈ Θ(n2 )
                                                        4

Of the simple sorting algorithms (bubble, selection, insertion), insertion sort is considered the best
option in general.

                                                       2
CS 385                               Analysis of Algorithms                          Spring 2011




Graph Traversals
We return to graph structures for our next group of decrease-and-conquer algorithms. In particular,
we consider the problem of visiting all vertices in a graph.
The two main approaches are the depth-first search (DFS) and the breadth-first search (BFS).
See the text for details...


Topological Sort
Again, see the text for details...




                                                3

More Related Content

PDF
CPSC 125 Ch 3 Sec 1
David Wood
 
PDF
tensor-decomposition
Kenta Oono
 
PDF
Tensor Decomposition and its Applications
Keisuke OTAKI
 
PDF
Sienna 3 bruteforce
chidabdu
 
PPTX
Advanced functions part i
wendyvazzy
 
PPTX
Recursion
Abdur Rehman
 
PPT
Probability And Random Variable Lecture(5)
University of Gujrat, Pakistan
 
PPTX
Radical and exponents (2)
Nurul Atiyah
 
CPSC 125 Ch 3 Sec 1
David Wood
 
tensor-decomposition
Kenta Oono
 
Tensor Decomposition and its Applications
Keisuke OTAKI
 
Sienna 3 bruteforce
chidabdu
 
Advanced functions part i
wendyvazzy
 
Recursion
Abdur Rehman
 
Probability And Random Variable Lecture(5)
University of Gujrat, Pakistan
 
Radical and exponents (2)
Nurul Atiyah
 

What's hot (20)

PDF
Weatherwax cormen solutions
kirankoushik
 
PDF
Generalization of Tensor Factorization and Applications
Kohei Hayashi
 
PPTX
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
PPTX
Limits of functions
Louie Joy Rosit
 
PDF
Phases of fuzzy field theories
Juraj Tekel
 
PDF
Machine Learning 1
cairo university
 
PDF
Lecture cochran
sabbir11
 
PDF
Digital Signal Processing Tutorial:Chapt 2 z transform
Chandrashekhar Padole
 
PDF
Random Matrix Theory and Machine Learning - Part 4
Fabian Pedregosa
 
PDF
SVM (Support Vector Machine & Kernel)
SEMINARGROOT
 
PDF
Mathematics notes and formula for class 12 chapter 7. integrals
sakhi pathak
 
PDF
Random Matrix Theory and Machine Learning - Part 2
Fabian Pedregosa
 
PDF
Random Matrix Theory and Machine Learning - Part 3
Fabian Pedregosa
 
PDF
11.generalized and subset integrated autoregressive moving average bilinear t...
Alexander Decker
 
PDF
Introduction to FEM
mezkurra
 
PDF
Lecture 1 computational intelligence
ParveenMalik18
 
PDF
Recursive Formulation of Gradient in a Dense Feed-Forward Deep Neural Network
Ashwin Rao
 
PDF
Lecture 2 fuzzy inference system
ParveenMalik18
 
Weatherwax cormen solutions
kirankoushik
 
Generalization of Tensor Factorization and Applications
Kohei Hayashi
 
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
Limits of functions
Louie Joy Rosit
 
Phases of fuzzy field theories
Juraj Tekel
 
Machine Learning 1
cairo university
 
Lecture cochran
sabbir11
 
Digital Signal Processing Tutorial:Chapt 2 z transform
Chandrashekhar Padole
 
Random Matrix Theory and Machine Learning - Part 4
Fabian Pedregosa
 
SVM (Support Vector Machine & Kernel)
SEMINARGROOT
 
Mathematics notes and formula for class 12 chapter 7. integrals
sakhi pathak
 
Random Matrix Theory and Machine Learning - Part 2
Fabian Pedregosa
 
Random Matrix Theory and Machine Learning - Part 3
Fabian Pedregosa
 
11.generalized and subset integrated autoregressive moving average bilinear t...
Alexander Decker
 
Introduction to FEM
mezkurra
 
Lecture 1 computational intelligence
ParveenMalik18
 
Recursive Formulation of Gradient in a Dense Feed-Forward Deep Neural Network
Ashwin Rao
 
Lecture 2 fuzzy inference system
ParveenMalik18
 
Ad

Viewers also liked (8)

PDF
Sienna 9 hashing
chidabdu
 
PDF
Sienna 8 countingsorts
chidabdu
 
PDF
Sienna 7 heaps
chidabdu
 
PDF
Sienna 6 bst
chidabdu
 
PDF
Sienna 10 dynamic
chidabdu
 
PDF
Sienna 12 huffman
chidabdu
 
PDF
Sienna 11 graphs
chidabdu
 
PDF
Algorithm chapter 7
chidabdu
 
Sienna 9 hashing
chidabdu
 
Sienna 8 countingsorts
chidabdu
 
Sienna 7 heaps
chidabdu
 
Sienna 6 bst
chidabdu
 
Sienna 10 dynamic
chidabdu
 
Sienna 12 huffman
chidabdu
 
Sienna 11 graphs
chidabdu
 
Algorithm chapter 7
chidabdu
 
Ad

Similar to Sienna 5 decreaseandconquer (20)

PDF
Algorithm chapter 5
chidabdu
 
PDF
Sienna 4 divideandconquer
chidabdu
 
PDF
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
PDF
Week09
hccit
 
PDF
Algorithm review
chidabdu
 
PDF
Ada notes
VIKAS SINGH BHADOURIA
 
PPTX
Algorithms - Rocksolid Tour 2013
Gary Short
 
PPT
Unit6 jwfiles
mrecedu
 
PPT
C Language Unit-6
kasaragadda srinivasrao
 
PPTX
chapter-4,5.pptxalgorithmalgorithmalgorithm
rudrainfrabuildenter
 
PPTX
Algorithim lec1.pptx
rediet43
 
PDF
CPSC 125 Ch 2 Sec 4
David Wood
 
PPTX
Algorithm & data structures lec4&5
Abdul Khan
 
PDF
Skiena algorithm 2007 lecture09 linear sorting
zukun
 
PPT
MergesortQuickSort.ppt
AliAhmad38278
 
PPT
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
PPTX
Lec1.pptx
AsadbekIbragimov1
 
PDF
Sure interview algorithm-1103
Sure Interview
 
PPT
course information of design analysis of alg
Bobby Pra A
 
Algorithm chapter 5
chidabdu
 
Sienna 4 divideandconquer
chidabdu
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
Week09
hccit
 
Algorithm review
chidabdu
 
Algorithms - Rocksolid Tour 2013
Gary Short
 
Unit6 jwfiles
mrecedu
 
C Language Unit-6
kasaragadda srinivasrao
 
chapter-4,5.pptxalgorithmalgorithmalgorithm
rudrainfrabuildenter
 
Algorithim lec1.pptx
rediet43
 
CPSC 125 Ch 2 Sec 4
David Wood
 
Algorithm & data structures lec4&5
Abdul Khan
 
Skiena algorithm 2007 lecture09 linear sorting
zukun
 
MergesortQuickSort.ppt
AliAhmad38278
 
presentation_mergesortquicksort_1458716068_193111.ppt
ajiths82
 
Sure interview algorithm-1103
Sure Interview
 
course information of design analysis of alg
Bobby Pra A
 

More from chidabdu (15)

PDF
Sienna 2 analysis
chidabdu
 
PDF
Sienna 1 intro
chidabdu
 
PDF
Sienna 13 limitations
chidabdu
 
PPT
Unit 3 basic processing unit
chidabdu
 
PPT
Unit 5 I/O organization
chidabdu
 
PDF
Algorithm chapter 1
chidabdu
 
PDF
Algorithm chapter 11
chidabdu
 
PDF
Algorithm chapter 10
chidabdu
 
PDF
Algorithm chapter 9
chidabdu
 
PDF
Algorithm chapter 8
chidabdu
 
PDF
Algorithm chapter 6
chidabdu
 
PDF
Algorithm chapter 2
chidabdu
 
PPT
Unit 2 arithmetics
chidabdu
 
PPT
Unit 1 basic structure of computers
chidabdu
 
PPT
Unit 4 memory system
chidabdu
 
Sienna 2 analysis
chidabdu
 
Sienna 1 intro
chidabdu
 
Sienna 13 limitations
chidabdu
 
Unit 3 basic processing unit
chidabdu
 
Unit 5 I/O organization
chidabdu
 
Algorithm chapter 1
chidabdu
 
Algorithm chapter 11
chidabdu
 
Algorithm chapter 10
chidabdu
 
Algorithm chapter 9
chidabdu
 
Algorithm chapter 8
chidabdu
 
Algorithm chapter 6
chidabdu
 
Algorithm chapter 2
chidabdu
 
Unit 2 arithmetics
chidabdu
 
Unit 1 basic structure of computers
chidabdu
 
Unit 4 memory system
chidabdu
 

Recently uploaded (20)

PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
PDF
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
PDF
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
PPTX
The Power of IoT Sensor Integration in Smart Infrastructure and Automation.pptx
Rejig Digital
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
This slide provides an overview Technology
mineshkharadi333
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
Make GenAI investments go further with the Dell AI Factory - Infographic
Principled Technologies
 
Shreyas_Phanse_Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
SHREYAS PHANSE
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
agentic-ai-and-the-future-of-autonomous-systems.pdf
siddharthnetsavvies
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
The Power of IoT Sensor Integration in Smart Infrastructure and Automation.pptx
Rejig Digital
 

Sienna 5 decreaseandconquer

  • 1. Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Decrease and Conquer Our next class of algorithms are the decrease-and-conquer group. The idea here: 1. Reduce the problem to a smaller instance 2. Solve the smaller instance 3. Modify the smaller instance solution to be a solution to the original The main variations: • Decrease by a constant (often by 1) • Decrease by a constant factor (often by half) • Variable size decrease For example, consider variations on how to compute the value an . The brute-force approach would involve applying the definition and multiplying a by itself, n times. A decrease-by-one approach would reduce the problem to computing the result for the problem of size n − 1 (computing an−1 ) then modifying that to be a solution to the original (by multiplying that result by a). n A decrease-by-constant-factor (2, in this case) approach would involve computing a 2 and multi- plying that result by itself to compute the answer. The only complication here is that we have to treat odd exponents specially, leading to a rule:  (a 2 )2  n   if n is even n 2 a =  (a n−1 2 ) · a if n > 1 and odd a if n = 1   This approach will lead to O(log n) multiplications. Insertion Sort
  • 2. CS 385 Analysis of Algorithms Spring 2011 Our decrease-by-one approach to sorting is the insertion sort. The insertion sort sorts an array of n elements by first sorting the first n−1 elements, then inserting the last element into its correct position in the array. insertion_sort(A[0..n-1]) { for (i=0 to n-1) { v = A[i] j = i-1 while (j >= 0 and A[j] > v) { A[j+1] = A[j] j-- } A[j+1] = v This is an in-place sort and is stable. Our basic operation for this algorithm is the comparison of keys in the while loop. We do have differences in worst, average, and best case behavior. In the worst case, the while loop always executes as many times as possible. This occurs when each element needs to go all the way at the start of the sorted portion of the array – exactly when the starting array is in reverse sorted order. The worst case number of comparisons: n−1 i−1 n−1 n(n − 1) Cworst (n) = 1= i= ∈ Θ(n2 ) i=1 j=0 i=1 2 In the best case, the inner loop needs to do just one comparison, determining that the element is already in its correct position. This happens when the algorithm is presented with already-sorted input. Here, the number of comparisons: n−1 Cbest (n) = 1 = n − 1 ∈ Θ(n) i=1 This behavior is unusual – after all, how often do we attempt to sort an already-sorted array? However, we come close in some very important cases. If we have nearly-sorted data, we have nearly this same performance. A careful analysis of the average case would result in: n2 Cavg (n) ≈ ∈ Θ(n2 ) 4 Of the simple sorting algorithms (bubble, selection, insertion), insertion sort is considered the best option in general. 2
  • 3. CS 385 Analysis of Algorithms Spring 2011 Graph Traversals We return to graph structures for our next group of decrease-and-conquer algorithms. In particular, we consider the problem of visiting all vertices in a graph. The two main approaches are the depth-first search (DFS) and the breadth-first search (BFS). See the text for details... Topological Sort Again, see the text for details... 3