Complexity of Algorithms Analysis, Stack, Queue, Tree and Binary Tree

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 39

COMP6127 - Algorithm Design and Analysis

Week 2
Session 3
Complexity of algorithms analysis,
Stack, Queue, Tree and
Binary Tree
The General Objectives

 Analysis the complexity of several Prime Number


algorithms given

 Understanding the implementation of Stack,


Queue, Tree, and Binary Tree as ADT in
processing data.

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

 A natural number is called a prime number (or a prime) if


it is bigger than one and has no divisors other than 1 and
itself.
 For example, 5 is prime, since no number except 1 and 5
divides it. On the other hand, 6 is not a prime (it is
composite), since 6 = 2 x 3.
 Case: how to create an algorithm to determine weather N
is prime number or not?
 As the definition, the algorithm should find that N number
is no divisor except 1 and itself (in the other words N has
only two divisor factor 1 and N).
4
Prime Number : Algorithm #1

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;
}

Can we revise the algorithm?


5
Prime Number : Algorithm #2

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;
}

Can we revise the algorithm?


6
Prime Number : Algorithm #3

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

 Can we revise algorithm #4?


- Yes, it probably can. But, we use it for the next
discussion.
 The four algorithms are to evaluate “whether N number
is prime or not.”
 Now, we change problem to “print prime number from 1
to N!”
- It means we print 1, 2, 3, ... until N by evaluating each
number is prime; and print if only prime number;
Prime Number : Algorithm #5

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

 Below are testing results for algorithm #5 to calculate prime number


1 to 1.000.000.
 To calculate prime number 1 to 1.000.000 needs about 1 hour and
10 minutes.
 Imagine how long to calculate 1 to 1.000.000.000!
N Processing Time
1 0 ms
10 0 ms
100 0 ms
1.000 0 ms
5.000 100 - 135 ms
10.000 450 - 600 ms
100.000 45.000 - 50.000 ms
1.000.000 Approx 4.200.000 ms
11
Prime Number using
Flagging (Sieve) Technique

 Finding which one of the numbers are not prime number.


 Basic idea:
1. A non prime number has factor that other prime number.
2. Non prime numbers are recorded in an array.
3. The array contains flag whether the number prime or not.

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

 Testing results in comparing algorithm #5 and algorithm #6 (Sieve)


show significant difference.
 Calculating N=1.000.000 spent more than one hour using algorithm
#5 and less than 0.2 second using algorithm #6.

N Algorithm #5 Algorithm #6
< 1.000 0 ms 0 ms

5.000 100 - 135 ms 0 - 15 ms

10.000 450 - 600 ms 0 - 15 ms

100.000 45.000 - 50.000 ms 0 - 15 ms


Approx. 4.200.000
1.000.000 120 - 125 ms
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”

 Can we conclude that flagging technique is always better


for solving prime number?
- The answer is “No”
 Flagging technique is effective for evaluating prime
number 1 to N.
 Try to apply the technique to evaluate whether N number
is prime or not !

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

 Algorithm #4 and algorithm #7 solves same case.


N #4 #7 N #4 #7
1 0 0 11 4 17
2 0 1 12 5 20
3 0 2 13 5 21
4 1 4 14 6 24
5 1 5 15 6 27
6 2 8 100 49 245
7 2 9 1.000 499 2.957
8 3 11 5.000 2.499 16.068
9 3 13 10.000 4.999 33.070
10 4 16 100.000 49.999 356.807
1.000.000 499.999 3.775.209

18
Prime Number : Conclusion

 Flagging technique is very slow to solve first case


(evaluate whether N is prime number or not).

 A good technique is fit/better in solving a problem but not


for other problem.

19
Abstract Data Type

 Abstract Data Type (ADT)


 ADT is an abstract concept created by humans to simplify the
calculation of a process through abstraction.
 ADT is not directly recognized by the computer's processor, but a
high level programming language can be used to implement ADT

Example of ADT
– Stack
– Queue
– Tree
– Graph

20
Stack

 ADT Stack is describing a stack of data.


 Rule of stack is LIFO (Last In First Out)
 Implementations of stack can use array or linked-list.

21
Stack : Operation

 A stack in computer programming has three


operations:
- PUSH X (adding the data X into a stack)
- POP (take the top element of a stack)
- EMPTY (emptying the stack)

22
Stack : Illustration of Stack

23
Queue

 Queue is an ADT that describes the data queue.


 Rule of queue is FIFO (Last In First Out)
 Implementations of queue can use array or linked-list

24
Queue : Operation

 A queue in computer programming has three


operations:
- PUSH X (adding the data X into a queue)
- POP (take the top element of a queue)
- EMTPY (emptying the queue)

25
Queue : Illustration of Queue

26
Circular ADT

Tail Head

ln l 1
l2
l3
27
Stack & Queue : Discussion

 When do we use stack?


 When do we use queue?
 What are advantage and disadvantage of stack?
 What are advantage and disadvantage of queue?
 Can do we combine stack and queue to solve a
problem?

28
Tree

 A tree is a widely-used data structure that emulates a


hierarchical tree structure with a set of linked nodes.
 There are a number of nodes connected on a hierarchical
arrangement of the parent (parents) and child (children).
 A child node must have one parent node.
 A parent node can have multiple other nodes underneath
it (child nodes).

29
Tree : Illustration of Tree

30
Binary Tree

 A binary tree is a tree data structure in which each


node has at most two child nodes, usually
distinguished as "left" and "right".

 The maximum number of nodes in level k = 2k-1

 The maximum number of nodes in a Binary Tree


depth k = 2k-1

31
Binary Tree

 Binary tree can be implemented in array or linked-list.

 Operation in binary tree :


1. Inserting data
2. Searching data
3. Deleting data
4. Sorting data

32
Binary Tree : Illustration of Binary
Tree

33
Tree Traversal

 Tree-traversal is the process of visiting (examining and/or


updating) each node in a tree data structure, exactly once, in a
systematic way.
 Such traversals are classified by the order in which the nodes are
visited.
- Pre-order Traversal : (parent–left–right)
- Level-order Traversal : (parent–left–right) (each level)
- In-order Traversal : (left–parent–right)
- Post-order Traversal : (left–right–parent)

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

You might also like