0% found this document useful (0 votes)
35 views18 pages

08b Analyzing Iterative Constructs

The document discusses analyzing the running time of iterative programs. Simple loops with a constant number of iterations per loop have running time linear in the number of iterations. Nested loops have running time quadratic in the number of outer loop iterations. Conditional statements take the running time of the larger branch. Function calls are analyzed separately and their costs are added, unless recursion is present.

Uploaded by

Sohan Kunkerkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views18 pages

08b Analyzing Iterative Constructs

The document discusses analyzing the running time of iterative programs. Simple loops with a constant number of iterations per loop have running time linear in the number of iterations. Nested loops have running time quadratic in the number of outer loop iterations. Conditional statements take the running time of the larger branch. Function calls are analyzed separately and their costs are added, unless recursion is present.

Uploaded by

Sohan Kunkerkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

CSCE 2100: Computing Foundations 1

Analyzing Iterative Programs


Tamara Schneider
Summer 2013

Running Time of Simple


Statements
Primitive operations in O(1)

Arithmetic operations (+, %, *, -, ...)


Logical operations (&&, ||, ...)
Accessing operations (A[i], x->y, ...)
Simple assignment
Calls to library functions (scanf,
printf, ... )

Simple For-Loops [1]


for(int i=0; i<n; i++)
A[i]=0;

Constant time for assignment


Neglect checking for loop
condition etc (constant time)
Loop is executed times
executed times
3

Simple For-Loops [2]


for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
A[i][j]=0;

Neglect checking for loop


condition etc. (constant time)
Inner loop:
The inner loop is executed n
times
executed times O(n2)

Analyzing if statements
if(<condition>)
<then part>
else <else part>
Evaluation
of condition takes a constant number of

primitive operations
Assume if-part has upper bound
Assume else-part has upper bound

Whether or is larger may depend on the input size


5

If-else example
if(A[1][1] == 0)
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
A[i][j]=0;
else
for(int i=0; i<n; i++)
A[i][i] = 0;

If-part: 2 nested loops executed times each; loop body


is O(n)
O(n2) total.
Else-part: 1 loop executed times; loop body is
O(n) total
Safe upper bound: O(n2)
6

Inner Loop of Selection Sort


for(int j=i+1; j<n; j++)
if(A[j] < A[small])
small = j;

The loop body takes time O(1)


The loop is executed times
The loop starts at
The loops condition becomes false for
The loop increment is 1
O(n-i-1)
7

Selection Sort [1]

for(int i=0; i<n-1; i++){


small = i;
for(int j=i+1; j<n; j++)
if(A[j] <
A[small])
small = j;
int temp = A[small];
A[small] = A[i];
A[i] = temp;
} //for

How often
is this block
executed?

Selection Sort [2]


Complex Loops
Some loops do not specify explicitly how
many times the loop body is executed

while, do-while, some for-loops

Analyze loop to find upper bound on


number of iterations
May need to prove statement by induction

10

Complex Loop Example


n = A.size()
i = 0;
while(x != A[i] && i < n)
i++;

The

loop is at most executed times


The upper bound on the loop is

11

Programs with Function


Calls
If function call is non-recursive

Analyze function separately


Add cost of function to block

Function calls are a condition of


while- or do-while loop

Add cost of function to block

12

Example [1]
main(){
int a, n;
scanf("%d", &n);
a = foo(0,n);
printf("%d",
bar(a,n));
}

int bar(int x, int n){


for(int i=1; i<=n; i+
+)
x += i;
return x;
}

int foo(int x, int n){


for(int i=1; i<=n; i++)
x += bar(i,n);
return x;
}
13

Example [2]
main(){
int a, n;
scanf("%d", &n);
a = foo(0,n);
printf("%d",
bar(a,n));
}
int foo(int x, int n){
for(int i=1; i<=n; i++)
x += bar(i,n);
return x;
}

int bar(int x, int n){


for(int i=1; i<=n; i+
+)
x += i;
return x;
}
1. Check for recursion
(direct or indirect)

foo

mai
n

bar

14

Example [3]
main(){
int a, n;
scanf("%d", &n);
a = foo(0,n);
printf("%d",
bar(a,n));
}

int bar(int x, int n){


for(int i=1; i<=n; i+
+)
x += i;
return x;
}

2. Running time of bar


int foo(int x, int n){
for(int i=1; i<=n; i++)
x += bar(i,n);
return x;
mai
}

foo

bar

O(n)

15

Example [4]
main(){
int a, n;
scanf("%d", &n);
a = foo(0,n);
printf("%d",
bar(a,n));
}

int bar(int x, int n){


for(int i=1; i<=n; i+
+)
x += i;
return x;
}

3. Running time of foo


int foo(int x, int n){
for(int i=1; i<=n; i++)
x += bar(i,n);
return x;
mai
}

O(n2)
foo

bar

O(n)

16

Example [5]
main(){
int a, n;
scanf("%d", &n);
a = foo(0,n);
printf("%d",
bar(a,n));
}

int bar(int x, int n){


for(int i=1; i<=n; i+
+)
x += i;
return x;
}

4. Running time of main


int foo(int x, int n){
for(int i=1; i<=n; i++)
x += bar(i,n);
O(n2)
O(n2)
return x;
foo
mai
}

bar

O(n)

17

Summary
Primitive operations are considered to have a
constant running time.
Analyzing simple loops makes use of the number
of iterations and the cost of the loop body.
To analyze complex loops the worst case may
need to be assumed.
Analyzing conditional statement involves using
the worst case.
Non-recursive functions can be analyzed
independently.

18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy