Docsity

Statement Analysis: Identifying Critical Sections and Complexity in Algorithms, Slides of Computer Science

Partial preview of the text

Download Statement Analysis: Identifying Critical Sections and Complexity in Algorithms and more Slides Computer Science in PDF only on Docsity!

Analysis of Algorithms

Statement Analysis

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 - 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 #

Block #

t 1

t 2

t 1 +t 2 = max(t 1 ,t 2 )

Statement Analysis - If

If/Else:

if cond then

S

else

S2; Block #1 t 1 Block #2 t 2 Max(t 1 ,t2)

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)

2

1

1 1 1

O n

O i

i

n

i

n

i

i

j

n

i

O = O

=

= = =