ANALYSIS AND DESIGN OF ALGORITHMS
(BCS401)
1
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
1
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
2
DESIGN AND ANALYSIS OF ALGORITHMS(18CS42)
Module 1
Syllabus :
Introduction:
What is an Algorithm?its properties, Algorithm Specification, Analysis Framework, Performance Analysis: Space complexity, Time complexity.
Asymptotic Notations:
Big-Oh notation (O), Omega notation (Ω), Theta notation (Θ), and Little-oh notation (o), Mathematical analysis of Non-Recursive and recursive Algorithms with Examples..
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
3
Brute Force Design Technique:
Selection sort ,sequential search, string matching algorithm with complexity Analysis
Text Book - Introduction to the Design and Analysis of Algorithms, Anany Levitin:, 2nd Edition, 2009. Pearson.
Chapter 2 and Chapter 3
4
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
4
Text Book -1
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
5
Text Book -2
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
6
Algorithm
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
7
How to prepare tea?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
8
How to prepare Maggi noodles?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
9
Shortest Distance between two cities
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
10
Module 1 – Outline Introduction to Algorithms
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
11
What is an Algorithm?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
12
13
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
13
Algorithm specification
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
14
1.Natural Language like English
For example: Euclid(m,n)
While(n!=0) do Step1: if n=0 return the value of m as the answer & stop; � otherwise proceed to step2
r<-m%n Step2:Divide m by n and assign the value of the � remainder to r
m<-n Step3:Assign the value of n to m &value of r to n goto step1
n<-r
Return m
15
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
15
2.Pseudo code convention
A pseudo code is a combination of a natural language and programming language. A pseudo code is usually more precise than a natural language.
16
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
16
Algorithm Specification for pseudo code
1.Comment is specified using //
2.Algorithm header is specified as algorithm Name with � parameter list().
3.Specify algorithms inputs and the required outputs.
4.Compound statements are specified with {} or begin…end
17
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
17
5.For if statement(branching statement)
if(condition)
statement1;
statement 2;
else
statements
endif
6.For looping statement
a)while(condition)do b)repeat c)for variable🡨value1 to value2 do
….. ----- -------
…… ------ --------
endwhile until(condition) endfor
18
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
18
19
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
19
Fundamentals of Algorithmic problem solving/
Algorithm design and analysis process
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
20
Step1.Understanding the Problem
Before designing the algorithm one must completely understand the Statement of the problem.so read the problem description carefully think about special cases. If the problem in the question is known one can use already existing algorithms otherwise design your own.It is Important to specify the exact range of inputs the algorithm must handle.
Step 2:Decide on
*Computational Devices: Once the problem is understood then we need to decide upon Computational Devices needed to implement the program.Von Neumann machine is a computer Architecture it uses RAM.Its central assumption is that instructions are executed one after the another.algorithms designed to be executed on such machines are called Sequential Algorithms. Some newer computers can execute operations concurrently i.e parallel.algorithms that take advantage of this capability are called parallel algorithms.
21
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
21
*Exact and Approximate Problem Solving:next to decide upon the whether to solve the problem Exactly or Approximately.
*Appropriate Data Structure
*Algorithm Design Technique:An algorithm design technique is a general approach to solving problems algorithmically that is applicable to variety of problems from different areas of computing.
22
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
22
Step 3:Design Algorithm
Once the technique is decided we should specify(design) the algorithm in natural Language, pseudocode ,flow chart using any of the specification.
Step 4:Prove the Correctness
once the algorithm has been specified one has to prove its correctness. that is algorithm yields a required result for every valid input in a finite amount of time.
Step 5:Analyzing an algorithm: There are two kinds of algorithm efficiency say time and space.how fast the algorithm runs and how much extra memory the algorithm needs.
Step 6:Coding an Algorithm :coding algorithm using any of the programming language.
23
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
23
Analysis Framework:
A general framework for analyzing the efficiency of algorithms. There are two kinds of efficiency Time,Space.
Space efficiency deals with the extra space the algorithm requires to run.
Time efficiency Indicates how fast an algorithm runs.
-Basic operation:is usually the most time-consuming operation in the algorithms innermost loop.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
24
Orders of Growth
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
25
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
26
Analysis Framework
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
27
Worst Case
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
28
Best Case
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
29
Average Case
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
30
Asymptotic Notations
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
31
Performance Analysis
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
32
Time Complexity
– Two ways
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
33
| |
Trade-off
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
34
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
35
Recursive Algorithm
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
36
Method-1
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
37
Method-2
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
38
Method-2
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
39
Summary of analysis framework
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
40
.
Big-Oh notation
A function t(n) is said to be in O(g(n)),
denoted t(n)∈ O(g(n)),
if t (n) is bounded above by some constant multiple of g(n) for all large n,
i.e., if there exist some positive constant c and some nonnegative integer n0 such that
t(n) ≤ c g(n) for all n ≥ n0.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
41
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
42
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
43
Problems on Big O
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
44
Omega notation
A function t(n) is said to be in Ω(g(n)), denoted t(n)∈ Ω(g(n)),
if t(n) is bounded below by some positive constant multiple of g(n) for all large n,
i.e., if there exist some positive constant c and some nonnegative integer n0 such that
t(n) ≥ c g(n) for all n ≥ n0.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
45
Big Omega t(n)= Ω(g(n))
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
46
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
47
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
48
Theta Notation
A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈
Θ(g(n)),
if t (n) is bounded both above and below by some positive constant multiples of g(n) for all large n,
i.e., if there exist some positive constants c1 and c2 and some nonnegative integer n0 such that
c2 g(n) ≤ t(n) ≤ c1g(n) for all n ≥ n0.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
49
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
50
Graphical representation
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
51
Strategies for Ω and Θ
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
52
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
53
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
54
Problem-3
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
55
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
56
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
57
Little Oh
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
58
Little Oh
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
59
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
60
Theorem: If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈
O(max{g1(n), g2(n)}).
(The analogous assertions are true for the Ω and Ө notations as well.)
Proof:
The proof extends to orders of growth the following simple fact about four arbitrary real numbers a1, b1, a2, b2:
if a1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2 max{b1, b2}.
Since t1(n) ∈ O(g1(n)), there exist some positive constant c1 and some nonnegative integer n1 such that
t1(n) ≤ c1g1(n) for all n ≥ n1.
Similarly, since t2(n) ∈ O(g2(n)), t2(n) ≤ c2g2(n) for all n ≥ n2.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
61
Theorem: If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈
O(max{g1(n), g2(n)}).
(The analogous assertions are true for the Ω and Ө notations as well.)
Proof: (continued):
Let us denote c3 = max{c1, c2} and consider n ≥ max{n1, n2} so that we can use both inequalities.
Adding them yields the following: t1(n) + t2(n) ≤ c1g1(n) + c2g2(n)
≤ c3 g1(n) + c3g2(n) = c3[g1(n) + g2(n)]
≤ c32 max{g1(n), g2(n)}.
Hence, t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}), with the constants c and n0 required by the O definition being 2c3 = 2 max{c1, c2} and max{n1, n2}, respectively.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
62
Basic Efficiency classes
Class
Name Comments
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
63
Class
Name
Comments
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
64
Quiz: Which kind of growth best characterizes each of these functions?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
65
Mathematical Analysis of Non-recursive Algorithms
If it also depends on some additional property, the worst- case, average-case, and, if necessary, best-case efficiencies have to be investigated separately.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
66
67
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
67
68
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
68
69
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
69
Best, Worst, Average case exist?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
70
Best, Worst, Average case exist?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
71
Best, Worst, Average case exist?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
72
Analysis
Best, Worst, Average case exist?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
73
The basic operation is count=count + 1 repeats times
number of
Best, Worst, Average case exist?
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
74
Analysis of Recursive Algorithms
if it can, the worst-case, average-case, and best-case efficiencies must be investigated separately.
5.Solve the recurrence or, at least, ascertain the order of growth of its solution.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
75
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
76
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
77
Tower of Hanoi puzzle.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
78
Tower of Hanoi puzzle.
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
79
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
80
Algorithm
TowerOfHanoi(n, source, dest, aux)
If n == 1, then
move disk from source to dest else
TowerOfHanoi (n - 1, source, aux, dest) move disk from source to dest TowerOfHanoi (n - 1, aux, dest, source)
End if
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
81
Recurrence relation for total number of moves
The number of moves M(n) depends only on n. The recurrence equation is
We have the following recurrence relation for the number of moves M(n):
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
82
24 M(n − 4) + 23 + 22 + 2 + 1, and
generally, after i substitutions, we get
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
83
we get the following formula for the solution to recurrence
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
84
Example 3
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
85
Recurrence relation for basic operation
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
86
Brute Force Design Technique
A brute force approach is an approach that finds all the possible solutions to find a satisfactory solution to a given problem. The brute force algorithm tries out all the possibilities till a satisfactory solution is not found.
Topics : 1.Selection Sort
2.Sequential Search
3.String Matching Algorithm With complexity Analysis
Text Book1-3.1,3.2
87
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
87
Selection Sort
How Selection Sort works
88
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
88
3. Else if the second element is greater than the minimum, which is our � first element, then we will do nothing and move on to the third element � and then compare it with the minimum.� We will repeat this process until we reach the last element.
89
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
89
A [] = (7, 4, 3, 6, 5).
1st Iteration:
90
4/16/2024
As, a2 < a3, set minimum= 3.
As, a2 < a4, set minimum =3.
Since 3 is the smallest element, so we will swap a0 and a2.
As, a1 > a2, set minimum = 3.
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
90
2nd Iteration:
Set minimum = 4
91
4/16/2024
As, a1 < a2, set minimum = 4.
As, A[1] < A[3], set minimum = 4.
Again, a1 < a4, set minimum = 4.
Since the minimum is already placed in the correct position, so there will be no swapping.
3rd Iteration:
Set minimum = 7
As, a2 > a3, set minimum = 6.
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
91
As, a3 > a4, set minimum = 5.
92
4/16/2024
Since 5 is the smallest element among the leftover unsorted elements, so we will swap 7 and 5.
4th Iteration:
Set minimum = 6
As a3 < a4, set minimum = 6.
Since the minimum is already placed in the correct position, so there will be no swapping.
Sorted Array is
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
92
Algorithm :
SelectionSort(A[0…n-1])
//sorts a given array by selection sort
//Input:An unsorted array A[0…n-1]
//Output:Sorted array A[0…n-1] in ascending order
for i 🡨 0 to n-2 do (One loop to select an element of Array one by one)
min🡨 i
for j 🡨 i+1 to n-1 do (Another loop to compare that element with every other Array element)
if A[j] < A[min]
min 🡨 j
Swap A[i] and A[min]
93
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
93
Complexity Analysis of Selection Sort
Step 1:Input size is given by the no.of elements in an array say n
Step2 :Basic operation is the key comparison A[j]<A[min]
Step3 :the no of times B.O is executed depends on input size n
C(n)=Ʃ Ʃ 1= Ʃ[(n-1)-(i+1)+1]= Ʃ (n-1-i)
= (by replacing i=0 ,1..n-2)
=
(n2 ) however no of key comparisons are only (n)
94
4/16/2024
n-2
i=0
n-1
j=i+1
n-2
i=0
n-2
i=0
ɵ
ɵ
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
94
Sequential Search
The steps used in the implementation of Linear Search are listed as follows -
Step 1: First, read the search element (Target element) in the array.
95
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
95
Algorithm
SequentialSearch(A[0..n-1],K)
//Input:An array A[0..n-1] and a search key K
//Output:The index of the first element of A that matches K or -1 if there are no matching elements
i<- 0
While i<n and A[i]≠K do
i<-i+1
if i<n return i
else return -1
96
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
96
Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 20
Step 1: Set i = 0 and check key with arr[0].
97
4/16/2024
Step 2: key and arr[0] are not the same. So make i = 1 and match key with arr[1].
Compare key with index 1
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
97
Step 3: arr[1] and key are different. Increment i and compare key with arr[2].
98
4/16/2024
Compare key with index 2
Step 4: arr[2] is not the same with key. Increment i and compare key with arr[3].
Compare key with index 3
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
98
Step 5: key and arr[3] are different. Make i = 4 and compare key with arr[4].
99
4/16/2024
Compare key with index 4
Step 6: key and arr[4] are not same. Make i = 5 and match key with arr[5].
Compare key with index 5
We can see here that key is present at index 5.
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
99
Time Complexity
Step 1:Input size is given by the no.of elements in an array say n
Step2 :Basic operation is the key comparison A[j]<A[min]
Step3 :the no of times B.O is executed depends not only on input size n it also depends on the position of key element present in the given array.so all the 3 cases we should find
100
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
100
1+2+3+4+5……..n/n
=(n(n+1)/2)/n
=(n+1)/2
O(n)
When search is successful ,the average no.of key comparisons is (n+1)/2.If the search is unsuccessful the average no.of key comparisons is n because the algorithm will inspect all n elements.
Worst Case Complexity - In Linear search, the worst case occurs when the element we are looking is present at the end of the array. The worst-case in linear search could be when the target element is not present in the given array, and we have to traverse the entire array. The worst-case time complexity of linear search is O(n).
101
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
101
102
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
102
103
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
103
104
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
104
105
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
105
106
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
106
107
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
107
108
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
108
Important Problem Types
Two properties
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
109
Important Problem Types
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
110
Fundamental Data structures
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
111
Fundamental Data structures
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
112
Fundamental Data structures
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
113
Fundamental Data structures
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
114
Fundamental Data structures
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
115
116
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
116
Fundamental Data structures
117
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
117
Fundamental Data structures
118
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
118
Fundamental Data structures
119
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
119
Extra Byte-1.1: Min Distance
120
4/16/2024
Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. Make as many improvements as you can in this algorithmic solution to the problem. If you need to, you may change the algorithm altogether; if not, improve the implementation given.
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
120
Extra Byte-1.2: Secret
121
4/16/2024
Consider the following algorithm.
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
121
Extra Byte-1.3: Enigma
122
4/16/2024
Consider the following algorithm.
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
122
Assignment-1 Due: Within 5 days
123
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
123
Class test-1 Max Marks: 20 Duration:45 Mins
124
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
124
End of Module-1
125
4/16/2024
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
/skit.org.in
/skit.org.in
/skit.org.in
Sri Krishna Institute of Technology
(Approved by AICTE, Accredited by NAAC, Affiliated to VTU, Karnataka)
4/16/2024
125