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

ADA

ANALYSIS AND DESIGN OF ALGORITHM


ALGORITHM:
This word came from the name of a Persian author, Abu Jafar
Mohammed Ibn Musa Al Khowarismi. Algorithm is a method that can be
used by a computer for the solution of a problem.
DEFINITION

It is a finite set of instruction that if followed, accomplish a particular


task.
PROPERTIES OF AN ALGORITHM:
1. Input
2. Output
3. Definiteness
4. Effectiveness
5. Finiteness

1. Input; zero or more quantities are externally supplied.

2. Output: at least one quantity is produced.

3. Definiteness; each instruction is clear and unambiguous.


4. Finiteness; If we trace out the instructions of any algo, then for
all cases, the algo terminates after a finite number of steps.
5. Effectiveness; Every instruction must be very basic so that it can
be carried out by a person using pencil and paper. It is not
enough that each operation be definite as in criteria 3, it also
must be feasible.

PERFORMANCE ANALYSIS:

Performance is about answering following questions:

1. does it do what we want it to do?


2. Does it work correctly according to specifications of the task?
3. Is the code readable?
And so on....
• Performance is about the overhead to execute the algorithm to get
the desired result
TYPES:
1. Space
2. Time
SPACE COMPLEXIETY:

The space complexiety of an algo is the amount of memory it needs to


run to completion.
TIME COMPLEXIETY:
It is about the computer time it needs to run to completion.
SPACE COMPLEXIETY:

ALGO 1.1:
Eg.Algo abc(a,b,c)
{
Return a+b+b*c+(a+b-c)/(a+b)+4.0;
}

Equation for Space Complexity:-S(P)=c+Sp S(P)=Space complexity of

a program Where c=constant part/fixed part Sp=variable part FIXED

part:

independent of characteristics of inputs and outputs

i.e. constants simple variables fixed size variables VARIABLE part:

Variables whose size is dependent on particular problem instances


*so we need to first determine which instance characteristics will be
used to measure space requirements.In algo 1.1
A,b,c are constants
& we assume that a single word is adequate to store values of a,b,c and
the result
• No instance characteristics So Sp=0

ALGO 1.2:

To calculate sum iteratively Algo sum(a,n)


{

s=0.0;

for i=1 to n do s=s+a[n]; return s;


}

• Complexiety calculation depends basically on n(no of elements)

Variable part=n
As n will determine the size of the array a[i] as it has n elements

Constant part 1 for n 1 for i 1 for s


Ssum(n) >= (n+3)
ALGO 1.3:

RECURSIVE algo for SUM Algo RSum(a,n)


{

If (n<=0) then return 0.0; else

return RSum(a,n-1)+ a[n];


}

• Instances characterized by n
• Return address requires only 1 word of memory
• Recursion is for (n+1) times
RSum(a,n-1)+a[n]

*for every recursion step


1. value of n
2. return address
3. pointer to a[] (array)
RSum(a,n-1)
0 to n-1=n A[n]=1
Times executed Total=n+1
statements s/e Frequency Total steps
Algo Sum(a,n) 0 - 0
{ 0 - 0
S=0.0 1 1 1
For i=1 to n do 1 n+1 n+1
S=s+a[n] 1 N n
Return s 1 1 1
} 0 0 0

Total steps= 2n+3

RECURSIVE ALGO:
Statements s/e Frequency Total steps
n=0 n>0 n=0 n>0
algo RSum(a,n) 0 0 0 0 0
{ 0 0 0 0 0
If (n<=0) then 1 1 1 1 1
Return 0.0 1 1 0 1 0
Else 0 0 0 0 0
return RSum(a,n-1) 1+x 0 1 0 1+x
+a[n]
} 0 0 0 0 0
Where x=RSum(a,n-1)

Total steps:

1. 2
N=0 2+x
2.
N>0
• The above is another method of calculating the complexiety of
an algo,and is called TABLE METHOD.
• Previous method is step wise complexiety calculation
• Bt table method is Comparatively an easier one

TIME COMPLEXIETY:
The equation for time complexity is T(P)=c+tp T(P)= Time Complexity
for the program Where c=constant part Tp=variable part

Constant part: Compile time Variable part: run time

RUN TIME: depends on instance characteristics


• We assume that we needn't recompile the program everytime
we want to execute it
• We assume every instruction/statement takes approx 1 unit
execution time
GRAPHS:
• Non-linear data structure
• A graph G consists of 2 things_
v- nodes (vertices) e- edges

PATH:Path p of length n from node u to v

P=(v0,v1,v2......................vn)
Here u = v0

• If v0=vn then
The path is called CLOSED PATH.

A graph G is said to be complete if every node u in G is


adjacent to every other node v in G

• No of nodes of G(complete)= n(n-1)/2 DIRECTED Graph:

(digraph)

■ Outdegree
■ Indegree
In graph 1.

-- 2 indegree -- 1 out degree

SEQUENTIAL REPRESENTATION OF GRAPHS:

1. adjacency matrix

2. adjacency list

3. adjacency multilist
1. ADJACENCY MATRIX:

A connected component (or simply a component) H of an undirected graph


is a maximal connected subgraph. By “maximal, we mean that G contains
no other subgraph that is both connected and properly contains H. G4 has
two components, H\ and Hi (see Figure 2.28).

Figure 2,28 A graph with two connected components

In graph 2
Aij = 1 vi adjacent to vj for edge(vi,vj)

0 otherwise
X Y Z W
X 0 0 0 1
Y 1 0 1 1
Z 1 0 0 1
W 0 0 1 0

2. ADJACENCY LIST:

Graph 3

Node Adjacency list


A B,C,D
B C
C
D C,E
E C

3. ADJACENCY MULTILIST:
(A) Node list:

NODE NEXT ADJ

NODE: name of node NEXT: pointer to next


node ADJ: pointer to first element of adjacency
list (B) Edge list:

DEST LINK

DEST: destination node of edge


LINK: link together the edges with same node.
GRAPH TRAVERSALS:
1. DFS
2. BFS

3 states
■ Ready
■ Waiting
■ Processed

DFS:
• To process the nodes which are reachable from starting
nodes..
• Is used to calculate MINIMUM PATH
• QUEUE is used

BFS:
• Is used to find all reachable nodes
STACK is used

You might also like