0% found this document useful (0 votes)
113 views

Data Structures - UNIT 3

The document discusses trees as a non-linear data structure and defines tree terminology. It describes binary trees and different types of binary trees including full binary trees. It discusses two methods for representing binary trees - array representation and linked list representation. Finally, it explains three tree traversal algorithms - preorder, inorder, and postorder traversal and provides pseudocode for each.

Uploaded by

vishwa.tdm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Data Structures - UNIT 3

The document discusses trees as a non-linear data structure and defines tree terminology. It describes binary trees and different types of binary trees including full binary trees. It discusses two methods for representing binary trees - array representation and linked list representation. Finally, it explains three tree traversal algorithms - preorder, inorder, and postorder traversal and provides pseudocode for each.

Uploaded by

vishwa.tdm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

UNIT 3 Data Structures Using C 1

Trees

Tree is a non-linear data structure.

Definition:

A tree is a finite set of one or more nodes such that

i)there is a special node called the root node.

ii)the remaining nodes are partitioned into n disjoint sets T 1, T2, T3, .........Tn where T1,
T2, T3, .........Tn are called sub trees.

Example

Terminologies:

Node: Node of a tree stores the actual data and links to the other node.

Root node: A first node written at the top is root node. The root node does not have the
parent.

Child: The node obtained from the parent node is called child node.

Predecessor: Every node N in a tree except root has a unique parent called as predecessor of
N. Here B is the predecessor of D, E, H, I and J.

Successor: Every node N in a tree except the leaf nodes has a unique child called as
successor of N. Here D, E, H, I and J are the successor of B.

Siblings: Two or more nodes having the same parent are called siblings. Here D and E are
siblings since they have same parent B.

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 2

Ancestors: The node n1 is said to be an ancestor of other node n2 if n1 is either the father of
n2 or the father of some ancestor of n2. Here A is the ancestor of B, D, E, H, I.

Descendant: The node n1 is called the descendant of node n2, if the node n2 is reachable
from n1. Here H is the descendant of D, B, A.

Degree: The number of sub trees of a node is called its degree. Here the node B has two sub
trees. So, degree of node B is 2.

Level: The distance of a node from the root is called level of the node. (or) Level is the rank
of hierarchy. The level of root node is 0.Here the level of D is 2.

Height (Depth): The height of the tree is defined as the maximum level + 1 of any leaf in the
tree. Here height of D is 3

Path − Path refers to sequence of nodes along the edges of a tree.

Forest: A forest is a set of disjoint trees. The representation of forest is similar to tree.
Example in the above figure if we remove the root of tree a forest is created with 2 trees
headed by node B and C as root.

Binary Tree:

A binary tree T is a finite set of nodes such that

i)T is empty or

ii)T contains a special node called root node and

iii) the remaining nodes of T form only upto two disjoint binary trees T1 and T2 which are
called the left sub-tree and right sub-tree.

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 3

Strictly Binary Tree:

Strictly binary tree is a binary tree that has non-empty left and right sub trees. The out degree
of every node is either zero or 2.

Full or Complete binary tree:

A complete binary tree is a binary tree, which is completely filled, with the possible
exception of the bottom level, which is filled from left to right.

Almost full Binary tree:

A binary tree of depth n is an almost complete binary tree if

i)any node at level less than n-1 has two children.

ii)For any node in the tree with a right descendant at level n, the node must have a left child
and every left descendant of node is either a leaf at level n or has two children.

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 4

Memory representation of Binary Tree

A binary tree data structure is represented using two methods. They are:

1. Array Representation
2. Linked List Representation

Consider the following binary tree...

1. Array Representation of Binary Tree


In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent
a binary tree.
Consider the above example of a binary tree and it is represented as follows:

To represent a binary tree of depth 'n' using array representation, we need one dimensional
array with a maximum size of 2n + 1.
Advantages
1. It is suitable for complete binary tree
2. Efficient if the tree does not go for changes such as insertion and deletion
Disadvantages
1. Not suitable for normal binary trees
2. It uses static allocation so that leads to memory wastage.

