THE ROLE OF ALGORITHMS IN COMPUTING · Introduction to Algorithm (3rd Edition) by Thomas H. Cormen,...
Transcript of THE ROLE OF ALGORITHMS IN COMPUTING · Introduction to Algorithm (3rd Edition) by Thomas H. Cormen,...
THE ROLE OF ALGORITHMS IN
COMPUTING
Instructor: Dr. Faisal Anwer, DCS, AMU
SOURCE:
Introduction to Algorithm (3rd Edition) by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein + Freely Accessible Web Resources
ALGORITHM
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.
We can also view an algorithm as a tool for solving a well-
specified computational problem.
Al-Khwarizmi referred algorism as the rules of performing
arithmetic using Hindu–Arabic numerals and the systematic
solution of linear and quadratic equations.
An algorithm is said to be correct if, for every input instance, it
halts with the correct output.
Algorithms are the threads that tie together most of the subfields
of computer science.
Calling something an algorithm means that the
following properties are all true:
An algorithm is an unambiguous description
An algorithm expects a defined set of inputs.
An algorithm produces a defined set of outputs.
An algorithm is guaranteed to terminate and produce
a result, always stopping after a finite time.
Most algorithms are guaranteed to produce the correct
result.
If an algorithm imposes a requirement on its inputs
(called a precondition), that requirement must be met.
KINDS OF PROBLEMS ARE SOLVED BY
ALGORITHMS
Finding good routes on which the data will travel ininternet enabled world.
Search engine to quickly find pages on which particularinformation resides.
We are given two ordered sequences of symbols, X =(x1,x2,…..) and Y(y1, y,….) and we wish to find alongest common subsequence of X and Y .
Finding route with minimum distance from one node toother node in a weighted graph.
Manufacturing and other commercial enterprises oftenneed to allocate resources in the most beneficial way. Anoil company may wish to know where to place its wellsin order to maximize its expected profit.
EXAMPLES OF ALGORITHM
Simple algorithm for finding maximum in a list ofpositive nos.
Problem: Given a list of positive numbers, return the largestnumber on the list.
Inputs: A list L of positive numbers. This list must contain atleast one number.
Outputs: A number n, which will be the largest number of thelist.
Algorithm
1. Start
2. Declare max
3. Set max to 0.
4. For each number x in the list L perform following
1. compare x to max.
2. If x is larger, set max to x.
5. max is now set to the largest number in the list.
6. Stop
EXAMPLES OF ALGORITHM(CONT…)
Does this meet the criteria for being an
algorithm?
Is it unambiguous? Yes.
Each step of the algorithm consists of primitive operations,
and translating each step into any language is very easy.
Does it have defined inputs and outputs? Yes.
Is it guaranteed to terminate? Yes.
The list L is of finite length, so after looking at every
element of the list the algorithm will stop.
Does it produce the correct result? Yes.
In a formal setting you would provide a careful proof of
correctness.
PROBLEMS VS ALGORITHMS VS PROGRAMS
For each problem or class of problems, there may
be many different algorithms.
For each algorithm, there may be many different
implementations (programs).
EXPRESSING ALGORITHMS
An algorithm may be expressed in a number of
ways, including:
natural language: usually verbose and ambiguous
flow charts: avoid most issues of ambiguity; difficult
to modify without specialized tools; largely
standardized.
pseudo-code: also avoids most issues of ambiguity;
vaguely resembles common elements of programming
languages; no particular agreement on syntax
programming language: tend to require expressing
low-level details.
PSEUDOCODE
High-level description of an algorithm.
More structured than plain English.
Less detailed than a program.
Preferred notation for describing algorithms.
Hides program design issues.
PSEUDOCODE
Control flow
if … then … [else …]
while … do …
repeat … until …
for … do …
Method declaration
Algorithm method (arg [, arg…])
Input …
Output
Method call
method (arg [, arg…])
Return value
return expression
Expressions
Assignment (equivalent to )
Equality testing(equivalent to )
n2 Superscripts and other mathematical formatting allowed
EXAMPLE: FIND THE MAX ELEMENT OF AN ARRAY
Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A
Max A[0]
for i 1 to n 1 do
if A[i] Max then
Max A[i]
return Max
ALGORITHMS AS A TECHNOLOGY
Suppose computers are infinitely fast and computer memory
are inexpensive.
Do we still need to study Algorithm and its
analysis ?
Yes, at least to check that the algorithm terminates and
produce correct result.
Of course, computers may be fast, but they are not
infinitely fast.
And memory may be inexpensive, but it is not free.
Computing time and memory are therefore bounded
resources.
EXERCISES
Suppose we are comparing implementations of insertion
sort and merge sort on the same machine. For inputs of
size n, insertion sort runs in 8n2 steps, while merge sort
runs in 64nlgn steps. For which values of n does
insertion sort beat merge sort?
What is the smallest value of n such that an algorithm
whose running time is 100n2 runs faster than an
algorithm whose running time is 2n on the same
machine?
SUMMARY
Knowledge of algorithm distinguishes the truly
skilled programmers from the novices.
Some tasks can be accomplished without knowing
much about algorithms, but with a good
background in algorithms, you can do much, much
more.
Next Task
Computing time and space in memory are bounded
resource.
We should use the resources wisely, and algorithms that are
efficient in terms of time or space will help you do so.