0% found this document useful (0 votes)
8 views14 pages

DSACAT2QP

Uploaded by

chithra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

DSACAT2QP

Uploaded by

chithra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
ACADEMIC YEAR 2024-25
CONTINUOUS ASSESSMENT TEST-II
Subject Name: DSA Time : 2 Hrs
Subject Code: ADES304 Max. Marks : 50
Date of Exam: 03.09.2024 YEAR / SEM : II/III

Course Outcomes:
To realize the properties of tree data structure and its importance in searching
CO3
large database.
CO4 To understand graph data structure and its applications.
Knowledge Level: K1-Knowledge, K2-Understand, K3-Apply, K4-Analyze, K5-Synthesis,
K6 – Evaluate.

Q.No Ms CO’s B.L


PART A (10 × 2 = 20 Marks)
Answer all the Questions
1. Define tree? 2 CO3 K1
2. What is an AVL tree? 2 CO3 K1
3. Define B tree? 2 CO3 K1
4. State the properties of tree? 2 CO3 K2
5. Define a Trie tree. 2 CO3 K1
6. Define graph 2 CO4 K1
7. Define a weighted graph. 2 CO4 K1
8. Define set. 2 CO4 K1
Differentiate depth first traversal and Breadth first 2 CO4
9. traversal
K1
10. Define digraph 2 CO4 K1
PART B (3× 10 = 30 Marks)
Answer ANY THREE Questions
11. Explain Binary Search Tree. 10 CO3 K1, K2
12. Explain B Tree in detail. 10 CO3 K2
13. Describe Graph traversal in detail. 10 CO4 K1
14. Explain Spanning Tree with examples. 10 CO4 K2
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

ANSWER KEY
PART A
1. Define tree?
Tree is a non linear data structure. There is no linear relation between the data items.

It can be defined as finite set of more than one node.

There is a special node designated as root node.

2. What is an AVL tree?


