Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu CSCI 240 Analysis of Algorithms Statement Analysis

2 Dale Roberts Statement Analysis – Critical Section When analyzing an algorithm, we do not care about the behavior of each statement We focus our analysis on the part of the algorithm where the greatest amount of its time is spent A critical section has the following characteristics: It is an operation central to the functioning of the algorithm, and its behavior typifies the overall behavior of the algorithm It is contained inside the most deeply nested loops of the algorithm and is executed as often as any other section of the algorithm.

3 Dale Roberts Statement Analysis – Critical Section (cont) The critical section can be said to be at the "heart" of the algorithm We can characterize the overall efficiency of an algorithm by counting how many times this critical section is executed as a function of the problem size The critical section dominates the completion time If an algorithm is divided into two parts, the first taking O(f(n)) followed by a second that takes O(g(n)), then the overall complexity of the algorithm is O(max[f(n), g(n)]). The slowest and most time-consuming section determines the overall efficiency of the algorithm.

4 Dale Roberts Statement Analysis - Sequence Consecutive statements Maximum statement is the one counted e.g. a fragment with single for-loop followed by double for- loop is O(n 2 ). Block #1 Block #2 t1t1 t2t2 t 1 +t 2 = max(t 1,t 2 )

5 Dale Roberts Statement Analysis - If If/Else: if cond then S1else S2; Block #1Block #2 t1t1 t2t2 Max(t 1,t 2 )

6 Dale Roberts Statement Analysis - For For Loops Running time of a for-loop is at most the running time of the statements inside the for- loop times number of iterations for (i = sum = 0; i < n; i++) for (i = sum = 0; i < n; i++) sum += a[i]; sum += a[i]; for loop iterates n times, executes 2 assignment statements each iteration ==> asymptotic complexity of O(n)

7 Dale Roberts Statement Analysis – Nested For Nested For-Loops Analyze inside-out. Total running time is running time of the statement multiplied by product of the sizes of all the for-loops e.g. for (i =0; i < n; i++) for (j = 0, sum = a[0]; j <= i ; j++) for (j = 0, sum = a[0]; j <= i ; j++) sum += a[j]; sum += a[j]; printf("sum for subarray - through %d is %d\n", i, sum); printf("sum for subarray - through %d is %d\n", i, sum);

8 Dale Roberts Statement Analysis – Nested For (cont) )( )()1( 2 1 111 nO iO i n i n i i j n i         O  O    

9 Dale Roberts Statement Analysis – General Rules Strategy for analysis analyze from inside out analyze function calls first if recursion behaves like a for-loop, analysis is trivial; otherwise, use recurrence relation to solve

10 Dale Roberts Acknowledgements Philadephia University, Jordan Nilagupta, Pradondet


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI"

Similar presentations


Ads by Google