Professional Documents
Culture Documents
Complexity of Algorithms Analysis, Stack, Queue, Tree and Binary Tree
Complexity of Algorithms Analysis, Stack, Queue, Tree and Binary Tree
Complexity of Algorithms Analysis, Stack, Queue, Tree and Binary Tree
Week 2
Session 3
Complexity of algorithms analysis,
Stack, Queue, Tree and
Binary Tree
The General Objectives
2
Outline
Prime Number
Prime Number using Flagging (Sieve) Technique
Abstract Data Type
Stack
Queue
Circular ADT
Stack & Queue Discussion
Tree
Binary Tree
Tree Traversal
3
Prime Number
bool prime(N){
int factor = 0; 1
for (i=1;i<=N;i++)N
If (N mod i == 0) N
factor++; N
if (factor=2) 1
return true;1
else
return false;
}
bool prime(N){
int factor = 2; 1
for (i=2;i<=N-1;i++) N-2
If (n mod i == 0) N-2
factor++; N-2
if (factor=2) 1
return true;1
else
return false;
}
bool prime(N){
int factor;
if (N > 1) 1
factor = 2; 1
Else
factor = 1;
for (i=2;i<=N-1;i++) N-2
If (n mod i == 0) N-2
factor++; N-2
if (factor=2) 1
return true; 1
else
return false;
}
Can we revise the algorithm?
7
Prime Number : Algorithm #4
bool prime(N){
int factor;
if (N > 1) 1
factor = 2; 1
Else
factor = 1;
for (i=2;i<=N div 2;i++) N/2-1
If (n mod i == 0) N/2-1
factor++; N/2-1
if (factor=2) 1
return true; 1
else
return false;
}
Can we revise the algorithm?
Prime Number : Problem
int factor;
For (x=2;x<=N;x++){
factor = 2;
for (i=2;i<=x div 2;i++)
If (n mod i == 0)
factor++;
if (factor=2)
print N, “ “;
}
10
Prime Number : Testing The
Algorithm
12
Prime Number : Algorithm #6
T[1]=”X”
for (i=2;i<=N;i++)
T[i]=”?”
for Z=2 to N do
if (T[Z]=”?”){
T[Z]=”P”
for j=2 to (N div Z)
if (T[Z*j]=”?”)
T[Z*j]=”X”
}
for (i=1;i<=N;i++)
if (T[Z]=”P”)
print Z,” ”
13
Prime Number :
Comparing The Algorithm
N Algorithm #5 Algorithm #6
< 1.000 0 ms 0 ms
14
Prime Number :
Calculating Number of Iterations
N #5 #6 N #5 #6
1 0 1 11 25 28
2 0 3 12 30 32
3 2 5 13 36 34
4 3 8 14 42 38
5 5 10 15 48 42
6 7 14 100 2.426 345
7 10 16 1.000 249.169 3.957
8 13 19 5.000 6.245.670 21.068
9 16 22 10.000 24.991.230 43.070
10 20 26 100.000 2.499.909.593 456.807
249.999.078.49
1.000.000 4.775.209
9
15
Prime Number :
Problem “Effective is Relative”
16
Prime Number : Algorithm #7
bool prime(N){
T[1]=0
for (X=2;X<=N;X++;)
T[X]=1
for (X=2;X<=N;X++;)
if (T[X]=1){
for (i=2;i<=(N div X);i++)
if (T[X*i]=1)
T[X*i]=0
}
if (T[X]=1)
return true
else
return false
}
17
Prime Number : Comparison
18
Prime Number : Conclusion
19
Abstract Data Type
Example of ADT
– Stack
– Queue
– Tree
– Graph
20
Stack
21
Stack : Operation
22
Stack : Illustration of Stack
23
Queue
24
Queue : Operation
25
Queue : Illustration of Queue
26
Circular ADT
Tail Head
ln l 1
l2
l3
27
Stack & Queue : Discussion
28
Tree
29
Tree : Illustration of Tree
30
Binary Tree
31
Binary Tree
32
Binary Tree : Illustration of Binary
Tree
33
Tree Traversal
34
Tree Traversal : Binary Tree
Traversal
Pre-order : F, B, A, D, C, E, G, I, H
In-order : A, B, C, D, E, F, G, H, I
Post-order : A, C, E, D, B, H, I, G, F
Level-order : F, B, G, A, D, I, C, E, H
35
Tree Traversal : Euler Tour Traversal
+
1
16
11
10
x x
2 12
9
15
4
3 13
14
2 – 3 b
5
8
7
6
Arithmetic Expression Tree
a 1 2 x (a – 1) + (3 x b)
36
Exercise
1. Revise algorithm #4 of prime number!
2. Create an algorithm to print first N prime number!
3. Create algorithms for operation of push and pop in
stack.
4. Create algorithms for operation of push and pop in
circular queue.
37
Exercise
5. Describe the differences in Tree implementations using arrays
and pointers!
6. Do traversal pre-order, in-order, post-order and level-order for
the tree below.
38
Thank You