Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views9 pages

Algo Run Time

The document discusses performance categories of algorithms, particularly focusing on the Towers of Hanoi problem, which involves moving disks between towers with exponential time complexity. It provides examples of algorithm runtimes and compares reasonable versus unreasonable algorithms based on their time complexity. Additionally, it includes exercises related to algorithm performance and binary tree operations.

Uploaded by

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

Algo Run Time

The document discusses performance categories of algorithms, particularly focusing on the Towers of Hanoi problem, which involves moving disks between towers with exponential time complexity. It provides examples of algorithm runtimes and compares reasonable versus unreasonable algorithms based on their time complexity. Additionally, it includes exercises related to algorithm performance and binary tree operations.

Uploaded by

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

Performance Categories An Exponential Algorithm

of Algorithms Towers of Hanoi Problem: involves moving a


specified number of disks (N) that are all different
• Sublinear - O(logN) sizes from one tower to another.

• Linear - O(N)
• Nearly linear - O(NlogN)
• Quadratic - O(N2)
• Exponential - O(2N)
O(N!)
O(N N) void tower(int n, char start, char finish, char temp)
{
if (n == 1)
printf(“Move from %c to %c\n”, start, finish);
else {
tower(n-1, start, temp, finish);
printf(“Move from %c to %c \n”, start, finish);
tower(n-1, temp, finish, start);
}
}

1 2
Towers of Hanoi – Runtime

• For 3 rings - 7 operations


o Cost = (2N) – 1
• Each time we increment N by 1, we double the amount
of work
• Run-time trace of function tower with 4 disks:

Original
call with
n=4

Call with Call with


n =3 n=3

Call with Call with Call with Call with


n =2 n= 2 n=2 n=2

Call with Call with Call with Call with Call with Call with Call with Call with
n=1 n =1 n=1 n=1 n =1 n=1 n =1 n =1

3 4
Effects of Exponents Reasonable vs Unreasonable
N
Consider Towers of Hanoi (or other 2 Reasonable Algorithms...
algorithm) for N of only 256: • Have N only as a polynomial factor
- O (logN)
Time cost is a number with 78 digits to - O (N)
the left of the decimal. - O (NK ) where K is a constant

For comparison: Unreasonable Algorithms...


• Number of microseconds since the Big • Have N as an exponential factor
Bang: a number with 24 digits. - O (2N)
• Number of protons (est’d) in the known - O (N!)
universe: a number with 77 digits. - O (NN)

5 6
Example Problems Answers
1. Algorithm A runs in O(N2 ) time, and for an input size of 4,
1. Algorithm A runs in O(N2 ) time, and for an input size of 4, the algorithm runs in 10 milliseconds, how long can you
the algorithm runs in 10 milliseconds, how long can you expect it to take to run on an input size of 16 ?
expect it to take to run on an input size of 16 ?
42 16 2
2. Algorithm A runs in O(log2N) time, and for an input size of ----- = ------- => x =160ms
16, the algorithm runs in 28 milliseconds, how long can you 10ms x
expect it to take to run on an input size of 64 ?
2. Algorithm A runs in O(log2N) time, and for an input size of
3. Algorithm A runs in O(N3 ) time. For an input size of 10, the 16, the algorithm runs in 28 milliseconds, how long can you
algorithm runs in 7 milliseconds. For another input size, the expect it to take to run on an input size of 64 ?
algorithm takes 189 milliseconds. What was that input size?

log 16 log64
4. For an O(Nk ) algorithm, where k is a positive rational ------------ = ------------ => x =42 ms
number, a friend tells you that instance of size M took 16 28 ms x
seconds to run. You run an instance of size 4M and find that
it takes 256 seconds to run. What is the value of k?
3. Algorithm A runs in O(N3 ) time. For an input size of 10, the
algorithm runs in 7 milliseconds. For another input size, the
5. Algorithm A runs in O(N3 ) time and Algorithm B solves the algorithm takes 189 milliseconds. What was that input size?
same problem in O(N2 ) time. If algorithm A takes 5
milliseconds to complete for an input size of 10, and 103 N3
algorithm B takes 20 milliseconds for an input size of 10, ----- = ------- => N = 30
what is the input size that you expect the two algorithms to 7 ms 189
perform about the same?

