Csa Unit-7
Csa Unit-7
2020
Data Structures
Data Structure is a way to store and organize data so
that it can be used efficiently.
Data Structures are the main part of many computer science algorithms as they
enable the programmers to handle the data in an efficient way. It plays a vitle role
in enhancing the performance of a software or a program as the main function of
the software is to store and retrieve the user's data as fast as possible
Basic Terminology
Data structures are the building blocks of any program or the software. Choosing
the appropriate data structure for a program is the most difficult task for a
programmer. Following terminology is used as far as data structures are
concerned
Data: Data can be defined as an elementary value or the collection of values, for
example, student's name and its id are the data about the student.
Group Items: Data items which have subordinate data items are called Group
item, for example, name of a student can have first name and the last name.
Record: Record can be defined as the collection of various data items, for
example, if we talk about the student entity, then its name, address, course and
marks can be grouped together to form the record for the student.
2
Data Structures and Algorithms Unit – 7
File: A File is a collection of various records of one type of entity, for example, if
there are 60 employees in the class, then there will be 20 records in the related
file where each record contains the data about each employee.
Attribute and Entity: An entity represents the class of certain objects. it contains
various attributes. Each attribute represents the particular property of that entity.
Processor speed: To handle very large amout of data, high speed processing is
required, but as the data is growing day by day to the billions of files per entity,
processor may fail to deal with that much amount of data.
Data Search: Consider an inventory size of 106 items in a store, If our application
needs to search for a particular item, it needs to traverse 106 items every time,
results in slowing down the search process.
in order to solve the above problems, data structures are used. Data is organized
to form a data structure in such a way that all items are not required to be
searched and required data can be searched instantly.
3
Data Structures and Algorithms Unit – 7
to search sequentially element by element. hence, using array may not be very
efficient here. There are better data structures which can make the search
process efficient like ordered array, binary search tree or hash tables.
4
Data Structures and Algorithms Unit – 7
Linear Data Structures: A data structure is called linear if all of its elements are
arranged in the linear order. In linear data structures, the elements are stored in
non-hierarchical way where each element has the successors and predecessors
except the first and last element.
Arrays: An array is a collection of similar type of data items and each data item is
called an element of the array. The data type of the element may be any valid
data type like char, int, float or double.
5
Data Structures and Algorithms Unit – 7
The elements of array share the same variable name but each one carries a
different index number known as subscript. The array can be one dimensional,
two dimensional or multidimensional.
Linked List: Linked list is a linear data structure which is used to maintain a list in
the memory. It can be seen as the collection of nodes stored at non-contiguous
memory locations. Each node of the list contains a pointer to its adjacent node.
Stack: Stack is a linear list in which insertion and deletions are allowed only at one
end, called top.
Queue: Queue is a linear list in which elements can be inserted only at one end
called rear and deleted only at the other end called front.
Non Linear Data Structures: This data structure does not form a sequence i.e.
each item or element is connected with two or more other items in a non-linear
arrangement. The data elements are not arranged in sequential structure.
Trees: Trees are multilevel data structures with a hierarchical relationship among
its elements known as nodes. The bottommost nodes in the herierchy are
called leaf node while the topmost node is called root node. Each node contains
pointers to point adjacent nodes.
6
Data Structures and Algorithms Unit – 7
Tree data structure is based on the parent-child relationship among the nodes.
Each node in the tree can have more than one children except the leaf nodes
whereas each node can have atmost one parent except the root node. Trees can
be classfied into many categories which will be discussed later in this tutorial.
1) Traversing: Every data structure contains the set of data elements. Traversing
the data structure means visiting each element of the data structure in order to
perform some specific operation like searching or sorting.
If the size of data structure is n then we can only insert n-1 data elements into it.
4) Searching: The process of finding the location of an element within the data
structure is called Searching. There are two algorithms to perform searching,
7
Data Structures and Algorithms Unit – 7
Linear Search and Binary Search. We will discuss each one of them later in this
tutorial.
5) Sorting: The process of arranging the data structure in a specific order is known
as Sorting. There are many algorithms that can be used to perform sorting, for
example, insertion sort, selection sort, bubble sort, etc.
6) Merging: When two lists List A and List B of size M and N respectively, of
similar type of elements, clubbed or joined to produce the third list, List C of size
(M+N), then this process is called merging
Algorithm
o Delete: Algorithm developed for deleting the existing element from the
data structure.
8
Data Structures and Algorithms Unit – 7
Example: Design an algorithm to multiply the two numbers x and y and display
the result in z.
o Step 1 START
o Step 6 print z
o Step 7 STOP
9
Data Structures and Algorithms Unit – 7
o Step 3 z← x * y
o Step 4 display z
o Step 5 STOP
Characteristics of an Algorithm
Array
o Arrays are defined as the collection of similar type of data items stored at
contiguous memory locations.
o Arrays are the derived data type in C programming language which can
store the primitive type of data such as int, char, double, float, etc.
o Array is the simplest data structure where each data element can be
randomly accessed by using its index number.
10
Data Structures and Algorithms Unit – 7
subject. instead of that, we can define an array which can store the marks
in each subject at a the contiguous memory locations.
The array marks[10] defines the marks of the student in 10 different subjects
where each subject marks are located at a particular subscript in the array
i.e. marks[0] denotes the marks in first subject, marks[1] denotes the marks in
2nd subject and so on.
1. Each element is of same data type and carries a same size i.e. int = 4 bytes.
2. Elements of the array are stored at contiguous memory locations where the
first element is stored at the smallest memory location.
3. Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the
size of data element.
In computer programming, the most of the cases requires to store the large
number of data of similar type. To store such amount of data, we need to define a
large number of variables. It would be very difficult to remember names of all the
variables while writing the programs. Instead of naming all the variables with a
different name, it is better to define an array and store all the elements into it.
Following example illustrates, how array can be useful in writing code for a
particular problem.
In the following example, we have marks of a student in six different subjects. The
problem intends to calculate the average of all the marks of the student.
11
Data Structures and Algorithms Unit – 7
In order to illustrate the importance of array, we have created two programs, one
is without using array and other involves the use of array to store marks.
1. #include <stdio.h>
2. void main ()
3. {
4. int marks_1 = 56, marks_2 = 78, marks_3 = 88, marks_4 = 76, marks_5 =
56, marks_6 = 89;
6. printf(avg);
7. }
1. #include <stdio.h>
2. void main ()
3. {
5. int i;
6. float avg;
8. {
12
Data Structures and Algorithms Unit – 7
10. }
11. printf(avg);
12. }
Time and space complexity of various array operations are described in the
following table.
Time Complexity
Space Complexity
Advantages of Array
o Array provides the single name for the group of variables of the same type
therefore, it is easy to remember the name of all the elements of an array.
13
Data Structures and Algorithms Unit – 7
o Any element in the array can be directly accessed by using the index.
As we have mentioned, all the data elements of an array are stored at contiguous
locations in the main memory. The name of the array represents the base address
or the address of first element in the main memory. Each element of the array is
represented by a proper indexing.
1. 0 (zero - based indexing) : The first element of the array will be arr[0].
2. 1 (one - based indexing) : The first element of the array will be arr[1].
3. n (n - based indexing) : The first element of the array can reside at any
random index number.
In the following image, we have shown the memory allocation of an array arr of
size 5. The array follows 0-based indexing approach. The base address of the array
is 100th byte. This will be the address of arr[0]. Here, the size of int is 4 bytes
therefore each element will take 4 bytes in the memory.
14
Data Structures and Algorithms Unit – 7
In 0 based indexing, If the size of an array is n then the maximum index number,
an element can have is n-1. However, it will be n if we use 1 based indexing.
Example :
1. In an array, A[-
10 ..... +2 ], Base address (BA) = 999, size of an element = 2 bytes,
15
Data Structures and Algorithms Unit – 7
4. = 999 + 18
5. = 1017
As we have mentioned earlier that, the name of the array represents the starting
address or the address of the first element of the array. All the elements of the
array can be traversed by using the base address.
The following example illustrate, how the array can be passed to a function.
Example:
1. #include <stdio.h>
2. int summation(int[]);
3. void main ()
4. {
7. printf("%d",sum);
8. }
9.
11. {
14. {
16. }
18. }
2D Array
The syntax of declaring two dimensional array is very much similar to that of a
one dimensional array, given as follows.
1. int arr[max_rows][max_columns];
17
Data Structures and Algorithms Unit – 7
Above image shows the two dimensional array, the elements are organized in the
form of rows and columns. First element of the first row is represented by a[0][0]
where the number shown in the first index is the number of that row while the
number shown in the second index is the number of the column.
Due to the fact that the elements of 2D arrays can be random accessed. Similar to
one dimensional arrays, we can access the individual cells in a 2D array by using
the indices of the cells. There are two indices attached to a particular cell, one is
its row number while the other is its column number.
18
Data Structures and Algorithms Unit – 7
However, we can store the value stored in any particular cell of a 2D array to
some variable x by using the following syntax.
1. int x = a[i][j];
where i and j is the row and column number of the cell respectively.
2. {
4. {
5. a[i][j] = 0;
6. }
7. }
Initializing 2D Arrays
The number of elements that can be present in a 2D array will always be equal to
(number of rows * number of columns).
C Example :
19
Data Structures and Algorithms Unit – 7
1. #include <stdio.h>
2. void main ()
3. {
4. int arr[3][3],i,j;
5. for (i=0;i<3;i++)
6. {
7. for (j=0;j<3;j++)
8. {
10. scanf("%d",&arr[i][j]);
11. }
12. }
14. for(i=0;i<3;i++)
15. {
16. printf("\n");
18. {
19. printf("%d\t",arr[i][j]);
20. }
21. }
20
Data Structures and Algorithms Unit – 7
22. }
Java Example
1. import java.util.Scanner;
2. publicclass TwoDArray {
7. {
8. for(intj=0;j<3;j++)
9. {
11. arr[i][j]=sc.nextInt();
12. System.out.println();
13. }
14. }
16. for(inti=0;i<3;i++)
17. {
18. System.out.println();
19. for(intj=0;j<3;j++)
21
Data Structures and Algorithms Unit – 7
20. {
21. System.out.print(arr[i][j]+"\t");
22. }
23. }
24. }
25. }
C# Example
1. using System;
2.
4. {
6. {
9. {
11. {
14. }
22
Data Structures and Algorithms Unit – 7
15. }
18. {
19. Console.WriteLine();
21. {
23. }
24. }
25. }
26. }
When it comes to map a 2 dimensional array, most of us might think that why this
mapping is required. However, 2 D arrays exists from the user point of view. 2D
arrays are created to implement a relational database table lookalike data
structure, in computer memory, the storage technique for 2D array is similar to
that of an one dimensional array.
23
Data Structures and Algorithms Unit – 7
There are two main techniques of storing 2D array elements into memory
In row major ordering, all the rows of the 2D array are stored into the memory
contiguously. Considering the array shown in the above image, its memory
allocation according to row major order is shown as follows.
first, the 1st row of the array is stored into the memory completely, then the
2nd row of the array is stored into the memory completely and so on till the last
row.
24
Data Structures and Algorithms Unit – 7
According to the column major ordering, all the columns of the 2D array are
stored into the memory contiguously. The memory allocation of the array which is
shown in in the above image is given as follows.
first, the 1st column of the array is stored into the memory completely, then the
2nd row of the array is stored into the memory completely and so on till the last
column of the array.
25
Data Structures and Algorithms Unit – 7
Due to the fact that, there are two different techniques of storing the two
dimensional array into the memory, there are two different formulas to calculate
the address of a random element of the 2D array.
1. Address(a[i][j]) = B. A. + (i * n + j) * size
where, B. A. is the base address or the address of the first element of the array
a[0][0] .
Example :
26
Data Structures and Algorithms Unit – 7
Address(a[15][68]) = 0 +
= (5 x 14 + 13) x 4
= 83 x 4
= 332 answer
1. Address(a[i][j]) = ((j*m)+i)*Size + BA
Example:
A [-
5 ... +20][20 ... 70], BA = 1020, Size of element = 8 bytes. Find the location of a[
0][30].
Linked List
o Linked List can be defined as collection of objects called nodes that are
randomly stored in the memory.
27
Data Structures and Algorithms Unit – 7
o A node contains two fields i.e. data stored at that particular address and
the pointer which contains the address of the next node in the memory.
o The list is not required to be contiguously present in the memory. The node
can reside any where in the memory and linked together to make a list. This
achieves optimized utilization of space.
o list size is limited to the memory size and doesn't need to be declared in
advance.
o We can store values of primitive types or objects in the singly linked list.
Till now, we were using array data structure to organize the group of elements
that are to be stored individually in the memory. However, Array has several
advantages and disadvantages which must be known in order to decide the data
structure which will be used throughout the program.
1. The size of array must be known in advance before using it in the program.
28
Data Structures and Algorithms Unit – 7
3. All the elements in the array need to be contiguously stored in the memory.
Inserting any element in the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array.
Using linked list is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-
contiguously stored in the memory and linked together with the help of
pointers.
2. Sizing is no longer a problem since we do not need to define its size at the
time of declaration. List grows as per the program's demand and limited to
the available memory space.
Singly linked list can be defined as the collection of ordered set of elements. The
number of elements may vary according to need of the program. A node in the
singly linked list consist of two parts: data part and link part. Data part of the node
stores actual information that is to be represented by the node while the link part
of the node stores the address of its immediate successor.
One way chain or singly linked list can be traversed only in one direction. In other
words, we can say that each node contains only next pointer, therefore we can
not traverse the list in the reverse direction.
Consider an example where the marks obtained by the student in three subjects
are stored in a linked list as shown in the figure.
29
Data Structures and Algorithms Unit – 7
In the above figure, the arrow represents the links. The data part of every node
contains the marks obtained by the student in the different subject. The last node
in the list is identified by the null pointer which is present in the address part of
the last node. We can have as many elements we require, in the data part of the
list.
Complexity
30
Data Structures and Algorithms Unit – 7
Singly θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
Linked
List
There are various operations which can be performed on singly linked list. A list of
all such operations is given below.
Node Creation
1. struct node
2. {
3. int data;
5. };
Insertion
The insertion into a singly linked list can be performed at different positions.
Based on the position of the new node being inserted, the insertion is categorized
into the following categories.
SN Operation Description
31
Data Structures and Algorithms Unit – 7
2 Insertion at It involves insertion at the last of the linked list. The new
end of the node can be inserted as the only node in the list or it can be
list inserted as the last one. Different logics are implemented in
each scenario.
3 Insertion It involves insertion after the specified node of the linked list.
after We need to skip the desired number of nodes in order to
specified reach the node after which the new node will be inserted. .
node
The Deletion of a node from a singly linked list can be performed at different
positions. Based on the position of the node being deleted, the operation is
categorized into the following categories.
SN Operation Description
32
Data Structures and Algorithms Unit – 7
2 Deletion at It involves deleting the last node of the list. The list can
the end of either be empty or full. Different logic is implemented for
the list the different scenarios.
3 Deletion It involves deleting the node after the specified node in the
after list. we need to skip the desired number of nodes to reach
specified the node after which the node will be deleted. This
node requires traversing through the list.
1. #include<stdio.h>
2. #include<stdlib.h>
3. struct node
4. {
5. int data;
33
Data Structures and Algorithms Unit – 7
7. };
9.
19. {
21. while(choice != 9)
22. {
25. printf("\n===============================================\n")
;
34
Data Structures and Algorithms Unit – 7
29. scanf("\n%d",&choice);
30. switch(choice)
31. {
32. case 1:
33. beginsert();
34. break;
35. case 2:
36. lastinsert();
37. break;
38. case 3:
39. randominsert();
40. break;
41. case 4:
42. begin_delete();
43. break;
44. case 5:
45. last_delete();
35
Data Structures and Algorithms Unit – 7
46. break;
47. case 6:
48. random_delete();
49. break;
50. case 7:
51. search();
52. break;
53. case 8:
54. display();
55. break;
56. case 9:
57. exit(0);
58. break;
59. default:
61. }
62. }
63. }
65. {
36
Data Structures and Algorithms Unit – 7
70. {
71. printf("\nOVERFLOW");
72. }
73. else
74. {
76. scanf("%d",&item);
81. }
82.
83. }
85. {
37
Data Structures and Algorithms Unit – 7
90. {
91. printf("\nOVERFLOW");
92. }
93. else
94. {
96. scanf("%d",&item);
99. {
103. }
104. else
105. {
108. {
38
Data Structures and Algorithms Unit – 7
110. }
114.
115. }
116. }
117. }
119. {
124. {
125. printf("\nOVERFLOW");
126. }
127. else
128. {
39
Data Structures and Algorithms Unit – 7
130. scanf("%d",&item);
132. printf("\nEnter the location after which you want to insert ");
133. scanf("\n%d",&loc);
134. temp=head;
135. for(i=0;i<loc;i++)
136. {
139. {
141. return;
142. }
143.
144. }
148. }
149. }
40
Data Structures and Algorithms Unit – 7
151. {
154. {
156. }
157. else
158. {
161. free(ptr);
163. }
164. }
166. {
169. {
171. }
41
Data Structures and Algorithms Unit – 7
173. {
175. free(head);
177. }
178.
179. else
180. {
183. {
186. }
188. free(ptr);
190. }
191. }
42
Data Structures and Algorithms Unit – 7
193. {
196. printf("\n Enter the location of the node after which you want to p
erform deletion \n");
197. scanf("%d",&loc);
198. ptr=head;
199. for(i=0;i<loc;i++)
200. {
203.
205. {
207. return;
208. }
209. }
211. free(ptr);
43
Data Structures and Algorithms Unit – 7
213. }
215. {
220. {
222. }
223. else
224. {
226. scanf("%d",&item);
228. {
230. {
232. flag=0;
233. }
44
Data Structures and Algorithms Unit – 7
234. else
235. {
236. flag=1;
237. }
238. i++;
240. }
241. if(flag==1)
242. {
244. }
245. }
246.
247. }
248.
250. {
254. {
45
Data Structures and Algorithms Unit – 7
256. }
257. else
258. {
261. {
262. printf("\n%d",ptr->data);
264. }
265. }
266. }
267.
Output:
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
46
Data Structures and Algorithms Unit – 7
8.Show
9.Exit
Enter value
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
47
Data Structures and Algorithms Unit – 7
8.Show
9.Exit
Enter value?
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
48
Data Structures and Algorithms Unit – 7
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
printing values . . . . .
49
Data Structures and Algorithms Unit – 7
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value?
123
Node inserted
50
Data Structures and Algorithms Unit – 7
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
Enter value
1234
Node inserted
*********Main Menu*********
51
Data Structures and Algorithms Unit – 7
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
*********Main Menu*********
===============================================
1.Insert in begining
52
Data Structures and Algorithms Unit – 7
2.Insert at last
8.Show
9.Exit
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
53
Data Structures and Algorithms Unit – 7
8.Show
9.Exit
Enter the location of the node after which you want to perform deletion
Deleted node 2
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
54
Data Structures and Algorithms Unit – 7
9.Exit
printing values . . . . .
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
55
Data Structures and Algorithms Unit – 7
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
8.Show
9.Exit
56
Data Structures and Algorithms Unit – 7
Doubly linked list is a complex type of linked list in which a node contains a
pointer to the previous as well as the next node in the sequence. Therefore, in a
doubly linked list, a node consists of three parts: node data, pointer to the next
node in sequence (next pointer) , pointer to the previous node (previous pointer).
A sample node in a doubly linked list is shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their
data part, is shown in the following image.
57
Data Structures and Algorithms Unit – 7
1. struct node
2. {
4. int data;
6. }
The prev part of the first node and the next part of the last node will always
contain null indicating end in each direction.
In a singly linked list, we could traverse only in one direction, because each node
contains address of the next node and it doesn't have any record of its previous
nodes. However, doubly linked list overcome this limitation of singly linked list.
Due to the fact that, each node of the list contains the address of its previous
node, we can find all the details about the previous node as well by using the
previous address stored inside the previous part of each node.
In the following image, the first element of the list that is i.e. 13 stored at address
1. The head pointer points to the starting address 1. Since this is the first element
being added to the list therefore the prev of the list contains null. The next node
of the list resides at address 4 therefore the first node contains 4 in its next
pointer.
58
Data Structures and Algorithms Unit – 7
We can traverse the list in this way until we find any node containing null or -1 in
its next part.
Node Creation
1. struct node
2. {
59
Data Structures and Algorithms Unit – 7
4. int data;
6. };
All the remaining operations regarding doubly linked list are described in the
following table.
SN Operation Description
2 Insertion at Adding the node into the linked list to the end.
end
3 Insertion Adding the node into the linked list after the specified
after node.
specified
node
60
Data Structures and Algorithms Unit – 7
6 Deletion of Removing the node which is present just after the node
the node containing the given data.
having
given data
Menu Driven Program in C to implement all the operations of doubly linked list
1. #include<stdio.h>
2. #include<stdlib.h>
3. struct node
4. {
7. int data;
8. };
19. {
21. while(choice != 9)
22. {
25. printf("\n===============================================\n")
;
27. 5.Delete from last\n6.Delete the node after the given data\n7.Search\
n8.Show\n9.Exit\n");
29. scanf("\n%d",&choice);
30. switch(choice)
62
Data Structures and Algorithms Unit – 7
31. {
32. case 1:
33. insertion_beginning();
34. break;
35. case 2:
36. insertion_last();
37. break;
38. case 3:
39. insertion_specified();
40. break;
41. case 4:
42. deletion_beginning();
43. break;
44. case 5:
45. deletion_last();
46. break;
47. case 6:
48. deletion_specified();
49. break;
50. case 7:
51. search();
63
Data Structures and Algorithms Unit – 7
52. break;
53. case 8:
54. display();
55. break;
56. case 9:
57. exit(0);
58. break;
59. default:
61. }
62. }
63. }
65. {
70. {
71. printf("\nOVERFLOW");
72. }
64
Data Structures and Algorithms Unit – 7
73. else
74. {
76. scanf("%d",&item);
77.
78. if(head==NULL)
79. {
81. ptr->prev=NULL;
82. ptr->data=item;
83. head=ptr;
84. }
85. else
86. {
87. ptr->data=item;
88. ptr->prev=NULL;
90. head->prev=ptr;
91. head=ptr;
92. }
65
Data Structures and Algorithms Unit – 7
94. }
95.
96. }
98. {
103. {
104. printf("\nOVERFLOW");
105. }
106. else
107. {
109. scanf("%d",&item);
110. ptr->data=item;
112. {
66
Data Structures and Algorithms Unit – 7
116. }
117. else
118. {
120. while(temp->next!=NULL)
121. {
123. }
127. }
128.
129. }
131. }
133. {
67
Data Structures and Algorithms Unit – 7
138. {
140. }
141. else
142. {
143. temp=head;
145. scanf("%d",&loc);
146. for(i=0;i<loc;i++)
147. {
150. {
152. return;
153. }
154. }
156. scanf("%d",&item);
68
Data Structures and Algorithms Unit – 7
161. temp->next->prev=ptr;
163. }
164. }
166. {
169. {
171. }
173. {
175. free(head);
177. }
69
Data Structures and Algorithms Unit – 7
178. else
179. {
183. free(ptr);
185. }
186.
187. }
189. {
192. {
194. }
196. {
198. free(head);
70
Data Structures and Algorithms Unit – 7
200. }
201. else
202. {
205. {
207. }
209. free(ptr);
211. }
212. }
214. {
217. printf("\n Enter the data after which the node is to be deleted : ");
71
Data Structures and Algorithms Unit – 7
223. {
225. }
227. {
229. }
230. else
231. {
235. free(temp);
237. }
238. }
240. {
72
Data Structures and Algorithms Unit – 7
245. {
246. printf("%d\n",ptr->data);
247. ptr=ptr->next;
248. }
249. }
251. {
256. {
258. }
259. else
260. {
73
Data Structures and Algorithms Unit – 7
262. scanf("%d",&item);
264. {
266. {
268. flag=0;
269. break;
270. }
271. else
272. {
273. flag=1;
274. }
275. i++;
277. }
278. if(flag==1)
279. {
281. }
282. }
74
Data Structures and Algorithms Unit – 7
283.
284. }
Output
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
printing values...
*********Main Menu*********
75
Data Structures and Algorithms Unit – 7
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
76
Data Structures and Algorithms Unit – 7
7.Search
8.Show
9.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
77
Data Structures and Algorithms Unit – 7
8.Show
9.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
78
Data Structures and Algorithms Unit – 7
printing values...
1234
123
12
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Enter value89
node inserted
79
Data Structures and Algorithms Unit – 7
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Enter value12345
node inserted
*********Main Menu*********
80
Data Structures and Algorithms Unit – 7
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
printing values...
1234
123
12345
12
89
*********Main Menu*********
81
Data Structures and Algorithms Unit – 7
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
node deleted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
82
Data Structures and Algorithms Unit – 7
7.Search
8.Show
9.Exit
node deleted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
83
Data Structures and Algorithms Unit – 7
printing values...
123
12345
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
84
Data Structures and Algorithms Unit – 7
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
printing values...
123
*********Main Menu*********
85
Data Structures and Algorithms Unit – 7
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
123
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
86
Data Structures and Algorithms Unit – 7
7.Search
8.Show
9.Exit
Can't delete
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
87
Data Structures and Algorithms Unit – 7
7.Search
8.Show
9.Exit
Exited..
In a circular Singly linked list, the last node of the list contains a pointer to the first
node of the list. We can have circular singly linked list as well as circular doubly
linked list.
We traverse a circular singly linked list until we reach the same node where we
started. The circular singly liked list has no beginning and no ending. There is no
null value present in the next part of any of the nodes.
88
Data Structures and Algorithms Unit – 7
Circular linked list are mostly used in task maintenance in operating systems.
There are many examples where circular linked list are being used in computer
science including browser surfing where a record of pages visited in the past by
the user, is maintained in the form of circular linked lists and can be accessed
again on clicking the previous button.
However, due to the fact that we are considering circular linked list in the
memory therefore the last node of the list contains the address of the first node
of the list.
89
Data Structures and Algorithms Unit – 7
We can also have more than one number of linked list in the memory with the
different start pointers pointing to the different start nodes in the list. The last
node is identified by its next part which contains the address of the start node of
the list. We must be able to identify the last node of any linked list so that we can
find out the number of iterations which need to be performed while traversing
the list.
Insertion
90
Data Structures and Algorithms Unit – 7
SN Operation Description
2 Insertion at the Adding a node into circular singly linked list at the
end end.
SN Operation Description
1 Deletion at Removing the node from circular singly linked list at the
beginning beginning.
2 Deletion at Removing the node from circular singly linked list at the
the end end.
3 Searching Compare each element of the node with the given item
and return the location at which the item is present in the
list otherwise return null.
91
Data Structures and Algorithms Unit – 7
Circular doubly linked list is a more complexed type of data structure in which a
node contain pointers to its previous node as well as the next node. Circular
doubly linked list doesn't contain NULL in any of the node. The last node of the list
contains the address of the first node of the list. The first node of the list also
contain address of the last node in its previous pointer.
Due to the fact that a circular doubly linked list contains three parts in its
structure therefore, it demands more space per node and more expensive basic
operations. However, a circular doubly linked list provides easy manipulation of
the pointers and the searching becomes twice as efficient.
The following figure shows the way in which the memory is allocated for a circular
doubly linked list. The variable head contains the address of the first element of
the list i.e. 1 hence the starting node of the list contains data A is stored at
address 1. Since, each node of the list is supposed to have three parts therefore,
the starting node of the list contains address of the last node i.e. 8 and the next
node i.e. 4. The last node of the list that is stored at address 8 and containing data
as 6, contains address of the first node of the list as shown in the image i.e. 1. In
92
Data Structures and Algorithms Unit – 7
circular doubly linked list, the last node is identified by the address of the first
node which is stored in the next part of the last node therefore the node which
contains the address of the first node, is actually the last node of the list.
There are various operations which can be performed on circular doubly linked
list. The node structure of a circular doubly linked list is similar to doubly linked
list. However, the operations on circular doubly linked list is described in the
following table.
93
Data Structures and Algorithms Unit – 7
SN Operation Description
2 Insertion at end Adding a node in circular doubly linked list at the end.
Traversing and searching in circular doubly linked list is similar to that in the
circular singly linked list.
Stack
3. Stacks are sometimes called as Last-In-First-Out (LIFO) lists i.e. the element
which is inserted first in the stack, will be deleted last from the stack.
Applications of Stack
1. Recursion
3. Parsing
4. Browsers
94
Data Structures and Algorithms Unit – 7
5. Editors
6. Tree Traversals
Operations on Stack
95
Data Structures and Algorithms Unit – 7
The stack is called empty if it doesn't contain any element inside it. At this stage,
the value of variable top is -1.
96
Data Structures and Algorithms Unit – 7
Value of top will get increased by 1 every time when we add any element to the
stack. In the following stack, After adding first element, top = 2.
Value of top will get decreased by 1 whenever an element is deleted from the
stack.
97
Data Structures and Algorithms Unit – 7
-1 Empty
N Overflow
98
Data Structures and Algorithms Unit – 7
In array implementation, the stack is formed by using the array. All the operations
regarding the stack are performed using arrays. see how each operation can be
implemented on the stack using array data structure.
Adding an element into the top of the stack is referred to as push operation. Push
operation involves following two steps.
1. Increment the variable Top so that it can now refere to the next memory
location.
Stack is overflown when we try to insert an element into a completely filled stack
therefore, our main function must always avoid stack overflow condition.
Algorithm:
1. begin
3. top = top + 1
5. end
2. {
3. if (top == n )
99
Data Structures and Algorithms Unit – 7
4. printf("\n Overflow");
5. else
6. {
8. stack[top] = val;
9. }
10. }
Deletion of an element from the top of the stack is called pop operation. The
value of the variable top will be incremented by 1 whenever an item is deleted
from the stack. The top most element of the stack is stored in an another variable
and then the top is decremented by 1. the operation returns the deleted value
that was stored in another variable as the result.
Algorithm :
1. begin
3. item := stack(top);
4. top = top - 1;
5. end;
1. int pop ()
2. {
3. if(top == -1)
4. {
5. printf("Underflow");
6. return 0;
7. }
8. else
9. {
11. }
12. }
Peek operation involves returning the element which is present at the top of the
stack without deleting it. Underflow condition can occur if we try to return the
top element in an already empty stack.
Algorithm :
1. Begin
3. item = stack[top]
4. return item
101
Data Structures and Algorithms Unit – 7
5. End
1. int peek()
2. {
3. if (top == -1)
4. {
5. printf("Underflow");
6. return 0;
7. }
8. else
9. {
11. }
12. }
Instead of using array, we can also use linked list to implement stack. Linked list
allocates the memory dynamically. However, time complexity in both the scenario
is same for all the operations i.e. push, pop and peek.
102
Data Structures and Algorithms Unit – 7
the stack. Stack is said to be overflown if the space left in the memory heap is not
enough to create a node.
The top most node in the stack always contains null in its address field. Lets
discuss the way in which, each operation is performed in linked list
implementation of stack.
103
Data Structures and Algorithms Unit – 7
2. If the list is empty then the item is to be pushed as the start node of the list.
This includes assigning value to the data part of the node and assign null to
the address part of the node.
3. If there are some nodes in the list already, then we have to add the new
element in the beginning of the list (to not violate the property of the
stack). For this purpose, assign the address of the starting element to the
address field of the new node and make the new node, the starting node of
the list.
104
Data Structures and Algorithms Unit – 7
C implementation :
1. void push ()
2. {
105
Data Structures and Algorithms Unit – 7
3. int val;
5. if(ptr == NULL)
6. {
8. }
9. else
10. {
12. scanf("%d",&val);
13. if(head==NULL)
14. {
17. head=ptr;
18. }
19. else
20. {
23. head=ptr;
106
Data Structures and Algorithms Unit – 7
24.
25. }
27.
28. }
29. }
Deleting a node from the top of stack is referred to as pop operation. Deleting a
node from the linked list implementation of stack is different from that in the
array implementation. In order to pop an element from the stack, we need to
follow the following steps :
30. Check for the underflow condition: The underflow condition occurs
when we try to pop from an already empty stack. The stack will be
empty if the head pointer of the list points to null.
31. Adjust the head pointer accordingly: In stack, the elements are
popped only from one end, therefore, the value stored in the head
pointer must be deleted and the node must be freed. The next node
of the head node now becomes the head node.
C implementation
1. void pop()
2. {
3. int item;
5. if (head == NULL)
6. {
7. printf("Underflow");
8. }
9. else
10. {
14. free(ptr);
16.
17. }
18. }
Displaying all the nodes of a stack needs traversing all the nodes of the linked list
organized in the form of stack. For this purpose, we need to follow the following
steps.
20. Move the temporary pointer through all the nodes of the list and
print the value field attached to every node.
108
Data Structures and Algorithms Unit – 7
C Implementation
1. void display()
2. {
3. int i;
5. ptr=head;
6. if(ptr == NULL)
7. {
8. printf("Stack is empty\n");
9. }
10. else
11. {
13. while(ptr!=NULL)
14. {
15. printf("%d\n",ptr->val);
17. }
18. }
19. }
Queue
109
Data Structures and Algorithms Unit – 7
3. For example, people waiting in line for a rail ticket form a queue.
Applications of Queue
Due to the fact that queue performs actions on first in first out basis which is
quite fair for the ordering of actions. There are various applications of queues
discussed as below.
1. Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
2. Queues are used in asynchronous transfer of data (where data is not being
transferred at the same rate between two processes) for eg. pipes, file IO,
sockets.
3. Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
4. Queue are used to maintain the play list in media players in order to add
and remove the songs from the play-list.
110
Data Structures and Algorithms Unit – 7
Complexity
Queue θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
We can easily represent queue by using linear arrays. There are two variables i.e.
front and rear, that are implemented in the case of every queue. Front and rear
variables point to the position from where insertions and deletions are performed
in a queue. Initially, the value of front and queue is -1 which represents an empty
queue. Array representation of a queue containing 5 elements along with the
respective values of front and rear, is shown in the following figure.
111
Data Structures and Algorithms Unit – 7
The above figure shows the queue of characters forming the English
word "HELLO". Since, No deletion is performed in the queue till now, therefore
the value of front remains -1 . However, the value of rear increases by one every
time an insertion is performed in the queue. After inserting an element into the
queue shown in the above figure, the queue will look something like following.
The value of rear will become 5 while the value of front remains same.
After deleting an element, the value of front will increase from -1 to 0. however,
the queue will look something like following.
112
Data Structures and Algorithms Unit – 7
Check if the queue is already full by comparing rear to max - 1. if so, then return
an overflow error.
If the item is to be inserted as the first element in the list, in that case set the
value of front and rear to 0 and insert the element at the rear end.
Otherwise keep increasing the value of rear and insert each element one by one
having rear as the index.
Algorithm
113
Data Structures and Algorithms Unit – 7
o Step 4: EXIT
C Function
1. void insert (int queue[], int max, int front, int rear, int item)
2. {
3. if (rear + 1 == max)
4. {
5. printf("overflow");
6. }
7. else
8. {
10. {
11. front = 0;
12. rear = 0;
13. }
14. else
15. {
114
Data Structures and Algorithms Unit – 7
17. }
18. queue[rear]=item;
19. }
20. }
If, the value of front is -1 or value of front is greater than rear , write an underflow
message and exit.
Otherwise, keep increasing the value of front and return the item stored at the
front end of the queue at each time.
Algorithm
o Step 2: EXIT
C Function
1. int delete (int queue[], int max, int front, int rear)
2. {
3. int y;
5.
115
Data Structures and Algorithms Unit – 7
6. {
7. printf("underflow");
8. }
9. else
10. {
11. y = queue[front];
13. {
15. else
17.
18. }
19. return y;
20. }
21. }
Although, the technique of creating a queue is easy, but there are some
drawbacks of using this technique to implement a queue.
o Memory wastage : The space of the array, which is used to store queue
elements, can never be reused to store the elements of that queue because
116
Data Structures and Algorithms Unit – 7
the elements can only be inserted at front end and the value of front might
be so high so that, all the space before that, can never be filled.
The above figure shows how the memory space is wasted in the array
representation of queue. In the above figure, a queue of size 10 having 3
elements, is shown. The value of the front variable is 5, therefore, we can not
reinsert the values in the place of already deleted element before the position of
front. That much space of the array is wasted and can not be used in the future
(for this queue).
On of the most common problem with array implementation is the size of the
array which requires to be declared in advance. Due to the fact that, the queue
can be extended at runtime depending upon the problem, the extension in the
array size is a time taking process and almost impossible to be performed at
runtime since a lot of reallocations take place. Due to this reason, we can declare
the array large enough so that we can store queue elements as enough as
possible but the main problem with this declaration is that, most of the array slots
(nearly half) can never be reused. It will again lead to memory wastage.
117
Data Structures and Algorithms Unit – 7
Due to the drawbacks discussed in the previous section of this tutorial, the array
implementation can not be used for the large scale applications where the queues
are implemented. One of the alternative of array implementation is linked list
implementation of queue.
In a linked queue, each node of the queue consists of two parts i.e. data part and
the link part. Each element of the queue points to its immediate next element in
the memory.
In the linked queue, there are two pointers maintained in the memory i.e. front
pointer and rear pointer. The front pointer contains the address of the starting
element of the queue while the rear pointer contains the address of the last
element of the queue.
Insertion and deletions are performed at rear and front end respectively. If front
and rear both are NULL, it indicates that the queue is empty.
118
Data Structures and Algorithms Unit – 7
There are two basic operations which can be implemented on the linked queues.
The operations are Insertion and Deletion.
Insert operation
The insert operation append the queue by adding an element to the end of the
queue. The new element will be the last element of the queue.
Firstly, allocate the memory for the new node ptr by using the following
statement.
There can be the two scenario of inserting this new node ptr into the linked
queue.
In the first scenario, we insert element into an empty queue. In this case, the
condition front = NULL becomes true. Now, the new element will be added as the
only element of the queue and the next pointer of front and rear pointer both,
will point to NULL.
2. if(front == NULL)
3. {
4. front = ptr;
5. rear = ptr;
8. }
In the second case, the queue contains more than one element. The condition
front = NULL becomes false. In this scenario, we need to update the end pointer
119
Data Structures and Algorithms Unit – 7
rear so that the next pointer of rear will point to the new node ptr. Since, this is a
linked queue, hence we also need to make the rear pointer point to the newly
added node ptr. We also need to make the next pointer of rear point to NULL.
2. rear = ptr;
3. rear->next = NULL;
In this way, the element is inserted into the queue. The algorithm and the C
implementation is given as follows.
Algorithm
o Step 4: END
C Function
2. {
3.
120
Data Structures and Algorithms Unit – 7
4.
6. if(ptr == NULL)
7. {
8. printf("\nOVERFLOW\n");
9. return;
10. }
11. else
12. {
15. {
20. }
21. else
22. {
121
Data Structures and Algorithms Unit – 7
26. }
27. }
28. }
Deletion
Deletion operation removes the element that is first inserted among all the queue
elements. Firstly, we need to check either the list is empty or not. The condition
front == NULL becomes true if the list is empty, in this case , we simply write
underflow on the console and make exit.
Otherwise, we will delete the element that is pointed by the pointer front. For
this purpose, copy the node pointed by the front pointer into the pointer ptr.
Now, shift the front pointer, point to its next node and free the node pointed by
the node ptr. This is done by using the following statements.
1. ptr = front;
3. free(ptr);
Algorithm
122
Data Structures and Algorithms Unit – 7
o Step 5: END
C Function
2. {
3. if(front == NULL)
4. {
5. printf("\nUNDERFLOW\n");
6. return;
7. }
8. else
9. {
12. free(ptr);
13. }
14. }
Circular Queue
Deletions and insertions can only be performed at front and rear end respectively,
as far as linear queue is concerned.
123
Data Structures and Algorithms Unit – 7
The Queue shown in above figure is completely filled and there can't be inserted
any more element due to the condition rear == max - 1 becomes true.
However, if we delete 2 elements at the front end of the queue, we still can not
insert any element since the condition rear = max -1 still holds.
This is the main problem with the linear queue, although we have space available
in the array, but we can not insert any more element in the queue. This is simply
the memory wastage and we need to overcome this problem.
124
Data Structures and Algorithms Unit – 7
One of the solution of this problem is circular queue. In the circular queue, the
first index comes right after the last index. You can think of a circular queue as
shown in the following figure.
Circular queue will be full when front = -1 and rear = max-1. Implementation of
circular queue is similar to that of a linear queue. Only the logic part that is
implemented in the case of insertion and deletion is different from that in a linear
queue.
Complexity
Time Complexity
Front O(1)
Rear O(1)
enQueue() O(1)
deQueue() O(1)
125
Data Structures and Algorithms Unit – 7
1. If (rear + 1)%maxsize = front, the queue is full. In that case, overflow occurs
and therefore, insertion can not be performed in the queue.
3. If front != 0 and rear = max - 1, then it means that queue is not full
therefore, set the value of rear to 0 and insert the new element there.
o Step 4: EXIT
C Function
2. {
126
Data Structures and Algorithms Unit – 7
3. if((rear+1)%maxsize == front)
4. {
5. printf("\nOVERFLOW");
6. return;
7. }
9. {
10. front = 0;
11. rear = 0;
12. }
14. {
15. rear = 0;
16. }
17. else
18. {
20. }
22. }
127
Data Structures and Algorithms Unit – 7
To delete an element from the circular queue, we must check for the three
following conditions.
1. If front = -1, then there are no elements in the queue and therefore this will
be the case of an underflow condition.
2. If there is only one element in the queue, in this case, the condition rear =
front holds and therefore, both are set to -1 and the queue is deleted
completely.
3. If front = max -1 then, the value is deleted from the front end the value of
front is set to 0.
Algorithm
o Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]
o Step 4: EXIT
128
Data Structures and Algorithms Unit – 7
Tree
o A Tree is a recursive data structure containing the set of one or more data
nodes where one node is designated as the root of the tree while the
remaining nodes are called as the children of the root.
o The nodes other than the root node are partitioned into the non empty sets
where each one of them is to be called sub-tree.
o In a general tree, A node can have any number of children nodes but it can
have only a single parent.
o The following image shows a tree, where the node A is the root node of the
tree while the other nodes can be seen as the children of A.
Basic terminology
129
Data Structures and Algorithms Unit – 7
o Root Node :- The root node is the topmost node in the tree hierarchy. In
other words, the root node is the one which doesn't have any parent.
o Sub Tree :- If the root node is not null, the tree T1, T2 and T3 is called sub-
trees of the root node.
o Leaf Node :- The node of tree, which doesn't have any child node, is called
leaf node. Leaf node is the bottom most node of the tree. There can be any
number of leaf nodes present in a general tree. Leaf nodes can also be
called external nodes.
o Path :- The sequence of consecutive edges is called path. In the tree shown
in the above image, path to the node E is A→ B → E.
o Level Number :- Each node of the tree is assigned a level number in such a
way that each node is present at one level higher than its parent. Root
node of the tree is always present at level 0.
2. struct treenode {
3. int root;
4. int father;
130
Data Structures and Algorithms Unit – 7
5. int son;
6. int next;
7. }
1. struct treenode
2. {
3. int root;
7. }
Types of Tree
The tree data structure can be classified into six different categories.
131
Data Structures and Algorithms Unit – 7
General Tree
General Tree stores the elements in a hierarchical order in which the top level
element is always present at level 0 as the root element. All the nodes except the
root node are present at number of levels. The nodes which are present on the
same level are called siblings while the nodes which are present on the different
levels exhibit the parent-child relationship among them. A node may contain any
number of sub-trees. The tree in which each node contain 3 sub-tree, is called
ternary tree.
Forests
Forest can be defined as the set of disjoint trees which can be obtained by
deleting the root node and the edges which connects root node to the first level
node.
Binary Tree
Binary tree is a data structure in which each node can have at most 2 children.
The node present at the top most level is called the root node. A node with the 0
132
Data Structures and Algorithms Unit – 7
children is called leaf node. Binary Trees are used in the applications like
expression evaluation and many more. We will discuss binary tree in detail, later
in this tutorial.
Binary search tree is an ordered binary tree. All the elements in the left sub-tree
are less than the root while elements present in the right sub-tree are greater
than or equal to the root node element. Binary search trees are used in most of
the applications of computer science domain like searching, sorting, etc.
Expression Tree
(a + b) / (a*b - c) + d
133
Data Structures and Algorithms Unit – 7
Tournament Tree
Tournament tree are used to record the winner of the match in each round being
played between two players. Tournament tree can also be called as selection tree
or winner tree. External nodes represent the players among which a match is
being played while the internal nodes represent the winner of the match played.
At the top most level, the winner of the tournament is present as the root node of
the tree.
For example, tree .of a chess tournament being played among 4 players is shown
as follows. However, the winner in the left sub-tree will play against the winner of
right sub-tree.
134
Data Structures and Algorithms Unit – 7
Binary Tree
Binary Tree is a special type of generic tree in which, each node can have at most
two children. Binary tree is generally partitioned into three disjoint subsets.
135
Data Structures and Algorithms Unit – 7
In Strictly Binary Tree, every non-leaf node contain non-empty left and right sub-
trees. In other words, the degree of every non-leaf node will always be 2. A
strictly binary tree with n leaves, will have (2n - 1) nodes.
A Binary Tree is said to be a complete binary tree if all of the leaves are located at
the same level d. A complete binary tree is a binary tree that contains exactly 2^l
nodes at each level between level 0 and d. The total number of nodes in a
complete binary tree with depth d is 2d+1-1 where leaf nodes are 2d while non-leaf
nodes are 2d-1.
136
Data Structures and Algorithms Unit – 7
SN Traversal Description
1 Pre-order Traverse the root first then traverse into the left sub-
Traversal tree and right sub-tree respectively. This procedure will
be applied to each sub-tree of the tree recursively.
2 In-order Traverse the left sub-tree first, and then traverse the
Traversal root and the right sub-tree respectively. This procedure
will be applied to each sub-tree of the tree recursively.
137
Data Structures and Algorithms Unit – 7
3 Post-order Traverse the left sub-tree and then traverse the right
Traversal sub-tree and root respectively. This procedure will be
applied to each sub-tree of the tree recursively.
1. Linked Representation
In this representation, the binary tree is stored in the memory, in the form of a
linked list where the number of nodes are stored at non-contiguous memory
locations and linked together by inheriting parent child relationship like a tree.
every node contains three parts : pointer to the left node, data element and
pointer to the right node. Each binary tree has a root pointer which points to the
root node of the binary tree. In an empty binary tree, the root pointer will point
to null.
138
Data Structures and Algorithms Unit – 7
In the above figure, a tree is seen as the collection of nodes where each node
contains three parts : left pointer, data element and right pointer. Left pointer
stores the address of the left child while the right pointer stores the address of
the right child. The leaf node contains null in its left and right pointers.
The following image shows about how the memory will be allocated for the binary
tree by using linked representation. There is a special pointer maintained in the
memory which points to the root node of the tree. Every node in the tree
contains the address of its left and right child. Leaf node contains null in its left
and right pointers.
2. Sequential Representation
This is the simplest memory allocation technique to store the tree elements but it
is an inefficient technique since it requires a lot of space to store the tree
139
Data Structures and Algorithms Unit – 7
elements. A binary tree is shown in the following figure along with its memory
allocation.
In this representation, an array is used to store the tree elements. Size of the
array will be equal to the number of nodes present in the tree. The root node of
the tree will be present at the 1st index of the array. If a node is stored at ith index
then its left and right children will be stored at 2i and 2i+1 location. If the 1st index
of the array i.e. tree[1] is 0, it means that the tree is empty.
140
Data Structures and Algorithms Unit – 7
1. Binary Search tree can be defined as a class of binary trees, in which the
nodes are arranged in a specific order. This is also called ordered binary
tree.
2. In a binary search tree, the value of all the nodes in the left sub-tree is less
than the value of the root.
3. Similarly, value of all the nodes in the right sub-tree is greater than or equal
to the value of the root.
4. This rule will be recursively applied to all the left and right sub-trees of the
root.
A Binary search tree is shown in the above figure. As the constraint applied on the
BST, we can see that the root node 30 doesn't contain any value greater than or
141
Data Structures and Algorithms Unit – 7
equal to 30 in its left sub-tree and it also doesn't contain any value less than 30 in
its right sub-tree.
1. Searching become very efficient in a binary search tree since, we get a hint
at each step, about which sub-tree contains the desired element.
Q. Create the binary search tree using the following data elements.
2. Read the next element, if it is lesser than the root node element, insert it as
the root of the left sub-tree.
142
Data Structures and Algorithms Unit – 7
The process of creating BST by using the given elements, is shown in the image
below.
143
Data Structures and Algorithms Unit – 7
There are many operations which can be performed on a binary search tree.
SN Operation Description
AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is
named AVL in honour of its inventors.
AVL Tree can be defined as height balanced binary search tree in which each node
is associated with a balance factor which is calculated by subtracting the height of
its right sub-tree from that of its left sub-tree.
144
Data Structures and Algorithms Unit – 7
If balance factor of any node is 1, it means that the left sub-tree is one level
higher than the right sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right sub-
tree contain equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level
lower than the right sub-tree.
An AVL tree is given in the following figure. We can see that, balance factor
associated with each node is in between -1 and +1. therefore, it is an example of
AVL tree.
Complexity
145
Data Structures and Algorithms Unit – 7
Due to the fact that, AVL tree is also a binary search tree therefore, all the
operations are performed in the same way as they are performed in a binary
search tree. Searching and traversing do not lead to the violation in property of
AVL tree. However, insertion and deletion are the operations which can violate
this property and therefore, they need to be revisited.
SN Operation Description
146
Data Structures and Algorithms Unit – 7
AVL tree controls the height of the binary search tree by not letting it to be
skewed. The time taken for all operations in a binary search tree of height h
is O(h). However, it can be extended to O(n) if the BST becomes skewed (i.e.
worst case). By limiting this height to log n, AVL tree imposes an upper bound on
each operation to be O(log n) where n is the number of nodes.
B Tree
B Tree is a specialized m-way tree that can be widely used for disk access. A B-
Tree of order m can have at most m-1 keys and m children. One of the main
reason of using B tree is its capability to store large number of keys in a single
node and large key values by keeping the height of the tree relatively small.
2. Every node in a B-Tree except the root node and the leaf node contain at
least m/2 children.
It is not necessary that, all the nodes contain the same number of children but,
each node must have m/2 number of nodes.
While performing some operations on B Tree, any property of B Tree may violate
such as number of minimum children a node can have. To maintain the properties
of B Tree, the tree may split or join.
Operations
Searching :
1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left
sub-tree.
Searching in a B tree depends upon the height of the tree. The search algorithm
takes O(log n) time to search any element in a B tree.
148
Data Structures and Algorithms Unit – 7
Inserting
Insertions are done at the leaf node level. The following algorithm needs to be
followed in order to insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the
node can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
o If the parent node also contain m-1 number of keys, then split it too
by following the same steps.
Example:
Insert the node 8 into the B Tree of order 5 shown in the following image.
149
Data Structures and Algorithms Unit – 7
The node, now contain 5 keys which is greater than (5 -1 = 4 ) keys. Therefore split
the node from the median i.e. 8 and push it up to its parent node shown as
follows.
150
Data Structures and Algorithms Unit – 7
Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can
either be a leaf node or an internal node. Following algorithm needs to be
followed in order to delete a node from a B tree.
2. If there are more than m/2 keys in the leaf node then delete the desired
key from the node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking
the element from eight or left sibling.
o If the left sibling contains more than m/2 elements then push its
largest element up to its parent and move the intervening element
down to the node where the key is deleted.
o If the right sibling contains more than m/2 elements then push its
smallest element up to the parent and move intervening element
down to the node where the key is deleted.
151
Data Structures and Algorithms Unit – 7
4. If neither of the sibling contain more than m/2 elements then create a new
leaf node by joining two leaf nodes and the intervening element of the
parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on
the parent too.
If the the node which is to be deleted is an internal node, then replace the node
with its in-order successor or predecessor. Since, successor or predecessor will
always be on the leaf node hence, the process will be similar as the node is being
deleted from the leaf node.
Example 1
Delete the node 53 from the B Tree of order 5 shown in the following figure.
152
Data Structures and Algorithms Unit – 7
Now, 57 is the only element which is left in the node, the minimum number of
elements that must be present in a B tree of order 5, is 2. it is less than that, the
elements in its left and right sub-tree are also not sufficient therefore, merge it
with the left sibling and intervening element of parent i.e. 49.
Application of B tree
153
Data Structures and Algorithms Unit – 7
B tree is used to index the data and provides fast access to the actual data stored
on the disks since, the access to value stored in a large database that is stored on
a disk is a very time consuming process.
B+ Tree
In B Tree, Keys and records both can be stored in the internal as well as leaf
nodes. Whereas, in B+ tree, records (data) can only be stored on the leaf nodes
while internal nodes can only store the key values.
The leaf nodes of a B+ tree are linked together in the form of a singly linked lists
to make the search queries more efficient.
B+ Tree are used to store the large amount of data which can not be stored in the
main memory. Due to the fact that, size of main memory is always limited, the
internal nodes (keys to access records) of the B+ tree are stored in the main
memory whereas, leaf nodes are stored in the secondary memory.
The internal nodes of B+ tree are often called index nodes. A B+ tree of order 3 is
shown in the following figure.
154
Data Structures and Algorithms Unit – 7
Advantages of B+ Tree
5. Faster search queries as the data is stored only on the leaf nodes.
B Tree VS B+ Tree
155
Data Structures and Algorithms Unit – 7
SN B Tree B+ Tree
2 Data can be stored in leaf nodes as Data can only be stored on the
well as internal nodes leaf nodes.
5 Leaf nodes can not be linked Leaf nodes are linked together to
together. make the search operations more
efficient.
Insertion in B+ Tree
Step 2: If the leaf doesn't have required space, split the node and copy the middle
node to the next index node.
156
Data Structures and Algorithms Unit – 7
Step 3: If the index node doesn't have required space, split the node and copy the
middle element to the next index page.
Example :
Insert the value 195 into the B+ tree of order 5 shown in the following figure.
195 will be inserted in the right sub-tree of 120 after 190. Insert it at the desired
position.
The node contains greater than the maximum number of elements i.e. 4,
therefore split it and place the median node up to the parent.
157
Data Structures and Algorithms Unit – 7
Now, the index node contains 6 children and 5 keys which violates the B+ tree
properties, therefore we need to split it, shown as follows.
Deletion in B+ Tree
Step 2: if the leaf node contains less than minimum number of elements, merge
down the node with its sibling and delete the key in between them.
Step 3: if the index node contains less than minimum number of elements, merge
the node with the sibling and move down the key in between them.
Example
Delete the key 200 from the B+ Tree shown in the following figure.
158
Data Structures and Algorithms Unit – 7
200 is present in the right sub-tree of 190, after 195. delete it.
Merge the two nodes by using 195, 190, 154 and 129.
159
Data Structures and Algorithms Unit – 7
Now, element 120 is the single element present in the node which is violating the
B+ Tree properties. Therefore, we need to merge it by using 60, 78, 108 and 120.
Graph
A graph can be defined as group of vertices and edges that are used to connect
these vertices. A graph can be seen as a cyclic tree, where the vertices (Nodes)
maintain any complex relationship among them instead of having parent child
relationship.
Definition
160
Data Structures and Algorithms Unit – 7
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set
of vertices and E(G) represents the set of edges which are used to connect these
vertices.
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E),
(E,D), (D,B), (D,A)) is shown in the following figure.
In a directed graph, edges form an ordered pair. Edges represent a specific path
from some vertex A to another vertex B. Node A is called initial node while node B
is called terminal node.
161
Data Structures and Algorithms Unit – 7
Graph Terminology
Path
A path can be defined as the sequence of nodes that are followed in order to
reach some terminal node V from the initial node U.
Closed Path
A path will be called as closed path if the initial node is same as terminal node. A
path will be closed path if V0=VN.
Simple Path
If all the nodes of the graph are distinct with an exception V 0=VN, then such path P
is called as closed simple path.
Cycle
A cycle can be defined as the path which has no repeated edges or vertices except
the first and last vertices.
Connected Graph
162
Data Structures and Algorithms Unit – 7
A connected graph is the one in which some path exists between every two
vertices (u, v) in V. There are no isolated nodes in connected graph.
Complete Graph
A complete graph is the one in which every node is connected with all other
nodes. A complete graph contain n(n-1)/2 edges where n is the number of nodes
in the graph.
Weighted Graph
In a weighted graph, each edge is assigned with some data such as length or
weight. The weight of an edge e can be given as w(e) which must be a positive (+)
value indicating the cost of traversing the edge.
Digraph
A digraph is a directed graph in which each edge of the graph is associated with
some direction and the traversing can be done only in the specified direction.
Loop
An edge that is associated with the similar end points can be called as Loop.
Adjacent Nodes
If two nodes u and v are connected via an edge e, then the nodes u and v are
called as neighbours or adjacent nodes.
A degree of a node is the number of edges that are connected with that node. A
node with degree 0 is called as isolated node.
Graph Representation
163
Data Structures and Algorithms Unit – 7
There are two ways to store Graph into the computer's memory. In this part of
this tutorial, we discuss each one of them in detail.
1. Sequential Representation
in the above figure, we can see the mapping among the vertices (A, B, C, D, E) is
represented by using the adjacency matrix which is also shown in the figure.
164
Data Structures and Algorithms Unit – 7
There exists different adjacency matrices for the directed and undirected graph.
In directed graph, an entry Aij will be 1 only when there is an edge directed from
Vi to Vj.
A directed graph and its adjacency matrix representation is shown in the following
figure.
The weighted directed graph along with the adjacency matrix representation is
shown in the following figure.
165
Data Structures and Algorithms Unit – 7
Linked Representation
In the linked representation, an adjacency list is used to store the Graph into the
computer's memory.
Consider the undirected graph shown in the following figure and check the
adjacency list representation.
166
Data Structures and Algorithms Unit – 7
An adjacency list is maintained for each node present in the graph which stores
the node value and a pointer to the next adjacent node to the respective node. If
all the adjacent nodes are traversed then store the NULL in the pointer field of
last node of the list. The sum of the lengths of adjacency lists is equal to the twice
of the number of edges present in an undirected graph.
Consider the directed graph shown in the following figure and check the
adjacency list representation of the graph.
In a directed graph, the sum of lengths of all the adjacency lists is equal to the
number of edges present in the graph.
In the case of weighted directed graph, each node contains an extra field that is
called the weight of the node. The adjacency list representation of a directed
graph is shown in the following figure.
167
Data Structures and Algorithms Unit – 7
In this part of the tutorial we will discuss the techniques by using which, we can
traverse all the vertices of the graph.
Traversing the graph means examining all the nodes and vertices of the graph.
There are two standard methods by using which, we can traverse the graphs. Lets
discuss each one of them in detail.
Breadth first search is a graph traversal algorithm that starts traversing the graph
from root node and explores all the neighbouring nodes. Then, it selects the
nearest node and explore all the unexplored nodes. The algorithm follows the
same process for each of the nearest node until it finds the goal.
The algorithm of breadth first search is given below. The algorithm starts with
examining the node A and all of its neighbours. In the next step, the neighbours of
168
Data Structures and Algorithms Unit – 7
the nearest node of A are explored and process continues in the further steps.
The algorithm explores all neighbours of all the nodes and ensures that each node
is visited exactly once and no node is visited twice.
Algorithm
o Step 6: EXIT
Example
Consider the graph G shown in the following image, calculate the minimum path p
from node A to node E. Given that each edge has a length of 1.
169
Data Structures and Algorithms Unit – 7
Solution:
Minimum Path P can be found by applying breadth first search algorithm that will
begin at node A and will end at E. the algorithm uses two queues,
namely QUEUE1 and QUEUE2. QUEUE1 holds all the nodes that are to be
processed while QUEUE2 holds all the nodes that are processed and deleted
from QUEUE1.
1. QUEUE1 = {A}
2. QUEUE2 = {NULL}
2. Delete the Node A from QUEUE1 and insert all its neighbours. Insert Node A
into QUEUE2
1. QUEUE1 = {B, D}
2. QUEUE2 = {A}
170
Data Structures and Algorithms Unit – 7
3. Delete the node B from QUEUE1 and insert all its neighbours. Insert node B into
QUEUE2.
1. QUEUE1 = {D, C, F}
2. QUEUE2 = {A, B}
4. Delete the node D from QUEUE1 and insert all its neighbours. Since F is the only
neighbour of it which has been inserted, we will not insert it again. Insert node D
into QUEUE2.
1. QUEUE1 = {C, F}
2. QUEUE2 = { A, B, D}
5. Delete the node C from QUEUE1 and insert all its neighbours. Add node C to
QUEUE2.
1. QUEUE1 = {F, E, G}
2. QUEUE2 = {A, B, D, C}
6. Remove F from QUEUE1 and add all its neighbours. Since all of its neighbours
has already been added, we will not add them again. Add node F to QUEUE2.
1. QUEUE1 = {E, G}
2. QUEUE2 = {A, B, D, C, F}
7. Remove E from QUEUE1, all of E's neighbours has already been added to
QUEUE1 therefore we will not add them again. All the nodes are visited and the
target node i.e. E is encountered into QUEUE2.
1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}
171
Data Structures and Algorithms Unit – 7
Depth first search (DFS) algorithm starts with the initial node of the graph G, and
then goes to deeper and deeper until we find the goal node or the node which
has no children. The algorithm, then backtracks from the dead end towards the
most recent node that is yet to be completely unexplored.
The data structure which is being used in DFS is stack. The process is similar to BFS
algorithm. In DFS, the edges that leads to an unvisited node are called discovery
edges while the edges that leads to an already visited node are called block edges.
Algorithm
o Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)
o Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed
state)
o Step 5: Push on the stack all the neighbours of N that are in the ready state
(whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
o Step 6: EXIT
Example :
Consider the graph G along with its adjacency list, given in the figure below.
Calculate the order to print all the nodes of the graph starting from node H, by
using depth first search (DFS) algorithm.
172
Data Structures and Algorithms Unit – 7
Solution :
1. STACK : H
POP the top element of the stack i.e. H, print it and push all the neighbours of H
onto the stack that are is ready state.
1. Print H
2. STACK : A
Pop the top element of the stack i.e. A, print it and push all the neighbours of A
onto the stack that are in ready state.
1. Print A
2. Stack : B, D
173
Data Structures and Algorithms Unit – 7
Pop the top element of the stack i.e. D, print it and push all the neighbours of D
onto the stack that are in ready state.
1. Print D
2. Stack : B, F
Pop the top element of the stack i.e. F, print it and push all the neighbours of F
onto the stack that are in ready state.
1. Print F
2. Stack : B
Pop the top of the stack i.e. B and push all the neighbours
1. Print B
2. Stack : C
Pop the top of the stack i.e. C and push all the neighbours.
1. Print C
2. Stack : E, G
Pop the top of the stack i.e. G and push all its neighbours.
1. Print G
2. Stack : E
Pop the top of the stack i.e. E and push all its neighbours.
1. Print E
2. Stack :
Hence, the stack now becomes empty and all the nodes of the graph have been
traversed.
174
Data Structures and Algorithms Unit – 7
1. H → A → D → F → B → C → G → E
Spanning Tree
There are two algorithms which are being used for this purpose.
Prim's Algorithm
Prim's Algorithm is used to find the minimum spanning tree from a graph.
Prim's algorithm finds the subset of edges that includes every vertex of the
graph such that the sum of the weights of the edges can be minimized.
Prim's algorithm starts with the single node and explore all the adjacent
nodes with all the connecting edges at every step. The edges with the
minimal weights causing no cycles in the graph got selected.
175
Data Structures and Algorithms Unit – 7
Algorithm
o Step 3: Select an edge e connecting the tree vertex and fringe vertex that
has minimum weight
o Step 4: Add the selected edge and the vertex to the minimum spanning
tree T
[END OF LOOP]
o Step 5: EXIT
Example :
Solution
o Step 2: Add the vertices that are adjacent to A. the edges that connecting
the vertices are shown by dotted lines.
176
Data Structures and Algorithms Unit – 7
o Step 3: Choose the edge with the minimum weight among all. i.e. BD and
add it to MST. Add the adjacent vertices of D i.e. C and E.
o Step 3: Choose the edge with the minimum weight among all. In this case,
the edges DE and CD are such edges. Add them to MST and explore the
adjacent of C i.e. E and A.
o Step 4: Choose the edge with the minimum weight i.e. CA. We can't choose
CE as it would cause cycle in the graph.
The graph produces in the step 4 is the minimum spanning tree of the
graph shown in the above figure.
cost(MST) = 4 + 2 + 1 + 3 = 10 units.
Kruskal's Algorithm
Algorithm
o Step 1: Create a forest in such a way that each graph is a separate tree.
o Step 2: Create a priority queue Q that contains all the edges of the graph.
o Step 5: IF the edge obtained in Step 4 connects two different trees, then
Add it to the forest (for combining two trees into one tree).
ELSE
Discard the edge
o Step 6: END
Example :
Solution:
178
Data Structures and Algorithms Unit – 7
E A A A A B C D
d E D C B C D E
g
e
W 5 1 7 1 3 4 2
ei 0
gh
t
E A D B C A A A
d B E C D E C D
g
e
W 1 2 3 4 5 7 1
ei 0
gh
t
179
Data Structures and Algorithms Unit – 7
180
Data Structures and Algorithms Unit – 7
The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a
cycle.
The next edge to be added is AD, but it can't be added as it will contain a
cycle.
Hence, the final MST is the one which is shown in the step 4.
Sorting Algorithms
The importance of sorting lies in the fact that data searching can be optimized to
a very high level, if data is stored in a sorted manner. Sorting is also used to
represent data in more readable formats. Following are some of the examples of
sorting in real-life scenarios −
181
Data Structures and Algorithms Unit – 7
Sorting algorithms may require some extra space for comparison and temporary
storage of few data elements. These algorithms do not require any extra space
and sorting is said to happen in-place, or for example, within the array itself. This
is called in-place sorting. Bubble sort is an example of in-place sorting.
However, in some sorting algorithms, the program requires space which is more
than or equal to the elements being sorted. Sorting which uses equal or more
space is called not-in-place sorting. Merge-sort is an example of not-in-place
sorting.
If a sorting algorithm, after sorting the contents, does not change the sequence of
similar content in which they appear, it is called stable sorting.
If a sorting algorithm, after sorting the contents, changes the sequence of similar
content in which they appear, it is called unstable sorting.
182
Data Structures and Algorithms Unit – 7
A non-adaptive algorithm is one which does not take into account the elements
which are already sorted. They try to force every single element to be re-ordered
to confirm their sortedness.
Important Terms
Some terms are generally coined while discussing sorting techniques, here is a
brief introduction to them −
Increasing Order
Decreasing Order
183
Data Structures and Algorithms Unit – 7
Non-Increasing Order
Non-Decreasing Order
Bubble sort
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-
based algorithm in which each pair of adjacent elements is compared and the
elements are swapped if they are not in order. This algorithm is not suitable for
large data sets as its average and worst case complexity are of Ο(n2) where n is
the number of items.
We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're
keeping it short and precise.
184
Data Structures and Algorithms Unit – 7
Bubble sort starts with very first two elements, comparing them to check which
one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next,
we compare 33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
We know then that 10 is smaller 35. Hence they are not sorted.
185
Data Structures and Algorithms Unit – 7
We swap these values. We find that we have reached the end of the array. After
one iteration, the array should look like this −
To be precise, we are now showing how an array should look like after each
iteration. After the second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is
completely sorted.
Algorithm
begin BubbleSort(list)
swap(list[i], list[i+1])
186
Data Structures and Algorithms Unit – 7
end if
end for
return list
end BubbleSort
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array element
unless the whole array is completely sorted in an ascending order. This may cause
a few complexity issues like what if the array needs no more swapping as all the
elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if
any swap has happened or not. If no swap has occurred, i.e. the array requires no
more processing to be sorted, it will come out of the loop.
loop = list.count;
swapped = false
/* swap them */
swapped = true
187
Data Structures and Algorithms Unit – 7
end if
end for
break
end if
end for
Implementation
One more issue we did not address in our original algorithm and its improvised
pseudocode, is that, after every iteration the highest values settles down at the
end of the array. Hence, the next iteration need not include already sorted
elements. For this purpose, in our implementation, we restrict the inner loop to
avoid already sorted values.
Quick Sort
Quick sort is the widely used sorting algorithm that makes n log n comparisons in
average case for sorting of an array of n elements. This algorithm follows divide
and conquer approach. The algorithm processes the array in the following way.
1. Set the first index of the array to left and loc variable. Set the last index of
the array to right variable. i.e. left = 0, loc = 0, en d = n - 1, where n is the
length of the array.
188
Data Structures and Algorithms Unit – 7
2. Start from the right of the array and scan the complete array from right to
beginning comparing each element of the array with the element pointed
by loc.
1. If this is the case, then continue with the comparison until right
becomes equal to the loc.
2. If a[loc] > a[right], then swap the two values. And go to step 3.
1. start from element pointed by left and compare each element in its way
with the element pointed by the variable loc. Ensure that a[loc] > a[left]
1. if this is the case, then continue with the comparison until loc
becomes equal to left.
2. [loc] < a[right], then swap the two values and go to step 2.
Complexity
Space O(log n)
Complexity
189
Data Structures and Algorithms Unit – 7
Algorithm
o Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =
o Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
o Step 8: END
190
Data Structures and Algorithms Unit – 7
Quick Sort
Quick sort is the widely used sorting algorithm that makes n log n comparisons in
average case for sorting of an array of n elements. This algorithm follows divide
and conquer approach. The algorithm processes the array in the following way.
1. Set the first index of the array to left and loc variable. Set the last index of
the array to right variable. i.e. left = 0, loc = 0, en d = n - 1, where n is the
length of the array.
2. Start from the right of the array and scan the complete array from right to
beginning comparing each element of the array with the element pointed
by loc.
1. If this is the case, then continue with the comparison until right
becomes equal to the loc.
2. If a[loc] > a[right], then swap the two values. And go to step 3.
1. start from element pointed by left and compare each element in its way
with the element pointed by the variable loc. Ensure that a[loc] > a[left]
1. if this is the case, then continue with the comparison until loc
becomes equal to left.
2. [loc] < a[right], then swap the two values and go to step 2.
Complexity
191
Data Structures and Algorithms Unit – 7
Time Complexity O(n) for 3 way partition or O(n log n) simple partition
Space Complexity
Algorithm
o Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =
o Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
192
Data Structures and Algorithms Unit – 7
o Step 8: END
Heap Sort
Heap sort processes the elements by creating the min heap or max heap using the
elements of the given array. Min heap or max heap represents the ordering of the
array in which root element represents the minimum or maximum element of the
array. At each step, the root element of the heap gets deleted and stored into the
sorted array and the heap will again be heapified.
Complexity
Time Complexity Ω(n log (n)) θ(n log (n)) O(n log (n))
193
Data Structures and Algorithms Unit – 7
Algorithm
HEAP_SORT(ARR, N)
o Step 3: END
Radix Sort
Radix sort processes the elements the same way in which the names of the
students are sorted according to their alphabetical order. There are 26 radix in
that case due to the fact that, there are 26 alphabets in English. In the first pass,
the names are grouped according to the ascending order of the first letter of
names.
In the second pass, the names are grouped according to the ascending order of
the second letter. The same process continues until we find the sorted list of
names. The bucket are used to store the names produced in each pass. The
number of passes depends upon the length of the name with the maximum letter.
In the case of integers, radix sort sorts the numbers according to their digits. The
comparisons are made among the digits of the number from LSB to MSB. The
194
Data Structures and Algorithms Unit – 7
number of passes depend upon the length of the number with the most number
of digits.
Complexity
Example
Consider the array of length 6 given below. Sort the array by using Radix sort.
Therefore, the list generated in the step 4 is the sorted list, arranged from radix
sort.
195
Data Structures and Algorithms Unit – 7
Algorithm
196
Data Structures and Algorithms Unit – 7
Step 1 - Assume that first element in the list is in sorted portion and all the
remaining elements are in unsorted portion.
Step 2: Take first element from the unsorted portion and insert that
element into the sorted portion in the order specified.
Step 3: Repeat the above process until all the elements from the unsorted
portion are moved into the sorted portion.
for i = 1 to size-1 {
temp = list[i];
j = i-1;
list[j] = list[j-1];
j = j - 1;
list[j] = temp;
Example
197
Data Structures and Algorithms Unit – 7
198
Data Structures and Algorithms Unit – 7
#include<stdio.h>
#include<conio.h>
void main(){
scanf("%d", &size);
scanf("%d", &list[i]);
199
Data Structures and Algorithms Unit – 7
temp = list[i];
j = i - 1;
list[j + 1] = list[j];
j = j - 1;
list[j + 1] = temp;
getch();
Output
200
Data Structures and Algorithms Unit – 7
Step 1 - Select the first element of the list (i.e., Element at first position in
the list).
Step 2: Compare the selected element with all the other elements in the
list.
Step 4: Repeat the same procedure with element in the next position in the
list till the entire list is sorted.
201
Data Structures and Algorithms Unit – 7
temp=list[i];
list[i]=list[j];
list[j]=temp;
Example
202
Data Structures and Algorithms Unit – 7
203
Data Structures and Algorithms Unit – 7
To sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n-
2)+(n-3)+......+1) = (n (n-1))/2 number of comparisions in the worst case. If the list
is already sorted then it requires 'n' number of comparisions.
#include<stdio.h>
#include<conio.h>
void main(){
int size,i,j,temp,list[100];
clrscr();
scanf("%d",&size);
scanf("%d",&list[i]);
204
Data Structures and Algorithms Unit – 7
temp=list[i];
list[i]=list[j];
list[j]=temp;
printf(" %d",list[i]);
getch();
Output
205
Data Structures and Algorithms Unit – 7
If we can break a single big problem into smaller sub-problems, solve the smaller
sub-problems and combine their solutions to find the solution for the original big
problem, it becomes easier to solve the whole problem.
When Britishers came to India, they saw a country with different religions living in
harmony, hard working but naive citizens, unity in diversity, and found it difficult
to establish their empire. So, they adopted the policy of Divide and Rule. Where
the population of India was collectively a one big problem for them, they divided
the problem into smaller problems, by instigating rivalries between local kings,
making them stand against each other, and this worked very well for them.
Well that was history, and a socio-political policy (Divide and Rule), but the idea
here is, if we can somehow divide a problem into smaller sub-problems, it
becomes easier to eventually solve the whole problem.
206
Data Structures and Algorithms Unit – 7
2. Conquer the subproblems by solving them. The idea is to break down the
problem into atomic subproblems, where they are actually solved.
3. Combine the solutions of the subproblems to find the solution of the actual
problem.
Hashing
207
Data Structures and Algorithms Unit – 7
Thus, it becomes a data structure in which insertion and search operations are
very fast irrespective of the size of the data. Hash Table uses an array as a storage
medium and uses hash technique to generate an index where an element is to be
inserted or is to be located from.
Hashing
(1,20)
(2,70)
(42,80)
(4,25)
(12,44)
(14,32)
(17,11)
(13,78)
208
Data Structures and Algorithms Unit – 7
(37,98)
1 1 1 % 20 = 1
2 2 2 % 20 = 2
3 42 42 % 20 = 2
4 4 4 % 20 = 4
5 12 12 % 20 = 12
6 14 14 % 20 = 14
7 17 17 % 20 = 17
8 13 13 % 20 = 13
9 37 37 % 20 = 17
Linear Probing
As we can see, it may happen that the hashing technique is used to create an
already used index of the array. In such a case, we can search the next empty
location in the array by looking into the next cell until we find an empty cell. This
technique is called linear probing.
209
Data Structures and Algorithms Unit – 7
1 1 1 % 20 = 1 1
2 2 2 % 20 = 2 2
3 42 42 % 20 = 2 2
4 4 4 % 20 = 4 4
5 12 12 % 20 = 12 12
6 14 14 % 20 = 14 14
7 17 17 % 20 = 17 17
8 13 13 % 20 = 13 13
9 37 37 % 20 = 17 17
Basic Operations
210
Data Structures and Algorithms Unit – 7
DataItem
Define a data item having some data and key, based on which the search is to be
conducted in a hash table.
struct DataItem {
int data;
int key;
};
Hash Method
Define a hashing method to compute the hash code of the key of the data item.
Search Operation
Example
211
Data Structures and Algorithms Unit – 7
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
Insert Operation
Whenever an element is to be inserted, compute the hash code of the key passed
and locate the index using that hash code as an index in the array. Use linear
probing for empty location, if an element is found at the computed hash code.
Example
212
Data Structures and Algorithms Unit – 7
Whenever an element is to be deleted, compute the hash code of the key passed
and locate the index using that hash code as an index in the array. Use linear
probing to get the element ahead if an element is not found at the computed
hash code. When found, store a dummy item there to keep the performance of
the hash table intact.
Example
If we want to go from city "A" to city "B", there can be many ways of doing this.
We can go by flight, by bus, by train and also by bicycle. Depending on the
availability and convenience, we choose the one which suits us. Similarly, in
computer science, there are multiple algorithms to solve a problem. When we
213
Data Structures and Algorithms Unit – 7
have more than one algorithm to solve a problem, we need to select the best one.
Performance analysis helps us to select the best algorithm from multiple
algorithms to solve a problem.
When there are multiple alternative algorithms to solve a problem, we analyze
them and pick the one which is best suitable for our requirements. The formal
definition is as follows...
1. Whether that algorithm is providing the exact solution for the problem?
When we want to analyse an algorithm, we consider only the space and time
required by that particular algorithm and we ignore all the remaining elements.
214
Data Structures and Algorithms Unit – 7
Recurrences
If the problem size is small enough, say n < c where c is a constant, the
straightforward solution takes constant time, which is written as θ(1). If the
division of the problem yields a number of sub-problems with size nbnb.
215
Data Structures and Algorithms Unit – 7
To solve the problem, the required time is a.T(n/b). If we consider the time
required for division is D(n) and the time required for combining the results of
sub-problems is C(n), the recurrence relation can be represented as −
T(n)={θ(1)aT(nb)+D(n)+C(n)ifn⩽cotherwiseT(n)={θ(1)ifn⩽caT(nb)+D(n)+C(n)other
wise
For any defined problem, there can be N number of solution. This is true in
general. If I have a problem and I discuss about the problem with all of my friends,
they will all suggest me different solutions. And I am the one who has to decide
which solution is the best based on the circumstances.
Similarly for any problem which must be solved using a program, there can be
infinite number of solutions. Let's take a simple example to understand this.
Below we have two different algorithms to find square of a number(for some
time, forget that square of any number n is n*n):
One solution to this problem can be, running a loop for n times, starting with the
number n and adding n to it, every time.
216
Data Structures and Algorithms Unit – 7
/*
we have to calculate the square of n
*/
for i=1 to n
do n = n + n
// when the loop ends n will hold its square
return n
/*
we have to calculate the square of n
*/
return n*n
In the above two simple algorithms, you saw how a single problem can have many
solutions. While the first solution required a loop which will execute for n number
of times, the second solution used a mathematical operator * to return the result
in one line. So which one is the better approach, of course the second one.
Time complexity of an algorithm signifies the total time required by the program
to run till its completion.
The time complexity of algorithms is most commonly expressed using the big O
notation. It's an asymptotic notation to represent the time complexity. We will
study about it in detail in the next tutorial.
217
Data Structures and Algorithms Unit – 7
And since the algorithm's performance may vary with different types of input
data, hence for an algorithm we usually use the worst-case Time complexity of an
algorithm because that is the maximum time taken for any input size.
Now lets tap onto the next big topic related to Time complexity, which is How to
Calculate Time Complexity. It becomes very confusing some times, but we will try
to explain it in the simplest way.
Now the most common metric for calculating time complexity is Big O notation.
This removes all constant factors so that the running time can be estimated in
relation to N, as N approaches infinity. In general you can think of it like this :
statement;
Above we have a single statement. Its Time Complexity will be Constant. The
running time of the statement will not change in relation to N.
218
Data Structures and Algorithms Unit – 7
NOTE: In general, doing something with every item in one dimension is linear,
doing something with every item in two dimensions is quadratic, and dividing the
working area in half is logarithmic.
219
Data Structures and Algorithms Unit – 7
Now we will discuss and understand the various notations used for Time
Complexity.
2. Big Omega denotes "more than or the same as" <expression> iterations.
O(expression) is the set of functions that grow slower than or at the same rate as
expression. It indicates the maximum required by an algorithm for all input
values. It represents the worst case of an algorithm's time complexity.
Omega(expression) is the set of functions that grow faster than or at the same
rate as expression. It indicates the minimum time required by an algorithm for all
input values. It represents the best case of an algorithm's time complexity.
Theta(expression) consist of all the functions that lie in both O(expression) and
Omega(expression). It indicates the average bound of an algorithm. It represents
the average case of an algorithm's time complexity.
The simplest explanation is, because Theta denotes the same as the expression.
Hence, as f(n) grows by a factor of n2, the time complexity can be best
represented as Theta(n2).
220
Data Structures and Algorithms Unit – 7
2. Program Instruction
3. Execution
Space complexity is the amount of memory used by the algorithm (including the
input values to the algorithm) to execute and produce the result.
Sometime Auxiliary Space is confused with Space Complexity. But Auxiliary Space
is the extra space or the temporary space used by the algorithm during it's
execution.
1. Instruction Space
It's the amount of memory used to save the compiled version of instructions.
2. Environmental Stack
For example, If a function A() calls function B() inside it, then all th variables of the
function A() will get stored on the system stack temporarily, while the
function B() is called and executed inside the funciton A().
221
Data Structures and Algorithms Unit – 7
3. Data Space
But while calculating the Space Complexity of any algorithm, we usually consider
only Data Space and we neglect the Instruction Space and Environmental Stack.
For calculating the space complexity, we need to know the value of memory used
by different type of datatype variables, which generally varies for different
operating systems, but the method for calculating the space complexity remains
the same.
Type Size
{
int z = a + b + c;
return(z);
}
222
Data Structures and Algorithms Unit – 7
In the above expression, variables a, b, c and z are all integer types, hence they
will take up 4 bytes each, so total memory requirement will be (4(4) + 4) = 20
bytes, this additional 4 bytes is for return value. And because this space
requirement is fixed for the above example, hence it is called Constant Space
Complexity.
Hence the total memory requirement will be (4n + 12), which is increasing linearly
with the increase in the input value n, hence it is called as Linear Space
Complexity.
Similarly, we can have quadratic and other complex space complexity as well, as
the complexity of an algorithm increases.
But we should always focus on writing algorithm code in such a way that we keep
the space complexity minimum.
Asymptotic Notations
When it comes to analysing the complexity of any algorithm in terms of time and
space, we can never provide an exact number to define the time required and the
223
Data Structures and Algorithms Unit – 7
Also, When we compare the execution times of two algorithms the constant
coefficients of higher order terms are also neglected.
An algorithm that takes a time of 200n2 will be faster than some other algorithm
that takes n3 time, for any value of n larger than 200. Since we're only interested
in the asymptotic behavior of the growth of the function, the constant factor can
be ignored too.
224
Data Structures and Algorithms Unit – 7
The word Asymptotic means approaching a value or curve arbitrarily closely (i.e.,
as some sort of limit is taken).
The only difference being, here we do not have to find the value of any expression
where n is approaching any finite number or infinity, but in case of Asymptotic
notations, we use the same model to ignore the constant factors and insignificant
parts of an expression, to device a better way of representing complexities of
algorithms, in a single coefficient, so that comparison between algorithms can be
done easily.
If we have two algorithms with the following expressions representing the time
required by them for execution, then:
Expression 1: (20n2 + 3n - 4)
Now, as per asymptotic notations, we should just worry about how the function
will grow as the value of n(input) will grow, and that will entirely depend on n2 for
the Expression 1, and on n3 for Expression 2. Hence, we can clearly say that the
algorithm for which running time is represented by the Expression 2, will grow
faster than the other one, simply by analysing the highest power coeeficient and
ignoring the other constants(20 in 20n2) and insignificant parts of the
expression(3n - 4 and 100n - 2).
The main idea behind casting aside the less important part is to make
things manageable.
225
Data Structures and Algorithms Unit – 7
All we need to do is, first analyse the algorithm to find out an expression to define
it's time requirements and then analyse how that expression will grow as the
input(n) will grow.
2. Big Oh(O)
When we say tight bounds, we mean that the time compexity represented by the
Big-Θ notation is like the average value or range within which the actual time of
execution of the algorithm will be.
For example, if for some algorithm the time complexity is represented by the
expression 3n2 + 5n, and we use the Big-Θ notation to represent this, then the
time complexity would be Θ(n2), ignoring the constant coefficient and removing
the insignificant part, which is 5n.
Here, in the example above, complexity of Θ(n2) means, that the avaerage time
for any input n will remain in between, k1 * n2 and k2 * n2, where k1, k2 are two
constants, therby tightly binding the expression rpresenting the growth of the
algorithm.
226
Data Structures and Algorithms Unit – 7
This notation is known as the upper bound of the algorithm, or a Worst Case of an
algorithm.
It tells us that a certain function will never exceed a specified time for any value of
input n.
The question is why we need this representation when we already have the big-Θ
notation, which represents the tightly bound running time for any algorithm. Let's
take a small example to understand this.
In Worst case, starting from the front of the array, we find the element or
number we are searching for at the end, which will lead to a time complexity of n,
where n represents the number of total elements.
But it can happen, that the element that we are searching for is the first element
of the array, in which case the time complexity will be 1.
Now in this case, saying that the big-Θ or tight bound time complexity for Linear
search is Θ(n), will mean that the time required will always be related to n, as this
is the right way to represent the average time complexity, but when we use the
big-O notation, we mean to say that the time complexity is O(n), which means
227
Data Structures and Algorithms Unit – 7
that the time complexity will never exceed n, defining the upper bound, hence
saying that it can be less than or equal to n, which is the correct representation.
This is the reason, most of the time you will see Big-O notation being used to
represent the time complexity of any algorithm, because it makes more sense.
Big Omega notation is used to define the lower bound of any algorithm or we can
say the best case of any algorithm.
This always indicates the minimum time required for any algorithm for all input
values, therefore the best case of any algorithm.
In simple words, when we represent a time complexity for any algorithm in the
form of big-Ω, we mean that the algorithm will take atleast this much time to
cmplete it's execution. It can definitely take more time than this too.
T(n) = 2T(n/2) + cn
T(n) = 2T(n/2) + √n
These types of recurrence relations can be easily solved using Master Method.
For recurrence relation T(n) = 2T(n/2) + cn, the values of a = 2, b = 2 and k =1.
Here logb(a) = log2(2) = 1 = k. Therefore, the complexity will be Θ(nlog2(n)).
Similarly for recurrence relation T(n) = 2T(n/2) + √n, the values of a = 2, b = 2 and
k =1/2. Here logb(a) = log2(2) = 1 > k. Therefore, the complexity will be Θ(n).
T(n) = T(n-1) + n
= T(n-2) + (n-1) + n
Substituting k = n, we get
T(n) = T(√n) + 1
S(m) = S(m/2) + 1
S(m) = Θ(logm)
As n = 2^m or m = log2(n),
Solution: For Tower of Hanoi, T(n) = 2T(n-1) + c for n>1 and T(1) = 1. Solving this,
T(n) = 2T(n-1) + c
= 2^k*T(n-k) + (c + 2c + .. kc)
S(m) = 2S(m/2) + 1
S(m) = Θ(m)
230
Data Structures and Algorithms Unit – 7
As n = 2^m or m = log2n,
o Combine the solution of the subproblems (top level) into a solution of the
whole original problem.
o Greedy Algorithm always makes the choice (greedy criteria) looks best at
the moment, to optimize a given objective.
231
Data Structures and Algorithms Unit – 7
4. Branch and Bound: In Branch & Bound algorithm a given subproblem, which
cannot be bounded, has to be divided into at least two new restricted
subproblems. Branch and Bound algorithm are methods for global optimization in
non-convex problems. Branch and Bound algorithms can be slow, however in the
worst case they require effort that grows exponentially with problem size, but in
some cases we are lucky, and the method coverage with much less effort.
Loop invariants
o If Si-1 is true before iteration i begin, then one can show that S i will be true
after iteration i is over.
232
Data Structures and Algorithms Unit – 7
In divide and conquer approach, a problem is divided into smaller problems, then
the smaller problems are solved independently, and finally the solutions of
smaller problems are combined into a solution for the large problem.
Combine the solutions to the sub-problems into the solution for the
original problem.
In this approach, most of the algorithms are designed using recursion, hence
memory management is very high. For recursive function stack is used, where
function state needs to be stored.
233
Data Structures and Algorithms Unit – 7
Following are some problems, which are solved using divide and conquer
approach.
Merge sort
Binary search
Dynamic Programming
234
Data Structures and Algorithms Unit – 7
Greedy Algorithms
Greedy algorithms try to find a localized optimum solution, which may eventually
lead to globally optimized solutions. However, generally greedy algorithms do not
provide globally optimized solutions.
Counting Coins
This problem is to count to a desired value by choosing the least possible coins
and the greedy approach forces the algorithm to pick the largest possible coin. If
we are provided coins of ₹ 1, 2, 5 and 10 and we are asked to count ₹ 18 then the
greedy procedure will be −
Though, it seems to be working fine, for this count we need to pick only 4 coins.
But if we slightly change the problem then the same approach may not be able to
produce the same optimum result.
For the currency system, where we have coins of 1, 7, 10 value, counting coins for
value 18 will be absolutely optimum but for count like 15, it may use more coins
than necessary. For example, the greedy approach will use 10 + 1 + 1 + 1 + 1 + 1,
total 6 coins. Whereas the same problem could be solved by using only 3 coins (7
+ 7 + 1)
235
Data Structures and Algorithms Unit – 7
Hence, we may conclude that the greedy approach picks an immediate optimized
solution and may fail where global optimization is a major concern.
Examples
Most networking algorithms use the greedy approach. Here is a list of few of
them −
Knapsack Problem
There are lots of similar problems that uses the greedy approach to find an
optimum solution.
236
Data Structures and Algorithms Unit – 7
In the same decade, Prim and Kruskal achieved optimization strategies that
were based on minimizing path costs along weighed routes.
Till date, protocols that run the web, such as the open-shortest-path-first
(OSPF) and many other network packet switching protocols use the greedy
strategy to minimize time spent on a network.
Logic in its easiest form was boiled down to "greedy" or "not greedy". These
statements were defined by the approach taken to advance in each algorithm
stage.
In short, an algorithm ceases to be greedy if at any stage it takes a step that is not
locally greedy. The problem halts with no further scope of greed.
You will take the maximum quantity of resources in the time a constraint
applies.
237
Data Structures and Algorithms Unit – 7
The greedy approach has a few tradeoffs, which may make it suitable for
optimization.
In the activity scheduling example, there is a "start" and "finish" time for every
activity. Each Activity is indexed by a number for reference. There are two activity
categories.
238
Data Structures and Algorithms Unit – 7
1. considered activity: is the Activity, which is the reference from which the
ability to do more than one remaining Activity is analyzed.
The total duration gives the cost of performing the activity. That is (finish - start)
gives us the durational as the cost of an activity.
You will learn that the greedy extent is the number of remaining activities you
can perform in the time of a considered activity.
It is not suitable for problems where a solution is required for every subproblem
like sorting.
In such problems, the greedy strategy can be wrong; in the worst case even lead
to a non-optimal solution.
Therefore the disadvantage of greedy algorithms is using not knowing what lies
ahead of the current greedy state.
239
Data Structures and Algorithms Unit – 7
In the greedy scan shown here as a tree (higher value higher greed), an algorithm
state at value: 40, is likely to take 29 as the next value. Further, its quest ends at
12. This amounts to a value of 41.
Backtracking
Optimisation problem used to find the best solution that can be applied.
Enumeration problem used to find the set of all feasible solutions of the
problem.
240
Data Structures and Algorithms Unit – 7
Example,
Here,
Green is the start point, blue is the intermediate point, red are points with no
feasible solution, dark green is end solution.
Algorithm
Step 2 − else,
Step 4 − else, if current_position is not end point, explore and repeat above steps.
Let’s use this backtracking problem to find the solution to N-Queen Problem.
241
Data Structures and Algorithms Unit – 7
Here, the binary output for n queen problem with 1’s as queens to the positions
are placed.
{0 , 1 , 0 , 0}
{0 , 0 , 0 , 1}
{1 , 0 , 0 , 0}
{0 , 0 , 1 , 0}
For solving n queens problem, we will try placing queen into different positions of
one row. And checks if it clashes with other queens. If current positioning of
queens if there are any two queens attacking each other. If they are attacking, we
will backtrack to previous location of the queen and change its positions. And
check clash of queen again.
Algorithm
Step 2.1 − After placing the queen, mark the position as a part of the solution
and then recursively check if this will lead to a solution.
Step 2.2 − Now, if placing the queen doesn’t lead to a solution and trackback
and go to step (a) and place queens to other rows.
Step 4 − If all rows are tried and no solution is found, return FALSE.
Generally, every constraint satisfaction problem which has clear and well-defined
constraints on any objective solution, that incrementally builds candidate to the
solution and abandons a candidate (“backtracks”) as soon as it determines that
the candidate cannot possibly be completed to a valid solution, can be solved by
Backtracking. However, most of the problems that are discussed, can be solved
using other known algorithms like Dynamic Programming or Greedy Algorithms in
logarithmic, linear, linear-logarithmic time complexity in order of input size, and
therefore, outshine the backtracking algorithm in every respect (since
backtracking algorithms are generally exponential in both time and space).
However, a few problems still remain, that only have backtracking algorithms to
solve them until now.
Consider a situation that you have three boxes in front of you and only one of
them has a gold coin in it but you do not know which one. So, in order to get the
coin, you will have to open all of the boxes one by one. You will first check the
243
Data Structures and Algorithms Unit – 7
first box, if it does not contain the coin, you will have to close it and check the
second box and so on until you find the coin. This is what backtracking is, that is
solving all sub-problems one by one in order to reach the best possible solution.
The Algorithm begins to build up a solution, starting with an empty solution set
. S = {}
1. Add to the first move that is still left (All possible moves are added to
one by one). This now creates a new sub-tree in the search tree of the
algorithm.
4. If the check for returns that it is a solution for the entire data .
Output and terminate the program.
If not, then return that no solution is possible with the current and hence
discard it.
244
Data Structures and Algorithms Unit – 7
3. if (found a solution) :
4. solutionsFound = solutionsFound + 1;
5. displaySolution();
7. System.exit(0);
8. return
9.
18. displaySolution();
20.
245
Data Structures and Algorithms Unit – 7
The expected output is a binary matrix which has 1s for the blocks where queens
are placed. For example, following is the output matrix for the above 4 queen
solution.
{ 0, 1, 0, 0}
{ 0, 0, 0, 1}
{ 1, 0, 0, 0}
246
Data Structures and Algorithms Unit – 7
{ 0, 0, 1, 0}
return true
3) Try all rows in the current column. Do following for every tried row.
a) If the queen can be placed safely in this row then mark this [row,
true.
3) If all rows have been tried and nothing worked, return false to trigger
backtracking.
Branch and bound is an algorithm design paradigm which is generally used for
solving combinatorial optimization problems. These problems are typically
247
Data Structures and Algorithms Unit – 7
exponential in terms of time complexity and may require exploring all possible
permutations in worst case. The Branch and Bound Algorithm technique solves
these problems relatively quickly.
There are many algorithms by which the knapsack problem can be solved:
the Branch and Bound Approach to solve the 0/1 Knapsack problem: The
Backtracking Solution can be optimized if we know a bound on best possible
solution subtree rooted with every node. If the best in subtree is worse than
current best, we can simply ignore this node and its subtrees. So we compute
bound (best solution) for every node and compare the bound with current best
solution before exploring the node.
Example bounds used in below diagram are, A down can give $315, B down can
$275, C down can $225, D down can $125 and E down can $30.
248
Data Structures and Algorithms Unit – 7
Lower Bound Theory Concept is based upon the calculation of minimum time that
is required to execute an algorithm is known as a lower bound theory or Base
Bound Theory.
Lower Bound Theory uses a number of methods/techniques to find out the lower
bound.
Techniques:
1. Comparisons Trees.
249
Data Structures and Algorithms Unit – 7
1. Comparison trees:
o ai = aj equal to
To determine their relative order, if we assume all elements are distinct, then we
just need to consider ai ≤ aj '=' is excluded &, ≥,≤,>,< are equivalent.
Consider sorting three numbers a1, a2, and a3. There are 3! = 6 possible
combinations:
Decision Tree: A decision tree is a full binary tree that shows the comparisons
between elements that are executed by an appropriate sorting algorithm
operating on an input of a given size. Control, data movement, and all other
conditions of the algorithm are ignored.
250
Data Structures and Algorithms Unit – 7
N! ≤2n
251
Data Structures and Algorithms Unit – 7
1. 1,2,3,4,5,6,7,8,9,10,11,12,13,14
252
Data Structures and Algorithms Unit – 7
1. 2, 4, 6, 8, 10, 12, 14
Thus, we will consider all the midpoints and we will make a tree of it by having
stepwise midpoints.
253
Data Structures and Algorithms Unit – 7
For Example
2k-1
23-1= 8-1=7
Where k = level=3
N! ≤ 2k-1
14 < 15
Where N = Nodes
Here, Internal Nodes will always be less than 2k in the Binary Search.
254
Data Structures and Algorithms Unit – 7
Step5:
n+1<= 2k
k >=
k >=log2(n+1)
Step6:
1. T (n) = k
Step7:
T (n) >=log2(n+1)
Given some model of estimation such as comparison trees, the oracle tells us the
outcome of each comparison.
In order to derive a good lower bound, the oracle efforts it's finest to cause the
algorithm to work as hard as it might.
It does this by deciding as the outcome of the next analysis, the result which
matters the most work to be needed to determine the final answer.
And by keeping step of the work that is finished, a worst-case lower bound for the
problem can be derived.
255
Data Structures and Algorithms Unit – 7
Example: (Merging Problem) given the sets A (1: m) and B (1: n), where the
information in A and in B are sorted. Consider lower bounds for algorithms
combining these two sets to give an individual sorted set.
Consider that all of the m+n elements are specific and A (1) < A (2) < ....< A (m)
and B (1) < B (2) < ....< B (n).
Elementary combinatory tells us that there are C ((m+n), n)) ways that the A's and
B's may merge together while still preserving the ordering within A and B.
Thus, if we need comparison trees as our model for combining algorithms, then
there will be C ((m+n), n)) external nodes and therefore at least log C ((m+n), m)
comparisons are needed by any comparison-based merging algorithm.
The upper bound and lower bound can get promptly far apart as m gets much
smaller than n.
1. State Space Method is a set of rules that show the possible states (n-tuples)
that an algorithm can assume from a given state of a single comparison.
2. Once the state transitions are given, it is possible to derive lower bounds by
arguing that the finished state cannot be reached using any fewer transitions.
4. Aim: When state changed count it that is the aim of State Space Method.
256
Data Structures and Algorithms Unit – 7
6. Analysis of the problem to find out the smallest and biggest items by using the
state space method.
2. To find out largest & second largest elements from an array of an element.
9. For the largest item, we need 7 comparisons and what will be the second
largest item?
Now we count those teams who lose the match with team A
10. In this no of comparisons are equal to the number of changes of states during
the execution of the algorithm.
257
Data Structures and Algorithms Unit – 7
Firstly, A, B compare out or match between them. A wins and in C, D.C wins and
so on. We can assume that B wins and so on. We can assume that B wins in place
of A it can be anything depending on our self.
258
Data Structures and Algorithms Unit – 7
Phase-3: This is a Phase in which teams which come under C-State are considered
and there will be matches between them to find out the team which is never
winning at all.
In this Structure, we are going to move upward for denoting who is not winning
after the match.
259
Data Structures and Algorithms Unit – 7
Here H is the team which is never winning at all. By this, we fulfill our second aim
to.
260
Data Structures and Algorithms Unit – 7
Lower bound (L (n)) is a property of the particular issue i.e. the sorting problem,
matrix multiplication not of any particular algorithm solving that problem.
Lower bound theory says that no calculation can carry out the activity in less than
that of (L (n)) times the units for arbitrary inputs i.e. that for every comparison
based sorting algorithm must take at least L (n) time in the worst case.
Trivial lower bounds are utilized to yield the bound best alternative is to count
the number of elements in the problems input that must be prepared and the
number of output items that need to be produced.
The lower bound theory is the method that has been utilized to establish the
given algorithm in the most efficient way which is possible. This is done by
discovering a function g (n) that is a lower bound on the time that any algorithm
261
Data Structures and Algorithms Unit – 7
must take to solve the given problem. Now if we have an algorithm whose
computing time is the same order as g (n) , then we know that asymptotically we
cannot do better.
If f (n) is the time for some algorithm, then we write f (n) = Ω (g (n)) to mean
that g (n) is the lower bound of f (n) . This equation can be formally written, if
there exists positive constants c and n0 such that |f (n)| >= c|g (n)| for all n > n0.
In addition for developing lower bounds within the constant factor, we are more
conscious of the fact to determine more exact bounds whenever this is possible.
Suppose we already have a lower bound S(n) on some problem A, and that there
is a reduction from A to some other problem B. Formally, a reduction is a function
f such that A(x) = B(f(x)). Let Tf(n) be an upper bound on the time to compute f(x)
when |x| = n, and let TB(n) be an upper bound on the time to compute B(x) when
|x| = n. Then computing A(x) as B(f(x)) with |x| = n takes time at most T f(n) +
TB(Tf(n)). (The odd second term comes from the fact that Tf(n) is an upper bound
on the length of the output of f(x); note that we are also assuming here that T B is
nondecreasing.)
An important special case of this is when Tf = O(nc) and TB = O(nd) for some c and
d. Then Tf(n) + Tf(TB(n)) = O(nc) + O(ncd) = O(ncd). In words:
How do we get lower bounds out of this? Since S(n) ≤ T f(n) + Tf(TB(n)), we have
TB(n) ≥ Tf-1(S(n) - Tf(n)). For example:
262
Data Structures and Algorithms Unit – 7
Shortest Paths
The shortest path problem is about finding a path between 2 vertices in a graph
such that the total sum of the edges weights is minimum.
This problem could be solved easily using (BFS) if all edge weights were (1), but
here weights can take any value. Three different algorithms are discussed below
depending on the use-case.
Bellman Ford's algorithm is used to find the shortest paths from the source vertex
to all other vertices in a weighted graph. It depends on the following concept:
Shortest path contains at most n−1 edges, because the shortest path couldn't
have a cycle.
Algorithm Steps:
263
Data Structures and Algorithms Unit – 7
Loop over all edges, check if the next node distance > current node distance
+ edge weight, in this case update the next node distance to "current node
distance + edge weight".
This algorithm depends on the relaxation principle where the shortest distance
for all vertices is gradually replaced by more accurate values until eventually
reaching the optimum solution. In the beginning all vertices have a distance of
"Infinity", but only the distance of the source vertex = 0, then update all the
connected vertices with the new distances (source vertex distance + edge
weights), then apply the same concept for the new vertices with new distances
and so on.
Implementation:
v[i].clear();
dis[i] = 2e9;
v[i].push_back(from);
v[i].push_back(next);
v[i].push_back(weight);
dis[0] = 0;
int j = 0;
while(v[j].size() != 0){
j++;
265
Data Structures and Algorithms Unit – 7
Dijkstra's Algorithm
Dijkstra's algorithm has many variants but the most common one is to find the
shortest paths from the source vertex to all other vertices in the graph.
Algorithm Steps:
Set all vertices distances = infinity except for the source vertex, set the
source distance = 0.
Pop the vertex with the minimum distance from the priority queue (at first
the popped vertex = source).
If the popped vertex is visited before, just continue without using it.
Apply the same algorithm again until the priority queue is empty.
Implementation:
vector < pair < int , int > > v [SIZE]; // each vertex has all the connected vertices
with the edges weights
266
Data Structures and Algorithms Unit – 7
void dijkstra(){
dist[1] = 0;
multiset < pair < int , int > > s; // multiset do the job as a min-priority
queue
while(!s.empty()){
pair <int , int> p = *s.begin(); // pop the vertex with the minimum
distance
s.erase(s.begin());
vis[x] = true;
dist[e] = dist[x] + w;
However, if we have to find the shortest path between all pairs of vertices, both
of the above methods would be expensive in terms of time. Discussed below is
another alogorithm designed for this case.
Floyd\u2013Warshall's Algorithm
268
Data Structures and Algorithms Unit – 7
Find all pair shortest paths that use 0 intermediate vertices, then find the
shortest paths that use 1 intermediate vertex and so on.. until using
all N vertices as intermediate nodes.
Minimize the shortest paths between any 2 pairs in the previous operation.
For any 2 vertices (i,j) , one should actually minimize the distances between
this pair using the first K nodes, so the shortest path will
be: min(dist[i][k]+dist[k][j],dist[i][j]).
Maximum flow
For any non-source and non-sink node, the input flow is equal to output
flow.
Total flow out of the source node is equal total to flow in to the sink node.
Maximum Flow:
It is defined as the maximum amount of flow that the network would allow to
flow from source to sink. Multiple algorithms exist in solving the maximum flow
problem. Two major algorithms to solve these kind of problems are Ford-
Fulkerson algorithm and Dinic's Algorithm. They are explained below.
Ford-Fulkerson Algorithm:
It was developed by L. R. Ford, Jr. and D. R. Fulkerson in 1956. A pseudocode for
this algorithm is given below,
Inputs required are network graph G, source node S and sink node T.
return
270
Data Structures and Algorithms Unit – 7
An augmenting path is a simple path from source to sink which do not include any
cycles and that pass only through positive weighted edges. A residual network
graph indicates how much more flow is allowed in each edge in the network
graph. If there are no augmenting paths possible from S to T, then the flow is
maximum. The result i.e. the maximum flow will be the total flow out of source
node which is also equal to total flow in to the sink node.
271
Data Structures and Algorithms Unit – 7
272
Data Structures and Algorithms Unit – 7
Implementation:
Updating residual graph includes following steps: (refer the diagrams for
better understanding)
Dinic's Algorithm
Level graph is one where value of each node is its shortest distance from source.
Blocking flow includes finding the new path from the bottleneck node.
Residual graph and augmenting paths are previously discussed.
273
Data Structures and Algorithms Unit – 7
Inputs required are network graph G, source node S and sink node T.
F=F+f
return F
Update of level graph includes removal of edges with full capacity. Removal of
nodes that are not sink and are dead ends. A demonstration of working of Dinic's
algorithm is shown below with the help of diagrams.
274
Data Structures and Algorithms Unit – 7
275
Data Structures and Algorithms Unit – 7
Optimization Problem
Decision Problem
There are many problems for which the answer is a Yes or a No. These types of
problems are known as decision problems. For example,
What is Language?
Every decision problem can have only two answers, yes or no. Hence, a decision
problem may belong to a language if it provides an answer ‘yes’ for a specific
input. A language is the totality of inputs for which the answer is Yes. Most of the
algorithms discussed in the previous chapters are polynomial time algorithms.
Algorithms such as Matrix Chain Multiplication, Single Source Shortest Path, All
Pair Shortest Path, Minimum Spanning Tree, etc. run in polynomial time. However
there are many problems, such as traveling salesperson, optimal graph coloring,
276
Data Structures and Algorithms Unit – 7
Hamiltonian cycles, finding the longest path in a graph, and satisfying a Boolean
formula, for which no polynomial time algorithms is known. These problems
belong to an interesting class of problems, called the NP-Complete problems,
whose status is unknown.
P-Class
The class P consists of those problems that are solvable in polynomial time, i.e.
these problems can be solved in time O(nk) in worst-case, where k is constant.
These problems are called tractable, while others are called intractable or
superpolynomial.
Problem requiring Ω(n50) time to solve are essentially intractable for large n. Most
known polynomial time algorithm run in time O(nk) for fairly low value of k.
NP-Class
The class NP consists of those problems that are verifiable in polynomial time. NP
is the class of decision problems for which it is easy to check the correctness of a
claimed answer, with the aid of a little extra information. Hence, we aren’t asking
for a way to find a solution, but only to verify that an alleged solution really is
correct.
Every problem in this class can be solved in exponential time using exhaustive
search.
277
Data Structures and Algorithms Unit – 7
P versus NP
All problems in P can be solved with polynomial time algorithms, whereas all
problems in NP - P are intractable.
It is not known whether P = NP. However, many problems are known in NP with
the property that if they belong to P, then it can be proved that P = NP.
The problem belongs to class P if it’s easy to find a solution for the problem. The
problem belongs to NP, if it’s easy to check a solution that may have been very
tedious to find.
NP-Completeness
If a polynomial time algorithm exists for any of these problems, all problems in NP
would be polynomial time solvable. These problems are called NP-complete. The
phenomenon of NP-completeness is important for both theoretical and practical
reasons.
Definition of NP-Completeness
B is in NP
If a language satisfies the second property, but not necessarily the first one, the
language B is known as NP-Hard. Informally, a search problem B is NP-Hard if
there exists some NP-Complete problem A that Turing reduces to B.
NP-Complete Problems
NP-Hard Problems
Set Cover
Vertex Cover
TSP is NP-Complete
279
Data Structures and Algorithms Unit – 7
The traveling salesman problem consists of a salesman and a set of cities. The
salesman has to visit each one of the cities starting from a certain one and
returning to the same city. The challenge of the problem is that the traveling
salesman wants to minimize the total length of the trip
Proof
To prove TSP is NP-Complete, first we have to prove that TSP belongs to NP. In
TSP, we find a tour and check that the tour contains each vertex once. Then the
total cost of the edges of the tour is calculated. Finally, we check if the cost is
minimum. This can be completed in polynomial time. Thus TSP belongs to NP.
Secondly, we have to prove that TSP is NP-hard. To prove this, one way is to show
that Hamiltonian cycle ≤p TSP (as we know that the Hamiltonian cycle problem is
NPcomplete).
Hence, an instance of TSP is constructed. We create the complete graph G' = (V,
E'), where
E′={(i,j):i,j∈Vandi≠j
Now, suppose that a Hamiltonian cycle h exists in G. It is clear that the cost of
each edge in h is 0 in G' as each edge belongs to E. Therefore, h has a cost
of 0 in G'. Thus, if graph G has a Hamiltonian cycle, then graph G' has a tour
of 0 cost.
Conversely, we assume that G' has a tour h' of cost at most 0. The cost of edges
in E' are 0 and 1 by definition. Hence, each edge must have a cost of 0 as the cost
of h' is 0. We therefore conclude that h' contains only edges in E.
280
Data Structures and Algorithms Unit – 7
We have thus proven that G has a Hamiltonian cycle, if and only if G' has a tour of
cost at most 0. TSP is NP-complete.
Reducibility
(34.1)
281
Data Structures and Algorithms Unit – 7
Figure
What is Reduction?
Let L1 and L2 be two decision problems. Suppose algorithm A 2 solves L2. That is, if y
is an input for L2 then algorithm A2 will answer Yes or No depending upon
whether y belongs to L2 or not.
The idea is to find a transformation from L1 to L2 so that the algorithm A2 can be
part of an algorithm A1 to solve L1.
282
Data Structures and Algorithms Unit – 7
NUMBER-THEORETIC ALGORITHMS
Number theory was once viewed as a beautiful but largely useless subject in pure
mathematics. Today number-theoretic algorithms are used widely, due in part to
the invention of cryptographic schemes based on large prime numbers. The
283
Data Structures and Algorithms Unit – 7
feasibility of these schemes rests on our ability to find large primes easily, while
their security rests on our inability to factor the product of large primes. This
chapter presents some of the number theory and associated algorithms that
underlie such applications.
Because we shall be working with large integers, we need to adjust how we think
about the size of an input and about the cost of elementary arithmetic
operations.
In this chapter, a "large input" typically means an input containing "large integers"
rather than an input containing "many integers" (as for sorting). Thus, we shall
measure the size of an input in terms of the number of bits required to represent
that input, not just the number of integers in the input. An algorithm with integer
inputs a1, a2, . . . , ak is a polynomial-time algorithm if it runs in time polynomial in
284
Data Structures and Algorithms Unit – 7
lg a1, lg a2, . . . , lg ak, that is, polynomial in the lengths of its binary-encoded
inputs.
This section provides a brief review of notions from elementary number theory
concerning the set Z = {. . . , -2, -1, 0, 1, 2, . . .} of integers and the set N = {0, 1, 2, .
. .} of natural numbers.
The notion of one integer being divisible by another is a central one in the theory
of numbers. The notation d | a (read "d divides a") means that a = kd for some
integer k. Every integer divides 0. If a > 0 and d | a, then |d| |a|. If d | a, then
we also say that a is a multiple of d. If d does not divide a, we write .
285
Data Structures and Algorithms Unit – 7
An integer a > 1 whose only divisors are the trivial divisors 1 and a is said to be
a prime number (or, more simply, a prime). Primes have many special properties
and play a critical role in number theory. The small primes, in order, are
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,... .
Exercise 33.1-1 asks you to prove that there are infinitely many primes. An
integer a > 1 that is not prime is said to be a composite number (or, more simply,
a composite). For example, 39 is composite because 3 | 39. The integer 1 is said
to be a unit and is neither prime nor composite. Similarly, the integer 0 and all
negative integers are neither prime nor composite.
Given an integer n, the integers can be partitioned into those that are multiples
of n and those that are not multiples of n. Much number theory is based upon a
refinement of this partition obtained by classifying the nonmultiples
of n according to their remainders when divided by n. The following theorem is
the basis for this refinement. The proof of this theorem will not be given here
(see, for example, Niven and Zuckerman [151]).
Theorem 1
For any integer a and any positive integer n, there are unique
integers q and r such that 0 r < n and a = qn + r.
286
Data Structures and Algorithms Unit – 7
The value q = a/n is the quotient of the division. The value r = a mod n is
the remainder (or residue) of the division. We have that n | a if and only
if a mod n = 0. It follows that
or
[a]n = {a + kn : k Z} .
For example, [3]7 = {. . . , -11, -4, 3, 10, 17, . . .}; other denotations for this set are
[-4]7 and [10]7. Writing a [b]n is the same as writing a b (mod n). The set of all
such equivalence classes is
287
Data Structures and Algorithms Unit – 7
The fast Fourier transform (FFT) is an algorithm which can take the discrete
Fourier transform of a array of size n = 2N in Θ(n ln(n)) time. This algorithm is
generally performed in place and this implementation continues in that tradition.
Two implementations are provided:
The value π is found using double const PI = 4.0*std::atan( 1.0 ); The inverse
tangent function of 1 returns π/4 and and multiplication by a power of 2 does not
affect the relative error of a floating point number: in this case, multiplication by
four simply adds two to the exponent.
The testing function applies the fast Fourier transform to the vector
(2, 2, 0, 0, 0, 0, 0, 0)T. It prints the vector, the fast Fourier transform of the vector,
and the inverse fast Fourier transform of that result. The output is
288
Data Structures and Algorithms Unit – 7
The function printing the complex array will print 0 in place of either component
being less than 10-15. This is to make aid in the visualization of the result.
The discrete Fourier transform maps a vector from the space domain to the
complex frequency domain. To describe this, recall that a vector of n dimensions
represents coordinates in space: the basis vectors are the n unit vectors
(1, 0, 0, ···), (0, 1, 0, ···), etc. These are shown in Figure 1.
While these unit vectors are exceelent for representing the specific location of a
vector, they do not suggest any periodicity. The discrete Fourier transforms is a
linear transformation from a basis of unit vectors to a basis of vectors which are
capture the periodic behaviour of the unit vector: if the copies of the vector were
laid end-to-end, what periodicity would there be in the patterns and what are the
periods thereof.
289
Data Structures and Algorithms Unit – 7
This code uses the STL complex class which has a constructor which takes the real
and imaginary parts as arguments. The following functions are friends of the class
and for the argument z = a + jb:
Function Returns
complex conj(z) z* = a − jb
complex exp(z) ez
complex pow(w, z) wz
complex sqrt(z) √z
290
Data Structures and Algorithms Unit – 7
The assertion assert( n > 0 && (n & (~n + 1)) == n ); checks to ensure that the
argument n is both positive and a power of two. The operation n & (~n +
1) selects the least significant one in the integer. If the least significant one is also
the most significant one, then the bitwise and will return that value; otherwise,
the result will not equal the original value n. This is shown in Figure 3 with both a
power of two (64) and a number which is not a power of two (84).
Figure 3. Selecting the least significant one (1) bit in a binary number.
291
Data Structures and Algorithms Unit – 7
Example:
S="Galois"
|S|=6
S[1...4]="aloi"
W="Evariste"
W+S="EvaristeGalois"
This simple problem has a lot of applications. For example, the text can be the
nucleotide sequence of the human DNA and the pattern a genomic sequence of
some genetic disease, or the text can be all the internet, and the pattern a query
(the Google problem), etc.
We are going to study the exact string matching problem, that is given two strings
T and P we want to find all substrings of T that are equal to P. Formally, we want
to calculate all indices i such that T[i+s] = P[s] for each 0 ≤ s ≤ |P|-1.
In the following example, you are given a string T and a pattern P, and all the
occurrences of P in T are highlighted in red.
292
Data Structures and Algorithms Unit – 7
One easy way to solve the problem, is to iterate over all i from 0to |T| - |P|, and
check if there is a substring of T that starts at i and matches with P:
def find_occurrences(t,p):
lt,lp=len(t),len(p)
for i in range(lt-lp+1):
match=True
for l in range(lp):
if t[i+l]!=p[l]:
match=False
break
if match: print i
Suppose that we are finding a match starting at position i on the text, then there
are two possibilities:
We don’t find a match. Then there exists at least one index in which the text is
not equal to the pattern. Let i+j be the smallest of such indices: T[i...i+j-1] = P[0...j-
1] and T[i+j] ≠ P[j]
293
Data Structures and Algorithms Unit – 7
Since we already know that T matches with P from i to i+j-1, then T[i+k...i+j-1] is a
suffix of P[0...j-1].
That means that if we find a mismatch at position j, we should start from the
smallest k such that P[k...j-1] is a prefix of P. k is the smallest, so P[k...j-1] is the
largest proper suffix that is also a proper prefix. From now on we will call “border”
to the proper prefixes that are also proper suffixes (e.g the string ABCDABCDAB
have two borders ABCDAB and AB).
We find a match. Using the same argument, it’s easy to see that we have to start
finding for a match from the smallest k such that P[k...j-1]is a proper prefix of P
294
Data Structures and Algorithms Unit – 7
In the picture above we are finding occurrences starting from position 4, but
there is a mismatch at position 12. Before getting a mismatch we have already
matched the string HACKHACK, and the largest proper prefix that is also a proper
suffix (border) of HACKHACK is HACK, so we should start finding occurrences again
from position 8 and taking into account that all characters from 8 to 11 are
already matched. It turns out that there is an occurrence starting at position 8, so
all characters of the pattern have matched, since HACKHACKIT does not have any
border, then we start finding occurrences again starting from position 18.
According to the previous analysis we need to know for each i, the the largest
border of P[0...i]. Let f[i] be the length of the largest border of P[0...i]. Function f is
known as failure function, because it says from where start if we find a mismatch.
How can we calculate f[i] efficiently? One approach is to think in an inductive way:
suppose that we have already calculated the function f for for all indices less than
i. Using that information how can we calculate f[i]?
Note that if P[0...j] is a border of P[0...i], then P[0...j-1] is a border of P[0...i-1] and
P[j] = P[i].
295
Data Structures and Algorithms Unit – 7
The previous argument suggest this algorithm for finding f[i]: Iterate over all
borders of P[0...i-1], in decreasing order of length, until find a border P[0...j-1]
such that P[j] = P[i]. How can we iterate over all the borders of a string? That can
be easily done using this observation: if B is a border of P, then a border of B is
also a border of P, that implies that the borders of P[0...i] are prefixes of P that
ends at f[i]-1, f[f[i]-1]-1, f[f[...f[i]-1...]-1]-1, ... (at most how many borders can have
a string?).
What is the complexity of our algorithm? Let P[j...i-1] be the largest border of
P[0...i-1]. If P[k...i] is the largest border of P[0...i], then k ≥ j (Why?). So when we
iterate over the borders of P[0...i-1], we are moving the index j to k. Since j is
moving always to the right, in the worst case it will touch all the elements of the
array. That means that we are calculating function f in O(|P|).
def failure_function(p):
l=len(p)
f=[0]*l
j=0
for i in range(1,l):
if j-1>=0: j=f[j-1]
else: j=-1
j+=1
f[i]=j
return f
296
Data Structures and Algorithms Unit – 7
def find_occurrences(t,p):
f=failure_function(p)
lt,lp=len(t),len(p)
j=0
for i in range(lt):
if j-1>=0: j=f[j-1]
else: j=-1
j+=1
if j==lp:
j=f[lp-1]
print i-lp+1
As you can note from the pseudo code (it is python code indeed),
find_occurrences is almost equal to failure_function, that is because in some
sense failure_function is like matching a string with itself.
The algorithm described above is known as Knut-Morris-Pratt (or KMP for short).
Note that with KMP algorithm we don’t need to have all the string T in memory,
we can read it character by character, and determine all the occurrences of a
pattern P in an online way.
The Z function
Given a string S, let z[i] be the longest substring of S that starts at i and is also a
prefix of S.
297
Data Structures and Algorithms Unit – 7
Example:
z[ 3 ] = 4 because starting from position 3, the largest string that is also a prefix of
S is ABRA.
If we can calculate the function Z efficiently, then how can we find all the
occurrences of a pattern P on a text T? Since z[i] gives matches with a prefix, a
good idea is to observe how the z function behaves when P is concatenated with
T. So let’s calculate the function z on S = P+T. It turns out that if for certain i, z[i] ≥
|P| then there is an occurrence of P starting at i.
The naive approach for calculate z in every i is to iterate over all j ≥ i until we find
a mismatch ( z[i...j] ≠ z[0...j-i] ), then z[i] = j-i. This algorithm is of quadratic time,
so we need a better solution.
Now let’s think again in a inductive way, and calculate z[i] given that we already
know z[0],...,z[i-1].
298
Data Structures and Algorithms Unit – 7
Let [L,R] be the z-box with largest R that we have seen until now.
If i is inside [L,R], then S[i...R] = S[i-L...R-L] (look at the picture above), so we can
use the value of z[i-L] that we have already calculated. If z[i-L] Is less than R-i+1,
then z[i] = z[i-L], otherwise since we already know that S[i...R] is a prefix of S, it
remains to check if we can expand S[i...R] starting from R+1.
On the other hand If i is outside [L...R], then we can calculate z[i] using the naive
approach. This algorithm is linear because the pointer R traverses the array at
most once.
def zeta(s):
n=len(s)
z=[0]*n
L,R=-1,-1
for i in range(1,n):
j,k=0,i
if L<=i<=R:
ii = i-L
j=min(ii+z[ii], R-L+1)-ii
k=i+j
j+=1
k+=1
z[i]=k-i
L,R=i,i+z[i]-1
return z
def find_occurrences(p,t):
lp,lt=len(p),len(t)
z=zeta(p+t)
for i in range(lp,lp+lt):
Since numbers are easy to compare it will be nice if h outputs a number. So the
question is how to represent a string as a number. Note that strings are like
numbers, because we can consider its characters as digits. Since the alphabet
have 26 letters, we could say that a string is a number in a numeration system of
base 27. The problem is that strings can be very large, and we can represent
integers in a very limited range (from 0 to 18446744073709551615 using a 64 bit
unsigned integer), in order to keep the output of h in a small range, let’s apply the
300
Data Structures and Algorithms Unit – 7
modulo operation (is because of this last necessary operation that two different
strings can map to the same integer i.e there is a hash collision).
Note that we are considering the value of the digit “a” as 1 instead of 0, that is to
prevent cases with leading zeroes (e.g “aab” and “b” are different, but if a = 0
they are equal).
A function like h, that converts elements from a very large range (like strings) to
elements in a small range (64 bit integers) are called hash functions The value of
the hash function applied to certain element is called it’s hash.
It remains to solve this problem: if we know the hash of the substring (of length
|P|) that starts at i, how to calculate the hash of the substring that starts at i+1?
(see figure below)
Let h be the hash of the substring of length |P| that starts at i. We can calculate
the hash of the substring that starts at i+1 in two steps:
301
Data Structures and Algorithms Unit – 7
The idea of calculate the hash that starts at position i+1 using the hash at i is
called rolling hash.
return ord(ch)-ord("a")+1
def find_occurrences(p,t):
lp,lt=len(p),len(t)
for i in range(lp):
hp=(hp*b+val(p[i]))%m
ht=(ht*b+val(t[i]))%m
pwr=1
for i in range(lp-1):
pwr=pwr*b%m
if hp==ht: print 0
for i in range(1,lt-lp+1):
302
Data Structures and Algorithms Unit – 7
#rolling hash
ht=(ht-val(t[i-1])*pwr)%m
ht=(ht+m)%m
ht=(ht*b+val(t[i+lp-1]))%m
if ht==hp: print i
Note that in the code above, we are finding matches with high probability
(because of hash collision). It is possible to increase the probability using two
modulos, but in programming contests usually one modulo is enough (given a
modulo how to generate two different strings with the same hash?).
Parallel Algorithm
An algorithm is a sequence of steps that take inputs from the user and after some
computation, produces an output. A parallel algorithm is an algorithm that can
execute several instructions simultaneously on different processing devices and
then combine all the individual outputs to produce the final result.
Concurrent Processing
The easy availability of computers along with the growth of Internet has changed
the way we store and process data. We are living in a day and age where data is
available in abundance. Every day we deal with huge volumes of data that require
complex computing and that too, in quick time. Sometimes, we need to fetch data
from similar or interrelated events that occur simultaneously. This is where we
require concurrent processing that can divide a complex task and process it
multiple systems to produce the output in quick time.
303
Data Structures and Algorithms Unit – 7
Concurrent processing is essential where the task involves processing a huge bulk
of complex data. Examples include − accessing large databases, aircraft testing,
astronomical calculations, atomic and nuclear physics, biomedical analysis,
economic planning, image processing, robotics, weather forecasting, web-based
services, etc.
What is Parallelism?
In this tutorial, we will discuss only about parallel algorithms. Before moving
further, let us first discuss about algorithms and their types.
What is an Algorithm?
Sequential Computer
Parallel Computer
304
Data Structures and Algorithms Unit – 7
It has been found that the time needed by the processors in communicating with
each other is more than the actual processing time. So, while designing a parallel
algorithm, proper CPU utilization should be considered to get an efficient
algorithm.
To design an algorithm properly, we must have a clear idea of the basic model of
computation in a parallel computer.
Model of Computation
SISD Computers
SISD computers contain one control unit, one processing unit, and one memory
unit.
305
Data Structures and Algorithms Unit – 7
SIMD Computers
SIMD computers contain one control unit, multiple processing units, and shared
memory or interconnection network.
Here, one single control unit sends instructions to all processing units. During
computation, at each step, all the processors receive a single set of instructions
from the control unit and operate on different set of data from the memory unit.
Each of the processing units has its own local memory unit to store both data and
instructions. In SIMD computers, processors need to communicate among
themselves. This is done by shared memory or by interconnection network.
306
Data Structures and Algorithms Unit – 7
MISD Computers
As the name suggests, MISD computers contain multiple control units, multiple
processing units, and one common memory unit.
Here, each processor has its own control unit and they share a common memory
unit. All the processors get instructions individually from their own control unit
and they operate on a single stream of data as per the instructions they have
received from their respective control units. This processor operates
simultaneously.
MIMD Computers
MIMD computers have multiple control units, multiple processing units, and
a shared memory or interconnection network.
307
Data Structures and Algorithms Unit – 7
Here, each processor has its own control unit, local memory unit, and arithmetic
and logic unit. They receive different sets of instructions from their respective
control units and operate on different sets of data.
Note
o Distributed system − When all the processors are far away from one
another (e.g.- in the different cities)
308
Data Structures and Algorithms Unit – 7
Enumeration Sort
Enumeration Sort
Enumeration sort is a method of arranging all the elements in a list by finding the
final position of each element in a sorted list. It is done by comparing each
element with all other elements and finding the number of elements having
smaller value.
Therefore, for any two elements, ai and aj any one of the following cases must be
true −
ai < aj
ai > aj
ai = aj
Algorithm
begin
309
Data Structures and Algorithms Unit – 7
C[j] := 0;
C[j] := 1;
else
C[j] := 0;
A[C[j]] := A[j];
end ENUM_SORTING
310
Data Structures and Algorithms Unit – 7
Algorithm
begin
id := process's label
for i := 1 to n do
begin
compare-exchange_min(id + 1);
311
Data Structures and Algorithms Unit – 7
else
compare-exchange_max(id - 1);
compare-exchange_min(id + 1);
else
compare-exchange_max(id - 1);
end for
end ODD-EVEN_PAR
Merge sort first divides the unsorted list into smallest possible sub-lists, compares
it with the adjacent list, and merges it in a sorted order. It implements parallelism
very nicely by following the divide and conquer algorithm.
312
Data Structures and Algorithms Unit – 7
Algorithm
begin
data = sequentialmergesort(data)
for dim = 1 to n
endfor
313
Data Structures and Algorithms Unit – 7
newdata = data
end
Hyper quick sort is an implementation of quick sort on hypercube. Its steps are as
follows −
Split each list locally, then exchange the halves across the highest
dimension.
Algorithm
begin
id := process’s label;
for i := 1 to d do
begin
x := pivot;
314
Data Structures and Algorithms Unit – 7
begin
B := B1 U C;
endif
else
B := B2 U C;
end else
end for
end HYPERQUICKSORT
Approximate Algorithms
315
Data Structures and Algorithms Unit – 7
o For the vertex cover problem, the optimization problem is to find the vertex
cover with fewest vertices, and the approximation problem is to find the
vertex cover with few vertices.
Performance Ratios
Another Example is we are considering for a maximum size Independent set (IS).
An approximate Algorithm returns an IS for us, but the size (cost) may not be
maximum. Let C be the cost of the solution returned by an approximate
algorithm, and C* is the cost of the optimal solution.
We say the approximate algorithm has an approximate ratio P (n) for an input size
n, where
Intuitively, the approximation ratio measures how bad the approximate solution
is distinguished with the optimal solution. A large (small) approximation ratio
measures the solution is much worse than (more or less the same as) an optimal
solution.
316
Data Structures and Algorithms Unit – 7
Observe that P (n) is always ≥ 1, if the ratio does not depend on n, we may
write P. Therefore, a 1-approximation algorithm gives an optimal solution. Some
problems have polynomial-time approximation algorithm with small constant
approximate ratios, while others have best-known polynomial time
approximation algorithms whose approximate ratios grow with
Randomized Algorithms
317
Data Structures and Algorithms Unit – 7
A Central Pivot is a pivot that divides the array in such a way that one side has at-
least 1/4 elements.
318
Data Structures and Algorithms Unit – 7
The important thing in our analysis is, time taken by step 2 is O(n).
How many times while loop runs before finding a central pivot?
The probability that the randomly chosen element is central pivot is 1/2.
Note that the above randomized algorithm is not the best way to implement
randomized Quick Sort. The idea here is to simplify the analysis as it is simple to
analyse.
Typically, randomized Quick Sort is implemented by randomly picking a pivot (no
loop). Or by shuffling array elements.
319
Data Structures and Algorithms Unit – 7
Searching
Searching is the process of finding some particular element in the list. If the
element is present in the list, then the process is called successful and the process
returns the location of that element, otherwise the search is called unsuccessful.
There are two popular search methods that are widely used in order to search
some item into the list. However, choice of the algorithm depends upon the
arrangement of the list.
o Linear Search
o Binary Search
Linear Search
Linear search is the simplest search algorithm and often called sequential search.
In this type of searching, we simply traverse the list completely and match each
element of the list with the item whose location is to be found. If the match found
then location of the item is returned otherwise the algorithm return NULL.
Linear search is mostly used to search an unordered list in which the items are not
sorted. The algorithm of linear search is given as follows.
Algorithm
o LINEAR_SEARCH(A, N, VAL)
[END OF IF]
SET I = I + 1
[END OF LOOP]
o Step 5: IF POS = -1
PRINT " VALUE IS NOT PRESENTIN THE ARRAY "
[END OF IF]
o Step 6: EXIT
Complexity of algorithm
Space O(1)
Binary Search
Binary search is the search technique which works efficiently on the sorted lists.
Hence, in order to search an element into some list by using binary search
technique, we must ensure that the list is sorted.
Binary search follows divide and conquer approach in which, the list is divided
into two halves and the item is compared with the middle element of the list. If
the match is found then, the location of middle element is returned otherwise, we
search into either of the halves depending upon the result produced through the
match.
321
Data Structures and Algorithms Unit – 7
o Step 5: IF POS = -1
PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
o Step 6: EXIT
Complexity
SN Performance Complexity
322
Data Structures and Algorithms Unit – 7
Example
Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location of
the item 23 in the array.
In 1st step :
1. BEG = 0
2. END = 8ron
3. MID = 4
in Second step:
1. Beg = mid +1 = 5
2. End = 8
3. mid = 13/2 = 6
in third step:
1. beg = mid + 1 = 7
2. End = 8
323
Data Structures and Algorithms Unit – 7
3. mid = 15/2 = 7
4. a[mid] = a[7]
5. a[7] = 23 = item;
324
DIWAKAR EDUCATION HUB
2020
a) O(n)
b) O(1)
c) O(n!)
a) max_num(sum[idx – 1] + arr[idx], d) O(n2)
arr[idx])
Answer: a
b) sum[idx – 1] + arr[idx].
Explanation: The above dynamic
c) min_num(sum[idx – 1] + arr[idx], arr[idx])
programming algorithm uses space equal to
d) arr[idx].
the length of the array to store the sum
Answer: a values. So, the space complexity is O(n).
Explanation: The array “sum” is used to
3. Consider the following code snippet:
store the maximum sub-array sum. The
appropriate way to do this is by using:
sum[idx] = max_num(sum[idx – 1] + arr[idx],
arr[idx]).
2
Data Structures and Algorithms Unit – 7 MCQs
Answer: b
Explanation: Array contains elements only
of the same type.
3
Data Structures and Algorithms Unit – 7 MCQs
4
Data Structures and Algorithms Unit – 7 MCQs
5
Data Structures and Algorithms Unit – 7 MCQs
6
Data Structures and Algorithms Unit – 7 MCQs
25. Consider the usual algorithm for stack algorithm to convert the expression
determining whether a sequence of from infix to postfix notation.
parentheses is balanced. The maximum number of symbols that will
Suppose that you run the algorithm on a appear on the stack AT ONE TIME during
sequence that contains 2 left parentheses the conversion of this expression?
and 3 right parentheses (in some order). a) 1
The maximum number of parentheses that b) 2
appear on the stack AT ANY ONE TIME c) 3
during the computation? d) 4
a) 1
Answer: d
b) 2
Explanation: When we perform the
c) 3
conversion from infix to postfix expression
d) 4 or more
+, *, (, * symbols are placed inside the stack.
Answer: b A maximum of 4 symbols are identified
Explanation: In the entire parenthesis during the entire conversion.
balancing method when the incoming token
28. The postfix form of the expression (A+
is a left parenthesis it is pushed into stack. A
B)*(C*D- E)*F / G is?
right parenthesis makes pop operation to
a) AB+ CD*E – FG /**
delete the elements in stack till we get left
b) AB + CD* E – F **G /
parenthesis as top most element. 2 left
c) AB + CD* E – *F *G /
parenthesis are pushed whereas one right
d) AB + CDE * – * F *G /
parenthesis removes one of left
parenthesis. 2 elements are there before Answer: c
right parenthesis which is the maximum Explanation: (((A+ B)*(C*D- E)*F) / G) is
number of elements in stack at run time. converted to postfix expression as
(AB+(*(C*D- E)*F )/ G)
26. What is the value of the postfix
(AB+CD*E-*F) / G
expression 6 3 2 4 + – *:
(AB+CD*E-*F * G/). Thus Postfix expression
a) 1
is AB+CD*E-*F*G/
b) 40
c) 74 29. The data structure required to check
d) -1 whether an expression contains balanced
parenthesis is?
Answer: d
a) Stack
Explanation: Postfix Expression is (6+(3-
b) Queue
(2*4))) which results -18 as output.
c) Array
27. Here is an infix expression: 4 + 3*(6*3- d) Tree
12). Suppose that we are using the usual
7
Data Structures and Algorithms Unit – 7 MCQs
Answer: a c) Array
Explanation: The stack is a simple data d) Stack
structure in which elements are added and
Answer: d
removed based on the LIFO principle. Open
Explanation: In serial access memory data
parenthesis is pushed into the stack and a
records are stored one after the other in
closed parenthesis pops out elements till
which they are created and are accessed
the top element of the stack is its
sequentially. In stack data structure,
corresponding open parenthesis. If the
elements are accessed sequentially. Stack
stack is empty, parenthesis is balanced
data structure resembles the serial access
otherwise it is unbalanced.
memory.
30. What data structure would you mostly
32. The postfix form of A*B+C/D is?
likely see in a non recursive implementation
a) *AB/CD+
of a recursive algorithm?
b) AB*CD/+
a) Linked List
c) A*BC+/D
b) Stack
d) ABCD+/*
c) Queue
d) Tree Answer: b
Explanation: Infix expression is (A*B)+(C/D)
Answer: b
AB*+(C/D)
Explanation: In recursive algorithms, the
AB*CD/+. Thus postfix expression is
order in which the recursive process comes
AB*CD/+.
back is the reverse of the order in which it
goes forward during execution. The 33. Which data structure is needed to
compiler uses the stack data structure to convert infix notation to postfix notation?
implement recursion. In the forwarding a) Branch
phase, the values of local variables, b) Tree
parameters and the return address are c) Queue
pushed into the stack at each recursion d) Stack
level. In the backing-out phase, the stacked
address is popped and used to execute the Answer: d
rest of the code. Explanation: The Stack data structure is
used to convert infix expression to postfix
31. The process of accessing data stored in expression. The purpose of stack is to
a serial access memory is similar to reverse the order of the operators in the
manipulating data on a ________ expression. It also serves as a storage
a) Heap structure, as no operator can be printed
b) Binary Tree until both of its operands have appeared.
8
Data Structures and Algorithms Unit – 7 MCQs
34. The prefix form of A-B/ (C * D ^ E) is? 37. Which data structure is used for
a) -/*^ACBDE implementing recursion?
b) -ABCD*^DE a) Queue
c) -A/B*C^DE b) Stack
d) -A/BC*^DE c) Array
d) List
Answer: c
Explanation: Infix Expression is (A- Answer: b
B)/(C*D^E) Explanation: Stacks are used for the
(-A/B)(C*D^E) implementation of Recursion.
-A/B*C^DE. Thus prefix expression is -
38. The result of evaluating the postfix
A/B*C^DE
expression 5, 4, 6, +, *, 4, 9, 3, /, +, * is?
35. What is the result of the following a) 600
operation? b) 350
Top (Push (S, X)) c) 650
a) X d) 588
b) X+S
Answer: b
c) S
Explanation: The postfix expression is
d) XS
evaluated using stack. We will get the infix
Answer: a expression as
Explanation: The function Push(S,X) pushes (5*(4+6))*(4+9/3). On solving the Infix
the value X in the stack S. Top() function Expression, we get
gives the value which entered last. X (5*(10))*(4+3)
entered into stack S at last. = 50*7
= 350.
36. The prefix form of an infix expression (p
+ q) – (r * t) is? 39. Convert the following infix expressions
a) + pq – *rt into its equivalent postfix expressions
b) – +pqr * t (A + B ⋀D)/(E – F)+G
c) – +pq * rt a) (A B D ⋀ + E F – / G +)
d) – + * pqrt b) (A B D +⋀ E F – / G +)
c) (A B D ⋀ + E F/- G +)
Answer: c
d) (A B D E F + ⋀ / – G +)
Explanation: Given Infix Expression is ((p+q)-
(r*t)) Answer: a
(+pq)-(r*t) Explanation: The given infix expression is (A
(-+pq)(r*t) + B ⋀D)/(E – F)+G.
-+pq*rt. Thus prefix expression is -+pq*rt. (A B D ^ + ) / (E – F) +G
9
Data Structures and Algorithms Unit – 7 MCQs
10
Data Structures and Algorithms Unit – 7 MCQs
11
Data Structures and Algorithms Unit – 7 MCQs
Answer: a Answer: c
Explanation: Element first added in queue Explanation: In dequeuer, we can insert or
will be deleted first which is FIFO principle. delete elements from both the ends. In
queue, we will follow first in first out
50. Circular Queue is also known as
principle for insertion and deletion of
________
elements. Element with least priority will be
a) Ring Buffer
deleted in a priority queue.
b) Square Buffer
c) Rectangle Buffer 53. A normal queue, if implemented using
d) Curve Buffer an array of size MAX_SIZE, gets full when
a) Rear = MAX_SIZE – 1
Answer: a
b) Front = (rear + 1)mod MAX_SIZE
Explanation: Circular Queue is also called as
c) Front = rear + 1
Ring Buffer. Circular Queue is a linear data
d) Rear = front
structure in which last position is connected
back to the first position to make a circle. It Answer: a
forms a ring structure. Explanation: When Rear = MAX_SIZE – 1,
there will be no space left for the elements
51. If the elements “A”, “B”, “C” and “D” are
to be added in queue. Thus queue becomes
placed in a queue and are deleted one at a
full.
time, in what order will they be removed?
a) ABCD 54. Queues serve major role in
b) DCBA ______________
c) DCAB a) Simulation of recursion
d) ABDC b) Simulation of arbitrary linked list
c) Simulation of limited resource allocation
Answer: a
d) Simulation of heap sort
Explanation: Queue follows FIFO approach.
i.e. First in First Out Approach. So, the order Answer: c
of removal elements are ABCD. Explanation: Simulation of recursion uses
stack data structure. Simulation of arbitrary
52. A data structure in which elements can
linked lists uses linked lists. Simulation of
be inserted or deleted at/from both the
resource allocation uses queue as first
ends but not in the middle is?
entered data needs to be given first priority
a) Queue
during resource allocation. Simulation of
b) Circular queue
heap sort uses heap data structure.
c) Dequeue
d) Priority queue 55. Which of the following is not the type of
queue?
a) Ordinary queue
12
Data Structures and Algorithms Unit – 7 MCQs
13
Data Structures and Algorithms Unit – 7 MCQs
14
Data Structures and Algorithms Unit – 7 MCQs
15
Data Structures and Algorithms Unit – 7 MCQs
Answer: c Answer: b
Explanation: Linked lists saves both space Explanation: fun1() prints the given Linked
and time. List in reverse manner.
For Linked List 1->2->3->4->5, fun1() prints
70. Which of the following points is/are not 5->4->3->2->1.
true about Linked List data structure when 72. Which of the following sorting
it is compared with array? algorithms can be used to sort a random
a) Arrays have better cache locality that can linked list with minimum time complexity?
make them better in terms of performance a) Insertion Sort
b) It is easy to insert and delete elements in b) Quick Sort
Linked List c) Heap Sort
c) Random access is not allowed in a typical d) Merge Sort
implementation of Linked Lists
d) Access of elements in linked list takes Answer: d
less time than compared to arrays Explanation: Both Merge sort and Insertion
sort can be used for linked lists. The slow
Answer: d random-access performance of a linked list
Explanation: To access an element in a makes other algorithms (such as quicksort)
linked list, we need to traverse every perform poorly, and others (such as
element until we reach the desired heapsort) completely impossible. Since
element. This will take more time than worst case time complexity of Merge Sort is
arrays as arrays provide random access to O(nLogn) and Insertion sort is O(n2), merge
its elements. sort is preferred.
71. What does the following function do for 73. What is the output of following function
a given Linked List with first node as head? for start pointing to first node of following
linked list?
void fun1(struct node* head)
{ 1->2->3->4->5->6
if(head == NULL) void fun(struct node* start)
return; {
fun1(head->next); if(start == NULL)
printf("%d ", head->data); return;
} printf("%d ", start->data);
a) Prints all nodes of linked lists if(start->next != NULL )
b) Prints all nodes of linked list in reverse fun(start->next->next);
order printf("%d ", start->data);
c) Prints alternate nodes of Linked List }
d) Prints alternate nodes in reverse order
a) 1 4 6 6 4 1
b) 1 3 5 1 3 5
16
Data Structures and Algorithms Unit – 7 MCQs
17
Data Structures and Algorithms Unit – 7 MCQs
18
Data Structures and Algorithms Unit – 7 MCQs
absent in the list. For this, we need to 82. What is the time complexity to count
compare every element in the linked list. If the number of elements in the linked list?
n elements are there, n comparisons will a) O(1)
b) O(n)
happen in the worst case.
c) O(logn)
80. Which of the following is not a d) O(n2)
disadvantage to the usage of array?
a) Fixed size Answer: b
b) There are chances of wastage of memory Explanation: To count the number of
space if elements inserted in an array are elements, you have to traverse through the
lesser than the allocated size entire list, hence complexity is O(n).
c) Insertion based on position
d) Accessing elements at specified positions 83. What is the functionality of the
following code?
Answer: d public void function(Node node)
Explanation: Array elements can be {
accessed in two steps. First, multiply the if(size == 0)
size of the data type with the specified head = node;
position, second, add this value to the base else
address. Both of these operations can be {
done in constant time, hence accessing Node temp,cur;
elements at a given index/position is faster. for(cur = head; (temp =
81. What is the time complexity of inserting cur.getNext())!=null; cur = temp);
at the end in dynamic arrays? cur.setNext(node);
a) O(1) }
b) O(n) size++;
c) O(logn) }
d) Either O(1) or O(n) a) Inserting a node at the beginning of the
list
Answer: d b) Deleting a node at the beginning of the
Explanation: Depending on whether the list
array is full or not, the complexity in c) Inserting a node at the end of the list
dynamic array varies. If you try to insert d) Deleting a node at the end of the list
into an array which is not full, then the
element is simply stored at the end, this Answer: c
takes O(1) time. If you try to insert into an Explanation: The for loop traverses through
array which is full, first you will have to the list and then inserts a new node as
allocate an array with double the size of the cur.setNext(node);
current array and then copy all the
elements into it and finally insert the new 84. What is the space complexity for
element, this takes O(n) time. deleting a linked list?
a) O(1)
b) O(n)
19
Data Structures and Algorithms Unit – 7 MCQs
public int length(Node head) 87. How do you insert an element at the
{
20
Data Structures and Algorithms Unit – 7 MCQs
21
Data Structures and Algorithms Unit – 7 MCQs
22
Data Structures and Algorithms Unit – 7 MCQs
a) head-0-1-2-3-4-5-6-tail head.setNext(temp);
b) head-1-2-3-4-5-6-tail temp.getNext().setPrev(temp);
c) head-6-1-2-3-4-5-0-tail Node temp1 = tail.getPrev();
d) head-0-1-2-3-4-5-tail tail.setPrev(temp1.getPrev());
temp1.getPrev().setNext(tail);
Answer: c
a) head-6-1-2-3-4-5-tail
Explanation: The given sequence of
b) head-6-1-2-3-4-tail
operations perform addition of nodes at the
c) head-1-2-3-4-5-6-tail
head and tail of the list.
d) head-1-2-3-4-5-tail
94. What is the functionality of the
following piece of code? Answer: b
Explanation: A new node is added to the
public int function() head of the list and a node is deleted from
{ the tail end of the list.
Node temp = tail.getPrev(); 96. What is the functionality of the
tail.setPrev(temp.getPrev()); following code? Choose the most
temp.getPrev().setNext(tail); appropriate answer.
size--;
return temp.getItem(); public int function()
} {
a) Return the element at the tail of the list if(head == null)
but do not remove it return
b) Return the element at the tail of the list Integer.MIN_VALUE;
and remove it from the list int var;
c) Return the last but one element from the Node temp = head;
list but do not remove it while(temp.getNext() != head)
d) Return the last but one element at the temp = temp.getNext();
tail of the list and remove it from the list if(temp == head)
{
Answer: b var = head.getItem();
Explanation: The previous and next pointers head = null;
of the tail and the last but one element are return var;
manipulated, this suggests that the last }
node is being removed from the list. temp.setNext(head.getNext());
var = head.getItem();
95. Consider the following doubly linked head = head.getNext();
list: head-1-2-3-4-5-tail return var;
What will be the list after performing the }
given sequence of operations?
a) Return data from the end of the list
b) Returns the data and deletes the node at
Node temp = new
the end of the list
Node(6,head,head.getNext());
c) Returns the data from the beginning of
23
Data Structures and Algorithms Unit – 7 MCQs
Answer: d Answer: b
Explanation: First traverse through the list Explanation: First traverse through the list
to find the end node, then manipulate the to find the end node, also have a trailing
‘next’ pointer such that it points to the pointer to find the penultimate node, make
current head’s next node, return the data this trailing pointer’s ‘next’ point to the
stored in head and make this next node as head and return the data stored in the
the head. ‘temp’ node.
97. What is the functionality of the 98. Which of the following is false about a
following code? Choose the most circular linked list?
appropriate answer. a) Every node has a successor
b) Time complexity of inserting a new node
public int function() at the head of the list is O(1)
{ c) Time complexity for deleting the last
if(head == null) node is O(n)
return d) We can traverse the whole circular linked
Integer.MIN_VALUE; list by starting from any point
int var;
Node temp = head; Answer: b
Node cur; Explanation: Time complexity of inserting a
while(temp.getNext() != head) new node at the head of the list is O(n)
{ because you have to traverse through the
cur = temp; list to find the tail node.
temp = temp.getNext();
99. Consider a small circular linked list. How
}
to detect the presence of cycles in this list
if(temp == head)
effectively?
{
a) Keep one node as head and traverse
var = head.getItem();
another temp node till the end to check if
head = null;
its ‘next points to head
return var;
b) Have fast and slow pointers with the fast
}
pointer advancing two nodes at a time and
var = temp.getItem();
slow pointer advancing by one node at a
cur.setNext(head);
time
return var;
c) Cannot determine, you have to pre-
}
define if the list contains cycles
a) Return data from the end of the list d) Circular linked list itself represents a
b) Returns the data and deletes the node at cycle. So no new cycles cannot be
the end of the list generated
c) Returns the data from the beginning of
24
Data Structures and Algorithms Unit – 7 MCQs
a) Print success if a particular element is not 103. What differentiates a circular linked list
found from a normal linked list?
b) Print fail if a particular element is not a) You cannot have the ‘next’ pointer point
found to null in a circular linked list
c) Print success if a particular element is b) It is faster to traverse the circular linked
equal to 1
list
c) You may or may not have the ‘next’
25
Data Structures and Algorithms Unit – 7 MCQs
26
Data Structures and Algorithms Unit – 7 MCQs
{
if(size_of_stack==size) System.out.println(element2);
{ }
}
System.out.println("Stack
a) stack is full
overflow");
b) 20
c) 0
return;
d) -999
}
else
Answer: d
{
Explanation: The first call to pop() returns
top++;
10, whereas the second call to pop() would
stk[top]=item;
result in stack underflow and the program
}
returns -999.
}
public Object pop() 108. What is the time complexity of pop()
{ operation when the stack is implemented
if(top<0) using an array?
{ a) O(1)
return -999; b) O(n)
} c) O(logn)
else d) O(nlogn)
{
Object Answer: a
ele=stk[top]; Explanation: pop() accesses only one end of
top--; the structure, and hence constant time.
size_of_stack--;
return ele; 109. Which of the following array position
} will be occupied by a new element being
} pushed for a stack of size N
} elements(capacity of stack > N).
a) S[N-1]
public class StackDemo b) S[N]
{ c) S[1]
public static void main(String d) S[0]
args[])
{ Answer: b
Stack myStack = new Explanation: Elements are pushed at the
Stack(); end, hence N.
myStack.push(10);
Object element1 = 110. What happens when you pop from an
myStack.pop(); empty stack while implementing using the
Object element2 = Stack ADT in Java?
myStack.pop(); a) Undefined error
27
Data Structures and Algorithms Unit – 7 MCQs
b) Compiler displays a warning the elements are popped out into the array
c) EmptyStackException is thrown ‘b’. Stack is a LIFO structure, this results in
d) NoStackException is thrown reversing the given array.
115. Which of the following data structures 118. Which of the following statements are
can be used for parentheses matching? not correct with respect to Singly Linked
a) n-ary tree List(SLL) and Doubly Linked List(DLL)?
b) queue
a) Complexity of Insertion and Deletion at
c) priority queue
d) stack known position is O(n) in SLL and O(1) in
DLL
Answer: d b) SLL uses lesser memory per node than
Explanation: For every opening brace, push DLL
it into the stack, and for every closing brace, c) DLL has more searching power than SLL
pop it off the stack. Do not take action for
d) Number of node fields in SLL is more
any other character. In the end, if the stack
than DLL
is empty, then the input has balanced
parentheses. Answer: d
116. Minimum number of queues to Explanation: To insert and delete at known
implement stack is ___________ positions requires complete traversal of the
a) 3 list in worst case in SLL, SLL consists of an
b) 4 item and a node field, while DLL has an item
c) 1
and two node fields, hence SLL occupies
d) 2
lesser memory, DLL can be traversed both
Answer: c ways(left and right), while SLL can traverse
Explanation: Use one queue and one in only one direction, hence more searching
counter to count the number of elements in power of DLL. Node fields in SLL is 2 (data
the queue. and address of next node) whereas in DLL is
29
Data Structures and Algorithms Unit – 7 MCQs
119. What is the functionality of the 121. What is the space complexity of a
following piece of code? linear queue having n elements?
a) O(n)
public void display() b) O(nlogn)
{ c) O(logn)
if(size == 0) d) O(1)
System.out.println("underflow"); Answer: a
else Explanation: Because there are n elements.
{
Node current = first; 122. Which of the following properties is
while(current != null) associated with a queue?
{ a) First In Last Out
b) First In First Out
System.out.println(current.getEle( c) Last In First Out
)); d) Last In Last Out
current =
current.getNext();
30
Data Structures and Algorithms Unit – 7 MCQs
Answer: b else
Explanation: Queue follows First In First Out {
structure. Object high;
high = q[front];
123. In a circular queue, how do you return high;
increment the rear end of the queue? }
a) rear++ }
b) (rear+1) % CAPACITY
c) (rear % CAPACITY)+1 a) Dequeue
d) rear– b) Enqueue
c) Return the front element
Answer: b d) Return the last element
Explanation: Ensures rear takes the values
from 0 to (CAPACITY-1). Answer: c
Explanation: q[front] gives the element at
124. What is the term for inserting into a the front of the queue, since we are not
full queue known as? moving the ‘front’ to the next element,
a) overflow it is not a dequeue operation.
b) underflow
c) null pointer exception 127. What is the need for a circular queue?
d) program won’t be compiled a) effective usage of memory
b) easier computations
Answer: a c) to delete elements based on priority
Explanation: Just as stack, inserting into a d) implement LIFO principle in queues
full queue is termed overflow.
Answer: a
125. What is the time complexity of Explanation: In a linear queue, dequeue
enqueue operation? operation causes the starting elements of
a) O(logn) the array to be empty, and there is no way
b) O(nlogn) you can use that space, while in a circular
c) O(n) queue, you can effectively use that space.
d) O(1) Priority queue is used to delete the
elements based on their priority. Higher
Answer: d priority elements will be deleted first
Explanation: Enqueue operation is at the whereas lower priority elements will be
rear end, it takes O(1) time to insert a new deleted next. Queue data structure always
item into the queue. follows FIFO principle.
126. What does the following piece of code 128. In linked list implementation of queue,
do? if only front pointer is maintained, which of
the following operation take worst case
public Object function()
linear time?
{
a) Insertion
if(isEmpty())
return -999; b) Deletion
31
Data Structures and Algorithms Unit – 7 MCQs
Answer: d Answer: c
Explanation: Since front pointer is used for Explanation: Since its the starting of queue,
deletion, so worst time for the other two so both values are changed.
cases.
132. In case of insertion into a linked queue,
129. In linked list implementation of a a node borrowed from the __________ list
queue, where does a new element be is inserted in the queue.
inserted? a) AVAIL
a) At the head of link list b) FRONT
b) At the centre position in the link list c) REAR
c) At the tail of the link list d) NULL
d) At any position in the linked list
Answer: a
Answer: c Explanation: All the nodes are collected in
Explanation: Since queue follows FIFO so AVAIL list.
new element inserted at last.
133. In linked list implementation of a
130. In linked list implementation of a queue, from where is the item deleted?
queue, front and rear pointers are tracked. a) At the head of link list
Which of these pointers will change during b) At the centre position in the link list
an insertion into a NONEMPTY queue? c) At the tail of the link list
a) Only front pointer d) Node before the tail
b) Only rear pointer
Answer: a
c) Both front and rear pointer
Explanation: Since queue follows FIFO so
d) No pointer will be changed
new element deleted from first.
Answer: b
134. In linked list implementation of a
Explanation: Since queue follows FIFO so
queue, the important condition for a queue
new element inserted at last.
to be empty is?
131. In linked list implementation of a a) FRONT is null
queue, front and rear pointers are tracked. b) REAR is null
Which of these pointers will change during c) LINK is empty
an insertion into EMPTY queue? d) FRONT==REAR-1
a) Only front pointer
Answer: a
b) Only rear pointer
Explanation: Because front represents the
deleted nodes.
32
Data Structures and Algorithms Unit – 7 MCQs
33
Data Structures and Algorithms Unit – 7 MCQs
34
Data Structures and Algorithms Unit – 7 MCQs
35
Data Structures and Algorithms Unit – 7 MCQs
DeleteRear(); Answer: b
InsertRear(15); Explanation: The addFront and
display(); removeFront operations can be performed
a) 10 30 10 15 using one stack itself as push and pop are
b) 20 30 40 15 supported (adding and removing element
c) 20 30 40 10 from top of the stack) but to perform
d) 10 30 40 15 addRear and removeRear you need to pop
each element from the current stack and
Answer: d
Explanation: A careful tracing of the given push it into another stack, push or pop the
operation yields the result. element as per the asked operation from
10 this stack and in the end pop elements from
20 10 this stack to the first stack.
20 10 30
10 30 151. You are asked to perform a queue
10 30 40 operation using a stack. Assume the size of
10 30 40 10 the stack is some value ‘n’ and there are ‘m’
10 30 40 number of variables in this stack. The time
10 30 40 15
complexity of performing deQueue
150. A Double-ended queue supports operation is (Using only stack operations
operations such as adding and removing like push and pop)(Tightly bound).
items from both the sides of the queue. a) O(m)
They support four operations like b) O(n)
addFront(adding item to top of the queue), c) O(m*n)
addRear(adding item to the bottom of the d) Data is insufficient
queue), removeFront(removing item from
Answer: a
the top of the queue) and
Explanation: To perform deQueue
removeRear(removing item from the
operation you need to pop each element
bottom of the queue). You are given only
from the first stack and push it into the
stacks to implement this data structure. You
second stack. In this case you need to pop
can implement only push and pop
‘m’ times and need to perform push
operations. What are the total number of
operations also ‘m’ times. Then you pop the
stacks required for this operation?(you can
first element from this second stack
reuse the stack)
(constant time) and pass all the elements to
a) 1
the first stack (as done in the beginning)(‘m-
b) 2
1’ times). Therfore the time complexity is
c) 3
O(m).
d) 4
36
Data Structures and Algorithms Unit – 7 MCQs
37
Data Structures and Algorithms Unit – 7 MCQs
elements in the stack to be n)? placed one above the other. You want to
a) Because of its time complexity O(n) remove the last ring in the jar. And the
b) Because of its time complexity O(log(n)) second jar is weak and cannot be used to
c) Extra memory is not required store rings for a long time.
d) There are no problem a) Empty the first jar by removing it one by
one from the first jar and placing it into the
Answer: a
second jar
Explanation: To perform Queue operations
b) Empty the first jar by removing it one by
such as enQueue and deQueue there is a
one from the first jar and placing it into the
need of emptying all the elements of a
second jar and empty the second jar by
current stack and pushing elements into the
placing all the rings into the first jar one by
next stack and vice versa. Therfore it has a
one
time complexity of O(n) and the need of
c) There exists no possible way to do this
extra stack as well, may not be feasible for a
d) Break the jar and remove the last on
large dataset.
Answer: b
156. Consider yourself to be in a planet
Explanation: This is similar to performing
where the computational power of chips to
dequeue operation using push and pop
be slow. You have an array of size 10.You
only. Elements in the first jar are taken out
want to perform enqueue some element
and placed in the second jar. After
into this array. But you can perform only
removing the last element from the first jar,
push and pop operations .Push and pop
remove all the elements in the second jar
operation both take 1 sec respectively. The
and place them in the first jar.
total time required to perform enQueue
operation is? 158. Given only a single array of size 10 and
a) 20 no other memory is available. Which of the
b) 40 following operation is not feasible to
c) 42 implement (Given only push and pop
d) 43 operation)?
a) Push
Answer: d
b) Pop
Explanation: First you have to empty all the
c) Enqueue
elements of the current stack into the
d) Returntop
temporary stack, push the required element
and empty the elements of the temporary Answer: c
stack into the original stack. Therfore taking Explanation: To perform Enqueue using just
10+10+1+11+11= 43 seconds. push and pop operations, there is a need of
another array of same size. But as there is
157. You have two jars, one jar which has
10 rings and the other has none. They are
38
Data Structures and Algorithms Unit – 7 MCQs
39
Data Structures and Algorithms Unit – 7 MCQs
40
Data Structures and Algorithms Unit – 7 MCQs
register set for long periods of time which is the maximum size without
d) Accessing Individual Elements is easy relocation of data.
41
Data Structures and Algorithms Unit – 7 MCQs
42
Data Structures and Algorithms Unit – 7 MCQs
43
Data Structures and Algorithms Unit – 7 MCQs
44
Data Structures and Algorithms Unit – 7 MCQs
193. What does the following piece of code c) Sparsity = Density*Total number of
do? elements
d) Sparsity = Density/Total number of
for(int i = 0; i < row; i++) elements
{
for(int j = 0; j < column; j++) Answer: a
{ Explanation: Sparsity of a matrix is equal to
if(i == j) 1 minus Density of the matrix. The Sparsity
sum = sum + (array[i][j]);
of matrix is defined as the total number of
}
} Zero Valued elements divided total number
System.out.println(sum); of elements.
45
Data Structures and Algorithms Unit – 7 MCQs
46
Data Structures and Algorithms Unit – 7 MCQs
array indicates how close or far the array is array are maximum when the given array is
from being completely sorted. The array is reverse sorted. As the necessary condition
sorted if the number of inversions are 0. for an inversion is arr[i]>arr[j] and i<j.
204. How many inversions does a sorted 207. What will be the resulting array after
array have? rotating arr[]={1, 2, 3, 4, 5} by 2?
a) 0 a) 2, 1, 3, 4, 5
b) 1 b) 3, 4, 5, 1, 2
c) 2 c) 4, 5, 1, 2, 3
d) cannot be determined d) 1, 2, 3, 5, 4
Answer: a Answer: b
Explanation: When an array is sorted then Explanation: When the given array is
there cannot be any inversion in the array. rotated by 2 then the resulting array will be
As the necessary condition for an inversion Rotation 1: {2,3,4,5,1}
is arr[i]>arr[j] and i<j. Rotation 2: {3,4,5,1,2}.
Thus, the final array is {3,4,5,1,2}.
205. What is the condition for two elements
arr[i] and arr[j] to form an inversion? 208. What will be the minimum number of
a) arr[i]<arr[j] jumps required to reach the end of the
b) i < j array arr[] = {1,3,6,3,6,8,5}?
c) arr[i] < arr[j] and i < j a) 1
d) arr[i] > arr[j] and i < j b) 2
c) 3
Answer: d
d) not possible to reach the end
Explanation: For two elements to form an
inversion the necessary condition is arr[i] > Answer: c
arr[j] and i < j. The number of inversions in Explanation: Each element of the array
an array indicate how close or far the array represents the maximum number of steps
is from being completely sorted. that can be taken forward from that
element. If the first element is 0 then it is
206. Under what condition the number of
not possible to reach the end.
inversions in an array are maximum?
a) when the array is sorted 209. What will be the minimum number of
b) when the array is reverse sorted jumps required to reach the end of the
c) when the array is half sorted array arr[] ={0,1,3,6,3,6,8,5}?
d) depends on the given array a) 1
b) 2
Answer: b
c) 3
Explanation: Number of inversions in an
d) not possible to reach the end
47
Data Structures and Algorithms Unit – 7 MCQs
48
Data Structures and Algorithms Unit – 7 MCQs
d) No linked list representation of tree is 216. Can a tree stored in an array using
only fine either one of inorder or post order or pre
order traversals be again reformed?
Answer: c a) Yes just traverse through the array and
Explanation: In case of sparse trees (where form the tree
one node per level in worst cases), the array b) No we need one more traversal to form a
size (2h)-1 where h is height but only h tree
indexes will be filled and (2h)-1-h nodes will c) No in case of sparse trees
be left unused leading to space wastage. d) Yes by using both inorder and array
elements
215. Why is heap implemented using array
representations than tree(linked list) Answer: b
representations though both tree Explanation: We need any two traversals
representations and heaps have same for tree formation but if some additional
complexities? stuff or techniques are used while storing a
tree in an array then one traversal can
for binary heap facilitate like also storing null values of a
-insert: O(log n) node in array.
-delete min: O(log n)
217. How many children does a binary tree
for a tree have?
-insert: O(log n) a) 2
-delete: O(log n) b) any number of children
Then why go with array representation c) 0 or 1 or 2
when both are having same values ? d) 0 or 1
a) arrays can store trees which are
complete and heaps are not complete Answer: c
b) lists representation takes more memory Explanation: Can have atmost 2 nodes.
hence memory efficiency is less and go with
arrays and arrays have better caching 218. What is/are the disadvantages of
c) lists have better caching implementing tree using normal arrays?
d) In lists insertion and deletion is difficult a) difficulty in knowing children nodes of a
node
Answer: b b) difficult in finding the parent of a node
Explanation: In memory the pointer address
c) have to know the maximum number of
for next node may not be adjacent or
nearer to each other and also array have nodes possible before creation of trees
wonderful caching power from os and d) difficult to implement
manipulating pointers is a overhead. Heap
data structure is always a complete binary Answer: c
tree. Explanation: The size of array is fixed in
normal arrays. We need to know the
number of nodes in the tree before array
49
Data Structures and Algorithms Unit – 7 MCQs
50
Data Structures and Algorithms Unit – 7 MCQs
Answer: c
Explanation: We are checking if left or right
node is what the argument sent or else if
not the case then move to left node or right
if(rootnode):
node and print all nodes while searching for
mirror(rootnode-->left)
the argument node.
mirror(rootnode-->right)
52
Data Structures and Algorithms Unit – 7 MCQs
233. What is the maximum number of 236. How many common operations are
children that a binary tree node can have? performed in a binary tree?
a) 0 a) 1
b) 1 b) 2
c) 2 c) 3
d) 3 d) 4
Answer: c Answer: c
Explanation: In a binary tree, a node can Explanation: Three common operations are
have atmost 2 nodes (i.e.) 0,1 or 2 left and performed in a binary tree- they are
right child. insertion, deletion and traversal.
234. The following given tree is an example 237. What is the traversal strategy used in
for? the binary tree?
a) depth-first traversal
b) breadth-first traversal
c) random traversal
d) Priority traversal
Answer: b
a) Binary tree Explanation: Breadth first traversal, also
b) Binary search tree known as level order traversal is the
c) Fibonacci tree traversal strategy used in a binary tree. It
d) AVL tree involves visiting all the nodes at a given
level.
Answer: a
Explanation: The given tree is an example 238. How many types of insertion are
for binary tree since has got two children performed in a binary tree?
and the left and right children do not satisfy a) 1
binary search tree’s property, Fibonacci and b) 2
AVL tree. c) 3
d) 4
235. A binary tree is a rooted tree but not
an ordered tree. Answer: b
a) true Explanation: Two kinds of insertion
b) false operation is performed in a binary tree-
inserting a leaf node and inserting an
Answer: b
internal node.
Explanation: A binary tree is a rooted tree
and also an ordered tree (i.e) every node in
a binary tree has at most two children.
53
Data Structures and Algorithms Unit – 7 MCQs
239. What operation does the following close to minimum possible space
diagram depict? established by lower bounds. A succinct
binary tree would occupy 2n+O(n) bits.
54
Data Structures and Algorithms Unit – 7 MCQs
Answer: b
Explanation: If a binary tree is represented
in an array, parent nodes are found at a)
indices (i-1)/2.
Answer: a
Explanation: In preorder, inorder and
postorder traversal the left subtrees are
visited before the right subtrees. In Inorder
traversal, the Left subtree is visited first
then the Root node then the Right subtree.
In postorder traversal, the Left subtree is
visited first, then Right subtree and then the
c)
Root node is visited.
d)
55
Data Structures and Algorithms Unit – 7 MCQs
Answer: d a) 2, 7, 2, 6, 5, 11, 5, 9, 4
Explanation: Here, b) 2, 7, 5, 2, 6, 9, 5, 11, 4
Preorder Traversal is 1, 2, 5, 3, 4 c) 2, 5, 11, 6, 7, 4, 9, 5, 2
Inorder Traversal is 2, 5, 1, 4, 3 d) 2, 7, 5, 6, 11, 2, 5, 4, 9
Root node of binary tree is the first node in
Answer: a
Preorder traversal.
Explanation: Pre order traversal follows
The rough sketch of tree is:
NLR(Node-Left-Right).
Answer: c
Explanation: Post order traversal follows
LRN(Left-Right-Node).
Answer: b
Explanation: Since you have to go through
all the nodes, the complexity becomes O(n).
56
Data Structures and Algorithms Unit – 7 MCQs
Answer: d
Explanation: In the worst case we have d
stack frames in the recursive call, hence the
The levelorder traversal (BFS traversal) is A,
complexity is O(d).
B, C, E, D.
251. To obtain a prefix expression, which of
253. Consider the following data and specify
the tree traversals is used?
which one is Preorder Traversal Sequence,
a) Level-order traversal
Inorder and Postorder sequences.
b) Pre-order traversal
S1: N, M, P, O, Q
c) Post-order traversal
S2: N, P, Q, O, M
d) In-order traversal
S3: M, N, O, P, Q
Answer: b a) S1 is preorder, S2 is inorder and S3 is
Explanation: As the name itself suggests, postorder
pre-order traversal can be used. b) S1 is inorder, S2 is preorder and S3 is
postorder
252. Consider the following data. The pre c) S1 is inorder, S2 is postorder and S3 is
order traversal of a binary tree is A, B, E, C, preorder
D. The in order traversal of the same binary d) S1 is postorder, S2 is inorder and S3 is
tree is B, E, A, D, C. The level order preorder
sequence for the binary tree is _________
a) A, C, D, B, E Answer: c
b) A, B, C, D, E Explanation: Preorder traversal starts from
c) A, B, C, E, D the root node and postorder and inorder
d) D, B, E, A, C starts from the left child node of the left
subtree. The first node of S3 is different and
Answer: b for S1 and S2 it’s the same. Thus, S3 is
Explanation: The inorder sequence is B, E, preorder traversal and the root node is M.
A, D, C and Preorder sequence is A, B, E, C, Postorder traversal visits the root node at
D. The tree constructed with the inorder last. S2 has the root node(M) at last that
and preorder sequence is implies S2 is postorder traversal. S1 is
inorder traversal as S2 is postorder traversal
and S3 is preorder traversal. Therefore, S1
57
Data Structures and Algorithms Unit – 7 MCQs
is inorder traversal, S2 is postorder traversal D. The in order traversal of the same binary
and S3 is preorder traversal. tree is B, E, A, D, C. The level order
sequence for the binary tree is _________
254. What is the time complexity of pre-
a) A, C, D, B, E
order traversal in the iterative fashion?
b) A, B, C, D, E
a) O(1)
c) A, B, C, E, D
b) O(n)
d) D, B, E, A, C
c) O(logn)
d) O(nlogn) Answer: b
Explanation: The inorder sequence is B, E,
Answer: b
A, D, C and Preorder sequence is A, B, E, C,
Explanation: Since you have to go through
D. The tree constructed with the inorder
all the nodes, the complexity becomes O(n).
and preorder sequence is
255. What is the space complexity of the
post-order traversal in the recursive
fashion? (d is the tree depth and n is the
number of nodes)
a) O(1)
b) O(nlogd)
c) O(logd)
d) O(d)
58
Data Structures and Algorithms Unit – 7 MCQs
Answer: c
Explanation: Preorder traversal starts from
the root node and postorder and inorder
starts from the left child node of the left
subtree. The first node of S3 is different and
for S1 and S2 it’s the same. Thus, S3 is
preorder traversal and the root node is M.
Postorder traversal visits the root node at
last. S2 has the root node(M) at last that
implies S2 is postorder traversal. S1 is
inorder traversal as S2 is postorder traversal
a) 2, 7, 2, 6, 5, 11, 5, 9, 4
and S3 is preorder traversal. Therefore, S1
b) 2, 7, 5, 2, 11, 9, 6, 5, 4
is inorder traversal, S2 is postorder traversal
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
and S3 is preorder traversal.
d) 2, 7, 5, 6, 11, 2, 5, 4, 9
259. For the tree below, write the in-order
Answer: b
traversal.
Explanation: Level order traversal follows a
breadth first search approach.
Answer: b
Explanation: The number of edges from the
a) 6, 2, 5, 7, 11, 2, 5, 9, 4 root to the node is called depth of the tree.
b) 6, 5, 2, 11, 7, 4, 9, 5, 2
c) 2, 7, 2, 6, 5, 11, 5, 9, 4 262. The number of edges from the node to
d) 2, 7, 6, 5, 11, 2, 9, 5, 4 the deepest leaf is called _________ of the
tree.
Answer: a a) Height
Explanation: In-order traversal follows b) Depth
LNR(Left-Node-Right). c) Length
d) Width
260. For the tree below, write the level-
order traversal. Answer: a
Explanation: The number of edges from the
59
Data Structures and Algorithms Unit – 7 MCQs
60
Data Structures and Algorithms Unit – 7 MCQs
Answer: a
Explanation: It is interesting to note that
61
Data Structures and Algorithms Unit – 7 MCQs
62
Data Structures and Algorithms Unit – 7 MCQs
63
Data Structures and Algorithms Unit – 7 MCQs
64
Data Structures and Algorithms Unit – 7 MCQs
c) at least 2n keys 290. B-tree and AVL tree have the same
d) at least (n – 1)/2 keys worst case time complexity for insertion
and deletion.
Answer: d
a) True
Explanation: A non-root node in a B-tree of
b) False
order n contains at least (n – 1)/2 keys. And
contains a maximum of (n – 1) keys and n Answer: a
sons. Explanation: Both the B-tree and the AVL
tree have O(log n) as worst case time
288. A B-tree of order 4 and of height 3 will
complexity for insertion and deletion.
have a maximum of _______ keys.
a) 255 291. 2-3-4 trees are B-trees of order 4. They
b) 63 are an isometric of _____ trees.
c) 127 a) AVL
d) 188 b) AA
c) 2-3
Answer: a
d) Red-Black
Explanation: A B-tree of order m of height h
will have the maximum number of keys Answer: d
when all nodes are completely filled. So, the Explanation: 2-3-4 trees are isometric of
B-tree will have n = (mh+1 – 1) keys in this Red-Black trees. It means that, for every 2-
situation. So, required number of maximum 3-4 tree, there exists a Red-Black tree with
keys = 43+1 – 1 = 256 – 1 = 255. data elements in the same order.
289. Five node splitting operations occurred 292. Figure shown below is B-tree of order
when an entry is inserted into a B-tree. 5. What is the result of deleting 130 from
Then how many nodes are written? the tree?
a) 14
b) 7
c) 11
d) 5
Answer: c
Explanation: If s splits occur in a B-tree, 2s +
1 nodes are written (2 halves of each split a)
and the parent of the last node split). So, if
5 splits occurred, then 2 * 5 + 1 , i.e. 11
nodes are written.
65
Data Structures and Algorithms Unit – 7 MCQs
c) logk (n+1) – 1
b) d) klogn
Answer: a
Explanation: B-tree of order n and with
height k has best case height h, where h =
logn (k+1) – 1. The best case occurs when all
the nodes are completely filled with keys.
Answer: a
Explanation: The front compression and the
d) rear compression are techniques used to
reduce space and time requirements in B-
tree. The compression enables to retain
more keys in a node so that the number of
nodes needed can be reduced.
66
Data Structures and Algorithms Unit – 7 MCQs
296. In a B+ tree, both the internal nodes 1)/2⌉ keys. Therefore, a minimum number
and the leaves have keys. of keys each leaf can have = ⌈(7 – 1)/2⌉ = 3.
a) True
299. Which of the following is false?
b) False
a) A B+ -tree grows downwards
Answer: b b) A B+ -tree is balanced
Explanation: In a B+ -tree, only the leaves c) In a B+ -tree, the sibling pointers allow
have keys, and these keys are replicated in sequential searching
non-leaf nodes for defining the path for d) B+ -tree is shallower than B-tree
locating individual records.
Answer: a
297. Which of the following is true? Explanation: A B+ -tree always grows
a) B + tree allows only the rapid random upwards. And In a B+tree – i)The path from
access the root to every leaf node is of the same
b) B + tree allows only the rapid sequential length, so the tree is balanced. ii) Leaves are
access linked, so allow sequential searching. iii) An
c) B + tree allows rapid random access as index is built with a single key per block of
well as rapid sequential access data rather than with one key per data
d) B + tree allows rapid random access and record, so it is shallower than B-tree.
slower sequential access
300. A B+ -tree of order 3 is generated by
Answer: c inserting 89, 9 and 8. The generated B+ -
Explanation: The B+ -tree being a variation tree is __________
of B-tree allows rapid random access. In a
B+ -tree the leaves are linked together, so it
also provides rapid sequential access.
Answer: b
Explanation: Maximum number of pointers
in a node is 7, i.e. the order of the B+ -tree
is 7. In a B+ tree of order n each leaf node
contains at most n – 1 key and at least ⌈(n −
67
Data Structures and Algorithms Unit – 7 MCQs
Answer: a
Explanation: During the split, the middle
key is retained in the right half node and
also promoted to parent node. When a key
is deleted from the leaf, it is retained in
non-leaves, because it can be still a valid
Answer: b separator between keys in nodes below.
Explanation:
301. Efficiency of finding the next record in
B+ tree is ____
a) O(n)
b) O(log n)
c) O(nlog n)
d) O(1)
Answer: d
Explanation: In a B+ -tree finding the next
recored (successor) involves accessing an
additional leaf at most. So, the efficiency of
finding the next record is O(1).
68
Data Structures and Algorithms Unit – 7 MCQs
Answer: d Answer: a
Explanation: A B+ tree of order n and height Explanation: The 2-3 trees is a balanced
h can have at most nh – 1 keys. Therefore tree. It is a specific form the B – tree. It is B
maximum number of keys = 33 -1 = 27 -1 = – tree of order 3, where every node can
26. have two child subtrees and one key or 3
child subtrees and two keys.
303. Which of the following is false?
a) Compared to B-tree, B+ -tree has larger 306. Which of the following is the 2-3 tree?
fanout
b) Deletion in B-tree is more complicated
than in B+ -tree
c) B+ -tree has greater depth than
corresponding B-tree
a)
d) Both B-tree and B+ -tree have same
search and insertion efficiencies
Answer: c
Explanation: A B+ -tree has larger fanout
and therefore have a depth smaller than
that of corresponding B-tree. b)
Answer: c
Explanation: The database-system
implementations use B+ -tree data
structure because they can be used for
multilevel indexing. d)
69
Data Structures and Algorithms Unit – 7 MCQs
307. AVL trees provide better insertion the node connected by a directed edge to its
2-3 trees. child.
a) True
310. What is the worst case efficiency for a
b) False
path compression algorithm?
Answer: b a) O(N)
Explanation: Insertion in AVL tree and 2-3 b) O(log N)
tree requires searching for proper position c) O(N log N)
for insertion and transformations for d) O(M log N)
balancing the tree. In both, the trees
Answer: d
searching takes O(log n) time, but
Explanation: The worst case efficiency for a
rebalancing in AVL tree takes O(log n), while
path compression algorithm is
the 2-3 tree takes O(1). So, 2-3 tree
mathematically found to be O(M log N).
provides better insertions.
311. Path Compression algorithm performs
308. How many child nodes does each node
in which of the following operations?
of Ternary Tree contain?
a) Create operation
a) 4
b) Insert operation
b) 6
c) Find operation
c) 5
d) Delete operation
d) 3
Answer: c
Answer: d
Explanation: Path compression algorithm is
Explanation: Each node of Ternary tree
performed during find operation and is
contains at most 3 nodes. So Ternary tree
independent of the strategy used to
can have 1, 2 or 3 child nodes but not more
perform unions.
than that.
312. What is the definition for Ackermann’s
309. Which of the following is the name of
function?
the node having child nodes?
a) A(1,i) = i+1 for i>=1
a) Brother
b) A(i,j) = i+j for i>=j
b) Sister
c) A(i,j) = i+j for i = j
c) Mother
d) A(1,i) = i+1 for i<1
d) Parent
Answer: a
Answer: d
Explanation: The Ackermann’s function is
Explanation: Parent node is the node having
defined as A(1,i) = i+1 for i>=1. This form in
child nodes and child nodes may contain
text grows faster and the inverse is slower.
references to their parents. Parent node is a
70
Data Structures and Algorithms Unit – 7 MCQs
315. Which of the following schemes does 318. A good hash approach is to derive the
quadratic probing come under? hash value that is expected to be
a) rehashing dependent of any patterns that might exist
b) extended hashing in the data.
c) separate chaining a) True
d) open addressing b) False
Answer: d Answer: b
Explanation: Quadratic probing comes Explanation: A hash value is expected to be
under open addressing scheme to resolve unrelated or independent of any patterns in
collisions in hash tables. the distribution of keys.
316. Which scheme uses a randomization 319. Interpret the given character string as
approach? an integer expressed in suitable radix
a) hashing by division notation.
b) hashing by multiplication
Character string = pt
71
Data Structures and Algorithms Unit – 7 MCQs
Answer: b Answer: c
Explanation: In division method for creating Explanation: The key 172 can be placed at
hash functions, k keys are mapped into one position 15 by using the formula
of m slots by taking the reminder of k H(k) = k mod m
divided by m. H(k) = 172 mod 157
H(k) = 15.
321. What can be the value of m in the
division method? 324. How many steps are involved in
a) Any prime number creating a hash function using a
b) Any even number multiplication method?
c) 2p – 1 a) 1
d) 2p b) 4
c) 3
Answer: a
d) 2
Explanation: A prime number not too close
to an exact power of 2 is often a good Answer: d
choice for m since it reduces the number of Explanation: In multiplication method 2
collisions which are likely to occur. steps are involved. First multiplying the key
value by a constant. Then multiplying this
322. Which scheme provides good
value by m.
performance?
a) open addressing
72
Data Structures and Algorithms Unit – 7 MCQs
73
Data Structures and Algorithms Unit – 7 MCQs
Answer: a Answer: a
Explanation: Because of randomization, the Explanation: In a walk if the vertices are
algorithm can behave differently on each distinct it is called a path, whereas if the
execution, providing good average case edges are distinct it is called a trail.
performance for any input.
334. In the given graph identify the cut
331. Double hashing is one of the best vertices.
methods available for open addressing.
a) True
b) False
Answer: a
Explanation: Double hashing is one of the
best methods for open addressing because
the permutations produced have many
characteristics of randomly chosen
permutations.
74
Data Structures and Algorithms Unit – 7 MCQs
a) True
b) False
Answer: a
Explanation: In a regular graph, degrees of
all the vertices are equal. In the given graph
the degree of every vertex is 3.
Answer: c Answer: b
Explanation: After removing vertices B and Explanation: The sum of the degrees of the
C, the graph becomes disconnected. vertices is equal to twice the number of
edges.
336. What is the number of edges present
in a complete graph having n vertices? 339. A connected planar graph having 6
a) (n*(n+1))/2 vertices, 7 edges contains _____________
b) (n*(n-1))/2 regions.
c) n a) 15
d) Information given is insufficient b) 3
c) 1
Answer: b d) 11
Explanation: Number of ways in which
every vertex can be connected to each Answer: b
other is nC2. Explanation: By euler’s formula the relation
between vertices(n), edges(q) and
337. The given Graph is regular. regions(r) is given by n-q+r=2.
75
Data Structures and Algorithms Unit – 7 MCQs
76
Data Structures and Algorithms Unit – 7 MCQs
Answer: b c) 16
Explanation: The given statement is the d) 0
definition of regular graphs.
Answer: b
347. Which of the following ways can be Explanation: Total number of values in the
used to represent a graph? matrix is 4*4=16, out of which 6 entries are
a) Adjacency List and Adjacency Matrix non zero.
b) Incidence Matrix
350. Adjacency matrix of all graphs are
c) Adjacency List, Adjacency Matrix as well
symmetric.
as Incidence Matrix
a) False
d) No way to represent
b) True
Answer: c
Answer: a
Explanation: Adjacency Matrix, Adjacency
Explanation: Only undirected graphs
List and Incidence Matrix are used to
produce symmetric adjacency matrices.
represent a graph.
351. Incidence matrix and Adjacency matrix
348. The number of elements in the
of a graph will always have same
adjacency matrix of a graph having 7
dimensions?
vertices is __________
a) True
a) 7
b) False
b) 14
c) 36 Answer: b
d) 49 Explanation: For a graph having V vertices
and E edges, Adjacency matrix have V*V
Answer: d
elements while Incidence matrix have V*E
Explanation: There are n*n elements in the
elements.
adjacency matrix of a graph with n vertices.
352. The column sum in an incidence matrix
349. What would be the number of zeros in
for a simple graph is __________
the adjacency matrix of the given graph?
a) depends on number of edges
b) always greater than 2
c) equal to 2
d) equal to the number of edges
Answer: c
Explanation: For every edge only the
vertices with which it is connected would
a) 10 have the value 1 in the matrix, as an edge
b) 6 connects two vertices sum will always be 2.
77
Data Structures and Algorithms Unit – 7 MCQs
353. What are the dimensions of an 356. Graph Structured Stack finds its
incidence matrix? application in _____________
a) Number of edges*number of edges a) Bogo Sort
b) Number of edges*number of vertices b) Tomita’s Algorithm
c) Number of vertices*number of vertices c) Todd–Coxeter algorithm
d) Number of edges * (1⁄2 * number of d) Heap Sort
vertices)
Answer: b
Answer: b Explanation: Tomita’s is a parsing algorithm
Explanation: Columns may represent edges which uses Graph Structured Stack in its
and vertices may be represented by the implementation.
rows.
357. Space complexity for an adjacency list
354. A Graph Structured Stack is a of an undirected graph having large values
_____________ of V (vertices) and E (edges) is ___________
a) Undirected Graph a) O(E)
b) Directed Graph b) O(V*V)
c) Directed Acyclic Graph c) O(E+V)
d) Regular Graph d) O(V)
Answer: c Answer: c
Explanation: A Graph Structured Stack is a Explanation: In an adjacency list for every
Directed Acyclic Graph with each path vertex there is a linked list which have the
representing a stack. values of the edges to which it is connected.
355. If a Graph Structured Stack contains 358. For some sparse graph an adjacency
{1,2,3,4} {1,5,3,4} {1,6,7,4} and {8,9,7,4}, list is more space efficient against an
what would be the source and sink vertices adjacency matrix.
of the DAC? a) True
a) Source – 1, 8 Sink – 7,4 b) False
b) Source – 1 Sink – 8,4
Answer: a
c) Source – 1, 8 Sink – 4
Explanation: Space complexity for
d) Source – 4, Sink – 1,8
adjacency matrix is always O(V*V) while
Answer: c space complexity for adjacency list in this
Explanation: Every Stack of the Graph case would be O(V).
Structured Stack represents a path, each
359. How many of the following statements
path starts with the source vertex and ends
are correct?
with the sink vertex.
i) All cyclic graphs are complete graphs.
78
Data Structures and Algorithms Unit – 7 MCQs
79
Data Structures and Algorithms Unit – 7 MCQs
80
Data Structures and Algorithms Unit – 7 MCQs
Answer: a Answer: b
Explanation: The best case occurs when the Explanation: Using the divide and conquer
given element to be found is at the first master theorem.
position. Therefore O(1) is the correct
374. What is the average case time
answer.
complexity of binary search using
371. Can linear search recursive algorithm recursion?
and binary search recursive algorithm be a) O(nlogn)
performed on an unordered list? b) O(logn)
a) Binary search can’t be used c) O(n)
b) Linear search can’t be used d) O(n2)
c) Both cannot be used
Answer: b
d) Both can be used
Explanation: T(n) = T(n/2) + 1, Using the
Answer: a divide and conquer master theorem.
Explanation: As binary search requires
375. Which of the following is not an
comparison, it is required that the list be
application of binary search?
ordered. Whereas this doesn’t matter for
a) To find the lower/upper bound in an
linear search.
ordered sequence
372. What is the advantage of recursive b) Union of intervals
approach than an iterative approach? c) Debugging
a) Consumes less memory d) To search in unordered list
b) Less code and easy to implement
Answer: d
c) Consumes more memory
Explanation: In Binary search, the elements
d) More code has to be written
in the list should be sorted. It is applicable
Answer: b only for ordered list. Hence Binary search in
Explanation: A recursive approach is easier unordered list is not an application.
to understand and contains fewer lines of
376. Jump search algorithm requires which
code.
of the following condition to be true?
373. What is the worst case complexity of a) array should be sorted
binary search using recursion? b) array should have not be sorted
a) O(nlogn) c) array should have a less than 64 elements
b) O(logn) d) array should be partially sorted
c) O(n)
Answer: a
d) O(n2)
Explanation: Jump sort requires the input
array to be sorted. The algorithm would fail
81
Data Structures and Algorithms Unit – 7 MCQs
to give the correct result if array is not insertion sort algorithm runs in linear time
sorted. and is given by O(N).
377. Which of the following examples 380. How many passes does an insertion
represent the worst case input for an sort algorithm consist of?
insertion sort? a) N
a) array in sorted order b) N-1
b) array sorted in reverse order c) N+1
c) normal unsorted array d) N2
d) large array
Answer: b
Answer: b Explanation: An insertion algorithm consists
Explanation: The worst case input for an of N-1 passes when an array of N elements
insertion sort algorithm will be an array is given.
sorted in reverse order and its running time
381. Which of the following algorithm
is quadratic.
implementations is similar to that of an
378. Which of the following sorting insertion sort?
algorithms is the fastest for sorting small a) Binary heap
arrays? b) Quick sort
a) Quick sort c) Merge sort
b) Insertion sort d) Radix sort
c) Shell sort
Answer: a
d) Heap sort
Explanation: Insertion sort is similar to that
Answer: b of a binary heap algorithm because of the
Explanation: For sorting small arrays, use of temporary variable to swap.
insertion sort runs even faster than quick
382. What is the average case running time
sort. But, it is impractical to sort large
of an insertion sort algorithm?
arrays.
a) O(N)
379. For the best case input, the running b) O(N log N)
time of an insertion sort algorithm is? c) O(log N)
a) Linear d) O(N2)
b) Binary
Answer: d
c) Quadratic
Explanation: The average case analysis of a
d) Depends on the input
tight bound algorithm is mathematically
Answer: a achieved to be O(N2).
Explanation: The best case input for an
82
Data Structures and Algorithms Unit – 7 MCQs
Answer: a Answer: a
Explanation: Each swap removes only one Explanation: During insertion sort, the
inversion, so O(N2) swaps are required. relative order of elements is not changed.
Therefore, it is a stable sorting algorithm.
384. Binary search can be used in an
And insertion sort requires only O(1) of
insertion sort algorithm to reduce the
additional memory space. Therefore, it
number of comparisons.
sorts In-place.
a) True
b) False 387. Which of the following sorting
algorithm is best suited if the elements are
Answer: a
already sorted?
Explanation: Binary search can be used in
a) Heap Sort
an insertion sort algorithm to reduce the
b) Quick Sort
number of comparisons. This is called a
c) Insertion Sort
Binary insertion sort.
d) Merge Sort
385. Which of the following options contain
Answer: c
the correct feature of an insertion sort
Explanation: The best case running time of
algorithm?
the insertion sort is O(n). The best case
a) anti-adaptive
occurs when the input array is already
b) dependable
sorted. As the elements are already sorted,
c) stable, not in-place
only one comparison is made on each pass,
d) stable, adaptive
so that the time required is O(n).
Answer: d
388. Insertion sort is an example of an
Explanation: An insertion sort is stable,
incremental algorithm.
adaptive, in-place and incremental in
a) True
nature.
b) False
386. Which of the following is correct with
Answer: a
regard to insertion sort?
Explanation: In the incremental algorithms,
a) insertion sort is stable and it sorts In-
the complicated structure on n items is built
place
by first building it on n − 1 items. And then
b) insertion sort is unstable and it sorts In-
we make the necessary changes to fix
83
Data Structures and Algorithms Unit – 7 MCQs
things in adding the last item. Insertion sort sorted and RHS is yet to be sorted. Starting
builds the sorted sequence one element at with the first element the ‘min’ element
a time. Therefore, it is an example of an moves towards the final element.
incremental algorithm.
392. What is the advantage of selection sort
389. What is an in-place sorting algorithm? over other sorting techniques?
a) It needs O(1) or O(logn) memory to a) It requires no additional storage space
create auxiliary locations b) It is scalable
b) The input is already sorted and in-place c) It works best for inputs which are already
c) It requires additional storage sorted
d) It requires additional space d) It is faster than any other sorting
technique
Answer: a
Explanation: Auxiliary memory is required Answer: a
for storing the data temporarily. Explanation: Since selection sort is an in-
place sorting algorithm, it does not require
390. In the following scenarios, when will
additional storage.
you use selection sort?
a) The input is already sorted 393. What is the average case complexity of
b) A large file has to be sorted selection sort?
c) Large values need to be sorted with small a) O(nlogn)
keys b) O(logn)
d) Small values need to be sorted with large c) O(n)
keys d) O(n2)
Answer: c Answer: d
Explanation: Selection is based on keys, Explanation: In the average case, even if the
hence a file with large values and small keys input is partially sorted, selection sort
can be efficiently sorted with selection sort. behaves as if the entire array is not sorted.
Selection sort is insensitive to input.
391. What is the worst case complexity of
selection sort? 394. What is the disadvantage of selection
a) O(nlogn) sort?
b) O(logn) a) It requires auxiliary memory
c) O(n) b) It is not scalable
d) O(n2) c) It can be used for small keys
d) It takes linear time to sort the elements
Answer: d
Explanation: Selection sort creates a sub-
list, LHS of the ‘min’ element is already
84
Data Structures and Algorithms Unit – 7 MCQs
85
Data Structures and Algorithms Unit – 7 MCQs
Answer: c Answer: a
Explanation: Merge sort uses divide and Explanation: Standard merge sort requires
conquer in order to sort a given array. This O(n) space to merge two sorted arrays. We
is because it divides the array into two can optimize this merging process so that it
halves and applies merge sort algorithm to takes only constant space. This version is
each half individually after which the two known as in place merge sort.
sorted halves are merged together.
404. What is the worst case time complexity
401. What is the average case time of merge sort?
complexity of merge sort? a) O(n log n)
a) O(n log n) b) O(n2)
b) O(n2) c) O(n2 log n)
c) O(n2 log n) d) O(n log n2)
d) O(n log n2)
Answer: a
Answer: a Explanation: The time complexity of merge
Explanation: The recurrence relation for sort is not affected by worst case as its
merge sort is given by T(n) = 2T(n/2) + n. It algorithm has to implement the same
is found to be equal to O(n log n) using the number of steps in any case. So its time
master theorem. complexity remains to be O(n log n).
402. What is the auxiliary space complexity 405. Which of the following method is used
of merge sort? for sorting in merge sort?
a) O(1) a) merging
b) O(log n) b) partitioning
c) O(n) c) selection
d) O(n log n) d) exchanging
Answer: c Answer: a
Explanation: An additional space of O(n) is Explanation: Merge sort algorithm divides
required in order to merge two sorted the array into two halves and applies merge
arrays. Thus merge sort is not an in place sort algorithm to each half individually after
sorting algorithm. which the two sorted halves are merged
together. Thus its method of sorting is
called merging.
86
Data Structures and Algorithms Unit – 7 MCQs
406. What will be the best case time merge sort is given by T(n) = 2T(n/2) + n.
complexity of merge sort? This can be solved using master’s theorem
a) O(n log n) and is found equal to O(n log n).
b) O(n2)
409. What is the auxiliary space complexity
c) O(n2 log n)
of standard merge sort?
d) O(n log n2)
a) O(1)
Answer: a b) O(log n)
Explanation: The time complexity of merge c) O(n)
sort is not affected in any case as its d) O(n log n)
algorithm has to implement the same
Answer: c
number of steps. So its time complexity
Explanation: The merging of two sorted
remains to be O(n log n) even in the best
arrays requires an additional space of n due
case.
to which the auxiliary space requirement of
407. Merge sort uses which of the following merge sort is O(n). Thus merge sort is not
algorithm to implement sorting? an in place sorting algorithm.
a) backtracking
410. What is the space complexity of in
b) greedy algorithm
place merge sort?
c) divide and conquer
a) O(1)
d) dynamic programming
b) O(n)
Answer: c c) O(log n)
Explanation: Merge sort uses the technique d) O(n log n)
of divide and conquer in order to sort a
Answer: c
given array. It divides the array into two
Explanation: Space complexity of in place
halves and apply merge sort algorithm to
version of merge sort is O(log n) which is
each half individually after which the sorted
used for storing call stack formed due to
versions of these halves are merged
recursion. Note that the algorithms with
together.
space complexity as O(log n) also qualifies
408. What is the average case time as in place algorithms as the value of log n is
complexity of standard merge sort? close to 1.
a) O(n log n)
411. Quick sort is a __________
b) O(n2)
a) greedy algorithm
c) O(n2 log n)
b) divide and conquer algorithm
d) O(n log n2)
c) dynamic programming algorithm
Answer: a d) backtracking algorithm
Explanation: The recurrence relation for
87
Data Structures and Algorithms Unit – 7 MCQs
88
Data Structures and Algorithms Unit – 7 MCQs
elements from the priority queue using standard sorting algorithm since its version
siftdown operation. 2.3. It is an example of hybrid sorting
algorithm which means it uses more than
417. Which of the following sorting
one sorting algorithm as a routine.
algorithm is used by C++ internally?
a) quicksort 420. Which of the following sorting
b) introsort algorithm is a constituent of tim sort?
c) merge sort a) selection sort
d) heap sort b) quick sort
c) merge sort
Answer: b
d) heap sort
Explanation: Introsort is the in built sorting
algorithm used by C++. It is an example of a Answer: c
hybrid sorting algorithm which means it Explanation: Tim sort is a hybrid sorting
uses more than one sorting algorithm as a algorithm which means it uses more than
routine. one sorting algorithm as a routine. It is
derived from insertion sort and merge sort.
418. Which of the following sorting
algorithm is not a constituent of introsort? 421. Tim sort begins sorting the given array
a) selection sort by using which of the following sorting
b) quicksort algorithm?
c) insertion sort a) selection sort
d) heap sort b) quick sort
c) insertion sort
Answer: a
d) merge sort
Explanation: Introsort is a hybrid sorting
algorithm which means it uses more than Answer: c
one sorting algorithm as a routine. It may Explanation: Tim sort begins sorting any
use quick sort or heap sort or insertion sort given array by using insertion sort for each
depending on the given situation. run. The array is divided into smaller parts
for this purpose, each part having a size
419. Which of the following is Python’s
equal to value of run. Then these small
standard sorting algorithm?
parts called runs are merged in order to
a) quick sort
obtain sorted array
b) introsort
c) merge sort 422. Which of the following is an example
d) tim sort of parallel sorting technique?
a) bogo sort
Answer: d
b) sleep sort
Explanation: Tim sort has been python’s
89
Data Structures and Algorithms Unit – 7 MCQs
91
Data Structures and Algorithms Unit – 7 MCQs
Answer: b Answer: c
Explanation: Strassen’s Algorithm for matrix Explanation: Strassen’s matrix
multiplication is a recursive algorithm since multiplication algorithm follows divide and
the present output depends on previous conquer technique. In this algorithm the
outputs and inputs. input matrices are divided into n/2 x n/2
sub matrices and then the recurrence
437. What is the running time of Strassen’s
relation is applied.
algorithm for matrix multiplication?
a) O(n2.81) 440. The number of scalar additions and
b) O(n3) subtractions used in Strassen’s matrix
c) O(n1.8) multiplication algorithm is ________
d) O(n2) a) O(n2.81)
b) Theta(n2)
Answer: a
c) Theta(n)
Explanation: Strassen’s matrix algorithm
d) O(n3)
92
Data Structures and Algorithms Unit – 7 MCQs
442. What is meant by the power set of a 445. Depth First Search is equivalent to
set? which of the traversal in the Binary Trees?
a) subset of all sets a) Pre-order Traversal
b) set of all subsets b) Post-order Traversal
c) set of particular subsets c) Level-order Traversal
d) empty set d) In-order Traversal
Answer: b Answer: a
Explanation: Power set of a set is defined as Explanation: In Depth First Search, we
the set of all subsets. Ex- S={1,2} then explore all the nodes aggressively to one
P={{},{1},{2}{1,2}}. path and then backtrack to the node.
Hence, it is equivalent to the pre-order
443. Number of elements in the power set
traversal of a Binary Tree.
of set S={1,2,3} will be?
a) 2 446. Time Complexity of DFS is? (V –
b) 4 number of vertices, E – number of edges)
c) 6 a) O(V + E)
d) 8 b) O(V)
c) O(E)
Answer: d
d) O(V*E)
Explanation: Power set of a set is defined as
93
Data Structures and Algorithms Unit – 7 MCQs
Answer: a Answer: a
Explanation: The Depth First Search Explanation: The Breadth First Search
explores every node once and every edge explores every node once and every edge
once (in worst case), so it’s time complexity once (in worst case), so it’s time complexity
is O(V + E). is O(V + E).
447. The Data structure used in standard 450. The Data structure used in standard
implementation of Breadth First Search is? implementation of Breadth First Search is?
a) Stack a) Stack
b) Queue b) Queue
c) Linked List c) Linked List
d) Tree d) Tree
Answer: a Answer: b
Explanation: The Depth First Search is Explanation: The Breadth First Search
implemented using recursion. So, stack can explores every node once and put that node
be used as data structure to implement in queue and then it takes out nodes from
depth first search. the queue and explores it’s neighbors.
Answer: c
Explanation: The Breadth First Search
Algorithm searches the nodes on the basis
of level. It takes a node (level 0), explores
it’s neighbors (level 1) and so on.
94