### Techniques to analysis running time of algorithms

• Overview of techniques

• We can use 2 general approaches to compute the running time of an algorithm:

• Direct computation: (count "absolutely")

 Count the number of operations/instructions exeucted by the program loops.

• Indirect approach: (count "relatively")

 Compare the number of operations/instructions executed by the program loop for different values of the loop-control variable This will result in a recurrence relation that you will need to solve.

• Direct approach

• For simple loops, we can count the number of times that the loop is executed and obtain the running time

• Example 1:

 ``` int N; int i; for ( i = 0; i < N; i++ ) do_marker(); ```

The following diagram shows the number of times that do_marker() is executed when N = n:

 ``` i = 0 1 2 3 ... n # do_marker(): 1 1 1 1 ... 1 ```

Count the number of 1's to find the number of times that do_marker() is executed....

Clearly: running time of this loop is equal to N

• Example 2:

 ``` int N; int i, j; for ( i = 0; i < N; i++ ) for ( j = 0; j < N; j++ ) do_marker(); ```

The following diagram shows the number of times that do_marker() is executed when N = n:

 ``` i = 0 1 2 3 ... n j = 0 0 0 0 ... 0 1 1 1 1 ... 1 | 2 2 2 2 ... 2 | V n n n n ... n ```

Count the number of red indices to find the number of times that do_marker() is executed....

Clearly: running time of this loop is equal to N2

• Indirect approach

• The indirect approach will be discussed in the next web page.