Sei sulla pagina 1di 29

Design & Analysis of Algorithms

Lecture#01:
Introduction, Algorithmic Structures
Lecture Contents
History
Definition
Algorithm Examples
Algorithm & Programming
Algorithms, Data Structures, Implementation Issues
Random Access Machine for Modeling Algorithms
Algorithmic Structures
 Indentation,
 Loop Structures & End of Loops
 Comments
 Arithmetic Expressions & Assignments
 Swapping,
 GOTO & Return Statement,
 Statement End,
 No Global Variables,
 Array Length,
 Parameters Passing

Algorithmic Functions & Permutations


History (Algorithm)
Originated from name of Muslim scientist Abu Ja’far Mohammad ibn Musaal-
Khowarizmi
Born in eighth century at Khwarizm, Uzbekistan
Parents migrated to a place south of Baghdad when he was a child
Flourished under Khalifa Al-Mamoon during 813-833 C.E.
Much of work is written in his book al Kitab al-mukhatasar fi hisab al-
jabrwa’l-muqabalah
Words Algebra & Algorithms were derived his book’s title
Died in 840 C.E.
Algorithm
“An algorithm is any well-defined computational procedure that takes some values,
or set of values, as input and produces some value, or set of values, as output”
Or simply:
An algorithm is thus a sequence of computational steps that transform the input
into the output

Definition from: Introduction to Algorithms by Thomas H. Cormen and others

“A procedure for solving a mathematical problem in a finite number of steps that


frequently involves repetition of an operation”
Or simply:
 An algorithm is a step-by-step procedure for solving a problem or
accomplishing something
Definition from: webster.com
An Algorithm
An Algorithm
Algorithm & Programming
Algorithm
Basic element of programming
A mathematical entity independent of programming languages, operating system &
compiler
Scientific way of handling general programming techniques
Project / Software
Software projects (small, large or huge) have macro issues & micro issues
Macro issues are the subject of software engineering practices
Micro issues involve variables, controls, I/O, data conversion, classes, functions,
interfaces, exceptions, threads etc.
Often a small part of a project has major difficulty in programming the code
Algorithm & Programming …
 Base of successful implementation often lies in efficient implementation of critical
section
 Path taken by most programmers:
1. Choose inefficient algorithm / data structure
2. Implement it
3. Try to fine-tune it using smart tricks or by implementing it on most expensive
Note: Expensive machines (with added features, better hardware) or any fine
tuning cannot compensate for inefficiency of a bad algorithm
 An algorithm for particular a problem is better than others since it uses efficient
data structures and vise versa
 Many subjects of your program base upon efficient algorithms including
programming fundamentals, OOP, DS&A, OS, AI, Databases, Compilers, Machine
Vision, Data Mining etc
Implementation Issues
Algorithm is a mathematical object independent of programming language, OS
and infrastructure specific details but practically these details can be very
important
Though we normally ignore these details but a really good algorithm designer
will keep in mind the features on offer
Another example can be choice of sorting algorithms insertion, selection, bubble
or even merge sort, heap sort & quick sort
While Designing Algorithms
They need to be understood by people (not by machines)
Algorithms need to be simple, and modifiable in case parameters are changed
Flexibility of present-ability
Need mathematical model of computation to present an algorithm
Reasonable generic abstraction of single processor machine (RAM)
Quantitative measures to evaluate & compare algorithms
Algorithms are measured in terms of space, time & other resources
Most important factor is time (Why?)
There may be other important factors as ease of debugging, maintainability
Random Access Machine (RAM)
An idealized machine with an infinitely large random-access memory
Instructions are executed one-by-one (no parallelism)
Each instruction involves performing some basic operation in the machines
memory
Basic operations include things like assigning a value to a variable, computing
any basic arithmetic operation, performing any comparison, Boolean
operations and/or accessing an element of an array (e.g. A[10])
We assume that each basic operation takes the same constant time to execute
Random Access Machine (RAM)
Though it seems to be a good model for measuring computational power
of most modern machines but
1. It does not model some elements, such as efficiency due to locality of reference
2. There are some “loop-holes” to beware of e.g. the model would allow you to add
two numbers that contain a billion digits in constant time
3. It makes theoretically possible to derive nonsensical results in the form of
efficient RAM programs that cannot be implemented efficiently on any machine
Nonetheless, the RAM model seems to be fairly sound, and has done a
good job of modelling typical machine technology since the early 60’s
Algorithmic Structures … Code Block
Indentation level for a block of code is associated with
body of that code-block
Algorithmic Structures … Loop Structures
A loop structure starts with the name of loop and ends on end-loop-name.
For example, a for loop body starts with for keyword and ends with end-
for
Similarly, a while loop body starts with while keyword and ends with
end-while
Algorithmic Structures … Expressions
Plus, Minus, Multiplication, Division (a+b, a-b, a*b, a/b)
Remainder (a mod b)
Relational operator (a = b (equals), a < b, a <= b, a > b, a >= b etc.)
Logical operators (AND, OR, NOT, both capital cases and small cases are fine)
Assignment Operator (x  a+b)
Algorithmic Structures … Comments
Comments
Same as C / C++ comments i.e. use of // & /*….*/

