You are on page 1of 40

The Role of Algorithms in

Computing
Chapter 1
Topics
 What is an algorithm?
 Why is the study of algorithms worthwhile?
 What is the role of algorithms relative to other
technologies used in computers?
 Analyzing an Algorithm
 Model of Computation
 Algorithm Language
 Complexity of Algorithms
Algorithms
 Informally, an algorithm is any well-defined
computational procedure that takes some
value, or set of values, as input and
produces some value, or set of values, as
output.
 An algorithm is thus a sequence of
computational steps that transform the input
into the output.
Algorithms
For example, given the input sequence
〈31, 41, 59, 26, 41, 58〉,
a sorting algorithm returns as output the
sequence
〈26, 31, 41, 41, 58, 59〉.
Such an input sequence is called an instance
of the sorting problem.
The numbers that we wish to sort are also
known as the keys.
Which Algorithm is Best?
1. The number of items to be sorted,
2. The extent to which the items are already
somewhat sorted,
3. Possible restrictions on the item values
4. Kind of storage device to be used: main
memory, disks, or tapes
What kinds of problems are
solved by algorithms?
 Human Genome Project
 Electronic commerce
 Resource Allocation
 Computational Problems and many more
 Others
Analyzing Algorithm
Analysis means predicting the resources that
the algorithm requires.
Before analysis we must know the
implementation technology/ Model of
computation.
 RAM (Random Access Machine)
 Concurrent Operations (Uni-Processor)
 Standard generic single-processor machine.
 Parallel Machines (PRAM)
Model Of Computation
 Random Access Machine
 A RAM is an idealized machine with an infinity
large random-access memory.
 Instruction are executed one by one (there is
no parallelism).
 Abstract machine which has an unlimited
number of registers of unlimited size which
can be accessed randomly
Random Access Machine
cont.
 The RAM model contains instructions commonly
