Sie sind auf Seite 1von 11

Lecture No.

06

Data Structures and


algorithms

1
ALGORITHM ANALYSIS

• Sections 2.1, 2.2 & 2.3


(completely) were discussed
in the class in the last two
lectures.
2
RUNNING TIME CALCULATIONS
• There are several ways to estimate the running
time of a program. The previous table was
obtained empirically. If two programs are
expected to take similar times, probably the
bes way to decide which is faster is to code
them both and run them!

• Bad ones are removed early !

3
SIMPLE EXAMPLE
int sum( int n )
{
int partialSum;
1 partialSum = 0;
2 for( int i = 1; i <= n; ++i )
3 partialSum += i * i * i;
4 return partialSum;
}
4
SIMPLE EXAMPLE
• Declaration takes no time in the
program.

• Lines 1 and 4 count for one unit each.

• Line 3 counts for four units per time


executed (two multiplications, one addition,
and one assignment) and is executed N
times, for a total of 4N units
5
SIMPLE EXAMPLE
• Line 2 has the hidden costs of initializing i,
testing i ≤ N, and incrementing i. The total
cost of all these is 1 to initialize, N+1 for all
the tests, and N for all the increments, which
is 2N + 2.
• O(N)=6N+4; Total running time
• Not feasible to estimate every time from the
above method
• So , generalize the running time for the whole
statement or an iteration as O(1). 6
2.4.2 GENERAL RULES
• Rule 1—FOR loops
The running time of a for loop is at most the running time of the statements
inside the for loop (including tests) times the number of iterations.

• Rule 2—Nested loops


Analyze these inside out. The total running time of a statement inside a
group of nested loops is the running time of the statement multiplied by the
product of the sizes of all the loops.

• Rule 3—Consecutive Statements


These just add i.e.
If T1(N) = O(f (N)) and T2(N) = O(g(N)), then
T1(N) + T2(N) = O(f (N) + g(N))
7
2.4.2 GENERAL RULES
• Rule 4—If/Else
For the fragment
if( condition )
S1
else
S2
The running time of an if/else statement is never more than the running
time of the test plus the larger of the running times of S1 and S2.

• HOMEWORK
• long fib( int n )
• {
• 1 if( n <= 1 )
8

• 2 return 1;
2.4.2 GENERAL RULES
• HOMEWORK

long fib( int n )


{
1 if( n <= 1 )
2 return 1;
else
3 return fib( n - 1 ) + fib( n - 2 );
}

FOR THIS READ YOUR TEXT BOOK Pages 59-60.

9
3.1 ADT’s
• An abstract data type (ADT) is a set of objects together
with a set of operations.

• ADT’S implement operations but does not know how the set
of operations is implemented. Objects such as lists, sets, and
graphs,along with their operations, can be viewed as ADTs,
just as integers, reals, and booleans are data types. Integers,
reals, and booleans have operations associated with them, and
so do ADTs. For the set ADT, we might have such operations
as add, remove, size, and contains. Alternatively, we might
only want the two operations union and find, which would
define a different ADT on the set.

• There is no rule telling us which operations must be supported


10
for each ADT; this is a design decision
3.2 The List ADT’s

• Will be discussed in class room

11

Das könnte Ihnen auch gefallen