Presentation is loading. Please wait.

Presentation is loading. Please wait.

At the end of this session, learner will be able to:

Similar presentations


Presentation on theme: "At the end of this session, learner will be able to:"— Presentation transcript:

1 17CS1102 (Data Structures) Topic: Introduction to Analysis of Algorithms Indicator:1
At the end of this session, learner will be able to: 1. Define Algorithm, Pseudo code 2. Understand how to calculate Running time for an Iterative algorithm.

2 Course Motivation Need to run computer programs efficiently!
Accepts Input (Data) Performs a Sequence of action with the input Generates Output (Data) Efficient Management of Data (Data Structures) Efficient Sequence of Actions Algorithms Design an efficient algorithm Use good data structures

3 Algorithm Data Structure An algorithm is a step-by-step procedure for
solving a problem in a finite amount of time. Data Structure A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. Program= Data Structure + Algorithm

4 Running Time The running time of an algorithm typically grows with the input size. We focus on the worst case running time -Easier to analyze In order to compare two algorithms, the same hardware and software environments must be used

5 Machine Independent Analysis
We assume that every basic operation takes constant time: Example Basic Operations: Addition, Subtraction, Multiplication, Memory Access Non-basic Operations: Sorting, Searching Efficiency of an algorithm is the number of basic operations it performs We do not distinguish between the basic operations.

6 Theoretical Analysis Uses a high-level description of the algorithm instead of an implementation Characterizes running time as a function of the input size, n. Takes into account all possible inputs Allows us to evaluate the speed of an algorithm independent of the hardware/software environment

7 Pseudocode High-level description of an algorithm
More structured than English prose Less detailed than a program Preferred notation for describing algorithms Hides program design issues

8 Primitive Operations Basic computations performed by an algorithm
Identifiable in pseudocode Largely independent from the programming language Assumed to take a constant amount of time in the RAM model

9 Counting Primitive Operations
By inspecting the pseudocode, we can determine the maximum number of primitive operations executed by an algorithm, as a function of the input size

10 Counting Primitive Operations List
Evaluating an expression Assigning a value to a variable Indexing into an array Calling a method Returning from a method Following an object reference Comparing two numbers

11 Counting Primitive Operations Example -1
By inspecting the pseudo code, we can determine the maximum number of primitive/basic operations executed by an algorithm, as a function of the input size Algorithm arrayMax(A, n) currentMax  A[0] # operations for (i =1; i<n; i++) n (i=1 once, i<n n times, i++ (n-1) times:post increment) if A[i]  currentMax then 2(n  1) currentMax  A[i] 2(n  1) return currentMax Total 6n 1

12 Estimating Running Time
Algorithm arrayMax executes 6n  1 primitive operations in the worst case. Define: a = Time taken by the fastest primitive operation b = Time taken by the slowest primitive operation Let T(n) be worst-case time of arrayMax. Then a (6n  1)  T(n)  b(6n  1 ) Hence, the running time T(n) is bounded by two linear functions

13 Growth Rate of Running Time
Changing the hardware/ software Environment Affects T(n) by a constant factor, but Does not alter the growth rate of T(n) The linear growth rate of the running time T(n) is an intrinsic property of algorithm arrayMax

14 Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size Basic operation: the operation that contributes most towards the running time of the algorithm T(n) ≈ copC(n) running time execution time for basic operation Number of times basic operation is executed input size

15 Best-case, average-case, worst-case
For some algorithms efficiency depends on form of input: Worst case: Cworst(n) – maximum over inputs of size n Best case: Cbest(n) – minimum over inputs of size n Average case: Cavg(n) – “average” over inputs of size n Number of times the basic operation will be executed on typical input NOT the average of worst and best case Expected number of basic operations considered as a random variable under some assumption about the probability distribution of all possible inputs

16 Counting Primitive Operations Example -2
Algorithm mean() Operations n = read input from user 1 sum = 0 1 i = 0 1 while i < n n+1 number = read input from user n*2 sum = sum + number n*2 i = i + 1 n*2 mean = sum / n 2 Total 6n+6

17 Compute the big-Oh running time of the following C++ code segment:
for (i = 2; i < n; i++) { 1+n-2+1 sum += i; (n-2)2*2 } total 5n-8 The number of iterations of a for loop is equal to the top index of the loop minus the bottom index, plus one more instruction to account for the final conditional test. Note: if the for loop terminating condition is i <= n, rather than i < n, then the number of times the conditional test is performed is: ((top_index + 1) – bottom_index) + 1)

18 Compute the big-Oh running time of the following C++ code segment:
for (i = 0; i < n; i++) { 1+n+1 for (j = 0; i < n; i++) { n(1+n+1) sum += i; n*n*2 } n*n } n total 5n2+5n+2

19 Time efficiency of nonrecursive algorithms
General Plan for Analysis Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best cases for input of size n Set up a sum for the number of times the basic operation is executed Simplify the sum using standard formulas and rules.

20 Algorithm Complexity Analysis
diff = sum = 0; For (k=0: k < N; k++) sumsum + 1; diff diff - 1; For (k=0: k < 3N; k++) sumsum - 1;

21 First line takes 2 basic steps
Every iteration of first loop takes 2 basic steps. First loop runs N times Every iteration of second loop takes 1 basic step Second loop runs for 3N times Overall, 2 + 2N + 3N steps This is O(N)

22 Rules Complexity of a loop:
O(Number of iterations in a loop * maximum complexity of each iteration) Nested Loops: Analyze the innermost loop first, complexity of next outer loop = number of iterations in this loop * complexity of inner loop, etc….. sum = 0; For (i=0; i < N; i++) For (j=0; j < N; j++) sumsum + 1;

23 If (Condition) S1 Else S2 Maximum of the two If (yes)
print(1,2,….1000N) Else print(1,2,….N2)

24 Maximum Subsequence Problem
There is an array of N elements Need to find i, j such that the sum of all elements between the ith and jth position is maximum for all such sums Maxsum = 0; For (i=0; i < N; i++) For (j=i; j < N; j++) { Thissum = sum of all elements between ith and jth positions; Maxsum = max(Thissum, Maxsum);}

25 Analysis Inner loop: j=iN-1 (j-i + 1) = (N – i + 1)(N-i)/2
Outer Loop: i=0N-1 (N – i + 1)(N-i)/2 = (N3 + 3N2 + 2N)/6

26 Maxsum = 0; For (i=0; i < N; i++) For (Thissum=0;j=i; j < N; j++) { Thissum = Thissum + A[i]; Maxsum = max(Thissum, Maxsum);} Complexity?

27 Divide and Conquer Break a big problem into two small sub-problems
Solve each of them efficiently. Combine the two solutions

28 Maximum subsequence sum by divide and conquer
Divide the array into two parts: left part, right part Max. subsequence lies completely in left, or completely in right or spans the middle. If it spans the middle, then it includes the max subsequence in the left ending at the last element and the max subsequence in the right starting from the center

29 Max subsequence sum for first half = 6 second half = 8
4 – – Max subsequence sum for first half = 6 second half = 8 Max subsequence sum for first half ending at the last element is 4 Max subsequence sum for sum second half starting at the first element is 7 Max subsequence sum spanning the middle is ? Max subsequence spans the middle

30 Maxsubsum(A[], left, right)
{ if left = right, maxsum = max(A[left], 0); Center = (left + right)/2 maxleftsum = maxsubsum(A[],left, center); maxrightsum = maxsubsum(A[],center+1,right); maxleftbordersum = 0; leftbordersum = 0; for (i=center; i>=left; i--) leftbordersum+=A[i]; Maxleftbordersum=max(maxleftbordersum, leftbordersum);

31 Find maxrightbordersum…..
return(max(maxleftsum, maxrightsum, maxrightbordersum + maxleftbordersum);

32 Linear Complexity Algorithm
Maxsum = 0; Thissum = 0; For (j=0; j<N; j++) { Thissum = Thissum + A[j]; If (Thissum  0), Thissum = 0; If (Maxsum  Thissum), Maxsum = Thissum; } O(N) complexity

33 Binary Search You have a sorted list of numbers
You need to search the list for the number If the number exists find its position. If the number does not exist you need to detect that

34 Search(num, A[],left, right)
{ if (left = right) if (A[left ]=num) return(left) and exit; else conclude NOT PRESENT and exit; } center = (left + right)/2; If (A[center]  num) Search(num,A[],center + 1,right); If (A[center]>num) Search(num,A[],left,center ); If (A[center]=num) return(center) and exit;


Download ppt "At the end of this session, learner will be able to:"

Similar presentations


Ads by Google