You are on page 1of 106

I:'

4,
Qb 89 b
7 _7 2
r
'I-
[uiv
L-1 bV 0b89 Sb' 8 LN d'om
d
t
1 I N
-uuJ [uwv
o-
r UtW
4;
'E b ob 8 S G)
Li 4
wob-1V

- ccozl i
1
j.! • flo Jx wUT
wn
1
LOt NO}iT3
£-J.M4fl
41

8t
C
4S 682- 3
2q 34 P1
-
'1'
j

ct 44c

ri.\n 4-a

6 10 2. 34
1'

M- 29

a 4S 6 qo .2.9 3L
-j
'1'
mir

[1 Z9

n- \\ 4J
4/
Sct ks 6 .2 17

r1tj1.

)wck ,P1tiJ Arm'tr3

P1 4S b qo 29 34 8C

1
3

,a
mn 4-j
S2 34 4s (8 sct

:EJ Q1QIfl

't3ac oFQioi

rCn)
&)BBL DRT

L
com?o)J

L4 Qk1 o oitThrL OT1

yo riuvo.

A LOR1THKL
-0+0 r- aD

jbi 3k- O 1-0 :\---t

t -a /-

ucq cLr) AV3ij

4
ci 45 6 o 2- 34

45 LA
2.039'

fg 68 2. 34 11

6 LP

2J1 34 ti
45_

3
4+bi ciCT)'
17
r
\4; \;
Qb L\'E
89 S47
t.
d
1
0, L 'L.
•71 b8 89 S3'
c7 5&
r
Qb Li c' 6' b8 89 S3
1'
oCfl<
Q1,7 L
\-c:
1-i o1,4 b8 Sb'
-Pr
r
L-J - 01, ?°
89 51
p
LI 9' b
b8
4S 68 2 3 8 zi o

4s 6s 34 t1 8 o

Lu

A ALYSI&

fl.2

1=o jO

øCt -2-

nCh-)
2,

CCn-)
A1t
(

1wfl t

-+ rcQan nr or& cTl) '1'.D

t1 .

-1 OCWC —

— oo QCth o

1 -.

YT) -1

-- 'r—i k -TQii

pJ -. -

¶itn

apxw
k,
t— u3 çtc , &

th35i o ko n -rod

QflX ornt ,

C) J(SL&& 2
r1X3LU1
L

jC)3j k4i.
AORITH.

(0.

0 ri-rn
I T

n-ni
0 2 0 rn-i
I tl%l14Q.131
1! rda)r49 )

Ot g c
to it f2 13
cz tW6 QO.LQ,

a\ mx — mk

r1\)cr

O(tfl?cr

cA eCr)

31L
C sw)

A1v3 k- k