2. Linked List Representation of Binary Tree


To represent a binary tree, we use doubly linked list. In a double linked list, every node consists
of three fields. First field for storing left child address, second for storing actual data and third
for storing right child address.
In this linked list representation, a node has the following structure.

Example:

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 5

Advantage

1. Insertion and deletion may be performed easily.

2. It allocates memory dynamically

Disadvantage

1. Extra memory is required to maintain the left link and right link pointers

Binary Tree Traversal:

Traversal is a process of visiting each nod exactly once in a systematic order. Binary tree has
three types of traversals.

1. Preorder
2. Inorder
3. Postorder
1. Preorder Traversal
The preorder traversal of a binary tree can be recursively defined as follows:
• Process the node data (D)
• Traverse the left subtree in preorder (L)
• Traverse the right subtree in preorder (R)

The following algorithm illustrates preorder traversal.

Algorithm:

Step1: Process the root node.


If root ≠ NULL then
Print ptr->info
Else
Print “Tree is empty”
Step 2: Traverse the left sub tree recursively in preorder
If ptr->left ≠ NULL

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 6

Call pre_order(ptr->left)
Step 3: Traverse the right sub tree recursively in preorder
If ptr->right ≠ NULL
Call pre_order(ptr->right)
Step 4: return

Explanation:

Using the above algorithm we write the preorder traversal of the below binary tree.

Step 1: It process the root node. It print 8


Step 2: Then it process the left link of 8, here it is 5.
Step 3: Then it process the left link of 5, here it is 9.
Step 4: Then it process the left link and right link of 9, here it is NULL.
Step 5: So it process the right link of 5, here it is 7.
Step 6: Recursively it call pre_order() until all the node has been visited.

The preorder traversal form of above binary tree is


8→ 5 → 9 → 7 → 1 → 12 → 2 → 4 → 11 → 3

2. Inorder Traversal
The inorder traversal of a binary tree can be recursively defined as follows:
• Traverse the left subtree in preorder (L)
• Process the node data (D)
• Traverse the right subtree in preorder (R)

Algorithm:

Step1: Check tree is empty


If root == NULL then
Print “Tree is empty”
Step 2: Traverse the left sub tree recursively in inorder
If ptr->left ≠ NULL
Call in_order(ptr->left)
Step 3: Process the root node.

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 7

Print ptr->info
Step 4: Traverse the right sub tree recursively in inorder
If ptr->right ≠ NULL
Call in_order(ptr->right)
Step 5: return

Explanation:

Using the above algorithm we write the inorder traversal of the below binary tree.

Step 1: It visit the root node. It check left node is NULL or not. Here it has value 5
Step 2: It visit the node 5. It check left node is NULL or not. Here it has value 9
Step 3: It visit the node 9. It check left node is NULL or not. Here the left child is NULL.
Step 4: So it process the data.
Step 5:Recursively it call in_order() until all the node has been visited.

The inorder traversal form of above binary tree is


9→ 5 → 1 → 7 → 2 → 12 → 8 → 4 → 3 → 11

3. Postorder Traversal
The postorder traversal of a binary tree can be recursively defined as follows:
• Traverse the left subtree in preorder (L)
• Traverse the right subtree in preorder (R)
• Process the node data (D)

Algorithm:

Step1: Check tree is empty


If root == NULL then
Print “Tree is empty”
Step 2: Traverse the left sub tree recursively in postorder
If ptr->left ≠ NULL
Call post_order(ptr->left)
Step 3: Traverse the right sub tree recursively in postorder
If ptr->right ≠ NULL

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 8

Call post_order(ptr->right)
Step 4: Process the root node.
Print ptr->info
Step 5: return

Explanation:

Using the above algorithm we write the postorder traversal of the below binary tree.

Step 1: It visit the root node. It check left node is NULL or not. Here it has value 5
Step 2: It visit the node 5. It check left node is NULL or not. Here it has value 9
Step 3: It visit the node 9. It check left node is NULL or not. Here the left child is NULL.
Step 4: So it process the data.
Step 5:Recursively it call post_order() until all the node has been visited.

