algorithms
algorithms
Sumesh S S
The words process and algorithm are very important for computers. By process we mean a
number of meaningful actions to be carried out to perform a particular task by the computer
and algorithm controls the process and causes it to take place.
Recipe
Imagine a kitchen, containing a supply of ingredients, an array of baking utensils, an oven,
and a (human) baker. Baking a delicious raisin cake is a process that is carried out from the
ingredients, by the baker, with the aid of the oven, and, most significantly, according to the
recipe. The ingredients are the inputs to the process, the cake is its output, and the recipe
is the algorithm. The recipes, or algorithms, relevant to a set of processes under discussion
are generally called software, whereas utensils and oven represent what is generally known as
hardware. The baker, in this case, can be considered a part of the hardware.
1
Algorithm
The word algorithm is derived from the name of the Persian Mathematician Mohammed al-
Khowarismi who is credited with providing the step-by-step rules for adding, subtracting,
multiplying, and dividing ordinary decimal numbers. When written in Latin, the name became
Algorismus, which means small steps. The algorithm prescribes the activities that constitute
the process. An algorithm contain carefully selected elementary instructions (step by step
procedure) that prescribe the basic actions to be performed.
The area of human study, knowledge and expertise that concerns algorithms is called algo-
rithmics. While the recipe, is the algorithm; the formal written version of the recipe, such as
the one found in a cook book, is analogous to a computer program. Software actually refers
more to programs - precise representations of algorithms written in special computer-readable
languages
The great Greek mathematician Euclid (between 400 and 300 B.C) invented an algorithm
for finding the greatest common divisor (gcd) of two positive integers, known as the Euclidian
algorithm, is considered to be the first non-trivial algorithm ever devised.
An algorithm can be thought of as being executed by a little robot, or a processor(call
it as Runaround). The processor receives orders to run around doing this and that, where the
“this and thats” are the basic actions of the algorithm. An algorithm must contain control
instructions to “push” the processor in this or that direction, telling it what to do at each step
and when to stop and say “I am done.”
Examples
Algorithm (1) : Sum of first 100 natural numbers.
Input : The numbers 1, 2, 3, . . . , 100. A variable called sum to store the sum of the given
numbers. Initially the value of sum will be zero.
Output : 5050.
Working : After step 1 the number is 1, the sum is 1. Then going to step 2. Now the number
is 2, sum is 3. Then to step 3, but it pulls back to step 2. Then the number is 3, the sum is 6.
Again performing step 3, then coming back to step 2, that number became 4 and sum becomes
10. This process continues till the number becomes 100 and sum became 5050. Again going
to step 3 which redirects to step 2. Since we have reached the last number, no action will be
performed. So we move to step 4, that will bring the value of sum, which is 5050.
2
Algorithm (2) : Largest of two given numbers.
2. find diff = a − b.
3. check whether diff > 0, if yes, goto 3.1, otherwise goto 3.2.
4. print max.
Output : maximum of a and b.
Input : The number N. Two variables called i and sum to store the number and sum of the
numbers 1, 2, 3 . . . , N. Initially the value of sum will be zero and i = 1.
1. Read the value of N.
Control Structures
Sequence control in an algorithm is usually carried out with the aid of various combinations
of instructions called control-flow structures, or simply control structures. Important control
structures are Direct sequencing, Conditional branching, iterations(or looping con-
structs) etc.
• Direct sequencing: has a general form “do A followed by B” or “do A and then B”.
In Algorithm (1), the statements “Add the first number to sum” and “Visit the next
number and add it to sum” are examples for direct sequencing.
3
Iterations
An algorithm containing only sequencing and branching can prescribe processes of at most some
fixed length, since no part of such an algorithm is ever executed more than once. There are
control constructs prescribing ever-longer processes by executing some statements a number of
times. They are genetically called iterations, or looping constructs. They are of two types:
• Bounded iteration: Iterations of the form “do A exactly N times” where N is a positive
integer are bounded iterations. Here the statements in A will be executed at most N times,
where N is previously known.
In Algorithm (3), the statement, “do the following N times” is a bounded iteration.
• Nested iterations(nested loops): nested loops means ‘a loop inside a loop’. General
form is “do A exactly N times”, where A itself is, say, of the form “repeat B until C.”
• The “Goto” Statement: the general form is “goto G”, where G marks some point in the
text of the algorithm. That is an instruction “goto k” direct the processor literally to go
to the line k of the algorithm and resume execution from there.
An algorithm that contains many “goto” statements directing control backwards and
forwards in a tangled fashion.
4
Data Types and Data Structures
Data elements are of various types. Some of the most common data types present in algorithms
are numbers of various kinds (integers, decimals, binary numbers, and so on), and words written
in various alphabets. An algorithms tells us what objects are to be manipulated and precisely
which manipulations are allowed. Manipulation includes not only carrying out operations but
also asking questions, such as testing whether a number is even or whether two words start
with the same letter etc.
Bubble sorting
Sorting is one of the most interesting topics in algorithmics. Bubblesort is one of the many
known sorting algorithms and is based on the following observation. “If the jumbled list is
traversed in sequence, one element at a time and whenever two adjacent elements are found to
be in the wrong order (that is, the first is larger than the second) they are exchanged, then on
completion of the traversal, the largest element is in its rightful place”: namely, at the end of
5
the list.
Bubble sorting algorithm (1) Input N
(2) Input a vector of values V [1], V [2], · · · V [N]
(3) Do the following N − 1 times:
(1.1) X ← 1;
(1.2) while X < N do the following:
(1.2.1) if V [X + 1] < V [X] then exchange them;
(1.2.2) X ← X + 1.
Since N − 1 comparisons are to be done in each traversal the outer loop is executed N − 1 times
and the total number of executions are (N − 1)2 .
We can improve the algorithm in such a way that on every repeated execution of the outer loop,
the inner loop checks one element less. That is, the first traversal is of N − 1 elements, the
next is of N − 2 elements, and so on. Hence the total number of comparisons are N(N − 1)/2.
The improved Bubblesort version (1) Input N
(2) Input a vector of values V [1], V [2], · · · V [N]
(3) X ← 1;
(4) If X ≤ N − 1 do the following:
(4.1) Y ← 1;
(4.2) If Y ≤ N − X do the following:
(4.2.1) If V [Y ] > V [Y + 1], exchange V [Y ] and V [Y + 1]
(4.2.2) Y ← Y + 1
(4.3) X ← X + 1
(5) Output the sorted values.
6
Recursion
The ability of a subroutine to call itself is known as recursion. Recursion in computer science
is a method where the solution to a problem depends on solutions to smaller instances of the
same problem.
Queue
A queue is a linear list of information that is accessed in first-in, first-out order which is
sometimes called FIFO. That is, first item placed in the queue is the first item retrieved. This
is the only means of storage and retrieval in a queue.
To visualize how a queue works, let us consider two functions, say qstore() and qretrieve().
qstore() places an item onto the end of the queue and qretrieve() removes the first item from
the queue and returns its value. The retrieval operation removes an item from the queue and
destroys it if it is not stored elsewhere; and a queue will be empty after all items have been
removed. The table given below shows the effect of a series of these operations.
Action Contents of Queue
qstore(A) A
qstore(B) AB
qstore(C) ABC
qretrieve() returns A BC
qstore(D) BCD
qretrieve() returns B CD
qretrieve() returns C D
Queues are used in many programming situations. One of the most common is in simula-
tions. Queues are also used by the task sheduler of an operating system and for I/O buffering.
Stacks
A stack is the opposite of queue, in the sense that it uses last-in first-out(LIFO) accessing. To
implement a stack we need the following functions: push() and pop() . push() places a value on
the stack and pop() retrieves a value from the stack (the retrieval function destroys the value
if it is not stored elsewhere).
A region of memory is needed to use the stack. We can use an array for this purpose or
allocate a region of memory using C’s dynamic allocation function.
Stacks are used frequently in system software, including compilers and interpreters.
The action of push() and pop() in a stack is given below
7
Action Contents of Stack
push(A) A
push(B) BA
push(C) CBA
pop() retrieves C BA
push(F) FBA
pop() retrieves F BA
pop() retrieves B A
pop() retrieves A empty
Trees
One of the most important and prominent data structures in existence is the tree. A tree is
essentially a hierarchical arrangement of data. One item resides in a special place called the
root, and the others are organized as the root’s offspring, or its offspring’s offspring, etc. In
computer science, trees are visualized “upside down” - the root at the top, and the rest of the
tree spread out below.
B C D
E F G H I J
K L M N O P Q
The points in the tree are called its nodes. A node having no offspring is a leaf. A sequence
of nodes corresponding to downward traversals in the direction from the root to a leaf is called
a path or branch.
If B is an offspring of A, then A is said to be the parent of B and B is said to be a child or
descendant of A. A leaf has no child. Two nodes are siblings if they have the same parent.
In the above tree, A is the parent of B, C and D. So B, C are siblings. Similarly, K, L are
siblings.
The level of a node in a tree is defined as follows:
• The level of any other node is one greater than the level of its parent.
8
That is the offsprings of root are at level 2 and their offsprings are at level 3 and so on. The
depth of a binary tree is the maximum level of any leaf in tree. The depth of the above tree
is 4. (K, L, M, . . . , Q are at level 4).
The number of offsprings of a node is called its outdegree.
Binary trees
Binary tree is a tree in which each node has at most two offspring. That is each node has
outdegree at most 2. If the root has two offsprings, one is called the left offspring and the
other, the right offsprings. The left offspring and its offsprings form a left subtree. Similarly
we have right subtree also.
B C
D E F
G H I
Here the left subtree is rooted at B and the right subtree is rooted at C. The absence of a
branch indicates an empty subtree.
Thus, two most common kinds of family trees are the “ancestor tree”, which starts at an
individual and works back through parents, grandparents, and so on, and the “descendant
tree” which works forward through children, grandchildren, and so on. ( In computer science,
trees are usually visualized “upside down”-the root at the top, and the rest of the tree spread
out below.)
A binary tree in which every nonleaf node has nonempty left and right subtrees is called a
strictly binary tree. A strictly binary tree with n leaves always contains 2n − 1 nodes.
A complete binary tree of depth d is a strictly binary tree, all of whose leaves are at
level d. Example of a strictly binary tree of depth 4 is
B C
D E F G
H I J K L M N O
9
Binary Search tree: A binary search tree is a binary tree having the following properties
1. The contents of all nodes in the left subtree of a node n are less than the contents of node
n.
2. The contents of all nodes in the right subtree of a node n are greater than or equal to the
contents of node n
10
3 15
2 8 14 18
1 7 9 16 20
5 17
4 6
Construct a binary search tree for the following list
128 76 106 402 100 46 354 1018 112 28 396 41 35
Treesort
Following are the two main steps in a treesort
(1) transform the input list into a binary search tree T
(2) traverse T in a left-first fashion and output each element on the second visit.
Subroutine: second-visit-traversal-of T
(1) if T is empty then return;
(2) otherwise (i.e., if T is not empty) do the following:
(2.1) call second-visit-traversal-of left(T );
(2.2) output the data element found at the root of T;
(2.3) call second-visit-traversal-of right(T );
(3) return.
10