Lec 1 Week 1
Lec 1 Week 1
Lec 1 Week 1
Lecture - 1
SE AA-262
Introduction
What is Algorithm?
a clearly specified set of simple instructions to be followed to
solve a problem
Takes a set of values, as input and
produces a value, or set of values, as output
May be specified
In English , As a computer program , As a pseudo-code
Data structures is methods of organizing data in memory
Program = algorithms + data structures
Introduction
Finiteness
The process should be terminated after a finite number of steps.
Effectiveness
Every instruction must be basic enough to be carried out theoretically or by
using paper and pencil.
Definiteness
Each instruction of the algorithm should be clear and unambiguous.
Input
An algorithm should have some specified set of inputs n
Output
At least one output should be returned by the algorithm after the
completion of the specific task based on the given inputs.
Correctness
Complexity analysis of an algo
Some algorithms are more efficient than others. We would prefer to chose an
efficient algorithm, so it would be nice to have metrics for comparing
algorithm efficiency.
The complexity of an algorithm is a function describing the efficiency of the
algorithm in terms of the amount of data the algorithm must process.
There are two main complexity measures of the efficiency of an algorithm:
Time complexity
Space complexity
Time complexity is a function describing the amount of time an algorithm takes in
terms of the amount of input to the algorithm.
For example, to decide which sorting algorithm will take less time to run
They are easily identifiable in pseudocode and largely independent from the programming language
In the analytical approach running time is measured by using primitive
operations.
They contribute much to overall time of algorithm
Example
An algorithm is said to
run in logarithmic
time if its time
execution is
proportional to the
logarithm of the input
size.
instead of increasing
the time it takes to
perform each
subsequent step, the
time is decreased at a
magnitude that is
inversely proportional
to the input “n”.
Example !!
Consider this example: let’s say that you want to look for a word in a dictionary
that has every word sorted alphabetically. There are at least two algorithms to
do that:
Algorithm A:
• Starts at the beginning of the book and goes in order until it finds the contact you are
looking for.
Algorithm B:
• Opens the book in the middle and checks the first word on it.
• If the word that you are looking for is alphabetically bigger, then it looks in the right half.
Otherwise, it looks in the left half.
Q: Which one of both is faster? While algorithm A goes word by word O(n),
algorithm B splits the problem in half on each iteration O(log n), achieving the
same result in a much more efficient way.
Quadratic Time
Complexity: O(n²)
Algorithm sum(a[ ] , n) 0
{ 0
S= 0; 1
for i=0 to n do n+1
0
s=s + a[ i ]; n
Return s ; 1
} 0
for(int i=0;i<=N;i++)
Let's calculate the time complexity for this particular loop-
assumption-> 1 unit for each comparison, initialization
and arithmetic operation
-- for initialization (i=0) 1
-- for comparison (i<=N) N+1
-- for incremental operator (i++) N
}
Memory Usage
version of instructions
another algorithm