The postorder traversal form of above binary tree is


9→ 1 → 2 → 12 → 7 → 5 → 3 → 11 → 4 → 8

Binary search tree or Ordered Binary Tree


The Binary search tree is a binary tree in which each node has value
greater than every node of left sub tree and less than every node of right sub tree.

Operations on Binary search tree


The major operations on Binary search tree are listed
1. Insertion / Creation - Insert a node in binary search tree.
2. Searching – It search an item in a binary search tree.
3. Deletion – It delete a node from binary search tree.
4. Traversals – This operation visit all the nodes once in an order.
5. Finding maximum value in BST
6. Finding minimum value in BST
Insertion:
This operation is used to create or insert a node in binary search tree.
Write create() C code

Explanation

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 9

Using the above create procedure we construct a binary search tree. Let the data are
56, 38, 10, 65, 72, 44 and 50
Step 1: Create the root node

5
6
Step 2: Take the second element 38, which is less than 56. So create a left child.

Step 3: Take the third element 10, which is less than 56. So go to left of 56. 10<38, so create
a left child.

Step 4: Likewise it compares value and node and add the node in corresponding order.

3 6

1 4 7

5
Tree Search

Searching an item in a binary search tree is finding the element in the tree. It is faster than
searching in binary trees, arrays or linked list.

Height Balance: AVL Tree

• An AVL tree is a binary tree in which the heights of the left sub tree and height of the
right sub trees of the root differ by at most 1 and in which the left and right subtrees
are again AVL trees.

• The structure of an AVL tree is same as binary search tree additionally it balances the
height using balance factor

Balance factor=Height(left sub tree) – Height (right sub tree)


SINDHI COLLEGE BANGALORE - 24
UNIT 3 Data Structures Using C 10

Balance factor values are {-1, 0, 1}

Operations on AVL Trees

• Search
• Insert
• Delete
AVL Rotation

Rotation is the process of moving nodes either to left or to right to make the tree balanced.
There are four types of rotations depending upon where the new node is inserted.

• Left to Left rotation


• Right to Right rotation
• Left to Right rotation
• Right to Left rotation
The last two rotation are double rotation

Left to Left rotation: When the pivot node is left heavy and the new node is inserted in left
sub tree of the left child of pivot node then the rotation performed is left to left rotation

Right to Right rotation: When the pivot node is right heavy and the new node is inserted
in right sub tree of the right child of pivot node then the rotation performed is right to right
rotation

Left to Right rotation: When the pivot node is left heavy and the new node is inserted in
right sub tree of the left child of pivot node then the rotation performed is left to right rotation

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 11

Right to Left rotation: When the pivot node is right heavy and the new node is inserted in
left sub tree of the right child of pivot node then the rotation performed is right to left rotation

Insertion Operation in AVL Tree


In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL
Tree, a new node is always inserted as a leaf node. The insertion operation is performed as
follows...

• Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
• Step 2 - After insertion, check the Balance Factor of every node.
• Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
• Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is
said to be imbalanced. In this case, perform suitable Rotation to make it balanced and
go for next operation.

Consider an AVL tree by inserting the following elements in the given order
63, 9, 19, 27, 18, 108, 99, 81

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 12

Search Operation in AVL Tree


It refers to finding an element in an AVL tree. In an AVL tree, the search operation is performed
with O(log n) time complexity. The search operation in the AVL tree is similar to the search
operation in a Binary search tree.

Deletion Operation in AVL Tree


The deletion operation in AVL Tree is similar to deletion operation in BST. But after every
deletion operation, we need to check with the Balance Factor condition. If the tree is balanced
after deletion go for next operation otherwise perform suitable rotation to make the tree
Balanced.
Heap
A binary heap is a complete binary tree in which every node satisfies the heap properties.
Property 1: Nodes must be arranged in an order according to their values based on Max heap
or Min heap.
Property 2: All levels in a heap must be full except the last level and all nodes must be filled
from left to right strictly.
The two types of heap data structures are:
1. Max Heap
2. Min Heap
Max Heap
Max heap is a specialized full binary tree in which every parent node contains greater or equal
value than its child nodes.
Insertion Operation in Max Heap
Insertion Operation in max heap is performed as follows

