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

Csa Unit-7

The document provides information about data structures and algorithms. It defines data structures as a way to organize and store data for efficient use. Some common data structures mentioned are arrays, linked lists, stacks, and queues. Algorithms are sets of steps or instructions to solve problems. The document discusses different types of data structures like linear and non-linear, and common operations on data structures such as traversing, inserting, deleting, searching, sorting, and merging.

Uploaded by

Vignesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
395 views

Csa Unit-7

The document provides information about data structures and algorithms. It defines data structures as a way to organize and store data for efficient use. Some common data structures mentioned are arrays, linked lists, stacks, and queues. Algorithms are sets of steps or instructions to solve problems. The document discusses different types of data structures like linear and non-linear, and common operations on data structures such as traversing, inserting, deleting, searching, sorting, and merging.

Uploaded by

Vignesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 418

DIWAKAR EDUCATION HUB

Data Structures and Algorithms


Unit – 7
As per updated syllabus
DIWAKAR EDUCATION HUB

2020

THE LEARN WITH EXPERTIES


Data Structures and Algorithms Unit – 7

Data Structures
Data Structure is a way to store and organize data so
that it can be used efficiently.

Data Structure can be defined as the group of data


elements which provides an efficient way of storing
and organising data in the computer so that it can be
used efficiently. Some examples of Data Structures
are arrays, Linked List, Stack, Queue, etc. Data
Structures are widely used in almost every aspect of
Computer Science i.e. Operating System, Compiler Design, Artifical intelligence,
Graphics and many more.

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.

Field: Field is a single elementary unit of information representing the attribute of


an entity.

Need of Data Structures

As applications are getting complexed and amount of data is increasing day by


day, there may arrise the following problems:

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.

Multiple requests: If thousands of users are searching the data simultaneously on


a web server, then there are the chances that a very large server can be failed
during that 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.

Advantages of Data Structures

Efficiency: Efficiency of a program depends upon the choice of data structures.


For example: suppose, we have some data and we need to perform the search for
a perticular record. In that case, if we organize our data in an array, we will have

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.

Reusability: Data structures are reusable, i.e. once we have implemented a


particular data structure, we can use it at any other place. Implementation of data
structures can be compiled into libraries which can be used by different clients.

Abstraction: Data structure is specified by the ADT which provides a level of


abstraction. The client program uses the data structure through interface only,
without getting into the implementation details.

Data Structure Classification

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.

Types of Linear Data Structures are given below:

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.

The individual elements of the array age are:

age[0], age[1], age[2], age[3],......... age[98], age[99].

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.

A stack is an abstract data type (ADT), can be implemented in most of the


programming languages. It is named as stack because it behaves like a real-world
stack, for example: - piles of plates or deck of cards etc.

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.

It is an abstract data structure, similar to stack. Queue is opened at both end


therefore it follows First-In-First-Out (FIFO) methodology for storing the data
items.

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.

Types of Non Linear Data Structures are given below:

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.

Graphs: Graphs can be defined as the pictorial representation of the set of


elements (represented by vertices) connected by the links known as edges. A
graph is different from tree in the sense that a graph can have cycle while the tree
can not have the one.

Operations on data structure

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.

Example: If we need to calculate the average of the marks obtained by a student


in 6 different subject, we need to traverse the complete array of marks and
calculate the total sum, then we will devide that sum by the number of subjects
i.e. 6, in order to find the average.

2) Insertion: Insertion can be defined as the process of adding the elements to


the data structure at any location.

If the size of data structure is n then we can only insert n-1 data elements into it.

3) Deletion:The process of removing an element from the data structure is called


Deletion. We can delete an element from the data structure at any random
location.

If we try to delete an element from an empty data structure


then underflow occurs.

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

An algorithm is a procedure having well defined steps for solving a particular


problem. Algorithm is finite set of logic or instructions, written in order for
accomplish the certain predefined task. It is not the complete program or code, it
is just a solution (logic) of a problem, which can be represented either as an
informal description using a Flowchart or Pseudo code.

The major categories of algorithms are given below:

o Sort: Algorithm developed for sorting the items in certain order.

o Search: Algorithm developed for searching the items inside a data


structure.

o Delete: Algorithm developed for deleting the existing element from the
data structure.

o Insert: Algorithm developed for inserting an item inside a data structure.

o Update: Algorithm developed for updating the existing element inside a


data structure.

The performance of algorithm is measured on the basis of following properties:

8
Data Structures and Algorithms Unit – 7

o Time complexity: It is a way of representing the amount of time needed by


a program to run to the completion.

o Space complexity: It is the amount of memory space required by an


algorithm, during a course of its execution. Space complexity is required in
situations when limited memory is available and for the multi user system.

Each algorithm must have:

o Specification: Description of the computational procedure.

o Pre-conditions: The condition(s) on input.

o Body of the Algorithm: A sequence of clear and unambiguous instructions.

o Post-conditions: The condition(s) on output.

Example: Design an algorithm to multiply the two numbers x and y and display
the result in z.

o Step 1 START

o Step 2 declare three integers x, y & z

o Step 3 define values of x & y

o Step 4 multiply values of x & y

o Step 5 store the output of step 4 in z

o Step 6 print z

o Step 7 STOP

. Alternatively the algorithm can be written as ?

o Step 1 START MULTIPLY

o Step 2 get values of x & y

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

An algorithm must follow the mentioned below characteristics:

o Input: An algorithm must have 0 or well defined inputs.

o Output: An algorithm must have 1 or well defined outputs, and should


match with the desired output.

o Feasibility: An algorithm must be terminated after the finite number of


steps.

o Independent: An algorithm must have step-by-step directions which is


independent of any programming code.

o Unambiguous: An algorithm must be unambiguous and clear. Each of their


steps and input/outputs must be clear and lead to only one meaning.

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.

o For example, if we want to store the marks of a student in 6 subjects, then


we don't need to define different variable for the marks in different

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.

Properties of the Array

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.

for example, in C language, the syntax of declaring an array is like following:

1. int arr[10]; char arr[10]; float arr[5]

Need of using Array

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.

Program without array:

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;

5. float avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 +marks_


6) / 6 ;

6. printf(avg);

7. }

Program by using array:

1. #include <stdio.h>

2. void main ()

3. {

4. int marks[6] = {56,78,88,76,56,89);

5. int i;

6. float avg;

7. for (i=0; i<6; i++ )

8. {

9. avg = avg + marks[i];

12
Data Structures and Algorithms Unit – 7

10. }

11. printf(avg);

12. }

Complexity of Array operations

Time and space complexity of various array operations are described in the
following table.

Time Complexity

Algorithm Average Case Worst Case

Access O(1) O(1)

Search O(n) O(n)

Insertion O(n) O(n)

Deletion O(n) O(n)

Space Complexity

In array, space complexity for worst case is O(n).

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 Traversing an array is a very simple process, we just need to increment the


base address of the array in order to visit each element one by one.

o Any element in the array can be directly accessed by using the index.

Memory Allocation of the array

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.

The indexing of the array can be defined in three ways.

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.

Accessing Elements of an array

To access any random element of an array we need the following information:

1. Base Address of the array.

2. Size of an element in bytes.

3. Which type of indexing, array follows.

Address of any element of a 1D array can be calculated by using the following


formula:

1. Byte address of element A[i] = base address + size * ( i - first index)

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

2. find the location of A[-1].

3. L(A[-1]) = 999 + [(-1) - (-10)] x 2

4. = 999 + 18

5. = 1017

Passing array to the function :

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. {

5. int arr[5] = {0,1,2,3,4};

6. int sum = summation(arr);

7. printf("%d",sum);

8. }

9.

10. int summation (int arr[])

11. {

12. int sum=0,i;


16
Data Structures and Algorithms Unit – 7

13. for (i = 0; i<5; i++)

14. {

15. sum = sum + arr[i];

16. }

17. return sum;

18. }

The above program defines a function named as summation which accepts an


array as an argument. The function calculates the sum of all the elements of the
array and returns it.

2D Array

2D array can be defined as an array of arrays. The 2D array is organized as


matrices which can be represented as the collection of rows and columns.

However, 2D arrays are created to implement a relational database look alike


data structure. It provides ease of holding bulk of data at once which can be
passed to any number of functions wherever required.

How to declare 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];

however, It produces the data structure which looks like following.

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.

How do we access data in a 2D array

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.

We can assign each cell of a 2D array to 0 by using the following code:

1. for ( int i=0; i<n ;i++)

2. {

3. for (int j=0; j<n; j++)

4. {

5. a[i][j] = 0;

6. }

7. }

Initializing 2D Arrays

We know that, when we declare and initialize one dimensional array in C


programming simultaneously, we don't need to specify the size of the array.
However this will not work with 2D arrays. We will have to define at least the
second dimension of the array.

The syntax to declare and initialize the 2D array is given as follows.

1. int arr[2][2] = {0,1,2,3};

The number of elements that can be present in a 2D array will always be equal to
(number of rows * number of columns).

Example : Storing User's data into a 2D array and printing it.

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. {

9. printf("Enter a[%d][%d]: ",i,j);

10. scanf("%d",&arr[i][j]);

11. }

12. }

13. printf("\n printing the elements ....\n");

14. for(i=0;i<3;i++)

15. {

16. printf("\n");

17. for (j=0;j<3;j++)

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 {

3. publicstaticvoid main(String[] args) {

4. int[][] arr = newint[3][3];

5. Scanner sc = new Scanner(System.in);

6. for (inti =0;i<3;i++)

7. {

8. for(intj=0;j<3;j++)

9. {

10. System.out.print("Enter Element");

11. arr[i][j]=sc.nextInt();

12. System.out.println();

13. }

14. }

15. System.out.println("Printing Elements...");

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.

3. public class Program

4. {

5. public static void Main()

6. {

7. int[,] arr = new int[3,3];

8. for (int i=0;i<3;i++)

9. {

10. for (int j=0;j<3;j++)

11. {

12. Console.WriteLine("Enter Element");

13. arr[i,j]= Convert.ToInt32(Console.ReadLine());

14. }

22
Data Structures and Algorithms Unit – 7

15. }

16. Console.WriteLine("Printing Elements...");

17. for (int i=0;i<3;i++)

18. {

19. Console.WriteLine();

20. for (int j=0;j<3;j++)

21. {

22. Console.Write(arr[i,j]+" ");

23. }

24. }

25. }

26. }

Mapping 2D array to 1D array

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.

The size of a two dimensional array is equal to the multiplication of number of


rows and the number of columns present in the array. We do need to map two
dimensional array to the one dimensional array in order to store them in the
memory.

23
Data Structures and Algorithms Unit – 7

A 3 X 3 two dimensional array is shown in the following image. However, this


array needs to be mapped to a one dimensional array in order to store it into the
memory.

There are two main techniques of storing 2D array elements into memory

1. Row Major ordering

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

2. Column Major ordering

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

Calculating the Address of the random element of a 2D array

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.

By Row Major Order

If array is declared by a[m][n] where m is the number of rows while n is the


number of columns, then address of an element a[i][j] of the array stored in row
major order is calculated as,

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 :

a[10...30, 55...75], base address of the array (BA) = 0, size of an element = 4 by


tes .

Find the location of a[15][68].

26
Data Structures and Algorithms Unit – 7

Address(a[15][68]) = 0 +

((15 - 10) x (68 - 55 + 1) + (68 - 55)) x 4

= (5 x 14 + 13) x 4

= 83 x 4

= 332 answer

By Column major order

If array is declared by a[m][n] where m is the number of rows while n is the


number of columns, then address of an element a[i][j] of the array stored in row
major order is calculated as,

1. Address(a[i][j]) = ((j*m)+i)*Size + BA

where BA is the base address of the array.

Example:

A [-
5 ... +20][20 ... 70], BA = 1020, Size of element = 8 bytes. Find the location of a[
0][30].

Address [A[0][30]) = ((30-


20) x 24 + 5) x 8 + 1020 = 245 x 8 + 1020 = 2980 bytes

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 last node of the list contains pointer to the null.

Uses of Linked List

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 Empty node can not be present in the linked list.

o We can store values of primitive types or objects in the singly linked list.

Why use linked list over array?

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.

Array contains following limitations:

1. The size of array must be known in advance before using it in the program.
28
Data Structures and Algorithms Unit – 7

2. Increasing size of the array is a time taking process. It is almost impossible


to expand the size of the array at run time.

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 or One way chain

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

Data Time Complexity Spac


Struct e
ure Com
pleity

Average Worst Worst

Acce Sear Insert Delet Acc Sear Insert Delet


ss ch ion ion ess ch ion ion

30
Data Structures and Algorithms Unit – 7

Singly θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
Linked
List

Operations on Singly 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;

4. struct node *next;

5. };

6. struct node *head, *ptr;

7. ptr = (struct node *)malloc(sizeof(struct node *));

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

1 Insertion at It involves inserting any element at the front of the list. We


beginning just need to a few link adjustments to make the new node as

31
Data Structures and Algorithms Unit – 7

the head of the list.

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

Deletion and Traversing

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

1 Deletion at It involves deletion of a node from the beginning of the list.


beginning This is the simplest operation among all. It just need a few
adjustments in the node pointers.

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.

4 Traversing In traversing, we simply visit each node of the list at least


once in order to perform some specific operation on it, for
example, printing data part of each node present in the
list.

5 Searching In searching, we match each element of the list with the


given element. If the element is found on any of the
location then location of that element is returned
otherwise null is returned. .

Linked List in C: Menu Driven Program

1. #include<stdio.h>

2. #include<stdlib.h>

3. struct node

4. {

5. int data;

33
Data Structures and Algorithms Unit – 7

6. struct node *next;

7. };

8. struct node *head;

9.

10. void beginsert ();

11. void lastinsert ();

12. void randominsert();

13. void begin_delete();

14. void last_delete();

15. void random_delete();

16. void display();

17. void search();

18. void main ()

19. {

20. int choice =0;

21. while(choice != 9)

22. {

23. printf("\n\n*********Main Menu*********\n");

24. printf("\nChoose one option from the following list ...\n");

25. printf("\n===============================================\n")
;

34
Data Structures and Algorithms Unit – 7

26. printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random l


ocation\n4.Delete from Beginning\n

27. 5.Delete from last\n6.Delete node after specified location\n7.Search fo


r an element\n8.Show\n9.Exit\n");

28. printf("\nEnter your choice?\n");

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:

60. printf("Please enter valid choice..");

61. }

62. }

63. }

64. void beginsert()

65. {

66. struct node *ptr;

36
Data Structures and Algorithms Unit – 7

67. int item;

68. ptr = (struct node *) malloc(sizeof(struct node *));

69. if(ptr == NULL)

70. {

71. printf("\nOVERFLOW");

72. }

73. else

74. {

75. printf("\nEnter value\n");

76. scanf("%d",&item);

77. ptr->data = item;

78. ptr->next = head;

79. head = ptr;

80. printf("\nNode inserted");

81. }

82.

83. }

84. void lastinsert()

85. {

86. struct node *ptr,*temp;

87. int item;

37
Data Structures and Algorithms Unit – 7

88. ptr = (struct node*)malloc(sizeof(struct node));

89. if(ptr == NULL)

90. {

91. printf("\nOVERFLOW");

92. }

93. else

94. {

95. printf("\nEnter value?\n");

96. scanf("%d",&item);

97. ptr->data = item;

98. if(head == NULL)

99. {

100. ptr -> next = NULL;

101. head = ptr;

102. printf("\nNode inserted");

103. }

104. else

105. {

106. temp = head;

107. while (temp -> next != NULL)

108. {

38
Data Structures and Algorithms Unit – 7

109. temp = temp -> next;

110. }

111. temp->next = ptr;

112. ptr->next = NULL;

113. printf("\nNode inserted");

114.

115. }

116. }

117. }

118. void randominsert()

119. {

120. int i,loc,item;

121. struct node *ptr, *temp;

122. ptr = (struct node *) malloc (sizeof(struct node));

123. if(ptr == NULL)

124. {

125. printf("\nOVERFLOW");

126. }

127. else

128. {

129. printf("\nEnter element value");

39
Data Structures and Algorithms Unit – 7

130. scanf("%d",&item);

131. ptr->data = 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. {

137. temp = temp->next;

138. if(temp == NULL)

139. {

140. printf("\ncan't insert\n");

141. return;

142. }

143.

144. }

145. ptr ->next = temp ->next;

146. temp ->next = ptr;

147. printf("\nNode inserted");

148. }

149. }

150. void begin_delete()

40
Data Structures and Algorithms Unit – 7

151. {

152. struct node *ptr;

153. if(head == NULL)

154. {

155. printf("\nList is empty\n");

156. }

157. else

158. {

159. ptr = head;

160. head = ptr->next;

161. free(ptr);

162. printf("\nNode deleted from the begining ...\n");

163. }

164. }

165. void last_delete()

166. {

167. struct node *ptr,*ptr1;

168. if(head == NULL)

169. {

170. printf("\nlist is empty");

171. }

41
Data Structures and Algorithms Unit – 7

172. else if(head -> next == NULL)

173. {

174. head = NULL;

175. free(head);

176. printf("\nOnly node of the list deleted ...\n");

177. }

178.

179. else

180. {

181. ptr = head;

182. while(ptr->next != NULL)

183. {

184. ptr1 = ptr;

185. ptr = ptr ->next;

186. }

187. ptr1->next = NULL;

188. free(ptr);

189. printf("\nDeleted Node from the last ...\n");

190. }

191. }

192. void random_delete()

42
Data Structures and Algorithms Unit – 7

193. {

194. struct node *ptr,*ptr1;

195. int loc,i;

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. {

201. ptr1 = ptr;

202. ptr = ptr->next;

203.

204. if(ptr == NULL)

205. {

206. printf("\nCan't delete");

207. return;

208. }

209. }

210. ptr1 ->next = ptr ->next;

211. free(ptr);

212. printf("\nDeleted node %d ",loc+1);

43
Data Structures and Algorithms Unit – 7

213. }

214. void search()

215. {

216. struct node *ptr;

217. int item,i=0,flag;

218. ptr = head;

219. if(ptr == NULL)

220. {

221. printf("\nEmpty List\n");

222. }

223. else

224. {

225. printf("\nEnter item which you want to search?\n");

226. scanf("%d",&item);

227. while (ptr!=NULL)

228. {

229. if(ptr->data == item)

230. {

231. printf("item found at location %d ",i+1);

232. flag=0;

233. }

44
Data Structures and Algorithms Unit – 7

234. else

235. {

236. flag=1;

237. }

238. i++;

239. ptr = ptr -> next;

240. }

241. if(flag==1)

242. {

243. printf("Item not found\n");

244. }

245. }

246.

247. }

248.

249. void display()

250. {

251. struct node *ptr;

252. ptr = head;

253. if(ptr == NULL)

254. {

45
Data Structures and Algorithms Unit – 7

255. printf("Nothing to print");

256. }

257. else

258. {

259. printf("\nprinting values . . . . .\n");

260. while (ptr!=NULL)

261. {

262. printf("\n%d",ptr->data);

263. ptr = ptr -> next;

264. }

265. }

266. }

267.

Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

46
Data Structures and Algorithms Unit – 7

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value

Node inserted

*********Main Menu*********

Choose one option from the following list ..

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

47
Data Structures and Algorithms Unit – 7

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value?

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

48
Data Structures and Algorithms Unit – 7

Enter your choice?

Enter element value1

Enter the location after which you want to insert 1

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

printing values . . . . .

49
Data Structures and Algorithms Unit – 7

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value?

123

Node inserted

50
Data Structures and Algorithms Unit – 7

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter value

1234

Node inserted

*********Main Menu*********

Choose one option from the following list ...

51
Data Structures and Algorithms Unit – 7

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Node deleted from the begining ...

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

52
Data Structures and Algorithms Unit – 7

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Deleted Node from the last ...

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

53
Data Structures and Algorithms Unit – 7

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

Enter the location of the node after which you want to perform deletion

Deleted node 2

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

54
Data Structures and Algorithms Unit – 7

9.Exit

Enter your choice?

printing values . . . . .

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

Enter your choice?

55
Data Structures and Algorithms Unit – 7

Enter item which you want to search?

item found at location 1

item found at location 2

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete node after specified location

7.Search for an element

8.Show

9.Exit

56
Data Structures and Algorithms Unit – 7

Enter your choice?

Doubly linked list

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.

In C, structure of a node in doubly linked list can be given as :

57
Data Structures and Algorithms Unit – 7

1. struct node

2. {

3. struct node *prev;

4. int data;

5. struct node *next;

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.

Memory Representation of a doubly linked list

Memory Representation of a doubly linked list is shown in the following image.


Generally, doubly linked list consumes more space for every node and therefore,
causes more expansive basic operations such as insertion and deletion. However,
we can easily manipulate the elements of the list since the list maintains pointers
in both the directions (forward and backward).

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.

Operations on doubly linked list

Node Creation

1. struct node

2. {

3. struct node *prev;

59
Data Structures and Algorithms Unit – 7

4. int data;

5. struct node *next;

6. };

7. struct node *head;

All the remaining operations regarding doubly linked list are described in the
following table.

SN Operation Description

1 Insertion at Adding the node into the linked list at beginning.


beginning

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

4 Deletion at Removing the node from beginning of the list


beginning

5 Deletion at Removing the node from end of the list.


the end

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

7 Searching Comparing each node data with the item to be searched


and return the location of the item in the list if the item
found else return null.

8 Traversing Visiting each node of the list at least once in order to


perform some specific operation like searching, sorting,
display, etc.

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. {

5. struct node *prev;

6. struct node *next;

7. int data;

8. };

9. struct node *head;

10. void insertion_beginning();

11. void insertion_last();


61
Data Structures and Algorithms Unit – 7

12. void insertion_specified();

13. void deletion_beginning();

14. void deletion_last();

15. void deletion_specified();

16. void display();

17. void search();

18. void main ()

19. {

20. int choice =0;

21. while(choice != 9)

22. {

23. printf("\n*********Main Menu*********\n");

24. printf("\nChoose one option from the following list ...\n");

25. printf("\n===============================================\n")
;

26. printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random l


ocation\n4.Delete from Beginning\n

27. 5.Delete from last\n6.Delete the node after the given data\n7.Search\
n8.Show\n9.Exit\n");

28. printf("\nEnter your choice?\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:

60. printf("Please enter valid choice..");

61. }

62. }

63. }

64. void insertion_beginning()

65. {

66. struct node *ptr;

67. int item;

68. ptr = (struct node *)malloc(sizeof(struct node));

69. if(ptr == NULL)

70. {

71. printf("\nOVERFLOW");

72. }

64
Data Structures and Algorithms Unit – 7

73. else

74. {

75. printf("\nEnter Item value");

76. scanf("%d",&item);

77.

78. if(head==NULL)

79. {

80. ptr->next = NULL;

81. ptr->prev=NULL;

82. ptr->data=item;

83. head=ptr;

84. }

85. else

86. {

87. ptr->data=item;

88. ptr->prev=NULL;

89. ptr->next = head;

90. head->prev=ptr;

91. head=ptr;

92. }

93. printf("\nNode inserted\n");

65
Data Structures and Algorithms Unit – 7

94. }

95.

96. }

97. void insertion_last()

98. {

99. struct node *ptr,*temp;

100. int item;

101. ptr = (struct node *) malloc(sizeof(struct node));

102. if(ptr == NULL)

103. {

104. printf("\nOVERFLOW");

105. }

106. else

107. {

108. printf("\nEnter value");

109. scanf("%d",&item);

110. ptr->data=item;

111. if(head == NULL)

112. {

113. ptr->next = NULL;

114. ptr->prev = NULL;

66
Data Structures and Algorithms Unit – 7

115. head = ptr;

116. }

117. else

118. {

119. temp = head;

120. while(temp->next!=NULL)

121. {

122. temp = temp->next;

123. }

124. temp->next = ptr;

125. ptr ->prev=temp;

126. ptr->next = NULL;

127. }

128.

129. }

130. printf("\nnode inserted\n");

131. }

132. void insertion_specified()

133. {

134. struct node *ptr,*temp;

135. int item,loc,i;

67
Data Structures and Algorithms Unit – 7

136. ptr = (struct node *)malloc(sizeof(struct node));

137. if(ptr == NULL)

138. {

139. printf("\n OVERFLOW");

140. }

141. else

142. {

143. temp=head;

144. printf("Enter the location");

145. scanf("%d",&loc);

146. for(i=0;i<loc;i++)

147. {

148. temp = temp->next;

149. if(temp == NULL)

150. {

151. printf("\n There are less than %d elements", loc);

152. return;

153. }

154. }

155. printf("Enter value");

156. scanf("%d",&item);

68
Data Structures and Algorithms Unit – 7

157. ptr->data = item;

158. ptr->next = temp->next;

159. ptr -> prev = temp;

160. temp->next = ptr;

161. temp->next->prev=ptr;

162. printf("\nnode inserted\n");

163. }

164. }

165. void deletion_beginning()

166. {

167. struct node *ptr;

168. if(head == NULL)

169. {

170. printf("\n UNDERFLOW");

171. }

172. else if(head->next == NULL)

173. {

174. head = NULL;

175. free(head);

176. printf("\nnode deleted\n");

177. }

69
Data Structures and Algorithms Unit – 7

178. else

179. {

180. ptr = head;

181. head = head -> next;

182. head -> prev = NULL;

183. free(ptr);

184. printf("\nnode deleted\n");

185. }

186.

187. }

188. void deletion_last()

189. {

190. struct node *ptr;

191. if(head == NULL)

192. {

193. printf("\n UNDERFLOW");

194. }

195. else if(head->next == NULL)

196. {

197. head = NULL;

198. free(head);

70
Data Structures and Algorithms Unit – 7

199. printf("\nnode deleted\n");

200. }

201. else

202. {

203. ptr = head;

204. if(ptr->next != NULL)

205. {

206. ptr = ptr -> next;

207. }

208. ptr -> prev -> next = NULL;

209. free(ptr);

210. printf("\nnode deleted\n");

211. }

212. }

213. void deletion_specified()

214. {

215. struct node *ptr, *temp;

216. int val;

217. printf("\n Enter the data after which the node is to be deleted : ");

218. scanf("%d", &val);

219. ptr = head;

71
Data Structures and Algorithms Unit – 7

220. while(ptr -> data != val)

221. ptr = ptr -> next;

222. if(ptr -> next == NULL)

223. {

224. printf("\nCan't delete\n");

225. }

226. else if(ptr -> next -> next == NULL)

227. {

228. ptr ->next = NULL;

229. }

230. else

231. {

232. temp = ptr -> next;

233. ptr -> next = temp -> next;

234. temp -> next -> prev = ptr;

235. free(temp);

236. printf("\nnode deleted\n");

237. }

238. }

239. void display()

240. {

72
Data Structures and Algorithms Unit – 7

241. struct node *ptr;

242. printf("\n printing values...\n");

243. ptr = head;

244. while(ptr != NULL)

245. {

246. printf("%d\n",ptr->data);

247. ptr=ptr->next;

248. }

249. }

250. void search()

251. {

252. struct node *ptr;

253. int item,i=0,flag;

254. ptr = head;

255. if(ptr == NULL)

256. {

257. printf("\nEmpty List\n");

258. }

259. else

260. {

261. printf("\nEnter item which you want to search?\n");

73
Data Structures and Algorithms Unit – 7

262. scanf("%d",&item);

263. while (ptr!=NULL)

264. {

265. if(ptr->data == item)

266. {

267. printf("\nitem found at location %d ",i+1);

268. flag=0;

269. break;

270. }

271. else

272. {

273. flag=1;

274. }

275. i++;

276. ptr = ptr -> next;

277. }

278. if(flag==1)

279. {

280. printf("\nItem not found\n");

281. }

282. }

74
Data Structures and Algorithms Unit – 7

283.

284. }

Output

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

printing values...

*********Main Menu*********

75
Data Structures and Algorithms Unit – 7

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

Enter Item value12

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

76
Data Structures and Algorithms Unit – 7

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

Enter Item value123

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

77
Data Structures and Algorithms Unit – 7

8.Show

9.Exit

Enter your choice?

Enter Item value1234

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

78
Data Structures and Algorithms Unit – 7

printing values...

1234

123

12

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

Enter value89

node inserted

79
Data Structures and Algorithms Unit – 7

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

Enter the location1

Enter value12345

node inserted

*********Main Menu*********

Choose one option from the following list ...

80
Data Structures and Algorithms Unit – 7

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

printing values...

1234

123

12345

12

89

*********Main Menu*********

Choose one option from the following list ...

81
Data Structures and Algorithms Unit – 7

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

82
Data Structures and Algorithms Unit – 7

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

83
Data Structures and Algorithms Unit – 7

Enter your choice?

printing values...

123

12345

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

84
Data Structures and Algorithms Unit – 7

Enter the data after which the node is to be deleted : 123

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

printing values...

123

*********Main Menu*********

85
Data Structures and Algorithms Unit – 7

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

Enter item which you want to search?

123

item found at location 1

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

86
Data Structures and Algorithms Unit – 7

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

7.Search

8.Show

9.Exit

Enter your choice?

Enter the data after which the node is to be deleted : 123

Can't delete

*********Main Menu*********

Choose one option from the following list ...

===============================================

1.Insert in begining

2.Insert at last

3.Insert at any random location

4.Delete from Beginning

5.Delete from last

6.Delete the node after the given data

87
Data Structures and Algorithms Unit – 7

7.Search

8.Show

9.Exit

Enter your choice?

Exited..

Circular Singly Linked List

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.

The following image shows a circular singly linked list.

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.

Memory Representation of circular linked list:

In the following image, memory representation of a circular linked list containing


marks of a student in 4 subjects. However, the image shows a glimpse of how the
circular list is being stored in the memory. The start or head of the list is pointing
to the element with the index 1 and containing 13 marks in the data part and 4 in
the next part. Which means that it is linked with the node that is being stored at
4th index of the list.

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.

Operations on Circular Singly linked list:

Insertion
90
Data Structures and Algorithms Unit – 7

SN Operation Description

1 Insertion at Adding a node into circular singly linked list at the


beginning beginning.

2 Insertion at the Adding a node into circular singly linked list at the
end end.

Deletion & Traversing

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.

4 Traversing Visiting each element of the list at least once in order to


perform some specific operation.

Circular Doubly Linked List

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.

A circular doubly linked list is shown in the following figure.

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.

Memory Management of Circular Doubly linked list

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.

Operations on circular doubly linked 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

1 Insertion at Adding a node in circular doubly linked list at the


beginning beginning.

2 Insertion at end Adding a node in circular doubly linked list at the end.

3 Deletion at Removing a node in circular doubly linked list from


beginning beginning.

4 Deletion at end Removing 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

1. Stack is an ordered list in which, insertion and deletion can be performed


only at one end that is called top.

2. Stack is a recursive data structure having pointer to its top element.

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

2. Expression evaluations and conversions

3. Parsing

4. Browsers

94
Data Structures and Algorithms Unit – 7

5. Editors

6. Tree Traversals

Operations on Stack

There are various operations which can be performed on stack.

1. Push : Adding an element onto the stack

95
Data Structures and Algorithms Unit – 7

2. Pop : Removing an element from the stack

3. Peek : Look all the elements of stack without removing them.

How the stack grows?

Scenario 1 : Stack is empty

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

Scenario 2 : Stack is not empty

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.

Scenario 3 : Deletion of an element

Value of top will get decreased by 1 whenever an element is deleted from the
stack.

97
Data Structures and Algorithms Unit – 7

In the following stack, after deleting 10 from the stack, top = 1.

Top and its value :

Top position Status of stack

-1 Empty

0 Only one element in the stack

N-1 Stack is full

N Overflow

Array implementation of Stack

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 onto the stack (push operation)

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.

2. Add element at the position of incremented top. This is referred to as


adding new element at the top of the stack.

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

2. if top = n then stack full

3. top = top + 1

4. stack (top) : = item;

5. end

Time Complexity : o(1)

implementation of push algorithm in C language

1. void push (int val,int n) //n is size of the stack

2. {

3. if (top == n )

99
Data Structures and Algorithms Unit – 7

4. printf("\n Overflow");

5. else

6. {

7. top = top +1;

8. stack[top] = val;

9. }

10. }

Deletion of an element from a stack (Pop operation)

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.

The underflow condition occurs when we try to delete an element from an


already empty stack.

Algorithm :

1. begin

2. if top = 0 then stack empty;

3. item := stack(top);

4. top = top - 1;

5. end;

Time Complexity : o(1)

Implementation of POP algorithm using C language


100
Data Structures and Algorithms Unit – 7

1. int pop ()

2. {

3. if(top == -1)

4. {

5. printf("Underflow");

6. return 0;

7. }

8. else

9. {

10. return stack[top - - ];

11. }

12. }

Visiting each element of the stack (Peek operation)

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 :

PEEK (STACK, TOP)

1. Begin

2. if top = -1 then stack empty

3. item = stack[top]

4. return item
101
Data Structures and Algorithms Unit – 7

5. End

Time complexity: o(n)

Implementation of Peek algorithm in C language

1. int peek()

2. {

3. if (top == -1)

4. {

5. printf("Underflow");

6. return 0;

7. }

8. else

9. {

10. return stack [top];

11. }

12. }

Linked list implementation of stack

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.

In linked list implementation of stack, the nodes are maintained non-contiguously


in the memory. Each node contains a pointer to its immediate successor node in

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.

Adding a node to the stack (Push operation)

Adding a node to the stack is referred to as push operation. Pushing an element


to a stack in linked list implementation is different from that of an array
implementation. In order to push an element onto the stack, the following steps
are involved.

1. Create a node first and allocate memory to it.

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.

Time Complexity : o(1)

104
Data Structures and Algorithms Unit – 7

C implementation :

1. void push ()

2. {

105
Data Structures and Algorithms Unit – 7

3. int val;

4. struct node *ptr =(struct node*)malloc(sizeof(struct node));

5. if(ptr == NULL)

6. {

7. printf("not able to push the element");

8. }

9. else

10. {

11. printf("Enter the value");

12. scanf("%d",&val);

13. if(head==NULL)

14. {

15. ptr->val = val;

16. ptr -> next = NULL;

17. head=ptr;

18. }

19. else

20. {

21. ptr->val = val;

22. ptr->next = head;

23. head=ptr;

106
Data Structures and Algorithms Unit – 7

24.

25. }

26. printf("Item pushed");

27.

28. }

29. }

Deleting a node from the stack (POP operation)

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.

Time Complexity : o(n)

C implementation

1. void pop()

2. {

3. int item;

4. struct node *ptr;


107
Data Structures and Algorithms Unit – 7

5. if (head == NULL)

6. {

7. printf("Underflow");

8. }

9. else

10. {

11. item = head->val;

12. ptr = head;

13. head = head->next;

14. free(ptr);

15. printf("Item popped");

16.

17. }

18. }

Display the nodes (Traversing)

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.

19. Copy the head pointer into a temporary pointer.

20. Move the temporary pointer through all the nodes of the list and
print the value field attached to every node.

Time Complexity : o(n)

108
Data Structures and Algorithms Unit – 7

C Implementation

1. void display()

2. {

3. int i;

4. struct node *ptr;

5. ptr=head;

6. if(ptr == NULL)

7. {

8. printf("Stack is empty\n");

9. }

10. else

11. {

12. printf("Printing Stack elements \n");

13. while(ptr!=NULL)

14. {

15. printf("%d\n",ptr->val);

16. ptr = ptr->next;

17. }

18. }

19. }

Queue

109
Data Structures and Algorithms Unit – 7

1. A queue can be defined as an ordered list which enables insert operations to be


performed at one end called REAR and delete operations to be performed at
another end called FRONT.

2. Queue is referred to be as First In First Out list.

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

Queues are used in operating systems for handling interrupts.

Complexity

Data Time Complexity Space


Structu Complei
re ty

Average Worst Worst

Acces Searc Inserti Deleti Acce Searc Inserti Deleti


s h on on ss h on on

Queue θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)

Array representation of Queue

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

Algorithm to insert any element in a queue

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

o Step 1: IF REAR = MAX - 1


Write OVERFLOW
Go to step
[END OF IF]

o Step 2: IF FRONT = -1 and REAR = -1


SET FRONT = REAR = 0
ELSE

113
Data Structures and Algorithms Unit – 7

SET REAR = REAR + 1


[END OF IF]

o Step 3: Set QUEUE[REAR] = NUM

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. {

9. if(front == -1 && rear == -1)

10. {

11. front = 0;

12. rear = 0;

13. }

14. else

15. {

16. rear = rear + 1;

114
Data Structures and Algorithms Unit – 7

17. }

18. queue[rear]=item;

19. }

20. }