Example:
Proc calculateFactor(a, b, c as integers)
factor  (-b+sqrt(power(b, 2) – 4*a*c))/(2*a)
//In above line, we calculate one factor of quadratic equations whose
//coefficients as provided as parameters
Return factor
End findSum
Algorithmic Structures
GOTO Label
GOTO statement with label defines transfer of control while execution
Algorithmic Structures … Procedure Boundaries
Procedure Start and End Proc isPrime(value)
factor as integer
A procedure starts with the keyword proc
followed by procedure name If value = 2 then
return true
End Procedure-Name indicates the end of
procedure body end if
factor  2
Return statement is used to return control and
while factor <= value / 2
result to caller statement
do
if value mod factor = 0 then
return false
end if
factor  factor + 1
end while
return true
End isPrime
Algorithmic Structures … Statement End
End of line is statement terminator
No semi-colon (like C++/Java) is used at end of statements

Example:
Proc findSum(a, b, c as integers)
Sum  a + b + c
Return Sum
End findSum
Algorithmic Structures … No Global Variables
All variables used in algorithms are local to procedures
If global variables are used (due to any reason), they will be explicitly
mentioned
Algorithmic Structures … Array Elements
Array Elements
Array elements are accessed same as C / C ++ syntax e.g. values[5]
Array index starts with 1 instead of 0

Example:
Proc printAll(A as array)
for x = 1 to |A| do
print A[x]
End-for
End findSum
Algorithmic Structures … Parameter Passing
All parameters to a function are passed by value i.e. any change in
parameter values does not affect actual variables
Algorithmic Structures … Structure Size
If S is a structure (an array or a collection), |S| is the size of structure
Algorithmic Functions … Ceil, Floor

Example:
x  5.3
Ceil will return 6
Floor will return 5
Algorithmic Functions … Power
Expression Simplification
Xa.Xb Xa+b
Xa/Xb Xa-b
(Xa)b Xab
Xa+Xa 2Xa
2a.2a 22a
Algorithmic Functions … Logarithmic Expressions
Expression Simplification
X = log2 y 2x=y
Logab logcb / logca
log ab log a + log b
log a/b log a – log b
(log a)b b log a

Note: While studying computer science we normally consider logarithmic


functions with base value 2
Permutations
A permutation of a set of distinct objects is an ordered arrangement of
these objects.

Order of elements matters


Values (in set) must be distinct
A permutation can use either few or all the elements of a set
If all values of set have to be used for a permutation, overall n! (n factorial)
permutations exist
For example for set {a, b, c} all permutations are: abc, acb, bac, bca, cab, cba
Permutations and Combinations
An ordered arrangement of r elements of a set is called an r-permutation.

The number of r-permutations of a set with n elements is denoted by P(n, r)


One can calculate P(n, r) using the formula:
P(n, r) = n!/(n-r)!
One can calculate P(n, r) also by:
P(n, r) = n * (n-1)*(n-2)*(n-3)*…..*(n-r+1)
Permutations and Combinations … Examples
In how many ways we can arrange three flags from five different
flags?
In how many ways can five flags be arranged?
Write down all 2-permutations from set S = {x, y, z}
How many ways are there to select a winner and a runner up from
a race of 10 athletes? (There are no ties)

Potrebbero piacerti anche