20IS42 Module-1
20IS42 Module-1
(20IS42)
Module 1
By,
MANJESH R,
Assistant Professor,
IS&E, VVCE, Mysuru - 02
Manjesh R, ISE, VVCE, Mysuru-02
Manjesh R, ISE, VVCE, Mysuru-02
Manjesh R, ISE, VVCE, Mysuru-02
Manjesh R, ISE, VVCE, Mysuru-02
What is Algorithm?
An algorithm is a sequence of unambiguous
instructions for solving a problem, i.e., for obtaining a
required output for any legitimate input in a finite
amount of time.
Problem
Algorithm
3. Blocks are indicated with matching braces: { and }. A compound Statement can
be represented as a block. The body of a procedure also forms a block.
6. Input and output are done using instruction read & write
Manjesh R, ISE, VVCE, Mysuru-02
7. There are two Boolean values true and false. In order to produce these values, the logical
operators and, or, and not and the relational Operators <,<,=,/,>,and > are provided.
10. The following looping statements are employed for, while and repeat, until.
break : Exit from loop (inner/outer) & return : exit of Function
As an example, the following algorithm finds and returns the maximum of n given numbers:
Algorithm Max (A, n)
// Input: An array A of size n
// Output: Maximum of array element
{
Result A[i]
for i 2 to n do
if A[i] > Result
then Result A[i]
return Result
}
• The value of the second integer eventually becomes 0, and the algorithm stops.
• Note that unlike Euclid’s algorithm, this algorithm, in the form presented, does
not work correctly when one of its input numbers is zero. This example
illustrates why it is so important to specify the set of an algorithm’s inputs
explicitly and carefully.
• What is the largest number p whose multiples can still remain on the list to
make further iterations of the algorithm necessary?
• Obviously, p . p should not be greater than n, and therefore p cannot exceed
“√n “ rounded down (denoted “ √ n “ using the so-called floor function).
• Sorting
• Searching
• String Processing
• Graph Problems
• Combinatorial Problems
• Geometric Problems
• Numerical Problems
• Why sorting?
• Help searching
• Algorithms often use sorting as a key subroutine.
• Sorting key
• A specially chosen piece of information used to guide sorting. E.g., sort
student records by names.
• Two properties
• Stability: A sorting algorithm is called stable if it preserves the relative
order of any two equal elements in its input.
• In place : A sorting algorithm is in place if it does not require extra
memory, except, possibly for a few memory units.
• Examples:
(i) searching for a word or phrase on WWW or in a Word document
(ii) searching for a short read in the reference genomic sequence
• Informal definition
• A graph is a collection of points called vertices, some of which are
connected by line segments called edges.
Geometric Problems
• Geometric algorithms deal with geometric objects such as points, lines, and
polygons.
• Some more examples are:
• Computer graphics
• Robotics
• Tomography – medicine
• Closest-pair problem
• Convex-Hull problem
• The best-case inputs for sequential search are lists of size n with their
first element equal to a search key; accordingly,
Cbest(n) = 1
for this algorithm.
• The analysis of the best-case efficiency is not nearly as important as that
of the worst-case efficiency. But it is not completely useless, either.
• The second notation, Ω(g(n)), stands for the set of all functions with a higher or same
order of growth as g(n) (to within a constant multiple, as n goes to infinity).
• For example,
• Finally, Θ(g(n)) is the set of all functions that have the same order of growth as g(n) (to
within a constant multiple, as n goes to infinity). Thus, every quadratic function
an2 + bn + c with a > 0 is in Θ (n2)
Manjesh R, ISE, VVCE, Mysuru-02
O (Big Oh)- notation
n! factorial Permutation
Indirect Recursion: A
function which contains a
call to another function,
which in turn calls another
function, ……. so on and
eventually calls the first
function.
• General Case: this case occurs if one or more discs have to be transferred from
source to destination.
• If there are n discs, then all n discs can be transferred recursively using following
3 steps:
Consider
Manjesh R, ISE, VVCE, Mysuru-02
Manjesh R, ISE, VVCE, Mysuru-02