Algorithm to delete an element from the queue

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 1: IF FRONT = -1 or FRONT > REAR


Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]

o Step 2: EXIT

C Function

1. int delete (int queue[], int max, int front, int rear)

2. {

3. int y;

4. if (front == -1 || front > rear)

5.

115
Data Structures and Algorithms Unit – 7

6. {

7. printf("underflow");

8. }

9. else

10. {

11. y = queue[front];

12. if(front == rear)

13. {

14. front = rear = -1;

15. else

16. front = front + 1;

17.

18. }

19. return y;

20. }

21. }

Drawback of array implementation

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).

o Deciding the array size

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.

Linked List implementation of Queue

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.

The storage requirement of linked representation of a queue with n elements is


o(n) while the time requirement for operations is o(1).

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.

The linked representation of queue is shown in the following figure.

Operation on Linked Queue

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.

1. Ptr = (struct node *) malloc (sizeof(struct node));

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.

1. ptr -> data = item;

2. if(front == NULL)

3. {

4. front = ptr;

5. rear = ptr;

6. front -> next = NULL;

7. rear -> next = NULL;

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.

1. rear -> next = ptr;

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 1: Allocate the space for the new node PTR

o Step 2: SET PTR -> DATA = VAL

o Step 3: IF FRONT = NULL


SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]

o Step 4: END

C Function

1. void insert(struct node *ptr, int item; )

2. {

3.

120
Data Structures and Algorithms Unit – 7

4.

5. ptr = (struct node *) malloc (sizeof(struct node));

6. if(ptr == NULL)

7. {

8. printf("\nOVERFLOW\n");

9. return;

10. }

11. else

12. {

13. ptr -> data = item;

14. if(front == NULL)

15. {

16. front = ptr;

17. rear = ptr;

18. front -> next = NULL;

19. rear -> next = NULL;

20. }

21. else

22. {

23. rear -> next = ptr;

24. rear = ptr;

121
Data Structures and Algorithms Unit – 7

25. rear->next = NULL;

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;

2. front = front -> next;

3. free(ptr);

The algorithm and C function is given as follows.

Algorithm

o Step 1: IF FRONT = NULL


Write " Underflow "
Go to Step 5
[END OF IF]

o Step 2: SET PTR = FRONT

o Step 3: SET FRONT = FRONT -> NEXT

122
Data Structures and Algorithms Unit – 7

o Step 4: FREE PTR

o Step 5: END

C Function

1. void delete (struct node *ptr)

2. {

3. if(front == NULL)

4. {

5. printf("\nUNDERFLOW\n");

6. return;

7. }

8. else

9. {

10. ptr = front;

11. front = front -> next;

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

Consider the queue shown in the following figure.

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

Insertion in Circular queue

There are three scenario of inserting an element in a queue.

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.

2. If rear != max - 1, then rear will be incremented to the mod(maxsize) and


the new value will be inserted at the rear end of 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.

Algorithm to insert an element in circular queue

o Step 1: IF (REAR+1)%MAX = FRONT


Write " OVERFLOW "
Goto step 4
[End OF IF]

o Step 2: IF FRONT = -1 and REAR = -1


SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = (REAR + 1) % MAX
[END OF IF]

o Step 3: SET QUEUE[REAR] = VAL

o Step 4: EXIT

C Function

1. void insert(int item, int queue[])

2. {

126
Data Structures and Algorithms Unit – 7

3. if((rear+1)%maxsize == front)

4. {

5. printf("\nOVERFLOW");

6. return;

7. }

8. else if(front == -1 && rear == -1)

9. {

10. front = 0;

11. rear = 0;

12. }

13. else if(rear == maxsize -1 && front != 0)

14. {

15. rear = 0;

16. }

17. else

18. {

19. rear = (rear+1)%maxsize;

20. }

21. queue[rear] = item;

22. }

Algorithm to delete an element from a circular queue

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.

4. Otherwise, the value of front is incremented by 1 and then delete the


element at the front end.

Algorithm

o Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]

o Step 2: SET VAL = QUEUE[FRONT]

o Step 3: IF FRONT = REAR


SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[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 Nodes of a tree either maintain a parent-child relationship between them


or they are sister nodes.

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 Ancestor node :- An ancestor of a node is any predecessor node on a path


from root to that node. The root node doesn't have any ancestors. In the
tree shown in the above image, the node F have the ancestors, B and A.

o Degree :- Degree of a node is equal to number of children, a node have. In


the tree shown in the above image, the degree of node B is 2. Degree of a
leaf node is always 0 while in a complete binary tree, degree of each node
is equal to 2.

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.

Static representation of tree

1. #define MAXNODE 500

2. struct treenode {

3. int root;

4. int father;

130
Data Structures and Algorithms Unit – 7

5. int son;

6. int next;

7. }

Dynamic representation of tree

1. struct treenode

2. {

3. int root;

4. struct treenode *father;

5. struct treenode *son

6. struct treenode *next;

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

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

Expression trees are used to evaluate the simple arithmetic expressions.


Expression tree is basically a binary tree where internal nodes are represented by
operators while the leaf nodes are represented by operands. Expression trees are
widely used to solve algebraic expressions like (a+b)*(a-b). Consider the following
example.

Q. Construct an expression tree by using the following algebraic expression.

(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.

1. Root of the node

2. left sub-tree which is also a binary tree.

3. Right binary sub-tree

A binary Tree is shown in the following image.

Types of Binary Tree

1. Strictly Binary Tree

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 strictly binary tree is shown in the following figure.

2. Complete Binary Tree

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

Binary Tree Traversal

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.

Binary Tree representation

There are two types of representation of a binary tree:

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.

Consider the binary tree given in the figure below.

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.

Binary Search Tree

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.

Advantages of using binary search 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.

2. The binary search tree is considered as efficient data structure in compare


to arrays and linked lists. In searching process, it removes half sub-tree at
every step. Searching for an element in a binary search tree takes o(log 2n)
time. In worst case, the time it takes to search an element is 0(n).

3. It also speed up the insertion and deletion operations as compare to that in


array and linked list.

Q. Create the binary search tree using the following data elements.

43, 10, 79, 90, 12, 54, 11, 9, 50

1. Insert 43 into the tree as the root of the tree.

2. Read the next element, if it is lesser than the root node element, insert it as
the root of the left sub-tree.

3. Otherwise, insert it as the root of the right of the right 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

Operations on Binary Search Tree

There are many operations which can be performed on a binary search tree.

SN Operation Description

1 Searching in Finding the location of some specific element in a


BST binary search tree.

2 Insertion in Adding a new element to the binary search tree at the


BST appropriate location so that the property of BST do not
violate.

3 Deletion in Deleting some specific node from a binary search tree.


BST However, there can be various cases in deletion
depending upon the number of children, the node
have.

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

Tree is said to be balanced if balance factor of each node is in between -1 to 1,


otherwise, the tree will be unbalanced and need to be balanced.

Balance Factor (k) = height (left(k)) - height (right(k))

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

Algorithm Average case Worst case

Space o(n) o(n)

Search o(log n) o(log n)

Insert o(log n) o(log n)

Delete o(log n) o(log n)

Operations on AVL tree

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

1 Insertion Insertion in AVL tree is performed in the same way as it is


performed in a binary search tree. However, it may lead to
violation in the AVL tree property and therefore the tree
may need balancing. The tree can be balanced by applying
rotations.

146
Data Structures and Algorithms Unit – 7

2 Deletion Deletion can also be performed in the same way as it is


performed in a binary search tree. Deletion may also disturb
the balance of the tree therefore, various types of rotations
are used to rebalance the tree.

Why AVL Tree ?

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.

A B tree of order m contains all the properties of an M way tree. In addition, it


contains the following properties.

1. Every node in a B-Tree contains at most m children.

2. Every node in a B-Tree except the root node and the leaf node contain at
least m/2 children.

3. The root nodes must have at least 2 nodes.

4. All leaf nodes must be at the same level.

It is not necessary that, all the nodes contain the same number of children but,
each node must have m/2 number of nodes.

A B tree of order 4 is shown in the following image.


147
Data Structures and Algorithms Unit – 7

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 :

Searching in B Trees is similar to that in Binary search tree. For example, if we


search for an item 49 in the following B Tree. The process will something like
following :

1. Compare item 49 with root node 78. since 49 < 78 hence, move to its left
sub-tree.

2. Since, 40<49<56, traverse right sub-tree of 40.

3. 49>45, move to right. Compare 49.

4. match found, return.

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 Insert the new element in the increasing order of elements.

o Split the node into the two nodes at the median.

o Push the median element upto its parent node.

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

8 will be inserted to the right of 5, therefore insert 8.

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.

1. Locate the leaf node.

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.

53 is present in the right child of element 49. Delete it.

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.

The final B tree is shown as follows.

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.

Searching an un-indexed and unsorted database containing n key values needs


O(n) running time in worst case. However, if we use B Tree to index this database,
it will be searched in O(log n) time in worst case.

B+ Tree

B+ Tree is an extension of B Tree which allows efficient insertion, deletion and


search operations.

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

1. Records can be fetched in equal number of disk accesses.

2. Height of the tree remains balanced and less as compare to B tree.

3. We can access the data stored in a B+ tree sequentially as well as directly.

4. Keys are used for indexing.

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

1 Search keys can not be repeatedly Redundant search keys can be


stored. present.

2 Data can be stored in leaf nodes as Data can only be stored on the
well as internal nodes leaf nodes.

3 Searching for some data is a slower Searching is comparatively faster


process since data can be found on as data can only be found on the
internal nodes as well as on the leaf nodes.
leaf nodes.

4 Deletion of internal nodes are so Deletion will never be a


complicated and time consuming. complexed process since element
will always be deleted from the
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 1: Insert the new node as a leaf node

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 1: Delete the key and data from the leaves.

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.

Now, the height of B+ tree will be decreased by 1.

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.

Directed and Undirected Graph

A graph can be directed or undirected. However, in an undirected graph, edges


are not associated with the directions with them. An undirected graph is shown in
the above figure since its edges are not attached with any of the directions. If an
edge exists between vertex A and B then the vertices can be traversed from B to A
as well as A to B.

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.

A directed graph is shown in the following figure.

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.

Degree of the Node

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

By Graph representation, we simply mean the technique which is to be used in


order to store some graph into the computer's memory.

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 sequential representation, we use adjacency matrix to store the mapping


represented by vertices and edges. In adjacency matrix, the rows and columns are
represented by the graph vertices. A graph having n vertices, will have a
dimension n x n.

An entry Mij in the adjacency matrix representation of an undirected graph G will


be 1 if there exists an edge between Vi and Vj.

An undirected graph and its adjacency matrix representation is shown in the


following figure.

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.

Representation of weighted directed graph is different. Instead of filling the entry


by 1, the Non- zero entries of the adjacency matrix are represented by the weight
of respective edges.

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

Graph Traversal Algorithm

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.

o Breadth First Search

o Depth First Search

Breadth First Search (BFS) Algorithm

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 1: SET STATUS = 1 (ready state)


for each node in G

o Step 2: Enqueue the starting node A


and set its STATUS = 2
(waiting state)

o Step 3: Repeat Steps 4 and 5 until


QUEUE is empty

o Step 4: Dequeue a node N. Process it


and set its STATUS = 3
(processed state).

o Step 5: Enqueue 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 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.

Lets start examining the graph from Node A.

1. Add A to QUEUE1 and NULL to QUEUE2.

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}

Now, backtrack from E to A, using the nodes available in QUEUE2.

171
Data Structures and Algorithms Unit – 7

The minimum path will be A → B → C → E.

Depth First Search (DFS) Algorithm

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 1: SET STATUS = 1 (ready state) for each node in G

o Step 2: Push the starting node A on the stack and set its STATUS = 2
(waiting state)

o Step 3: Repeat Steps 4 and 5 until STACK is empty

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 :

Push H onto the stack

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

The printing sequence of the graph will be :

1. H → A → D → F → B → C → G → E

Spanning Tree

Spanning tree can be defined as a sub-graph of connected, undirected


graph G that is a tree produced by removing the desired number of edges
from a graph. In other words, Spanning tree is a non-cyclic sub-graph of a
connected and undirected graph G that connects all the vertices together.
A graph G can have multiple spanning trees.

Minimum Spanning Tree

There can be weights assigned to every edge in a weighted graph. However,


A minimum spanning tree is a spanning tree which has minimal total
weight. In other words, minimum spanning tree is the one which contains
the least weight among all other spanning tree of some particular graph.

Shortest path algorithms

In this section, algorithms will be discussed to calculate the shortest path


between two nodes in a graph.

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.

The algorithm is given as follows.

175
Data Structures and Algorithms Unit – 7

Algorithm

o Step 1: Select a starting vertex

o Step 2: Repeat Steps 3 and 4 until there are fringe vertices

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 :

Construct a minimum spanning tree of the graph given in the following


figure by using prim's algorithm.

Solution

o Step 1 : Choose a starting vertex B.

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.

The cost of MST will be calculated as;

cost(MST) = 4 + 2 + 1 + 3 = 10 units.

Kruskal's Algorithm

Kruskal's Algorithm is used to find the minimum spanning tree for a


connected weighted graph. The main target of the algorithm is to find the
subset of edges by using which, we can traverse every vertex of the graph.
177
Data Structures and Algorithms Unit – 7

Kruskal's algorithm follows greedy approach which finds an optimum


solution at every stage instead of focusing on a global optimum.

The Kruskal's algorithm is given as follows.

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 3: Repeat Steps 4 and 5 while Q is NOT EMPTY

o Step 4: Remove an edge from Q

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 :

Apply the Kruskal's algorithm on the graph given as follows.

Solution:
178
Data Structures and Algorithms Unit – 7

the weight of the edges given as :

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

Sort the edges according to their weights.

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

Start constructing the tree;

Add AB to the MST;

179
Data Structures and Algorithms Unit – 7

Add DE to the MST;

Add BC to the MST;

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.

the cost of MST = 1 + 2 + 3 + 4 = 10.

Sorting Algorithms

A Sorting Algorithm is used to rearrange a given array or list elements according


to a comparison operator on the elements. The comparison operator is used to
decide the new order of element in the respective data structure.

Sorting refers to arranging data in a particular format. Sorting algorithm specifies


the way to arrange data in a particular order. Most common orders are in
numerical or lexicographical order.

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

 Telephone Directory − The telephone directory stores the telephone


numbers of people sorted by their names, so that the names can be
searched easily.

 Dictionary − The dictionary stores words in an alphabetical order so that


searching of any word becomes easy.

In-place Sorting and Not-in-place Sorting

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.

Stable and Not Stable 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

Stability of an algorithm matters when we wish to maintain the sequence of


original elements, like in a tuple for example.

Adaptive and Non-Adaptive Sorting Algorithm

A sorting algorithm is said to be adaptive, if it takes advantage of already 'sorted'


elements in the list that is to be sorted. That is, while sorting if the source list has
some element already sorted, adaptive algorithms will take this into account and
will try not to re-order them.

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

A sequence of values is said to be in increasing order, if the successive element is


greater than the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order,
as every next element is greater than the previous element.

Decreasing Order

183
Data Structures and Algorithms Unit – 7

A sequence of values is said to be in decreasing order, if the successive element is


less than the current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as
every next element is less than the previous element.

Non-Increasing Order

A sequence of values is said to be in non-increasing order, if the successive


element is less than or equal to its previous element in the sequence. This order
occurs when the sequence contains duplicate values. For example, 9, 8, 6, 3, 3, 1
are in non-increasing order, as every next element is less than or equal to (in case
of 3) but not greater than any previous element.

Non-Decreasing Order

A sequence of values is said to be in non-decreasing order, if the successive


element is greater than or equal to its previous element in the sequence. This
order occurs when the sequence contains duplicate values. For example, 1, 3, 3,
6, 8, 9 are in non-decreasing order, as every next element is greater than or equal
to (in case of 3) but not less than the previous one.

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.

How Bubble Sort Works?

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.

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

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.

Now we should look into some practical aspects of bubble sort.

Algorithm

We assume list is an array of n elements. We further assume that swap function


swaps the values of the given array elements.

begin BubbleSort(list)

for all elements of list

if list[i] > list[i+1]

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.

Pseudocode of BubbleSort algorithm can be written as follows −

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:

swapped = false

for j = 0 to loop-1 do:

/* compare the adjacent elements */

if list[j] > list[j+1] then

/* swap them */

swap( list[j], list[j+1] )

swapped = true
187
Data Structures and Algorithms Unit – 7

end if

end for

/*if no number was swapped that means

array is sorted now, break the loop.*/

if(not swapped) then

break

end if

end for

end procedure return list

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.

Ensure that, a[loc] is less than a[right].

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.

3. Set, loc = right

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.

3. Set, loc = left.

Complexity

Complexity Best Case Average Worst


Case Case

Time O(n) for 3 way partition or O(n O(n log n) O(n2)


Complexity log n) simple partition

Space O(log n)
Complexity

189
Data Structures and Algorithms Unit – 7

Algorithm

PARTITION (ARR, BEG, END, LOC)

o Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =

o Step 2: Repeat Steps 3 to 6 while FLAG =

o Step 3: Repeat while ARR[LOC] <=ARR[RIGHT]


AND LOC != RIGHT
SET RIGHT = RIGHT - 1
[END OF LOOP]

o Step 4: IF LOC = RIGHT


SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]

o Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]

o Step 6:IF LOC = LEFT


SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT]
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]

o Step 7: [END OF LOOP]

o Step 8: END
190
Data Structures and Algorithms Unit – 7

QUICK_SORT (ARR, BEG, END)

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.

Ensure that, a[loc] is less than a[right].

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.

3. Set, loc = right

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.

3. Set, loc = left.

Complexity

191
Data Structures and Algorithms Unit – 7

Complexity Best Case

Time Complexity O(n) for 3 way partition or O(n log n) simple partition

Space Complexity

Algorithm

PARTITION (ARR, BEG, END, LOC)

o Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =

o Step 2: Repeat Steps 3 to 6 while FLAG =

o Step 3: Repeat while ARR[LOC] <=ARR[RIGHT]


AND LOC != RIGHT
SET RIGHT = RIGHT - 1
[END OF LOOP]

o Step 4: IF LOC = RIGHT


SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]

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 6:IF LOC = LEFT


SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT]
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]

