SlideShare a Scribd company logo
Chapter 2: Algorithm Analysis - I
Text: Read Weiss, §2.1 – 2.4.2
1
Definition
• An Algorithm is a clearly specified set of
simple instructions to be followed to solve
a problem.
• Once an algorithm is given and decided
somehow to be correct, an important step
is to determine how much in the way of
resources, such as time or space, the
algorithm will require.
2
Mathematical Background
• Definition 2.1. T(N) = O(f(N)) if there are
c,n0 ≥0 such that T(N)≤cf(N) when N ≥ n0.
• Definition 2.2. T(N) = Ω(g(N)) if there are
c,n0 ≥0 such that T(N)≥cg(N) when N ≥ n0.
• Definition 2.3. T(N) = Ɵ(h(N)) iff
T(N)=O(h(N)) and T(N)=Ω(h(N)).
• Definition 2.4. T(N) = o(p(N)) if for all c,
there exists an n0 such that T(N)<cp(N)
when N > n0. (T(N) = o(p(N)) if
T(N)=O(p(N)) and T(N) <> Ɵ(p(N))
3
O (Big-Oh) Notation
• Definitions establish a relative order among
functions. We compare their relative rates of
growth.
• Example: For small values of N, T(N)=1000N
is larger than f(N)=N2. For N≥n0=1000 and c=
1, T(N) ≤ cf(N). Therefore, 1000N = O(N2)
(Big-Oh notation).
• Big-Oh notation says that the growth rate of
T(N) is less than or equal to that of f(N).
T(N)=O(f(N)) means f(N) is an upper bound
on T(N).
4
Ω and Ɵ notations
• T(N) = Ω(g(N)) (pronounced “omega”) says that
the growth rate of T(N) is greater than or equal
to that of g(N). g(N) is a lower bound on T(N).
• T(N) = Ɵ(h(N)) (pronounced “theta”) says that
the growth rate of T(N) equals the growth rate
h(N).
• T(N) = o(p(N)) (pronounced “little-oh”) says that
the growth rate of T(N) is less than that of p(N).
• Example: N2=O(N3), N3=Ω(N2)
5
Rules for Asymptotic Analysis-I
• Rule 1: If T1(N)=O(f(N)) and T2(N)=O(g(N)), then
a) T1(N)+T2(N)=O(f(N)+g(N))
(intuitevely, max(O(f(N)), O(g(N)))
b) T1(N)*T2(N)=O(f(N)*g(N))
• Rule 2: If T(N) is a polynomial
of degree k, then T(N)=Ɵ(Nk).
• Rule 3: logkN=O(N) for any
constant k.
6
Rules for Asymptotic Analysis-II
• It is very bad style to include constants or low-order
terms inside a Big-Oh. Do not say T(N)=O(2N2) or
T(N)=O(N2+N). The correct form is T(N)=O(N2).
• The relative growth rates of f(N) and g(N) can always be
determined by limN→∞(f(N)/g(N)): (using L’Hỏpital’s rule if
necessary)
1. The limit is 0: f(N)=o(g(N))
2. The limit is c≠0: f(N)=Ɵ(g(N))
3. The limit is ∞: g(N)=o(f(N))
4. The limit oscillates: no relation
7
)(
)(lim
)(
)(lim
)(lim,)(lim
Ng
Nf
NNg
Nf
N
Ng
N
Nf
N
′
′
∞→
=
∞→
∞=
∞→
∞=
∞→
Rules for Asymptotic Analysis-III
• Sometimes simple algebra is just sufficient.
• Example: f(N)=NlogN, g(N)=N1.5 are given.
Decide which of the two functions grows faster!
• This amounts to comparing logN and N0.5. This,
in turn, is equivalent to testing log2N and N. But
we already know that N grows faster than any
power of a log.
• It is bad to say f(N)≤O(g(N)), because the
inequality is implied by the definition.
f(N)≥O(g(N)) is incorrect since it does not make
sense.
8
Model of Computation
• Our model is a normal computer.
• Instructions are executed sequentially.
• It has a repertoire of simple instructions (addition,
multiplication, comparison, assignment).
• It takes one (1) time unit to execute these simple
instructions (assume our model has fixed size
integers and no fancy operations like matrix
inversion or sorting).
• It has infinite memory.
• Weaknesses: disk read vs addition, page faults
when memory is not infinite
9
What to Analyze
• The most important resource to analyze is generally
the running time of a program.
• Compiler and computer used affect it but are not
taken into consideration.
• The algorithm used and the input to it will be
considered.
• Tavg(N) (average running time: typical behavior) and
Tworst(N) (worst case: it is generally the required
quantity: guarantee for performance: bound for all
input) running times on input size N.
• The details of the programming language do not
affect a Big-Oh answer. It is the algorithm that is
analyzed not the program (implementation).
10
Maximum Subsequence Sum
Problem - I
• Definition: Given (possibly negative) integers
A1, A2, ..., AN, find the maximum value of .
(For convenience, the maximum subsequence
sum is 0 if all integers are negative)
• Example: For input -2, 11, -4, 13, -5, -2, the
answer is 20 (A2 through A4).
• There are many algorithms to solve this
problem. We will discuss 4 of these.
11
∑
=
j
ik
k
A
Maximum Subsequence Sum
Problem - II
• For a small amount of input, they all run in a
blink of the eye.
• Algorithms should not form bottlenecks.Times do
not include the time to read.
12
Maximum Subsequence Sum
Problem - III
-O(NlogN)
Algorithm is
not linear.
Verify it by a
straight-edge.
-Relative
growth rates
are evident.
13
Maximum Subsequence Sum
Problem - IV
• Illustrates
how useless
inefficient
algorithms
are for even
moderately
large
amounts of
input.
14
Running Time Calculations
• Several ways to estimate the running time of a program (empirical vs
analytical)
• Big-Oh running times. Here is a simple program fragment to
calculate
- The declarations count for no time.
- Lines 1 and 4 count for 1 unit each
- Line 3 counts for 4 units per time
executed (2 multiplications, 1 addition,
1 assignment) and is executed N
times for a total of 4N units.
- Line 2 costs 2N+2 units (1 unit for initial
assignment, N+1 units for comparison tests
N units for all increments).
- Total time T(N) is 6N+4 which is O(N).
15
∑ =
N
i
i
1
3
unsigned int sum( int n ) {
unsigned int i, partial_sum;
/*1*/ partial_sum = 0;
/*2*/ for( i=1; i<=n; i++ )
/*3*/ partial_sum += i*i*i;
/*4*/ return( partial_sum );
}
Some shortcuts could be taken
without affecting the final answer
(Line 3 is an O(1) statement, Line 1
is insignificant compared to for loop.
General Rules - I
• RULE 1-FOR LOOPS:The running time of a for loop is at
most the running time of the statements inside the for
loop (including tests) times the number of iterations.
• RULE 2-NESTED FOR LOOPS: Analyze these inside
out. The total running time of a statement inside a group
of nested for loops is the running time of the statement
multiplied by the product of the sizes of all the for loops.
Example: the following program fragment is O(n2):
16
for( i = 0; i < n; i++ )
for( j=0; j < n; j++ )
k++;
General Rules - II
• RULE 3-CONSECUTIVE STATEMENTS: These just
add (which means that the maximum is the one that
counts – Rule 1(a) on page 6).
Example: the following program fragment, which has O(n)
work followed by O (n2) work, is also O (n2):
17
for( i = 0; i < n; i++)
a[i] = 0;
for( i = 0; i < n; i++ )
for( j = 0; j < n; j++ )
a[i] += a[j] + i + j;
General Rules - III
• RULE 4-lF/ELSE: For the fragment
• the running time of an if/else statement is never more
than the running time of the test plus the larger of the
running times of S1 and S2.
• Clearly, this can be an over-estimate in some cases, but
it is never an under-estimate.
18
if( condition )
S1
else
S2
General Rules - IV
• Other rules are obvious, but a basic strategy of analyzing
from the inside (or deepest part) out works. If there are
function calls, obviously these must be analyzed first.
• If there are recursive procedures, there are several
options. If it is really just a thinly veiled for loop, the
analysis is usually trivial. Example: The following function
is really just a simple loop and is obviously O (n):
19
unsigned int factorial( unsigned int n ) {
if( n <= 1 )
return 1;
else
return( n * factorial(n-1) );
}
General Rules - V
• When recursion is properly used, it is difficult to convert the
recursion into a simple loop structure. In this case, the analysis
will involve a recurrence relation that needs to be solved.
• Example: Consider the following program:
- If the program is run for values of n
around 40, it becomes terribly inefficient.
Let T(n) be the running time for the
Function fib(n). T(0)=T(1)=1 (time to do
the test at Line 1 and return). For n≥2,
the total time required is then
T(n ) = T(n - 1) + T(n - 2) + 2
(where the 2 accounts for the work at Line 1 plus the addition at
Line 3).
T(n) ≥ fib(n)=fib(n-1)+fib(n-2) ≥ (3/2)n (for n > 4, by induction)
• Huge amount of redundant work (violates 4th rule of recursion-
compound interest rule. fib(n-1) has already computed fib(n-2)
20
unsigned int fib( unsigned int n ) {
/*1*/ if( n <= 1 )
/*2*/ return 1;
else
/*3*/ return( fib(n-1) + fib(n-2) );
}

More Related Content

What's hot

Time complexity
Time complexityTime complexity
Time complexity
Kartik Chandra Mandal
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
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
jayavignesh86
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
Daffodil International University
 
time domain analysis, Rise Time, Delay time, Damping Ratio, Overshoot, Settli...
time domain analysis, Rise Time, Delay time, Damping Ratio, Overshoot, Settli...time domain analysis, Rise Time, Delay time, Damping Ratio, Overshoot, Settli...
time domain analysis, Rise Time, Delay time, Damping Ratio, Overshoot, Settli...
Waqas Afzal
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
Ashim Lamichhane
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
Nisha Soms
 
Design and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. MohiteDesign and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. Mohite
Zeal Education Society, Pune
 
asymptotic notation
asymptotic notationasymptotic notation
asymptotic notation
SangeethaSasi1
 
Intro to Sorting + Insertion Sort
Intro to Sorting + Insertion SortIntro to Sorting + Insertion Sort
Intro to Sorting + Insertion Sort
Nicholas Case
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
Tareq Hasan
 
Algorithm in Computer, Sorting and Notations
Algorithm in Computer, Sorting  and NotationsAlgorithm in Computer, Sorting  and Notations
Algorithm in Computer, Sorting and Notations
Abid Kohistani
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
Master theorem
Master theoremMaster theorem
Master theorem
fika sweety
 
Unit 4
Unit 4Unit 4
Unit 4
guna287176
 
Unit 3
Unit 3Unit 3
Unit 3
guna287176
 
Analysis of Algorithm
Analysis of AlgorithmAnalysis of Algorithm
Analysis of Algorithm
أحلام انصارى
 
Quicksort
QuicksortQuicksort
Quicksort
Gayathri Gaayu
 
Unit 5
Unit 5Unit 5
Unit 5
guna287176
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Vikas Sharma
 

What's hot (20)

Time complexity
Time complexityTime complexity
Time complexity
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - 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
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
time domain analysis, Rise Time, Delay time, Damping Ratio, Overshoot, Settli...
time domain analysis, Rise Time, Delay time, Damping Ratio, Overshoot, Settli...time domain analysis, Rise Time, Delay time, Damping Ratio, Overshoot, Settli...
time domain analysis, Rise Time, Delay time, Damping Ratio, Overshoot, Settli...
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Design and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. MohiteDesign and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. Mohite
 
asymptotic notation
asymptotic notationasymptotic notation
asymptotic notation
 
Intro to Sorting + Insertion Sort
Intro to Sorting + Insertion SortIntro to Sorting + Insertion Sort
Intro to Sorting + Insertion Sort
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
Algorithm in Computer, Sorting and Notations
Algorithm in Computer, Sorting  and NotationsAlgorithm in Computer, Sorting  and Notations
Algorithm in Computer, Sorting and Notations
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
Analysis of Algorithm
Analysis of AlgorithmAnalysis of Algorithm
Analysis of Algorithm
 
Quicksort
QuicksortQuicksort
Quicksort
 
Unit 5
Unit 5Unit 5
Unit 5
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 

Similar to 2 chapter2 algorithm_analysispart1

1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt
1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt
1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt
yaikobdiriba1
 
lecture 1
lecture 1lecture 1
lecture 1
sajinsc
 
analysis.ppt
analysis.pptanalysis.ppt
analysis.ppt
AarushSharma69
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
subhashchandra197
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
YekoyeTigabuYeko
 
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
Sajid Marwat
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
Deepak John
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
ChSreenivasuluReddy
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
abay golla
 
Introduction of Algorithm.pdf
Introduction of Algorithm.pdfIntroduction of Algorithm.pdf
Introduction of Algorithm.pdf
LaxmiMobile1
 
l1.ppt
l1.pptl1.ppt
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
Budditha Hettige
 
l1.ppt
l1.pptl1.ppt
l1.ppt
ImXaib
 
Welcome to Introduction to Algorithms, Spring 2004
Welcome to Introduction to Algorithms, Spring 2004Welcome to Introduction to Algorithms, Spring 2004
Welcome to Introduction to Algorithms, Spring 2004
jeronimored
 
l1.ppt
l1.pptl1.ppt
l1.ppt
ssuser1a62e1
 
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
Sreedhar Chowdam
 

Similar to 2 chapter2 algorithm_analysispart1 (20)

1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt
1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt
1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt
 
lecture 1
lecture 1lecture 1
lecture 1
 
analysis.ppt
analysis.pptanalysis.ppt
analysis.ppt
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
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
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
Introduction of Algorithm.pdf
Introduction of Algorithm.pdfIntroduction of Algorithm.pdf
Introduction of Algorithm.pdf
 
l1.ppt
l1.pptl1.ppt
l1.ppt
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
l1.ppt
l1.pptl1.ppt
l1.ppt
 
Welcome to Introduction to Algorithms, Spring 2004
Welcome to Introduction to Algorithms, Spring 2004Welcome to Introduction to Algorithms, Spring 2004
Welcome to Introduction to Algorithms, Spring 2004
 
l1.ppt
l1.pptl1.ppt
l1.ppt
 
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
 

More from SSE_AndyLi

Chapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic CircuitsChapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic Circuits
SSE_AndyLi
 
Chapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsChapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic Functions
SSE_AndyLi
 
Chapter 5 introduction to VHDL
Chapter 5 introduction to VHDLChapter 5 introduction to VHDL
Chapter 5 introduction to VHDL
SSE_AndyLi
 
Chapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational LogicChapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational Logic
SSE_AndyLi
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and Gates
SSE_AndyLi
 
Chapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital SystemsChapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital Systems
SSE_AndyLi
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst
SSE_AndyLi
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
SSE_AndyLi
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
SSE_AndyLi
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
SSE_AndyLi
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
SSE_AndyLi
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
SSE_AndyLi
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
SSE_AndyLi
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
SSE_AndyLi
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2
SSE_AndyLi
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
SSE_AndyLi
 

More from SSE_AndyLi (17)

Chapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic CircuitsChapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 07 Digital Alrithmetic and Arithmetic Circuits
 
Chapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic FunctionsChapter 06 Combinational Logic Functions
Chapter 06 Combinational Logic Functions
 
Chapter 5 introduction to VHDL
Chapter 5 introduction to VHDLChapter 5 introduction to VHDL
Chapter 5 introduction to VHDL
 
Chapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational LogicChapter 03 Boolean Algebra and Combinational Logic
Chapter 03 Boolean Algebra and Combinational Logic
 
Chapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and GatesChapter 02 Logic Functions and Gates
Chapter 02 Logic Functions and Gates
 
Chapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital SystemsChapter 01 Basic Principles of Digital Systems
Chapter 01 Basic Principles of Digital Systems
 
15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst15 chapter9 graph_algorithms_mst
15 chapter9 graph_algorithms_mst
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
 
10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues10 chapter6 heaps_priority_queues
10 chapter6 heaps_priority_queues
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
4 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart14 chapter3 list_stackqueuepart1
4 chapter3 list_stackqueuepart1
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 

Recently uploaded

Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
aisafed42
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Peter Caitens
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
Severalnines
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 

Recently uploaded (20)

Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 

2 chapter2 algorithm_analysispart1

  • 1. Chapter 2: Algorithm Analysis - I Text: Read Weiss, §2.1 – 2.4.2 1
  • 2. Definition • An Algorithm is a clearly specified set of simple instructions to be followed to solve a problem. • Once an algorithm is given and decided somehow to be correct, an important step is to determine how much in the way of resources, such as time or space, the algorithm will require. 2
  • 3. Mathematical Background • Definition 2.1. T(N) = O(f(N)) if there are c,n0 ≥0 such that T(N)≤cf(N) when N ≥ n0. • Definition 2.2. T(N) = Ω(g(N)) if there are c,n0 ≥0 such that T(N)≥cg(N) when N ≥ n0. • Definition 2.3. T(N) = Ɵ(h(N)) iff T(N)=O(h(N)) and T(N)=Ω(h(N)). • Definition 2.4. T(N) = o(p(N)) if for all c, there exists an n0 such that T(N)<cp(N) when N > n0. (T(N) = o(p(N)) if T(N)=O(p(N)) and T(N) <> Ɵ(p(N)) 3
  • 4. O (Big-Oh) Notation • Definitions establish a relative order among functions. We compare their relative rates of growth. • Example: For small values of N, T(N)=1000N is larger than f(N)=N2. For N≥n0=1000 and c= 1, T(N) ≤ cf(N). Therefore, 1000N = O(N2) (Big-Oh notation). • Big-Oh notation says that the growth rate of T(N) is less than or equal to that of f(N). T(N)=O(f(N)) means f(N) is an upper bound on T(N). 4
  • 5. Ω and Ɵ notations • T(N) = Ω(g(N)) (pronounced “omega”) says that the growth rate of T(N) is greater than or equal to that of g(N). g(N) is a lower bound on T(N). • T(N) = Ɵ(h(N)) (pronounced “theta”) says that the growth rate of T(N) equals the growth rate h(N). • T(N) = o(p(N)) (pronounced “little-oh”) says that the growth rate of T(N) is less than that of p(N). • Example: N2=O(N3), N3=Ω(N2) 5
  • 6. Rules for Asymptotic Analysis-I • Rule 1: If T1(N)=O(f(N)) and T2(N)=O(g(N)), then a) T1(N)+T2(N)=O(f(N)+g(N)) (intuitevely, max(O(f(N)), O(g(N))) b) T1(N)*T2(N)=O(f(N)*g(N)) • Rule 2: If T(N) is a polynomial of degree k, then T(N)=Ɵ(Nk). • Rule 3: logkN=O(N) for any constant k. 6
  • 7. Rules for Asymptotic Analysis-II • It is very bad style to include constants or low-order terms inside a Big-Oh. Do not say T(N)=O(2N2) or T(N)=O(N2+N). The correct form is T(N)=O(N2). • The relative growth rates of f(N) and g(N) can always be determined by limN→∞(f(N)/g(N)): (using L’Hỏpital’s rule if necessary) 1. The limit is 0: f(N)=o(g(N)) 2. The limit is c≠0: f(N)=Ɵ(g(N)) 3. The limit is ∞: g(N)=o(f(N)) 4. The limit oscillates: no relation 7 )( )(lim )( )(lim )(lim,)(lim Ng Nf NNg Nf N Ng N Nf N ′ ′ ∞→ = ∞→ ∞= ∞→ ∞= ∞→
  • 8. Rules for Asymptotic Analysis-III • Sometimes simple algebra is just sufficient. • Example: f(N)=NlogN, g(N)=N1.5 are given. Decide which of the two functions grows faster! • This amounts to comparing logN and N0.5. This, in turn, is equivalent to testing log2N and N. But we already know that N grows faster than any power of a log. • It is bad to say f(N)≤O(g(N)), because the inequality is implied by the definition. f(N)≥O(g(N)) is incorrect since it does not make sense. 8
  • 9. Model of Computation • Our model is a normal computer. • Instructions are executed sequentially. • It has a repertoire of simple instructions (addition, multiplication, comparison, assignment). • It takes one (1) time unit to execute these simple instructions (assume our model has fixed size integers and no fancy operations like matrix inversion or sorting). • It has infinite memory. • Weaknesses: disk read vs addition, page faults when memory is not infinite 9
  • 10. What to Analyze • The most important resource to analyze is generally the running time of a program. • Compiler and computer used affect it but are not taken into consideration. • The algorithm used and the input to it will be considered. • Tavg(N) (average running time: typical behavior) and Tworst(N) (worst case: it is generally the required quantity: guarantee for performance: bound for all input) running times on input size N. • The details of the programming language do not affect a Big-Oh answer. It is the algorithm that is analyzed not the program (implementation). 10
  • 11. Maximum Subsequence Sum Problem - I • Definition: Given (possibly negative) integers A1, A2, ..., AN, find the maximum value of . (For convenience, the maximum subsequence sum is 0 if all integers are negative) • Example: For input -2, 11, -4, 13, -5, -2, the answer is 20 (A2 through A4). • There are many algorithms to solve this problem. We will discuss 4 of these. 11 ∑ = j ik k A
  • 12. Maximum Subsequence Sum Problem - II • For a small amount of input, they all run in a blink of the eye. • Algorithms should not form bottlenecks.Times do not include the time to read. 12
  • 13. Maximum Subsequence Sum Problem - III -O(NlogN) Algorithm is not linear. Verify it by a straight-edge. -Relative growth rates are evident. 13
  • 14. Maximum Subsequence Sum Problem - IV • Illustrates how useless inefficient algorithms are for even moderately large amounts of input. 14
  • 15. Running Time Calculations • Several ways to estimate the running time of a program (empirical vs analytical) • Big-Oh running times. Here is a simple program fragment to calculate - The declarations count for no time. - Lines 1 and 4 count for 1 unit each - Line 3 counts for 4 units per time executed (2 multiplications, 1 addition, 1 assignment) and is executed N times for a total of 4N units. - Line 2 costs 2N+2 units (1 unit for initial assignment, N+1 units for comparison tests N units for all increments). - Total time T(N) is 6N+4 which is O(N). 15 ∑ = N i i 1 3 unsigned int sum( int n ) { unsigned int i, partial_sum; /*1*/ partial_sum = 0; /*2*/ for( i=1; i<=n; i++ ) /*3*/ partial_sum += i*i*i; /*4*/ return( partial_sum ); } Some shortcuts could be taken without affecting the final answer (Line 3 is an O(1) statement, Line 1 is insignificant compared to for loop.
  • 16. General Rules - I • RULE 1-FOR LOOPS:The running time of a for loop is at most the running time of the statements inside the for loop (including tests) times the number of iterations. • RULE 2-NESTED FOR LOOPS: Analyze these inside out. The total running time of a statement inside a group of nested for loops is the running time of the statement multiplied by the product of the sizes of all the for loops. Example: the following program fragment is O(n2): 16 for( i = 0; i < n; i++ ) for( j=0; j < n; j++ ) k++;
  • 17. General Rules - II • RULE 3-CONSECUTIVE STATEMENTS: These just add (which means that the maximum is the one that counts – Rule 1(a) on page 6). Example: the following program fragment, which has O(n) work followed by O (n2) work, is also O (n2): 17 for( i = 0; i < n; i++) a[i] = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ ) a[i] += a[j] + i + j;
  • 18. General Rules - III • RULE 4-lF/ELSE: For the fragment • the running time of an if/else statement is never more than the running time of the test plus the larger of the running times of S1 and S2. • Clearly, this can be an over-estimate in some cases, but it is never an under-estimate. 18 if( condition ) S1 else S2
  • 19. General Rules - IV • Other rules are obvious, but a basic strategy of analyzing from the inside (or deepest part) out works. If there are function calls, obviously these must be analyzed first. • If there are recursive procedures, there are several options. If it is really just a thinly veiled for loop, the analysis is usually trivial. Example: The following function is really just a simple loop and is obviously O (n): 19 unsigned int factorial( unsigned int n ) { if( n <= 1 ) return 1; else return( n * factorial(n-1) ); }
  • 20. General Rules - V • When recursion is properly used, it is difficult to convert the recursion into a simple loop structure. In this case, the analysis will involve a recurrence relation that needs to be solved. • Example: Consider the following program: - If the program is run for values of n around 40, it becomes terribly inefficient. Let T(n) be the running time for the Function fib(n). T(0)=T(1)=1 (time to do the test at Line 1 and return). For n≥2, the total time required is then T(n ) = T(n - 1) + T(n - 2) + 2 (where the 2 accounts for the work at Line 1 plus the addition at Line 3). T(n) ≥ fib(n)=fib(n-1)+fib(n-2) ≥ (3/2)n (for n > 4, by induction) • Huge amount of redundant work (violates 4th rule of recursion- compound interest rule. fib(n-1) has already computed fib(n-2) 20 unsigned int fib( unsigned int n ) { /*1*/ if( n <= 1 ) /*2*/ return 1; else /*3*/ return( fib(n-1) + fib(n-2) ); }