You are on page 1of 22

Inception to Algorithms

Course Title: Advanced Algorithm


Analysis
• Purpose: A rigorous introduction to the
design and analysis of algorithms

• Textbook: Introduction to Algorithms

– Author : Cormen, Leiserson, Rivest, Stein


– 2nd edition:
What is an Algorithm ?
• An Algorithm is a sequence of
unambiguous instructions that takes some
values (or set of values) as input &
produce some value (or set of values) as
output in a finite amount of time.

• An algorithm is a finite step-by-step


procedure to achieve a required result
Examples Of Algorithms
• LINEAR SEARCH
– To search a particular element from a list of
elements
• SUM
– A procedure that calculates sum of finite numbers
• FIND MAX:
– Algorithm for finding largest value in a list of
elements
• BUBBLE SORT:
– Algorithm that sorts a list of numbers
REAL WORLD EXAMPLES
• To setup alarm on the clock
• A Lecturer checks test
• A Student gives an exam
• Play a football match
• Fetch a glass of water
Algorithm Comprises Of
•Basic Idea
•Input
•Intermediate set of
instructions
•Output
NOTION OF ALGORITHM
Problem

Algorithm

Input “Computer” Output

Algorithmic solution
EXAMPLE: SORTING
Basic Idea:
To sort a list of numbers
Input:
A sequence of n numbers <a1, a2, …, an>
Output:
A sorted form of the input sequence
<b1, b2, …, bn> such that
bi ≤ bj whenever i < j
EXAMPLE:
SORTING(contd..)
Sorting algorithms:
• Selection sort?
problem • Insertion sort?
• Merge sort?
• …
algorithm

input “computer” output


<5, 3, 2, 8, 3> <2, 3, 3, 5, 8>
Algorithm Requirements
1. Finiteness
terminates after a finite number of steps
2. Definiteness
Precise and unambiguously specified
3. Input
valid inputs are clearly specified
4. Output
can be proved to produce the correct output
given a valid input
5. Effectiveness
steps are sufficiently simple and basic
What is a Pseudocode ??
• High-level description of an algorithm

• More structured than English prose

• Less detailed than a program

• Preferred notation for describing algorithms

• Easy to understand as compared to programs


Example Of Pseudocode
• FIND-MAX ALGORITHM:
– Algorithm for finding maximum value in a list of
integers
• PSEUDOCODE
Algorithm FIND-MAX(A, n)
Input : array A of n integers
Output : Maximum element of array A

CurrentMax ← A[0]
for i ← 1 to n − 1 do
if A[i] > currentMax then
currentMax ← A[i]
return currentMax
Example Of Pseudocode
A Linear Search Algorithm
Basic Idea:
Traverse the list and compare each element of the list
with the element that we are searching.

Input:
A list of n elements and the element to be searched
Output
Returns Location of the searched element if found
If not found,
returns -1
Pseudo-Code Of Linear Search
Algorithm
Important Points Regarding
Algorithms
• Algorithms should be non-ambiguous

• Range of Inputs has to be specified carefully

• Several algorithms for one problem

• Algorithms of same problems can have different


speeds of execution
METRICS TO JUDGE AN
ALGO
What metric should be used to judge algorithms?

– Length of the program (lines of code)


– Ease of programming (bugs, maintenance)
– Memory required
– Running time

Running time is the dominant standard.

– Quantifiable and easy to compare


– Often the critical bottleneck
Running time/ Time Complexity
Definition :
Time that algorithm takes to execute is the running
time of an algorithm.

• Running time is determined for a range of inputs to


check the maximum time that algorithm takes to execute

• Running Time= Sum of running time taken by each


instruction
Example
• Algorithm arrayMax(A, n) Operations Count

currentMax ← A[0] 1
for i ← 1 to n − 1 do 2n=1+1n+1(n-1)

if A[i] > currentMax then (n − 1)


currentMax ← A[i] (n − 1)

return currentMax 1

Total Running Time = 1+2n+(n-1)+(n-1)+1


= 4n unit time
Running Time

• Best Case
• Lower bound on running time

• Worst Case
• Upper bound on running time

• Average Case
• Expected running time
Running Time
Input size vs Running Time
Generalizing Running Time
Comparing the growth of the running time as the input grows to the growth of known
functions.

Input (1) nlog n n n² n³ 2ⁿ


Size:
n
5 1 3 5 25 125 32

10 1 4 10 100 10³ 10³

100 1 7 100 104 106 1030

1000 1 10 1000 106 109 10300

10000 1 13 10000 108 1012 103000

You might also like