DAA Unit 1 Slides
DAA Unit 1 Slides
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Introduction to Algorithms,
Design Techniques and Analysis
Slides courtesy of Anany Levitin
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Syllabus
UNIT I (12 Hours)
• Introduction
• Analysis of Algorithm Efficiency,
• Algebric structures
UNIT II (12 Hours)
• Brute Force,
• Divide-and-Conquer
UNIT III (10 Hours)
• Decrease-and-Conquer
• Transform-and-Conquer
UNIT IV (10 Hours)
• Space and Time Tradeoffs
• Greedy Technique
UNIT V (12 Hours)
• Limitations of Algorithm Power
• Coping with the Limitations of Algorithm Power
• Dynamic Programming
Design and Analysis of Algorithms
Text Books
Publication Information
Book Type Code Title & Author
Edition Publisher Year
Algorithm Design
Reference Book R3 1 Pearson Education 2006
Jon Kleinberg, Eva Tardos,
Design and Analysis of Algorithms
Algorithm
What is an algorithm?
An algorithm is a sequence of unambiguous instructions for solving a problem,
i.e., for obtaining a required output for any legitimate input in a finite amount of time
Important Points about Algorithms
The non-ambiguity requirement for each step of an algorithm cannot be compromised
The range of inputs for which an algorithm works has to be specified carefully.
There may exist several algorithms for solving the same problem.
Design and Analysis of Algorithms
Characteristics of Algorithm
Efficiency
Theoretical analysis
Empirical analysis
Design and Analysis of Algorithms
Basic Issues related to Algorithms
Efficiency
Theoretical analysis
Empirical analysis
Design and Analysis of Algorithms
Algorithm Design Technique
What do you mean by Algorithm Design Techniques?
General Approach to solving problems algorithmically .
Applicable to a variety of problems from different areas of computing
Various Algorithm Design Techniques
Brute Force
Divide and Conquer
Dynamic Programming
Greedy Technique
Backtracking
Efficiency
Theoretical analysis
Empirical analysis
Design and Analysis of Algorithms
Methods of Specifying an Algorithm
Natural language
Ambiguous
Pseudocode
A mixture of a natural language and programming language-like structures
Flowchart
Method of expressing algorithm by collection of
Euclid’s Algorithm
Problem: Find gcd(m,n), the greatest common divisor of two nonnegative,
not both zero integers m and n
Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?
ALGORITHM Euclid(m,n)
//computes gcd(m,n) by Euclid’s method
//Input: Two nonnegative,not both zero integers
//Output:Greatest common divisor of m and n
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Design and Analysis of Algorithms
Algorithm for Sequential Search
Design and Analysis of Algorithms
Basic Issues related to Algorithms
Efficiency
Theoretical analysis
Empirical analysis
Design and Analysis of Algorithms
Basic Issues related to Algorithms
Efficiency
Theoretical analysis
Empirical analysis
Design and Analysis of Algorithms
Need of Analysis
Memory space
Compare different methods for solving the same problem before actually
implementing them and running the programs.
Time complexity
How fast an algorithm maps input to output as a function of input
Space complexity
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Algorithm Design and Analysis Process
Design an algorithm
Prove correctness
Linear
Linear list, Stack, Queues
Non Linear
Trees, Graphs
Dijkstra Algorithm
O(VlogV+E) with Fibonacci heap
Design and Analysis of Algorithms
Algorithm Design Technique
ADT serves as heuristic for designing algorithms for new problems for which
no satisfactory algorithm exists!!!
Natural Language
Pseudo Code
Flowchart
Design and Analysis of Algorithms
Specifying an algorithm
Natural Language
Pseudo Code
Flowchart
Design and Analysis of Algorithms
Proving Correctness
Exact algorithms
Proving that algorithm yields a correct result for legitimate input in finite
amount of time
Approximation algorithms
Error produced by algorithm does not exceed a predefined limit
Design and Analysis of Algorithms
Analyzing an algorithm
Efficiency
Time efficiency
Space efficiency
Simplicity
Generality
Design an algorithm for the problem posed in more general terms
Design an algorithm that can handle a range of inputs that is natural for
the problem at hand
Design and Analysis of Algorithms
Coding an algorithm
Efficient implementation
Correctness of program
Mathematical Approach: Formal verification for small programs
Practical Methods: Testing and Debugging
Code optimization
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Important Problem Types
sorting
searching
string processing
graph problems
combinatorial problems
geometric problems
numerical problems
Design and Analysis of Algorithms
Important Problem Types: Sorting
Help searching
Algorithms often use sorting as a key subroutine.
Sorting key
Selection sort
Bubble sort
Insertion sort
Merge sort
Heap sort …
Evaluate sorting algorithm complexity: the number of key comparisons.
Two properties
Sequential searching
Binary searching…
Design and Analysis of Algorithms
Important Problem Types: String Processing
Pattern: computer
Design and Analysis of Algorithms
Important Problem Types: Graph Problems
Definition
Graph G is represented as a pair G= (V, E),
where V is a finite set of vertices and E is a finite set of edges
Modeling real-life problems
Modeling WWW
communication networks
Project scheduling …
Shortest-path algorithms
Topological sorting
Design and Analysis of Algorithms
Important Problem Types: Combinatorial Problems
Solving Equations
Evaluating functions
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Analysis Framework
Slides courtesy of Anany Levitin
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Analysis Framework
Space
CPU time
Memory space
Compare different methods for solving the same problem before actually
implementing them and running the programs.
To find an efficient algorithm
Design and Analysis of Algorithms
Complexity of an Algorithm
internal factors
S(P)=C+SP(I)
Fixed Space Requirements (C)
Independent of the characteristics of the inputs and outputs
instruction space
S(P)=C+SP(I)
float rsum(float list[ ], int n)
{ Ssum(I)=Ssum(n)=6n
if (n)
return rsum(list, n-1) + list[n-1]
return 0
}
Type Name Number of bytes
parameter: float list [ ] 2
parameter: integer n 2
return address:(used 2
internally)
TOTAL per recursive call 6
Design and Analysis of Algorithms
Time Complexity
T(P)=C+TP(I)
Theoretical Analysis
Experimental study
Design and Analysis of Algorithms
Time Complexity
Experimental study
Results may not be indicative of the running time on other inputs not
included in the experiment.
Two approaches:
Measure the running time using standard unit of time measurements, such
as seconds, minutes?
Depends on the speed of the computer.
input size
Exponential-growth functions
Orders of growth:
• consider only the leading term of a formula
• ignore the constant coefficient.
22
Design and Analysis of Algorithms
Order of Growth
700
600
500
n*n*n
400 n*n
n log(n)
300 n
log(n)
200
100
0
1 2 3 4 5 6 7 8
Design and Analysis of Algorithms
Basic Efficiency Classes
1 constant
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
Design and Analysis of Algorithms
Best, Worst and Average case Analysis
ALGORITHM SequentialSearch(A[0..n-1], K)
//Searches for a given value in a given array by sequential
search
//Input: An array A[0..n-1] and a search key K
//Output: Returns 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
ii+1
if i < n //A[I] = K
return i
else
27
return -1
Design and Analysis of Algorithms
Best, Worst and Average case Analysis: Sequential Search
Worst-Case: Cworst(n) = n
Best-Case: Cbest(n) = 1
Average-Case
from (n+1)/2 to (n+1)
28
Design and Analysis of Algorithms
Average case Analysis: Sequential Search
29
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Asymptotic Notations
Slides courtesy of Anany Levitin
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Asymptotic Notations
Formal definition
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
Formal definition
A function t(n) is said to be in (g(n)), denoted t(n) (g(n)), if
t(n) is bounded below 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
Formal definition
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
>=
(g(n)), functions that grow at least as fast as g(n)
=
(g(n)), functions that grow at the same rate as g(n)
g(n)
<=
O(g(n)), functions that grow no faster than g(n)
Design and Analysis of Algorithms
Little-o Notation
Formal Definition:
A function t(n) is said to be in Little-o(g(n)), denoted t(n) o(g(n)),
if for any positive constant c and some nonnegative integer n0
Example: n o(n2)
Design and Analysis of Algorithms
Little Omega Notation
Formal Definition:
A function t(n) is said to be in Little- (g(n)), denoted t(n) (g(n)),
if for any positive constant c and some nonnegative integer n0
t(n) >cg(n) 0 for all n n0
Implication: The algorithm’s overall efficiency will be determined by the part with a larger
order of growth, I.e., its least efficient part.
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Using Limits to Compare Order of Growth
L’Hopital’s Rule
Stirling’s Formula
Design and Analysis of Algorithms
Using Limits to Compare Order of Growth: Example 1
t(n) 5n3 6n 2 5 6 2 0
lim lim lim
n g(n) n n 4 n n n3 n 4
As per case1
t(n) = O(g(n))
5n3 + 6n + 2 = O(n4)
Design and Analysis of Algorithms
Using Limits to Compare Order of Growth: Example 2
t (n) 5n2 4n 2
using the Limits approach determine g(n) such that f(n) = Θ(g(n))
Leading term in square root n2
g(n) n2 n
t(n) 5n2 4n 2
lim lim
n g(n) n
n2
5n2 4n 2 4 2
lim 2
lim 5 2 5
n n n n n
non-zero constant
Hence, t(n) = Θ(g(n)) = Θ(n)
Design and Analysis of Algorithms
Using Limits to Compare Order of Growth
Design and Analysis of Algorithms
Using Limits to Compare Order of Growth: Example 3
Compare the order of growth of t(n) and g(n) using method of limits
t(n) log2 n, g(n)
Design and Analysis of Algorithms
Orders of growth of some important functions
order log n < order n (>0) < order an < order n! < order nn
Design and Analysis of Algorithms
How to Establish Orders of Growth of an Algorithm’s Basic Operation Count
Summary
• Method 1: Using limits.
• L’ Hôpital’s rule
• Method 2: Using the theorem.
• Method 3: Using the definitions of O-, -, and -notation.
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Time Efficiency of Non-recursive Algorithms
liu1 = 1+1+…+1 = u - l + 1
1 (u l 1)
il
Design and Analysis of Algorithms
Example 1: Finding Max Element in a list
Algorithm MaxElement (A[0..n-1])
//Determines the value of the largest element
in a given array
//Input: An array A[0..n-1] of real numbers
//Output: The value of the largest element in A
maxval A[0]
for i 1 to n-1 do
if A[i] > maxval
maxval A[i]
return maxval
• The basic operation- comparison
• Number of comparisons is the same for all arrays of size n.
• Number of comparisons
Design and Analysis of Algorithms
Example 2: Element Uniqueness Problem
Best-case: 1 comparison
Worst-case: n2/2 comparisons
T(n)worst case = O(n2)
Design and Analysis of Algorithms
Example 3:Matrix Multiplication
M(n) € Θ (n3)
8
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Steps in Mathematical Analysis of Recursive Algorithms
Decrease-by-one recurrences
A decrease-by-one algorithm solves a problem by exploiting a relationship between a
given instance of size n and a smaller size n – 1.
Example: n!
The recurrence equation has the form
T(n) = T(n-1) + f(n)
Decrease-by-a-constant-factor recurrences
A decrease-by-a-constant algorithm solves a problem by dividing its given instance of
size n into several smaller instances of size n/b, solving each of them recursively, and
then, if necessary, combining the solutions to the smaller instances into a solution to
the given instance.
Example: binary search.
The recurrence has the form
T(n) = aT(n/b) + f (n)
Design and Analysis of Algorithms
Decrease-by-one Recurrences
Substitution Method
Mathematical Induction
Backward substitution
Recursion Tree Method
Master Method (Decrease by constant factor recurrences)
Design and Analysis of Algorithms
Recursive Evaluation of n!
Best/Worst/Average Case?
input size?
basic operation?
Best/Worst/Average Case?
Design and Analysis of Algorithms
Tower of Hanoi
Algorithm TowerOfHanoi(n, Src, Aux, Dst)
if (n = 0)
return
TowerOfHanoi(n-1, Src, Dst, Aux)
Move disk n from Src to Dst
TowerOfHanoi(n-1, Aux, Src, Dst)
Input Size: n
Basic Operation : Move disk n from Src to Dst
C(n) = 2C(n-1) + 1 for n > 0 and C(0)=0
= 2n - 1 ∈ Θ(2n)
Design and Analysis of Algorithms
Tower of Hanoi : Tree of Recursive calls
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Solving Recurrences
Slides courtesy of Anany Levitin
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Solving Recurrences: Example1
T (n) T (n / 2) 1 n 1
T (1) 1
T (n) T (n / 2) 1
T (n / 2 2 ) 1 1
T (n / 23 ) 1 1 1
......
T (n / 2i ) i
......
T (n / 2 k ) k (k log n)
1 log n
O( log n)
Design and Analysis of Algorithms
Solving Recurrences: Example4
T (n) 2T (n / 2) cn n 1 T (1) c
T (n) 2T (n / 2) cn
2(2T (n / 2 2 ) c(n / 2)) cn 2 2 T (n / 2 2 ) cn cn
2 2 (2T (n / 23 ) c(n / 2 2 )) cn cn 23 T (n / 23 ) 3cn
......
2i T (n / 2i ) icn
......
2 k T (n / 2 k ) kcn (k log n)
nT (1) cn log n cn cn log n
O(n log n)
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
Design and Analysis of Algorithms
Vandana M L
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Vandana M L
Department of Computer Science & Engineering
Design and Analysis of Algorithms
Performance Evaluation of Algorithm
Performance Analysis
Machine Independent
Prior Evaluation
Performance Measurement
Machine Dependent
Posterior Evaluation
Design and Analysis of Algorithms
Performance Analysis of Sequential search :Worst Case
ALGORITHM SequentialSearch(A[0..n-1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n-1] and a search key K
//Output: Returns 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 Basic operation: A[i] ‡ K
ii+1 Basic operation count: n
if i < n //A[i] = K Time Complexity: T(n) ∈O(n)
return i
else
return -1
Design and Analysis of Algorithms
Performance Analysis of Sequential Search
4000 4000
4500 4500
Design and Analysis of Algorithms
Performance Measurement of Sequential Search
0.008
2000 0.00596 0.006
0.002
3000 0.007868 0
3500 0.008821 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
4000 0.010014
4500 0.011921
THANK YOU
Vandana M L
Department of Computer Science & Engineering
vandanamd@pes.edu
DESIGN AND ANALYSIS
OF ALGORITHMS
UE19CS251
Shylaja S S
Department of Computer Science
& Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Shylaja S S
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Brute Force
DESIGN AND ANALYSIS OF ALGORITHMS
Brute Force
https://en.wikipedia.org/wiki/Brute-force_search
DESIGN AND ANALYSIS OF ALGORITHMS
Brute Force
• A brute-force algorithm to find the divisors of a natural
number n would
• enumerate all integers from 1 to n
• check whether each of them divides n without
remainder
• A brute-force approach for the eight queens puzzle would
• examine all possible arrangements of 8 pieces on the
64-square chessboard
• check whether each (queen) piece can attack any other,
for each arrangement
• The brute-force method for finding an item in a table (linear
search)
• checks all entries of the table, sequentially, with the
item
DESIGN AND ANALYSIS OF ALGORITHMS
Brute Force
• A brute-force search is simple to implement, and will always
find a solution if it exists
• But, its cost is proportional to the number of candidate
solutions – which in many practical problems tends to grow
very quickly as the size of the problem increases
(Combinatorial explosion)
• Brute-force search is typically used
• when the problem size is limited
• when there are problem-specific heuristics that can be
used to reduce the set of candidate solutions to a
manageable size
• when the simplicity of implementation is more
important than speed
DESIGN AND ANALYSIS OF ALGORITHMS
Brute Force Sorting Algorithms
• Selection Sort
• Bubble Sort
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
• Scan the array to find its smallest element and swap it with
the first element, putting the smallest element in its final
position in the sorted list
• Then, starting with the second element, scan the elements to
the right of it to find the smallest among them and swap it
with the second element, putting the second smallest element
in its final position
• Generally, on pass i (0 i n-2), find the smallest element in
A[i..n-1] and swap it with A[i ]
elements in
final position
a 89 45 68 90 29 34 17
Pass 0 i=0 0 1 2 3 4 5 6
min j
45 < 89 ??
min = 01 j=1
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 89 45 68 90 29 34 17
i=0 0 1 2 3 4 5 6
min j
68 < 45 ??
min = 1 j=2
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 89 45 68 90 29 34 17
i=0 0 1 2 3 4 5 6
min j
90 < 45 ??
min = 1 j=3
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 89 45 68 90 29 34 17
i=0 0 1 2 3 4 5 6
min j
29 < 45 ??
min = 14 j=4
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 89 45 68 90 29 34 17
i=0 0 1 2 3 4 5 6
min j
34 < 29 ??
min = 4 j=5
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 89 45 68 90 29 34 17
i=0 0 1 2 3 4 5 6
min j
17 < 29 ??
min = 46 j=6
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 89 45 68 90 29 34 17
i=0 0 1 2 3 4 5 6
min
swap a[i] and a[min]
min = 6
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 45 68 90 29 34 89
i=0 0 1 2 3 4 5 6
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 45 68 90 29 34 89
Pass 1 i=1 0 1 2 3 4 5 6
min min
j
min = 4
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 68 90 45 34 89
i=1 0 1 2 3 4 5 6
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 68 90 45 34 89
Pass 2 i=2 0 1 2 3 4 5 6
min min
j
min = 5
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 34 90 45 68 89
i=2 0 1 2 3 4 5 6
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 34 90 45 68 89
Pass 3 i=3 0 1 2 3 4 5 6
min min
j
min = 4
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 34 45 90 68 89
i=3 0 1 2 3 4 5 6
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 34 45 90 68 89
Pass 4 i=4 0 1 2 3 4 5 6
min min
j
min = 5
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 34 45 68 90 89
i=4 0 1 2 3 4 5 6
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 34 45 68 90 89
Pass 5 i=5 0 1 2 3 4 5 6
min min
j
min = 6
DESIGN AND ANALYSIS OF ALGORITHMS
Selection Sort
elements in
final position
a 17 29 34 45 68 89 90
i=5 0 1 2 3 4 5 6
Original array 89 45 68 90 29 34 17
0 1 2 3 4 5 6
Shylaja S S
Department of Computer Science
& Engineering
shylaja.sharath@pes.edu
DESIGN AND ANALYSIS
OF ALGORITHMS
UE19CS251
Shylaja S S
Department of Computer Science
& Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
Major Slides Content: Anany Levitin
Shylaja S S
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
j=0 89 45
45 89 68 90 29 34 17
0 1 2 3 4 5 6
45 < 89 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 89 68 90 29 34 17
i=0 0 1 2 3 4 5 6
j=1 45 68
89 68
89 90 29 34 17
0 1 2 3 4 5 6
68 < 89 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 89 90 29 34 17
i=0 0 1 2 3 4 5 6
j=2 45 68 89 90 29 34 17
0 1 2 3 4 5 6
90 < 89 no swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 89 90 29 34 17
i=0 0 1 2 3 4 5 6
j=3 45 68 89 90 29
29 90 34 17
0 1 2 3 4 5 6
29 < 90 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 89 29 90 34 17
i=0 0 1 2 3 4 5 6
j=4 45 68 89 29 34
90 90
34 17
0 1 2 3 4 5 6
34 < 90 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 89 29 34 90 17
i=0 0 1 2 3 4 5 6
j=5 45 68 89 29 34 17
90 90
17
0 1 2 3 4 5 6
17 < 90 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
After elements in
first final position
iteration 45 68 89 29 34 17 90
i=1 0 1 2 3 4 5 6
j=0 45 68 89 29 34 17 90
0 1 2 3 4 5 6
68 < 45 no swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 89 29 34 17 90
i=1 0 1 2 3 4 5 6
j=1 45 68 89 29 34 17 90
0 1 2 3 4 5 6
89 < 68 no swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 89 29 34 17 90
i=1 0 1 2 3 4 5 6
j=2 29 89
45 68 89 29 34 17 90
0 1 2 3 4 5 6
29 < 89 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 29 89 34 17 90
i=1 0 1 2 3 4 5 6
j=3 45 68 29 34 34
89 89 17 90
0 1 2 3 4 5 6
34 < 89 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 29 34 89 17 90
i=1 0 1 2 3 4 5 6
j=4 45 68 29 34 17
89 89
17 90
0 1 2 3 4 5 6
17 < 89 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
After elements in
second final position
iteration 45 68 29 34 17 89 90
i=2 0 1 2 3 4 5 6
j=0 45 68 29 34 17 89 90
0 1 2 3 4 5 6
68 < 45 no swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 68 29 34 17 89 90
i=2 0 1 2 3 4 5 6
j=1 29 29
45 68 68 34 17 89 90
0 1 2 3 4 5 6
29 < 68 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 29 68 34 17 89 90
i=2 0 1 2 3 4 5 6
j=2 34 68
45 29 68 34 17 89 90
0 1 2 3 4 5 6
34 < 68 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
45 29 34 68 17 89 90
i=2 0 1 2 3 4 5 6
j=3 45 29 34 17 68 89 90
68 17
0 1 2 3 4 5 6
17 < 68 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
After elements in
third final position
iteration 45 29 34 17 68 89 90
i=3 0 1 2 3 4 5 6
j=0 29
45 45
29 34 17 68 89 90
0 1 2 3 4 5 6
29 < 45 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
29 45 34 17 68 89 90
i=3 0 1 2 3 4 5 6
j=1 29 34
45 45
34 17 68 89 90
0 1 2 3 4 5 6
34 < 45 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
29 34 45 17 68 89 90
i=3 0 1 2 3 4 5 6
j=2 17 45
29 34 45 17 68 89 90
0 1 2 3 4 5 6
17 < 45 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
After elements in
fourth final position
iteration 29 34 17 45 68 89 90
i=4 0 1 2 3 4 5 6
j=0 29 34 17 45 68 89 90
0 1 2 3 4 5 6
34 < 29 no swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
elements in
final position
29 34 17 45 68 89 90
i=4 0 1 2 3 4 5 6
j=1 29 17 17
34 34 45 68 89 90
0 1 2 3 4 5 6
17 < 34 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
After elements in
fifth final position
iteration 29 17 34 45 68 89 90
i=5 0 1 2 3 4 5 6
j=0 17 17
29 29 34 45 68 89 90
0 1 2 3 4 5 6
17 < 29 swap
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
After elements in
sixth final position
iteration 17 29 34 45 68 89 90
0 1 2 3 4 5 6
DESIGN AND ANALYSIS OF ALGORITHMS
Bubble Sort
Shylaja S S
Department of Computer Science
& Engineering
shylaja.sharath@pes.edu
DESIGN AND ANALYSIS
OF ALGORITHMS
UE19CS251
Shylaja S S
Department of Computer Science
& Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Sequential Search
Major Slides Content: Anany Levitin
Shylaja S S
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Sequential Search
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
AlGORITHM SequentialSearch2(A[0 .. n ], K)
//Implements sequential search with a search key as a sentinel
//Input: An array A of n elements and a search key K
//Output: The index of the first element in A[0 .. n -1] whose value is
// equal to K or -1 if no such element is found
A[n]<---K
i<---0
while A[i] ≠K do
i<--- i + 1
if i < n return i
else return -1
DESIGN AND ANALYSIS OF ALGORITHMS
Sequential Search
Shylaja S S
Department of Computer Science
& Engineering
shylaja.sharath@pes.edu
DESIGN AND ANALYSIS
OF ALGORITHMS
UE19CS251
Shylaja S S
Department of Computer Science
& Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
String Matching
Major Slides Content: Anany Levitin
Shylaja S S
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Brute – Force String Matching
Step 3: While pattern is not found and the text is not yet exhausted,
realign pattern one position to the right and repeat Step 2
DESIGN AND ANALYSIS OF ALGORITHMS
String Matching
Worst Case:
• The algorithm might have to make all the ‘m’ comparisons for
each of the (n-m+1) tries
• Therefore, the algorithm makes m(n-m+1) comparisons
• Brute Force String Matching is a O(nm) algorithm
THANK YOU
Shylaja S S
Department of Computer Science
& Engineering
shylaja.sharath@pes.edu
DESIGN AND ANALYSIS
OF ALGORITHMS
UE19CS251
Shylaja S S
Department of Computer Science
& Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Shylaja S S
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Exhaustive Search
Example
Tour Length
2
a 5 3
b a→b→c→d→a
a→b→d→c→a
2+3+7+5 = 17
2+4+7+8 = 21
8 4 a→c→b→d→a 8+3+4+5 = 20
a→c→d→b→a 8+7+4+2 = 21
a→d→b→c→a 5+4+3+8 = 20
c 7 d a→d→c→b→a 5+7+3+2 = 17
DESIGN AND ANALYSIS OF ALGORITHMS
Travelling Salesman Problem
Efficiency
• The Exhaustive Search solution to the Travelling Salesman
problem can be obtained by keeping the origin city constant and
generating permutations of all the other n – 1 cities
• Thus, the total number of permutations needed will be (n – 1)!
THANK YOU
Shylaja S S
Department of Computer Science
& Engineering
shylaja.sharath@pes.edu
DESIGN AND ANALYSIS
OF ALGORITHMS
UE19CS251
Shylaja S S
Department of Computer Science
& Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Knapsack Problem
Major Slides Content: Anany Levitin
Shylaja S S
Department of Computer Science & Engineering
DESIGN AND ANALYSIS OF ALGORITHMS
Knapsack Problem
Given n items:
• weights: w1 w2 … wn
• values: v1 v2 … vn
• a knapsack of capacity W
Find the most valuable subset of items that fit into the knapsack
DESIGN AND ANALYSIS OF ALGORITHMS
Knapsack Problem
Example
Knapsack Capacity W = 16
1 2 20
2 5 30
3 10 50
4 5 10
DESIGN AND ANALYSIS OF ALGORITHMS
Knapsack Problem
Subset Total Weight Total Value
{1} 2 20
{2} 5 30
{3} 10 50
Knapsack Problem by
{4} 5 10
Exhaustive Search
{1, 2} 7 50
{1, 3} 12 70
{1, 4} 7 30
{2, 3} 15 80
{2, 4} 10 40
{3, 4} 15 60
{1, 2, 3} 17 Not Feasible
{1, 2, 4} 12 60
{1, 3, 4} 17 Not Feasible
{2, 3, 4} 20 Not Feasible
{1, 2, 3, 4} 22 Not Feasible
DESIGN AND ANALYSIS OF ALGORITHMS
Knapsack Problem
Shylaja S S
Department of Computer Science
& Engineering
shylaja.sharath@pes.edu