Ada Dcet
Ada Dcet
Syllabus:- What is an Algorithm? Fundamentals of Algorithmic problem solving, important problem types.
Fundamental data structures, Analysis Framework, Measuring the input size, Units for measuring Running time,
Orders of Growth, Worst-case, Best-case and Average-case efficiencies, Asymptotic Notations and Basic Efficiency
classes, Informal Introduction, O-notation, Ω-notation, θ-notation, Introduction to Brute Force approach,
Selection Sort and Bubble Sort, Sequential search, Exhaustive Search- Travelling salesman Problem and Knapsack
Problem, Depth First Search, Breadth First Search, Introduction to divide and conquer, Merge Sort, Quick Sort,
Binary Search, Binary Tree traversals and related properties, Decrease-and-Conquer- Introduction, Insertion Sort,
Topological Sorting.
Introduction
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a
required output for any accepted input in a finite amount of time.
This definition can be illustrated by a simple diagram
STRING PROCESSING: -A string is a sequence of character from an Alphabet. It can be text strings which
comprise letters, numbers and special characters. It is mainly used in string handling algorithms.
GRAPH PROBLEM: - One of the interesting areas in algorithm is graph algorithm.
A graph is a collection of points called vertices. Which are connected by line
segment called Edges.
Graphs are used for Modeling, a wide verity of real life application, transportation
and communication network. It includes Graph traversal, Shortest Path and
Topological Sorting Algorithm.
COMBINATORIAL PROBLEM: -TSP and graph coloring problem are example of combinatorial problem, these
are the problem, which is used to find a combinatorial object such as permutation, combination or subset that
satisfy certain constraints.
GEOMETRIC PROBLEM: - They deal with geometric object such as points, line and polygon it also include various
geometric shape such as ∆, O etc. Application for this algorithm is computer graphics, robotics.
Measuring an input size: - Time required by an algorithm is proportional to size of the problem instance.
For e.g.: more time is required to sort 20 elements than what is required to sort 10 elements.
For example:
In bubble sort, when the input array is already sorted, the time taken by the algorithm is linear i.e. the best
case.
But, when the input array is in reverse condition, the algorithm takes the maximum time to sort the
elements i.e. the worst case.
When the input array is neither sorted nor in reverse order, then it takes average time. These durations are
denoted using asymptotic notations.
There are mainly three asymptotic notations:
1. Omega notation - Best
2. Big-O notation- Worst
3. Theta notation- Average
Omega Notation (Ω-notation)
Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides best case
complexity of an algorithm.
C2g(n)>=f(n)>=c1g(n)
Bubble sort
Another brute-force application to the sorting problem is to compare adjacent elements of the list and
exchange them if they are out of order. By doing it repeatedly, we end up “bubbling up” the largest element
to the last position on the list.
The next pass bubbles up the second largest element, and so on.
After n − 1 passes the list is sorted.
Best case efficiency is Ω(n), Average case efficacy is Θ (n2), and worst case efficacy is O(n2)
Exhaustive search
Exhaustive search is simply a brute-force approach to combinatorial problems. It suggests generating each
and every element of the problem domain, selecting those of them that satisfy all the constraints, and then finding
a desired element.
We illustrate exhaustive search by applying it to three important problems: the traveling salesman
problem, the knapsack problem, and the assignment problem.
Compiled by- Arun J +91-7795287475
SGM Coaching Center
Knapsack problem
Here is another well-known problem in algorithmic. Given ‘n’ items of known weights w1, w2. . . wn and
values v1, v2, . . . , vn and a knapsack of capacity W, find the most valuable subset of the items that fit into the
knapsack.
The exhaustive-search approach to this problem leads to generating all the subsets of the set of ‘n’ items
given, computing the total weight of each subset in order to identify feasible subsets and finding a subset of the
largest value among them.
Since the number of subsets of an n-element set is 2n, the exhaustive search leads to a Ω(2n)algorithm,
no matter how efficiently individual subsets are generated.
Depth-first search
Depth-first search starts a graph’s traversal at an arbitrary vertex by marking it as visited. On each iteration,
the algorithm proceeds to an unvisited vertex that is adjacent to the one it is currently in.
This process continues until a dead end—a vertex with no adjacent
unvisited vertices—is found.
At a dead end, the algorithm backs up one edge to the vertex it came
from and tries to continue visiting unvisited vertices from there.
If unvisited vertices still remain, the depth-first search must be restarted
at any one of them. We push a vertex onto the stack when the vertex is
reached for the first time, and we pop a vertex off the stack when it
becomes a dead end.
DFS is equivalent to Pre-order traversal in the binary trees
Below Figure provides an example of a breadth-first search traversal, with the traversal queue and corresponding
breadth-first search forest shown.
Merge sort
Merge sort is a perfect example of a successful
application of the divide-and conquer technique. It sorts a given
array A[0..n − 1] by dividing it into two parts A[0..⌊n/2⌋− 1] and
A[⌊n/2⌋..n − 1]sorting each of them recursively, and then merging
the two smaller sorted arrays into a single sorted one.
Best case efficiency is Ω(n log(n)), Average case efficacy is θ(n log(n)), and worst case efficacy is O(n log(n))
Quick sort
Quicksort is the other important sorting algorithms that is based on the divide-and conquer approach. A
partition is an arrangement of the array’s elements so that all the elements to the left of some element A[s] are
less than or equal to A[s], and all the elements to the right of A[s] are greater than or equal to it. A[s] is called as
pivot element.
A[s] will be in its final position in the sorted array, and we can continue sorting the two subarrays to the left and
to the right of A[s] independently
Best case efficiency is Ω(n log(n)), Average case efficacy is θ(n log(n)), and worst case efficacy is O(n2).
Binary search
Binary search is efficient algorithm for searching in a sorted array. It works by comparing a search key K
with the array’s middle element A[m]. If they match, the algorithm stops; otherwise, the same operation is
repeated recursively for the first half of the array ifK <A[m], and for the second half ifK >A[m]
The decrease-by-a-constant-factor technique suggests reducing a problem instance by the same constant factor
on each iteration of the algorithm. In most applications, this constant factor is equal to two.
Finally, in the variable-size-decrease variety of decrease-and-conquer, the size-reduction pattern varies from one
iteration of an algorithm to another. Euclid’s algorithm for computing the greatest common divisor provides a
good example of such a situation.
Insertion sort
Insertion sort is an application of decrease and conquer technique. It is a comparison based sort in which
the sorted array is built on one entry at a time.
The procedure is similar to the way we play cards. After shuffling the cards we pick each card and insert
it into the proper place. So that cards in hand are arranged with ascending order. The same technique is being
followed in insertion sort.
Best case efficiency is Ω(n2), Average case efficacy is θ(n2), and worst case efficacy is O(n2).
Topological sorting
Topological sorting is a sorting method to list the vertices of the graph in such an order that for every
edge in the graph, the vertex where the edge starts is listed before the vertex where the edge ends.
There is no solution for topological sorting if there is a cycle in the digraph. The diagraph must be a
Directed Acyclic Graph (DAG).
Topological sorting problem can be solved by using
1. DFS Method
2. Source removal method
DFS Method:
Perform DFS traversal and note the order in which vertices become dead ends. (Popped order)
Reverse the order, yields the topological sorting.
8. While solving the problem with computer the most difficult step is __________.
a) Describing the problem b) Finding out the cost of the software
c) Writing the computer instructions d) Testing the solution
11. When an algorithm is written in the form of a programming language, it becomes a _________
a) Flowchart b) Program c) Pseudo code d) Syntax
12. Which of the following case does not exist in complexity theory?
a) Best case b) Worst case c) Average case d) Null case
13. What is the efficiency for an algorithm that generates all subsets of an n- element set?
a) Cubic b) Linear c) Exponential d) Factorial
14. What is the efficiency for an algorithm that generates all permutations of an n- element set?
a) Cubic b) Linear c) Exponential d) Factorial
15. What is the efficiency for an algorithm with three embedded loops?
a) Cubic b) Linear c) Exponential d) Factorial
16. What is the efficiency for an algorithm with two embedded loops?
a) Cubic b) Linear c) Exponential d) Quadratic
18. The space factor when determining the efficiency of algorithm is measured by
a) Counting the maximum memory needed by the algorithm
b) Counting the minimum memory needed by the algorithm
c) Counting the average memory needed by the algorithm
d) Counting the maximum disk space needed by the algorithm
19. The time factor when determining the efficiency of algorithm is measured by
a) Counting microseconds b) Counting the number of key operations
c) Counting the number of statements d) Counting the kilobytes of algorithm
28. How many passes are required to sort a file of size n by bubble sort method?
a) N2 b) N c) N-1 d) N/2
30. The algorithm like Quick sort does not require extra memory for carrying out the sorting procedure. This
technique is called __________.
a) In-place b) Stable c) Unstable d) In-partition
32. Which of the following sorting methods would be most suitable for sorting a list which is almost sorted?
a) Bubble Sort b) Insertion Sort c) Selection Sort d) Quick Sort
33. A sort which compares adjacent elements in a list and switches where necessary is ____.
a) Insertion sort b) Heap sort c) Quick sort d) Merge sort
Compiled by- Arun J +91-7795287475
SGM Coaching Center
34. A sort which iteratively passes through a list to exchange the first element with any element less than it
and then repeats with a new first element is called
a) Insertion sort b) Selection sort c) Heap sort d) Quick sort
35. The number of swapping’s needed to sort the numbers 8, 22, 7, 9, 31, 19, 5, 13 in ascending order, using
bubble sort is
a) 10 b) 9 c) 13 d)14
36. The way a card game player arranges his cards as he picks them one by one can be compared to
a) Quick sort b) Merge sort c) Insertion sort d) Bubble sort
38. For the improvement of efficiency of quick sort the pivot can be
a) The first element b) The mean element c) The last element d) None of the above
40. Number of selections required to sort a file of size N by straight selection requires
a) N – 1 b) log N c) O(N2) d) None of the above
41. What is the advantage of selection sort over other sorting techniques?
a) It requires no additional storage space b) It is scalable
c) It works best for inputs which are already sorted d) It is faster than any other sorting technique
42. The given array is a={3,4,5,2,1}. The number of iterations in bubble sort and selection sort respectively are,
a) 4 and 4 b) 4 and 5 c) 2 and 4 d) 2 and 5
45. Merge sort uses which of the following algorithm to implement sorting?
a) Backtracking b) Greedy algorithm c) Divide and conquer d) Dynamic programming
47. Merge sort uses which of the following method to implement sorting?
a) Merging b) Partitioning c) Selection d) Exchanging
50. How many passes does an insertion sort algorithm consist of?
a) N b) N-1 c) N+1 d) N2
51. What is the running time of an insertion sort algorithm if the input is pre-sorted?
a) Ω(N2) b) Ω(N log N) c) Ω(N) d) Ω(M log N)
52. What will be the number of passes to sort the elements using insertion sort?14, 12,16, 6, 3, 10
a) 6 b) 5 c) 7 d) 1
53. For the following question, how will the array elements look like after second pass using insertion sort?
34, 8, 64, 51, 32, 21
a)8, 34, 64, 51, 32, 21 b) 8, 32, 34, 51, 64, 21
c) 8, 21, 64, 51, 32, 34 d) 8, 21, 32, 51, 64, 34
54. Which of the following real time examples is based on insertion sort?
a) Arranging a pack of playing cards b) database scenarios and distributes scenarios
c) Arranging books on a library shelf d) real-time systems
55. Which of the following sorting algorithms is the fastest for sorting small arrays?
a) Quick sort b) Insertion sort c) Shell sort d) Heap sort
62. A straight forward approach to solving a problem, usually directly based on the problem statement and
definitions of the concept involved.
a) Brute Force b) Dynamic program c) Greedy technique d) Program
63. 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
64. The efficiency of BFS when graph is represented in the form of adjacency matrix is______
a) O(n) b)O(Θ(|V|2 ) c) Θ(|V|+|E|) d) Θ(|V|2)
66. The algorithm design technique used for Insertion sort is_____
a) Brute Force b) Decrease & Conquer c) Divide & Conquer d) Greedy technique
69. Depth -First search method of graph traversal cannot be used to determine
a) Connectivity b) Acyclicity c) Tree edges d) Minimum edge path
70. The Breadth First Search traversal of a graph will result into?
a) Linked List b) Tree c) Graph with back edges d) All of the mentioned
72. While traversing a binary tree the root is visited before the left and right subtree are visited
a) Pre-order b) In-order c) Post-order d) None of these
73. While traversing a binary tree the root is visited after visiting its left subtree but before visiting its right
subtree
a) Pre-order b) In-order c) Post-order d) None of these
74. While traversing a binary tree the root is visited after visiting the left and right subtrees
a) Pre-order b) In-order c) Post-order d) None of these
75. The algorithms like merge sort, quick sort and binary search are based on
a) Brute Force b) Decrease & Conquer c) Divide & Conquer d) Greedy technique
80. A person wants to visit some places. He starts from a vertex and then wants to visit every place connected
to this vertex and so on. What algorithm he should use?
a) Depth First Search b) Breadth First Search
c) Trim’s algorithm d) None of the mentioned
81. 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) None of the mentioned
3. Which is the best sorting technique when the list is already sorted?
1) Insertion sort 2) Bubble sort 3) Merge sort 4) Selection sort
DCET - 2019
1. Finding the location of the element with a give value is
(A) Traversal (b) Search (C) Sort (D) None of these
2. How many passes are require to sort a file of size ‘n’ by bubble sort method?
(A) n2 b) n – 1 (C) n (D) n/2
DCET - 2020
1. The complexity of merge sort algorithm is
a. O(n) b. O(log n) c. O(n2) d. O(n log n)
4. The relationship between number of back edges and number of cycles in DFS is
a. Both are equal b. Back edges are half of cycles
c. Back edges are on quarter of cycle d. There is no relationship between number of edges and cycles