6. For an O(N 3 ) algorithm, an instance with N = 512 takes 56


milliseconds. If you used a different-sized data instance and
it took 7 milliseconds how large must that instance be?

7 8
4. For an O(Nk ) algorithm, where k is a positive rational
6. For an O(N 3 ) algorithm, an instance with N = 512 takes 56
number, a friend tells you that instance of size M took 16
milliseconds. If you used a different-sized data instance and
seconds to run. You run an instance of size 4M and find that
it took 7 milliseconds how large must that instance be?
it takes 256 seconds to run. What is the value of k?

Mk (4M)k 256
------- = ---------- => 4k = -------- => k = 2 5123 N3
16 ms 256 16 ----- = ------- => N =256ms
56ms 7

5. Algorithm A runs in O(N3 ) time and Algorithm B solves the


same problem in O(N2 ) time. If algorithm A takes 5
milliseconds to complete for an input size of 10, and 7. What is the computational complexity of the following
algorithm B takes 20 milliseconds for an input size of 10, algorithm?
what is the input size that you expect the two algorithms to
int N,j,k, sum = 0;
perform about the same?
For algorithm A: O(N 3 ) means scanf(“%d”,&N);
execution time ≤ c1 * N3
for N = 10 execution time is 5ms = c1 * 103 for(j=0; j < N; j++){
so , c1 = 5 / 103
for(k=0; k < j; k++) {
2
For algorithm B: O(N ) means sum = sum + j * k;
execution time ≤ c2 * N3
for N = 10 execution time is 20 ms = c2 * 10 2 }
so , c2 = 20 / 10 2 }

what is N for which


c1 * N 3 = c2 * N2

Substitute for c1 and c2:


5 / 103 * N3 = 20 / 102 * N2
=> N = 40

9 10
8. What is the computational complexity of the following
algorithm? (Assume N is a positive integer)
Binary Trees
int N,j,k,sum = 0;
Given the following postorder and inorder traversals of a
scanf(“%d”,&N); binary tree, draw the tree.
j = N;
Postorder: ABCDEFIKJGH
while (j > 1) { Inorder: CBAEDFHIGKJ
k = 0;

while (k > N){

sum = sum + j * k;
k ++;

}
j = j / 2;
}

11 12
An Application of Binary Trees
Exercise 1:
Contemporary compilers make use of tree structures in
obtaining forms of an arithmetic expression for efficient Consider the following node structure:
evaluation: struct node {
dataType data;
Example: Binary expression tree for struct node *left;
(A-B) + C * E/F struct node *right;
}

What is the output produced by the following function for


+ the pictured tree?

void treewalk(struct node *tree)


- / {
if (tree == NULL)
printf(“OOPS\n”);
else{
A B * F treewalk(tree->right);
treewalk(tree->left);
print(tree->data);
}
}
C D

Inorder traversal gives infix form


Preorder traversal gives prefix form
Postoredr traversal gives postfix form

13 14
tree Exercise 2:
A
a) Insert the following names into a binary search tree and
draw the resulting tree.

B C JONES
BILL
DAVE
MARY
LARRY
D E F PAUL
PENNY
KATY
LEO
MIKE
G H BETTY
DON
ROGER
TOM
I J
b) Delete JONES, PENNY, MARY

15 16
QUEUES a) isEmpty() :
int isEmpty(struct queue q){
Consider the circular array implementation of queues. In
this implementation limit the number of items in the queue
to one less than the number of elements in the array (i.e.
always keep one empty element at the end of the queue). }

Using this solution, determine the conditions for checking


whether the queue is empty or full.
Adopt the following conventions for the front and rear:
front is to point at the next item to be removed from the b) isFull():
queue. rear is to point at the next available location, that is
the next location to be filled. As an illustration, the int isFull(struct queue q){
following is a queue with three items:
0 1 2 3 4 5
A B C
}

front rear
c) enqueue():
Given the following declarations for the queue :
void enqueue (struct queue *q, int item){
#define SIZE 100
struct queue{
int items[SIZE];
int front;
int rear;
};
Write dow n the functions isEmpty(), isFull() and
enqueue().

17 18

You might also like