Professional Documents
Culture Documents
Unit 1 Introduction To Algorithms: Structure
Unit 1 Introduction To Algorithms: Structure
1.1 Introduction
We use algorithms to solve problems in a systematic way. It is difficult to
give a single specific definition for an algorithm. Different fields of
development and study use algorithms to solve their own problems.
Levitin defines algorithm as given below:
“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.”
This unit covers the various definitions of algorithm, its types, properties and
the steps for designing it. This unit gives a brief idea on solving a problem
using different algorithmic problem solving techniques. It also introduces
various data structures, their classification and characteristics.
Objectives:
After studying this unit, you should be able to:
define ‘Algorithm’
explain the role of algorithm in computing
describe the fundamentals of algorithms
list the important types of algorithms
describe the fundamental data structures
5) Do it again for all items except the last two items, and so on.
6) Sort the array when there are only two items left and they have been
swapped (if necessary).
The C program for bubble sort is given below:
Activity 1
Write a sorting algorithm, its psuedocode and the program in any
language.
Theta notation – For non-negative functions, f(n) and g(n), f(n) is theta of
g(n) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)). We denote this as
"f(n) = Θ(g(n))". This implies that the function, f(n) is bounded both from the
top and bottom by the same function, g(n).
Little-O notation – For non-negative functions, f(n) and g(n), f(n) is little o of
g(n) if and only if f(n) = O(g(n)), but f(n) ≠ Θ(g(n)). We denote this as
"f(n) = o(g(n))". This represents a loose bounding version of Big O. g(n)
bounds from the top, but it does not bound the bottom.
Little Omega notation – For non-negative functions, f(n) and g(n), f(n) is
little omega of g(n) if and only if f(n) = Ω(g(n)), but f(n) ≠ Θ(g(n)). We denote
this "f(n) = ω(g(n))".
1.4.2 Time and space complexity and efficiency
The efficiency of algorithms strongly depends on the time and space
complexity. Let us see how it affects the algorithm.
Time complexity – It is a function that describes the time of execution of an
algorithm based on its input parameters. In complex algorithms it is difficult
to find the exact time taken. Therefore we find the order of the functions
expressed in Big-O notation using the asymptotic notations. Complexity
classes are equivalence classes of time complexities which are equal in
Big-O notation. There are meta-complexity classes of time complexities
which have Big-O notations that differ only by some specific parameter. For
instance, O(n2) and O(n3) are both polynomial time complexity classes,
similarly O(2n) and O(3n) are exponential time complexity classes.
The time T(p) taken by a program P is the sum of the compile time and the
runtime. The compile time does not depend on the instance characteristics.
Also we may assume that a compiled program will run several times without
recompilation. So we concern ourselves with just the runtime of a program.
This runtime is denoted by tp. Therefore time complexity can also be known
as the number of instructions which a program executes during its run time.
Space complexity – Space complexity is defined as the amount of memory
that an algorithm needs. A good algorithm minimizes the memory needed.
The space needed by algorithm is the sum of the following components:
A fixed part that is independent of the characteristics of the input and
outputs. This part typically includes the instruction space, space for
Sikkim Manipal University B1480 Page No. 9
Analysis and Design of Algorithms Unit 1
simple variables and fixed size component variables, space for constant
and so on.
A variable part that consist of the space needed by components
variables whose size is dependent on the particular problems instance
being solved, the space needed by referenced variables and the
recursion stack space.
The space requirement S(p) of any algorithm P may therefore be written as
S(p) = C+ Sp(instance characteristics), where C is a Constant.
Self Assessment Questions
7. An algorithm that invokes itself within the process is called _______.
8. ______ is the method of expressing the upper bound of an algorithm's
running time.
9. _____________ is defined as the number of memory cells which an
algorithm needs.
Heuristic: A thumb rule that helps you decide which possibilities to look
at first.
Optimization: A way to eliminate certain possibilities without fully
exploring them.
1.5.8 Randomized algorithms
A randomized algorithm or a probabilistic algorithm uses a random number
at least once during the computation to make a decision. It employs a
degree of randomness as part of its logic.
Examples of randomized algorithm are:
Quick sort uses a random number to choose a pivot
Trying to factor a large prime by choosing random numbers as possible
divisors
Self Assessment Questions
10. A ____________ algorithm converts the solution to a simpler sub
problem to arrive at the correct solution.
11. A ______________ algorithm simply tries all possibilities until a
satisfactory solution is found.
12. ___________ algorithms are based on a depth-first recursive search.
Queue – It is a first in first out (FIFO) data structure. The basic operations
on a queue are enqueue and dequeue. Enqueue is used for insertion and
dequeue is used for retrieval of elements
List – It is a sequence of data elements connected to each other where
every element has a link field which refers to the location of the next
element. Different types of lists are linked list, doubly linked lists, skip list,
jump list etc.
Tree – It is a hierarchical representation of elements as nodes linked with
each other. The fist element of the tree structure is known as the root. Each
node has a sub node known as child node. The node without a child node is
known as leaf node. Binary tree is a type tree structure.
Homogeneous and heterogeneous data structures – Non primitive data
structures can be homogenous or heterogeneous. Homogeneous data
structure has all the elements of same data type in it. Example of a
homogeneous data structure is an array. Heterogeneous data structure
contains different types of data elements in it. Example of a heterogeneous
data structure is a record.
Static and dynamic data structures – We classify data structures as static
and dynamic considering the time of memory allocation. The system
allocates memory for static data structure at the compile time itself (design
time allocation). For a dynamic data structure memory is allocated during
run time (through functions such as calloc(), malloc()).
Linear and non-linear data structures – We classify data structures as
linear and non linear based on the type of relationship between its elements.
Linear data structure such as arrays and lists maintains a constant and
direct relationship between its elements. Non-linear data structures such as
a tree do not maintain a constant and direct relationship between their
elements.
1.6.4 Data structure characteristics
Choosing the best data structure for a program is a challenging task. Same
task may have suitable different data structures for it. We derive new data
structures for complex tasks using the already existing ones. We need to
compare the characteristics before choosing the right data structure. The
table 1.1 shows the characteristics of the commonly used data structures.
Activity 2
Write a program to sort two arrays and identify the data structures used
in it.
1.7 Summary
Let us summarize what we have discussed in this unit.
An algorithm is defined as a set of instructions describing the sequence of
actions to achieve the result of the problem with limited resources. The word
‘algorithm’ is derived from the name of Abu Abdullah Muhammad ibn Musa
al-Khwarizmi, a 9th-century Persian mathematician.
1.8 Glossary
Terms Description
Subroutine A program unit which does not return any value through
its name and has a number of arguments.
Optimization Problem Optimization problem is a computational problem which
has the objective to find the best of all possible solutions.
1.10 Answers
Self Assessment Questions
1. Algorism
2. Speed, size, resources
3. Good programming
4. Resources
5. Merging
6. Programs
7. Direct recursive
8. Big – O
9. Space complexity
10. Simple recursive
11. Brute force
12. Backtracking
13. Data structure
14. Homogeneous
15. Static
Terminal Questions
1. Refer section 1.3.2 – Properties of algorithms
2. Refer section 1.4.1 – Asymptotic notations
3. Refer section 1.5.5 – Greedy algorithm
4. Refer section 1.6.3 – Data structure classification
5. Refer section 1.6.4 – Data structure characteristics
References
Cormann, Thomas (1990). Introduction to algorithms. The MIT press,
Cambridge
Levitin, A. (2003). Introduction to the analysis and design of algorithms.
Dorling Kindersley, India
Wirth, N. (1991). Algorithms + Data Structures = Programs. Jay Print
Pack publications, New Delhi
E-References
http://www.brpreiss.com
http://www.esirav.info/definitions of algorithms, By Reality Software,
copyright 2000
http://www.stackoverflow.com/stack overflow internet services.