o Step 7: [END OF LOOP]

o Step 8: END

QUICK_SORT (ARR, BEG, 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.

The heap sort basically recursively performs two main operations.

o Build a heap H, using the elements of ARR.

o Repeatedly delete the root element of the heap formed in phase 1.

Complexity

Complexity Best Case Average Case Worst case

Time Complexity Ω(n log (n)) θ(n log (n)) O(n log (n))

193
Data Structures and Algorithms Unit – 7

Space Complexity O(1)

Algorithm

HEAP_SORT(ARR, N)

o Step 1: [Build Heap H]


Repeat for i=0 to N-1
CALL INSERT_HEAP(ARR, N, ARR[i])
[END OF LOOP]

o Step 2: Repeatedly Delete the root element


Repeat while N > 0
CALL Delete_Heap(ARR,N,VAL)
SET N = N+1
[END OF LOOP]

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

Complexity Best Case Average Case Worst Case

Time Complexity Ω(n+k) θ(nk) O(nk)

Space Complexity O(n+k)

Example

Consider the array of length 6 given below. Sort the array by using Radix sort.

A = {10, 2, 901, 803, 1024}

Pass 1: (Sort the list according to the digits at 0's place)

10, 901, 2, 803, 1024.

Pass 2: (Sort the list according to the digits at 10's place)

02, 10, 901, 803, 1024

Pass 3: (Sort the list according to the digits at 100's place)

02, 10, 1024, 803, 901.

Pass 4: (Sort the list according to the digits at 1000's place)

02, 10, 803, 901, 1024

Therefore, the list generated in the step 4 is the sorted list, arranged from radix
sort.
195
Data Structures and Algorithms Unit – 7

Algorithm

o Step 1:Find the largest number in ARR as LARGE

o Step 2: [INITIALIZE] SET NOP = Number of digits


in LARGE

o Step 3: SET PASS =0

o Step 4: Repeat Step 5 while PASS <= NOP-1

o Step 5: SET I = 0 and INITIALIZE buckets

o Step 6:Repeat Steps 7 to 9 while I<n-1< li=""></n-1<>

o Step 7: SET DIGIT = digit at PASSth place in A[I]

o Step 8: Add A[I] to the bucket numbered DIGIT

o Step 9: INCREMENT bucket count for bucket


numbered DIGIT
[END OF LOOP]

o Step 10: Collect the numbers in the bucket


[END OF LOOP]

o Step 11: END

Insertion Sort Algorithm

Sorting is the process of arranging a list of elements in a particular order


(Ascending or Descending).

Insertion sort algorithm arranges a list of elements in a particular order. In


insertion sort algorithm, every iteration moves an element from unsorted portion
to sorted portion until all the elements are sorted in the list.

196
Data Structures and Algorithms Unit – 7

Step by Step Process

The insertion sort algorithm is performed using the following steps...

 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.

Following is the sample code for insertion sort...

Insertion Sort Logic

//Insertion sort logic

for i = 1 to size-1 {

temp = list[i];

j = i-1;

while ((temp < list[j]) && (j > 0)) {

list[j] = list[j-1];

j = j - 1;

list[j] = temp;

Example

197
Data Structures and Algorithms Unit – 7

Complexity of the Insertion Sort Algorithm

198
Data Structures and Algorithms Unit – 7

To sort an unsorted list with 'n' number of elements, we need to


make (1+2+3+......+n-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.

Worst Case : O(n2)


Best Case : Ω(n)
Average Case : Θ(n2)

Implementaion of Insertion Sort Algorithm using C Programming Language

#include<stdio.h>

#include<conio.h>

void main(){

int size, i, j, temp, list[100];

printf("Enter the size of the list: ");

scanf("%d", &size);

printf("Enter %d integer values: ", size);

for (i = 0; i < size; i++)

scanf("%d", &list[i]);

//Insertion sort logic

199
Data Structures and Algorithms Unit – 7

for (i = 1; i < size; i++) {

temp = list[i];

j = i - 1;

while ((temp < list[j]) && (j >= 0)) {

list[j + 1] = list[j];

j = j - 1;

list[j + 1] = temp;

printf("List after Sorting is: ");

for (i = 0; i < size; i++)

printf(" %d", list[i]);

getch();

Output

200
Data Structures and Algorithms Unit – 7

Selection Sort Algorithm

Selection Sort algorithm is used to arrange a list of elements in a particular order


(Ascending or Descending). In selection sort, the first element in the list is
selected and it is compared repeatedly with all the remaining elements in the list.
If any element is smaller than the selected element (for Ascending order), then
both are swapped so that first position is filled with the smallest element in the
sorted order. Next, we select the element at a second position in the list and it is
compared with all the remaining elements in the list. If any element is smaller
than the selected element, then both are swapped. This procedure is repeated
until the entire list is sorted.

Step by Step Process

The selection sort algorithm is performed using the following steps...

 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 3: In every comparision, if any element is found smaller than the


selected element (for Ascending order), then both are swapped.

 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

Following is the sample code for selection sort...

Selection Sort Logic

//Selection sort logic

for(i=0; i<size; i++){

for(j=i+1; j<size; j++){

if(list[i] > list[j])

temp=list[i];

list[i]=list[j];

list[j]=temp;

Example

202
Data Structures and Algorithms Unit – 7

Complexity of the Selection Sort Algorithm

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.

Worst Case : O(n2)


Best Case : Ω(n2)
Average Case : Θ(n2)

Implementaion of Selection Sort Algorithm using C Programming Language

#include<stdio.h>

#include<conio.h>

void main(){

int size,i,j,temp,list[100];

clrscr();

printf("Enter the size of the List: ");

scanf("%d",&size);

printf("Enter %d integer values: ",size);

for(i=0; i<size; i++)

scanf("%d",&list[i]);

204
Data Structures and Algorithms Unit – 7

//Selection sort logic

for(i=0; i<size; i++){

for(j=i+1; j<size; j++){

if(list[i] > list[j])

temp=list[i];

list[i]=list[j];

list[j]=temp;

printf("List after sorting is: ");

for(i=0; i<size; i++)

printf(" %d",list[i]);

getch();

Output

205
Data Structures and Algorithms Unit – 7

Divide and Conquer

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.

an example, Divide and Rule.

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.

In Merge Sort, the given unsorted array with n elements, is divided


into n subarrays, each having one element, because a single element is always

206
Data Structures and Algorithms Unit – 7

sorted in itself. Then, it repeatedly merges these subarrays, to produce new


sorted subarrays, and in the end, one complete sorted array is produced.

The concept of Divide and Conquer involves three steps:

1. Divide the problem into multiple small problems.

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

Hash Table is a data structure which stores data in an associative manner. In a


hash table, data is stored in an array format, where each data value has its own
unique index value. Access of data becomes very fast if we know the index of the
desired data.

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

Hashing is a technique to convert a range of key values into a range of indexes of


an array. We're going to use modulo operator to get a range of key values.
Consider an example of hash table of size 20, and the following items are to be
stored. Item are in the (key,value) format.

 (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)

Sr.No. Key Hash

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

Sr.No. Key Hash Array Index

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

Following are the basic primary operations of a hash table.

 Search − Searches an element in a hash table.

 Insert − inserts an element in a hash table.

210
Data Structures and Algorithms Unit – 7

 delete − Deletes an element from a hash table.

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.

int hashCode(int key){

return key % SIZE;

Search Operation

Whenever an element is to be searched, compute the hash code of the key


passed and locate the element using that hash code as index in the array. Use
linear probing to get the element ahead if the element is not found at the
computed hash code.

Example

struct DataItem *search(int key) {


//get the hash
int hashIndex = hashCode(key);
//move in array until an empty

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

void insert(int key,int data) {


struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
item->data = data;
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
Delete Operation

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

struct DataItem* delete(struct DataItem* item) {


int key = item->key;
//get the hash
int hashIndex = hashCode(key)
//move in array until an empty
while(hashArray[hashIndex] !=NULL) {
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
Performance Analysis

What is Performance Analysis of an algorithm?

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...

Performance of an algorithm is a process of making evaluative judgement


about algorithms.
It can also be defined as follows...

Performance of an algorithm means predicting the resources which are


required to an algorithm to perform its task.
That means when we have multiple algorithms to solve a problem, we need to
select a suitable algorithm to solve that problem.
We compare algorithms with each other which are solving the same problem, to
select the best algorithm. To compare algorithms, we use a set of parameters or
set of elements like memory required by that algorithm, the execution speed of
that algorithm, easy to understand, easy to implement, etc.,

Generally, the performance of an algorithm depends on the following elements...

1. Whether that algorithm is providing the exact solution for the problem?

2. Whether it is easy to understand?

3. Whether it is easy to implement?

4. How much space (memory) it requires to solve the problem?

5. How much time it takes to solve the problem? Etc.,

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

Based on this information, performance analysis of an algorithm can also be


defined as follows...

Performance analysis of an algorithm is the process of calculating space and


time required by that algorithm.

Performance analysis of an algorithm is performed by using the following


measures...

1. Space required to complete the task of that algorithm (Space Complexity).


It includes program space and data space

2. Time required to complete the task of that algorithm (Time Complexity)

Recurrences

The recursive nature of D&C leads to recurrences, or functions defined in terms


of:

 one or more base cases, and

 itself, with smaller arguments.

Solving Recurrence Equations

A recurrence is an equation or inequality that describes a function in terms of its


value on smaller inputs. Recurrences are generally used in divide-and-conquer
paradigm.

consider T(n) to be the running time on a problem of size n.

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

A recurrence relation can be solved using the following methods −

 Substitution Method − In this method, we guess a bound and using


mathematical induction we prove that our assumption was correct.

 Recursion Tree Method − In this method, a recurrence tree is formed


where each node represents the cost.

 Master’s Theorem − This is another important technique to find the


complexity of a recurrence relation.

Time Complexity of Algorithms

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

Or, we can simply use a mathematical operator * to find the square.

/*
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.

What is Time Complexity?

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.

Time Complexity is most commonly estimated by counting the number of


elementary steps performed by any algorithm to finish execution. Like in the
example above, for the first code the loop will run n number of times, so the time
complexity will be n atleast and as the value of n will increase the time taken will
also increase. While for the second code, time complexity is constant, because it
will never be dependent on the value of n, it will always give the result in 1 step.

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.

Calculating Time Complexity

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.

for(i=0; i < N; i++)


{
statement;
}
The time complexity for the above algorithm will be Linear. The running time of
the loop is directly proportional to N. When N doubles, so does the running time.

for(i=0; i < N; i++)


{
for(j=0; j < N;j++)
{
statement;
}
}
This time, the time complexity for the above code will be Quadratic. The running
time of the two loops is proportional to the square of N. When N doubles, the
running time increases by N * N.

218
Data Structures and Algorithms Unit – 7

while(low <= high)


{
mid = (low + high) / 2;
if (target < list[mid])
high = mid - 1;
else if (target > list[mid])
low = mid + 1;
else break;
}

This is an algorithm to break a set of numbers into halves, to search a particular


field(we will study this in detail later). Now, this algorithm will have
a Logarithmic Time Complexity. The running time of the algorithm is proportional
to the number of times N can be divided by 2(N is high-low here). This is because
the algorithm divides the working area in half with each iteration.

void quicksort(int list[], int left, int right)


{
int pivot = partition(list, left, right);
quicksort(list, left, pivot - 1);
quicksort(list, pivot + 1, right);
}
Taking the previous algorithm forward, above we have a small logic of Quick
Sort(we will study this in detail later). Now in Quick Sort, we divide the list into
halves every time, but we repeat the iteration N times(where N is the size of list).
Hence time complexity will be N*log( N ). The running time consists of N loops
(iterative or recursive) that are logarithmic, thus the algorithm is a combination of
linear and logarithmic.

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.

Types of Notations for Time Complexity

219
Data Structures and Algorithms Unit – 7

Now we will discuss and understand the various notations used for Time
Complexity.

1. Big Oh denotes "fewer than or the same as" <expression> iterations.

2. Big Omega denotes "more than or the same as" <expression> iterations.

3. Big Theta denotes "the same as" <expression> iterations.

4. Little Oh denotes "fewer than" <expression> iterations.

5. Little Omega denotes "more than" <expression> iterations.

Understanding Notations of Time Complexity with Example

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.

Suppose you've calculated that an algorithm takes f(n) operations, where,

f(n) = 3*n^2 + 2*n + 4. // n^2 means square of n


Since this polynomial grows at the same rate as n2, then you could say that the
function f lies in the set Theta(n2). (It also lies in the sets O(n2) and Omega(n2) for
the same reason.)

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

Space Complexity of Algorithms

Whenever a solution to a problem is written some memory is required to


complete. For any algorithm memory may be used for the following:

1. Variables (This include the constant values, temporary values)

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.

Space Complexity = Auxiliary Space + Input space

Memory Usage while Execution

While executing, algorithm uses memory space for three reasons:

1. Instruction Space

It's the amount of memory used to save the compiled version of instructions.

2. Environmental Stack

Sometimes an algorithm(function) may be called inside another


algorithm(function). In such a situation, the current variables are pushed onto the
system stack, where they wait for further execution and then the call to the inside
algorithm(function) is made.

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

Amount of space used by the variables and constants.

But while calculating the Space Complexity of any algorithm, we usually consider
only Data Space and we neglect the Instruction Space and Environmental Stack.

Calculating the Space Complexity

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

bool, char, unsigned char, signed char, __int8 1 byte

__int16, short, unsigned short, wchar_t, __wchar_t 2 bytes

float, __int32, int, unsigned int, long, unsigned long 4 bytes

double, __int64, long double, long long 8 bytes

how to compute space complexity by taking a few examples:

{
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.

another example, this time a bit complex one,

// n is the length of array a[]


int sum(int a[], int n)
{
int x = 0; // 4 bytes for x
for(int i = 0; i < n; i++) // 4 bytes for i
{
x = x + a[i];
}
return(x);
}
 In the above code, 4*n bytes of space is required for the array a[] elements.

 4 bytes each for x, n, i and the return value.

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

space required by the algorithm, instead we express it using some standard


notations, also known as Asymptotic Notations.

When we analyse any algorithm, we generally get a formula to represent the


amount of time required for execution or the time required by the computer to
run the lines of code of the algorithm, number of memory accesses, number of
comparisons, temporary variables occupying memory space etc. This formula
often contains unimportant details that don't really tell us anything about the
running time.

an example, if some algorithm has a time complexity of T(n) = (n 2 + 3n + 4), which


is a quadratic equation. For large values of n, the 3n + 4 part will become
insignificant compared to the n2 part.

For n = 1000, n2 will be 1000000 while 3n + 4 will be 3004.

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

What is Asymptotic Behaviour

The word Asymptotic means approaching a value or curve arbitrarily closely (i.e.,
as some sort of limit is taken).

Remember studying about Limits in High School, this is the same.

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.

an example to understand this:

If we have two algorithms with the following expressions representing the time
required by them for execution, then:

Expression 1: (20n2 + 3n - 4)

Expression 2: (n3 + 100n - 2)

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.

Types of Asymptotic Notations

We use three types of asymptotic notations to represent the growth of any


algorithm, as input increases:

1. Big Theta (Θ)

2. Big Oh(O)

3. Big Omega (Ω)

Tight Bounds: Theta

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

Upper Bounds: Big-O

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.

Consider Linear Search algorithm, in which we traverse an array elements, one by


one to search a given number.

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.

Lower Bounds: Omega

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.

Different types of recurrence relations and their solutions

Type 1: Divide and conquer recurrence relations –


Following are some of the examples of recurrence relations based on divide and
conquer.

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).

Type 2: Linear recurrence relations –


Following are some of the examples of recurrence relations based on linear
recurrence relation.
228
Data Structures and Algorithms Unit – 7

T(n) = T(n-1) + n for n>0 and T(0) = 1

These types of recurrence relations can be easily solved using substitution


method.
For example,

T(n) = T(n-1) + n

= T(n-2) + (n-1) + n

= T(n-k) + (n-(k-1))….. (n-1) + n

Substituting k = n, we get

T(n) = T(0) + 1 + 2+….. +n = n(n+1)/2 = O(n^2)

Type 3: Value substitution before solving –


Sometimes, recurrence relations can’t be directly solved using techniques like
substitution, recurrence tree or master method. Therefore, we need to convert
the recurrence relation into appropriate form before solving. For example,

T(n) = T(√n) + 1

To solve this type of recurrence, substitute n = 2^m as:

T(2^m) = T(2^m /2) + 1

Let T(2^m) = S(m),

S(m) = S(m/2) + 1

Solving by master method, we get

S(m) = Θ(logm)

As n = 2^m or m = log2(n),

T(n) = T(2^m) = S(m) = Θ(logm) = Θ(loglogn)

some questions based on the approaches discussed.


229
Data Structures and Algorithms Unit – 7

Que – 1. What is the time complexity of Tower of Hanoi problem?


(A) T(n) = O(sqrt(n))
(D) T(n) = O(n^2)
(C) T(n) = O(2^n)
(D) None

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(2T(n-2)+ c) + c = 2^2*T(n-2) + (c + 2c)

= 2^k*T(n-k) + (c + 2c + .. kc)

Substituting k = (n-1), we get

T(n) = 2^(n-1)*T(1) + (c + 2c + (n-1)c) = O(2^n)

Que – 2. Consider the following recurrence:


T(n) = 2 * T(ceil (sqrt(n) ) ) + 1, T(1) = 1
Which one of the following is true?
(A) T(n) = (loglogn)
(B) T(n) = (logn)
(C) T(n) = (sqrt(n))
(D) T(n) = (n)

Solution: To solve this type of recurrence, substitute n = 2^m as:

T(2^m) = 2T(2^m /2) + 1

Let T(2^m) = S(m),

S(m) = 2S(m/2) + 1

Solving by master method, we get

S(m) = Θ(m)

230
Data Structures and Algorithms Unit – 7

As n = 2^m or m = log2n,

T(n) = T(2^m) = S(m) = Θ(m) = Θ(logn)

Algorithm Design Techniques

The following is a list of several popular design approaches:

1. Divide and Conquer Approach: It is a top-down approach. The algorithms


which follow the divide & conquer techniques involve three steps:

o Divide the original problem into a set of subproblems.

o Solve every subproblem individually, recursively.

o Combine the solution of the subproblems (top level) into a solution of the
whole original problem.

2. Greedy Technique: Greedy method is used to solve the optimization problem.


An optimization problem is one in which we are given a set of input values, which
are required either to be maximized or minimized (known as objective), i.e. some
constraints or conditions.

o Greedy Algorithm always makes the choice (greedy criteria) looks best at
the moment, to optimize a given objective.

o The greedy algorithm doesn't always guarantee the optimal solution


however it generally produces a solution that is very close in value to the
optimal.

3. Dynamic Programming: Dynamic Programming is a bottom-up approach we


solve all possible small problems and then combine them to obtain solutions for
bigger problems.

This is particularly helpful when the number of copying subproblems is


exponentially large. Dynamic Programming is frequently related to Optimization
Problems.

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.

5. Randomized Algorithms: A randomized algorithm is defined as an algorithm


that is allowed to access a source of independent, unbiased random bits, and it is
then allowed to use these random bits to influence its computation.

6. Backtracking Algorithm: Backtracking Algorithm tries each possibility until they


find the right one. It is a depth-first search of the set of possible solution. During
the search, if an alternative doesn't work, then backtrack to the choice point, the
place which presented different alternatives, and tries the next alternative.

7. Randomized Algorithm: A randomized algorithm uses a random number at


least once during the computation make a decision.

Example 1: In Quick Sort, using a random number to choose a pivot.

Example 2: Trying to factor a large number by choosing a random number as


possible divisors.

Loop invariants

This is a justification technique. We use loop invariant that helps us to understand


why an algorithm is correct. To prove statement S about a loop is correct, define S
concerning series of smaller statement S0 S1....Sk where,

o The initial claim so is true before the loop begins.

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

o The final statement Sk implies the statement S that we wish to justify as


being true.

Divide and Conquer Approach


Many algorithms are recursive in nature to solve a given problem recursively
dealing with sub-problems.

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.

Generally, divide-and-conquer algorithms have three parts −

 Divide the problem into a number of sub-problems that are smaller


instances of the same problem.

 Conquer the sub-problems by solving them recursively. If they are small


enough, solve the sub-problems as base cases.

 Combine the solutions to the sub-problems into the solution for the
original problem.

Pros and cons of Divide and Conquer Approach

Divide and conquer approach supports parallelism as sub-problems are


independent. Hence, an algorithm, which is designed using this technique, can
run on the multiprocessor system or in different machines simultaneously.

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.

Application of Divide and Conquer Approach

233
Data Structures and Algorithms Unit – 7

Following are some problems, which are solved using divide and conquer
approach.

 Finding the maximum and minimum of a sequence of numbers

 Strassen’s matrix multiplication

 Merge sort

 Binary search

Dynamic Programming

Dynamic Programming is mainly an optimization over plain recursion. Wherever


we see a recursive solution that has repeated calls for same inputs, we can
optimize it using Dynamic Programming. The idea is to simply store the results of
subproblems, so that we do not have to re-compute them when needed later.
This simple optimization reduces time complexities from exponential to
polynomial. For example, if we write simple recursive solution for Fibonacci
Numbers, we get exponential time complexity and if we optimize it by storing
solutions of subproblems, time complexity reduces to linear.

234
Data Structures and Algorithms Unit – 7

Greedy Algorithms

An algorithm is designed to achieve optimum solution for a given problem. In


greedy algorithm approach, decisions are made from the given solution domain.
As being greedy, the closest solution that seems to provide an optimum solution
is chosen.

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 −

 1 − Select one ₹ 10 coin, the remaining count is 8

 2 − Then select one ₹ 5 coin, the remaining count is 3

 3 − Then select one ₹ 2 coin, the remaining count is 1

 4 − And finally, the selection of one ₹ 1 coins solves the problem

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 −

 Travelling Salesman Problem

 Prim's Minimal Spanning Tree Algorithm

 Kruskal's Minimal Spanning Tree Algorithm

 Dijkstra's Minimal Spanning Tree Algorithm

 Graph - Map Coloring

 Graph - Vertex Cover

 Knapsack Problem

 Job Scheduling Problem

There are lots of similar problems that uses the greedy approach to find an
optimum solution.

History of Greedy Algorithms

Here is an important landmark of greedy algorithms:

 Greedy algorithms were conceptualized for many graph walk algorithms in


the 1950s.

 Esdger Djikstra conceptualized the algorithm to generate minimal spanning


trees. He aimed to shorten the span of routes within the Dutch capital,
Amsterdam.

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.

 In the '70s, American researchers, Cormen, Rivest, and Stein proposed a


recursive substructuring of greedy solutions in their classical introduction to
algorithms book.

 The greedy paradigm was registered as a different type of optimization


strategy in the NIST records in 2005.

 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.

Greedy Strategies and Decisions

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.

For example, Djikstra's algorithm utilized a stepwise greedy strategy identifying


hosts on the Internet by calculating a cost function. The value returned by the
cost function determined whether the next path is "greedy" or "non-greedy".

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.

Characteristics of the Greedy Approach

The important characteristics of a greedy method are:

 There is an ordered list of resources, with costs or value attributions. These


quantify constraints on a system.

 You will take the maximum quantity of resources in the time a constraint
applies.

237
Data Structures and Algorithms Unit – 7

 For example, in an activity scheduling problem, the resource costs are in


hours, and the activities need to be performed in serial order.

Why use the Greedy Approach?

Here are the reasons for using the greedy approach:

 The greedy approach has a few tradeoffs, which may make it suitable for
optimization.

 One prominent reason is to achieve the most feasible solution immediately.


In the activity selection problem (Explained below), if more activities can be
done before finishing the current activity, these activities can be performed
within the same time.

 Another reason is to divide a problem recursively based on a condition,


with no need to combine all the solutions.

 In the activity selection problem, the "recursive division" step is achieved


by scanning a list of items only once and considering certain activities.

How to Solve the activity selection problem

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.

2. remaining activities: activities at one or more indexes ahead of the


considered activity.

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.

Disadvantages of Greedy Algorithms

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.

Below is a depiction of the disadvantage of the greedy approach.

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.

However, if the algorithm took a sub-optimal path or adopted a conquering


strategy. then 25 would be followed by 40, and the overall cost improvement
would be 65, which is valued 24 points higher as a suboptimal decision.

Backtracking

Backtracking is a technique based on algorithm to solve problem. It uses recursive


calling to find the solution by building a solution step by step increasing values
with time. It removes the solutions that doesn't give rise to the solution of the
problem based on the constraints given to solve the problem.

Backtracking algorithm is applied to some specific types of problems,

 Decision problem used to find a feasible solution of the problem.

 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

In backtracking problem, the algorithm tries to find a sequence path to the


solution which has some small checkpoints from where the problem can
backtrack if no feasible solution is found for the problem.

Example,

Here,

Green is the start point, blue is the intermediate point, red are points with no
feasible solution, dark green is end solution.

Here, when the algorithm propagates to an end to check if it is a solution or not, if


it is then returns the solution otherwise backtracks to the point one step behind it
to find track to the next point to find solution.

Algorithm

Step 1 − if current_position is goal, return success

Step 2 − else,

Step 3 − if current_position is an end point, return failed.

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

In N-Queen problem, we are given an NxN chessboard and we have to place n


queens on the board in such a way that no two queens attack each other. A
queen will attack another queen if it is placed in horizontal, vertical or diagonal
points in its way. Here, we will do 4-Queen problem.

Here, the solution is −

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 1 − Start from 1st position in the array.

Step 2 − Place queens in the board and check. Do,


242
Data Structures and Algorithms Unit – 7

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 2.3 − If placing queen returns a lead to solution return TRUE.

Step 3 − If all queens are placed return TRUE.

Step 4 − If all rows are tried and no solution is found, return FALSE.

There are three types of problems in backtracking –

1. Decision Problem – In this, we search for a feasible solution.

2. Optimization Problem – In this, we search for the best solution.

3. Enumeration Problem – In this, we find all feasible solutions.

How to determine if a problem can be solved using Backtracking?

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.

Consider the below example to understand the Backtracking approach more


formally,

Given an instance of any computational problem and data corresponding to


the instance, all the constraints that need to be satisfied in order to solve the
problem are represented by . A backtracking algorithm will then work as
follows:

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.

2. Check if satisfies each of the constraints in .

 If Yes, then the sub-tree is “eligible” to add more “children”.

 Else, the entire sub-tree is useless, so recurs back to step 1 using


argument .

3. In the event of “eligibility” of the newly formed sub-tree , recurs back to


step 1, using argument .

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.

Pseudo Code for Backtracking :

244
Data Structures and Algorithms Unit – 7

1. Recursive backtracking solution.

2. void findSolutions(n, other params) :

3. if (found a solution) :

4. solutionsFound = solutionsFound + 1;

5. displaySolution();

6. if (solutionsFound >= solutionTarget) :

7. System.exit(0);

8. return

9.

10. for (val = first to last) :

11. if (isValid(val, n)) :

12. applyValue(val, n);

13. findSolutions(n+1, other params);

14. removeValue(val, n);

15. Finding whether a solution exists or not

16. boolean findSolutions(n, other params) :

17. if (found a solution) :

18. displaySolution();

19. return true;

20.

21. for (val = first to last) :

245
Data Structures and Algorithms Unit – 7

22. if (isValid(val, n)) :

23. applyValue(val, n);

24. if (findSolutions(n+1, other params))

25. return true;

26. removeValue(val, n);

27. return false;

Let us try to solve a standard Backtracking problem, N-Queen Problem.


The N Queen is the problem of placing N chess queens on an N×N chessboard so
that no two queens attack each other. For example, following is a solution for 4
Queen problem.

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}

Backtracking Algorithm: The idea is to place queens one by one in different


columns, starting from the leftmost column. When we place a queen in a column,
we check for clashes with already placed queens. In the current column, if we find
a row for which there is no clash, we mark this row and column as part of the
solution. If we do not find such a row due to clashes then we backtrack and return
false.

1) Start in the leftmost column

2) If all queens are placed

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,

column] as part of the solution and recursively check if placing

queen here leads to a solution.

b) If placing the queen in [row, column] leads to a solution then return

true.

c) If placing queen doesn't lead to a solution then unmark this [row,

column] (Backtrack) and go to step (a) to try other rows.

3) If all rows have been tried and nothing worked, return false to trigger

backtracking.

Branch and Bound Algorithm

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.

the 0/1 Knapsack problem to understand Branch and Bound.

There are many algorithms by which the knapsack problem can be solved:

 Greedy Algorithm for Fractional Knapsack

 DP solution for 0/1 Knapsack

 Backtracking Solution for 0/1 Knapsack.

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

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.

Concept/Aim: The main aim is to calculate a minimum number of comparisons


required to execute an algorithm.

Techniques:

The techniques which are used by lower Bound Theory are:

1. Comparisons Trees.

2. Oracle and adversary argument

249
Data Structures and Algorithms Unit – 7

3. State Space Method

1. Comparison trees:

In a comparison sort, we use only comparisons between elements to gain order


information about an input sequence (a1; a2......an).

Given ai,aj from (a1, a2.....an)We Perform One of the Comparisons

o ai < aj less than

o ai ≤ aj less than or equal to

o ai > aj greater than

o ai ≥ aj greater than or equal to

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:

1. (a1, a2, a3), (a1, a3, a2),

2. (a2, a1, a3), (a2, a3, a1)

3. (a3, a1, a2), (a3, a2, a1)

The Comparison based algorithm defines a decision tree.

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.

In a decision tree, there will be an array of length n.

250
Data Structures and Algorithms Unit – 7

So, total leaves will be n! (I.e. total number of comparisons)

If tree height is h, then surely

n! ≤2n (tree will be binary)

Taking an Example of comparing a1, a2, and a3.

Left subtree will be true condition i.e. ai ≤ aj

Right subtree will be false condition i.e. a i >aj

Fig: Decision Tree

So from above, we got

N! ≤2n

Taking Log both sides

251
Data Structures and Algorithms Unit – 7

Comparison tree for Binary Search:

Example: Suppose we have a list of items according to the following Position:

1. 1,2,3,4,5,6,7,8,9,10,11,12,13,14

252
Data Structures and Algorithms Unit – 7

And the last midpoint is:

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.

The Bold letters are Mid-Points Here

According to Mid-Point, the tree will be:

253
Data Structures and Algorithms Unit – 7

Step1: Maximum number of nodes up to k level of the internal node is 2k-1

For Example

2k-1

23-1= 8-1=7

Where k = level=3

Step2: Maximum number of internal nodes in the comparisons tree is n!

Note: Here Internal Nodes are Leaves.

Step3: From Condition1 & Condition 2 we get

N! ≤ 2k-1

14 < 15

Where N = Nodes

Step4: Now, n+1 ≤ 2k

Here, Internal Nodes will always be less than 2k in the Binary Search.

254
Data Structures and Algorithms Unit – 7

Step5:

n+1<= 2k

Log (n+1) = k log 2

k >=

k >=log2(n+1)

Step6:

1. T (n) = k

Step7:

T (n) >=log2(n+1)

Here, the minimum number of Comparisons to perform a task of the search of n


terms using Binary Search

2. Oracle and adversary argument:

Another technique for obtaining lower bounds consists of making use of an


"oracle."

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.

If we let MERGE (m, n) be the minimum number of comparisons used to merge m


items with n items then we have the inequality

1. Log C ((m+n), m) MERGE (m, n) m+n-1.

The upper bound and lower bound can get promptly far apart as m gets much
smaller than n.

3. State Space Method:

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.

3. Given n distinct items, find winner and loser.

4. Aim: When state changed count it that is the aim of State Space Method.

5. In this approach, we will count the number of comparison by counting the


number of changes in state.

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.

7. State: It is a collection of attributes.

8. Through this we sort out two types of Problems:

1. Find the largest & smallest element from an array of elements.

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

Teams are: B, D, and E

So the total no of comparisons are: 7

Let n is the total number of items, then

Comparisons = n-1 (to find the biggest item)

No of Comparisons to find out the 2nd biggest item = log2n-1

10. In this no of comparisons are equal to the number of changes of states during
the execution of the algorithm.

Example: State (A, B, C, D)

A. No of items that can never be compared.

B. No of items that win but never lost (ultimate winner).

C. No of items that lost but never win (ultimate loser)

D. No of items who sometimes lost & sometimes win.

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.

In Phase-1 there are 4 states


If the team is 8 then 4 states
As if n team the n/2 states.

258
Data Structures and Algorithms Unit – 7

4 is Constant in C-State as B, D, F, H are lost teams that never win.

Thus there are 3 states in Phase-2,


If n is 8 then states are 3

If n teams that are states.

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.

Thus there are 3 states in Phase -3

If n teams that are states

Note: the total of all states value is always equal to 'n'.

260
Data Structures and Algorithms Unit – 7

Thus, by adding all phase's states we will get:-

Phase1 + Phase 2 + Phase 3

Suppose we have 8 teams then states are there (as minimum) to


find out which one is never wins team.

Thus, Equation is:

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.

L (n) is the base overall conceivable calculation which is greatest finished.

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.

Deriving good lower bounds is more challenging than arrange efficient


algorithms. This happens because a lower bound states a fact about all possible
algorithms for solving a problem. Generally, we cannot enumerate and analyze all
these algorithms, so lower bound proofs are often hard to obtain.

Lower bounds by reduction

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:

 Any problem that has a polynomial-time reduction to a polynomial time


problem can be solved in polynomial time.

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

 Suppose there is a reduction f from sorting to some problem B that takes


O(n) time and does not examine the elements of the sorting problem. Then
B(f(x)) is a comparison-based sorting algorithm if B uses only comparisions,
and TB(n) = Ω(n log n) - O(n) = Ω(n log n).

 Suppose there is no polynomial-time algorithm for A but there is a


polynomial-time reduction from A to B. Then there is no polynomial-time
algorithm for B. This is the central idea behind NP-completeness, discussed
in PvsNp.

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:

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.

So why shortest path shouldn't have a cycle ?


There is no need to pass a vertex again, because the shortest path to all other
vertices could be found without the need for a second visit for any vertices.

Algorithm Steps:

 The outer loop traverses from 0 : n−1.

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:

Assume the source node has a number (0):

vector <int> v [2000 + 10];

int dis [1000 + 10];

for(int i = 0; i < m + 2; i++){

v[i].clear();

dis[i] = 2e9;

for(int i = 0; i < m; i++){

scanf("%d%d%d", &from , &next , &weight);


264
Data Structures and Algorithms Unit – 7

v[i].push_back(from);

v[i].push_back(next);

v[i].push_back(weight);

dis[0] = 0;

for(int i = 0; i < n - 1; i++){

int j = 0;

while(v[j].size() != 0){

if(dis[ v[j][0] ] + v[j][2] < dis[ v[j][1] ] ){

dis[ v[j][1] ] = dis[ v[j][0] ] + v[j][2];

j++;

A very important application of Bellman Ford is to check if there is a negative


cycle in the graph,

Time Complexity of Bellman Ford algorithm is relatively high O(V⋅E), in


case E=V2, O(V3).
Let's discuss an optimized algorithm.

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.

 Push the source vertex in a min-priority queue in the form (distance ,


vertex), as the comparison in the min-priority queue will be according to
vertices distances.

 Pop the vertex with the minimum distance from the priority queue (at first
the popped vertex = source).

 Update the distances of the connected vertices to the popped vertex in


case of "current vertex distance + edge weight < next vertex distance", then
push the vertex
with the new distance to the priority queue.

 If the popped vertex is visited before, just continue without using it.

 Apply the same algorithm again until the priority queue is empty.

Implementation:

Assume the source vertex = 1.

#define SIZE 100000 + 1

vector < pair < int , int > > v [SIZE]; // each vertex has all the connected vertices
with the edges weights

int dist [SIZE];

266
Data Structures and Algorithms Unit – 7

bool vis [SIZE];

void dijkstra(){

// set the vertices distances as infinity

memset(vis, false , sizeof vis); // set all vertex as unvisited

dist[1] = 0;

multiset < pair < int , int > > s; // multiset do the job as a min-priority
queue

s.insert({0 , 1}); // insert the source node with distance = 0

while(!s.empty()){

pair <int , int> p = *s.begin(); // pop the vertex with the minimum
distance

s.erase(s.begin());

int x = p.s; int wei = p.f;

if( vis[x] ) continue; // check if the popped vertex is visited before

vis[x] = true;

for(int i = 0; i < v[x].size(); i++){


267
Data Structures and Algorithms Unit – 7

int e = v[x][i].f; int w = v[x][i].s;

if(dist[x] + w < dist[e] ){ // check if the next vertex distance could be


minimized

dist[e] = dist[x] + w;

s.insert({dist[e], e} ); // insert the next vertex with the updated


distance

Time Complexity of Dijkstra's Algorithm is O(V2) but with min-priority queue it


drops down to O(V+ElogV).

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

Floyd\u2013Warshall's Algorithm is used to find the shortest paths between


between all pairs of vertices in a graph, where each edge in the graph has a
weight which is positive or negative. The biggest advantage of using this algorithm
is that all the shortest distances between any 2 vertices could be calculated
in O(V3), where V is the number of vertices in a graph.

The Algorithm Steps:

For a graph with N vertices:

 Initialize the shortest paths between any 2 vertices with Infinity.

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]).

dist[i][k] represents the shortest path that only uses the


first K vertices, dist[k][j] represents the shortest path between the pair k,j. As the
shortest path will be a concatenation of the shortest path from i to k, then
from k to j.

for(int k = 1; k <= n; k++){

for(int i = 1; i <= n; i++){

for(int j = 1; j <= n; j++){

dist[i][j] = min( dist[i][j], dist[i][k] + dist[k][j] );

Time Complexity of Floyd\u2013Warshall's Algorithm is O(V3), where V is the


number of vertices in a graph.

Maximum flow

In graph theory, a flow network is defined as a directed graph involving a


source(S) and a sink(T) and several other nodes connected with edges. Each edge
has an individual capacity which is the maximum limit of flow that edge could
allow.
269
Data Structures and Algorithms Unit – 7

Flow in the network should follow the following conditions:

 For any non-source and non-sink node, the input flow is equal to output
flow.

 For any edge(Ei) in the network, 0≤flow(Ei)≤Capacity(Ei).

 Total flow out of the source node is equal total to flow in to the sink node.

 Net flow in the edges follows skew symmetry


i.e. F(u,v)=−F(v,u) where F(u,v) is flow from node u to node v. This leads to a
conclusion where you have to sum up all the flows between two
nodes(either directions) to find net flow between the nodes initially.

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.

function: FordFulkerson(Graph G,Node S,Node T):

Initialise flow in all edges to 0

while (there exists an augmenting path(P) between S and T in residual network


graph):

Augment flow between S to T along the path P

Update residual network graph

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.

A demonstration of working of Ford-Fulkerson algorithm is shown below with the


help of diagrams.

271
Data Structures and Algorithms Unit – 7

272
Data Structures and Algorithms Unit – 7

Implementation:

 An augmenting path in residual graph can be found using DFS or BFS.

 Updating residual graph includes following steps: (refer the diagrams for
better understanding)

o For every edge in the augmenting path, a value of minimum capacity


in the path is subtracted from all the edges of that path.

o An edge of equal amount is added to edges in reverse direction for


every successive nodes in the augmenting path.

The complexity of Ford-Fulkerson algorithm cannot be accurately computed as it


all depends on the path from source to sink. For example, considering the
network shown below, if each time, the path chosen
are S−A−B−T and S−B−A−T alternatively, then it can take a very long time. Instead,
if path chosen are only S−A−T and S−B−T, would also generate the maximum flow.

Dinic's Algorithm

In 1970, Y. A. Dinitz developed a faster algorithm for calculating maximum flow


over the networks. It includes construction of level graphs and residual graphs
and finding of augmenting paths along with blocking flow.

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

Pseudocode for Dinic's algorithm is given below.

Inputs required are network graph G, source node S and sink node T.

function: DinicMaxFlow(Graph G,Node S,Node T):

Initialize flow in all edges to 0, F = 0

Construct level graph

while (there exists an augmenting path in level graph):

find blocking flow f in level graph

F=F+f

Update level graph

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

P and NP Class Problems

275
Data Structures and Algorithms Unit – 7

In Computer Science, many problems are solved where the objective is to


maximize or minimize some values, whereas in other problems we try to find
whether there is a solution or not. Hence, the problems can be categorized as
follows −

Optimization Problem

Optimization problems are those for which the objective is to maximize or


minimize some values. For example,

 Finding the minimum number of colors needed to color a given graph.

 Finding the shortest path between two vertices in a graph.

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,

 Whether a given graph can be colored by only 4-colors.

 Finding Hamiltonian cycle in a graph is not a decision problem, whereas


checking a graph is Hamiltonian or not is a decision problem.

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.

For input size n, if worst-case time complexity of an algorithm is O(nk), where k is


a constant, the algorithm is a polynomial time algorithm.

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.

In this context, we can categorize the problems as follows −

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.

Formally, an algorithm is polynomial time algorithm, if there exists a


polynomial p(n) such that the algorithm can solve any instance of size n in a
time O(p(n)).

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.

The advantages in considering the class of polynomial-time algorithms is that all


reasonable deterministic single processor model of computation can be
simulated on each other with at most a polynomial slow-d

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

Every decision problem that is solvable by a deterministic polynomial time


algorithm is also solvable by a polynomial time non-deterministic algorithm.

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.

If P ≠ NP, there are problems in NP that are neither in P nor in NP-Complete.

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

A problem is in the class NPC if it is in NP and is as hard as any problem in NP. A


problem is NP-hard if all problems in NP are polynomial time reducible to it, even
though it may not be in NP itself.

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

A language B is NP-complete if it satisfies two conditions


278
Data Structures and Algorithms Unit – 7

 B is in NP

 Every A in NP is polynomial time reducible to B.

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.

The problem in NP-Hard cannot be solved in polynomial time, until P = NP. If a


problem is proved to be NPC, there is no need to waste time on trying to find an
efficient algorithm for it. Instead, we can focus on design approximation
algorithm.

NP-Complete Problems

Following are some NP-Complete problems, for which no polynomial time


algorithm is known.

 Determining whether a graph has a Hamiltonian cycle

 Determining whether a Boolean formula is satisfiable, etc.

NP-Hard Problems

The following problems are NP-Hard

 The circuit-satisfiability problem

 Set Cover

 Vertex Cover

 Travelling Salesman Problem

In this context, now we will discuss TSP is NP-Complete

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).

Assume G = (V, E) to be an instance of Hamiltonian cycle.

Hence, an instance of TSP is constructed. We create the complete graph G' = (V,
E'), where

E′={(i,j):i,j∈Vandi≠j

Thus, the cost function is defined as follows −

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

Intuitively, a problem Q can be reduced to another problem Q′ if any instance


of Q can be "easily rephrased" as an instance of Q′, the solution to which provides
a solution to the instance of Q. For example, the problem of solving linear
equations in an indeterminate x reduces to the problem of solving quadratic
equations. Given an instance ax + b = 0, we transform it to 0x2 + ax + b = 0, whose
solution provides a solution to ax + b = 0. Thus, if a problem Q reduces to another
problem Q′, then Q is, in a sense, "no harder to solve" than Q′.

Returning to our formal-language framework for decision problems, we say that a


language L1 is polynomial-time reducible to a language L2, written L1 ≤P L2, if there
exists a polynomial-time computable function f : {0, 1}* → {0,1}* such that for
all x {0, 1}*,

(34.1)

We call the function f the reduction function, and a polynomial-time


algorithm F that computes f is called a reduction algorithm.

Figure illustrates the idea of a polynomial-time reduction from a language L1 to


another language L2. Each language is a subset of {0, 1}*. The reduction
function f provides a polynomial-time mapping such that if x ∈ L1, then f(x) ∈ L2.
Moreover, if x ∉ L1, then f (x) ∉ L2. Thus, the reduction function maps any
instance x of the decision problem represented by the language L1 to an
instance f (x) of the problem represented by L2. Providing an answer to
whether f(x) ∈ L2 directly provides the answer to whether x ∈ L1.

281
Data Structures and Algorithms Unit – 7

Figure

An illustration of a polynomial-time reduction from a language L1 to a


language L2 via a reduction function f. For any input x ∈ {0, 1}*, the question of
whether x ∈ L1 has the same answer as the question of whether f(x) ∈ L2.

Polynomial-time reductions give us a powerful tool for proving that various


languages belong to P.

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.

Learning reduction in general is very important. For example, if we have library


functions to solve certain problem and if we can reduce a new problem to one of
the solved problems, we save a lot of time. Consider the example of a problem
where we have to find minimum product path in a given directed graph where

282
Data Structures and Algorithms Unit – 7

product of path is multiplication of weights of edges along the path. If we have


code for Dijkstra’s algorithm to find shortest path, we can take log of all weights
and use Dijkstra’s algorithm to find the minimum product path rather than writing
a fresh code for this new problem.

How to prove that a given problem is NP complete?


From the definition of NP-complete, it appears impossible to prove that a
problem L is NP-Complete. By definition, it requires us to that show every
problem in NP is polynomial time reducible to L. Fortunately, there is an
alternate way to prove it. The idea is to take a known NP-Complete problem and
reduce it to L. If polynomial time reduction is possible, we can prove that L is NP-
Complete by transitivity of reduction (If a NP-Complete problem is reducible to L
in polynomial time, then all problems are reducible to L in polynomial time).

What was the first problem proved as NP-Complete?


There must be some first NP-Complete problem proved by definition of NP-
Complete problems. SAT (Boolean satisfiability problem) is the first NP-Complete
problem proved by Cook.

It is always useful to know about NP-Completeness even for engineers. Suppose


you are asked to write an efficient algorithm to solve an extremely important
problem for your company. After a lot of thinking, you can only come up
exponential time approach which is impractical. If you don’t know about NP-
Completeness, you can only say that I could not come with an efficient algorithm.
If you know about NP-Completeness and prove that the problem as NP-complete,
you can proudly say that the polynomial time solution is unlikely to exist. If there
is a polynomial time solution possible, then that solution solves a big problem of
computer science many scientists have been trying for years.

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.

Section 33.1 introduces basic concepts of number theory, such as divisibility,


modular equivalence, and unique factorization. Section 33.2 studies one of the
world's oldest algorithms: Euclid's algorithm for computing the greatest common
divisor of two integers. Section 33.3 reviews concepts of modular arithmetic.
Section 33.4 then studies the set of multiples of a given number a, modulo n, and
shows how to find all solutions to the equation ax b (mod n) by using Euclid's
algorithm. The Chinese remainder theorem is presented in Section 33.5. Section
33.6 considers powers of a given number a, modulo n, and presents a repeated-
squaring algorithm for efficiently computing ab mod n, given a, b, and n. This
operation is at the heart of efficient primality testing. Section 33.7 then describes
the RSA public-key cryptosystem. Section 33.8 describes a randomized primality
test that can be used to find large primes efficiently, an essential task in creating
keys for the RSA cryptosystem. Finally, Section 33.9 reviews a simple but effective
heuristic for factoring small integers. It is a curious fact that factoring is one
problem people may wish to be intractable, since the security of RSA depends on
the difficulty of factoring large integers.

Size of inputs and cost of arithmetic computations

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.

In most of this book, we have found it convenient to think of the elementary


arithmetic operations (multiplications, divisions, or computing remainders) as
primitive operations that take one unit of time. By counting the number of such
arithmetic operations an algorithm performs, we have a basis for making a
reasonable estimate of the algorithm's actual running time on a computer.
Elementary operations can be time-consuming, however, when their inputs are
large. It thus becomes convenient to measure how many bit operations a
number-theoretic algorithm requires. In this model, a multiplication of two -bit
integers by the ordinary method uses ( 2) bit operations. Similarly, the
operation of dividing a -bit integer by a shorter integer, or the operation of
taking the remainder of a -bit integer when divided by a shorter integer, can be
performed in time ( 2) by simple algorithms. (See Exercise 33.1-11.) Faster
methods are known. For example, a simple divide-and-conquer method for
multiplying two -bit integers has a running time of ( lg2 3), and the fastest
known method has a running time of ( lg lg lg ). For practical purposes,
however, the ( 2) algorithm is often best, and we shall use this bound as a basis
for our analyses.

Elementary number-theoretic notions

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.

Divisibility and divisors

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

If d | a and d 0, we say that d is a divisor of a. Note that d | a if and only if -


d | a, so that no generality is lost by defining the divisors to be nonnegative, with
the understanding that the negative of any divisor of a also divides a. A divisor of
an integer a is at least 1 but not greater than |a|. For example, the divisors of 24
are 1, 2, 3, 4, 6, 8, 12, and 24.

Every integer a is divisible by the trivial divisors 1 and a. Nontrivial divisors


of a are also called factors of a. For example, the factors of 20 are 2, 4, 5, and 10.

Prime and composite numbers

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.

The division theorem, remainders, and modular equivalence

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

a = a/n n + (a mod n) (1)

or

a mod n = a - a/n n . (2)

Given a well-defined notion of the remainder of one integer when divided by


another, it is convenient to provide special notation to indicate equality of
remainders. If (a mod n) = (b mod n), we write a b (mod n) and say
that a is equivalent to b, modulo n. In other words, a b (mod n) if a and b have
the same remainder when divided by n. Equivalently, a b (mod n) if and only
if n | (b - a). We write a b (mod n) if a is not equivalent to b, modulo n. For
example, 61 6 (mod 11). Also, -13 22 2 (mod 5).

The integers can be divided into n equivalence classes according to their


remainders modulo n. The equivalence class modulo n containing an integer a is

[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

Zn = {[a]n : 0 a n - 1}. (3)

One often sees the definition

Zn = {0, 1,..., n - 1}, (4)

which should be read as equivalent to equation (33.3) with the understanding


that 0 represents [0]n, 1 represents [1]n, and so on; each class is represented by its
least nonnegative element. The underlying equivalence classes must be kept in

287
Data Structures and Algorithms Unit – 7

mind, however. For example, a reference to - 1 as a member of Zn is a reference


to [n - 1]n, since - 1 n - 1 (mod n).

The Fast Fourier Transform

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 first implementation, FFT.basic.h, emphasizes the algorithm;


consequently, to simplify the presentation, it allocates additional memory
when dividing the vector into even and odd entries and explicit recursion is
used, and

 The second implementation, FFT.array.h, demonstrates how the FFT


algorithm can be performed with only O(1) additional memory and without
recursive function calls. Reference, Maple 8, Waterloo Maple Inc.,
Waterloo, Ontario.

Some comments on the code:

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

(1,0) (1,0) (0,0) (0,0) (0,0) (0,0) (0,0) (0,0)

(2,0) (1.70711,-0.707107) (1,-1) (0.292893,-0.707107) (0,0) (0.292893,0.707107)


(1,1) (1.70711,0.707107)

288
Data Structures and Algorithms Unit – 7

(1,0) (1,0) (0,0) (0,0) (0,0) (0,0) (0,0) (0,0)

You will note that 1/√2 ≈ 0.707107, and 1 − 1/√2 ≈ 0.292893 .

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

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.

Figure 1. The unit basis vectors for a vector of dimension 8.

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.

Figure 2. The basis vectors of the frequency space of dimension 8.

template <typename T> class complex<T>

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

double abs(z) |z| = √(a2 + b2)

double arg(z) atan2(ℑ(z), ℜ(z)) = atan2(b, a)

complex conj(z) z* = a − jb

double imag(z) ℑ(z) = b

double norm(z) |z|2 = a2 + b2

complex polar(r, θ) rejθ

double real(z) ℜ(z) = a

complex exp(z) ez

complex log(z) ln(z)

complex pow(w, z) wz

complex sqrt(z) √z

complex sin(z) sin(z)

complex cos(z) cos(z)

290
Data Structures and Algorithms Unit – 7

complex sinh(z) sinh(z)

complex cosh(z) cosh(z)

A complex number a + jb is printed as the ordered pair (a,b).

Determining Powers of Two

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.

String Matching Algorithms

A string is a sequence of characters. In our model we are going to represent a


string as a 0-indexed array. So a string S = ”Galois” is indeed an array [‘G’, ’a’, ’l’,
’o’, ’i’, ’s’]. The number of characters of a string is called its length and is denoted
by |S|. If we want to reference the character of the string at position i, we will use
S[i].

A substring is a sequence of consecutive contiguous elements of a string, we will


denote the substring starting at i and ending at j of string S by S[i...j].

291
Data Structures and Algorithms Unit – 7

A prefix of a string S is a substring that starts at position 0, and a suffix a substring


that ends at |S|-1. A proper prefix of a S is a prefix that is different to S. Similarly,
a proper suffix of S is a suffix that is different to S. The + operator will represent
string concatenation.

Example:

S="Galois"

|S|=6

S[0]='G', S[1]='a', S[2]='l',...,S[5]='s'

S[1...4]="aloi"

W="Evariste"

W+S="EvaristeGalois"

A Needle in the haystack (KMP algorithm)

Given a text T we are interested in calculating all the occurrences of a pattern P.

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

Unfortunately this solution is very slow, its time complexity is O(|T|·|P|),


however it gives us some insights.

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 there is no match at position i, we should start finding a match from


another position, but the question is from where? Our previous algorithm always
selects as next position i+1, but if we start from i+1, it is probable that we could
end up finding a mismatch in a position even before than i+j-1. At least, we could
want to start from a position that guarantees that the strings matches until i+j-1.
Therefore, we should start finding for a match starting from the smallest i+k such
that T[i+k...i+j-1] matches with some prefix of P (look at the picture above).

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

the above two cases with an example:

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|).

Using function f it is easy to search for the occurrences of a pattern on a text in


O(|T|+|P|):

def failure_function(p):

l=len(p)

f=[0]*l

j=0

for i in range(1,l):

while j>=0 and p[j]!=p[i]:

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):

while j>=0 and t[i]!=p[j]:

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.

Now only remains to find a way of calculate that powerful z function.

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.

The z function applied at position i of a string determines an interval [i, i+z[i]-1]


known as z-box, that interval is special, because by the definition of z, S[ i...i+z[i]-1
] = S[0...z[i]-1].

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

while k<n and s[j]==s[k]:

j+=1

k+=1

z[i]=k-i

if z[i]>0 and i+z[i]-1>R:


299
Data Structures and Algorithms Unit – 7

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):

if z[i]>=lp: print i-lp

Hashing strikes back (Rabin-Karp algorithm)

Pattern P matches with text T at position i, if and only if there is a substring of T


that starts at i and is equal to P. So if we can compare quickly two strings ( T[i...i
+|P|-1] with P ), then we can use our naive algorithm (iterate over all i, and check
if there is a match). One way of checking if two strings are not equal is to find a
property that is different in those strings. Let h be a black-box that have as input a
string, and outputs certain property of the string, that property is defined in such
a way that if two strings are different, the value of that property is also different:
if S1 ≠ S2 then h(S1) ≠ h(S2). Note that with this definition, we can’t assert if two
strings are equal, because two different strings can have the same value of the
chosen property.

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).

Example: if S=”hack”, then h(S) = (8 · 283 + 1 · 282 + 3 · 281 + 11 · 280) %M.

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.

In order to reduce hash collisions, M is usually chosen as a large prime. However if


we use an unsigned 32 bits integer we could just let the value overflow (in this
case the modulo is 232).

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:

Remove the character at position i:

Add the character at position i+|P|:

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.

def val(ch): #maps a character to a digit e.g a = 1, b = 2,...

return ord(ch)-ord("a")+1

def find_occurrences(p,t):

lp,lt=len(p),len(t)

m=10**9+7 #modulo (a "big" prime)

b=30 #numeration system base

hp=0 #hash of the pattern

ht=0 #hash of a substring of length |P| of the text

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

#remove character i-1:

ht=(ht-val(t[i-1])*pwr)%m

ht=(ht+m)%m

#add character i+|P|-1

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?

Parallelism is the process of processing several set of instructions simultaneously.


It reduces the total computational time. Parallelism can be implemented by
using parallel computers, i.e. a computer with many processors. Parallel
computers require parallel algorithm, programming languages, compilers and
operating system that support multitasking.

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?

An algorithm is a sequence of instructions followed to solve a problem. While


designing an algorithm, we should consider the architecture of computer on
which the algorithm will be executed. As per the architecture, there are two types
of computers −

 Sequential Computer

 Parallel Computer

Depending on the architecture of computers, we have two types of algorithms −

 Sequential Algorithm − An algorithm in which some consecutive steps of


instructions are executed in a chronological order to solve a problem.

 Parallel Algorithm − The problem is divided into sub-problems and are


executed in parallel to get individual outputs. Later on, these individual
outputs are combined together to get the final desired output.

304
Data Structures and Algorithms Unit – 7

It is not easy to divide a large problem into sub-problems. Sub-problems may


have data dependency among them. Therefore, the processors have to
communicate with each other to solve the problem.

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

Both sequential and parallel computers operate on a set (stream) of instructions


called algorithms. These set of instructions (algorithm) instruct the computer
about what it has to do in each step.

Depending on the instruction stream and data stream, computers can be


classified into four categories −

 Single Instruction stream, Single Data stream (SISD) computers

 Single Instruction stream, Multiple Data stream (SIMD) computers

 Multiple Instruction stream, Single Data stream (MISD) computers

 Multiple Instruction stream, Multiple Data stream (MIMD) computers

SISD Computers

SISD computers contain one control unit, one processing unit, and one memory
unit.

305
Data Structures and Algorithms Unit – 7

In this type of computers, the processor receives a single stream of instructions


from the control unit and operates on a single stream of data from the memory
unit. During computation, at each step, the processor receives one instruction
from the control unit and operates on a single data received from the memory
unit.

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.

While some of the processors execute a set of instructions, the remaining


processors wait for their next set of instructions. Instructions from the control
unit decides which processor will be active (execute instructions) or inactive (wait
for next instruction).

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

 An MIMD computer that shares a common memory is known


as multiprocessors, while those that uses an interconnection network is
known as multicomputers.

 Based on the physical distance of the processors, multicomputers are of


two types −

o Multicomputer − When all the processors are very close to one


another (e.g., in the same room).

o Distributed system − When all the processors are far away from one
another (e.g.- in the different cities)

Parallel Algorithm - Sorting

308
Data Structures and Algorithms Unit – 7

Sorting is a process of arranging elements in a group in a particular order, i.e.,


ascending order, descending order, alphabetic order, etc. Here we will discuss the
following −

 Enumeration Sort

 Odd-Even Transposition Sort

 Parallel Merge Sort

 Hyper Quick Sort

Sorting a list of elements is a very common operation. A sequential sorting


algorithm may not be efficient enough when we have to sort a huge volume of
data. Therefore, parallel algorithms are used in sorting.

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

procedure ENUM_SORTING (n)

begin
309
Data Structures and Algorithms Unit – 7

for each process P1,j do

C[j] := 0;

for each process Pi, j do

if (A[i] < A[j]) or A[i] = A[j] and i < j) then

C[j] := 1;

else

C[j] := 0;

for each process P1, j do

A[C[j]] := A[j];

end ENUM_SORTING

Odd-Even Transposition Sort

Odd-Even Transposition Sort is based on the Bubble Sort technique. It compares


two adjacent numbers and switches them, if the first number is greater than the
second number to get an ascending order list. The opposite case applies for a
descending order series. Odd-Even transposition sort operates in two phases
− odd phase and even phase. In both the phases, processes exchange numbers
with their adjacent number in the right.

310
Data Structures and Algorithms Unit – 7

Algorithm

procedure ODD-EVEN_PAR (n)

begin

id := process's label

for i := 1 to n do

begin

if i is odd and id is odd then

compare-exchange_min(id + 1);
311
Data Structures and Algorithms Unit – 7

else

compare-exchange_max(id - 1);

if i is even and id is even then

compare-exchange_min(id + 1);

else

compare-exchange_max(id - 1);

end for

end ODD-EVEN_PAR

Parallel Merge Sort

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

procedureparallelmergesort(id, n, data, newdata)

begin

data = sequentialmergesort(data)

for dim = 1 to n

data = parallelmerge(id, dim, data)

endfor
313
Data Structures and Algorithms Unit – 7

newdata = data

end

Hyper Quick Sort

Hyper quick sort is an implementation of quick sort on hypercube. Its steps are as
follows −

 Divide the unsorted list among each node.

 Sort each node locally.

 From node 0, broadcast the median value.

 Split each list locally, then exchange the halves across the highest
dimension.

 Repeat steps 3 and 4 in parallel until the dimension reaches 0.

Algorithm

procedure HYPERQUICKSORT (B, n)

begin

id := process’s label;

for i := 1 to d do

begin

x := pivot;

partition B into B1 and B2 such that B1 ≤ x < B2;

if ith bit is 0 then

314
Data Structures and Algorithms Unit – 7

begin

send B2 to the process along the ith communication link;

C := subsequence received along the ith communication link;

B := B1 U C;

endif

else

send B1 to the process along the ith communication link;

C := subsequence received along the ith communication link;

B := B2 U C;

end else

end for

sort B using sequential quicksort;

end HYPERQUICKSORT

Approximate Algorithms

An Approximate Algorithm is a way of approach NP-COMPLETENESS for the


optimization problem. This technique does not guarantee the best solution. The
goal of an approximation algorithm is to come as close as possible to the
optimum value in a reasonable amount of time which is at the most polynomial
time. Such algorithms are called approximation algorithm or heuristic algorithm.

315
Data Structures and Algorithms Unit – 7

o For the traveling salesperson problem, the optimization problem is to find


the shortest cycle, and the approximation problem is to find a short cycle.

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

Suppose we work on an optimization problem where every solution carries a cost.


An Approximate Algorithm returns a legal solution, but the cost of that legal
solution may not be optimal.

For Example, suppose we are considering for a minimum size vertex-cover


(VC). An approximate algorithm returns a VC for us, but the size (cost) may not be
minimized.

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

An algorithm that uses random numbers to decide what to do next anywhere in


its logic is called Randomized Algorithm. For example, in Randomized Quick Sort,
we use random number to pick the next pivot (or we randomly shuffle the array).
Typically, this randomness is used to reduce time complexity or space complexity
in other standard algorithms.

What is a Randomized Algorithm?

An algorithm that uses random numbers to decide what to do next anywhere in


its logic is called Randomized Algorithm.. For example, in Randomized Quick Sort,
we use random number to pick the next pivot (or we randomly shuffle the array).
And in Karger’s algorithm, we randomly pick an edge.

How to analyse Randomized Algorithms?

Some randomized algorithms have deterministic time complexity. For


example, this implementation of Karger’s algorithm has time complexity as O(E).
Such algorithms are called Monte Carlo Algorithms and are easier to analyse for
worst case.
On the other hand, time complexity of other randomized algorithms (other than
Las Vegas) is dependent on value of random variable. Such Randomized
algorithms are called Las Vegas Algorithms. These algorithms are typically
analysed for expected worst case. To compute expected time taken in worst case,
all possible values of the used random variable needs to be considered in worst
case and time taken by every possible value needs to be evaluated. Average of all
evaluated times is the expected worst case time complexity. Below facts are

317
Data Structures and Algorithms Unit – 7

generally helpful in analysis os such algorithms.


Linearity of Expectatio
Expected Number of Trials until Success.

For example consider below a randomized version of QuickSort.

A Central Pivot is a pivot that divides the array in such a way that one side has at-
least 1/4 elements.

// Sorts an array arr[low..high]

randQuickSort(arr[], low, high)

1. If low >= high, then EXIT.

2. While pivot 'x' is not a Central Pivot.

(i) Choose uniformly at random a number from [low..high].

Let the randomly picked number number be x.

(ii) Count elements in arr[low..high] that are smaller

than arr[x]. Let this count be sc.

(iii) Count elements in arr[low..high] that are greater

than arr[x]. Let this count be gc.

(iv) Let n = (high-low+1). If sc >= n/4 and

gc >= n/4, then x is a central pivot.

3. Partition arr[low..high] around the pivot x.

318
Data Structures and Algorithms Unit – 7

4. // Recur for smaller elements

randQuickSort(arr, low, sc-1)

5. // Recur for greater elements

randQuickSort(arr, high-gc+1, high)

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.

Therefore, expected number of times the while loop runs is 2

Thus, the expected time complexity of step 2 is O(n).

What is overall Time Complexity in Worst Case?


In worst case, each partition divides array such that one side has n/4 elements
and other side has 3n/4 elements. The worst case height of recursion tree is
Log 3/4 n which is O(Log n).

T(n) < T(n/4) + T(3n/4) + O(n)

T(n) < 2T(3n/4) + O(n)

Solution of above recurrence is O(n Log n)

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)

o Step 1: [INITIALIZE] SET POS = -1

o Step 2: [INITIALIZE] SET I = 1

o Step 3: Repeat Step 4 while I<=N

o Step 4: IF A[I] = VAL


SET POS = I
PRINT POS
Go to Step 6
320
Data Structures and Algorithms Unit – 7

[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

Complexity Best Case Average Case Worst Case

Time O(1) O(n) O(n)

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.

Binary search algorithm is given below.

321
Data Structures and Algorithms Unit – 7

BINARY_SEARCH(A, lower_bound, upper_bound, VAL)

o Step 1: [INITIALIZE] SET BEG = lower_bound


END = upper_bound, POS = - 1

o Step 2: Repeat Steps 3 and 4 while BEG <=END

o Step 3: SET MID = (BEG + END)/2

o Step 4: IF A[MID] = VAL


SET POS = MID
PRINT POS
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]

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

1 Worst case O(log n)

322
Data Structures and Algorithms Unit – 7

2 Best case O(1)

3 Average Case O(log n)

4 Worst case space complexity O(1)

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

4. a[mid] = a[4] = 13 < 23, therefore

in Second step:

1. Beg = mid +1 = 5

2. End = 8

3. mid = 13/2 = 6

4. a[mid] = a[6] = 20 < 23, therefore;

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;

6. therefore, set location = mid;

7. The location of the item will be 7.

324
DIWAKAR EDUCATION HUB

Data Structures and


Algorithms Unit – 7 MCQs
As per updated syllabus
DIWAKAR EDUCATION HUB

2020

THE LEARN WITH EXPERTIES


Data Structures and Algorithms Unit – 7 MCQs

1. Which line should be inserted in the


blank to complete the following dynamic
programming implementation of the
maximum sub-array sum problem?

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. What is the space complexity of the


following dynamic programming algorithm
used to find the maximum sub-array sum?

2
Data Structures and Algorithms Unit – 7 MCQs

5. Which of these best describes an array?


a) A data structure that shows a hierarchical
behaviour
b) Container of objects of similar types
c) Arrays are immutable once initialised
d) Array is not a data structure

Answer: b
Explanation: Array contains elements only
of the same type.

6. How do you initialize an array in C?


a) int arr[3] = (1,2,3);
b) int arr(3) = {1,2,3};
Which method is used by line 4 of the c) int arr[3] = {1,2,3};
above code snippet? d) int arr(3) = (1,2,3);
a) Divide and conquer
b) Recursion Answer: c
c) Both memoization and divide and Explanation: This is the syntax to initialize
an array in C.
conquer
d) Memoization 7. How do you instantiate an array in Java?
a) int arr[] = new int(3);
Answer: d b) int arr[];
Explanation: The array “sum” is used to c) int arr[] = new int[3];
store the previously calculated values, so d) int arr() = new int(3);
that they aren’t recalculated. So, line 4 uses
the memoization technique. Answer: c
Explanation: Note that option b is
4. Find the maximum sub-array sum for the declaration whereas option c is to
following array: instantiate an array.
{3, 6, 7, 9, 3, 8} 8. Which of the following is a correct way to
a) 33 declare a multidimensional array in Java?
b) 36 a) int[] arr;
c) 23 b) int arr[[]];
c) int[][]arr;
d) 26
d) int[[]] arr;
Answer: b
Answer: c
Explanation: All the elements of the array
Explanation: The syntax to declare
are positive. So, the maximum sub-array multidimensional array in java is either
sum is equal to the sum of all the elements, int[][] arr; or int arr[][];
which is 36.

3
Data Structures and Algorithms Unit – 7 MCQs

9. What is the output of the following piece Answer: c


of code? Explanation: Trying to access an element
beyond the limits of an array gives
public class array ArrayIndexOutOfBoundsException.
{
11. When does the Array Index Out Of
public static void main(String
Bounds Exception occur?
args[])
a) Compile-time
{
b) Run-time
int []arr = {1,2,3,4,5};
c) Not an error
d) Not an exception at all
System.out.println(arr[2]);
Answer: b
System.out.println(arr[4]);
Explanation:
}
ArrayIndexOutOfBoundsException is a run-
}
time exception and the compilation is error-
a) 3 and 5 free.
b) 5 and 3
12. Which of the following concepts make
c) 2 and 4
extensive use of arrays?
d) 4 and 2
a) Binary trees
b) Scheduling of processes
Answer: a
c) Caching
Explanation: Array indexing starts from 0.
d) Spatial locality
10. What is the output of the following
piece of code? Answer: d
Explanation: Whenever a particular
public class array memory location is referred, it is likely that
{ the locations nearby are also referred,
public static void main(String arrays are stored as contigous blocks in
args[]) memory, so if you want to access array
{ elements, spatial locality makes it to access
int []arr = {1,2,3,4,5}; quickly.

13. What are the advantages of arrays?


System.out.println(arr[5]); a) Objects of mixed data types can be
} stored
} b) Elements in an array cannot be sorted
c) Index of first element of an array is 1
a) 4 d) Easier to store elements of same data
b) 5 type
c) ArrayIndexOutOfBoundsException
d) InavlidInputException Answer: d
Explanation: Arrays stores elements of

4
Data Structures and Algorithms Unit – 7 MCQs

same data type and present in continuous b) sequentially


memory locations. c) exponentially
d) logarithmically
14. What are the disadvantages of arrays?
a) Data structure like queue or stack cannot Answer: a
be implemented Explanation: Elements in an array are
b) There are chances of wastage of memory accessed randomly. In Linked lists, elements
space if elements inserted in an array are are accessed sequentially.
lesser than the allocated size
c) Index value of an array can be negative 18. Process of inserting an element in stack
d) Elements are sequentially accessed is called ____________
a) Create
Answer: b
b) Push
Explanation: Arrays are of fixed size. If we
insert elements less than the allocated size, c) Evaluation
unoccupied positions can’t be used again. d) Pop
Wastage will occur in memory.
Answer: b
15. Assuming int is of 4bytes, what is the Explanation: Push operation allows users to
size of int arr[15];? insert elements in stack. If stack is filled
a) 15 completely and trying to perform push
b) 19
operation stack – overflow can happen.
c) 11
d) 60 19. Process of removing an element from
stack is called __________
Answer: d
Explanation: Since there are 15 int elements a) Create
and each int is of 4bytes, we get 15*4 = b) Push
60bytes. c) Evaluation
d) Pop
16. In general, the index of the first element
in an array is __________ Answer: d
a) 0 Explanation: Elements in stack are removed
b) -1
using pop operation. Pop operation
c) 2
d) 1 removes the top most element in the stack
i.e. last entered element.
Answer: a
Explanation: In general, Array Indexing 20. In a stack, if a user tries to remove an
starts from 0. Thus, the index of the first element from empty stack it is called
element in an array is 0. _________
a) Underflow
17. Elements in an array are accessed
b) Empty collection
_____________
a) randomly

5
Data Structures and Algorithms Unit – 7 MCQs

c) Overflow operation. Stack follows LIFO Principle i.e.


d) Garbage Collection Last In First Out(LIFO).

Answer: a 23. Which of the following applications may


Explanation: Underflow occurs when the use a stack?
user performs a pop operation on an empty a) A parentheses balancing program
stack. Overflow occurs when the stack is full b) Tracking of local variables at run time
and the user performs a push operation. c) Compiler Syntax Analyzer
Garbage Collection is used to recover the d) Data Transfer between two
memory occupied by objects that are no asynchronous process
longer used.
Answer: d
21. Pushing an element into stack already Explanation: Data transfer between the two
having five elements and stack size of 5, asynchronous process uses the queue data
then stack becomes structure for synchronisation. The rest are
a) Overflow all stack applications.
b) Crash
24. Consider the usual algorithm for
c) Underflow
determining whether a sequence of
d) User flow
parentheses is balanced.
Answer: a The maximum number of parentheses that
Explanation: The stack is filled with 5 appear on the stack AT ANY ONE TIME
elements and pushing one more element when the algorithm analyzes: (()(())(())) are:
causes a stack overflow. This results in a) 1
overwriting memory, code and loss of b) 2
unsaved work on the computer. c) 3
d) 4 or more
22. Entries in a stack are “ordered”. What is
the meaning of this statement? Answer: c
a) A collection of stacks is sortable Explanation: In the entire parenthesis
b) Stack entries may be compared with the balancing method when the incoming token
‘<‘ operation is a left parenthesis it is pushed into stack. A
c) The entries are stored in a linked list right parenthesis makes pop operation to
d) There is a Sequential entry that is one by delete the elements in stack till we get left
one parenthesis as top most element. 3
elements are there in stack before right
Answer: d
parentheses comes. Therefore, maximum
Explanation: In stack data structure,
number of elements in stack at run time is
elements are added one by one using push
3.

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

(A B D ^ + E F – ) + G. ‘/’ is present in stack. Pop();


A B D ^ + E F – / G +. Thus Postfix Expression Push(2);
is A B D ^ + E F – / G +. Push(3);
Pop();
40. Convert the following Infix expression to
Push(4);
Postfix form using a stack
Pop();
x + y * z + (p * q + r) * s, Follow usual
Pop();
precedence rule and assume that the
Push(5);
expression is legal.
After the completion of all operation, the
a) xyz*+pq*r+s*+
number of elements present in stack are
b) xyz*+pq*r+s+*
a) 1
c) xyz+*pq*r+s*+
b) 2
d) xyzp+**qr+s*+
c) 3
Answer: a d) 4
Explanation: The Infix Expression is x + y * z
Answer: a
+ (p * q + r) * s.
Explanation: Number of elements present in
(x y z ) + (p * q + r) * s. ‘+’, ‘*’ are present in
stack is equal to the difference between
stack.
number of push operations and number of
(x y z * + p q * r) * s. ‘+’ is present in stack.
pop operations. Number of elements is 5-
x y z * + p q * r + s * +. Thus Postfix
4=1.
Expression is x y z * + p q * r + s * +.
43. Which of the following is not an
41. Which of the following statement(s)
inherent application of stack?
about stack data structure is/are NOT
a) Reversing a string
correct?
b) Evaluation of postfix expression
a) Linked List are used for implementing
c) Implementation of recursion
Stacks
d) Job scheduling
b) Top of the Stack always contain the new
node Answer: d
c) Stack is the FIFO data structure Explanation: Job Scheduling is not
d) Null link is present in the last node at the performed using stacks.
bottom of the stack
44. The type of expression in which
Answer: c operator succeeds its operands is?
Explanation: Stack follows LIFO. a) Infix Expression
b) Prefix Expression
42. Consider the following operation
c) Postfix Expression
performed on a stack of size 5.
d) Both Prefix and Postfix Expressions
Push(1);

10
Data Structures and Algorithms Unit – 7 MCQs

Answer: c 47. A linear list of elements in which


Explanation: The expression in which deletion can be done from one end (front)
operator succeeds its operands is called and insertion can take place only at the
postfix expression. The expression in which other end (rear) is known as a ?
operator precedes the operands is called a) Queue
prefix expression. If an operator is present b) Stack
between two operands, then it is called infix c) Tree
expressions. d) Linked list

45. Assume that the operators +,-, X are left Answer: a


associative and ^ is right associative. Explanation: Linear list of elements in which
The order of precedence (from highest to deletion is done at front side and insertion
lowest) is ^, X, +, -. The postfix expression at rear side is called Queue. In stack we will
for the infix expression a + b X c – d ^ e ^ f is delete the last entered element first.
a) abc X+ def ^^ –
48. The data structure required for Breadth
b) abc X+ de^f^ –
First Traversal on a graph is?
c) ab+c Xd – e ^f^
a) Stack
d) -+aXbc^ ^def
b) Array
Answer: b c) Queue
Explanation: Given Infix Expression is a + b X d) Tree
c – d ^ e ^ f.
Answer: c
(a b c X +) (d ^ e ^ f). ‘–‘ is present in stack.
Explanation: In Breadth First Search
(a b c X + d e ^ f ^ -). Thus the final
Traversal, BFS, starting vertex is first taken
expression is (a b c X + d e ^ f ^ -).
and adjacent vertices which are unvisited
46. If the elements “A”, “B”, “C” and “D” are are also taken. Again, the first vertex which
placed in a stack and are deleted one at a was added as an unvisited adjacent vertex
time, what is the order of removal? list will be considered to add further
a) ABCD unvisited vertices of the graph. To get first
b) DCBA unvisited vertex we need to follows First In
c) DCAB First Out principle. Queue uses FIFO
d) ABDC principle.

Answer: b 49. A queue follows __________


Explanation: Stack follows LIFO(Last In First a) FIFO (First In First Out) principle
Out). So the removal order of elements are b) LIFO (Last In First Out) principle
DCBA. c) Ordered array
d) Linear tree

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

b) Single ended queue c) I, II and III


c) Circular queue d) I, II and IV
d) Priority queue
Answer: b
Answer: b Explanation: We know the head node in the
Explanation: Queue always has two ends. given linked list. Insertion and deletion of
So, single ended queue is not the type of elements at the front of the linked list
queue. completes in O (1) time whereas for
insertion and deletion at the last node
56. A linear collection of data elements
requires to traverse through every node in
where the linear node is given by means of
the linked list. Suppose there are n
pointer is called?
elements in a linked list, we need to
a) Linked list
traverse through each node. Hence time
b) Node list
complexity becomes O(n).
c) Primitive list
d) Unordered list 58. In linked list each node contain
minimum of two fields. One field is data
Answer: a
field to store the data second field is?
Explanation: In Linked list each node has its
a) Pointer to character
own data and the address of next node.
b) Pointer to integer
These nodes are linked by using pointers.
c) Pointer to node
Node list is an object that consists of a list
d) Node
of all nodes in a document with in a
particular selected set of nodes. Answer: c
Explanation: Each node in a linked list
57. Consider an implementation of
contains data and a pointer (reference) to
unsorted singly linked list. Suppose it has its
the next node. Second field contains
representation with a head pointer only.
pointer to node.
Given the representation, which of the
following operation can be implemented in 59. What would be the asymptotic time
O(1) time? complexity to add a node at the end of
i) Insertion at the front of the linked list singly linked list, if the pointer is initially
ii) Insertion at the end of the linked list pointing to the head of the list?
iii) Deletion of the front node of the linked a) O(1)
list b) O(n)
iv) Deletion of the last node of the linked c) θ(n)
list d) θ(1)
a) I and II
Answer: c
b) I and III
Explanation: In case of a linked list having n

13
Data Structures and Algorithms Unit – 7 MCQs

elements, we need to travel through every b) O(n)


node of the list to add the element at the c) O(n2)
end of the list. Thus asymptotic time d) O(n3)
complexity is θ(n).
Answer: a
60. What would be the asymptotic time Explanation: A new node is created with the
complexity to insert an element at the front required element. The pointer of the new
of the linked list (head is known)? node points the node to which the head
a) O(1) node of the linked list is also pointing. The
b) O(n) head node pointer is changed and it points
c) O(n2) to the new node which we created earlier.
d) O(n3) The entire process completes in O (1) time.
Thus the asymptotic time complexity to
Answer: b
insert an element in the second position of
Explanation: To add an element at the front
the linked list is O (1).
of the linked list, we will create a new node
which holds the data to be added to the 63. The concatenation of two list can
linked list and pointer which points to head performed in O(1) time. Which of the
position in the linked list. The entire thing following variation of linked list can be
happens within O (1) time. Thus the used?
asymptotic time complexity is O (1). a) Singly linked list
b) Doubly linked list
61. What would be the asymptotic time
c) Circular doubly linked list
complexity to find an element in the linked
d) Array implementation of list
list?
a) O(1) Answer: c
b) O(n) Explanation: We can easily concatenate two
c) O(n2) lists in O (1) time using singly or doubly
d) O(n4) linked list, provided that we have a pointer
to the last node at least one of the lists. But
Answer: b
in case of circular doubly linked lists, we will
Explanation: If the required element is in
break the link in both the lists and hook
the last position, we need to traverse the
them together. Thus circular doubly linked
entire linked list. This will take O (n) time to
list concatenates two lists in O (1) time.
search the element.
64. Consider the following definition in c
62. What would be the asymptotic time programming language
complexity to insert an element at the
second position in the linked list? struct node
a) O(1) {

14
Data Structures and Algorithms Unit – 7 MCQs

int data; Answer: d


struct node * next; Explanation: It cannot be implemented
} using linked lists.
typedef struct node NODE;
NODE *ptr; 67. Linked list is considered as an example
Which of the following c code is used to of ___________ type of memory allocation.
create new node? a) Dynamic
a) ptr = (NODE*)malloc(sizeof(NODE)); b) Static
b) ptr = (NODE*)malloc(NODE); c) Compile time
c) ptr = (NODE*)malloc(sizeof(NODE*)); d) Heap
d) ptr = (NODE)malloc(sizeof(NODE));
Answer: a
Answer: a Explanation: As memory is allocated at the
Explanation: As it represents the right way
run time.
to create a node.
68. In Linked List implementation, a node
65.What kind of linked list is best to answer
question like “What is the item at position carries information regarding ___________
n?” a) Data
b) Link
a) Singly linked list
c) Data and Link
b) Doubly linked list
d) Node
c) Circular linked list
d) Array implementation of linked list Answer: b
Explanation: A linked list is a collection of
Answer: d
Explanation: Arrays provide random access objects linked together by references from
to elements by providing the index value an object to another object. By convention
these objects are names as nodes. Linked
within square brackets. In the linked list, we
list consists of nodes where each node
need to traverse through each element
contains one or more data fields and a
until we reach the nth position. Time taken
reference(link) to the next node.
to access an element represented in arrays
is less than the singly, doubly and circular 69. Linked list data structure offers
linked lists. Thus, array implementation is considerable saving in _____________
used to access the item at the position n. a) Computational Time
b) Space Utilization
66. Linked lists are not suitable to for the
c) Space Utilization and Computational Time
implementation of?
d) Speed Utilization
a) Insertion sort
b) Radix sort
c) Polynomial manipulation
d) Binary search

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

c) 1 2 3 5 c) head = p; p->next = q; q->next = NULL;


d) 1 3 5 5 3 1 d) q->next = NULL; p->next = head; head =
p;
Answer: d
Explanation: fun() prints alternate nodes of Answer: d
the given Linked List, first from head to end, Explanation: When while loop completes its
and then from end to head. execution, node ‘p’ refers to the last node
If Linked List has even number of nodes, whereas the ‘q’ node refers to the node
then skips the last node. before ‘p’ in the linked list. q->next=NULL
makes q as the last node. p->next=head
74. The following C function takes a simply- places p as the first node. the head must be
linked list as input argument. modified to ‘p’ as ‘p’ is the starting node of
It modifies the list by moving the last the list (head=p). Thus the sequence of
element to the front of the list and returns steps are q->next=NULL, p->next=head,
the modified list. Some part of the code is head=p.
left blank. Choose the correct alternative
to replace the blank line. 75. The following C function takes a single-
linked list of integers as a parameter and
typedef struct node rearranges the elements of the list.
{ The function is called with the list
int value; containing the integers 1, 2, 3, 4, 5, 6, 7 in
struct node *next; the given order. What will be the contents
}Node; of the list after the function completes
execution?
Node *move_to_front(Node *head)
{ struct node
Node *p, *q; {
if ((head == NULL: || (head->next == int value;
NULL)) struct node *next;
return head; };
q = NULL; p = head; void rearrange(struct node *list)
while (p-> next !=NULL) {
{ struct node *p, * q;
q = p; int temp;
p = p->next; if ((!list) || !list->next)
} return;
_______________________________ p = list;
return head; q = list->next;
} while(q)
{
temp = p->value;
a) q = NULL; p->next = head; head = p;
p->value = q->value;
b) q->next = NULL; head = p; p->next =
q->value = temp;
head;
p = q->next;

17
Data Structures and Algorithms Unit – 7 MCQs

q = p?p->next:0; 78. You are given pointers to first and last


} nodes of a singly linked list, which of the
} following operations are dependent on the
a) 1, 2, 3, 4, 5, 6, 7 length of the linked list?
b) 2, 1, 4, 3, 6, 5, 7 a) Delete the first element
c) 1, 3, 2, 5, 4, 7, 6 b) Insert a new element as a first element
d) 2, 3, 4, 5, 6, 7, 1 c) Delete the last element of the list
d) Add a new element at the end of the list
Answer: b
Explanation: The function rearrange() Answer: c
exchanges data of every node with its next
Explanation: Deletion of the first element of
node. It starts exchanging data from the
first node itself. the list is done in O (1) time by deleting
memory and changing the first pointer.
76. In the worst case, the number of Insertion of an element as a first element
comparisons needed to search a singly can be done in O (1) time. We will create a
linked list of length n for a given element is
node that holds data and points to the head
a) log 2 n
b) n⁄2 of the given linked list. The head pointer
c) log 2 n – 1 was changed to a newly created node.
d) n Deletion of the last element requires a
pointer to the previous node of last, which
Answer: d can only be obtained by traversing the list.
Explanation: In the worst case, the element
This requires the length of the linked list.
to be searched has to be compared with all
Adding a new element at the end of the list
elements of linked list.
can be done in O (1) by changing the
77. Given pointer to a node X in a singly pointer of the last node to the newly
linked list. Only one pointer is given, pointer created node and last is changed to a newly
to head node is not given, can we delete
created node.
the node X from given linked list?
a) Possible if X is not last node 79. In the worst case, the number of
b) Possible if size of linked list is even
comparisons needed to search a singly
c) Possible if size of linked list is odd
d) Possible if X is not first node linked list of length n for a given element is
a) log2 n
Answer: a b) n⁄2
Explanation: c) log2 n – 1
Following are simple steps. d) n
struct node *temp = X->next;
X->data = temp->data; Answer: d
X->next = temp->next; Explanation: The worst-case happens if the
free(temp); required element is at last or the element is

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

c) Either O(1) or O(n) int size = 0;


d) O(logn) Node cur = head;
while(cur!=null)
Answer: a {
Explanation: You need a temp variable to cur = cur.getNext();
keep track of current node, hence the space size++;
complexity is O(1). }
return size;
85. Which of these is not an application of }
linked list?
a) To implement file systems c)
b) For separate chaining in hash-tables
c) To implement non-binary trees public int length(Node head)
d) Random Access of elements {
int size = 0;
Answer: d Node cur = head;
Explanation: To implement file system, for while(cur!=null)
separate chaining in hash-tables and to {
implement non-binary trees linked lists are size++;
used. Elements are accessed sequentially in cur = cur.getNext();
linked list. Random access of elements is }
not an applications of linked list. }
86. Which of the following piece of code has d)
the functionality of counting the number of
elements in the list? public int length(Node head)
a) {
int size = 0;
public int length(Node head) Node cur = head;
{ while(cur!=null)
int size = 0; {
Node cur = head; size++;
while(cur!=null) cur = cur.getNext().getNext();
{ }
size++; return size;
cur = cur.getNext(); }
}
Answer: a
return size;
Explanation: ‘cur’ pointer traverses through
}
list and increments the size variable until
b) the end of list is reached.

public int length(Node head) 87. How do you insert an element at the
{

20
Data Structures and Algorithms Unit – 7 MCQs

beginning of the list?


a)
88. What is the functionality of the
public void insertBegin(Node node) following piece of code?
{ public int function(int data)
node.setNext(head); {
head = node; Node temp = head;
size++; int var = 0;
} while(temp != null)
b) {
if(temp.getData() ==
data)
public void insertBegin(Node node)
{
{
return var;
head = node;
}
node.setNext(head);
var = var+1;
size++;
temp = temp.getNext();
}
}
c) return Integer.MIN_VALUE;
}
public void insertBegin(Node node) a) Find and delete a given element in the
{ list
Node temp = head.getNext() b) Find and return the given element in the
node.setNext(temp); list
head = node; c) Find and return the position of the given
size++; element in the list
} d) Find and insert a new element in the list
d)
Answer: c
Explanation: When temp is equal to data,
public void insertBegin(Node node)
the position of data is returned.
{
Node temp = head.getNext() 89. Which of the following is false about a
node.setNext(temp); doubly linked list?
node = head;
a) We can navigate in both the directions
size++;
} b) It requires more space than a singly
linked list
Answer: a c) The insertion and deletion of a node take
Explanation: Set the ‘next’ pointer point to
a bit longer
the head of the list and then make this new
node as the head. d) Implementing a doubly linked list is
easier than singly linked list

21
Data Structures and Algorithms Unit – 7 MCQs

Answer: d linked list?


Explanation: A doubly linked list has two a) head xor tail
pointers ‘left’ and ‘right’ which enable it to b) pointer to previous node xor pointer to
next node
traverse in either direction. Compared to
c) pointer to previous node – pointer to
singly liked list which has only a ‘next’ next node
pointer, doubly linked list requires extra d) pointer to next node – pointer to
space to store this extra pointer. Every previous node
insertion and deletion requires
manipulation of two pointers, hence it takes Answer: b
a bit longer time. Implementing doubly Explanation: The pointer difference is
calculated by taking XOR of pointer to
linked list involves setting both left and
previous node and pointer to the next
right pointers to correct nodes and takes node.
more time than singly linked list.
92. What is the worst case time complexity
90. What is a memory efficient double of inserting a node in a doubly linked list?
linked list? a) O(nlogn)
a) Each node has only one pointer to b) O(logn)
c) O(n)
traverse the list back and forth
d) O(1)
b) The list has breakpoints for faster
traversal Answer: c
c) An auxiliary singly linked list acts as a Explanation: In the worst case, the position
helper list to traverse through the doubly to be inserted maybe at the end of the list,
linked list hence you have to traverse through the
d) A doubly linked list that uses bitwise AND entire list to get to the correct position,
hence O(n).
operator for storing addresses
93. Consider the following doubly linked
Answer: a list: head-1-2-3-4-5-tail
Explanation: Memory efficient doubly What will be the list after performing the
linked list has only one pointer to traverse given sequence of operations?
the list back and forth. The implementation
is based on pointer difference. It uses Node temp = new
bitwise XOR operator to store the front and Node(6,head,head.getNext());
rear pointer addresses. Instead of storing Node temp1 = new
Node(0,tail.getPrev(),tail);
actual memory address, every node store
head.setNext(temp);
the XOR address of previous and next
temp.getNext().setPrev(temp);
nodes. tail.setPrev(temp1);
temp1.getPrev().setNext(temp1);
91. How do you calculate the pointer
difference in a memory efficient double

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

the list the list


d) Returns the data and deletes the node d) Returns the data and deletes the node
from the beginning of the list from the beginning of the list

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

Answer: b d) Print fail if the list is empty


Explanation: Advance the pointers in such a View Answer
way that the fast pointer advances two Answer: b
nodes at a time and slow pointer advances Explanation: The function prints fail if the
one node at a time and check to see if at given element is not found. Note that this
any given instant of time if the fast pointer option is inclusive of option d, the list being
points to slow pointer or if the fast pointer’s empty is one of the cases covered.
‘next’ points to the slow pointer. This is
applicable for smaller lists. 101. What is the time complexity of
searching for an element in a circular linked
100. What is the functionality of the list?
following piece of code? Select the most a) O(n)
appropriate b) O(nlogn)
public void function(int data) c) O(1)
{ d) O(n2)
int flag = 0;
if( head != null) Answer: a
{ Explanation: In the worst case, you have to
Node temp = traverse through the entire list of n
head.getNext(); elements.
while((temp != head) &&
(!(temp.getItem() == data))) 102. Which of the following application
{ makes use of a circular linked list?
temp = a) Undo operation in a text editor
temp.getNext(); b) Recursive function calls
flag = 1; c) Allocating CPU to resources
break; d) Implement Hash Tables
}
} Answer: c
if(flag) Explanation: Generally, round robin fashion
is employed to allocate CPU time to
System.out.println("success"); resources which makes use of the circular
else linked list data structure. Recursive function
calls use stack data structure. Undo
System.out.println("fail"); Operation in text editor uses doubly linked
} lists. Hash tables uses singly linked lists.

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

pointer point to null in a circular linked list int function(stack *s)


d) Head node is known in circular linked list {
if(s->top == -1)
Answer: c return 1;
Explanation: The ‘next’ pointer points to else return 0;
null only when the list is empty, otherwise it }
points to the head of the list. Every node in a) full stack
circular linked list can be a starting b) invalid index
point(head). c) empty stack
d) infinite stack
104. Which of the following real world
scenarios would you associate with a stack Answer: c
data structure? Explanation: An empty stack is represented
a) piling up of chairs one above the other with the top-of-the-stack(‘top’ in this case)
b) people standing in a line to be serviced at to be equal to -1.
a counter
c) offer services based on the priority of the 106. What does ‘stack underflow’ refer to?
customer a) accessing item from an undefined stack
d) tatkal Ticket Booking in IRCTC b) adding items to a full stack
c) removing items from an empty stack
Answer: a d) index out of bounds exception
Explanation: Stack follows Last In First Out
(LIFO) policy. Piling up of chairs one above Answer: c
the other is based on LIFO, people standing Explanation: Removing items from an
in a line is a queue and if the service is empty stack is termed as stack underflow.
based on priority, then it can be associated 107. What is the output of the following
with a priority queue. Tatkal Ticket Booking program?
Follows First in First Out Policy. People who
click the book now first will enter the
public class Stack
booking page first.
{
105. What does the following function protected static final int CAPACITY
check for? (all necessary headers to be = 100;
included and function is called from main) protected int size,top = -1;
protected Object stk[];
#define MAX 10
public Stack()
typedef struct stack {
{ stk = new
int top; Object[CAPACITY];
int item[MAX]; }
}stack;
public void push(Object item)

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.

Answer: c 112. Array implementation of Stack is not


Explanation: The Stack ADT throws an dynamic, which of the following statements
EmptyStackException if the stack is empty supports this argument?
and a pop() operation is tried on it. a) space allocation for array is fixed and
cannot be changed during run-time
111. What is the functionality of the b) user unable to give the input for stack
following piece of Java code? operations
Assume: ‘a’ is a non empty array of c) a runtime exception halts execution
integers, the Stack class creates an array of d) improper program compilation
specified size and provides a top pointer
indicating TOS(top of stack), push and pop Answer: a
have normal meaning. Explanation: You cannot modify the size of
an array once the memory has been
public void some_function(int[] a) allocated, adding fewer elements than the
{ array size would cause wastage of space,
Stack S=new Stack(a.length); and adding more elements than the array
int[] b=new int[a.length]; size at run time would cause Stack
for(int i=0;i<a.length;i++) Overflow.
{
113. Which of the following array element
S.push(a[i]);
will return the top-of-the-stack-element for
}
a stack of size N elements(capacity of stack
for(int i=0;i<a.length;i++)
> N).
{
a) S[N-1]
b[i]=(int)(S.pop());
b) S[N]
}
c) S[N-2]
System.out.println("output :");
d) S[N+1]
for(int i=0;i<b.length;i++)
{
Answer: a
System.out.println(b[i]);
Explanation: Array indexing start from 0,
}
hence N-1 is the last index.
}
114. Consider these functions:
a) print alternate elements of array
push() : push an element into the stack
b) duplicate the given array
pop() : pop the top-of-the-stack element
c) parentheses matching
top() : returns the item stored in top-of-the-
d) reverse the array
stack-node
What will be the output after performing
Answer: d
these sequence of operations
Explanation: Every element from the given
push(20);
array ‘a’ is pushed into the stack, and then
push(4);
28
Data Structures and Algorithms Unit – 7 MCQs

top(); 117. What is the best case time complexity


pop(); of deleting a node in Singly Linked list?
pop(); a) O (n)
pop();
b) O (n2)
push(5);
top(); c) O (nlogn)
d) O (1)
a) 20
b) 4 Answer: d
c) stack underflow Explanation: Deletion of the head node in
d) 5 the linked list is taken as the best case. The
successor of the head node is changed to
Answer: d
Explanation: 20 and 4 which were pushed head and deletes the predecessor of the
are popped by the two pop() statements, newly assigned head node. This process
the recent push() is 5, hence top() returns 5. completes in O(1) time.

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

3(data, address to next node, address to }


previous node). }
}
a) reverse the list
119. What does the following function do?
b) display the list
public Object some_func()throws
c) display the list excluding top-of-the-stack-
emptyStackException
element
{
d) reverse the list excluding top-of-the-
if(isEmpty())
stack-element
throw new
emptyStackException("underflow");
Answer: b
return first.getEle();
Explanation: An alias of the node ‘first’ is
}
created which traverses through the list and
a) pop displays the elements.
b) delete the top-of-the-stack element
c) retrieve the top-of-the-stack element 120. What does ‘stack overflow’ refer to?
d) push operation a) accessing item from an undefined stack
b) adding items to a full stack
Answer: c c) removing items from an empty stack
Explanation: This code is only retrieving the d) index out of bounds exception
top element, note that it is not equivalent
to pop operation as you are not setting the Answer: b
‘next’ pointer point to the next node in Explanation: Adding items to a full stack is
sequence. termed as stack underflow.

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

c) To empty a queue c) Both front and rear pointer


d) Both Insertion and To empty a queue d) No pointer will be changed

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

135. The essential condition which is Answer: a


checked before insertion in a linked queue Explanation: It can be done by both the
is? methods.
a) Underflow
138. With what data structure can a priority
b) Overflow
queue be implemented?
c) Front value
a) Array
d) Rear value
b) List
Answer: b c) Heap
Explanation: To check whether there is d) Tree
space in the queue or not.
Answer: d
136. The essential condition which is Explanation: Priority queue can be
checked before deletion in a linked queue implemented using an array, a list, a binary
is? search tree or a heap, although the most
a) Underflow efficient one being the heap.
b) Overflow
139. Which of the following is not an
c) Front value
application of priority queue?
d) Rear value
a) Huffman codes
Answer: a b) Interrupt handling in operating system
Explanation: To check whether there is c) Undo operation in text editors
element in the list or not. d) Bayesian spam filter

137. Which of the following is true about Answer: c


linked list implementation of queue? Explanation: Undo operation is achieved
a) In push operation, if new nodes are using a stack.
inserted at the beginning of linked list, then
140. What is the time complexity to insert a
in pop operation, nodes must be removed
node based on key in a priority queue?
from end
a) O(nlogn)
b) In push operation, if new nodes are
b) O(logn)
inserted at the beginning, then in pop
c) O(n)
operation, nodes must be removed from
d) O(n2)
the beginning
c) In push operation, if new nodes are Answer: c
inserted at the end, then in pop operation, Explanation: In the worst case, you might
nodes must be removed from end have to traverse the entire list.
d) In push operation, if new nodes are
inserted at the end, then in pop operation, 141. What is not a disadvantage of priority
nodes must be removed from beginning scheduling in operating systems?

33
Data Structures and Algorithms Unit – 7 MCQs

a) A low priority process might have to wait Answer: c


indefinitely for the CPU Explanation: In the worst case, you might
b) If the system crashes, the low priority have to traverse the entire list.
systems may be lost permanently
144. What is the functionality of the
c) Interrupt handling
following piece of code?
d) Indefinite blocking

Answer: c public Object delete_key()


Explanation: The lower priority process {
if(count == 0)
should wait until the CPU completes the
{
processing higher priority process. Interrupt System.out.println("Q is
handling is an advantage as interrupts empty");
should be given more priority than tasks at System.exit(0);
hand so that interrupt can be serviced to }
produce desired results. else
{
142. Which of the following is not an Node cur =
advantage of priority queue? head.getNext();
a) Easy to implement Node dup =
cur.getNext();
b) Processes with different priority can be
Object e = cur.getEle();
efficiently handled head.setNext(dup);
c) Applications with differing requirements count--;
d) Easy to delete elements in any case return e;
}
Answer: d }
Explanation: In worst case, the entire queue
a) Delete the second element in the list
has to be searched for the element having
b) Return but not delete the second
highest priority. This will take more time element in the list
than usual. So deletion of elements is not c) Delete the first element in the list
an advantage. d) Return but not delete the first element in
the list
143. What is the time complexity to insert a
node based on position in a priority queue? Answer: c
a) O(nlogn) Explanation: A pointer is made to point at
b) O(logn) the first element in the list and one more to
point to the second element, pointer
c) O(n)
manipulations are done such that the first
d) O(n2) element is no longer being pointed by any
other pointer, its value is returned.

34
Data Structures and Algorithms Unit – 7 MCQs

145. What is a dequeue? dequeue


a) A queue with insert/delete defined for d) Fetch the element at the front end of the
both front and rear ends of the queue dequeue
b) A queue implemented with a doubly
Answer: b
linked list Explanation: If the list is empty, this new
c) A queue implemented with both singly node will point to ‘trail’ and will be pointed
and doubly linked lists at by ‘head’. Otherwise, traverse till the end
d) A queue with insert/delete defined for of the list and insert the new node there.
front side of the queu
147. What are the applications of dequeue?
Answer: a a) A-Steal job scheduling algorithm
b) Can be used as both stack and queue
Explanation: A dequeue or a double ended
c) To find the maximum of all sub arrays of
queue is a queue with insert/delete defined size k
for both front and rear ends of the queue. d) To avoid collision in hash tables

146. What is the functionality of the Answer: d


following piece of code? Explanation: All of the mentioned can be
public void function(Object item) implemented with a dequeue.
{
Node temp=new Node(item,trail); 148. What is the time complexity of
if(isEmpty()) deleting from the rear end of the dequeue
{ implemented with a singly linked list?
head.setNext(temp); a) O(nlogn)
temp.setNext(trail); b) O(logn)
} c) O(n)
else d) O(n2)
{
Node Answer: c
cur=head.getNext(); Explanation: Since a singly linked list is used,
first you have to traverse till the end, so the
while(cur.getNext()!=trail) complexity is O(n).
{
149. After performing these set of
cur=cur.getNext(); operations, what does the final list look
} contain?
cur.setNext(temp);
} InsertFront(10);
size++; InsertFront(20);
} InsertRear(30);
DeleteFront();
a) Insert at the front end of the dequeue InsertRear(40);
b) Insert at the rear end of the dequeue InsertRear(10);
c) Fetch the element at the rear end of the

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

152. Consider you have an array of some Answer: a


random size. You need to perform dequeue Explanation: By performing push(pop()) on
operation. You can perform it using stack all elements on the current stack to the
operation (push and pop) or using queue next stack you get 2 3 4 5 << top.Push(6)
operations itself (enQueue and Dequeue). and perform push(pop()) you’ll get back 6 5
The output is guaranteed to be same. Find 4 3 2 << top. You have actually performed
some differences? enQueue operation using push and pop.
a) They will have different time
154. A double-ended queue supports
complexities
operations like adding and removing items
b) The memory used will not be different
from both the sides of the queue. They
c) There are chances that output might be
support four operations like
different
addFront(adding item to top of the queue),
d) No differences
addRear(adding item to the bottom of the
Answer: a queue), removeFront(removing item from
Explanation: To perform operations such as the top of the queue) and
Dequeue using stack operation you need to removeRear(removing item from the
empty all the elements from the current bottom of the queue). You are given only
stack and push it into the next stack, stacks to implement this data structure. You
resulting in a O(number of elements) can implement only push and pop
complexity whereas the time complexity of operations. What’s the time complexity of
dequeue operation itself is O(1). And there performing addFront and addRear?
is a need of a extra stack. Therefore more (Assume ‘m’ to be the size of the stack and
memory is needed. ‘n’ to be the number of elements)
a) O(m) and O(n)
153. Consider you have a stack whose
b) O(1) and O(n)
elements in it are as follows.
c) O(n) and O(1)
5 4 3 2 << top
d) O(n) and O(m)
Where the top element is 2.
You need to get the following stack Answer: b
6 5 4 3 2 << top Explanation: addFront is just a normal push
The operations that needed to be operation. Push operation is of O(1).
performed are (You can perform only push Whereas addRear is of O(n) as it requires
and pop): two push(pop()) operations of all elements
a) Push(pop()), push(6), push(pop()) of a stack.
b) Push(pop()), push(6)
155. Why is implementation of stack
c) Push(pop()), push(pop()), push(6)
operations on queues not feasible for a
d) Push(6)
large dataset (Asssume the number of

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

no extra available memeory, the given {


operation is not feasible. q1.offer(x);
}
159. Given an array of size n, let’s assume
a) Perform push() with push as the costlier
an element is ‘touched’ if and only if some
operation
operation is performed on it(for example, b) Perform push() with pop as the costlier
for performing a pop operation the top operation
element is ‘touched’). Now you need to c) Perform pop() with push as the costlier
perform Dequeue operation. Each element operation
in the array is touched atleast? d) Perform pop() with pop as the costlier
operation
a) Once
b) Twice Answer: b
c) Thrice Explanation: offer() suggests that it is a push
d) Four times operation, but we see that it is performed
with only one queue, hence the pop
Answer: d operation is costlier.
Explanation: First each element from the
first stack is popped, then pushed into the 162. Reversing a word using stack can be
second stack, dequeue operation is done on used to find if the given word is a
the top of the stack and later the each palindrome or not.
element of second stack is popped then a) True
pushed into the first stack. Therfore each b) False
element is touched four times. Answer: a
160. To implement a stack using queue(with Explanation: This application of stack can
only enqueue and dequeue operations), also be used to find if the given word is a
how many queues will you need? palindrome because, if the reversed is same
a) 1 as that of the original word, the given word
b) 2 is a palindrome.
c) 3 163. Which is the most appropriate data
d) 4 structure for reversing a word?
Answer: b a) queue
Explanation: Either the push or the pop has b) stack
to be a costly operation, and the costlier c) tree
operation requires two queues. d) graph

161. What is the functionality of the Answer: b


following piece of code? Explanation: Stack is the most appropriate
public void fun(int x)

39
Data Structures and Algorithms Unit – 7 MCQs

data structure for reversing a word because bit to 1?


stack follows LIFO principle. a) OR
b) AND
164. Operations required for reversing a
c) XOR
word or a string using stack are push() and
d) NOR
pop().
a) True Answer: a
b) False Explanation: 1 OR 1 = 1, 0 OR 1 = 1, any bit
OR’ed with 1 gives 1.
Answer: a
Explanation: Push operation inserts a 168. Which of the following bitwise
character into the stack and pop operation operations will you use to set a particular
pops the top of the stack. bit to 0?
a) OR
165. What is the time complexity of
b) AND
reversing a word using stack algorithm?
c) XOR
a) O (N log N)
d) NAND
b) O (N2)
c) O (N) Answer: b
d) O (M log N) Explanation: 1 AND 0 = 0, 0 AND 0 = 0, any
bit AND with 0 gives 0.
Answer: c
Explanation: The time complexity of 169. Which of the following bitwise
reversing a stack is mathematically found to operations will you use to toggle a
be O (N) where N is the input. particular bit?
a) OR
166. What is a bit array?
b) AND
a) Data structure for representing arrays of
c) XOR
records
d) NOT
b) Data structure that compactly stores bits
c) An array in which most of the elements Answer: c
have the same value Explanation: 1 XOR 1 = 0, 0 XOR 1 = 1, note
d) Array in which elements are not present that NOT inverts all the bits, while XOR
in continuous locations toggles only a specified bit.

Answer: b 170. Which of the following is not an


Explanation: It compactly stores bits and advantage of bit array?
exploits bit-level parallelism. a) Exploit bit level parallelism
b) Maximal use of data cache
167. Which of the following bitwise
c) Can be stored and manipulated in the
operations will you use to set a particular

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.

Answer: d 173. The number of items used by the


Explanation: Individual Elements are dynamic array contents is its __________
difficult to access and can’t be accessed in a) Physical size
some programming languages. If random b) Capacity
access is more common than sequential c) Logical size
access, they have to be compressed to d) Random size
byte/word array. Exploit Bit parallelism,
Answer: c
Maximal use of data cache and storage and
Explanation: The number of items used by
manipulation for longer time in register set
the dynamic array contents is called logical
are all advantages of bit array.
size. Physical size is the size of the
171. What is a dynamic array? underlying array, which is the maximum
a) A variable size data structure size without reallocation of data.
b) An array which is created at runtime
174. How will you implement dynamic
c) The memory to the array is allocated at
arrays in Java?
runtime
a) Set
d) An array which is reallocated everytime
b) Map
whenever new elements have to be added
c) HashMap
Answer: a d) List
Explanation: It is a varying-size list data
Answer: d
structure that allows items to be added or
Explanation: ArrayList is used to implement
removed, it may use a fixed sized array at
dynamic arrays in Java.
the back end.
175. Which of the following is the correct
172. What is meant by physical size in a
syntax to declare an ArrayList in Java?
dynamic array?
a) ArrayList al = new ArrayList();
a) The size allocated to elements
b) ArrayList al = new ArrayList[];
b) The size extended to add new elements
c) ArrayList al() = new ArrayList();
c) The size of the underlying array at the
d) ArrayList al[] = new ArrayList[];
back-end
d) The size visible to users Answer: a
Explanation: This is a non-generic way of
Answer: c
creating an ArrayList.
Explanation: Physical size, also called array
capacity is the size of the underlying array, 176. Array is divided into two parts in
____________

41
Data Structures and Algorithms Unit – 7 MCQs

a) Hashed Array Tree a) When a language does not support


b) Geometric Array records, parallel arrays can be used
c) Bounded-size dynamic array b) Increased locality of reference
d) Sparse Array c) Ideal cache behaviour
d) Insertion and Deletion becomes tedious
Answer: c
Explanation: The first part stores the items Answer: d
of the dynamic array and the second part is Explanation: Insertion and deletion of
reserved for new allocations. elements require to move every element
from their initial positions. This will become
177. Which of the following is a
tedious. For Record collection, locality of
disadvantage of dynamic arrays?
reference and Ideal Cache behaviour we
a) Locality of reference
can use parallel arrays.
b) Data cache utilization
c) Random access 180. Which of the following is an advantage
d) Memory leak of parallel arrays?
a) Poor locality of reference for non-
Answer: d
sequential access
Explanation: Dynamic arrays share the
b) Very little direct language support
advantage of arrays, added to it is the
c) Expensive to shrink or grow
dynamic addition of elements to the array.
d) Increased Locality of Reference
Memory can be leaked if it is not handled
properly during allocation and deallocation. Answer: d
It is a disadvantage. Explanation: Elements in the parallel array
are accessed sequentially as one arrays
178. What are parallel arrays?
holds the keys whereas other holds the
a) Arrays of the same size
values. This sequential access generally
b) Arrays allocated one after the other
improves Locality of Reference. It is an
c) Arrays of the same number of elements
advantage.
d) Arrays allocated dynamically
181. What is a sorted array?
Answer: c
a) Arrays sorted in numerical order
Explanation: Different arrays can be of
b) Arrays sorted in alphabetical order
different data types but should contain
c) Elements of the array are placed at
same number of elements. Elements at
equally spaced addresses in the memory
corresponding index belong to a record.
d) All of the mentioned
179. Which of the following is a
Answer: d
disadvantage of parallel array over the
Explanation: The array can be sorted in any
traditional arrays?
way, numerical, alphabetical or any other

42
Data Structures and Algorithms Unit – 7 MCQs

way but the elements are placed at equally Answer: c


spaced addresses. Explanation: In the worst case, an element
must added to the front of the array, which
182. To search for an element in a sorted
means that rest of the elements have to be
array, which searching technique can be
shifted, hence the worst case time
used?
complexity becomes O(n).
a) Linear Search
b) Jump Search 185. What is the order of a matrix?
c) Binary Search a) number of rows X number of columns
d) Fibonacci Search b) number of columns X number of rows
c) number of rows X number of rows
Answer: c
d) number of columns X number of columns
Explanation: Since the array is sorted,
binary search is preferred as its time Answer: a
complexity is O(logn). Explanation: The order of the matrix is the
number of rows X number of columns.
183. Which of the following is not an
application of sorted array? 186. Which of the following property does
a) Commercial computing not hold for matrix multiplication?
b) Priority Scheduling a) Associative
c) Discrete Mathematics b) Distributive
d) Hash Tables c) Commutative
d) Additive Inverse
Answer: d
Explanation: Sorted arrays have widespread Answer: c
applications as all commercial computing Explanation: In matrix multiplication, AB !=
involves large data which is very useful if it BA
is sorted. It makes best use of locality of
187. How do you allocate a matrix using a
reference and data cache. Linked lists are
single pointer in C?(r and c are the number
used in Hash Tables not arrays.
of rows and columns respectively)
184. What is the worst case time complexity a) int *arr = malloc(r * c * sizeof(int));
of inserting an element into the sorted b) int *arr = (int *)malloc(r * c * sizeof(int));
array? c) int *arr = (int *)malloc(r + c * sizeof(int));
a) O(nlogn) d) int *arr = (int *)malloc(r * c * sizeof(arr));
b) O(logn)
Answer: b
c) O(n)
Explanation: Total number of elements in
d) O(n2)
the matrix will be r*c

43
Data Structures and Algorithms Unit – 7 MCQs

188. If row-major order is used, how is the Answer: d


following matrix stored in memory? Explanation: Numbers uses arrays(1-D) for
abc sorting not matrices(2-D arrays). Solving
def linear equations is a separate field in
ghi Mathematics involving matrices, Image
a) ihgfedcba processing stores the pixels in the form of
b) abcdefghi matrices, and the graphs are represented
c) cfibehadg with the help of matrices to indicate the
d) adgbehcfi nodes and edges.

Answer: b 191. Which of the following is an advantage


Explanation: It starts with the first element of matrices?
and continues in the same row until the end a) Internal complexity
of row is reached and then proceeds with b) Searching through a matrix is complex
the next row. C follows row-major order. c) Not space efficient
d) Graph Plotting
189. If column-major order is used, how is
the following matrix stored in memory? Answer: d
abc Explanation: Adjacency and Incidence
def Matrices are used to store vertices and
ghi edges of a graph. It is an advantage to plot
a) ihgfedcba graphs easily using matrices. But Time
b) abcdefghi complexity of a matrix is O(n2) and
c) cfibehadg sometimes the internal organization
d) adgbehcfi becomes tedious. They are all
disadvantages of matrices.
Answer: d
Explanation: It starts with the first element 192. Matrix A when multiplied with Matrix
and continues in the same column until the C gives the Identity matrix I, what is C?
end of column is reached and then a) Identity matrix
proceeds with the next column. Fortran b) Inverse of A
follows column-major order. c) Square of A
d) Transpose of A
190. Which of the following don’t use
matrices? Answer: b
a) In solving linear equations Explanation: Any square matrix when
b) Image processing multiplied with its inverse gives the identity
c) Graph theory matrix. Note that non square matrices are
d) Sorting numbers not invertible.

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.

a) Normal of a matrix 196. Who coined the term Sparse Matrix?


b) Trace of a matrix a) Harry Markowitz
c) Square of a matrix b) James Sylvester
d) Transpose of a matrix
c) Chris Messina
Answer: b d) Arthur Cayley
Explanation: Trace of a matrix is the sum of
Answer: a
the principal diagonal elements.
Explanation: Harry Markowitz coined the
194. Which matrix has most of the elements term Sparse Matrix. James Sylvester coined
(not all) as Zero? the term Matrix. Chris Messina coined the
a) Identity Matrix term Hashtag and Arthur Cayley developed
b) Unit Matrix the algebraic aspects of a matrix.
c) Sparse Matrix
d) Zero Matrix 197. Is O(n) the Worst case Time
Complexity for addition of two Sparse
Answer: c Matrix?
Explanation: Sparse Matrix is a matrix in a) True
which most of the elements are Zero. b) False
Identity Matrix is a matrix in which all
principle diagonal elements are 1 and rest Answer: a
of the elements are Zero. Unit Matrix is also Explanation: In Addition, the matrix is
called Identity Matrix. Zero Matrix is a traversed linearly, hence it has the time
matrix in which all the elements are Zero. complexity of O(n) where n is the number
of non-zero elements in the largest matrix
195. What is the relation between Sparsity amongst two.
and Density of a matrix?
a) Sparsity = 1 – Density 198. The matrix contains m rows and n
b) Sparsity = 1 + Density columns. The matrix is called Sparse Matrix

45
Data Structures and Algorithms Unit – 7 MCQs

if ________ while Dense Matrix is a matrix with most of


a) Total number of Zero elements > (m*n)/2 the elements as Non-Zero element.
b) Total number of Zero elements = m + n
201. Which one of the following is a Special
c) Total number of Zero elements = m/n
Sparse Matrix?
d) Total number of Zero elements = m-n
a) Band Matrix
Answer: a b) Skew Matrix
Explanation: For matrix to be Sparse Matrix, c) Null matrix
it should contain Zero elements more than d) Unit matrix
the non-zero elements. Total elements of
Answer: a
the given matrix is m*n. So if Total number
Explanation: A band matrix is a sparse
of Zero elements > (m*n)/2, then the matrix
matrix whose non zero elements are
is called Sparse Matrix.
bounded to a diagonal band, comprising the
199. Which of the following is not the main diagonal and zero or more diagonals
method to represent Sparse Matrix? on either side.
a) Dictionary of Keys
202. In what way the Symmetry Sparse
b) Linked List
Matrix can be stored efficiently?
c) Array
a) Heap
d) Heap
b) Binary tree
Answer: d c) Hash table
Explanation: Heap is not used to represent d) Adjacency List
Sparse Matrix while in Dictionary, rows and
Answer: b
column numbers are used as Keys and
Explanation: Since Symmetry Sparse Matrix
values as Matrix entries, Linked List is used
arises as the adjacency matrix of the
with each node of Four fields (Row, Column,
undirected graph. Hence it can be stored
Value, Next Node) (2D array is used to
efficiently as an adjacency list.
represent the Sparse Matrix with three
fields (Row, Column, Value). 203. What does the number of inversions in
an array indicate?
200. Is Sparse Matrix also known as Dense
a) mean value of the elements of array
Matrix?
b) measure of how close or far the array is
a) True
from being sorted
b) False
c) the distribution of values in the array
Answer: b d) median value of the elements of array
Explanation: Sparse Matrix is a matrix with
Answer: b
most of the elements as Zero elements
Explanation: The number of inversions in an

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

Answer: d future. Such a situation is referred to as


Explanation: Each element of the array over-rewarding.
represents the maximum number of steps
212. What is xor linked list ?
that can be taken forward from that
a) uses of bitwise XOR operation to
element. So as the first element here is 0 so
decrease storage requirements for doubly
we cannot move any further from the first
linked lists
element. Thus, it is not possible to reach
b) uses of bitwise XOR operation to
the end of the array.
decrease storage requirements for linked
210. What is a skip list? lists
a) a linkedlist with size value in nodes c) uses of bitwise operations to decrease
b) a linkedlist that allows faster search storage requirements for doubly linked lists
within an ordered sequence d) just another form of linked list
c) a linkedlist that allows slower search
Answer: a
within an ordered sequence
Explanation: Why we use bitwise XOR
d) a tree which is in the form of linked list
operation is to decrease storage
Answer: b requirements for doubly linked lists.
Explanation: It is a datastructure, which can
213. What does a xor linked list have ?
make search in sorted linked list faster in
a) every node stores the XOR of addresses
the same way as binary search tree and
of previous and next nodes
sorted array (using binary search) are faster.
b) actuall memory address of next node
211.Which of the following is true about the c) every node stores the XOR of addresses
Move-To-Front Method for rearranging of previous and next two nodes
nodes? d) every node stores xor 0 and the current
a) node with highest access count is moved node address
to head of the list
Answer: a
b) requires extra storage
Explanation: Every node stores the XOR of
c) may over-reward infrequently accessed
addresses.
nodes
d) requires a counter for each node 214. Consider a situation of writing a binary
tree into a file with memory storage
Answer: c efficiency in mind, is array representation of
Explanation: In Move-To-front Method the tree is good?
element which is searched is moved to the a) yes because we are overcoming the need
head of the list. And if a node is searched of pointers and so space efficiency
even once, it is moved to the head of the b) yes because array values are indexable
list and given maximum priority even if it is c) No it is not efficient in case of sparse
trees and remaning cases it is fine
not going to be accessed frequently in the

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

declaration. It is the main disadvantage of Answer: a


using arrays to represent binary trees. Explanation: Floor of w-1/2 because we
can’t miss a node.
219. What must be the ideal size of array if
the height of tree is ‘l’? 222. If the tree is not a complete binary tree
a) 2l-1 then what changes can be made for easy
b) l-1 access of children of a node in the array?
c) l a) every node stores data saying which of its
d) 2l children exist in the array
b) no need of any changes continue with 2w
Answer: a
and 2w+1, if node is at i
Explanation: Maximum elements in a tree
c) keep a seperate table telling children of a
(complete binary tree in worst case) of
node
height ‘L’ is 2L-1. Hence size of array is taken
d) use another array parallel to the array
as 2L-1.
with tree
220. What are the children for node ‘w’ of a
Answer: a
complete-binary tree in an array
Explanation: Array cannot represent
representation?
arbitrary shaped trees. It can only be used
a) 2w and 2w+1
in case of complete trees. If every node
b) 2+w and 2-w
stores data saying that which of its children
c) w+1/2 and w/2
exists in the array then elements can be
d) w-1/2 and w+1/2
accessed easily.
Answer: a
223. Advantages of linked list
Explanation: The left child is generally taken representation of binary trees over arrays?
as 2*w whereas the right child will be taken a) dynamic size
as 2*w+1 because root node is present at b) ease of insertion/deletion
index 0 in the array and to access every c) ease in randomly accessing a node
index position in the array. d) both dynamic size and ease in
insertion/deletion
221. What is the parent for a node ‘w’ of a
complete binary tree in an array Answer: d
Explanation: It has both dynamic size and
representation when w is not 0?
ease in insertion and deletion as
a) floor(w-1/2)
advantages.
b) ceil(w-1/2)
c) w-1/2 224. Disadvantages of linked list
d) w/2 representation of binary trees over arrays?
a) Randomly accessing is not possible
b) Extra memory for a pointer is needed
with every element in the list

50
Data Structures and Algorithms Unit – 7 MCQs

c) Difficulty in deletion null and difficulties are raised while finding


d) Random access is not possible and extra successor nodes. Size constraints are not
memory with every element taken on threaded binary trees, but they
occupy less space than a stack/queue.
Answer: d
Explanation: Random access is not possible 228. The following lines talks about deleting
with linked lists. a node in a binary tree.(the tree property
must not be violated after deletion)
225. Which of the following traversing i) from root search for the node to be
algorithm is not used to traverse in a tree? deleted
a) Post order ii)
b) Pre order iii) delete the node at
c) Post order what must be statement ii) and fill up
d) Randomized statement iii)
a) ii)-find random node,replace with node
Answer: d to be deleted. iii)- delete the node
Explanation: Generally, all nodes in a tree b) ii)-find node to be deleted. iii)- delete the
are visited by using preorder, inorder and node at found location
postorder traversing algorithms. c) ii)-find deepest node,replace with node
to be deleted. iii)- delete a node
226. Level order traversal of a tree is d) ii)-find deepest node,replace with node
formed with the help of to be deleted. iii)- delete the deepest node
a) breadth first search
b) depth first search Answer: d
c) dijkstra’s algorithm Explanation: We just replace a to be deleted
d) prims algorithm node with last leaf node of a tree. this must
not be done in case of BST or heaps.
Answer: a
Explanation: Level order is similar to bfs. 229. What may be the psuedo code for
finding the size of a tree?
227. Identify the reason which doesn’t play a) find_size(root_node–>left_node) + 1 +
a key role to use threaded binary trees? find_size(root_node–>right_node)
a) The storage required by stack and queue b) find_size(root_node–>left_node) +
is more find_size(root_node–>right_node)
b) The pointers in most of nodes of a binary c) find_size(root_node–>right_node) – 1
tree are NULL d) find_size(root_node–>left_node + 1
c) It is Difficult to find a successor node
d) They occupy less size Answer: a
Explanation: Draw a tree and analyze the
Answer: d expression. we are always taking size of left
Explanation: Threaded binary trees are subtree and right subtree and adding root
introduced to make the Inorder traversal value(1) to it and finally printing size.
faster without using any stack or recursion.
Stack and Queue require more space and 230. What is missing in this logic of finding a
pointers in the majority of binary trees are path in the tree for a given sum (i.e
51
Data Structures and Algorithms Unit – 7 MCQs

checking whether there will be a path from //missing


roots to leaf nodes with given sum)?
end
checkSum(struct bin-treenode *root , int a) swapping of left and right nodes is
sum) : missing
if(root==null) b) swapping of left with root nodes is
return sum as 0 missing
else : c) swapping of right with root nodes is
leftover_sum=sum-root_node-->value missing
//missing d) nothing is missing
a) code for having recursive calls to either
only left tree or right trees or to both Answer: a
subtrees depending on their existence Explanation: Mirror is another tree with left
b) code for having recursive calls to either and right children of nodes are
only left tree or right trees interchanged as shown in the figure.
c) code for having recursive calls to either
232. What is the code below trying to print?
only left tree
d) code for having recursive calls to either
only right trees void print(tree *root,tree *node)
{
Answer: a if(root ==null) return 0
Explanation: if(left subtree and right if(root-->left==node || root-->right==node
subtree) then move to both subtrees || print(root->left,node)||printf(root-
else if only left subtree then move to left >right,node)
subtree carrying leftover_sum parameter {
else if only right subtree then move to right print(root->data)
subtree carrying leftover_sum parameter. }
}
231. What must be the missing logic below
a) just printing all nodes
so as to print mirror of a tree as below as an
b) not a valid logic to do any task
example?
c) printing ancestors of a node passed as
argument
d) printing nodes from leaf node to a node
passed as argument

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.

242. The average depth of a binary tree is


given as?
a) O(N)
b) O(√N)
c) O(N2)
d) O(log N)
a) inserting a leaf node Answer: d
b) inserting an internal node Explanation: The average depth of a binary
c) deleting a node with 0 or 1 child tree is given as O(√N). In case of a binary
d) deleting a node with 2 children search tree, it is O(log N).
Answer: c 243. How many orders of traversal are
Explanation: The above diagram is a applicable to a binary tree (In General)?
depiction of deleting a node with 0 or 1 a) 1
child since the node D which has 1 child is b) 4
deleted. c) 2
d) 3
240. General ordered tree can be encoded
into binary trees. Answer: d
a) true Explanation: The three orders of traversal
b) false that can be applied to a binary tree are in-
order, pre-order and post order traversal.
Answer: a
Explanation: General ordered tree can be 244. If binary trees are represented in
mapped into binary tree by representing arrays, what formula can be used to locate
them in a left-child-right-sibling way. a left child, if the node has an index i?
a) 2i+1
241. How many bits would a succinct binary
b) 2i+2
tree occupy?
c) 2i
a) n+O(n)
d) 4i
b) 2n+O(n)
c) n/2 Answer: a
d) n Explanation: If binary trees are represented
in arrays, left children are located at indices
Answer: b
2i+1 and right children at 2i+2.
Explanation: A succinct binary tree occupies

54
Data Structures and Algorithms Unit – 7 MCQs

245. Using what formula can a parent node


be located in an array?
a) (i+1)/2
b) (i-1)/2
c) i/2
d) 2i/2

Answer: b
Explanation: If a binary tree is represented
in an array, parent nodes are found at a)
indices (i-1)/2.

246. Which of the following properties are


obeyed by all three tree – traversals?
a) Left subtrees are visited before right
subtrees
b) Right subtrees are visited before left
subtrees
c) Root node is visited before left subtree
b)
d) Root node is visited before right subtree

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.

247. Construct a binary tree using the


following data.
The preorder traversal of a binary tree is 1,
2, 5, 3, 4. The inorder traversal of the same
binary tree is 2, 5, 1, 4, 3.

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).

248. For the tree below, write the post-


order traversal.

Second node in preorder traversal is 2. This


makes 5 as right child to node 2. The fourth
node in preorder traversal is 3. This makes 4
as right child to node 3. Thus the final tree
is:
a) 2, 7, 2, 6, 5, 11, 5, 9, 4
b) 2, 7, 5, 2, 6, 9, 5, 11, 4
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
d) 2, 7, 5, 6, 11, 2, 5, 4, 9

Answer: c
Explanation: Post order traversal follows
LRN(Left-Right-Node).

249. What is the time complexity of pre-


order traversal in the iterative fashion?
1. For the tree below, write the pre-order a) O(1)
traversal. b) O(n)
c) O(logn)
d) O(nlogn)

Answer: b
Explanation: Since you have to go through
all the nodes, the complexity becomes O(n).

250. What is the space complexity of the


post-order traversal in the recursive

56
Data Structures and Algorithms Unit – 7 MCQs

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)

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)

Answer: d The levelorder traversal (BFS traversal) is A,


Explanation: In the worst case we have d B, C, E, D.
stack frames in the recursive call, hence the
complexity is O(d). 258. Consider the following data and specify
which one is Preorder Traversal Sequence,
256. To obtain a prefix expression, which of Inorder and Postorder sequences.
the tree traversals is used? S1: N, M, P, O, Q
a) Level-order traversal S2: N, P, Q, O, M
b) Pre-order traversal S3: M, N, O, P, Q
c) Post-order traversal a) S1 is preorder, S2 is inorder and S3 is
d) In-order traversal postorder
Answer: b b) S1 is inorder, S2 is preorder and S3 is
Explanation: As the name itself suggests, postorder
pre-order traversal can be used. c) S1 is inorder, S2 is postorder and S3 is
preorder
257. Consider the following data. The pre d) S1 is postorder, S2 is inorder and S3 is
order traversal of a binary tree is A, B, E, C, preorder

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.

261. The number of edges from the root to


the node is called __________ of the tree.
a) Height
b) Depth
c) Length
d) Width

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

node to the deepest leaf is called height of a) h = O(loglogn)


the tree. b) h = O(nlogn)
c) h = O(n)
263. What is a full binary tree?
d) h = O(log n)
a) Each node has exactly zero or two
children Answer: d
b) Each node has exactly two children Explanation: The nodes are either a part of
c) All the leaves are at the same level left sub tree or the right sub tree, so we
d) Each node has exactly one or two don’t have to traverse all the nodes, this
children means the complexity is lesser than n, in
the average case, assuming the nodes are
Answer: a
spread evenly, the time complexity
Explanation: A full binary tree is a tree in
becomes O(logn).
which each node has exactly 0 or 2 children.
266. Which of the following is not an
264. What is a complete binary tree?
advantage of trees?
a) Each node has exactly zero or two
a) Hierarchical structure
children
b) Faster search
b) A binary tree, which is completely filled,
c) Router algorithms
with the possible exception of the bottom
d) Undo/Redo operations in a notepad
level, which is filled from right to left
c) A binary tree, which is completely filled, Answer: d
with the possible exception of the bottom Explanation: Undo/Redo operations in a
level, which is filled from left to right notepad is an application of stack.
d) A tree In which all nodes have degree 2 Hierarchical structure, Faster search, Router
algorithms are advantages of trees.
Answer: c
Explanation: A binary tree, which is 267. In a full binary tree if number of
completely filled, with the possible internal nodes is I, then number of leaves L
exception of the bottom level, which is are?
filled from left to right is called complete a) L = 2*I
binary tree. A Tree in which each node has b) L = I + 1
exactly zero or two children is called full c) L = I – 1
binary tree. A Tree in which the degree of d) L = 2*I – 1
each node is 2 except leaf nodes is called
Answer: b
perfect binary tree.
Explanation: Number of Leaf nodes in full
265. What is the average case time binary tree is equal to 1 + Number of
complexity for finding the height of the Internal Nodes i.e L = I + 1
binary tree?

60
Data Structures and Algorithms Unit – 7 MCQs

268. What is an AVL tree?


a) a tree which is balanced and is a height
balanced tree
b) a tree which is unbalanced and is a
height balanced tree
c) a tree with three children
d) a tree with atmost 3 children ii.
a) only i
Answer: a b) only i and ii
Explanation: It is a self balancing tree with c) only ii
height difference atmost 1. d) i is not a binary search tree

269. Why we need to a binary tree which is Answer: b


height balanced? Explanation: The property of AVL tree is it is
a) to avoid formation of skew trees height balanced tree with difference of
b) to save memory atmost 1 between left and right subtrees.
c) to attain faster memory access All AVL trees are binary search tree.
d) to simplify storing
271. What is the maximum height of an AVL
Answer: a tree with p nodes?
Explanation: In real world dealing with a) p
random values is often not possible, the b) log(p)
probability that u are dealing with non c) log(p)/2
random values(like sequential) leads to d) p⁄2
mostly skew trees, which leads to worst
case. hence we make height balance by Answer: b
rotations. Explanation: Consider height of tree to be
‘he’, then number of nodes which totals to
270. Which of the below diagram is p can be written in terms of height as
following AVL tree property? N(he)=N(he-1)+1+N(he-2). since N(he)
which is p can be written in terms of height
as the beside recurrence relation which on
solving gives N(he)= O(logp) as worst case
height.

272. To restore the AVL property after


inserting a element, we start at the
insertion point and move towards root of
i. that tree. is this statement true?
a) true
b) false

Answer: a
Explanation: It is interesting to note that

61
Data Structures and Algorithms Unit – 7 MCQs

after insertion, only the path from that


point to node or only that subtrees are if(left_tree_height== -1)
imbalanced interms of height. return left_tree_height

273. Given an empty AVL tree, how would right_tree_height= avl(right_of_root)


you construct AVL tree when a set of
numbers are given without performing any if(right_tree_height==-1)
rotations? return right_tree_height
a) just build the tree with the given input
b) find the median of the set of elements Does the above code can check if a binary
given, make it as root and construct the search tree is an AVL tree?
tree a) yes
c) use trial and error b) no
d) use dynamic programming to build the
tree Answer: a
Explanation: The condition to check the
Answer: b height difference between left and right
Explanation: Sort the given input, find the subtrees is missing. if
median element among them, make it as (absolute(left_tree_height –
root and construct left and right subtrees right_tree_height)>1) must be added.
with elements lesser and greater than the
276. Consider the below left-left rotation
median element recursively. this ensures
pseudo code where the node contains value
the subtrees differ only by height 1.
pointers to left, right child nodes and a
274. What maximum difference in heights height value and Height() function returns
between the leafs of a AVL tree is possible? height value stored at a particular node.
a) log(n) where n is the number of nodes
b) n where n is the number of nodes avltree leftrotation(avltreenode z):
c) 0 or 1 avltreenode w =x-left
d) atmost 1 x-left=w-right
w-right=x
Answer: a x-height=max(Height(x-left),Height(x-
Explanation: At every level we can form a right))+1
tree with difference in height between w-height=max(missing)+1
subtrees to be atmost 1 and so there can be return w
log(n) such levels since height of AVL tree is
log(n). What is missing?
a) Height(w-left), x-height
275. Consider the pseudo code: b) Height(w-right), x-height
c) Height(w-left), x
int avl(binarysearchtree root): d) Height(w-left)
if(not root)
return 0 Answer: a
left_tree_height = avl(left_of_root) Explanation: In the code we are trying to

62
Data Structures and Algorithms Unit – 7 MCQs

make the left rotation and so we need to Answer: a


find maximum of those two values. Explanation: As there are majority of
pointers with null value going wasted we
277. Why to prefer red-black trees over AVL use threaded binary trees.
trees?
a) Because red-black is more rigidly 280. In general, the node content in a
balanced threaded binary tree is ________
b) AVL tree store balance factor in every a) leftchild_pointer, left_tag, data,
node which costs space right_tag, rightchild_pointer
c) AVL tree fails at scale b) leftchild_pointer, left_tag
d) Red black is more efficient c) leftchild_pointer, left_tag, right_tag,
rightchild_pointer
Answer: b d) leftchild_pointer, left_tag, data
Explanation: Every node in an AVL tree need
to store the balance factor (-1, 0, 1) hence Answer: a
space costs to O(n), n being number of Explanation: It contains additional 2
nodes. but in red-black we can use the sign pointers over normal binary tree node
of number (if numbers being stored are structure.
only positive) and hence save space for
storing balancing information. there are 281. What are null nodes filled with in a
even other reasons where redblack is threaded binary tree?
mostly prefered. a) inorder predecessor for left node and
inorder successor for right node
278. What is a threaded binary tree information
traversal? b) right node with inorder predecessor and
a) a binary tree traversal using stacks left node with inorder successor
b) a binary tree traversal using queues information
c) a binary tree traversal using stacks and c) they remain null
queues d) some other values randomly
d) a binary tree traversal without using
stacks and queues Answer: a
Explanation: If preorder or postorder is
Answer: d used then the respective predecessor and
Explanation: This type of tree traversal will successor info is stored.
not use stack or queue.
282. Which of the following tree traversals
279. What are the disadvantages of normal work if the null left pointer pointing to the
binary tree traversals? predecessor and null right pointer pointing
a) there are many pointers which are null to the successor in a binary tree?
and thus useless a) inorder, postorder, preorder traversals
b) there is no traversal which is efficient b) inorder
c) complexity in implementing c) postorder
d) improper traversals d) preorder

63
Data Structures and Algorithms Unit – 7 MCQs

Answer: a 285. What is inefficient with the below


Explanation: In threaded binary trees, the threaded binary tree picture?
null left pointer points to the predecessor
and the right null pointer point to the
successor. In threaded binary trees, we can
use in-order, preorder and postorder
traversals to visit every node in the tree.

283. What are double and single threaded


trees?
a) when both left, right nodes are having
null pointers and only right node is null a) it has dangling pointers
pointer respectively b) nothing inefficient
b) having 2 and 1 node c) incorrect threaded tree
c) using single and double linked lists d) space is being used more
d) using heaps and priority queues
Answer: a
Answer: a Explanation: The nodes extreme left and
Explanation: They are properties of double right are pointing to nothing which could be
and single threaded binary trees also used efficiently.
respectively.
286. Which of the following is the most
284. What is wrong with below code for widely used external memory data
inorder traversal of inorder threaded binary structure?
tree: a) AVL tree
b) B-tree
inordertraversal(threadedtreenode root):
c) Red-black tree
threadedtreenode q =
d) Both AVL tree and Red-black tree
inorderpredecessor(root)
while(q!=root):
Answer: b
q=inorderpredecessor(q)
Explanation: In external memory, the data is
print q.data
transferred in form of blocks. These blocks
a) inordersuccessor instead of have data valued and pointers. And B-tree
inorderpredecessor must be done
can hold both the data values and pointers.
b) code is correct
c) it is code for post order So B-tree is used as an external memory
d) it is code for pre order data structure.

287. B-tree of order n is a order-n multiway


Answer: a
Explanation: Property of inorder threaded tree in which each non-root node contains
binary tree is left node with inorder __________
predecessor and right node with inorder a) at most (n – 1)/2 keys
successor information are stored. b) exact (n – 1)/2 keys

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.

c) 294. Compression techniques can be used


on the keys to reduce both space and time
requirements in a B-tree.
a) True
b) False

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.

295. Which of the following is true?


a) larger the order of B-tree, less frequently
Answer: c
the split occurs
Explanation: Each non-root in a B-tree of
b) larger the order of B-tree, more
order 5 must contain at least 2 keys. Here,
frequently the split occurs
when the key 130 is deleted the node gets
c) smaller the order of B-tree, more
underflowed i.e. number of keys in the
frequently the split occurs
node drops below 2. So we combine the
d) smaller the order of B-tree, less
node with key 110 with it’s brother node
frequently the split occurs
having keys 144 and 156. And this
combined node will also contain the Answer: a
separator key from parent i.e. key 140, Explanation: The average probability of the
leaving the root with two keys 110 and 160. split is 1/(⌈m / 2⌉ – 1), where m is the order
of B-tree. So, if m larger, the probability of
293. What is the best case height of a B-tree
split will be less.
of order n and which has k keys?
a) logn (k+1) – 1
b) nk

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.

298. A B+ tree can contain a maximum of 7


pointers in a node. What is the minimum
number of keys in leaves?
a)
a) 6
b) 3
c) 4 b)
d) 7

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

c) Statement 2: When a key is deleted from


the leaf, it is also deleted from the non-leaf
nodes of the tree.
a) Statement 1 is true but statement 2 is
false
b) Statement 2 is true but statement 1 is
false
d) c) Both the statements are true
d) Both the statements are false

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).

302. What is the maximum number of keys


that a B+ -tree of order 3 and of height 3
have?
a) 3
b) 80
c) 27
6. Statement 1: When a node is split during d) 26
insertion, the middle key is promoted to the
parent as well as retained in right half-node.

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)

304. Which one of the following data


structures are preferred in database-system
implementation?
a) AVL tree
b) B-tree
c) B+ -tree c)
d) Splay tree

Answer: c
Explanation: The database-system
implementations use B+ -tree data
structure because they can be used for
multilevel indexing. d)

305. 2-3 tree is a specific form of _________ Answer: c


a) B – tree Explanation: Tree should have two subtrees
b) B+ – tree at node2, but it should not have three
c) AVL tree elements. The node with elements 11 and
d) Heap 15 should have three child subtrees.

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

313. Which of the following is not a collision c) universal hashing


resolution strategy for open addressing? d) open addressing
a) Linear probing
Answer: c
b) Quadratic probing
Explanation: Universal hashing scheme uses
c) Double hashing
a randomization approach whereas hashing
d) Rehashing
by division and hashing by multiplication
Answer: d are heuristic in nature.
Explanation: Linear probing, quadratic
317. Which hash function satisfies the
probing and double hashing are all collision
condition of simple uniform hashing?
resolution strategies for open addressing
a) h(k) = lowerbound(km)
whereas rehashing is a different technique.
b) h(k)= upperbound(mk)
314. Hashing can be used in online spelling c) h(k)= lowerbound(k)
checkers. d) h(k)= upperbound(k)
a) True
Answer: a
b) False
Explanation: If the keys are known to be
Answer: a random real numbers k independently and
Explanation: If misspelling detection is uniformly distributed in the range 0<=k<=1,
important, an entire dictionary can be pre- the hash function which satisfies the
hashed and words can be checked in condition of simple uniform hashing is
constant time. h(k)= lowerbound(km).

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

a) 14963 b) universal hashing


b) 14392 c) hashing by division
c) 12784 d) hashing by multiplication
d) 14452
Answer: b
Answer: d Explanation: Universal hashing scheme
Explanation: The given character string can provides better performance than other
be interpreted as (112,116) (Ascii values) schemes because it uses a unique
then expressed as a radix-128 integer, randomisation approach.
hence the value is 112*128 + 116 = 14452.
323. Using division method, in a given hash
320. What is the hash function used in the table of size 157, the key of value 172 be
division method? placed at position ____
a) h(k) = k/m a) 19
b) h(k) = k mod m b) 72
c) h(k) = m/k c) 15
d) h(k) = m mod k d) 17

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

325. What is the hash function used in m= 27


multiplication method? m = 128.
a) h(k) = floor( m(kA mod 1))
328. What is the value of h(k) for the key
b) h(k) = ceil( m(kA mod 1))
123456?
c) h(k) = floor(kA mod m)
Given: p=14, s=2654435769, w=32
d) h(k) = ceil( kA mod m)
a) 123
Answer: a b) 456
Explanation: The hash function can be c) 70
computed by multiplying m with the d) 67
fractional part of kA (kA mod 1) and then
Answer: d
computing the floor value of the result.
Explanation: A = s/2w
326. What is the advantage of the A = 2654435769/ 232
multiplication method? k.A = 123456 * (2654435769/ 232)
a) only 2 steps are involved = (76300 * 232) + 17612864
b) using constant Hence r1= 76300; r0=17612864
c) value of m not critical Since w=14 the 14 most significant bits of r0
d) simple multiplication yield the value of h(k) as 67.

Answer: c 329. What is the average retrieval time


Explanation: The value of m can be simply when n keys hash to the same slot?
in powers of 2 since we can easily a) Theta(n)
implement the function in most computers. b) Theta(n2)
m=2p where p is an integer. c) Theta(nlog n)
d) Big-Oh(n2)
327. What is the table size when the value
of p is 7 in multiplication method of Answer: a
creating hash functions? Explanation: The average retrieval time
a) 14 when n keys hash to the same slot is given
b) 128 by Theta(n) as the collision occurs in the
c) 49 hash table.
d) 127
330. Collisions can be reduced by choosing
Answer: b a hash function randomly in a way that is
Explanation: In multiplication method of independent of the keys that are actually to
creating hash functions the table size can be be stored.
taken in integral powers of 2. a) True
m = 2p b) False

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.

332. What is the hash function used in


Double Hashing?
a) (h1(k) – i*h2(k))mod m
b) h1(k) + h2(k) a) B and E
c) (h1(k) + i*h2(k))mod m b) C and D
d) (h1(k) + h2(k))mod m c) A and E
Answer: c d) C and B
Explanation: Double hashing uses a hash Answer: d
function of the form (h1(k) + i*h2(k))mod m Explanation: After removing either B or C,
where h1 and h2 are auxiliary hash the graph becomes disconnected.
functions and m is the size of the hash
table. 335. For the given graph(G), which of the
following statements is true?
333. Which of the following statements for
a simple graph is correct?
a) Every path is a trail
b) Every trail is a path
c) Every trail is a path as well as every path
is a trail
d) Path and trail have no relation

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.

338. In a simple graph, the number of edges


a) G is a complete graph is equal to twice the sum of the degrees of
b) G is not a connected graph the vertices.
c) The vertex connectivity of the graph is 2 a) True
d) The edge connectivity of the graph is 1 b) False

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.

340. If a simple graph G, contains n vertices


and m edges, the number of edges in the
Graph G'(Complement of G) is ___________
a) (n*n-n-2*m)/2
b) (n*n+n+2*m)/2
c) (n*n-n-2*m)/2
d) (n*n-n+2*m)/2

75
Data Structures and Algorithms Unit – 7 MCQs

Answer: a d) A graph may contain no vertices and


Explanation: The union of G and G’ would many edges
be a complete graph so, the number of
Answer: b
edges in G’= number of edges in the
Explanation: A graph must contain at least
complete form of G(nC2)-edges in G(m).
one vertex.
341. Which of the following properties does
344. For a given graph G having v vertices
a simple graph not hold?
and e edges which is connected and has no
a) Must be connected
cycles, which of the following statements is
b) Must be unweighted
true?
c) Must have no loops or multiple edges
a) v=e
d) Must have no multiple edges
b) v = e+1
Answer: a c) v + 1 = e
Explanation: A simple graph maybe d) v = e-1
connected or disconnected.
Answer: b
342. What is the maximum number of Explanation: For any connected graph with
edges in a bipartite graph having 10 no cycles the equation holds true.
vertices?
345. For which of the following
a) 24
combinations of the degrees of vertices
b) 21
would the connected graph be eulerian?
c) 25
a) 1,2,3
d) 16
b) 2,3,4
Answer: c c) 2,4,5
Explanation: Let one set have n vertices d) 1,3,5
another set would contain 10-n vertices.
Answer: a
Total number of edges would be n*(10-n),
Explanation: A graph is eulerian if either all
differentiating with respect to n, would
of its vertices are even or if only two of its
yield the answer.
vertices are odd.
343. Which of the following is true?
346. A graph with all vertices having equal
a) A graph may contain no edges and many
degree is known as a __________
vertices
a) Multi Graph
b) A graph may contain many edges and no
b) Regular Graph
vertices
c) Simple Graph
c) A graph may contain no edges and no
d) Complete Graph
vertices

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

ii) All complete graphs are cyclic graphs. Answer: c


iii) All paths are bipartite. Explanation: Worst case is when the desired
iv) All cyclic graphs are bipartite. element is at the tail of the array or not
v) There are cyclic graphs which are present at all, in this case you have to
complete. traverse till the end of the array, hence the
a) 1 complexity is O(n).
b) 2
363. Where is linear searching used?
c) 3
a) When the list has only a few elements
d) 4
b) When performing a single search in an
Answer: b unordered list
Explanation: Statements iii) and v) are c) Used all the time
correct. d) When the list has only a few elements
and When performing a single search in an
360. All paths and cyclic graphs are bipartite
unordered list
graphs.
a) True Answer: d
b) False Explanation: It is practical to implement
linear search in the situations mentioned in
Answer: b
When the list has only a few elements and
Explanation: Only paths and even cycles are
When performing a single search in an
bipartite graphs.
unordered list, but for larger elements the
361. What is the best case for linear search? complexity becomes larger and it makes
a) O(nlogn) sense to sort the list and employ binary
b) O(logn) search or hashing.
c) O(n)
364. What is the best case and worst case
d) O(1)
complexity of ordered linear search?
Answer: d a) O(nlogn), O(logn)
Explanation: The element is at the head of b) O(logn), O(nlogn)
the array, hence O(1). c) O(n), O(1)
d) O(1), O(n)
362. What is the worst case for linear
search? Answer: d
a) O(nlogn) Explanation: Although ordered linear search
b) O(logn) is better than unordered when the element
c) O(n) is not present in the array, the best and
d) O(1) worst cases still remain the same, with the
key element being found at first position or
at last position.

79
Data Structures and Algorithms Unit – 7 MCQs

365. Which of the following is a Answer: a


disadvantage of linear search? Explanation: The recursive algorithm
a) Requires more space consumes more space as it involves the
b) Greater time complexities compared to usage the stack space(calls the function
other searching algorithms numerous times).
c) Not easy to understand
368. What is the worst case runtime of
d) Not easy to implement
linear search(recursive) algorithm?
Answer: b a) O(n)
Explanation: The complexity of linear search b) O(logn)
as the name suggests is O(n) which is much c) O(n2)
greater than other searching techniques like d) O(nx)
binary search(O(logn)). Linear search is easy
Answer: a
to implement and understand than other
Explanation: In the worst case scenario,
searching techniques.
there might be a need of calling the stack n
366. Is there any difference in the speed of times. Therfore O(n).
execution between linear serach(recursive)
369. Linear search(recursive) algorithm
vs linear search(lterative)?
used in _____________
a) Both execute at same speed
a) When the size of the dataset is low
b) Linear search(recursive) is faster
b) When the size of the dataset is large
c) Linear search(Iterative) is faster
c) When the dataset is unordered
d) Cant be said
d) Never used
Answer: c
Answer: a
Explanation: The Iterative algorithm is
Explanation: It is used when the size of the
faster than the latter as recursive algorithm
dataset is low as its runtime is O(n) which is
has overheads like calling function and
more when compared to the binary search
registering stacks repeatedly.
O(logn).
367. Is the space consumed by the linear
370. What is the best case runtime of linear
search(recursive) and linear
search(recursive) algorithm on an ordered
search(iterative) same?
set of elements?
a) No, recursive algorithm consumes more
a) O(1)
space
b) O(n)
b) No, recursive algorithm consumes less
c) O(logn)
space
d) O(nx)
c) Yes
d) Nothing can be said

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

383. Any algorithm that sorts by exchanging place


adjacent elements require O(N2) on c) insertion sort is stable and it does not
average. sort In-place
a) True d) insertion sort is unstable and it does not
b) False sort In-place

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

Answer: b from the first element and swapping the


Explanation: As the input size increases, the elements if required in each iteration.
performance of selection sort decreases.
398. What is the average case complexity of
395. What is an external sorting algorithm? bubble sort?
a) Algorithm that uses tape or disk during a) O(nlogn)
the sort b) O(logn)
b) Algorithm that uses main memory during c) O(n)
the sort d) O(n2)
c) Algorithm that involves swapping
Answer: d
d) Algorithm that are considered ‘in place’
Explanation: Bubble sort works by starting
Answer: a from the first element and swapping the
Explanation: As the name suggests, external elements if required in each iteration even
sorting algorithm uses external memory like in the average case.
tape or disk.
399. Which of the following is not an
396. What is an internal sorting algorithm? advantage of optimised bubble sort over
a) Algorithm that uses tape or disk during other sorting techniques in case of sorted
the sort elements?
b) Algorithm that uses main memory during a) It is faster
the sort b) Consumes less memory
c) Algorithm that involves swapping c) Detects whether the input is already
d) Algorithm that are considered ‘in place’ sorted
d) Consumes less time
Answer: b
Explanation: As the name suggests, internal Answer: c
sorting algorithm uses internal main Explanation: Optimised Bubble sort is one
memory. of the simplest sorting techniques and
perhaps the only advantage it has over
397. What is the worst case complexity of
other techniques is that it can detect
bubble sort?
whether the input is already sorted. It is
a) O(nlogn)
faster than other in case of sorted array and
b) O(logn)
consumes less time to describe whether the
c) O(n)
input array is sorted or not. It consumes
d) O(n2)
same memory than other sorting
Answer: d techniques. Hence it is not an advantage.
Explanation: Bubble sort works by starting
400. Merge sort uses which of the following
technique to implement sorting?

85
Data Structures and Algorithms Unit – 7 MCQs

a) backtracking 403. Merge sort can be implemented using


b) greedy algorithm O(1) auxiliary space.
c) divide and conquer a) true
d) dynamic programming b) false

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

Answer: b c) Insertion sort


Explanation: Quick sort is a divide and d) Binary tree sort
conquer algorithm. Quick sort first
Answer: d
partitions a large array into two smaller
Explanation: Quick sort is a space-optimised
sub-arrays. And then recursively sorts the
version of the binary tree sort. In binary
sub-arrays.
sort tree, the elements are inserted
412. What is the worst case time complexity sequentially into the binary search tree and
of the Quick sort? Quick sort organises elements into a tree
a) O(nlogn) that is implied by the recursive calls.
b) O(n)
415. QuickSort can be categorized into
c) O(n3)
which of the following?
d) O(n2)
a) Brute Force technique
Answer: d b) Divide and conquer
Explanation: The worst case running time c) Greedy algorithm
for Quick sort is O(n2). In Quick sort, the d) Dynamic programming
worst case behaviour occurs when the
Answer: b
partitioning routine produces two sub-
Explanation: First you divide(partition) the
arrays one with n – 1 element and other
array based on the pivot element and sort
with 0 elements.
accordingly.
413. Quick sort is a stable sorting algorithm.
416. Heap sort is an implementation of
a) True
____________ using a descending priority
b) False
queue.
Answer: b a) insertion sort
Explanation: In stable sorting algorithm the b) selection sort
records with equal keys appear in the same c) bubble sort
order in the sorted sequence as they appear d) merge sort
in the input unsorted sequence. Quick sort
Answer: b
does not preserve the relative order of
Explanation: Heap sort is an
equal sort items. Therefore, Quick sort is
implementation of selection sort using the
not a stable sort.
input array as a heap representing a
414. Quick sort is a space-optimised version descending priority queue. Heap sort
of ____ algorithm is divided into two phase. In first
a) Bubble sort phase the max-heap is created and the
b) Selection sort second phase (selection phase) deletes the

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

c) cube sort Answer: b


d) merge sort Explanation: Best case time complexity of
cube sort occurs when the input array is
Answer: c
almost sorted. So in such a case only O(n)
Explanation: Out of the given options only
time is required for sorting.
cube sort is a parallel sorting algorithm. It
builds self balancing multi dimensional 426. Consider the original array 17 8 12 4
arrays from the input keys. 26. How many comparisons are needed to
construct the BST on the original array?
423. What is the worst case time complexity
a) 5
of cube sort?
b) 4
a) O(n)
c) 7
b) O(log n)
d) 10
c) O(n log n)
d) O(n2) Answer: d
Explanation: Original array is 17 8 12 4 26.
Answer: c
The BST built on this array is shown in the
Explanation: The worst case performance of
figure below.
cube sort is O(n log n). This is the fastest
possible complexity with a comparison
based sort.

424. What is the auxiliary space


requirement of cube sort?
a) O(n)
b) O(1)
c) O(log n)
d) O(n log n)

Answer: a To built the BST, we travel down the tree


Explanation: Cube sort requires an auxiliary until a leaf is reached. Therefore, for every
space of O(n). This is the worst case of element we compare the element with the
auxiliary space complexity. internal nodes until we the leaves and then
once again compare the element with its
425. What is the best case time complexity parent to decide whether it is right child or
of cube sort? left child. So, for given array we need to
a) O(n2) perform 10 comparisons to build the BST.
b) O(n)
c) O(n log n) 427. In binary tree sort, we first construct
d) O(1) the BST and then we perform _______
traversal to get the sorted order.
90
Data Structures and Algorithms Unit – 7 MCQs

a) inorder equal valued elements in the input and


b) postorder sorted array does not remain the same
c) preorder when we use cycle sort.
d) level order
431. Euclid’s algorithm is used for finding
Answer: a ___________
Explanation: In binary tree sort is a sort a) GCD of two numbers
algorithm where a binary search tree is built b) GCD of more than three numbers
from the elements to be sorted, and then c) LCM of two numbers
we perform inorder traversal on the BST to d) LCM of more than two numbers
get the elements in sorted order.
Answer: a
428. What is the worst case time complexity Explanation: Euclid’s algorithm is basically
of the binary tree sort? used to find the GCD of two numbers. It
a) O(n) cannot be directly applied to three or more
b) O(nlogn) numbers at a time.
c) O(n2)
432. Who invented Euclid’s algorithm?
d) O(logn)
a) Sieve
Answer: c b) Euclid
Explanation: For the binary tree sort the c) Euclid-Sieve
worst case when the BST constructed is d) Gabriel lame
unbalanced. BST gets unbalanced when the
Answer: b
elements are already sorted. So, in the
Explanation: Euclid invented Euclid’s
worst case, O(n2) time is required to built
algorithm. Sieve provided an algorithm for
the BST and O(n) time to traverse the tree.
finding prime numbers. Gabriel lame
Therefore, the worst case time complexity
proved a theorem in Euclid’s algorithm.
is O(n2) + O(n) = O(n2).
433. If 4 is the GCD of 16 and 12, What is
429. Which of the following is an example
the GCD of 12 and 4?
of an unstable sorting algorithm?
a) 12
a) cycle sort
b) 6
b) insertion sort
c) 4
c) bubble sort
d) 2
d) merge sort
Answer: c
Answer: a
Explanation: Euclid’s algorithm states that
Explanation: Out of the given options only
the GCD of two numbers does not change
cycle sort is an unstable sorting algorithm.
even if the bigger number is replaced by a
This implies that the relative position of

91
Data Structures and Algorithms Unit – 7 MCQs

difference of two numbers. So, GCD of 16 requires only 7 recursive multiplications of


and 12 and 12 and (16-12)=4 is the same. n/2 x n/2 matrix and Theta(n2) scalar
additions and subtractions yielding the
435. Which of the following is not an
running time as O(n2.81).
application of Euclid’s algorithm?
a) Simplification of fractions 438. What is the running time of naïve
b) Performing divisions in modular matrix multiplication algorithm?
arithmetic a) O(n2.81)
c) Solving quadratic equations b) O(n4)
d) Solving diophantine equations c) O(n)
d) O(n3)
Answer: c
Explanation: Solving quadratic equations is Answer: d
not an application of Euclid’s algorithm Explanation: The traditional matrix
whereas the rest of the options are multiplication algorithm takes O(n3) time.
mathematical applications of Euclid’s The number of recursive multiplications
algorithm. involved in this algorithm is 8.

436. Strassen’s algorithm is 439. Strassen’s matrix multiplication


a/an_____________ algorithm. algorithm follows ___________ technique.
a) Non- recursive a) Greedy technique
b) Recursive b) Dynamic Programming
c) Approximation c) Divide and Conquer
d) Accurate d) Backtracking

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

Answer: b the set of all subsets. Number of elements


Explanation: Using Theta(n2) scalar in the power set of a set having n elements
additions and subtractions, 14 matrices are is given as 2n. Thus, here number of
computed each of which is n/2 x n/2. Then elements will be 23=8.
seven matrix products are computed
444. Number of elements in the power set
recursively.
of set S={1,2,2} will be?
441. What is meant by number theory? a) 2
a) study of integers b) 4
b) study of complex numbers c) 6
c) numerology d) 8
d) theory of origination of mathematics
Answer: c
Answer: a Explanation: For finding the number of
Explanation: Number theory is a branch of elements in the power set of the given set
mathematics that deals with the study of we need to remove duplicates. So we will
integers. Partitioning of a number comes be left with 6 unique elements which will be
under the study of number theory. P={{},{1},{2},{1,2},{2,2},{1,2,2}}.

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.

448.Breadth First Search is equivalent to


which of the traversal in the Binary Trees?
a) Pre-order Traversal
b) Post-order Traversal
c) Level-order Traversal
d) In-order Traversal

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.

449. Time Complexity of Breadth First


Search is? (V – number of vertices, E –
number of edges)
a) O(V + E)
b) O(V)
c) O(E)
d) O(V*E)

94

You might also like