First Example: Identifying Genes in Human DNA
Identifying all the 100,000 genes in human DNA
determining the sequences of the 3 billion(109) chemical
base pairs that make up human DNA.
Insertion sort: running time n2
3 109 3 109 instruc 9 109
9 10 seconds
9
y 31.71years
109 instruc/s 60 60 24 365
3 10 9
lg(3 10
Merge 9sort: running
9
)instruc time nlgn
3 lg(3 109 )seconds 94.45seconds
10 instruc/s
1
What are algorithms?
Why is the study of algorithms worthwhile?
What is the role of algorithms relative to
other technologies used in computers?
2
Origin of the word:-Algorithm
• The word algorithm comes from the name of the
9th century Persian mathematician
Abu Abdullah Muhammad ibn Musa al-Khwarizmi
whose works introduced Indian numerals and
algebraic concepts.
• He worked in Baghdad at the time when it was the
centre of scientific studies and trade.
• The word algorism originally referred only to
the rules of performing arithmetic using
Arabic numerals but evolved via European
Latin translation of al-Khwarizmi's name into
algorithm by the 18th century.
• The word evolved to include all definite
procedures for solving problems or
performing tasks.
1.1 What are algorithms?
Algorithm
a well-defined computational procedure
that takes some value, or a set of values, as
input and produces some value, or a set of
values, as output
Algorithm
sequence of computational steps that
transform the input into the output.
1.1 What are algorithms
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.
Algorithms
Informally,
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.
Input Algorithm Output
An algorithm is a sequence of computational steps that
transform the input into the output.
Notion of algorithm
problem
algorithm
input “computer” output
Algorithmic solution
1.1 What are algorithms
1. Algorithm is any well-defined computational procedure
Computational
Input Output
Procedure
algorithm
< 31,41,59,26,12,58 > < 31,41,59,26,12,58 >
<31,41,26,12,58,59>
<31,26,12,41,58,59>
<12,26,31,41,58,59>
……
8
1.1 What are algorithms
2. Algorithm is a tool for solving a well-specified
computational problem
Problem ? instance
general questiones (input) ?
needed to be computed input1 ?
input2 ?
input3 ? output
……
Algorithm
(a tool)
9
1.1 What are algorithms
2. Algorithm is a tool for solving a well-specified
comutational problem (example)
Problem:to sort a sequence of numbers into nondecreasing order
Input: A sequence of n numbers <a1 ,a2 , , an >
' ' '
Output: A permutation (reordering) <a1 ,a2 , , an > of the
instance
input sequence such that a1 a2 an
' ' '
uestiones (input) ?
be computed An instance of the sorting problem
input1 ?
Input: <31,41,59,26,41,58>
input2 ?
input3 ? Output: <26,31,41,41,58,59>
output
……
lgorithm
10
1.1 What are algorithms
Sorting is a fundamental operation in computer science
An algorithm is said to be correct
if, for every input instance, it halts with the correct
output
Incorrect algorithm
might not halt at all on some input instances, or
might halt with an answer other than the desired one
can sometimes be useful (if error rate can be controlled)
11
Important Features
Finiteness:- If we trace out the instructions of an algorithm, then
for all cases, the algorithm terminates after a finite
number of steps.
Definiteness: Each instruction is clear and unambiguous, it must
be perfectly clear what should be done.
Input:- Zero or more quantities are externally supplied.
Output:- At least one quantity is produced
Effectiveness: Every instructions must be very basic so that it can
be carried out, in principle, by a person using only
pencil and paper in a finite amount of time.
Performing arithmetic on integers is an example of
an effective operation but arithmetic with real
numbers is not since some values may be
expressible only by infinitely long decimal
numbers.
1.1.1 What kinds of problems are solved by algorithms
Genome Project
Internet
finding good routes; search engine
Electronic commerce
keeping information; digital signatures
Manufacturing and other commercial settings
placing wells; buying campaign advertising
Finding the shortest path from one vertex to another
Matrices product: A1 A2 A3 A4
A1 ( A2 ( A3 A4 )); A1 (( A2 A3 ) A4 ); (A1 A2 )( A3 A4 )
……
13
1.1.1 What kinds of problems are solved by algorithms
Common characteristics of many algorithms
Many candidate solutions
( one of which is best, but finding it can present quite a
challenge.)
for example, A1 A2 A3 A4
A1 ( A2 ( A3 A4 )); A1 (( A2 A3 ) A4 ); (A1 A2 )( A3 A4 )
Practical application
shortest paths: a transportation (lower labor, fuel
costs)
Theory value
14
What kinds of problems are solved by
algorithms?
• Biological Problems
• To manage and manipulate large volume of data over
Internet
• Problems based on numerical algorithms and number
theory (e.g Public-key cryptography and digital signature)
• Problems of linear programming, Dynamic programming,
Shortest Path Problems , Strings Matching Problems,
Matrix Problems etc.
1.1.2 Data structures
A data structure is a way to store and organize data in
order to facilitate access and modifications
No single data structure works well for all purposes
It is important to know the strengths and limitations of
data structure
Data structures: static
Algorithms: dynamic
The Art of Computer programming
Static + Dynamic
16
1.1.3 Technique
Purpose of studying the course
Grasping techniques of algorithm design and analysis
(chapters and sections)
Developing algorithms on your own, showing that they
give the correct answer (exercises and problems)
Understanding algorithm’s efficiency
17
1.1.4 Hard problems
A story of the barber
A story of the new manager
NP-complete problem
It is unknown whether or not efficient algorithms exist for NP-
complete problems
In the set of NPC, if an efficient algorithms exists for any one, then
efficient algorithms exist for all
Several are similar, but not identical, to problems for which we do
know of efficient algorithms.
How to show that the problem is NP-complete
Traveling-salesman problem
……
18
1.2 Algorithms as a technology
If computer infinitely fast, memory free.
Study algorithms worth?
YES! Need demonstrate that the solution method
terminates with the correct answer. ?
If any method correct, then
Implementation within the bounds of good software
engineering practice (i.e. well designed and documented);
But most, what method is the easiest to implement!
Computing time is a bounded resources, so is space
Computers may be fast, but not infinitely fast
Memory may be cheap, but not free
Resources should be used wisely
Algorithms can do so in terms of time or space
19
1.2.1 Efficiency
To same problem, algorithms often differ dramatically in their
efficiency
The differences can be much more significant than differences
due to hardware and software
Example: sorting ten million (107) numbers
Insertion sort: T (n) c1n2 Merge sort: T ( n) c2 n lg n
Computer A: 109 instruc/s Computer B: 107 instruc/s
World’s craftiest programmer Average programmer
Machine language High-level language
T ( n) 2n 2 T (n) 50n lg n
2 (107 ) 2instruc 50 107 lg107 instruc
t 9
2 105 s 55.56h t 19.38m
10 instruc/s 107 instruc/s
20
1.2.1 Efficiency
Efficient algorithm gains much more from the hardware improv
ement. For example,
Suppose that the computer makes one basic step per second. Algori
thm A runs as (nA)2 , and B as 100nB .
If an application requires you to complete as many items as
possible in one hour.
Thus if A can make nA items , and B makes nB items, then
3600 = (nA)2 = 100nB
yields nA = 60 and nB = 36.
But suppose that you replace the computer with one that is ten
times as fast. So you can make 36000 steps in the same time. Then
36000 = (nA)2 = 100nB
yields nA = 189 and nB = 360.
Algorithm B gains much more from the faster computer than A !!!
21
1.2.2 Algorithms and other technologies
Like computer hardware, algorithms are technology
(1) Algorithms are important on contemporary computers
advanced technologies, such as
Hardware with high clock rates, pipelining, and superscalar
architectures
Easy-to-use, intuitive graphical user interfaces (GUIs)
Object-oriented systems
Local-area and wide-area networking
22
1.2.2 Algorithms and other technologies
For example
a web-based service that determines how to travel
from one location to another
The implementation would rely on fast hardware, a
GUI, wide-area networking, and also possibly on OO
It would also require algorithms for certain operations,
such as finding routes, rendering maps, interpolating
address
23
1.2.2 Algorithms and other technologies
(2) Other technologies rely on algorithms
The hardware design used algorithms
The design of any GUI relies on algorithms
Routing in networks relies heavily on algorithms
(3) The program languages were processed by a
compiler, interpreter, assembler, all of which make
extensive use of algorithms (accidence analysis,
syntax analysis)
24
1.2.2 Algorithms and other technologies
Algorithms are at the core of most technologies
used in contemporary computers
Computing environment
(.net, grid, web service)
Program designing environment
Program designing language Hilbert
Turing
Program designing model
John Von Nouma
(machine, assembler, high-level language, OO
Knuth
Complexity of computing
( (Non)-Computable actually)
Computing model
(Computable, Non-computable)
25
1.2.2 Algorithms and other technologies
Algorithms are at the core of most technologies used in
contemporary computers
Having a solid base of algorithmic knowledge and
technique is one characteristic that separates the truly
skilled programmers from the novices
With modern computing technology, you can
accomplish some tasks without knowing much about
algorithms
With a good background in algorithms, you can do
much, much more !!!
26
What is Pseudocode?
• Pseudocode:- An outline of a program, written in a form
that can easily be converted into real programming
statements.
• The easiest way to express an algorithm is by
using pseudocode.
• Pseudocode is an English-like way of expressing
algorithms.
• It is more precise than "real" English, but not as
picky as a programming language.
What is Pseudocode?
• For example, the pseudocode for a bubble-sort
routine might be written:
while not at end of list
compare adjacent elements
if second is greater than first
switch them
get next two elements
if elements were switched
repeat for entire list
Difference between Pseudocode and Algorithms
• Pseudocode cannot be compiled nor executed, and there
are no real formatting or syntax rules.
• It is simply one step - an important one - in producing the
final code. The benefit of pseudocode is that it enables
the programmer to concentrate on the algorithms without
worrying about all the syntactic details of a particular
programming language.
• In fact, you can write pseudocode without even knowing
what programming language you will use for the final
implementation
Difference between Pseudocode and Algorithms
• Pseudocode consists of short readable and formally-styled
natural language used to explain specific tasks within a
program's algorithm while an Algorithm is a set of instructions
used to solve a particular problem.
• Pseudocode is a statement which is not language specific,
Pesuedocode is not syntax related, consider it a plan for the
algorithm, algorithm requires perfect syntax.
Difference between Pseudocode and Algorithms
• Algorithms can be expressed in many kinds of
notation, including natural languages,
pseudocode, flowcharts, and
programming languages.
• Natural language expressions of algorithms
tend to be verbose and ambiguous, and are
rarely used for complex or technical
algorithms.
Difference between Pseudocode and Algorithms
• Pseudocode and flowcharts are structured
ways to express algorithms that avoid many of the
ambiguities common in natural language
statements, while remaining independent of a
particular implementation language.