• Step 1 - Insert the newNode as last leaf from left to right.

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 13

• Step 2 - Compare newNode value with its Parent node.


• Step 3 - If newNode value is greater than its parent, then swap both of them.
• Step 4 - Repeat step 2 and step 3 until newNode value is less than its parent node (or)
newNode reaches to root.

Example
Construct a max heap tree with the given data values: 25, 35, 20, 10, 45, 70, 50

Deletion Operation in Max Heap

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 14

The following steps are involved deleting a node from the heap tree

• Find the node that is to be deleted


• Take the last node of the tree at the place of deleted node
• Keep the node at the appropriate place using the following steps:
• Step 1 - Now, compare root value with its left child value.
• Step 2 - If root value is smaller than its left child, then compare left child with
its right sibling. Else goto Step 4
• Step 3 - If left child value is larger than its right sibling, then swap
root with left child otherwise swap root with its right child.
• Step 4 - If root value is larger than its left child, then compare root value with
its right child value.
• Step 5 - If root value is smaller than its right child, then swap root with right
child otherwise stop the process.
• Step 6- Repeat the same until root node fixes at its exact position.

Heap Sort
Heap sort is one of the sorting algorithms used to arrange a list of elements in order.

• Step 1 - Construct a Binary Tree with given list of Elements.


• Step 2 - Transform the Binary Tree into Min Heap.
• Step 3 - Delete the root element from Min Heap using Heapify method.
• Step 4 - Put the deleted element into the Sorted list.
• Step 5 - Repeat the same until Min Heap becomes empty.
• Step 6 - Display the sorted list.

Example

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 15

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 16

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 17

Lexicographic Search Trees: Trie


Trie : Introduction

• The term trie came from the word retrieval.


• Trie data structure makes retrieval of a string from the collection of string more easily.
• Trie is also called as Prefix Tree or Digital Tree

Trie: Definition

• Trie is an ordered tree data structure that is used to store a dynamic set or associative
array where the keys are usually strings.
• It is a tree of order m either empty or consisting of an ordered sequence of exactly m
tries each of order m.

Trie is a special data structure used to store strings that can be visualized like a graph. It consists
of nodes and edges.

• Each node consists of maximum 26 childrens and edges from parent to children
• 26 pointers are 26 English alphabets.
• Strings are stored in top to bottom fashion.
• All prefixes of length 1 stored at until level 1
• All prefixes of length 2 stored at until level 2 and so on.

Example

Advantages of Tries

• Faster Search
• Less space
• Longest Prefix matching

Disadvantages of Trie

• Some cases trie is slower than hash table while searching data.

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 18

• All the key values cannot be easily represented as strings.


• Example: 1 is equivalent to 1.0, 1.00, +1.0 etc

Applications of Trie

• To store a Dictionary
• Spell-checking software

External Searching: B-Trees

The process of searching for data from external storage (outside memory) is known as
external searching or external memory searching. Example: B-Tree

B-Tree

• A B-Tree is a self-balancing m-way tree data structure that allows searches, accesses,
insertions and deletions in logarithmic time.
• Each node in a B-Tree of order m can have at most m children and m-1 keys

B-Tree Properties

• Every node in the B tree has at most m children and m-1 keys
• Every node in the B tree except the root and leaf nodes has atleast m/2 children
• The root node has at least two children if it is not a leaf node.
• All leaf nodes are at the same level

Example B-tree of order 4

B-Tree Operations

• Searching
• Insertion
• Deletion

B-Tree Operations: Insertion

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 19

