Algorithms
Set of well define rules or recipe for solving some computation problems
You have bunch of number and you want to rearrange them may be in sorted order
Why Study Algorithms?
Important for all branches of computer science
Understanding the basics of algorithms and data structures are essentials in performing
serious work in computer science
Examples
Routing and communication networks based on shortest path algorithms
Computer graphics need geometric algorithms
Database industry relies on balanced search tree data structures
Computation biology uses dynamic programming algorithms to measure GNOME similarities
and the list goes on…
Search engines like google uses the page rank algorithms currently
Introduction: Integer
multiplications
1. We all did learn multiplication in our basic classes in which we did get 2 numbers as input and we calculate output
2. So we knew some sets of predefined steps to multiply which is actually an algorithm
Input
: two n-digit number x,y
The Grade-School Algorithm
In third grade what we called this algorithms
is Correct one.
No matter what integer you use in x and y if
you carry out this procedure/algorithm
All intermediate computation done properly
Then the algorithm terminate with the
product of x.y.
But you probably did not think about was the
amount of time needed to carry out this algorithm
22712
17034X
5678
X 1234
11356XX
5678XXX
Informal
Analysis of our Multiplication
Algorithm
1. Let’s begin with the first partial product the top row
2. How did we compute this number
1. Well we multiply 4 with each number 5678
2. So there were 4 basic operations
3. Plus we have to deal the carry outs for additions
1. Almost twice timed the number of digits at the first <=2n Operations
4. Still we need to add these rows
22712
17034X
5678
X 1234
11356XX
5678XXX
7006652
<= 2n Operations
n rows
2n(operations each) x n (rows)
Informal
Analysis of our Multiplication
Algorithm
1. So # of operations overall <= constant . n2
The Algorithm Designer’s Code
Perhaps the most important principle for the good algorithm designer is to refuse to be content.”
Aho, HopcroF, and Ullman, The Design and Analysis of Computer Algorithms, 1974
Can we do better?
Karatsuba Multiplication
Step1: compute a.c=672
Step2: computer b.d=2652
Step3: compute (a+b)(c+d)=134x46=6164
Step4: step3-step2-step1=2840
Step5:
6720000
2652
284000
7006652
X = 5 6 7 8
Y = 1 2 3 4
b
c a
Guiding Principal #1
Worst Case Analysis (Big Oh)
our running time bound holds for every input of n or our max time
Average case analysis
analyze the average running time of the algorithm
Bench marks
Worst case analysis (Big Oh
Notation)
We do not take assumptions
Particularly appropriate for general purpose algorithms about which we do not have enough knowledge
Guiding Principal #2
We won’t pay attention to the constants
Justification:
Way easier if we ignore constants
Constants depend on architecture/compiler/programmer own way
Guiding Principal #3
Asymptotic Analysis:
we focus on large values rather smaller ones mean large input size Only big problems are interesting like how big problems we can solve while computer arebecoming faster and faster
Like merg sort time is say 6nlog2n+n
Justification of principal #3
Insertion Sort
Justification of principal #3
Insertion Sort
What is a Fast Algorithm?
we will consider the three guiding principal for this purpose
Asymptotic Analysis (Motivation)
Basic vocabulary for the analysis and design of algorithms e.g. Big Oh notation
Sharp enough to make comparison between algorithms especially for large inputs
High Level Idea
Example: Loop
Problem: search an integer in array Given A an array of length n
And t an integer
For i=1 to n
If(a[i]==t) return true Else
rerutn false
What is the running time of this algorithm?
1. Big O(n) 3. Big O(nlogn)
Example: Two Loops
Problem: search an integer in both arrays
Given A,B an array of length n
And t an integer
For i=1 to n
If(a[i]==t) return true
For i=1 to n
If(b[i]==t) return true Else rerut false
What is the running time of this algorithm?
Example: Nested Loops
Problem: search for duplicate values in arrays Given A,B arrays of length n
And t an integer
For i=1 to n
For j=1 to n
If(a[i]==b[j]) return true Else rerun false
What is the running time of this algorithm?
1. Big O(n) 3. Big O(nlogn)
Example: Nested Loops (II)
Problem: search for duplicate values in single array Given A arrays of length n
And t an integer
For i=1 to n
For j=i+1 to n
If(a[i]==a[j]) return true Else rerun false
What is the running time of this algorithm?
Lower bound (Big Omega)
T(n)= Ω(f(n))
If and only if constants c and no
such that