100 likes | 110 Views
This analysis focuses on the critical section of an algorithm, the part that dominates its completion time and determines its overall efficiency. By counting how many times the critical section is executed as a function of the problem size, we can assess the algorithm's performance. The complexity of the algorithm is determined by the slowest and most time-consuming section.
E N D
Department of Computer and Information Science,School of Science, IUPUI CSCI 240 Analysis of Algorithms Statement Analysis Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu
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.
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.
t1 Block #1 Block #2 t2 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(n2). t1+t2 = max(t1,t2)
Block #1 t1 Block #2 t2 Statement Analysis - If If/Else: if cond then S1 else S2; Max(t1,t2)
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++) sum += a[i]; • for loop iterates n times, executes 2 assignment statements each iteration ==> asymptotic complexity of O(n)
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++) sum += a[j]; printf("sum for subarray - through %d is %d\n", i, sum);
Statement Analysis – Nested For (cont) n i n å å å = ( 1 ) ( i ) O O = = = 1 1 1 i j i é ù n å = O i ê ú ë û = 1 i = 2 O ( n )
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
Acknowledgements • Philadephia University, Jordan • Nilagupta, Pradondet