×

Close

UNIT IV: ALGORITHMS
Number of questions
LAST SIX YEARS' GATE ANALYSIS
9
8
7
6
5
4
3
2
1
0
Marks 1
Marks 2
Total number of questions
2015 2014 2013 2012 2011 2010
Concepts on which questions were asked in the previous six years
Year
Concept
2015
Algorithms, Unsorted array, BFS, Spanning tree, Recurrence relation, Job sequence, Planar graph, Hash
table, Complexity, Asymptotic notations, Polynomial time reducible, SAT Problem, Connected graphs,
Algorithm design paradigms, Graphs
2014
Arithmetic expressions, Rooted tree, Graph, Depth-first search, Sorting algorithms and their complexity,
Balanced binary, NP-complete problem, Hashing
2013
Sorting algorithms, Time complexity, NP problems, Bellman—Ford algorithm, Graphs, Heap sort, queue
2012
Time complexity, Recurrence relation, Balanced binary search tree, NP problem, Dijkstra’s algorithm,
Merge sort
2011
Arrays, Binary heap, Dynamic programming, Time complexity, Hashing, Minimum spanning tree (MST)
2010
Undirected graphs, Spanning tree, Hashing
Chapter 4.indd 175
4/9/2015 9:54:02 AM

Chapter 4.indd 176
4/9/2015 9:54:02 AM

CHAPTER 4
ALGORITHMS
Syllabus: Algorithms: Analysis, Asymptotic notation, Notions of space and time complexity, Worst- and average-case
analysis; Design: Greedy approach, Dynamic programming, Divide-and-conquer; Tree and graph traversals, Connected
components, Spanning trees, Shortest paths; Hashing, Sorting, Searching. Asymptotic analysis (best, worst, average
cases) of time and space, Upper and lower bounds, Basic concepts of complexity classes — P, NP, NP-hard, NP-complete.
4.1 INTRODUCTION
4.2.1 Properties of Algorithm
There are certain properties algorithms have:
In computer science, an algorithm provides a set of stepby-step instructions for solving various problems. The
set of instructions should be unambiguous, finite and
provide a clear termination point. An algorithm accepts
some input values and produces outputs. The algorithm
can be built in simple English language or in programming language. This subject helps in solving and analysing complex problems through logical thinking.
4.2 ALGORITHM
An algorithm is a well-defined procedure which takes a
set of values as an input and produces a set of values as
the output.
Chapter 4.indd 177
1. An algorithm has zero or more inputs.
2. It should produce at least one output.
3. It should terminate within the finite time.
4. It should be deterministic (ambiguous).
5. It is generic to all programming languages.
4.2.2 Steps to Solve a Problem
1. Define the problem, which means what is input
and what should be the output.
2. Identify the constraints which are to be satisfied.
3. Design the algorithm for a given problem.
4. Draw the flowchart.
5. Verify the process.
6. Implement the coding.
7. Perform time and space analysis.
4/9/2015 9:54:03 AM

178 Chapter 4: Algorithms
4.2.3 Algorithm Analysis
If a problem has more than one solution, then the technique used to decide which solution is best is known as
algorithm analysis.
4.2.3.1 A Posteriori and A Priori Analysis
Analysis is done on two parameters: time complexity and
space complexity. Time complexity means time taken
by an algorithm to solve a problem. Space complexity
means memory space required by an algorithm to solve
a problem. There are two types of analysis:
1. A Posteriori Analysis: It is a software- and
hardware-dependent analysis. It keeps on changing from one system to other system. This type of
analysis tells the exact time and space complexity,
meaning how much an algorithm takes time and
space for solving a problem on the given system.
2. A Priori Analysis: It is a software- and hardware-independent analysis. It is constant for all the
systems. This type of analysis tells the approximate
time and space complexity. It is the determination
of the order of magnitude of a statement, meaning
how many times a statement is executing.
4.2.3.2 Asymptotic Analysis
Suppose there are given two algorithms for a task, how
do we find out which one is better?
This can be done by actually implementing both the
algorithms and running them on the same computer with
different input values. Then the comparison is made in
terms of time. Algorithm that takes less time is considered
better. But there are so many problems with this approach.
1. It might be possible that for certain inputs first
algorithms performs better than second and for
other set of inputs second algorithm performs
better. So,the decision of choosing best among
them becomes difficult.
2. With the change in operating machine, performance
of algorithm varies. On one machine first algorithm
can work better and on another machine, the second
works better. This is another problem associated
with this approach.
Example 4.1
Let us consider an example of a search problem
(searching a given item) in a sorted array. One way to
search is linear search (the order of growth is linear)
and the other way is binary search. Suppose we have
two machines with different processing speeds. We run
linear search on the fast machine and binary search on
slow machine. For small input size, the linear search
will perform better. But as we increase the size of
input, binary search will perform better. The reason
behind this is,the order of growth of binary search is
logarithmic and that of linear search is linear. So, by
using this technique the issues of machine dependency
are resolved.
4.2.4 Asymptotic Notation
Asymptotic notation is based on two assumptions, which
hold in most of the cases and have their importance.
It is very much important to understand the assumptions and limitations of asymptotic notations:
1. Input size: It is interesting to know how the running time of an algorithm grows for a large input
size n.
2. Constant: The running time of an algorithm also
depends on various constants. But for a large input
n, the constant factors would be ignored.
For representing best-case, average-case, and worst-case
complexity, there are three different notations as given
in the following sub-sections.
4.2.4.1 Theta Notation (Q)
Theta notation shows the tightest upper bound and the
tightest lower bound to the given function (Fig. 4.1). It is
used to define the average-case running time. For a given
function f(n), we denote Q(g(n)) as Q(g(n)) = {f(n):
there exists positive constants c1 > 0, c2 > 0 and n0 > 0
such that 0 < c1(g(n)) ≤ f (n) ≤ c2 (g(n)) ∀ n ≥ n0}
c1g(n)
f(n)
Above issues can be handled using asymptotic analysis techniques for analysing algorithms. In asymptotic
analysis, analysis of algorithm in done in terms of input
size. Actual running time is not computed, variation of
time with input size is calculated.
Now, understand that how the issues with above
technique are resolved by asymptotic analysis technique
using the below example:
Chapter 4.indd 178
f(n)
c2g(n)
g(n)
n
n0
Figure 4.1 | Theta notation (Q)
4/9/2015 9:54:04 AM

## Leave your Comments