Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
UNIT – I INTRODUCTION
1. Define algorithm.
Algorithm
ALGORITHM Euclid(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ← m mod n
m←n
n←r
return m
5. Define time efficiency and space efficiency.
Time efficiency, indicating how fast the algorithm runs.
Space efficiency refers to the amount of memory units required by the algorithm in addition to
the space needed for its input and output.
Let cop be the execution time of an algorithm’s basic operation on a particular computer, and let
C(n) be the number of times this operation needs to be executed for this algorithm. Then we can
estimate the running time T(n) of a program implementing this algorithm on that computer by the
formula
T(n) ≈ cop C(n).
11. Define Order of growth.
The order of growth of the running time of an algorithm gives a simple characterization of the
algorithm's efficiency and also allows us to compare the relative performance (efficiencies) of
alternative algorithms.
xn+1 = f(xn)
for some function f.
16. Define Big Oh Notation. Give examples.
A function t(n) is said to be in O(g(n)), denoted t(n) ∈ O(g(n)), if t(n) is bounded above by some
constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some
nonnegative integer n0 such that
t(n) ≤ cg(n) for all n ≥ n0.
Examples:
n ∈ O(n2), 100n + 5 ∈ O(n2), n(n − 1) ∈ O(n2).
Examples:
n3 ∈ Ω(n2), n(n − 1) ∈ Ω(n2).
18. Define Big Theta Notation. Give examples.
A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈ Θ(g(n)), if t(n) is bounded both above
and below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive
constants c1 and c2 and some nonnegative integer n0 such that
c2 g(n) ≤ t(n) ≤ c1 g(n) for all n ≥ n0.
Example:
n(n − 1) ∈ Θ(n2).
= (1 - )
=
Since the limit is equal to a positive constant, the functions have the same order of growth or,
symbolically, n(n − 1) ∈ Θ(n2).
22. Give the general plan for Analyzing the Time Efficiency of Recursive Algorithms.
Decide on a parameter (or parameters) indicating an input’s size.
Identify the algorithm’s basic operation.
Check whether the number of times the basic operation is executed can vary on different
inputs of the same size; if it can, the worst-case, average-case, and best-case efficiencies
must be investigated separately.
Set up a recurrence relation, with an appropriate initial condition, for the number of times
the basic operation is executed.
Solve the recurrence or, at least, ascertain the order of growth of its solution.
23. Give the general plan for Empirical Analysis of Algorithm Time Efficiency.
An algorithm visualization can be defined as the use of images to convey some useful
information about algorithms. That information can be a visual illustration of an algorithm’s
operation, of its performance on different kinds of inputs, or of its execution speed versus that
of other algorithms for the same problem.
There are two principal variations of algorithm visualization:
Static algorithm visualization
Dynamic algorithm visualization, also called algorithm animation
UNIT II BRUTE FORCE AND DIVIDE – AND – CONQUER
= or ( and )
= min { , + } for k ≥ 1,
= .
Need to compute the result of same The key is remembering because it stores
3
subproblem more than one time. the result of sub-problems in a table.
The sub-problems are independent of each The sub-problems are not independent of
4
other. each other.
9. What is meant by All-Pairs Shortest path problem?
Given a directed, connected weighted graph G(V,E), for each edge ⟨u,v⟩ ∈ E, a weight w(u,v) is
associated with the edge. The all pairs shortest paths problem (APSP) is to find a shortest path from u
to v for every pair of vertices u and v in V.
x 1 ≥ 0, . . . , x n ≥ 0.
It contains exactly one vertex with no entering edges; this vertex is called the source and
assumed to be numbered 1 or s.
It contains exactly one vertex with no leaving edges; this vertex is called the sink and
assumed to be numbered n or t.
The weight uij of each directed edge (i, j) is a positive integer, called the edge capacity.
For example, (Bob, Lea) is a blocking pair for the marriage matching M = {(Bob, Ann),
(Jim, Lea), (Tom, Sue)} because they are not matched in M while Bob prefers Lea to Ann and Lea
prefers Bob to Jim.
13. Define Cut of a graph.
A cut of a graph is defined as by partitioning vertices of a network into some subset X
containing the source and , the complement of X, containing the sink is the set of all the edges with a
tail in X and a head in . We denote a cut C(X, ) or simply C. The capacity of a cut C(X, ), denoted
c(X, ), is defined as the sum of capacities of the edges that compose the cut.
4. Define class P.
Class P is a class of decision problems that can be solved in polynomial time by (deterministic)
algorithms. This class of problems is called polynomial.
Deterministic (“verification”) stage: A deterministic algorithm takes both I and S as its input
and outputs yes if S represents a solution to instance I.
For example, for A = {1, 2, 5, 6, 8} and d = 9, there are two solutions: {1, 2, 6} and {1, 8}.
r( ) =
27. Write down greedy algorithm for the continuous knapsack algorithm.
Step 1 Compute the value-to-weight ratios ri = vi /wi , i = 1, . . . , n, for the items given.
Step 2 Sort the items in nonincreasing order of the ratios computed in Step1.
Step 3 Repeat the following operation until the knapsack is filled to its full capacity or no item
is left in the sorted list:
If the current item on the list fits into the knapsack in its entirety, take it and proceed to
the next item; otherwise, take its largest fraction to fill the knapsack to its full capacity
and stop.
These algorithms are capable of finding in seconds a tour that is within a few percent of
optimum for large Euclidean instances of the traveling salesman problem.
_________