., L.fl ', \,
ikuii A[13 k do
1
I* -i-H- (pti3

Lrt LttW1.L
QQ
Q19q 0
—L
DECREASE AND CONQUER-INSERTION SORT
? SO 2: Implement Insertion sort Algorithm (Ap,C)
Insertion Sort

Sorted
List

Pick elements from unsorted


subset and keep on inserting into
sorted subset
->Pick 2 from unsorted subset and
store it in a variable and make a hole
in that position.
->To insert 2 to sorted subset we shift
all numbers in the sorted subset to
one position right
Algorithm:

Insertion Sort(A,n)
{
for i 🡸 1 to n-1
{
value 🡸 a[i]
hole 🡸 i
while(hole>0 &&
A[hole-1]>value
{
A[hole] 🡸 A[hole-1]
hole 🡸 hole-1
}
A[hole] 🡸 value
}
}
? SO 3: Analyse the time complexity of Insertion
sort (Ap,P)
Time
complexity
? SO 4: Compare Divide and Conquer with
Decrease and Conquer (Ap,P)
BFS AND DFS
BINARY TREE
● Binary tree is a special tree data structure.
● In a binary tree, each node can have at
most 2 children.
TREE TRAVERSAL
❖ Tree Traversal refers to the process of
visiting each node in a tree data structure
exactly once
Depth First Search
DFS TRAVERSAL
DFS of the below graph is 1 2 4 6 3 5 7 8
• Implemented using stack
• Push a vertex onto the stack when the vertex is
reached for the first time
• Pop a vertex off the stack when it becomes a
dead end

Tree Edge
Whenever a new unvisited vertex is reached
for the first time, it is attached as a child
from which it is being reached. Such edge is
called Tree edge
Tree Edge:
It is an edge which is present in the tree
obtained after applying DFS on the graph. All
the Green edges are tree edges.
Forward Edge: It is an edge (u, v) such that v is
descendant but not part of the DFS tree. Edge
from 1 to 8 is a forward edge
Back edge

An edge leading to a previously visited


vertex other than its immediate
predecessor (parent in the tree). Such an
edge is called back edge.
Back edge:
It is an edge (u, v) such that v is ancestor of
edge u but not part of DFS tree. Edge from 6 to
2 is a back edge. Presence of back edge
indicates a cycle in directed graph.
Cross Edge:
It is a edge which connects two node such
that they do not have any ancestor and a
descendant relationship between them. Edge
from node 5 to 4 is cross edge
Articulation Point:
A vertex of a connected graph is said to be
its articulation point if its removal with all edges
incident to it breaks the graph into disjoint pieces
Example:
Stack
Implementation

d-dead end remove


d

1 2 3

4
5 e-droppe Remove
6 d b
TIME COMPLEXITY
? Time proportional to the size of the data
structure used for representing the graph
? Adjacency matrix representation: O(|v2|)
? Adjacency linked list representation :
O(|V|+|E|)
TRAVERSAL IN DFS
Following three traversal techniques fall under
Depth First Traversal-
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
PREORDER TRAVERSAL
1. Visit the root
2. Traverse the left sub tree i.e. call Preorder
(left sub tree)
3. Traverse the right sub tree i.e. call Preorder
(right sub tree)

Root → Left → Right


APPLICATIONS OF PREORDER TRAVERSAL
● Preorder traversal is used to get prefix
expression of an expression tree.
● Preorder traversal is used to create a
copy of the tree.
INORDER TRAVERSAL
1. Traverse the left sub tree i.e. call
Inorder (left sub tree)
2. Visit the root
3. Traverse the right sub tree i.e. call
Inorder (right sub tree)

Left → Root → Right


? SO 2: Implement the Depth first search
algorithm with an example .
APPLICATIONS OF INORDER TRAVERSAL
● Inorder traversal is used to get infix
expression of an expression tree.
POST ORDER TRAVERSAL
1. Traverse the left sub tree i.e. call
Postorder (left sub tree)
2. Traverse the right sub tree i.e. call
Postorder (right sub tree)
3. Visit the root

Left → Right → Root


APPLICATIONS OF POST ORDER TRAVERSAL
● Postorder traversal is used to get
postfix expression of an expression
tree.
● Postorder traversal is used to delete
the tree.
● This is because it deletes the children
first and then it deletes the parent
APPLICATIONS
? Checking Connectivity
? Checking Acyclicity of a graph
⚫ If there exist a back edge from some vertex u to its
ancestor v, graph has a cycle
? SO 3: Implement the Breadth first search
algorithm with an example
BREADTH FIRST TRAVERSAL
● Breadth First Traversal of a tree prints
all the nodes of a tree level by level.
● Breadth First Traversal is also called as
Level Order Traversal.
EDGES
? Tree Edge
? Cross Edge
⚫ If an edge leading to a previously
visited vertex other than its
immediate predecessor (its parent in
the tree) is encountered, the edge is
cross edge
Exam
ple
f is dropped-it is
done
a is dropped-it is
visited
d is dropped-it is Implemented as a
done
Queue where parents
are dropped from front
c is dropped-it is and children is added to
done
e is dropped-it is the back
done
TIME COMPLEXITY
? Time proportional to the size of the data
structure used for representing the graph
? Adjacency matrix representation: O(|v2|)
? Adjacency linked list representation :
O(|V|+|E|)
APPLICATIONS
Level order traversal is used to print the
data in the same order as stored in the
array representation of a complete binary
tree.
? SO 4: Compare the time complexity of Depth
first search with Breadth first search algorithms

You might also like