• Step 1: Search the B-Tree to find the leaf node where the new value or key is to be
inserted.
• Step 2: If the leaf node is full, (it already contains (m-1) keys)
1. Insert the new key into the existing set of keys in order
2. Split the node into two halves
3. Push the middle element upward to its parent node. If the
parent node is full, then split the parent node using the step 2
procedure.
• Step 3: If the leaf node is not full, then insert the new key into the node, keeping the
elements in the order.

B-Tree Operations : Deletion

• Deletion of keys in a B-Tree


• First requires traversal
• After reaching a particular node, delete the node using any of the following case
1. Node is a leaf node
2. Node is not a leaf node

1. Node is a leaf node


1. If the node has more than minimum number of keys then deletion can be done easily

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 20

2. If the node has a minimum number of keys, then first we will check the number of
keys in the adjacent leaf node.
a. If the number of keys in the adjacent node is more than the minimum number
of keys, then the first key of the adjacent leaf node will go to the parent node,
and the key parent in the parent node will be combined together in a single
node.
b. If the parent node has less than the minimum number of keys then the same
steps will be repeated until we get a minimum number of keys

2. Node is not a leaf node

• The key from the node is deleted, and its place will be occupied by either its successor
or predecessor key
• If both predecessor and successor nodes have keys less than the minimum number
then the keys of the successor and predecessor are combined

Consider the B tree of order 3 given below and perform the following operation a) insert
122, 88 and b) delete 37, 110

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 21

Application of B-Trees

• B-Trees are used to index the data especially in large databases


• Searching of data in larger unsorted data sets takes a lot of time but this can be
improved significantly with indexing using B-trees

Applications of trees

• Storing hierarchical data


• It is used to organize data for efficient insertion, deletion and searching
• Trie is a fast and efficient way for dynamic spell checking
• Heap is used to implement priority queues
• B-tree is used to implement indexing in databases

SINDHI COLLEGE BANGALORE - 24


?ll l RaDuaT ro^f To qRnehr aE Fr ril ll lo^l s

I qnyh.
qsarA q h -l
1.otur ) and a ao.t F o5
:+' ) b"la

t1 =Q,e ) , v D;h rht


va\hce\ E l)

atuyt
4=(v,tt
+u'fu,a
y=\ A,a,(,D J
E - \ h 'r,'
( ( h't )
'(B 'c) '(c 'D)
j D

?.-Diwh.t qne,Fh,

h c|'rcrtt "l a, d;c1-,yh , is a-


1
Gl = (v, c) Lrl^nc ',1 sat vaho* abmtth
oLlec( vo.yLiu., eis a da-t: X orh 'ad
oleft\lhtt @ t\/ vc\ti L? av al<o calk,'l &t
%
t,

flah,atts % t ar(t ' ot dtgttad "hf


Itr.r,o, q.0,e)
v, [, t,>,3,r
E - \:t,,1 (1,?) (3',r)'(4,1)

n* a -)v rl nrl ra. z a\ v)tl


3 un&ktttad qholh

t.d raFh t7. t\iE ) ,

\,1i\ a sa e(t mr nb cahd uct L.

rt a \a,l of oaducd par ts y ci sR.tt: ah fir bla


Y'
*z olt o uLh ol ,t, t
l.)
aa LhchQtfuil kl ,,1,,
w
4.Ha,a
l]vuo
fi=(v,e)
v.t, t,>,l,l,s| 5
E, q(1,1),( 1,1).(,,),O,A),(a, \
f+ Lt)v i:s 3atlt ,t! v.;LL

stba^df

,tTh whasc ,or hccr a"d


,f
1

fl'^f
ca[b"l s,L
r" (v 1
tt "[
f.Y
q E ) t'th k"t
y)cr
fftu
B,nd €t!€

1Lt )
ta) suLo1ta,ph
l

, ,ojou,', ad -1
"tub"t ,"atkur

-fr.r rp.ttifts f *d aljae*


i
,f ,j ) q*"rr -tF" )
I) 4a
l, irc icb at -rk \r1L,ct s t

