0% found this document useful (0 votes)
5 views6 pages

DSA-L11

The document discusses algorithm complexity, particularly focusing on the 'Maximum Subarray Sum' problem and its efficient solution using Kadane's Algorithm. It provides examples of different complexity classes such as constant, linear, logarithmic, and exponential functions, along with real-life analogies to illustrate these concepts. Additionally, it includes tasks for analyzing algorithm complexity and mentions polynomial functions in relation to computational growth rates.

Uploaded by

Alkit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

DSA-L11

The document discusses algorithm complexity, particularly focusing on the 'Maximum Subarray Sum' problem and its efficient solution using Kadane's Algorithm. It provides examples of different complexity classes such as constant, linear, logarithmic, and exponential functions, along with real-life analogies to illustrate these concepts. Additionally, it includes tasks for analyzing algorithm complexity and mentions polynomial functions in relation to computational growth rates.

Uploaded by

Alkit Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD

(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.


BITS-Pilani Hyderabad Campus
ALGORITHM COMPLEXITY CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
WHY IS IT SO IMPORTANT? Metaphor: Daily Budget and Spending

Problem Statement: Input :arr[]= {100, 200, 300, 400}, k = 2 Output : ???
Problem Name: "Maximum Subarray for (int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
Sum" for (int j = i; j < n; j++) { int currentSum = 0;
int currentSum = 0;

Hypothetical Example
Input: An array of integers. for(int j = i; j < n; j++) {
for (int k = i; k <= j; k++) { currentSum += arr[j];
Output: maximum sum. currentSum += arr[k]; if (currentSum>maxSum){
} maxSum = currentSum;
Constraints: if (currentSum > maxSum) { }
• Time Limit: 1 second maxSum = currentSum; } Complexity?
} }
• Memory Limit: 256 MB } for (int i = 1; i < n; i++) {
• 1 <= Array Size <= 10^6 } currentSum=max(arr[i], currentSum + arr[i]);
maxSum = max(maxSum, currentSum);
• -10^9 <= Array Element <= 10^9 Complexity? }
Efficient solution: Kadane's Algorithm using DP (Ignore –ve subarray sum): Complexity is ???
FUNCTIONS FOR ALGORITHM ANALYSIS
(Seating in the class everyday) (Reading chapters of book) (Climbing a ladder quickly) (Population growth)

Execution time
Execution time

Execution time
Execution time
O(1) O(n)
2n
log2(n)

Input size(n) Input size(n) Input size(n) Input size(n)


(Constant Function) (Linear Function) (Logarithmic Function) (Exponential Function)

Ex.s: Array indexing, Vari Ex.s: Searching in unsorted Ex.s: Finding a word in a d Ex.s: Fibonacci sequence, T
able assignment, Basic arit array, Printing all element ictionary, Treasure hunt, et owers of Hanoi, Generatin
hmetic operations, … s of a list, … c… g all subsets of set, etc…
COMPLEXITY EXAMPLES FROM REAL LIFE
1km 1km 1km • Is it Linear?
… (Ex.1)
• Total distance over time?
Mon Tues Wed
• Per day effort?

1km 2kms 3kms


… (Ex. 2)
Mon Tues Wed

Que: 1 + 4 + 9 + 16 + 25 +…n cubic {[n(n+1)(2n+1)]/6}


TASKS FOR YOU…COMPLEXITY?
function isEvenOrOdd(n) { list<int> numbers {1, 2, 3, 4}; int binarySearch(int array[], int x,
for(int number : numbers) int low, int high)
if (n%2 == 0)
{ {
return even; cout << number <<", "; while (low <= high)
else } {
int mid = low + (high - low) / 2;
return odd;
if (array[mid] == x) return mid;
} (printing out all the elements)
if (array[mid] < x)
int partition(int arr[], int low, int high) { int pivot=arr[high]; low = mid +1;
int i = (low - 1); else
for (int j = low; j <= high - 1; j++) { high = mid - 1;
if (arr[j] < pivot) { i++; swap(&arr[i], &arr[j]); } } }
swap(&arr[i + 1], &arr[high]); return (i + 1); return -1;
} }
CUBIC EXAMPLE AND POLYNOMIAL FUNCTIONS
1E+29
1E+27 Cubic
1E+25
Quadratic
No of multiplications: 1E+23
1E+21 Linear
1E+19
n n n n n 1E+17
∑∑∑1 ∑∑n

T(n)
1E+15
i=1 j=1 k=1 i=1 j=1 1E+13
1E+11
n 1E+9
∑ n2 n3 1E+7
1E+5
i=1
1E+3
1E+1
1E-1
1E-1 1E+2 1E+5 1E+8
Interestingly, all the functions that we have listed are n
part of large class of functions called, polynomials: (In this log-log graph, the slope of the line
g(n) = a0 + a1n + a2n2 + a3n3 + ...+ adnd What is d? corresponds to the growth rate)

You might also like