Algorithms_Midsems[1]
Algorithms_Midsems[1]
January 2025
CSE201
First things first!!
Evaluation plan
Quiz 1: 10 marks
Mid semester: 32 marks
Quiz 2: 10 marks
End semester: 48 marks
Introduction (5 hours)
Definition and characteristics(1), Algorithm design and pseudocode(2),
Flowchart(2)
Analysis of Algorithms (5 hours)
Time and space complexity(1),Asymptotic notation(1.5),Recurrence relation(1.5)
Divide and Conquer paradigm (8 hours)
Strategy(1),Binary search(1),Quicksort(2),Mergesort(2),Order statistics(1),Matrix
multiplication(1)
Greedy Algorithms (8 hours)
Strategy(1),Fractional knapsack(1),Minimum Spanning Tree(2), Single source
shortest Path(1.5),Activity Selection(1.5),Optimal Merge Patterns(2)
Core issue
Problem solving by a computer.
Core issue
Problem solving by a computer.
Core issue
Problem solving by a computer.
A computer needs to be
programmed to do tasks.
Program: sequence of
instructions to do a task,
computer processes the
instructions sequentially one
after the other.
Programming is the process of
writing instructions in a
language that can be
understood by the computer so
that a desired task can be
performed by it.
Problem
Find the maximum of N integers.
The problem statement does not give us the steps for solving it.
Why steps?
Because the computer does not understand a visual expression! Given
a snap of a series of numbers, the computer cannot determine the
maximum by inspection.
The notion of steps is inherent to the structure/architecture of a
computer.
Similarly the notion of efficiently organizing the data in computer
memory for efficient access and manipulation is also inherent to the
construct of the computer.
Our task is to design the steps and plan the organization of the data
for efficient implementation of the solution to a problem.
(January 2025) Introduction CSE201 14 / 68
Array in memory
Problem
Find the maximum of N integers.
Efficient
Running time
Space used
Efficiency as a function of input size
The number of bits in an input.
Number of data elements.
In this course, we shall focus on efficiency in terms of running time.
Code 1
READ(n)
sum <- 0
LOOP: i <- 1 to n, 1
READ(value)
sum <- sum + value
PRINT(sum)
n n2 n2 + n 2n2 + n + 2
5 25 30 57
50 2500 2550 5052
500 250000 250500 500502
5000 25000000 25005000 50005002
50000 2500000000 2500050000 5000050002
.. .. .. ..
. . . .
Best case is the smallest possible time your algorithm will take on an
input of size n.
Worst case is the maximum possible time your algorithm will take on
an input of size n.
Average case is the average of all the possible run times of your
algorithm on any input of size n.
(January 2025) Introduction CSE201 38 / 68
Best/Worst/Average case
For functions f (n) and g (n) there are positive constants c and n0 such
that: 0 ≤ f (n) ≤ cg (n) for n ≥ n0
Conclusion:
2n + 6 is O(n)
n2 is not O(n).
There is no c and n0 such that:
n2 ≤ cn for n ≥ n0
The graph to the left shows that
no matter how large a c you
choose, there is always an n big
enough that n2 > cn
Procedure prefixAverages(A)
Input: An n-element array X of numbers.
Output: An n-element array A of numbers such that A[i]
is the average of elements X[0], ..., X[i].
for i <- 1 to n do
a <- 0
for j <- 1 to i do
a <- a + X[j]
A[i] <- a / j
return array A
Abuse of notation: f (n) = O(g (n)) actually means f (n) ∈ O(g (n)).
Analogy with real numbers
f (n) = O(g (n)) ∼
=f ≤g
f (n) = Ω(g (n)) ∼
=f ≥g
f (n) = Θ(g (n)) ∼
=f =g
LIN_SEARCH(A,k,key,n) // k is an index
IF k > n:
RETURN -1
IF A[k] = key:
THEN RETURN k
ELSE
LIN_SEARCH(A,k+1,key)
What will be the complexity for an input array A of size n for the call
LIN SEARCH(A, 1, key , n)?
LIN_SEARCH(A,k,key,n) // k is an index
IF k > n:
RETURN -1
IF A[k] = key:
THEN RETURN k
ELSE
LIN_SEARCH(A,k+1,key)
What will be the complexity for an input array A of size n for the call
LIN SEARCH(A, 1, key , n)?
Let the number of comparisons for an input size n be T (n).
If n = 1, the number of comparisons is 1 or T (1) = 1.
If n = 2, the number of comparisons is T (2) = 1 + T (1) = 2.
...
T (n) = T (n/2) + c
= c + c + T (n/4)
...
= c + c + c + · · · + c (log2 n times)
= clog2 n
= O(log2 n)
√
T (n) = 2T (⌊ n⌋) + log n and T (1) = 1 for n = 2m
where T (n) is the time complexity of the algorithm for an input of size
n.
a = number of sub-problems in the recursion
n/b = size of each subproblem
f (n) represents the time complexity of combining the solutions of the
subproblems and any additional work done outside the recursive calls.
It provides a simple way to determine the time complexity based on
the values of ’a’, ’b’, and the complexity of ’f (n)’.
It has three cases, depending on how ’f (n)’ compares with nlogb a
08/01/2025 1
Student Grade Calculation
• Input:
• Scores for exams and assignments.
• Output:
• The final grade.
08/01/2025 2
FlowChart/Algorithm
• Flow chart/ Algorithm:
• A step-by-step procedure for solving a particular problem.
• Should be independent of the programming language.
• Program
• A translation of the algorithm/flowchart into a form that can be processed by a
computer.
• Typically written in a high-level language like C, C++, Java, etc.
08/01/2025 3
Flowchart Symbols
Input/ Output
Condition / Decision Box
Computation
08/01/2025 4
Adding two numbers
Start
Read A, B
S= A+B
Print S
Stop
08/01/2025 5
Celsius to Fahrenheit
Start
Read C
F=C*(9/5)+32
Print F
Stop
08/01/2025 6
Greatest of two numbers
Start
Read A, B
If
A>B
Print A Print B
Stop
08/01/2025 7
Greatest of 3 Numbers
Start
Read A, B, C
If A > B
If A > C
If B > C
Print B Print A
Print A Print C
08/01/2025 8
Sum of first N natural numbers
Start
Read N
SUM=0
Count=1
SUM=SUM+count
Count=Count+1
NO Is Output
YES
Count> SUM
N?
Stop
08/01/2025 9
SUM = 12+ 22+ 32+ N2
Start Start
Read N Read N
SUM=0 SUM=0
Count=1 Count=n
SUM=SUM+(count*count) SUM=SUM+(count*count)
Count=Count+1 Count=Count-1
Is NO Is YES Output
NO YES Output
Count> Count<= 0 SUM
SUM ?
N?
Stop Stop
08/01/2025 10
SUM =1.2 +2.3+3.4 to N terms
Start
Read N
SUM=0
Count=1
SUM=SUM+count*(count+1)
Count=Count+1
NO Is Output
YES
Count> SUM
N?
Stop
08/01/2025 11
Computing Factorial
Start
Read N
Prod=1
Count=1
Prod=Prod*count
Count=Count+1
NO Is Output
YES
Count> SUM
N?
Stop
08/01/2025 12
Practice
• Prime number
• Even and odd
• Any other series like exponential series
• Fibonacci series
08/01/2025 13
Divide and Conquer