1 / 10

Statement Analysis - Critical Section

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.

janisf
Download Presentation

Statement Analysis - Critical Section

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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. 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. 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)

  5. Block #1 t1 Block #2 t2 Statement Analysis - If If/Else: if cond then S1 else S2; Max(t1,t2)

  6. 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)

  7. 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);

  8. 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 )

  9. 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. Acknowledgements • Philadephia University, Jordan • Nilagupta, Pradondet

More Related