1k c,Latc tcd edqt (i,j) L3 t n( i(@^t Lb

, .' -l vorh,
ru/1 t^t rclt h t i ,11

h rcrbr j aL.,l vort"x dl bh \ e.l h*


ll
4
4

F,ty { t,a) tr,". : a^d inti&ol b tt


6 an&
x*, ""1
tul
1" a 6livtr:d t, 4.. nr,b"-, e7 aely,
7,.(
l,,vo v a\ tfuit tot ^.te it c atbc,'l tr*
hr t fk
a,nba, otr dtr
i
"d,zgtu 't" T] init.nt .va"ltx is calb,l
ae tfu vcjbr v
I
an&q,ar (:) = r

i,-And,u t i .>
a*Jq,, t) ,
r]trF.{r[,iu!I))= r

lnc|a", a*zb/.,u3). 3
- "u re,
1nc{.?
ap

(,). 2 ul c!|1,or t n) = a
1 'DcX

-tn non- l",,rt k",l al, a 'tha d,X*t q ^


&tr,,, d b,l
I h Lttp t.Litkhr
VolDa
..u "J
ahv t aM ?ath otLtl n- un,
v inQ )s d, L f ,) A,1'
+k vet bx
I I
d, .(v)
I *J (r)=.: I

*o (t). h
,*a (:l :
[*) ' z
"i
fl,"!,h ;t trr
ffi, lalflorL drl tt"
"t.t j 'f
Lo tlat
T"fh
r& h
Plrt,
I
h sinyb C,t i! .sa; d Lr ha t''Pbtt i7
nw7'1,
wr&>r q ts
I 1

r, o r tn,L -ifrc L{t$Pbta' ,,:itI" uothat rs &nbi


ll
k edXer
bY

I
fl
Pl k" t
3
kr [n t-!
4
ShonaL c"\,hs
hn "nclr*tctad
L ca llt rl ct'D ckd ial
?
0(!t"J ftu, cl \athta, ff,oa rtrt-
1or
1kr'

ri,j) ffura o..tth a path fi'an 1


h
t- ^rl; 6) "b

d.. I t, L , ltr., u. tr th"t +k


tt
slronXll co"ne tt "l
.

L
)

-t
.3 p)
o
to) tomtttad ctrxa7h Ir] ,51,onfu tearrc c h'l pr
x*
ta NLi
n fl'"Ph
h contishEowLv$
wo i,,1

Jal: E oi
, ,r
rx,, and LLbiflhb 6w ',rlt.n,"l
lo cr,o' Or1
1"
v
4 ( 5

t9

?
tr . PoFl, ,

Pok^ b
Vo l. Vl1 q ra.,!th

srL,tr nu a ""tf
hT
:[-::i,. vo and ot c!;.3
\vo,a ,u ,u,ur, vn-, , o6,v" ]
2

n rirn/)L Pfi 1".^ % f. v, L a pLA ha1,,


vc h Vn 'r,i6 nc Llaatd Yett'ct!
t t, 1,A,6,3) G,n,,t,e,o a, e1,s,at,q) -,si6pl, Fdlt'

lt,t,r,t,z, z) (a,a, 't,ot,,l,/ t,t, a) , L,2,3)- Lbt-


' sql
tL t'{b
, rl, h ic a t[,ra,l
gta'Li
"3 I
1_+ lrt3-54-)|
it ^ .,;Jch

fl
,"p1, *ttt "" ,,1'1"
LI t,
tl. L

A,) ,+ I t) t
h wh;6 sta,Ls ,,.,,c1 an{'
nl +fu J4h.! !'elt.). i5

61RFP.r Rt- pR E J6d.rA.1oN

n""r
!;a"" ,lah s,l,u,t.e.
b,. btnraon LL'diJ\ l;. r,t7atent 4 1/LarL.

t.hd1acer,cttr
A h.tjacc ^.,1 l,s t
,l na-ti t :
^di
-]I'c d*a.rrtL,l nv-h,ix , t o, nak,x wi.

