0% found this document useful (0 votes)
7 views

algorithms

algorithms an introduction
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

algorithms

algorithms an introduction
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Algorithms and Data

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.

Figure 1 Baking a cake

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.

1. Add the first number to sum.

2. Visit the next number and add it to sum.

3. Repeat step 2 until we reach the last element.

4. Print the value of sum.

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.

Input : Two numbers a, b, two variables called sum and diff.


1. Read the values of aand b.

2. find diff = a − b.

3. check whether diff > 0, if yes, goto 3.1, otherwise goto 3.2.

3.1 assign max = a.


3.2 assign max = b.

4. print max.
Output : maximum of a and b.

We modify Algorithm (1) and write a more general one.

Algorithm (3) : Sum of first N natural numbers.

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.

2. do the following N times

2.1 add i to sum


2.2 increase the value of i by 1

3. Print the value of sum.


Output : The value of 1 + 2 + 3 + · · · + 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.

• Conditional branching: is of the form “if Q then do A” or “if Q then do A otherwise


do B” where Q is a condition.
In Algorithm (2), the statements “check whether diff ¿ 0, if yes, goto 3.1, otherwise goto
3.2.” is a conditional branching.

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.

• Conditional iteration(unbounded iteration): general form is “repeat A until Q” or


“while Q do A” where Q is a condition.
For example, the statement like “Enter some integers and enter 0 to stop”.

• 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.

Combining Control Structures


An algorithm can contain many control-flow constructs in nontrivial combinations. Sequencing,
branching, and iteration can be interleaved and nested within each other. Algorithms can
contain nested iterations, or nested loops. Nested loop means, a loop inside a loop. Its general
form is “do A exactly N times,” where A itself is, say, of the form “do B until Q.” The
processor executing such a segment has to work quite hard; each of the N times it carries out
A - that is, each time the outer loop is traversed - the inner loop must be traversed repeatedly,
until Q becomes true. Here the outer loop is bounded and the inner one conditional, but other
combinations are possible too. The A part of the outer loop can contain many further segments,
each of which can, in turn, employ additional sequencing, branching, and iteration constructs.
Thus, there is no limit to the potential intricacy of one’s algorithms.

Diagrams for Algorithms


Visual, diagrammatic techniques are one way of presenting the control flow of an algorithm in
a clear and readable fashion. One of the best of drawing algorithms is writing the elementary
instructions in rectangular boxes, the tests (decisions) in diamond shaped boxes, the start(stop)
is indicating in rectangles with rounded edges and using arrows to describe how the processor
Runaround runs around executing the algorithm. The resulting objects are called flowcharts.

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.

Variables, or Little Boxes


The word “variable” is used to denote a cell that can contain different values at different
times. Many variables with different names are used in an algorithm for different purposes
When variables are used in practice, we write things like X ← 0 (read “X gets 0” or
“set Xto 0”) to set the initial value of X to 0). Similarly, the instruction X ← X + 1 (read
“X gets X + 1′′ ) tells the processor to “read” the number found in X, increase it by one, and
replace it with the result.

Vectors or Lists or One-dimensional arrays


If a variable is like a hotel room, then a vector can be thought of as an entire corridor, or floor,
in the hotel. The entire corridor or a floor has a name (say “floor 3”), and the rooms therein
can be accessed by their index. For example, the 15th room in that floor is 315 and the X
th room is 3X. This means that we can use a variable to index the vector. Here we use the
notation V [X] for the element of V whose index is the current value of the variable X

Two dimensional arrays or Tables


In many cases it is convenient to arrange the data not in a simple, one-dimensional list, but in
a table. The corresponding algorithmic data structure is called a matrix, or a two-dimensional
array, or simply an array. Referring to an array element is typically achieved using two indices,
row and column. Notations like A[X, Y ] is used represent the value stored in the element of A
corresponding the indices.
If a variable is like a hotel room and a vector is like a hotel corridor/floor, then a matrix, or
an array, is like an entire hotel. Its “rows” are the various floors and its “columns” represent
the locations along the corridor/floor.

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.

Subroutine (Procedure or Function)


The idea of subroutine is to write a searching loop or a subprogram only once, with a parameter
that is assumed to contain the particular combination of symbols searched for and it is activated
(or invoked, or called) more than once in the main algorithm(program). When told to “call”
a subroutine, the processor will stop whatever it has been doing, remember where it was, pick
up the parameter(s), so to speak, move over to the text of the subroutine and do whatever the
subroutine tells it to do. When the subroutine tells the processor to return, it will do just that;
namely, it will return to the point following the “call” that led it to the subroutine in the first
place, and will resume its duties from there.
The Virtues of Subroutines: Subroutines can be very economical as far as the size of an
algorithm is concerned and this economy becomes considerably more significant for complex
algorithms with many subroutines that are called from various places. Apart from its economical
advantages a subroutine can be viewed as a “chunk” of algorithmic material, some kind of
building block, which once formed, can be used in another algorithmic chunk by a single
instruction.

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 root is at level 1

• 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

Example of a binary search tree


Let us consider the input list 10 15 3 8 18 7 2 9 16 6 20 1 5 17 4 14
The first element in the list is taken to be the root. All other elements are arranged as per the
instructions given above.

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

You might also like