SlideShare a Scribd company logo
Chapter 2: Algorithm Analysis - II
Text: Read Weiss, §2.4.3 – 2.4.6
1
Solutions for the Maximum Subsequence
Sum Problem: Algorithm 1
•exhaustively tries all possibilities: for
all combinations of all the values for
starting and ending points (i and j
respectively), the partial sum
(ThisSum) is calculated and
compared with the maximum sum
value (MaxSum) computed so far.
The running time is O(N3 ) and is
entirely due to lines 5 and 6.
• A more precise analysis;
2
int MaxSubSum1( const int A[ ], int N ) {
int ThisSum, MaxSum, i, j, k;
/* 1*/ MaxSum = 0;
/* 2*/ for( i = 0; i < N; i++ )
/* 3*/ for( j = i; j < N; j++ ) {
/* 4*/ ThisSum = 0;
/* 5*/ for( k = i; k <= j; k++ )
/* 6*/ ThisSum += A[ k ];
/* 7*/ if( ThisSum > MaxSum )
/* 8*/ MaxSum = ThisSum;
}
/* 9*/ return MaxSum;
}
6/)2233()232(2/12/)1()2/3(6/)12)(1(2/1
1
1)232(2/1
1
)2/3(
1
22/1
1
2/)1)(2(
1
0
2/))(1(
1
0
1
1
1
0
1
1
NNNNNNNNNNNN
N
i
NN
N
i
iN
N
i
i
N
i
iNiN
N
i
iNiN
N
i
N
ij
ij
N
i
N
ij
j
ik
++=+++++−++=
∑
=
+++∑
=
+−∑
=
=
∑
=
+−+−=
∑
−
=
−+−=∑
−
=
∑
−
=
+−=∑
−
=
∑
−
=
∑
=
3
Solutions for the Maximum Subsequence
Sum Problem: Algorithm 2
• We can improve upon Algorithm 1
to avoid the cubic running time by
removing a for loop. Obviously,
this is not always possible, but in
this case there are an awful lot of
unnecessary computations
present in Algorithm 1.
• Notice that
• so the computation at lines 5 and
6 in Algorithm 1 is unduly
expensive. Algorithm 2 is clearly
O(N2 ); the analysis is even
simpler than before.
int MaxSubSum2( const int A[ ], int N ) {
int ThisSum, MaxSum, i, j;
/* 1*/ MaxSum = 0;
/* 2*/ for( i = 0; i < N; i++ ) {
/* 3*/ ThisSum = 0;
/* 4*/ for( j = i; j < N; j++ ) {
/* 5*/ ThisSum += A[ j ];
/* 6*/ if( ThisSum > MaxSum )
/* 7*/ MaxSum = ThisSum;
}
}
/* 8*/ return MaxSum;
}
k i
j
Ak Aj
k i
j 1
Ak
4
Solutions for the Maximum Subsequence
Sum Problem: Algorithm 3
• It is a recursive O(N log N) algorithm
using a divide-and-conquer
strategy. Divide part: Split the
problem into two roughly equal
subproblems, each half the size of
the original. The subproblems are
then solved recursively. Conquer
part: Patch together the two
solutions of the subproblems
possibly doing a small amount of
additional work, to arrive at a
solution for the whole problem.
• The maximum subsequence sum
can (1) either occur entirely in the
left half of the input, or (2) entirely in
the right half, or (3) it crosses the
middle and is in both halves.
•Solve (1) and (2) recursively. For (3),
Add the largest sum in the first half
including the last element in the first
half and the largest sum in the
second half including the first element
in the second half.
•Example:
•(1) first half: 6 (A0 - A2), (2) second
half: 8 (A5 - A6). (3) max sum (first
half) covering the last item: 4 (A0 -
A3), max sum (second half) spanning
the first element: 7 (A4 - A6). Thus, the
max sum crossing the middle is
4+7=11 (A0 - A6). Answer!
First Half Second Half
4,-3,5,-2 -1,2,6,-2
Solutions for the Maximum Subsequence Sum
Problem: Algorithm 3 – Implementation I
/* Implementation */
static int MaxSubSum(const int A[ ], int Left, int Right) {
int MaxLeftSum, MaxRightSum;
int MaxLeftBorderSum, MaxRightBorderSum;
int LeftBorderSum, RightBorderSum;
int Center, i;
/* 1*/ if( Left == Right ) /* Base case */
/* 2*/ if( A[ Left ] > 0 )
/* 3*/ return A[ Left ];
else
/* 4*/ return 0;
/* Initial Call */
int MaxSubSum3( const int A[ ], int N ) {
return MaxSubSum( A, 0, N - 1 );
}
/* Utility Function */
static int Max3( int A, int B, int C ) {
return A > B ? A > C ? A : C : B > C ? B : C;
}
5
Solutions for the Maximum Subsequence Sum
Problem: Algorithm 3 – Implementation II
/* Implementation */
/* Calculate the center */
/* 5*/ Center = ( Left + Right ) / 2;
/* Make recursive calls */
/* 6*/ MaxLeftSum = MaxSubSum( A, Left, Center );
/* 7*/ MaxRightSum = MaxSubSum( A, Center + 1, Right );
/* Find the max subsequence sum in the left half where the */
/* subsequence spans the last element of the left half */
/* 8*/ MaxLeftBorderSum = 0; LeftBorderSum = 0;
/* 9*/ for( i = Center; i >= Left; i-- )
{
/*10*/ LeftBorderSum += A[ i ];
/*11*/ if( LeftBorderSum > MaxLeftBorderSum )
/*12*/ MaxLeftBorderSum = LeftBorderSum;
}
6
/* Implementation */
/*13*/ MaxRightBorderSum = 0; RightBorderSum = 0;
/*14*/ for( i = Center + 1; i <= Right; i++ )
{
/*15*/ RightBorderSum += A[ i ];
/*16*/ if( RightBorderSum > MaxRightBorderSum )
/*17*/ MaxRightBorderSum = RightBorderSum;
}
/* The function Max3 returns the largest of */
/* its three arguments */
/*18*/ return Max3( MaxLeftSum, MaxRightSum,
/*19*/ MaxLeftBorderSum + MaxRightBorderSum );
}
Solutions for the Maximum Subsequence Sum
Problem: Algorithm 3 – Implementation III
7
• T(n) : time to solve a maximum subsequence sum problem of size n.
• T(1) = 1; constant amount of time to execute lines 1 to 4
• Otherwise, the program must perform two recursive calls, the two for
loops between lines 9 and 17, and some small amount of
bookkeeping, such as lines 5 and 18. The two for loops combine to
touch every element from A0 to AN-1, and there is constant work
inside the loops, so the time spent in lines 9 to 17 is O(N). The
remainder of the work is performed in lines 6 and 7 to solve two
subsequence problems of size N/2 (assuming N is even). The total
time for the algorithm then obeys:
• T(1) = 1
• T(N) = 2T(N/2) + O(N)
• we can replace the O(N) term in the equation above with N; since
T(N) will be expressed in Big-Oh notation anyway, this will not affect
the answer.
• T(N) = 2(2T(N/4)+N/2) + N = 4T(N/4) + 2N
• = 4(2T(N/8)+N/4) + 2N = 8T(N/8) + 3N = ... = 2kT(N/2k) + kN
• If N = 2k then T(N) = N + kN = N log N + N = O(N log N)
Solutions for the Maximum Subsequence Sum
Problem: Algorithm 3 – Analysis
8
9
Solutions for the Maximum Subsequence
Sum Problem: Algorithm 4
• Algorithm 4 is O(N).
• Why does the algorithm
actually work? It’s an
improvement over Algorithm
2 given the following:
• Observation 1: If A[i] < 0
then it can not start an
optimal subsequence. Hence,
no negative subsequence
can be a prefix in the optimal.
• Observation 2: If
i can advance to j+1.
• Proof: Let . Any
subsequence starting at p
int MaxSubSum4(const int A[], int N)
{
int ThisSum, MaxSum, j;
/* 1*/ ThisSum = MaxSum = 0;
/* 2*/ for( j = 0; j < N; j++ )
{
/* 3*/ ThisSum += A[ j ];
/* 4*/ if( ThisSum > MaxSum )
/* 5*/ MaxSum = ThisSum;
/* 6*/ else if( ThisSum < 0 )
/* 7*/ ThisSum = 0;
}
/* 8*/ return MaxSum;
}
0][ <∑ =
j
ik
kA
[ ]jip ..1+∈
is not larger than the corresponding sequence starting at i,
since j is the first index causing sum<0).
Logarithms in the Running Time
• The most frequent appearance of logarithms
centers around the following general rule: An
algorithm is O(log n) if it takes constant (O(1))
time to cut the problem size by a fraction (which
is usually 1/2).
• On the other hand, if constant time is required to
merely reduce the problem by a constant
amount (such as to make the problem smaller by
1), then the algorithm is O(n).
• We usually presume that the input is preread
(Otherwise Ω(n)).
10
Binary Search - I
• Definition: Given an
integer x and integers
A0, A1, . . . , AN-1, which
are presorted and already
in memory, find i such that
Ai = x, or return i = -1 if x
is not in the input.
• The loop is O(1) per
iteration. It starts with
High-Low=N - 1 and ends
with High-Low ≤-1. Every
time through the loop the
value High-Low must be
at least halved from its
previous value; thus, the
loop is repeated at most
= O(log N).
11
typedef int ElementType;
#define NotFound (-1)
int BinarySearch(const ElementType A[],
ElementType X, int N){
int Low, Mid, High;
/* 1*/ Low = 0; High = N - 1;
/* 2*/ while( Low <= High ){
/* 3*/ Mid = ( Low + High ) / 2;
/* 4*/ if( A[ Mid ] < X )
/* 5*/ Low = Mid + 1;
/* 6*/ else if( A[ Mid ] > X )
/* 7*/ High = Mid - 1;
else
/* 8*/ return Mid;/* Found */
}
/* 9*/ return NotFound;
}
  2)1log( +−N
Binary Search - II
• Initially, High – Low = N – 1 = d
• Assume 2k ≤ d < 2k+1
• After each new iteration, new value for d may be one of
High – Mid – 1 or Mid – 1 – Low which are both bounded
from above as shown below:
12
 
222
1
2
)1(
222
1
2
dLowHigh
Low
LowHigh
Low
LowHigh
xxx
dLowHighLowHigh
High
LowHigh
High
=
−
=−
+
≤−−


 +
+≤≤=
−
=
+
−≤−




 +
− 



iterationsAfter,122/02
iterations1After,2212/12
...
iteration1After,212/12
Initially,122
kkd
kkd
kdk
kdk
<≤
−<−≤
<≤−
+<≤
• Hence, after k iterations, d
becomes 1. Loop iterates 2
more times where d takes on
the values 0 and -1 in this
order. Thus, it is repeated
k+2 times.
1log1log +<≤⇒+<≤ 





kdkkdk ≤
Euclid’s Algorithm
13
• It computes the greatest common
divisor. The greatest common
divisor (gcd) of two integers is the
largest integer that divides both.
Thus, gcd (50, 15) = 5.
• It computes gcd(M, N), assuming
M≥ N (If N > M, the first iteration of
the loop swaps them).
• Fact: If M>N, then M mod N < M/2
• Proof: There are two cases:
• If N≤M/2, then since the remainder
is always smaller than N, the
theorem is true for this case.
• If N>M/2,But then N goes into M
once with a remainder M-N<M/2,
proving the theorem.
unsigned int gcd(unsigned int M,
unsigned int N)
{
unsigned int Rem;
/* 1*/ while( N > 0 )
{
/* 2*/ Rem = M % N;
/* 3*/ M = N;
/* 4*/ N = Rem;
}
/* 5*/ return M;
}
iteration M N
After 1st N rem1=M mod N < M/2
rem1 < N
After 2nd rem1<M/2 rem2=N mod rem1 < N/2
• Thus, the Algorithm takes O(log N)
Exponentiation - I
• Algorithm pow(X, N) raises an
integer to an integer power.
• Count the number of
multiplications as the
measurement of running time.
• XN : N -1 multiplications.
• Lines 1 to 4 handle the base
case of the recursion.
• XN=XN/2*XN/2 if N is even
• XN=X(N-1)/2*X(N-1)/2*X if N is odd
• # of multiplications required is
clearly at most 2 log N, because
at most two multiplications are
required to halve the problem.
14
#define IsEven( N ) (( N )%2==0)
long int pow( long int X,
unsigned int N )
{
/* 1*/ if( N == 0 )
/* 2*/ return 1;
/* 3*/ if( N == 1 )
/* 4*/ return X;
/* 5*/ if( IsEven( N ) )
/* 6*/ return pow(X*X, N/2);
else
/* 7*/ return pow(X*X, N/2)*X;
}
15
Exponentiation - II
• It is interesting to note how much the code can be tweaked.
• Lines 3 and 4 are unnecessary (Line 7 does the right thing).
• Line 7
/* 7*/ return pow(X*X, N/2)*X;
can be rewritten as
/* 7*/ return pow(X, N-1)*X;
• Line 6, on the other hand,
/* 6*/ return pow(X*X, N/2);
cannot be substituted by any of the following:
/*6a*/ return( pow( pow( X, 2 ), N/2 ) );
/*6b*/ return( pow( pow( X, N/2 ), 2 ) );
/*6c*/ return( pow( X, N/2 ) * pow( X, N/2 ) );
Both lines 6a and 6b are incorrect because pow(X, 2) can not make any
progress and an infinite loop results. Using line 6c affects the efficiency,
because there are now two recursive calls of size N/2 instead of only one.
An analysis will show that the running time is no longer O(log N).

More Related Content

PPT
PPTX
Performance analysis and randamized agoritham
PPTX
Asymptotic Notation
PPT
how to calclute time complexity of algortihm
PDF
Model of Computation-Turing Machine
PPT
Fundamentals of the Analysis of Algorithm Efficiency
PDF
Asymptotic Analysis
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Performance analysis and randamized agoritham
Asymptotic Notation
how to calclute time complexity of algortihm
Model of Computation-Turing Machine
Fundamentals of the Analysis of Algorithm Efficiency
Asymptotic Analysis
Algorithm And analysis Lecture 03& 04-time complexity.

What's hot (20)

PPT
Numerical Methods
PPT
Time andspacecomplexity
PDF
Romberg’s method
PPTX
Mathematical Analysis of Recursive Algorithm.
PPT
Asymptotic analysis
PPTX
asymptotic analysis and insertion sort analysis
PPT
Analysis of Algorithum
PPT
Introduction to Algorithms
PPTX
Lecture 5: Asymptotic analysis of algorithms
PPT
Algorithm.ppt
PDF
Time complexity (linear search vs binary search)
PPTX
Algorithm big o
PDF
Lecture 4 asymptotic notations
PPT
P1121133705
DOC
Time and space complexity
PPT
Analysis of Algorithm
PPT
Chap09alg
PDF
Asymptotic Notation
PPT
Asymptotic Notation and Complexity
Numerical Methods
Time andspacecomplexity
Romberg’s method
Mathematical Analysis of Recursive Algorithm.
Asymptotic analysis
asymptotic analysis and insertion sort analysis
Analysis of Algorithum
Introduction to Algorithms
Lecture 5: Asymptotic analysis of algorithms
Algorithm.ppt
Time complexity (linear search vs binary search)
Algorithm big o
Lecture 4 asymptotic notations
P1121133705
Time and space complexity
Analysis of Algorithm
Chap09alg
Asymptotic Notation
Asymptotic Notation and Complexity
Ad

Similar to 3 chapter2 algorithm_analysispart2 (20)

PDF
Data Structure & Algorithms - Mathematical
PPTX
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
PPTX
UNIT I- Session 3.pptx
PPT
How to calculate complexity in Data Structure
PPTX
01 - DAA - PPT.pptx
PDF
A* and Min-Max Searching Algorithms in AI , DSA.pdf
PPTX
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
PPTX
week1 cs72 module 01 temp check 190.pptx
PPTX
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
PPT
Time complexity.ppt
PPT
Time complexity.pptr56435 erfgegr t 45t 35
PPT
Lecture#9
PPT
Lec03 04-time complexity
PPTX
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
PPTX
Data Structure Algorithm -Algorithm Complexity
PPT
Big-O notations, Algorithm and complexity analaysis
PPTX
Data structures notes for college students btech.pptx
PDF
Introduction to Greedy method, 0/1 Knapsack problem
PPT
Lecture 8 dynamic programming
Data Structure & Algorithms - Mathematical
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
UNIT I- Session 3.pptx
How to calculate complexity in Data Structure
01 - DAA - PPT.pptx
A* and Min-Max Searching Algorithms in AI , DSA.pdf
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
week1 cs72 module 01 temp check 190.pptx
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
Time complexity.ppt
Time complexity.pptr56435 erfgegr t 45t 35
Lecture#9
Lec03 04-time complexity
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Data Structure Algorithm -Algorithm Complexity
Big-O notations, Algorithm and complexity analaysis
Data structures notes for college students btech.pptx
Introduction to Greedy method, 0/1 Knapsack problem
Lecture 8 dynamic programming
Ad

More from SSE_AndyLi (17)

PDF
Chapter 07 Digital Alrithmetic and Arithmetic Circuits
PDF
Chapter 06 Combinational Logic Functions
PDF
Chapter 5 introduction to VHDL
PDF
Chapter 03 Boolean Algebra and Combinational Logic
PDF
Chapter 02 Logic Functions and Gates
PDF
Chapter 01 Basic Principles of Digital Systems
PDF
15 chapter9 graph_algorithms_mst
PDF
14 chapter9 graph_algorithmstopologicalsort_shortestpath
PDF
10 chapter6 heaps_priority_queues
PDF
9 chapter4 trees_avl
PDF
8 chapter4 trees_bst
PDF
7 chapter4 trees_binary
PDF
6 chapter3 list_stackqueuepart3
PDF
5 chapter3 list_stackqueuepart2
PDF
4 chapter3 list_stackqueuepart1
PDF
2 chapter2 algorithm_analysispart1
PDF
1 chapter1 introduction
Chapter 07 Digital Alrithmetic and Arithmetic Circuits
Chapter 06 Combinational Logic Functions
Chapter 5 introduction to VHDL
Chapter 03 Boolean Algebra and Combinational Logic
Chapter 02 Logic Functions and Gates
Chapter 01 Basic Principles of Digital Systems
15 chapter9 graph_algorithms_mst
14 chapter9 graph_algorithmstopologicalsort_shortestpath
10 chapter6 heaps_priority_queues
9 chapter4 trees_avl
8 chapter4 trees_bst
7 chapter4 trees_binary
6 chapter3 list_stackqueuepart3
5 chapter3 list_stackqueuepart2
4 chapter3 list_stackqueuepart1
2 chapter2 algorithm_analysispart1
1 chapter1 introduction

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
top salesforce developer skills in 2025.pdf
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Nekopoi APK 2025 free lastest update
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Strategies for Manufacturing Companies
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
history of c programming in notes for students .pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
top salesforce developer skills in 2025.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Nekopoi APK 2025 free lastest update
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How to Migrate SBCGlobal Email to Yahoo Easily
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Odoo Companies in India – Driving Business Transformation.pdf
Reimagine Home Health with the Power of Agentic AI​
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Strategies for Manufacturing Companies
Lecture 3: Operating Systems Introduction to Computer Hardware Systems

3 chapter2 algorithm_analysispart2

  • 1. Chapter 2: Algorithm Analysis - II Text: Read Weiss, §2.4.3 – 2.4.6 1
  • 2. Solutions for the Maximum Subsequence Sum Problem: Algorithm 1 •exhaustively tries all possibilities: for all combinations of all the values for starting and ending points (i and j respectively), the partial sum (ThisSum) is calculated and compared with the maximum sum value (MaxSum) computed so far. The running time is O(N3 ) and is entirely due to lines 5 and 6. • A more precise analysis; 2 int MaxSubSum1( const int A[ ], int N ) { int ThisSum, MaxSum, i, j, k; /* 1*/ MaxSum = 0; /* 2*/ for( i = 0; i < N; i++ ) /* 3*/ for( j = i; j < N; j++ ) { /* 4*/ ThisSum = 0; /* 5*/ for( k = i; k <= j; k++ ) /* 6*/ ThisSum += A[ k ]; /* 7*/ if( ThisSum > MaxSum ) /* 8*/ MaxSum = ThisSum; } /* 9*/ return MaxSum; } 6/)2233()232(2/12/)1()2/3(6/)12)(1(2/1 1 1)232(2/1 1 )2/3( 1 22/1 1 2/)1)(2( 1 0 2/))(1( 1 0 1 1 1 0 1 1 NNNNNNNNNNNN N i NN N i iN N i i N i iNiN N i iNiN N i N ij ij N i N ij j ik ++=+++++−++= ∑ = +++∑ = +−∑ = = ∑ = +−+−= ∑ − = −+−=∑ − = ∑ − = +−=∑ − = ∑ − = ∑ =
  • 3. 3 Solutions for the Maximum Subsequence Sum Problem: Algorithm 2 • We can improve upon Algorithm 1 to avoid the cubic running time by removing a for loop. Obviously, this is not always possible, but in this case there are an awful lot of unnecessary computations present in Algorithm 1. • Notice that • so the computation at lines 5 and 6 in Algorithm 1 is unduly expensive. Algorithm 2 is clearly O(N2 ); the analysis is even simpler than before. int MaxSubSum2( const int A[ ], int N ) { int ThisSum, MaxSum, i, j; /* 1*/ MaxSum = 0; /* 2*/ for( i = 0; i < N; i++ ) { /* 3*/ ThisSum = 0; /* 4*/ for( j = i; j < N; j++ ) { /* 5*/ ThisSum += A[ j ]; /* 6*/ if( ThisSum > MaxSum ) /* 7*/ MaxSum = ThisSum; } } /* 8*/ return MaxSum; } k i j Ak Aj k i j 1 Ak
  • 4. 4 Solutions for the Maximum Subsequence Sum Problem: Algorithm 3 • It is a recursive O(N log N) algorithm using a divide-and-conquer strategy. Divide part: Split the problem into two roughly equal subproblems, each half the size of the original. The subproblems are then solved recursively. Conquer part: Patch together the two solutions of the subproblems possibly doing a small amount of additional work, to arrive at a solution for the whole problem. • The maximum subsequence sum can (1) either occur entirely in the left half of the input, or (2) entirely in the right half, or (3) it crosses the middle and is in both halves. •Solve (1) and (2) recursively. For (3), Add the largest sum in the first half including the last element in the first half and the largest sum in the second half including the first element in the second half. •Example: •(1) first half: 6 (A0 - A2), (2) second half: 8 (A5 - A6). (3) max sum (first half) covering the last item: 4 (A0 - A3), max sum (second half) spanning the first element: 7 (A4 - A6). Thus, the max sum crossing the middle is 4+7=11 (A0 - A6). Answer! First Half Second Half 4,-3,5,-2 -1,2,6,-2
  • 5. Solutions for the Maximum Subsequence Sum Problem: Algorithm 3 – Implementation I /* Implementation */ static int MaxSubSum(const int A[ ], int Left, int Right) { int MaxLeftSum, MaxRightSum; int MaxLeftBorderSum, MaxRightBorderSum; int LeftBorderSum, RightBorderSum; int Center, i; /* 1*/ if( Left == Right ) /* Base case */ /* 2*/ if( A[ Left ] > 0 ) /* 3*/ return A[ Left ]; else /* 4*/ return 0; /* Initial Call */ int MaxSubSum3( const int A[ ], int N ) { return MaxSubSum( A, 0, N - 1 ); } /* Utility Function */ static int Max3( int A, int B, int C ) { return A > B ? A > C ? A : C : B > C ? B : C; } 5
  • 6. Solutions for the Maximum Subsequence Sum Problem: Algorithm 3 – Implementation II /* Implementation */ /* Calculate the center */ /* 5*/ Center = ( Left + Right ) / 2; /* Make recursive calls */ /* 6*/ MaxLeftSum = MaxSubSum( A, Left, Center ); /* 7*/ MaxRightSum = MaxSubSum( A, Center + 1, Right ); /* Find the max subsequence sum in the left half where the */ /* subsequence spans the last element of the left half */ /* 8*/ MaxLeftBorderSum = 0; LeftBorderSum = 0; /* 9*/ for( i = Center; i >= Left; i-- ) { /*10*/ LeftBorderSum += A[ i ]; /*11*/ if( LeftBorderSum > MaxLeftBorderSum ) /*12*/ MaxLeftBorderSum = LeftBorderSum; } 6
  • 7. /* Implementation */ /*13*/ MaxRightBorderSum = 0; RightBorderSum = 0; /*14*/ for( i = Center + 1; i <= Right; i++ ) { /*15*/ RightBorderSum += A[ i ]; /*16*/ if( RightBorderSum > MaxRightBorderSum ) /*17*/ MaxRightBorderSum = RightBorderSum; } /* The function Max3 returns the largest of */ /* its three arguments */ /*18*/ return Max3( MaxLeftSum, MaxRightSum, /*19*/ MaxLeftBorderSum + MaxRightBorderSum ); } Solutions for the Maximum Subsequence Sum Problem: Algorithm 3 – Implementation III 7
  • 8. • T(n) : time to solve a maximum subsequence sum problem of size n. • T(1) = 1; constant amount of time to execute lines 1 to 4 • Otherwise, the program must perform two recursive calls, the two for loops between lines 9 and 17, and some small amount of bookkeeping, such as lines 5 and 18. The two for loops combine to touch every element from A0 to AN-1, and there is constant work inside the loops, so the time spent in lines 9 to 17 is O(N). The remainder of the work is performed in lines 6 and 7 to solve two subsequence problems of size N/2 (assuming N is even). The total time for the algorithm then obeys: • T(1) = 1 • T(N) = 2T(N/2) + O(N) • we can replace the O(N) term in the equation above with N; since T(N) will be expressed in Big-Oh notation anyway, this will not affect the answer. • T(N) = 2(2T(N/4)+N/2) + N = 4T(N/4) + 2N • = 4(2T(N/8)+N/4) + 2N = 8T(N/8) + 3N = ... = 2kT(N/2k) + kN • If N = 2k then T(N) = N + kN = N log N + N = O(N log N) Solutions for the Maximum Subsequence Sum Problem: Algorithm 3 – Analysis 8
  • 9. 9 Solutions for the Maximum Subsequence Sum Problem: Algorithm 4 • Algorithm 4 is O(N). • Why does the algorithm actually work? It’s an improvement over Algorithm 2 given the following: • Observation 1: If A[i] < 0 then it can not start an optimal subsequence. Hence, no negative subsequence can be a prefix in the optimal. • Observation 2: If i can advance to j+1. • Proof: Let . Any subsequence starting at p int MaxSubSum4(const int A[], int N) { int ThisSum, MaxSum, j; /* 1*/ ThisSum = MaxSum = 0; /* 2*/ for( j = 0; j < N; j++ ) { /* 3*/ ThisSum += A[ j ]; /* 4*/ if( ThisSum > MaxSum ) /* 5*/ MaxSum = ThisSum; /* 6*/ else if( ThisSum < 0 ) /* 7*/ ThisSum = 0; } /* 8*/ return MaxSum; } 0][ <∑ = j ik kA [ ]jip ..1+∈ is not larger than the corresponding sequence starting at i, since j is the first index causing sum<0).
  • 10. Logarithms in the Running Time • The most frequent appearance of logarithms centers around the following general rule: An algorithm is O(log n) if it takes constant (O(1)) time to cut the problem size by a fraction (which is usually 1/2). • On the other hand, if constant time is required to merely reduce the problem by a constant amount (such as to make the problem smaller by 1), then the algorithm is O(n). • We usually presume that the input is preread (Otherwise Ω(n)). 10
  • 11. Binary Search - I • Definition: Given an integer x and integers A0, A1, . . . , AN-1, which are presorted and already in memory, find i such that Ai = x, or return i = -1 if x is not in the input. • The loop is O(1) per iteration. It starts with High-Low=N - 1 and ends with High-Low ≤-1. Every time through the loop the value High-Low must be at least halved from its previous value; thus, the loop is repeated at most = O(log N). 11 typedef int ElementType; #define NotFound (-1) int BinarySearch(const ElementType A[], ElementType X, int N){ int Low, Mid, High; /* 1*/ Low = 0; High = N - 1; /* 2*/ while( Low <= High ){ /* 3*/ Mid = ( Low + High ) / 2; /* 4*/ if( A[ Mid ] < X ) /* 5*/ Low = Mid + 1; /* 6*/ else if( A[ Mid ] > X ) /* 7*/ High = Mid - 1; else /* 8*/ return Mid;/* Found */ } /* 9*/ return NotFound; }   2)1log( +−N
  • 12. Binary Search - II • Initially, High – Low = N – 1 = d • Assume 2k ≤ d < 2k+1 • After each new iteration, new value for d may be one of High – Mid – 1 or Mid – 1 – Low which are both bounded from above as shown below: 12   222 1 2 )1( 222 1 2 dLowHigh Low LowHigh Low LowHigh xxx dLowHighLowHigh High LowHigh High = − =− + ≤−−    + +≤≤= − = + −≤−      + −     iterationsAfter,122/02 iterations1After,2212/12 ... iteration1After,212/12 Initially,122 kkd kkd kdk kdk <≤ −<−≤ <≤− +<≤ • Hence, after k iterations, d becomes 1. Loop iterates 2 more times where d takes on the values 0 and -1 in this order. Thus, it is repeated k+2 times. 1log1log +<≤⇒+<≤       kdkkdk ≤
  • 13. Euclid’s Algorithm 13 • It computes the greatest common divisor. The greatest common divisor (gcd) of two integers is the largest integer that divides both. Thus, gcd (50, 15) = 5. • It computes gcd(M, N), assuming M≥ N (If N > M, the first iteration of the loop swaps them). • Fact: If M>N, then M mod N < M/2 • Proof: There are two cases: • If N≤M/2, then since the remainder is always smaller than N, the theorem is true for this case. • If N>M/2,But then N goes into M once with a remainder M-N<M/2, proving the theorem. unsigned int gcd(unsigned int M, unsigned int N) { unsigned int Rem; /* 1*/ while( N > 0 ) { /* 2*/ Rem = M % N; /* 3*/ M = N; /* 4*/ N = Rem; } /* 5*/ return M; } iteration M N After 1st N rem1=M mod N < M/2 rem1 < N After 2nd rem1<M/2 rem2=N mod rem1 < N/2 • Thus, the Algorithm takes O(log N)
  • 14. Exponentiation - I • Algorithm pow(X, N) raises an integer to an integer power. • Count the number of multiplications as the measurement of running time. • XN : N -1 multiplications. • Lines 1 to 4 handle the base case of the recursion. • XN=XN/2*XN/2 if N is even • XN=X(N-1)/2*X(N-1)/2*X if N is odd • # of multiplications required is clearly at most 2 log N, because at most two multiplications are required to halve the problem. 14 #define IsEven( N ) (( N )%2==0) long int pow( long int X, unsigned int N ) { /* 1*/ if( N == 0 ) /* 2*/ return 1; /* 3*/ if( N == 1 ) /* 4*/ return X; /* 5*/ if( IsEven( N ) ) /* 6*/ return pow(X*X, N/2); else /* 7*/ return pow(X*X, N/2)*X; }
  • 15. 15 Exponentiation - II • It is interesting to note how much the code can be tweaked. • Lines 3 and 4 are unnecessary (Line 7 does the right thing). • Line 7 /* 7*/ return pow(X*X, N/2)*X; can be rewritten as /* 7*/ return pow(X, N-1)*X; • Line 6, on the other hand, /* 6*/ return pow(X*X, N/2); cannot be substituted by any of the following: /*6a*/ return( pow( pow( X, 2 ), N/2 ) ); /*6b*/ return( pow( pow( X, N/2 ), 2 ) ); /*6c*/ return( pow( X, N/2 ) * pow( X, N/2 ) ); Both lines 6a and 6b are incorrect because pow(X, 2) can not make any progress and an infinite loop results. Using line 6c affects the efficiency, because there are now two recursive calls of size N/2 instead of only one. An analysis will show that the running time is no longer O(log N).