a^d r coh,nnt fatA tt 4k m"h, n lrltuqht' !


bab:uu" i an,'!
t ttu, o

u:fun T]"re
a- d t j)
o.ltr -tfu
r
1,*
d
fl'"rh
&u t6,l X
L utn
Fo, u.,d;r*c l,d q
fl fA
i.(;,l) 6E o, (j i\€ E
ndt"i x = A(i,i)=
4 1
o a&,Y ui+e t
trot a Jiutt d q
T16
llti,.l)eE)
hd.1au nt1 matvix , A( | ,i) .\

to uLored
hfr Q (,,,rr
x
fr.t
t1
j hh l-
ncLH bl',"J
:l
d4ii.ili, n .

w(,.,j) I( ,j) c€
h(i,i J -
hcljout c;1 n atr,'c =
I & Atl*t oigz

+
Llnd,,r,. Ld Di.&,hd t

A.tj"uh.l hatu t x
4 1

h
I o lo I alo
2 I ol z
3 I o
3 oooo
lf r0 ooio
[lar h larl

nllyun1 fi, ,,

') 3 +
I

L ,
3 3
l
z hdt

?n
", ^:'J Ltt r,e ptatt"taho" #, arth vc,hr in
.
tLt
?^l)'
Ael ctcab,l lt; atorc- all
)< 1t,,

a\atot,to,hur '1k a,*11 1L4h


^dlt:l ,

1 1 l''^hr
Gnlad Ls[ c,r"os ,J +hr adAarq.,l
'f
t" eat l, ,c,hx i" 1A" f,

*t1 t,1 F

4 h1
aa1 tt [@
afitu) 5

^ct1l51

U*,clil,ttnl Gnnrl"

4t,1
3
a,t; h1

agkl I

aditLl
rl E,[@

4hr
UNIT 3 Data Structures Using C 31

Directed Acyclic Graphs(DAG)

• It is a directed graph with no cycles

Articulation Points (or Cut Vertices) in a Graph

A vertex in an undirected connected graph is an articulation point (or cut vertex) if removing
it (and edges through it) disconnects the graph.

Biconnected graph

A graph is said to be Biconnected if:

1) It is connected, i.e. it is possible to reach every vertex from every other vertex, by a
simple path.
2) 2) Even after removing any vertex the graph remains connected. (No articulation points
in the graph

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

Graph Traversal
Graph traversal is a technique used for a searching vertex in a graph. That means using graph
traversal we visit all the vertices of the graph.
There are two graph traversal techniques and they are as follows...

1. DFS (Depth First Search)


2. BFS (Breadth First Search)

DFS (Depth First Search)


Depth-first search (DFS) is an algorithm for traversing a graph data structures. The
algorithm starts at the root node and explores as far as possible along each branch before
backtracking. DFS(Depth First Search) uses Stack data structure
Procedure

1. Push starting node into the stack


2. Pop an element from the stack, if it has not been traversed then traverse it. If it has
already been traversed then just ignore it. (visited[i]=true)
3. Now push all the unvisited adjacent nodes of the popped element on to the stack. (push
if it is already on the stack)
4. Repeat step 3 and step 4 until stack is empty

Algorithm

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

BFS (Breadth First Search)

Breadth First Search is a traversal technique in which we traverse all the nodes of the graph in
a breadth-wise motion. In BFS, we traverse one level at a time and then jump to the next level.
The BFS algorithm makes use of the queue data structure for implementation.

Procedure

1. Insert starting node into the queue


2. Delete front element from the queue and insert all its unvisited neighbours into the rear
end of the queue, and traverse them.(visited=true)
3. Repeat step 2 until queue is empty

Algorithm

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

Topological sorting

Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such
that for every directed edge u v, vertex u comes before v in the ordering. Topological
Sorting for a graph is not possible if the graph is not a DAG.

Example

Algorithm

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

SINDHI COLLEGE BANGALORE - 24


UNIT 3 Data Structures Using C 31

SINDHI COLLEGE BANGALORE - 24

You might also like