(Uscs33) - (Data Structures) : Question & Answers Type: 100% Theory
(Uscs33) - (Data Structures) : Question & Answers Type: 100% Theory
2. Give few examples for data structures? [Nov/Dec 2015, Apl/May 2014]
A hash table is a common implementation of an associative array. A record (also called
tuple or struct) is an aggregate data structure. A record is a value that contains other values,
typically in fixed number and sequence and typically indexed by names. The elements of
records are usually called fields or members.
5. What are the advantages in the array implementation of Lists? [Nov 2014]
Easy to implement
Random access is easier
suitable when the number of elements are predefined or already known.
8. Define Array?
2. Specific data structures are essential ingredients of many efficient algorithms, and
make possible the management of huge amounts of data, such as large integrated
collection of databases.
Implementation of the list data structure may provide some of the following operations:
Index − Each location of an element in an array has a numerical index which is used
to identify the element.
ARRAY REPRESENTATION
Arrays can be declared in various ways in different languages. For illustration, let's
take C array declaration.
Arrays can be declared in various ways in different languages. For illustration, let's
take C array declaration.
As per above shown illustration, following are the important points to be considered.
Each element can be accessed via its index. For example, we can fetch element at
index 6 as 9.
Data Structures is about rendering data elements in terms of some relationship, for
better organization and storage.
For example, we have data player's name "Virat" and age 26. Here "Virat" is
of String data type and 26 is of integer data type.
We can organize this data as a record like Player record. Now we can collect and store
player's records in a file or database as a data structure. For example: "Dhoni" 30, "Gambhir"
31, "Sehwag" 33
In simple language, Data Structures are structures programmed to store ordered data,
so that various operations can be performed on it easily.
Then we also have some complex Data Structures, which are used to store large and
connected data. Some example of Abstract Data Structure are :
Linked List
Tree
Graph
Stack, Queue etc.
We select these data structures based on which type of operation is required. We will
look into these data structures in more details in our later lessons.
Index − Each location of an element in an array has a numerical index which is used to
identify the element.
Basic Operations
Following are the basic operations supported by an array.
In C, when an array is initialized with size, then it assigns defaults values to its
elements in following order.
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the
requirement, new element can be added at the beginning, end or any given index of array.
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all
elements of an array.
Search Operation
You can perform a search for array element based on its value or its index.
Update Operation
Update operation refers to updating an existing element from the array at a given
index.
Each node consists of its own data and the address of the next node and forms a chain.
Linked Lists are used to create trees and graphs.
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Singly Linked List : Singly linked lists contain nodes which have a data part as well
as an address part i.e. next, which points to the next node in sequence of nodes. The operations
we can perform on singly linked lists are insertion, deletion and traversal.
Doubly Linked List : In a doubly linked list, each node contains two links the first link
points to the previous node and the next link points to the next node in the sequence.
Circular Linked List : In the circular linked list the last node of the list contains the
address of the first node and forms a circular chain.
Primitive data types are the basic units of a langauge; each primitive value is a single
datum and holds that datum directly.
We have seen examples of numbers and strings these are a the most basic primitive
data types.
They are simple and can hold text messages, frame numbers, counters, etc..
number.
string.
boolean.
undefined.
null.
Using composite data, we can manage multiple pieces of related data as a single datum.
As single variables but then when you wanted to add another person you would have to
remember to create all the variables but with slightly different names.
AS the number of people increased so would the complexity of your naming and the
relationship between the variables would be lost.
In Action Script you could create an object called person which had properties to store
the date of birth, sex, address etc, you would then create instances of that object.
array.
object.
movie clip.
functions.
Infix Notation
Parsing
Recursive Function
Now that we have a basic picture in mind of what a stack conceptually looks like, we
can define what underflow and overflow are.
Stack underflow happens when we try to pop (remove) an item from the stack, when
nothing is actually there to remove.
This will raise an alarm of sorts in the computer, because we told it to do something
that cannot be done.
Stack overflow happens when we try to push one more item onto our stack than it can
actually hold. You see, the stack usually can only hold so much stuff.
Typically, we allocate (set aside) where the stack is going to be in memory and how
big it can get.
So, when we stick too much stuff there or try to remove nothing, we will generate a
stack overflow condition or stack underflow condition, respectively.
a) Create an empty stack called opstack for keeping operators. Create an empty list for
output.
b) Convert the input infix string to a list by using the string method split.
c) Scan the token list from left to right.
a. If the token is an operand, append it to the end of the output list.
b. If the token is a left parenthesis, push it on the opstack.
c. If the token is a right parenthesis, pop the opstack until the corresponding left
parenthesis is removed. Append each operator to the end of the output list.
d. If the token is an operator, *, /, +, or -, push it on the opstack. However, first
remove any operators already on the opstack that have higher or equal
precedence and append them to the output list.
d) When the input expression has been completely processed, check the opstack. Any
operators still on the stack can be removed and appended to the end of the output list.
Parsing
Recursive Function
Calling Function
Expression Evaluation
Expression Conversion
o Infix to Postfix
o Infix to Prefix
o Postfix to Infix
o Prefix to Infix
Towers of hanoi
Expression Representation
(a + b) * (c - d) *+ab-cd ab+cd-*
To use a stack efficiently we need to check status of stack as well. For the same purpose,
the following functionality is added to stacks −
peek() − get the top data element of the stack, without removing it.
At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer
always represents the top of the stack, hence named top. The toppointer provides top value of
the stack without actually removing it.
Implementation of Stack
Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but
are limited in size and Linked List requires overhead to allocate, link, unlink, and
deallocate, but is not limited in size. Here we will implement Stack using array.
Operations
Stack operations may involve initializing the stack, using it and then de-initializing it.
Apart from these basic stuffs, a stack is used for the following two primary operations −
To use a stack efficiently we need to check status of stack as well. For the same purpose,
the following functionality is added to stacks −
peek() − get the top data element of the stack, without removing it.
2. If the stack is empty or contains a left parenthesis on top, push the incoming
operator onto the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the
operators until you see a left parenthesis. Discard the pair of parentheses.
5. If the incoming symbol has higher precedence than the top of the stack, push it on
the stack.
6. If the incoming symbol has equal precedence with the top of the stack, use
association. If the association is left to right, pop and print the top of the stack and
then push the incoming operator. If the association is right to left, push the incoming
operator.
7. If the incoming symbol has lower precedence than the symbol on the top of the
stack, pop the stack and print the top operator. Then test the incoming operator
against the new top of stack.
8. At the end of the expression, pop and print all operators on the stack. (No
parentheses should remain.)
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 1 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
Linked list is a collection of data in which each element contains the location of the next
element—that is, each element contains two parts: data and link.
The name of the list is the same as the name of this pointer variable.
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 2 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 3 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 4 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
Part- B
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 5 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
DELETING A NODE
Before deleting a node in a linked list, we apply the search algorithm.
If the flag returned from the search algorithm is true (the node is found), we can
delete the node from the linked list.
However, deletion is simpler than insertion: we have only two cases—
1. Deleting the first node and deleting any other node.
2. Deletion of the last and the middle nodes can be done by the same process.
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 6 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 7 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
A queue is an ordered collection of items where the addition of new items happens
at one end, called the “rear,” and the removal of existing items occurs at the other end,
commonly called the “front.”
Types of Operation in queue:
Queue() creates a new queue that is empty. It needs no parameters and returns an empty
queue.
Enqueue(item) adds a new item to the rear of the queue. It needs the item and returns
nothing.
Dequeue() removes the front item from the queue. It needs no parameters and returns the
item. The queue is modified.
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 8 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
IsEmpty() tests to see whether the queue is empty. It needs no parameters and returns a
boolean value.
Size() returns the number of items in the queue. It needs no parameters and returns an
integer.
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 9 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
What is a polynomial?
- Dynamic degree
PART – C
1) CREATION
2) INSERT AT STARTING
3) INSERT AT MIDDLE(USER'S CHOICE)
4) INSERT AT END
5) DELETE 1ST NODE
6) DELETE LAST NODE
7) DELETE MIDDLE NODE(USER'S CHOICE)
8) DISPLAY
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 10 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
Definition:
A doubly linked list is a linked data structure that consists of a set of sequentially
linked records called nodes. Each node contains two fields, called links, that are references
to the previous and to the next node in the sequence of nodes. The beginning and ending
nodes’ previous and next links.
Insertion of a node in a linked list can be done at three places, viz. at start, in
between at a specified location or at end.
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 11 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
Traverse the list to end. Let’s call the current last node of list as Last node.
Make next pointer of New node to point to NULL and prev pointer of new node to point
to Last node.
Update next pointer of Last node to point to new Node.
Reversing a list.
This is one of the favorite question which interviewer is bound to ask while
interviewing a candidate on data structures.
P(x) = 4x3+6x2+7x+9
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 12 of 13
ACADEMIC YEAR 2015-2018 REGULATION 2012
with 0. The coefficients of the respective exponent are placed at an appropriate index in
the array. The array representation for the above polynomial expression is given below:
struct polynomial
int coefficient;
int exponent;
};
Thus the above polynomial may be represented using linked list as shown below:
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 3 /ANSWER
Page 13 of 13
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
SECTION – A
(2 Marks)
1. What is Tree? Nov/Dec 2015 Apl/May 2015
A tree T is a finite, non-empty set of nodes.
T={r} U T1 U T2 U…….U Tn;
Properties:
1. A designated node of the set, r is called the root of the tree.
2. Remaining nodes are partitioned into n>=0 subsets, T1,T2…..Tn each of
which is a tree.
T= {R, T1, T2…..Tn} to denote the tree T.
2. Define the term: Binary Tree Nov 2014 April 2011 Nov 2010
A binary tree T is a finite set of nodes.
Properties:
1. Either the set is empty, T=NULL ; or
2. The set consists of a root, r, and exactly two distinct binary trees
TL and TR, T={r, TL,TR}.
A A
B D C
B
C E F
D E F G
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 1 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
5. Define BST.
A binary search tree is a rooted binary tree, whose internal nodes each store
a key (and optionally, an associated value) and each have two distinguished sub-trees,
commonly denoted left and right.
7. What is Degree?
Degree of a node is the number of subtrees associated with that node.
T={r,T1,T2,……,Tn},n>=0
E.g., the degree of tree T is n
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 2 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
B C D
1
Here, A! is the parent of B, C, and D. so B, C and D are the siblings.
1
Height of node – The height of a node is the number of edges on the longest
downward path between that node and a leaf.
Depth of node – The depth of a node is the number of edges from the node to the
tree's root node.
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 3 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
A A
B B
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 4 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
17. What are the three basic fields in each node of a Tree?
1. INFO(P) – contains the data stored in the linked list pointed to by P.
2. LEFT(P) – A pointer to the left child of the node pointed to by P.
3. RIGHT(P) – A pointer to the right child of the node pointed to by P.
A T
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 5 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
A T
Eg : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 6 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
SECTION – B
(5 Marks)
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 7 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
Procedure IN-Order :
Definition : Print out all the elements in the binary search tree in
order from smallest to largest.
Size : Nodes in the whole tree.
Base case : When P=NULL, do nothing.
General case : Traverse the left subtree in order.
Print INF(P)
Traverse the right subtree in order.
Example for IN-ORDER Tree Traversal:
F S
B H R V
G T Z
In - Order : BFGHPRSTWYZ
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 8 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
In General Tree :
In Binary Tree :
F S
B H R V
G T Z
4. Explain various operations on Binary Tree Nov 2014 & 2015 April 2011
Binary Tree : A binary tree T is a finite set of nodes.
Two Operations :
Inserting into binary search tree.
Deleting from a binary search tree.
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 10 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
5 5 5
9
9
L L
N P N P
C H C H
A F J A F
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 11 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
c) Delete C d) Delete L
L H
N P N P
A H
A F
Depth –The depth of a node is the number of edges from the node to the tree's root
node.
Forest – A forest is a set of n ≥ 0 disjoint trees.
4 2
2 6 1 4
3 7
1 3 5 7
5
b) Input : 1 2 3 4 5 6 7
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 13 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
PART – C QUESTIONS
1. Explain the functioning of Tree Traversal Methods in Detail? Nov 2014 Nov/Dec 2013
Tree Traversal:
Procedure:
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 14 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
A T
Procedure:
void postorder(tree_pointer ptr)
/* postorder tree traversal */
{
if (ptr) {
postorder(ptr->left_child);
postdorder(ptr->right_child);
printf(“%d”, ptr->data);
}
}
Eg : (BATM) Left Right Root
A T
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 15 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 16 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
The above properties of Binary Search Tree provide an ordering among keys so that
the operations like search, minimum and maximum can be done fast.
If there is no ordering, then we may have to compare every key to search a given key.
Searching a key:
To search a given key in Bianry Search Tree, we first compare it with root, if the key
is present at root, we return root.
If key is greater than root’s key, we recur for right subtree of root node. Otherwise we
recur for left subtree.
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 17 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
Insertion of a key
Once a leaf node is found, the new node is added as a child of the leaf node.
A new key is always inserted at leaf.
We start searching a key from root till we hit a leaf node.
100 100
/ \ Insert 40 / \
/ \ / \
10 30 10 30
40
/ \ delete(20) / \
30 70 ---------> 30 70
/ \ / \ \ / \
20 40 60 80 40 60 80
Node to be deleted has only one child: Copy the child to the node and delete the
child
50 50
/ \ delete(30) / \
30 70 ---------> 40 70
\ / \ / \
40 60 80 60 80
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 18 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
Node to be deleted has two children: Find inorder successor of the node. Copy
contents of the inorder successor to the node and delete the inorder successor. Note
that inorder predecessor can also be used.
50 60
/ \ delete(50) / \
40 70 ---------> 40 70
/ \ \
60 80 80
F S
B H R Y
G T Z
In-Order : B F G H P R S T W Y Z
Pre-Order : P F B H G S R Y T W Z
Post-order : B G H F R N T Z Y S P
b) Forest:
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 20 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
A A
B B
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 21 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
A Binary tree with n>=0 internal nodes contains n+1 external nodes.
The number of nodes pointing should be less than or equal to two, then tree is said to
be Binary Tree.
r
d
a e n s
r
d
a e n s
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 22 of 23
ACADEMIC YEAR: 2015 – 2018 REGULATION CBCS - 2012
If all the parents have 2 childrens, then the tree is said to be a complete tree.
If all the parents have not 2 childrens, then the tree is said to be an Incomplete
tree.
The left child of any parent will store the value lesser than its parent.
The Right child of any parent will store the values greater than its parent.
Example of Binary Tree construction:
a) Input : 4 2 6 1 3 5 7
2 6
1 3 5 7
RAAK / BSC (CS) / C. VICTOR VIMAL RAJ / II YEAR / III SEM /DATA STRUCTURES
USCS33 / UNIT- 4 /ANSWER
Page 23 of 23
ACADEMIC YEAR: 2016 – 2017 REGULATION CBCS - 2012
Syllabus:
UNIT V: Trees: Graph – Definition, Types of Graphs, Graphs Traversal – DFS and BFS.
Sorting by selection – Sorting by exchange (Bubble) – Sorting by insertion – Linear
Search – Binary Search.
PART – A QUESTIONS
a b V= {a,b,c,d,e}
E=
c {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
d e
A node which is not adjacent to any other node is called isolated node.
A graph which contains only isolated node is called null graph.
10. Define path and path length.
A path in a directed graph G=(V,E) is a non-empty sequence of vertices
P={v1;v2…….vk}
Where vi belongs to V for 1<=i<=k such that (vi,vi+1) belongs to E for 1<=i<=k.
The length of path p is k-1.
11. What is cycle and loop in graph?
A cycle is a path P of non-zero length in which v1=vk. The length of a cycle is just
the length of the path P.
A loop is a cycle of length one. i.e. it is a path of the form {v,v}
b c
A C E
S F
RAAK/BSC.CS /C VICTOR
G VIMAL RAJ/II
H YEAR/III Sem/USCS33
DATA STRUCTURES/UNIT-5 Q&A
Unit – 5 Q & A Page 3 of 23
ACADEMIC YEAR: 2016 – 2017 REGULATION CBCS - 2012
Output : ABSCGDEFH
A C E
S F
G H
Output : ABSCDEHGF
22. Write the average and best case analysis of insertion sort. Nov/Dec 2011
Average case analysis O(N2)
Best case analysis O(N)
It is simple to implement and efficient on small data sets and expensive
PART – B QUESTIONS
1. Explain the following Nov 2010
a) BFS
b) DFS Nov 2010
a) BFS - Breadth First Search
The aim of BFS algorithm is to traverse the graph as close as possible to the root
node.
BFS is one of the types of graph traversal.
Go to all nearest nodes first.
The data structure “queue” is used to store the visited nodes.
Expand from visited (but not dead) nodes.
Algorithm: while queue Q not empty
dequeue the first node u from Q
for each adjacent node v from u
if v is unvisited
enqueue v to Q
mark v as visited
Initialize all nodes as unvisited, except the starting node
Advantages
Shortest route is guaranteed on unweighted graphs
Avoid stack overflow
Example:
B D
A C E
S F
G H
Output : ABSCGDEFH
b) DFS - Depth-First-Search
The aim of DFS algorithm is to traverse the graph in such a way that it
tries to go far from the root node.
DFS is one of the types of graph traversal.
Once a possible path is found, continue the search until the end of the path
Algorithm:
DFS (node u) {
mark u as visited
for each adjacent node v from u
if (v is unvisited) DFS (v)
}
Initialize all nodes as unvisited
Advantages
Useful for checking whether 2 nodes are connected.
Drawbacks
Finding a shortest path using DFS is difficult.
Stack overflow.
Example:
B D
A C E
S F
G H
Output : ABSCDEHGF
a[j] = temp;
}
}
bool linear_search ( int *list, int size, int key, int* rec )
int i;
if ( key == list[i] )
break;
if ( i < size )
found = true;
rec = &list[i];
return found;
Example:
Assume the element 45 is searched from a sequence of sorted elements 12, 18, 25, 36, 45,
48, 50.
The Linear search starts from the first element 12, since the value to be searched is not
12 (value 45), the next element 18 is compared and is also not 45, by this way all the elements
before 45 are compared and when the index is 5, the element 45 is compared with the search
value and is equal, hence the element is found and the element position is 5.
4. Write an algorithm for quick sort and explain it with an example. April 2012,2015
Return results
PART – C QUESTIONS
a b V= {a,b,c,d,e}
E=
c {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
d e
Properties of Graph:
1. Complete Graph:
Complete graph is a graph in which there is an edge between every pair of vertices.
A complete graph with n vertices will have n(n-1)/2 edges.
V1 V2
V3 V4
2. Sub Graph:
A subgraph G’ of G is a graph G such that the set of vertices and set of edges of G’ are
proper subset of the set of edges of G.
G G’
V1 V1
V2 V3 V2
3. Connected Graph:
Any two vertices are connected by some path.
V1 V2
V3 V4
5. Cyclic Graph:
A directed graph is said to be a cyclic graph in which no vertex is repeated except the
first and last vertex are the same.
B C
6. Acyclic Graph:
A graph is said to be a cyclic graph if it has no cycles.
Directed Acyclic Graph(DAG):
A C
B
Undirected Acyclic Graph:
A C
7. Weighted Graph:
A graph is said to be weighted graph if every edge in the graph is assigned a weight or
value.
It can be either a directed or an undirected graph.
15
A C
10
1
A C
9
8. Bioconnectivity:
A bioconnectivity graph are graphs which cann’t be broke into disconnected graphs by
disconnectivity single edge.
If we remove any single edge the graph the graph does n’t become disconnected.
Example:
E1
V1 V2
RAAK/BSC.CS /C VICTOR VIMAL RAJ/II YEAR/III Sem/USCS33
V5
DATA STRUCTURES/UNIT-5 Q&A
V3 V4
Unit – 5 Q & A Page 18 of 23
ACADEMIC YEAR: 2016 – 2017 REGULATION CBCS - 2012
E5
E2 E3
E4 E6
V1 V2 E5
V5
E2 E3
V3 V4
E4 E6
All of the sequential search algorithms have the same problem; they walk over the entire
list.
Some of our improvements work to minimize the cost of traversing the whole data set,
but those improvements only cover up what is really a problem with the algorithm.
By thinking of the data in a different way, we can make speed improvements that are
much better than anything sequential search can guarantee.
Consider a list in ascending sorted order. It would work to search from the beginning
until an item is found or the end is reached, but it makes more sense to remove as much
of the working data set as possible so that the item is found more quickly.
If we started at the middle of the list we could determine which half the item is in
(because the list is sorted).
This effectively divides the working range in half with a single test. By repeating the
procedure, the result is a highly efficient search algorithm called binary search.
The actual algorithm is surprisingly tricky to implement considering the apparent
simplicity of the concept.
The necessity of this method is that all the elements should be sorted.
-40 11 33 37 42 45 99 100
Algorithm:
if(low>high)
Return;
Mid=(low+high)/2;
If(x==a[mid])
Return(mid);
If(x<a[mid])
Search for x in a[low] to a[mid-1];
Else
Search for x in a[mid+1] to a[high];
}
}
}