An AVL tree (Adelson-Velskii and Landis' tree, named after the inventors) is a self-balancing binary
search tree.
3. Define B tree?
B-tree is a tree data structure that keeps data sorted and allows searches, sequential access, insertions, and
deletions in logarithmic time
4. State the properties of tree?
Any node can be the root of the tree.If the root is identified; then that tree is called as the rooted node.

If it is not identified; then that tree is called as the free tree.


5. Define a Trie tree.
A trie, also called digital tree and sometimes radix tree or prefix tree (as they can be searched by
prefixes), is an ordered tree data structure that is used to store a dynamic set or associative array where the
keys are usually strings.
6. Define graph
A graph consists of two sets V, E.

o V is a finite and non empty set of vertices.

o E is a set of pair of vertices; each pair is called an edge.


7. Define a weighted graph.
Linked list is a dynamic and linear data structure.
A graph is said to be a weighted graph if every edge in the graph is assigned some weight or value. The weight
of an edge is a positive value that may be representing the distance between the vertices or the weights of the
edges along the path.

8. Define set.

A set is an abstract data structure that can store certain values, without any particular order, and no repeated
values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection
types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

9. Differentiate depth first traversal and Breadth first traversal

Both are graph traversal algorithms. They differ in the order they reach the nodes. Depth first explores
all of the nodes reachable from X before moving on to any of X’s siblings, whereas breadth first search explores
siblings before children. For a depth first use a stack implementation and for breadth first queue can be used.

10. . Define digraph?


If an edge between any two nodes in a graph is directionally oriented, a graph is called as directed .it is also
referred as digraph.

PART B
11. Explain Binary Search Tree.
Binary search tree is a binary tree in which every node X in the tree, the values of all keys in its left subtree are
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
smaller than the key value in X, and the values of all keys in its right subtree are larger than the key value in X.

OPERATIONS:
 Make Empty
 Insert
 Find
 Find Min
 Find Max
 Delete
MAKE EMPTY:
This operation is mainly for initialization when the programmer prefer to initialize the first element as a one –
node tree.
PROGRAM:
SearchTree MakeEmpty(Search Tree T)
{
if(T!=NULL)
{
MakeEmpty(T->left);
MakeEmpty(T->right);
free(T);
}
return NULL;
}
INSERT:
To insert the element X into the tree.
 Check with the root node T
 If it is less than the root
o Traverse the left subtree recursively until it reaches the T->left equals to NULL. Then X is
placed in T->left
 If it is greater than the root.
o traverse the right subtree recursively until it reaches the T->right equals to NULL. then X is
placed in T->right
PROGRAM:
SearchTree Insert(int X, searchTree T)
{
if(X<T->element)
T->left=Insert(X,T->left);
else
if(X>T->element)
T->right=Insert(X,T->right);
return T;
}
FIND:
 Check whether the root is NULL if so the return NULL
 Otherwise , check the value X with the root node value(i.e.T->data)
o If X is equal to T->data, return T
o If X is less than T->data, Traverse the left of T recursively
o If X is greater than T->data, Traverse the right of T recursively
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
PROGRAM
int Find(int X, SearchTree T)
{
if(T==NULL)
return NULL;
if(X<T->Element)
return Find(X,T->left);
else
if(X>T->Element)
returnFind(X,T->right);
else
return T;
}

FIND MIN:
This operation returns the position of the smallest element in the tree. To perform FindMin, start at the root node
and go left as long as there is a left child. The stopping point is the smallest element.
PROGRAM
int FindMin(SearchTree T)
{
if(T!=NULL)
while(T->left!=NULL)
T=T->left;
return T;
}

FIND MAX:
This operation returns the position of the largest element in the tree. To perform FindMax, start at the root node
and go right as long as there is a right child. The stopping point is the largest element.
PROGRAM
int FindMax(SearchTree T)
{
if(T!=NULL)
while(T->right!=NULL)
T=T->right;
return T;
}

DELETE:
Deletion operation is the complex operation in the binary search tree. To delete an element , consider the
following three possibilities.
CASE 1: Node to be deleted is a leaf node (i.e.) No Children.
CASE 2: Node with one child
CASE 3: Node with two children

CASE 1: Node to be deleted is a leaf node (i.e.) No Children.


If the node is a leaf node, it can be deleted immediately.

CASE 2: Node with one child


If the node has one child, it can be deleted by adjusting its parent pointer that points to its child node.

CASE 3: Node with two children


It is difficult to delete a node which has two children . The general strategy is to replace the data of the node to
be deleted with its smallest data of the right subtree and recursively delete that node.
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

12. Explain B Tree in detail.


• B Tree of order nm is an m – way search tree with the following properties.
 The root node must have atleast two child nodes and atmost m child nodes.
 All internal nodes other than root node must have atleast m/2 non empty child nodes.
 The number of keys in each internal node is one less than its number of child nodes, which will
partition the keys of the tree into subtree.
 All internal nodes are at same level.
• B-trees are balanced search trees: height = O log(n) for the worst case.

GENERAL REPRESENTATION

It can also be represented as

Basic Operations: (Explain the Basic operations on B-trees? (Nov 12)

• B-Tree-Insert

• B-Tree-Delete

INSERTION:
To insert a key K in the node X of the B tree of order m can be proceed in one of the two ways.
CASE 1:
When the node X of the B Tree of order nm can be accommodate the key K1, then it is inserted in that
node and the number of child pointer fields are appropriately upgraded.

CASE 2:
If the node is full, then the key K is apparently inserted into the list of elements and the list is splitted into two
on the same level as its median ( K median ). The keys which are less than Kmedian are placed in Xleft and those
greater than K median are placed at X right.

DELETION:
The deletion of Key K from a B tree order m may trigger many cases.
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
CASE 1:
If the key K to be deleted belongs to a leaf node and its deletion does not result in the node having less than its
minimum number of elements. Then delete the key from the leaf and adjust the child pointers.

CASE 2:
If the key K to be deleted belong to a non leaf node. Then replace K with the largest key K Lmax in the
left subtree of K or the smallest key KRmin from the right subtree of tree K and then delete KRmin or
KLmax from the node, which in turn will trigger case 1 and 2

The largest key K Lmax in the left subtree of 25 is replaced and then the key 23 is deleted immediately
since it is a leaf node.

CASE 3:
If the key to be deleted from a node leaves it with less than its minimum number of elements, then the
element may be borrowed either from left or right siblings.
If the left sibling node has an element to spare, then move the largest key K Lmax in the left sibling node
to the parent node and the element P in the parent node is moved down to set the vacancy created by
deletion of K in node X.
If the left sibling has no element to spare then move to case 4.

CASE 4:
If the key K to be deleted from a node X leaves it with less than its minimum number of elements and
both siblings nodes are unable to spare an element . Then the node X is merged with one of the sibling
nodes along with intervening element P in the parent node.

13. Describe Graph traversal in detail.


GRAPH TRAVERSAL

Given an undirected graph G = (V.E) and a vertex v in V(G) we are interested in visiting all
vertices in G that are reachable from v (that is all vertices connected to v ). We have two ways
to do the traversal. They are

 DEPTH FIRST SEARCH


 BREADTH FIRST SEARCH.
DEPTH FIRST SEARCH
In graphs, we do not have any start vertex or any special vertex singled out to start traversal
from. Therefore the traversal may start from any arbitrary vertex.
We start with say, vertex v. An adjacent vertex is selected and a Depth First search is
intimated from it, i.e. let V1, V2.. Vk are adjacent vertices to vertex v. We may select any
vertex from this list. Say, we select V1. Now all the adjacent vertices to v1 are identified and
all of those are visited; next V2 is selected and all its adjacent vertices visited and so on.
This process continues till are the vertices are visited. It is very much possible that we reach a
traversed vertex second time. Therefore we have to set a flag somewhere to check if the
vertex is already visited.
Let us see an example, consider the following graph.

Let us start with V1,


ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
V8

1. Its adjacent vertices are V2, V8, and V3. Let us pick on v2.

2. Its adjacent vertices are V1, V4, V5, V1 is already visited.

3. Let us pick on V4.

4. Its adjacent vertices are V2, V8.

5. V2 is already visited .let us visit V8.

6. Its adjacent vertices are V4, V5, V1, V6, V7.

7. V4 and V1 are visited. Let us traverse V5.

8. Its adjacent vertices are V2, V8. Both are already visited therefore, we back track.

9. We had V6 and V7 unvisited in the list of V8. We may visit any. We may visit any. We
visit V6.

10. Its adjacent is V8 and V3. Obviously the choice is V3.

11. Its adjacent vertices are V1, V7. We visit V7.

12. All the adjacent vertices of V7 are already visited, we back track and find that we
have visited all the

vertices.

Therefore the sequence of traversal is V1, V2, V4, V5, V6, V3, V7.

This is not a unique or the only sequence possible using this traversal method.

We may implement the Depth First search by using a stack, pushing all unvisited vertices to
the one just visited and popping the stack to find the next vertex to visit.
This procedure is best described recursively as in, Procedure DFS(v)

// Given an undirected graph G = (V.E) with n vertices and an array visited (n) initially set to
zero . This algorithm visits all vertices reachable from v .G and VISITED are global >
//VISITED (v) ß 1

for each vertex w adjacent to v do if VISITED (w) =0 then call DFS (w) end

end DFS
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
COMPUTING TIME

1. In case G is represented by adjacency lists then the vertices w adjacent to v can be


determined by following a chain of links. Since the algorithm DFS would examine
each node in the adjacency lists at most once and there are 2e list nodes. The time to
complete the search is O (e).

2. If G is represented by its adjacency matrix, then the time to determine all vertices
adjacent to v is O(n).

Since at most n vertices are visited. The total time is O(n2).

BREADTH FIRST SEARCH

 In DFS we pick on one of the adjacent vertices; visit all of its adjacent vertices and
back track to visit the unvisited adjacent vertices.

 In BFS , we first visit all the adjacent vertices of the start vertex and then visit all the
unvisited vertices adjacent to these and so on.

 Let us consider the same example, given in figure. We start say, with V1. Its adjacent
vertices are V2, V8 , V3.

 We visit all one by one. We pick on one of these, say V2. The unvisited adjacent
vertices to V2 are V4, V5 . we visit both.

 We go back to the remaining visited vertices of V1 and pick on one of this, say V3. T
The unvisited adjacent vertices to V3 are V6,V7. There are no more unvisited
adjacent vertices of V8, V4, V5, V6 and V7.

Algorithm BFS gives the details.


ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

Procedure BFS(v)

//A breadth first search of G is carried out beginning at vertex v. All vertices visited are
marked as VISITED(I) = 1. The graph G and array VISITED are global and VISITED is
initialised to 0.//

1. VISITED(v) ß 1

2. Initialise Q to be empty //Q is a queue//

3. loop

4. for all vertices w adjacent to v do

5. if VISITED(w) = 0 //add w to queue//


6. then [call ADDQ(w, Q); VISITED(w) ß 1] //mark w as VISITED//

7. end

8. if Q is empty then return

9. call DELETEQ(v,Q)

10. forever

11. end BFS

COMPUTING TIME

1. Each vertex visited gets into the queue exactly once, so the loop forever is iterated at
most n times.

2. If an adjacency matrix is used, then the for loop takes O(n) time for each vertex
visited. The total time is, therefore, O(n2).
3. In case adjacency lists are used the for loop as a total cost of d1+……..+dn =
O(e) where di = degree(vi). Again, all vertices visited. Together with all edges
incident to from a connected component of G.

14.Explain Spanning Tree with examples.


SPANNING TREE:
A spanning tree of a connected graph is its connected a cyclic subgraph that contains all the
vertices of graph.
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
PRIMS ALGORITHM:
Prims Algorithm is one of the way to compute a minimum spanning tree which uses a greedy
technique.This algorithm begins with a set U initialized to { 1 }. It then grows a spanning
tree, one edge at a time. At each step it finds a shortest edge (u,v) such that cost of(u,v) is the
smallest among all the edges, whwre u is in minimum spanning tree and V is not in Minimum
Spanning Tree.
ALGOTITHM:
void Prims(Table T)
{
vertex V,W;
/* Table initialization */
for(i=0;i<NumVertex;i++)
{
T[i].known=False;
T[i].dist=Infinity;
T[i].path=0;
}
for(; ;)
{
Let V be the start vertex with smallest distance
T[V].dist=0;
T[v].known=Treu;
for each W adjacent to V
if(!T[w].known)
{
T[w].dist=Min(T[W].dist,CVW);
T[W].path=V;
}
}
EXAMPLE:

Here a is taken as source vertex.


Then the distance of its adjacent vertex is as follows: T[b].dist=Min(T[b].dist,Cab)
= Min( ∞,2)
=2
T[d].dist=Min(T[d].dist,Cad)
= Min( ∞,1)
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
=1
T[c].dist=Min(T[c].dist,Cac)
= Min( ∞,3)
=3

Next vertex d with


its minimum
distance is
marked as visited and the distance of its unknown adjacent vertex is updated.
T[b].dist=Min(T[b].dist,Cdb)
= Min( 2,2)
=2
T[c].dist=Min(T[c].dist,Cdc)
= Min(3,1)
=1

Next vertex c with its minimum


distance is marked as visited and
the distance of its unknown adjacent
vertex is updated.

Finally vertex b which


is not visited is
marked
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

KRUSKAL ALGORITHM:
Kruskal algorithm ueses a greedy technique to compute a minimum spanning tree. This
algorithm selects the edges in the order of smallest weight and accept an edge if it does not
cause a cycle.The algorithm terminates if enough edges are accepted.

ALGORITHM

Voidd Kruskal(graph G)
{
int EdgesAccepted=0;
while(EdgesAccepted<Numvertx -1)
{
USet=Find(U,S);
VSet=Find(V,S);
if(USet!=VSet)
{
EdgesAccepted++;
SetUnion(S,Uset,VSet);
}
}
}
EXAMPLE:

Step 1: Initially each vertex is in its own set


ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE

Step 2: Now select the edge with minimum weight

Edge(a,b) is added to minimum spanning tree


Step 3: Select the next edge , with minimum weight and check whether it forms a cycle. If it
forms a cycle then that edge is rejected. else add the edge to minimum spanning tree.

(c,d) is next minimum weighted edge.


Step 4: Repeat stpe 3 until all vertices included in minimum spanning tree.
ROLL/REG NO

ACHARIYA
COLLEGE OF ENGINEERING TECHNOLOGY
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE & DATA SCIENCE
The minimum cost of spanning tree is 4 (2 + 1 + 1 = 4)

You might also like