found in real computers:
 Arithmetic (Add, subtract, multiply, divide,
remainder floor, ceiling),
 Data movement (load, store, copy)
 Control (Conditional and unconditional
Control(Conditional and unconditional branch)
 * Each instruction takes a constant amount of
time
Parallel Random Access
Machine
 PRAM stands for Parallel Random Access
Machine, which is an abstract machine for
designing the algorithms applicable to parallel
computers.
 It eliminates the focus on miscellaneous issues
such as synchronization and communication.
 In terms of Flynn‘s, PRAMs are multiple
instruction multiple data computers (MIMDs).
Algorithm Selection Criteria
 Correctness
Compute valid input for a finite amount of time
and produce a right output
 Amount of Work Done
How much data is processed in a unit of time
 Amount of space used
How much memory is used
 Simplicity
 Optimality
Optimal Algorithm can save both time and space
Space-Time Trade Off
 Refers to choice between algorithm that
allows one to decrease the running time
of an algorithm solution by increasing the
space to store and vice-versa
Algorithmic Language
 Indentation indicates block structure. e.g body of loop
 Looping Constructs while, for and the conditional if-
then-else
 The symbol ► indicates that the reminder of the line is a
comment.
 Arithmetic expressions may use usual arithmetic op (+,
-,*,/, ). Relational expression may use relational
operators ( =, ,, and ) logical ops and, or and not
Algorithmic Language
cont.
 Assignment statements are
a←b
a ← b ← c ( Multiple Assignment)
 Swapping
a ←→ b
 GOTO
GoTo Lable
loop : if c then do
s
goto loop
end
Algorithmic Language
cont.
 An exit statement may be used to terminate a while or
for loop
 Return is used to indicate termination of an algorithm.
 Algorithm language mostly relies on end of lines rather
than semi colons. Several short stmts (eg, assignment
stmt) may be written on one line separated by
semicolons.
 Local variables are used in a procedure. We shall not
use global variables without explicit indication.
Algorithmic Language
cont.
 If A is a structure then |A| size of structure
 If A is an Array then n=legth[A] upper bound
of array of array.
 All Array elements are accessed by name
followed by index in square brackets.
A[i]
 Parameters are passed to a procedure by
values.
Mathematical Notations &
Functions
 Floor Function
X called the floor of x, denotes the
greatest integer that does not exceed x.
 Ceiling Function

X called the ceiling of x, denotes the


least integer that is not less than x.
Mathematical Notations &
Functions
 Permutation
Set of n elements is an arrangement of the
elements in given order
e.g. Permutation for elements are a, b, c
abc, acb, bac, bca, cab, cba
n! permutation exist for a set of elements
5! = 120 permutation for 5 elements
Mathematics Review
Mathematics Review
 Arithmetic Series
A sequence of numbers in which each differs from the preceding one by a
constant quantity (e.g. 1, 2, 3, 4, etc.; 9, 7, 5, 3, etc.).

 Quadratic Series
A quadratic sequence is a sequence of numbers in which the second
difference between any two consecutive terms is constant.
Mathematics Review
 Geometric Series
The terms of a geometric series form a geometric progression, meaning that
the ratio of successive terms in the series is constant.
is geometric, because each successive term can be obtained by multiplying the
previous term by 1/2

1/2+1/4+1/8+1/16+--------
Loop invariants (Property)
We use loop invariants to help us understand why
an algorithm is correct. We must show three things
about a loop invariant
 Initialization: It is true prior to the first iteration
of the loop.
 Maintenance: If it is true before an iteration of
the loop, it remains true before the next iteration.
 Termination: When the loop terminates, the
invariant gives us a useful property that helps
show that the algorithm is correct.
Analysis of an algorithm
 Time taken by the algorithm depends on the
input
 Sorting a thousand numbers takes longer
than sorting three numbers
 Take different amounts of time to sort two
input sequences of the same size depending
on how nearly sorted they already
Analysis of an algorithm
 input size depends on the problem being
studied
 The most natural measure is the number of
items in the input—for example, the array
size n for sorting.
 Sometimes, it is more appropriate to describe
the size of the input with two numbers rather
than one
Analysis of an algorithm
 If the input to an algorithm is a graph, the
input size can be described by the numbers
of vertices and edges in the graph.
Analysis of an algorithm
 The running time of an algorithm on a
particular input is the number of primitive
operations or “steps” executed
 Time taken by an algorithm grows with the
size of the input
 Running time of a program as a function of
the size of its input
 We need to define the terms “running
time” and “size of input” more carefully.
Complexity of Algorithm
- Each instruction executes within const
time
e.g. Assignment Statement x=a
....................1
x= a+b*c/h-u ......1
a>b ......................1
If Statement

Complexity= ConstTime(bool exp) + max(ThanPart /else Part)

e.g. if (a>b) than .........1


a=2......................1
else
x=x+2*3 .................1

T(n)= 1+1 = 2
= O(1) Or O(c)
If Statement
e.g.
if (a>b) than……..........1
a=2.......................1
b= b+3*t...............1
else
x=x+2*3 ..............1

T(n)= 1+2 = 3
= O(1)
LOOP
For n complete iterations
- while ............n
- for loop ..........n+1
- repeat..until ....n
An Example: While LOOP

i=0 .............................1
while i<=n ..................n
i++ ........................n-1
a=i ...........................1

T(n)=1+n+n-1+1
= 2n+1
= O(n)
An Example: While LOOP
i=1 ...........................1
while i<=n ................n
i++ ........................n-1
a=i ............................1
T (n)= 1+n+(n-1)+1 = 2n+1 = O(n)
OR
∑1 +1
n

T(n) = 1+ i=1

= 1+ n+1
= O(n)
An Example: While LOOP
i=1 ...............................1
while (i<=n)....................n
a=2+g........................n-1
i=i+1 ............. ..........n-1
if (i<=n)........... .............1
a=2 ..........................1
else
a=3............................1
T(n) = 1+ n+(n-1)+(n-1)+1+1
= 3n+1
= O(n)
An Example: While LOOP
i=1................................?
while (i<=10)................?
i=i+1........................?
i=1 ...............................?
while (i<=n)..................?
a=2+g......................?
i=i+1 ......................?
if (i<=n).........................?
a=2 ..........................? T (n) = ?
else
a=3...........................?
An Example: While LOOP
i=1..............................1
while (i<=10)................10
i=i+1........................9
i=1 ..............................1
while (i<=n)..................n
a=2+g......................n-1
i=i+1 .......................n-1
if (i<=n)........................1
a=2 ..........................1
else
a=3...........................1
T ( n) = 1+10+9+1+n+n-1+n-1+1+1+1
= 3n+22
= O(n)
An Example: While LOOP
for (int i=0, i<10, i++) 10+1 =11

for (int i=0, i<n, i++) n+1


Linear search
 Linear search or sequential search is a
method for finding a target value within a list.
It sequentially checks each element of the list
for the target value until a match is found or
until all the elements have been searched.
Linear Search
LinearSearch(A, Key)
1
i 1
while (A[i] !=key) and (i<=length[A]) n
n-1
i 1
if ( i<=length[A]) 1

return true 1

else
return false 1

T(n) = 1 + n + n-1 + 1 +1 = 2n+2


Linear Search- Best-case
Best case occurs when key element occurs
at first location of Array then loop executes
only once

T(n) = 1 + n + (n-1) + 1+1


= 1+1+0+1+1
= O(1)
Linear Search- Worst-case
Worst case occurs when key element is the
last element in array or is not there at all
loop executes n times
T(n) = 1 + n + (n-1) + 1 +1
= 2n +2
= O(n)

You might also like