BCS401 Ada PPT 24 25
BCS401 Ada PPT 24 25
4th Sem
BCS401
AY : 2024-25
Department
DepartmentoofCSE ATME College of Engineering
CSE
Course Outcomes(COs):
Course Outcomes:
2
Agenda
What is an Algorithm?
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
Recursive Algorithms with Examples .
Important Problem Types: Sorting, Searching, String processing, Graph Problems,
Combinatorial Problems.
Fundamental Data Structures: Stacks, Queues, Graphs, Trees, Sets and Dictionaries.
3
MODULE – 1
INTRODUCTION
What is an algorithm?
Algorithmic: The sprit of computing – David Harel.
Department of 5
What is an algorithm?
Recipe, process, method, technique, procedure,
routine,… with the following requirements:
1. Finiteness
terminates after a finite number of steps
2. Definiteness
rigorously and unambiguously specified
3. Clearly specified input
valid inputs are clearly specified
4. Clearly specified/expected output
can be proved to produce the correct output given a valid input
5. Effectiveness
steps are sufficiently simple and basic
Department
Departmentof
of ATME College of Engineering 6
CCSSEE
Algorithm
Department
Departmentof
of ATME College of Engineering 7
CCSSEE
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.
Problem
Algorithm
while n ≠ 0 do
r ← m mod n
m← n
n←r
return m
Department
Departmentof
of ATME College of Engineering 11
CCSSEE
Other methods for computing
Consecutive integer checking algorithm
Step 1 Assign the valuegcd(m,n)
of min{m,n} to t
Step 2 Divide m by t. If the remainder is 0, go to Step 3;
otherwise, go to Step 4
Step 3 Divide n by t. If the remainder is 0, return t and stop;
otherwise, go to Step 4
Step 4 Decrease t by 1 and go to Step 2
Is this an algorithm?
Department
Departmentof
of ATME College of Engineering 13
CCSSEE
Sieve of Eratosthenes
Input: Integer n ≥ 2
Output: List of primes less than or equal to n
for p ← 2 to n do A[p] ← p
for p ← 2 to sqrt(n) do
if A[p] 0 //p hasn’t been eliminated on previous passes
j ← p* p
while j ≤ n do
A[j] ← 0 //mark element as eliminated
j←j+p
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Output: 2 3 5 7 11 13 17 19
Department
Departmentof
of ATME College of Engineering 14
CCSSEE
Fundamental steps in solving problems
Department of Department
CCSSEE of ATME College of Engineering 15
Fundamental steps in solving problems
• Searching
• String processing
• Graph problems
• Combinatorial problems
• Geometric problems
• Numerical problems
Department
Departmentof
of ATME College of Engineering 17
CCSSEE
Graph Problems
• Informal definition
– A graph is a collection of points called vertices, some of
which are connected by line segments called edges.
• Modeling real-life problems
– Modeling WWW
– Communication networks
– Project scheduling …
• Examples of graph algorithms
– Graph traversal algorithms
– Shortest-path algorithms
– Topological sorting
Department
Departmentof
of ATME College of Engineering 18
CCSSEE
Linear Data Structures
• Arrays Arrays
– A sequence of n items of the same fixed length (need preliminary
data type that are stored contiguously reservation of memory)
in computer memory and made contiguous memory locations
accessible by specifying a value of the
direct access
array’s index.
• Linked List Insert/delete
– A sequence of zero or more nodes Linked Lists
each containing two kinds of dynamic length
information: some data and one or arbitrary memory locations
more links called pointers to other
nodes of the linked list. access by following links
– Singly linked list (next pointer) Insert/delete
– Doubly linked list (next + previous
pointers)
a1 a2 … an .
19
Stacks and Queues
• Stacks
– A stack of plates
• insertion/deletion can be done only at the top.
• LIFO
– Two operations (push and pop)
• Queues
– A queue of customers waiting for services
• Insertion/enqueue from the rear and deletion/dequeue from the
front.
• FIFO
– Two operations (enqueue and dequeue)
Department
Departmentof
of ATME College of Engineering 20
CCSSEE
Priority Queue and Heap
9 6 8 5 2 3
21
Graphs
• Formal definition
– A graph G = <V, E> is defined by a pair of two sets: a finite set V of
items called vertices and a set E of vertex pairs called edges.
• Undirected and directed graphs (digraphs).
• What’s the maximum number of edges in an
undirected graph with |V| vertices?
• Complete, dense, and sparse graphs
– A graph with every pair of its vertices connected by an edge is called
complete, K|V|
1 2
3 4
DDeeppaarrt tmmeennttooff AATTMMEECCooleleggeeooffEEnngginineeerirningg 22
CCSSEE
Graph Representation
• Adjacency matrix
– n x n boolean matrix if |V| is n.
– The element on the ith row and jth column is 1 if there’s an edge from ith
vertex to the jth vertex; otherwise 0.
– The adjacency matrix of an undirected graph is symmetric.
• Adjacency linked lists
– A collection of linked lists, one for each vertex, that contain all the vertices
adjacent to the list’s vertex.
• Which data structure would you use if the graph is a 100-node star shape?
0111 2 3 4
0001
4
0001
4
0000
• Weighted graphs
– Graphs or digraphs with numbers assigned to the edges.
5
1 2
6 7
9
3 4
8
Department
Departmentof
of ATME College of Engineering 24
CCSSEE
Graph Properties -- Paths and Connectivity
• Paths
– A path from vertex u to v of a graph G is defined as a sequence of
adjacent (connected by an edge) vertices that starts with u and ends
with v.
– Simple paths: All edges of a path are distinct.
– Path lengths: the number of edges, or the number of vertices – 1.
• Connected graphs
– A graph is said to be connected if for every pair of its vertices u and v
there is a path from u to v.
• Connected component
– The maximum connected subgraph of a given graph.
• Cycle
– A simple path of a positive length that starts and ends
a the same vertex.
• Acyclic graph
– A graph without cycles
– DAG (Directed Acyclic Graph)
1 2
3 4
Department
Departmentof
of ATME College of Engineering 26
CCSSEE
Trees
• Trees
– A tree (or free tree) is a connected acyclic graph.
– Forest: a graph that has no cycles but is not necessarily connected.
• Properties of trees
– For every two vertices in a tree there always exists exactly one simple
path from one of these vertices to the other. Why?
• Rooted trees: The above property makes it possible to select an arbitrary vertex in a
free tree and consider it as the root of the so called rooted tree.
• Levels in a rooted tree.
rooted
3
|E| = |V| - 1 1 3 5
4 1 5
2 4
2
DDeeppaarrt tmmeennttooff AATTMMEECCooleleggeeooffEEnngginineeerirningg 27
CCSSEE
Rooted Trees (I)
• Ancestors
– For any vertex v in a tree T, all the vertices on the simple path
from the root to that vertex are called ancestors.
• Descendants
– All the vertices for which a vertex v is an ancestor are said to be
descendants of v.
• Parent, child and siblings
– If (u, v) is the last edge of the simple path from the root to
vertex v, u is said to be the parent of v and v is called a child of
u.
– Vertices that have the same parent are called siblings.
• Leaves
– A vertex without children is called a leaf.
• Subtree
– A vertex v with all its descendants is called the subtree of T
rooted at v.
Department
Departmentof
of ATME College of Engineering 28
CCSSEE
Rooted Trees (II)
• Depth of a vertex
– The length of the simple path from the root to the vertex.
• Height of a tree
– The length of the longest simple path from the root to a leaf.
h=2
3
4 1 5
9 6
6 8 3 9
5 2 3 2 5 8
Department
Departmentof
of ATME College of Engineering 30
CCSSEE
Computing time functions
Department
Departmentof
of ATME College of Engineering 32
CCSSEE
Order of growth
• Most important: Order of growth within a
constant multiple as n→∞
• Example:
– How much faster will the algorithm run on computer that is
twice as fast?
Department
Departmentof
of ATME College of Engineering 33
CCSSEE
Best-case, average-case, worst-case
Department
Departmentof
of ATME College of Engineering 34
CCSSEE
Asymptotic order of growth
A way of comparing functions that ignores constant
factors and small input sizes
Department
Departmentof
of ATME College of Engineering 35
CCSSEE
Establishing order of growth using the definition
Example:
• 5n+2 is O(n); c= 7 and n0 = 1
Note : The Upper Bound indicates that the function will be the worst case that it
does not consume more than this computing time.
Department
Departmentof
of ATME College of Engineering 36
CCSSEE
Big-oh
Department
Departmentof
of ATME College of Engineering 37
CCSSEE
Establishing order of growth using the definition
Department
Departmentof
of ATME College of Engineering 38
CCSSEE
Big-omega
Department
Departmentof
of ATME College of Engineering 39
CCSSEE
Establishing order of growth using the definition
Example:
• 3n+2 is Ɵ (n)
• c1 g(n) ≤ f(n) ≤ c2 g(n) for every n ≥ n0
• 3 n ≤ 3n+2 ≤ 4 n for every n0 = 2, c1 = 3, c2 = 4
Department
Departmentof
of ATME College of Engineering 40
CCSSEE
Big-theta
Department
Departmentof
of ATME College of Engineering 41
CCSSEE
Properties of asymptotic order of growth
• f(n) O(f(n))
Department
Departmentof
of ATME College of Engineering 42
CCSSEE
Time efficiency of nonrecursive
algorithms
General Plan for Analysis
• Decide on parameter n indicating input size
Department
Departmentof
of ATME College of Engineering 43
CCSSEE
Establishing order of growth using limits
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
Department
Departmentof
of ATME College of Engineering 44
CCSSEE
Example: Sequential search
Department of 45
CSE
Example 1: Maximum element
Department of 46
CSE
Analysis
1. Input parameter : n 3.
2. Basic operation:
Comparison
A[i] > max
4.
Department of 47
CSE
Example 2: Element uniqueness
problem
Department
Departmentof
of ATME College of Engineering 48
CCSSEE
Analysis
1. Input parameter is input size n
2. Basic operation: Comparison A[i] == A[j]
3.
4.
Є O(n2)
Department of 49
CSE
Example 3: Matrix
multiplication
DDeeppaarrtmmeenntoof AATTMMEECCooleleggeeoofEEnngginineeerirningg 50
CCSSEE
Department
Departmentof
of ATME
ATMECollege
CollegeofofEngineering
Engineering 51
CCSSEE
Analysis
1. Input parameter is input size n2 X n2
2. Basic operation: Comparison C[i,j] == C[i,j] + A[i,k] * B[k,j]
3.
Є O(n3)
Department of 52
CSE
Selection Sort
Algorithm SelectionSort (A[0..n-1])
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i 0 to n – 2 do
min i
for j i + 1 to n – 1 do
if A[j] < A[min]
min j
swap A[i] and A[min]
DDeeppaarrtmmeenntoof AATTMMEECCooleleggeeoofEEnngginineeerirningg 53
CCSSEE
Solve recurrence relations
•X(n) = x(n-1) + 5 for n>1, x(1) = 0
X(n) = x(n-1) + 5
X(n) = x(n-2) + 5+5
= x(n-2) + 2 *5
X(n) = x(n-3) + 3*5
Department
Departmentof
of ATME College of Engineering 56
CCSSEE
Plan for Analysis of Recursive
• Decide on a parameter indicating an input’s size.
Algorithms
• Identify the algorithm’s basic operation.
Size:
Basic operation:
Recurrence relation:
Department of 58
CSE
Solving the recurrence for M(n)
Department
Departmentof
of ATME College of Engineering 59
CCSSEE
Tower of Hanoi Problem
• In this problem, we have n disks of different sizes and
three pegs.
• Initially, all the disks are on the first peg in order of
size, the largest on the bottom and the smallest on
top.
• The goal is to move all the disks to the third peg,
using the second one as an auxiliary if necessary.
• We can move only one disk at a time, and it is
forbidden to place a larger disk on top of a smaller
one.
Department
Departmentof
of ATME College of Engineering 60
CCSSEE
Example 2: The Tower of Hanoi
Puzzle
1 3
Department
Departmentof
of ATME College of Engineering 63
CCSSEE
Tree of calls for the Tower of Hanoi
Puzzle
n
n-1 n-1
1 1 1 1 1 1 1 1
Department
Departmentof
of ATME College of Engineering 64
CCSSEE
Fibonacci numbers
The Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, …
Department
Departmentof
of ATME College of Engineering 65
CCSSEE
•The recurrence equation for this problem is:
T(n) = T(n-1) + T(n-2) for n>1 and the initial
conditions are T(0) =0, T(1) = 1
Solution to recurrence relation:
T(n) = T(n-1) + T(n-2)
T(n) –T(n-1) –T(n-2) = 0
This is of the form ax(n) +bx(n-1) +cx(n-2) =0
Which is a homogeneous second order linear
relation with constant co-efficients.
De pa rtmen t oof
Department f ISE BATME
MS I n sCollege
tit u t e ofofTe ch nol og y and Mgmt
Engineering 66
CSE
Department
Departmentof
of ATME College of Engineering 67
CCSSEE
Department
Departmentof
of ATME College of Engineering 68
CCSSEE
Little oh Notation (o)
• The asymptotic upper bound provided by O-
notation may or may not be asymptotically
tight. The bound 2n2 = O(n2) is asymptotically
tight but the bound 2n = o(n2) is not.
• We use o-notation to denote an upper bound
that is not asymptotically tight
• f(n) = o(g(n)); f(n) is equal to the little oh of
g(n), iff f(n) < c, g(n) for any +ve constant c>0,
no>0 and n>no
Department
Departmentof
of ATME College of Engineering 69
CCSSEE
Establishing order of growth using limits
Examples:
• 10n vs. n2
• 5n + 2 vs. n
Department
Departmentof
of ATME College of Engineering 70
CCSSEE
Property of the Asymptotic Notations
Department
Departmentof
of ATME College of Engineering 71
CCSSEE
Brute Force
A straightforward approach, usually based directly on the
problem’s statement and definitions of the concepts involved
Examples:
1. Computing an (a > 0, n a nonnegative integer)
2. Computing n!
Department
Departmentof
of ATME College of Engineering 72
CCSSEE
Brute-Force Sorting Algorithm
Selection Sort Scan the array to find its smallest element and
swap it with the first element. 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 elements.
Generally, on pass i (0 i n-2), find the smallest element in
A[i..n-1] and swap it with A[i]:
Example: 7 3 2 5
Department
Departmentof
of ATME College of Engineering 73
CCSSEE
Analysis of Selection Sort
In place: Yes
Stability: yes
Department of 74
CSE
Brute-Force String Matching
• pattern: a string of m characters to search for
• text: a (longer) string of n characters to search in
• problem: find a substring in the text that matches the pattern
Brute-force algorithm
Step 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character of
pattern to the corresponding character in text until
• all characters are found to match (successful search); or
• a mismatch is detected
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
Department
Departmentof
of ATME College of Engineering 75
CCSSEE
Examples of Brute-Force String Matching
1. Pattern: 001011
Text: 10010101101001100101111010
2. Pattern: happy
Text: It is never too late to have a
happy childhood.
Department
Departmentof
of ATME College of Engineering 76
CCSSEE
Pseudocode and Efficiency
Department of 77
CSE
Brute-Force Strengths and
Weaknesses
• Strengths
– wide applicability
– simplicity
– yields reasonable algorithms for some important problems
(e.g., matrix multiplication, sorting, searching, string
matching)
• Weaknesses
– rarely yields efficient algorithms
– some brute-force algorithms are unacceptably slow
– not as constructive as some other design techniques
Department
Departmentof
of ATME College of Engineering 78
CCSSEE
MODULE – 2
Department of 79
CSE
Divide-and-Conquer
Department
Departmentof
of ATME College of Engineering 80
CCSSEE
Divide and conquer involves three steps,
at each level of recursion.
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
Department
Departmentof
of ATME College of Engineering 83
CCSSEE
General Divide-and-Conquer Recurrence
Department
Departmentof
of ATME College of Engineering 84
CCSSEE
Merge Sort Algorithm
Mergesort(low, high)
//Given an array A of n elements. This algorithm sorts the elements in
//ascending order. The variables low and high are used to identify the
//positions of first and last element in each partition.
1. If (low< high)
2. mid = (low+high)/2;
3. Mergesort (low,mid);
4. Mergesort(mid+1,high);
5. Merge(low,mid,high);
6. End if
7. Exit
Department
Departmentof
of ATME College of Engineering 85
CCSSEE
Merge Algorithm
Merge(low, mid, high)
// The variables low, mid, and high are used to identify the
portions of elements in each partition.
1. Initialize i=low, j= mid+1, h=low;
2. while ((h <= mid) && (j <= high))
3. if (a[h] < a[j])
b[i++] = a[h++];
else
b[i++] = a[j++];
Department
Departmentof
of ATME College of Engineering 86
CCSSEE
Cont…
4. if (h > mid)
for(k = j; k <= high; k++)
b[i++] = a[k];
else
for (k = h; k <= mid; k++)
b[i++] = a[k];
5. for (k = low; k <= high; k++)
a[k] = b[k];
Department
Departmentof
of ATME College of Engineering 87
CCSSEE
Mergesort
Department
Departmentof
of ATME College of Engineering 88
CCSSEE
Mergesort Example
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
83 29 71 54
8 3 2 9 7 1 5 4
The non-recursive
version of Mergesort
38 29 17 45 starts from merging
single elements into
2 3 8 9 1 4 5 7 sorted pairs.
1 2 3 4 5 7 8 9
Department of 89
CSE
Analysis of Mergesort
Department
Departmentof
of ATME College of Engineering 90
CCSSEE
• All cases have same efficiency: Θ(n log n)
• Number of comparisons in the worst case is
close to theoretical minimum for comparison-
based sorting:
log2 n! ≈ n log2 n - 1.44n
• Space requirement: Θ(n) (not in-place)
• Can be implemented without recursion
Department
Departmentof
of ATME College of Engineering 91
CCSSEE
Quicksort
• Select a pivot (partitioning element) – as the first element
•
p
i j
A[i]p A[i]p
p
A[i]p A[i]p
• Exchange the pivot with the last element in the first (i.e., )
subarray — the pivot is now in its final position
• Sort the two subarrays recursively
• Note : Invented by
DDeeppaarrt tmmeennttooff AATTMMEECCooleleggeeooffEEnngginineeerirningg 92
CCSSEE
Quick Sort Algorithm
Quick sort(low, high)
// A is an array of elements.
// The variables low and high are used to identify the positions of first and
// last elements in each partition.
If(low< high) then
J= partition(low, high)
Quick sort( low, j-1)
Quick sort(j+1, high)
End if
Exit
Department
Departmentof
of ATME College of Engineering 93
CCSSEE
Partition Algorithm
Partition(low, high)
//This procedure partitions the element into two lists and places the pivot
//element into a appropriate place. Low = first element of the array, high =
//last element of the array, a[low] = pivot.
Step 1. Set pivot = a[low];
i = low +1;
j = high;
Step 2. Repeat step 3 while (a[i] < pivot && i < high)
Step 3. i++;
Step 4. Repeat step 5 while (a[j] > pivot)
Step 5. j--;
Step 6. If(i<j)
swap a[i] and a[j]
go to step 2
else
swap a[j] and pivot
Department of CSE ATME College of Engineering 94
Step 7. Return (j)
Quicksort Example
5 3 1 9 8 2 4 7
2 3 1 4 5 8 9 7
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
Department
Departmentof
of ATME College of Engineering
CCSSEE
Analysis of Quicksort
• Best case: split in the middle — Θ(n log n)
• Worst case: sorted array! — Θ(n2) T(n) = T(n-1) + Θ(n)
• Improvements:
– better pivot selection: median of three partitioning
– switch to insertion sort on small subfiles
– elimination of recursion
These combine to 20-25% improvement
Department
Departmentof
of ATME College of Engineering
CCSSEE
Binary Search
Algorithm Binary_Search( A[0…n-1], Key)
Input: Given an array of n elements in sorted order and key is an element to be
searched.
Output: Returns the position of key element, if successful and returns -1
otherwise.
1. Set first = 0, last = n-1
2. While (first < = last)
mid = (first +last) / 2
if (key == A[mid])
return (mid+1); // successful
else if ( key < A[mid] )
last = mid – 1
else
first = mid+1
end while
3. return -1 // unsuccessful
Department
Departmentof
of ATME College of Engineering
CCSSEE
Analysis
Best Case: Best case occurs, when we are
searching the middle element itself. In that case,
total number of comparisons required is 1. there
fore best case time complexity of binary search
is Ω(1).
Worst Case: Let T(n) be the cost involved to
search ‘n’ elements. Let T(n/2) be the cost
involved to search either left part or the right
part of an array.
Department
Departmentof
of ATME College of Engineering
CCSSEE
Analysis
T(n) = a if n = 1
T(n/2) + b otherwise
Department
Departmentof
of ATME College of Engineering
CCSSEE
Analysis
Average Case:
The average case occurs when an element is found some where
in the recursive calls, but not till the recursive call ends.
The average number of key comparisons made by binary search
is only slightly smaller than that in this worst case.
T(n) = log 2n
The average number of comparison in a successful search is
T(n) = log 2n – 1
The average number of comparison in a unsuccessful search is
T(n) = log 2n + 1
Department
Departmentof
of ATME College of Engineering
CCSSEE
Algorithm for straight forward maximum and
minimum
StraightMaxMin(a,n,max,min)
// set max to the maximum and min to the minimum of a[1:n].
{
max := min := a[1];
for i := 2 to n do
{
if(a[i] > max) then max := a[i];
if(a[i] < min) then min := a[i];
}
}
Department
Departmentof
of ATME College of Engineering
CCSSEE
Analysis
• This algorithm requires 2(n-1) element
comparisons in the best, average, and worst
cases.
• Now the Best case occurs when the elements
are in increasing order. The number of
element comparisons is n-1.
• The worst case occurs when the element are
in decreasing order. In this case number of
comparisons is 2(n-1).
Department
Departmentof
of ATME College of Engineering
CCSSEE
Finding maximum and minimum using
divide and conquer technique
Algorithm max_min(i, j, max, min)
{
// Input: a[1:n] is a global array. Parameters i and j
are integers, 1<=i <= j<= n.
// output: to set max and min to the largest and
smallest values in a[i: j], respectively.
If (i == j) then // Small(P)
{ max = min A[i];
} Department
Departmentof
of ATME
ATMECollege 103
CollegeofofEngineering
Engineering
CCSSEE
else if ( i =j-1) then // Another case of Small(P)
{
if (A[i] < A[j]) then
{
max A[j]
min A[i]
}
else
{
max A[i]
min A[j]
}
} DDeeppaarrt tmmeennttooff
CCSSEE
AATTMMEECCooleleggeeooffEEnngginineeerirningg
else
{
// if P is not small, divide P into sub problems. //
Find where to split the set
mid := (i+j)/2;
// Solve the sub problems.
max_min(i,mid,max,min);
max_min(mid+1, j, max1,min1);
// Combine the solutions
if( max < max1) then max := max1;
if( min > min1) then min:= min1;
}
} DDeeppaarrt tmmeennttooff
CCSSEE
AATTMMEECCooleleggeeooffEEnngginineeerirningg
Analysis
0 n=1
T(n) = 1 n=2
T(n/2) + T(n/2) + 2 n>2
A = 12345678901357986429 B = 87654321284820912836
Department
Departmentof
of ATME College of Engineering
CCSSEE
Second Divide-and-Conquer
A B = A B ·10 + (A B + A B ) ·10 + A B
n n/2
1 1
Algorithm
1 2 2 1 2 2
Department
Departmentof
of ATME College of Engineering
CCSSEE
Example of Large-Integer
2135 4014
Multiplication
= (21*10^2 + 35) * (40*10^2 + 14)
= (21*40)*10^4 + c1*10^2 + 35*14
where c1 = (21+35)*(40+14) - 21*40 - 35*14, and
21*40 = (2*10 + 1) * (4*10 + 0)
= (2*4)*10^2 + c2*10 + 1*0
where c2 = (2+1)*(4+0) - 2*4 - 1*0, etc.
Department
Departmentof
of ATME College of Engineering
CCSSEE
Matrix Multiplication
• Brute-force algorithm
C1 = E + I + J - G C2 = D + G
=
C3 = E + F C4 = D + H + J - F
D = A1(B2 – B4)
E = A4( B3 – B1)
F = (A3 + A4) B1
G = (A1 + A2) B4
7 multiplications
H = (A3 – A1) (B1 + B2)
I = (A2 – A4) (B3 +B4) 18 additions
J = (A1 +A4)(B1 +B4)
DDeeppaarrt tmmeennttooff AATTMMEECCooleleggeeooffEEnngginineeerirningg 112
CCSSEE
Strassen’s Matrix Multiplication
A= 1 2 B= 1 1
3 4 2 2
A1 = 1, A2 =2, A3 = 3, A4 = 4
B1 = 1, B2 = 2, B3 = 2, B4 = 2
1. D = A1(B2 – B4) = 1(1 – 2) = -1
2. E = A4(B3-B1) = 4(2-1) = 4
3. F = (A3 + A4) B1 = (3+4)1 = 7
4. G = (A1 + A2)B4 = (1+2)2 = 6
Department
Departmentof
of ATME College of Engineering
CCSSEE
5. H = (A3 – A1) (B1 + B2) = (3-1)(1+1) = 4
6. I = (A2 – A4)(B3+B4) = (2-4)(2+2) = -8
7. J = (A1+A4)(B1+B4) = (1+4)(1+2) = 15
Department
Departmentof
of ATME College of Engineering
CCSSEE
Strassen’s Matrix Multiplication
Strassen observed [1969] that the product of two
matrices can be computed in general as follows:
M1 + M4 - M5 + M7 M3 + M5
=
M2 + M4 M1 + M3 - M2 + M6
Department
Departmentof
of ATME College of Engineering
CCSSEE
M4 = A11 (B10 - B00)
= 4 * (2 - 1) = 4
A00 A01 B00 B01
M5 = (A00 + A01) B11
1 2 1 1 = (1 + 2) * 2 = 6
M1 + M4 - M5 + M7 M3 + M5 5 5
M2 = (A 10 + A11) B 00
= (3 + 4) * 1 = 7 =
M2 + M4 M1 + M3 - M2 + M6 11 11
M3 = A00 (B01 - B11)
= 1 * (1 - 2) = -1
C10 C11
Department
Departmentof
of ATME College of Engineering
CCSSEE
M4 = A11 (B10 - B00)
= 4 * (1 - 5) = -16
A00 A01 B00 B01
M5 = (A00 + A01) B11
2 1 5 2 = (2 + 1) * 2 = 6
M1 + M4 - M5 + M7 M3 + M5 11 6
M2 = (A 10 + A11) B 00
= (3 + 4) * 5 = 35 =
M2 + M4 M1 + M3 - M2 + M6 19 14
M3 = A00 (B01 - B11)
= 2 * (2 - 2) = 0
C10 C11
Department
Departmentof
of ATME College of Engineering
CCSSEE
Solve
1021
0101
4110
2104
A= 0130 B = 2011
5021
1 350
A1 A2 B1 B2
10 21 01 01
41 10 21 04
01 30 20 11
50 21 13 50
A3 A4 B3 B4
Department
Departmentof
of ATME College of Engineering
CCSSEE
1. D = A1 (B2 – B4)
01 11
10 -
* 04 50
41
10 -1 0
41 * -5 4
-6 0
-9 4
2. E = A4 (B3 – B1)
Department
Departmentof
of ATME College of Engineering
CCSSEE
Analysis of Strassen’s Algorithm
If n is not a power of 2, matrices can be padded with zeros.
Number of multiplications:
M(n) = 7M(n/2), M(1) = 1
M(n) = 7M(2 k-1)
= 7[7M(2 k-2)] = 7 2 M(2 k-2)]
= 7 k M(2 k-k)] = 7 k (1)
Department
Departmentof
of ATME College of Engineering
CCSSEE
Advantages and Disadvantages
• Difficult problems is broken down into sub
problems and each sub problem is solved
independently.
• It gives efficient algorithms like quick sort,
merge sort, streassen’s matrix multiplication.
• Sub problems can be executed on parallel
processor.
Disadvantage
• It makes use of recursive methods and the
recursion is slow and complex.
Department
Departmentof
of ATME College of Engineering
CCSSEE
Decrease-and-Conquer
The decrease and conquer technique is almost similar to the
divide and conquer technique, but instead of dividing the
problem into size n/2, it is decremented by a constant or
constant factor.
There are three variations of decrease and conquer
• Decrease by a constant
• Decrease by a constant factor
• Variable size decrease
The problems can be solved either top down (recursively) or
bottom up ( without recursion)
Department
Departmentof
of ATME College of Engineering
CCSSEE
Decrease by a constant
• In this type of variation, the size of an instance
is reduced by the same constant ‘1’ on each
iteration. So, if a problem is of size ‘n’ , then a
sub problem of size ‘n-1’ is solved first but
before a sub sub problem of size ‘n-2’ is solved
and so on.
Department
Departmentof
of ATME College of Engineering
CCSSEE
Decrease by a constant
1 2 n
…….
Problem of Size n
1 ……. n-1
= a. a. a. a. . . n times
The above definition is a recursive definition i.e, a top down approach
Department
Departmentof
of ATME College of Engineering
CCSSEE
Decrease by a constant factor
1 2 n
…….
Problem of Size n
1 ……. n/2
(n / 4)
---
Department
Departmentof
of ATME College of Engineering
CCSSEE
Variable size decrease
In this type, the reduction in the size of the
problem instance is varied from one iteration to
another.
Eg: Euclid’s algorithm for computing
GCD of two nos.
gcd (m,n) = gcd (n, m mod n) if n> 0
m if n=0
Eg: Computing a median, Interpolation Search
and Binary Search Tree
Department
Departmentof
of ATME College of Engineering
CCSSEE
DAGs and Topological Sorting
A dag: a directed acyclic graph, i.e. a directed graph with no
(directed) cycles
a b a b
c d c d
Vertices of a dag can be linearly ordered so that for every edge its
starting vertex is listed before its ending vertex (topological
sorting). Being a dag is also a necessary condition for topological
sorting to be possible.
Department
Departmentof
of ATME College of Engineering
CCSSEE
Topological Sorting Example
Order the following items in a food chain
tiger
human
fish
sheep
shrimp
plankton wheat
Department
Departmentof
of ATME College of Engineering
CCSSEE
DFS-based Algorithm
DFS-based algorithm for topological sorting
– Perform DFS traversal, noting the order vertices
are popped off the traversal stack
– Reverse order solves topological sorting problem
– Back edges encountered?→ NOT a dag!
Example:
b a c d
e f g h
Department
Departmentof
of ATME College of Engineering
CCSSEE
Source Removal Algorithm
a d
Example 2 c
b e
Department
Departmentof
of ATME College of Engineering
CCSSEE
Source Removal Algorithm
Topological Sort(G)
1. Find the indegree INDG(n) of each node n of
G.
2. Put in a queue Q all the nodes with zero
indegree.
3. Repeat step 4 and 5 until G becomes empty.
4. Repeat the element n of the queue Q and
add it to T (Set Front = Front +1).
Department
Departmentof
of ATME College of Engineering
CCSSEE
Source Removal Algorithm
5.Repeat the following for each neighbour, m of
the node n
a) Set INDEG(m) = INDG(m)-1
b) If INDEG(m) = 0 then add m to the rear end
of the Q.
6. Exit.
Department
Departmentof
of ATME College of Engineering
CCSSEE
MODULE – 3
GREEDY METHOD
Department of
CSE
Greedy Method
• Approach for Solving problem
• Used for Solving Optimization Problem
• Optimization Problem : Problems which demands
minimum/maximum results
• Example:
12 hrs Minimum cost
A B
S1 S2 S3 S4 S5……
Department
Departmentof
of ATME
ATMECollege
CollegeofofEngineering
Engineering 140
CCSSEE
Greedy Technique
Greedy algorithms, construct a solution through a
sequence of steps, each step expanding a partially
constructed solution obtained so far, until a complete
solution to the problem is reached.
144
Change-Making Problem
• Problem Statement: Given coins of several denominations
find out a way to give a customer an amount with fewest
number of coins.
• Example: if denominations are 1,5,10, 25 and 100 and the
change required is 30, the solutions are,
• Amount : 30
• Solutions : 3 x 10 ( 3 coins )
6 x 5 ( 6 coins )
1 x 25 + 5 x 1 ( 6 coins )
1 x 25 + 1 x 5 ( 2 coins )
The last solution is the optimal one as it gives us change only with 2
coins. Department
Departmentof
ofCSE ATME
ATMECollege
CollegeofofEngineering
Engineering 145
CSE
Change-Making Problem
for i 1 to n do
{
Coins[i] = C/d[i];
C = C mod d[i]
Print coins[i]
}
Optimal solution using this method is (x1, x2, x3,x4,x5,x6,x7) = (1, 0,1, 0.57,0,1,0)
with profit = 47
Department of ATME College of Engineering
CSE
Profits = Solution Vector = (1, 1,4/5,0,1,1,1)= (1, 1,0.8,0,1,1,1)
Optimal solution using this method is (x1, x2, x3,x4,x5,x6,x7) = (1, 1,0.8,0,1,1,1)
with profit = 54
Optimal solution is not guaranteed using method 1 and 2
Department of ATME College of Engineering
CSE
Profits = Solution Vector = (1, 2/3,1,0, 1,1,1)= (1, 0.67,1,0, 1,1,1)
Jobs n= 5
p1,p2,p3,p4,p5 = 20,15,10,1,6
d1,d2,d3,d4,d5 = 2, 2, 1, 3,3
6 c 6 c c
a a a
4 1 1 1
4
2 2
d d d
b 3 b b 3
Example:
6 c 6 c c
a a a
1 1 1
4 4
2 2
d d d
b b b 3
3
COST=11 COST=6
Department
Departmentof
of ATME College of Engineering
CCSSEE
MST – Prim’s algorithm
Department
Departmentof
of ATME College of Engineering
CCSSEE
Department
Departmentof
of ATME College of Engineering
CCSSEE
Departm
Department
ent of
of ISE BM
ATSME
I nCo
s titluletg
e eofo fTE
encg
hinnoelo
egriyng
a Mgmt 170
CSE nd
DeDeppaartr tmmeennttooffCS ATATMMEECoColeleggeeooffEEnngginineeeeririnngg 171
ECSE
Efficiency
The time efficiency of depends on the data
structures used for implementing the priority
queue and for representing the input graph.
Since we have implemented using weighted
matrix and unordered array, the efficiency is
O(|V2|).
If we implement using adjacency list and the
priority queue for min-heap, the efficiency is
O(|E|log|V|).
Department
Departmentof
of ATME College of Engineering
CCSSEE
Shortest paths – Dijkstra’s
algorithm
The Dijkstra’s algorithm finds the shortest path
from a given vertex to all the remaining vertices
in a diagraph.
The constraint is that each edge has non-
negative cost. The length of the path is the sum
of the costs of the edges on the path.
We have to find out the shortest path from a
given source vertex ‘S’ to each of the
destinations (other vertices ) in the graph.
DDeeppaarrt tmmeennttooff AATTMMEECCooleleggeeooffEEnngginineeerirningg
CCSSEE
Example
1 2 3 4
5
Initially S 0 1 0 0 0 0
1
d ∞ 1 60 100 ∞ 10
60 100
1
10 60 10
2 3
0
10
20 2 3
50 20
20
4 50 20
5
5
4
5
DDeeppaarrt tmmeennttooff
CCSSEE
5
BAMTSMEInCstoitluletgeeooffTEencghinnoe leorgiyngandMgmt
Example
1 2 3 4 5
S 1 0 0 0 1
1
d 1 60 100 ∞ 10
60 100
1
10 60 100
2 3
10
20 2 3
50 20
20
4 50 20
5
5
4
5
DDeeppaarrt tmmeennttooff
CCSSEE
5
BAMTSMEInCstoitluletgeeooffTEencghinnoe leorgiyngandMgmt
Example 1 2 3 4 5
S 1 0 0 1 1
1
d 1 60 100 15 10
60 100
1
10 60 100
2 3
10
20 2 3
50 20
20
4 50 20
5
5
4
5
DDeeppaarrt tmmeennttooff
CCSSEE
5
BAMTSMEInCstoitluletgeeooffTEencghinnoe leorgiyngandMgmt
Example
1 2 3 4 5
S 1 0 1 1 1
1
d 1 60 35 15 10
60 100
1
10 60 100
2 3
10
20 2 3
50 20
20
4 50 20
5
5
4
5
DDeeppaarrt tmmeennttooff
CCSSEE
5
BAMTSMEInCstoitluletgeeooffTEencghinnoe leorgiyngandMgmt
Example Final distance form note 1 to
all other nodes
1 2 3 4 5
S 1 1 1 1 1
1
d 1 60 35 15 10
60 100
1
10 60 100
2 3
10
20 2 3
50 20
20
4 50 20
5
5
4
5
DDeeppaarrt tmmeennttooff
CCSSEE
5
BAMTSMEInCstoitluletgeeooffTEencghinnoe leorgiyngandMgmt
b 4
c
Example2 a
3
7 2
dd
5 4
6
b 4
c
b(a,3) c(b,3+4) d(b,3+2) e(-,∞) 3 6
2 5
a d e
7 4
b 4 c
d(b,5) c(b,7) e(d,5+4) 3 6
2 5
a d e
7 4
4
b c
c(b,7) e(d,9) 3 6
2 5
a d e
7 4
e(d,9)
Department of ATME College of Engineering 184
CSE
Dijkstra’s algorithm
Dijkstra’s( s)
// Finds shortest path from source vertex to all other vertices
//Input: Weighted connected graph G=<V,E> with nonnegative
weights and its vertices s
//Output: The length of distance of a shortest path from s to v
{
1. for i = 1 to n do // Intialize
S[i] = 0;
d[i] = a[s][i];
DY N A M I C P RO G R A M M I N G
Department
Departmentof
of ATME
ATMECollege
CollegeofofEngineering
Engineering 190
CCSSEE
Example: Fibonacci numbers
Department
Departmentof
of ATME College of Engineering
CCSSEE
Department
Departmentof
of ATME College of Engineering
CCSSEE
DeDeppaartr tmmeennttooffCS ATATMMEECoColeleggeeooffEEnngginineeeeririnngg 194
ECSE
Examples of DP algorithms
• Computing a binomial coefficient
Department
Departmentof
of ATME College of Engineering
CCSSEE
Warshall’s Algorithm: Transitive
Closure
{
R(k)[i,j] =
R(k-1)[i,j]
or
(path using just 1 ,…,k-1)
Initial condition?
j
Department of
CSE
Warshall’s Algorithm (matrix generation)
Department of
CSE
Warshall’s Algorithm (pseudocode and analysis)
Example:
D(k-1)[i,k]
k
i
D(k-1)[k,j]
D(k-1)[i,j]
j
Initial condition?
Department of ATME College of Engineering 212
CSE
Time efficiency: Θ(n3)
Note: Works on graphs with negative edges but without negative
cycles.
DDeeppaarrtmmeenntoof AATTMMEECCooleleggeeoofEEnngginineeerirningg 213
CCSSEE
Optimal Binary Search Tree
Binary Tree
Department
Departmentof
of ATME College of Engineering
CCSSEE
total number of BSTs with n nodes is given
by C(2n,n)/(n+1)
Department
Departmentof
of ATME College of Engineering
CCSSEE
Cost of searching any Key
Department
Departmentof
of ATME College of Engineering
CCSSEE
Cost of BST-Frequencies
18
C
Average # of comparisons
= 1*0.4 + 2*(0.2+0.3) + 3*0.1
B D
= 1.7
A
Department of
CSE
Knapsack problem
Department of
CSE
For the given instances of problem obtain the
optimal solution for the knapsack problem
Department of
CSE
Department
Departmentof
of ATME College of Engineering
CCSSEE
Department
Departmentof
ofCSE BMS
ATME
Institute
CollegeofofTechnology and M gmt
Engineering 234
CSE
DeDeppaartr tmmeennttooffCS ATATMMEECoColeleggeeooffEEnngginineeeeririnngg 235
ECSE
Department of CSE ATME College of Engineering 236
DeDeppaartr tmmeennttooffCS ATATMMEECoColeleggeeooffEEnngginineeeeririnngg
ECSE
DeDeppaartr tmmeennttooffCS ATATMMEECoColeleggeeooffEEnngginineeeeririnngg
ECSE
DeDeppaartr tmmeennttooffCS ATATMMEECoColeleggeeooffEEnngginineeeeririnngg
ECSE
Department
Departmentof
of ATME College of Engineering
CCSSEE
To find items to be selected
Department
Departmentof
of ATME College of Engineering
CCSSEE
DeDeppaartr tmmeennttooffCS ATATMMEECoColeleggeeooffEEnngginineeeeririnngg
ECSE
DeDeppaartr tmmeennttooffCS ATATMMEECoColeleggeeooffEEnngginineeeeririnngg
ECSE
Knapsack Problem by DP (pseudocode)
Algorithm DPKnapsack(int: w[1..n], int: p[1..n], M)
int: V[0..n,0..M]
for j := 0 to M do
V[0,j] := 0
for i := 0 to n do
V[i,0] := 0
for i := 1 to n do
for j := 1 to M do
if w[i] j and p[i] + V[i-1,j-w[i]] > V[i-1,j] then
V[i,j] := p[i] + V[i-1,j-w[i]];
else
V[i,j] := V[i-1,j];
return V[n,M]
w3 = 3, p3= 16 3 0 -1 -1 -1 -1 -1
w4 = 2, p4= 11 4 0 -1 -1 -1 -1 -1 ? 249
Memory Function Knapsack
Department
Departmentof
of ATME College of Engineering
CCSSEE
Find V[2,3] = max{ mkf(1,3), 6+mfk(1,2)}
= max{ , 6+ -}
Find V[1,5] = max{ mkf(0,5), 8+mfk(0,3)}
= max{ 0 , 8 + 0} = 8
Find V[1,4] = max{ mkf(0,4), 8+mfk(0,2)}
= max{ 0 , 8 + 0} = 8 Back
Substitute
Find V[1,2] = max{ mkf(0,2), 8+mfk(0,0)} these
values
= max{ 0 , 8 + 0} = 8
Find V[1,3] = max{ mkf(0,3), 8+mfk(0,0)}
= max{ 0 , 8 + 0} = 8
Find V[1,1] = max{ mkf(0,1)} = 0
Department
Departmentof
of ATME College of Engineering
CCSSEE
Find V[2,3] = max{ mkf(1,3), 6+mfk(1,2)}
= max{ 8, 6 + 8} = 14
Find V[1,5] = max{ mkf(0,5), 8+mfk(0,3)}
= max{ 0 , 8 + 0} = 8
Find V[1,4] = max{ mkf(0,4), 8+mfk(0,2)}
= max{ 0 , 8 + 0} = 8 Back
Substitute
Find V[1,2] = max{ mkf(0,2), 8+mfk(0,0)} these
values
= max{ 0 , 8 + 0} = 8
Find V[1,3] = max{ mkf(0,3), 8+mfk(0,0)}
= max{ 0 , 8 + 0} = 8
Find V[1,1] = max{ mkf(0,1)} = 0
Department
Departmentof
of ATME College of Engineering
CCSSEE
Memory Function Knapsack
Find V[3,3] = max{ mfk[i-1,j], p[i] + mfk[i-1, j-wi]}
= max{ mkf(2,3), 11+mfk(2,0)}
= max{ 14 , 16+ 0)} = 16
Find V[2,5] = max{ mkf(1,5), 6+mfk(1,4)}
= max{ 8, 6+ 8} = 14
Find V[2,2] = max{ mkf(1,2), 6+mfk(1,1)}
= max{ 8, 6 + 0} = 8
Department
Departmentof
of ATME College of Engineering
CCSSEE
Memory Function Knapsack
V[i,j] = max{ mfk[i-1,j], p[i] + mfk[i-1, j-wi]}
i=4, j=5, p[i]=11, wi =2
J-wi = 5-2 = 3 ( able to fit into knapsack)
Find V[4,5] = max{ mfk[i-1,j], p[i] + mfk[i-1, j-wi]}
= max{ mkf(3,5), 11+mfk(3,3)}
= max{ 24 , 11+ 16)} = 27
Find V[3,5] = max{ mkf(2,5), 16+mfk(2,2)}
= max{ 14, 16+ 8} = 24
Department
Departmentof
of ATME College of Engineering
CCSSEE
Memory Function Knapsack
Example: Knapsack of capacity M = 5
item weight value
1 2 $8
2 1 $6
3 3 $16
4 2 $11 capacity j
0 1 2 3 4 5
0 0 0 0 0 0 0 0 0 0 0 0 0
w1 = 2, p1= 8 1 0 -1 -1 -1 -1 -1 0 0 8 8 8 8
w = 1, p = 6 2 0 -1 -1 -1 -1 -1 0 -1
2 2
8 14 -1 14
w3 = 3, p3 = 16 3 0 -1 -1 -1 -1 -1 0 -1 -1 16 -1 24
w4 = 2, p4= 11 4 0 -1 -1 -1 -1 -1 ? 0 -1 -1 -1 -1 27 256
Memory Function Knapsack Algo.
ALGORITHM MFKnapsack(i, j) //Implements the memory function method for
the knapsack problem //Input: A nonnegative integer i indicating the number
of the first // items being considered and a nonnegative integer j indicating //
the knapsack capacity //Output: The value of an optimal feasible subset of the
first i items //Note: Uses as global variables input arrays Weights[1..n],
Values[1..n], //and table V[0..n, 0..W]whose entries are initialized with −1’s
except for //row 0 and column 0 initialized with 0’s
if V[i, j]< 0
if j<Weights[i] = value←MFKnapsack(i −1,j)
else value←max,MFKnapsack(i −1, j),
values[i]+MFKnapsack(i −1, j−Weights*i+)-
V[i, j+←value
return V[i, j]
Department of CSE ATME College of Engineering 257
MODULE – 5
BACKTRACKING
Q4
1 2
1
2 3 4
4 3
3 2 4 3
Dead end
4 2
1 1
Solution Solution
Department of
CSE
1
2 3 4
3 2 4 3
Dead end
4 2
1 1
Solution Solution
Department of
CSE
Department of
CSE
Sum of subsets
Department of
CSE
Sum of subsets
Department of
CSE
Sum of Subsets
Find a subset of a given set S= {S1, S2, S3, S4, ------ Sn}
Of n +ve integers whose sum is equal to given +ve integer d subject
to the constrains
Department of
CSE
For a node at level i, the left child corresponds to Xi = 1
and the right child to Xi = 0
Department of
CSE
The bounding function can be strengthened if we
assume that Wi’s are initially in increasing order.
𝑎𝑛𝑑 ∑ 𝑊𝑖 𝑋𝑖 + 𝑊 𝑘 + 1 𝑑
≤
𝑖=1
Department of
CSE
State space tree
0, 1, 21
X1=1 X1=0
3, 2, 18 0, 2, 18
X2=1 X2=0
8, 3, 13 3, 3, 13
14, 4, 7 8, 4, 7 9, 4, 7 3, 4, 7
X4=1
15, 5, 0
Solution
Department of
CSE
Sum of subsets
Department of
CSE
Sum of subsets
Department of
CSE
Sum of subsets
Department of
CSE
Coding
for (int i = 1; i <= n; i++)
sum = sum + S[i];
if (sum < d || S[1] > d)
System.out.println("No Subset possible");
else
SumofSub(0, 0, sum);
Department of
CSE
static void SumofSub(int i, int weight, int total) {
if (promising(i, weight, total) == true)
if (weight == d) {
for (int j = 1; j <= i; j++) {
if (soln[j] == 1)
System.out.print(S[j] + " ");
}
System.out.println();
} else {
soln[i + 1] = 1;
SumofSub(i + 1, weight + S[i + 1], total - S[i + 1]);
soln[i + 1] = 0;
SumofSub(i + 1, weight, total - S[i + 1]);
}
} Department of CSE ATME College of Engineering 294
static boolean promising(int i, int weight, int
total) {
return ((weight + total >= d) && (weight == d ||
weight + S[i + 1] <= d));
Department of
CSE
Graph Coloring
Department of
CSE
Department of
CSE
Department of
CSE
Graph Coloring
Department of
CSE
Graph Coloring
Department of
CSE
Branch and Bound
The term Branch means the way in which we search the
state space tree and Bound means assigning bounding
function at each node. This bounding function is used to
prevent the expansion of nodes that cannot possibly
lead to an answer node.
Department of
CSE
Example 1
Assignment Problem : given n jobs <j1,j2,--- jn> and n persons
<p1,p2,p3 ---pn>, it is required to assign all n jobs to all n persons
with the constraint that one job has to be assigned to one person
and the cost involved in completing all the jobs should be
minimum.
J1 J2 j3 j4
A 9 2 7 8
B 6 4 3 7
C 5 8 1 8
D 7 6 9 4
Department of
CSE
Example 1
J1 J2 j3 j4 Take minimum in each row
A 9 2 7 8 2
B 6 4 3 7 3
C 5 8 1 8 1
D 7 6 9 4 4
10
a J1 a J2 a J3 a J4
a 9 2 7 8
b 3 3 4 3
c 8 5 5 5
d 4 4 4 6
24 14 20 22
Department of
CSE
Example 1
J1 J2 J3 J4 Take minimum in each row
A 9 2 7 8 2
B 6 4 3 7 3
C 5 8 1 8 1
D 7 6 9 4 4
10
b J1 b J3 b J4
a 2 2 2
b 6 3 7
c 1 5 1
d 4 4 7
13 14 17
Department of
CSE
Example 1
J1 J2 J3 J4 Take minimum in each row
A 9 2 7 8 2
B 6 4 3 7 3
C 5 8 1 8 1
D 7 6 9 4 4
10
C J3 C J4 C J4
a 2 2 a 2
b 6 6 b 6
c 1 8 c 1
d 4 9 d 4
13 25 13
Department of
CSE
Start
Lb=10
c –> J3 c –> J4
Lb=13 Lb=25
d –> J4
Lb=13
Department of
CSE
Example 2
J1 J2 j3 j4
A 10 3 8 9
B 7 5 4 8
C 6 9 2 9
D 8 7 10 5
Department of
CSE
Knapsack Problem
Knapsack Problem: Given n items of known weights wi and values vi, i=1, 2, . . . ,
n,and a knapsack of capacity W, find the most valuable subset of the items that
fit in the knapsack. It is convenient to order the items of a given instance in
descending order by their value-to-weight ratios. Then the first item gives the
best payoff per weight unit and the last one gives the worst payoff per weight
unit
Department of
CSE
First arrange in V/W in decreasing order
Since it is a maximization problem. The upper bound is calculated
using the function
Department of
CSE
w=0. v=0
Ub = 100
Department of
CSE
w=0. v=0
Ub = 100
Department of
CSE
w=0. v=0
Ub = 100
Lower bound = lb = S / 2;
Where S = [Va+ Vb+Vc+Vd]
Va = sum of distances from vertex a to the nearest
vertices 1 + 3 = 4
Vb = 1+3 = 4
Vc = 1+2= 3
Vd = 1+2 = 3
Lb = [4 +4 +3+3] / 2 = 14 / 2 = 7
Department of
CSE
Now find,
a b = (3+1)+(3+1)+(1+2)+(1+2) = 14/ 2 = 7
a c = (1+3)+(3+1)+(1+2)+(1+2) = 14/ 2 = 7
a d = (4+1)+(1+3)+(1+2)+(4+1) = 17/2 = 8
Start
Lb=7
Department of
CSE
Now find,
b c = (3+1)+(5+1)+(5+1)+(1+2) = 19/ 2 = 9
b d = (1+3)+(1+3)+(1+2)+(1+2) = 14/ 2 = 7
c b = (1+3)+(5+1)+(5+1)+(1+2) = 19/2 = 9
c d = (1+3)+(3+1)+(2+1)+(2+1) = 14/ 2 = 7
Start
Lb=7
Lb=7 Lb=8
Lb=7
Department of
CSE
Now find,
d c = (1+3)+(1+3)+(2+1)+(1+2) = 14/2 = 7
d b = (1+3)+(1+3)+(1+2)+(1+2) = 14/2 = 7
c a = (1+3)+(1+3)+(1+2)+(1+2) = 14/2 = 7
b a = (3+1)+(3+1)+(1+2)+(1+2) = 14/2 = 7
Start
Lb=7
a b – > d c a c d – > b
Lb=7 Lb=7
a c d – > b
a b – > d c
Lb=7
Lb=7
Department of
CSE
Thank you
24-08-2020 Department of
CSE