Advanced Problem Solving Techniques CSE703 Module-1
Advanced Problem Solving Techniques CSE703 Module-1
Module-1
Design and Analysis of
Algorithm
CSE 703
Advanced Problem Solving
Techniques
Syllabus
• Course Objective: (CSE703)
• To impart the basic concepts of data structures
and algorithms and understand about writing
step by step approach in solving problems with
the help of appropriate data structures also
improve problem solving skills in operating
system and database management.
Amity School of Engineering & Technology
Course Outcomes:
• Able to analyze algorithms and determine their
complexity.
• Ability to apply the algorithms and design techniques
to solve problems.
• Ability to understand various views and management
policies adopted by O.S. as pertaining with processes ,
Deadlock , memory , File and I/O operations.
• Ability to understand the concepts of relational data
model, entity-relationship model, relational database
design, relational algebra and SQL.
• Ability to improve the database design by
normalization.
Amity School of Engineering & Technology
Algorithm
• An algorithm is a set of commands that must be
followed for a computer to perform calculations
or other problem-solving operations.
• According to its formal definition, an algorithm is
a finite set of instructions carried out in a specific
order to perform a particular task.
Amity School of Engineering & Technology
Analyzing Algorithms
• Algorithms are analyzed through their complexity
analysis.
• Complexity analysis is defined as a technique to
measure how long an algorithm would take to complete
given an input of size n; independent of the machine,
language, and compiler. It is used for evaluating the
variations of execution time/memory space on different
algorithms.
• Complexity Analysis is Required?
– It gives you an estimated time and space required to
execute a program.
– It is used for comparing different algorithms for different
input sizes.
– It helps to determine the difficulty of a problem.
Amity School of Engineering & Technology
Asymptotic Notations
• The study of the variations in the performance of the
algorithm with the change in the order of the input size is
called Asymptotic Analysis.
• Asymptotic notations are mathematical notations to
describe the running time of an algorithm when the input
tends towards a particular value or a limiting value.
• In other words, it defines the mathematical limits of its
run-time performance.
• There are mainly three asymptotic notations for the
complexity analysis of algorithms. They are as
follows:
– Big-O notation
– Omega notation
– Theta notation
Amity School of Engineering & Technology
• Space Complexity
• Space complexity is the amount of memory space an
algorithm or a problem takes during the execution of that
particular problem/algorithm.
• Here, the space used includes both, the variables and
the input values. Space complexity is the combination or
sum up of the auxiliary space and the space used by
input values.
• Auxiliary space is the temporary space required by an
algorithm/problem during the execution of that
algorithm/problem.
• Space Complexity = Auxiliary Space + Space used
for input values. It is frequently represented using Big
O notation, just like time complexity, to describe the
maximum amount of memory utilization.
Amity School of Engineering & Technology
Algorithm cases
• Worst-Case: The maximum time or space an
algorithm can take, providing an upper bound.
• Average-Case: The expected time or space an
algorithm takes, averaged over all possible
inputs.
• Best-Case: The minimum time or space an
algorithm can take, providing a lower bound.
Amity School of Engineering & Technology
Substitution Method
• The Substitution Method Consists of two main steps:
– Guess the Solution.
– Use the mathematical induction to find the boundary condition
and shows that the guess is correct.
• For Example Solve the equation by Substitution
Method. T (n) = T (n/2)+ n We have to show that it is
asymptotically bound by O (log n).
• Solution:
– For T (n) = O (log n) We have to show that for some constant c
– T (n) ≤c logn.
– Put this in given Recurrence Equation.
– T (n) ≤c log (n/2)+ 1 ≤c log(n/2)+ 1 = c logn-clog2 2+1 ≤c logn for
c≥1
– Thus T (n) =O logn.
Amity School of Engineering & Technology
Iteration Methods
• Consider the Recurrence
• T (n) = 1 if n=1
= 2T (n-1) if n>1
Amity School of Engineering & Technology
• T (n) = 2T (n-1)
• = 2[2T (n-2)] = 22T (n-2)
• = 4[2T (n-3)] = 23T (n-3)
• = 8[2T (n-4)] = 24T (n-4) …………….(Eq.1)
Answer: O(n2 )
Amity School of Engineering & Technology
Assignment-1
Consider the following recurrence
T (n) = 4T(n/2) +n
Master Method
• It is a direct way to get the solution. The master
method works only for the following type of
recurrences or for recurrences that can be
transformed into the following type.
T(n) = aT(n/b) + f(n)
where a >= 1 and b > 1
There are the following three cases:
• If f(n) = O(nc) where c < Logba then T(n) =
Θ(nLogba)
• If f(n) = Θ(nc) where c = Logba then T(n) = Θ(ncLog
n)
• If f(n) = Ω(nc) where c > Logba then T(n) = Θ(f(n))
Amity School of Engineering & Technology
Working
• The master method is mainly derived from the
recurrence tree method.
• If we draw the recurrence tree of
T(n) = aT(n/b) + f(n),
• we can see that the work done at the root is f(n),
and work done at all leaves is Θ(nc)
where c is Logba. And the height of the
recurrence tree is Logbn
Amity School of Engineering & Technology
Amity School of Engineering & Technology
• Route 4: 1 → 3 → 4 → 2 → 1
– Distance: 15+12+8+5=40
• Route 5: 1 → 4 → 2 → 3 → 1
– Distance: 20+8+9+6=43
• Route 6: 1 → 4 → 3 → 2 → 1
– Distance: 20+9+13+5=47
Amity School of Engineering & Technology
Examples
Amity School of Engineering & Technology
Dynamic Programming
• Dynamic programming is an optimization technique,
which divides the problem into smaller sub-problems
and after solving each sub-problem, dynamic
programming combines all the solutions to get
ultimate solution.
• Unlike divide and conquer method, dynamic
programming reuses the solution to the sub-
problems many times.
• Application of Dynamic programming:
– Matrix Chain Multiplication
– Longest Common Subsequence
– Travelling Salesman Problem
– 0/1 Knapsack Problem
Amity School of Engineering & Technology
Randomized Algorithm
• An algorithm that uses random numbers to
decide what to do next anywhere in its
logic is called Randomized Algorithm.
• For example, in Randomized Quick Sort,
we use random number to pick the next
pivot (or we randomly shuffle the array).
• Example:
• Randomized Binary Search Algorithm
• Randomized Quick Sort Algorithm
Amity School of Engineering & Technology
Backtracking Algorithm
• Backtracking is an optimization technique to
solve combinational problems. It is applied to
both programmatic and real-life problems.
• In backtracking, we start with a possible solution,
which satisfies all the required conditions.
• Then we move to the next level and if that level
does not produce a satisfactory solution, we
return one level back and start with a new
option.
Amity School of Engineering & Technology
Example
Amity School of Engineering & Technology
Binary Search
• Binary search compares the target value to the
middle element of the array.
• If they are not equal, the half in which the target
cannot lie is eliminated and the search continues
on the remaining half, again taking the middle
element to compare to the target value, and
repeating this until the target value is found.
• If the search ends with the remaining half being
empty, the target is not in the array.
Amity School of Engineering & Technology
Quick Sort
• Quicksort is a sorting algorithm.
• The algorithm picks a pivot element,
rearranges the array elements in such a
way that all elements smaller than the
picked pivot element move to left side of
pivot, and all greater elements move to
right side.
• Finally, the algorithm recursively sorts the
subarrays on left and right of pivot
element.
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Time complexity Of Quick Sort
2
Worst-case performance O( n )
Merge Sort
• Merge Sort is a Divide and Conquer algorithm.
• It divides input array in two halves, calls itself for
the two halves and then merges the two sorted
halves.
• The merge() function is used for merging two
halves.
• The merge(arr, l, m, r) is key process that
assumes that arr[l..m] and arr[m+1..r] are sorted
and merges the two sorted sub-arrays into one.
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Time complexity
T(n)=2T(n/2) + O(n)
C11 = P + S - T+ V
C12 = R+ T
C21 = Q + S
C22 = P + R - Q + U
Amity School of Engineering & Technology
Answer
Amity School of Engineering & Technology
Answer
Amity School of Engineering & Technology
Greedy Approach
Greedy algorithms build a solution part by part,
choosing the next part in such a way, that it gives an
immediate benefit.
This approach never reconsiders the choices taken
previously.
This approach is mainly used to solve optimization
problems.
Greedy method is easy to implement and quite
efficient in most of the cases.
Hence, we can say that Greedy algorithm is an
algorithmic paradigm based on heuristic that follows
local optimal choice at each step with the hope of
finding global optimal solution.
Amity School of Engineering & Technology
Huffman Coding
• Huffman Coding is a famous Greedy Algorithm.
• It is used for the lossless compression of data.
• It uses variable length encoding.
• It assigns variable length code to all the
characters.
• The code length of a character depends on how
frequently it occurs in the given text.
• The character which occurs most frequently gets
the smallest code.
• The character which occurs least frequently gets
the largest code.
• It is also known as Huffman Encoding.
Amity School of Engineering & Technology
Prefix Rule
• Huffman Coding implements a rule known
as a prefix rule.
• This is to prevent the ambiguities while
decoding.
• It ensures that the code assigned to any
character is not a prefix of the code
assigned to any other character.
Amity School of Engineering & Technology
Step-03:
Considering the first two nodes having minimum
frequency,
• Create a new internal node.
• The frequency of this new node is the sum of
frequency of those two nodes.
• Make the first node as a left child and the other
node as a right child of the newly created node.
Step-04:
• Keep repeating Step-02 and Step-03 until all the
nodes form a single tree.
• The tree finally obtained is the desired Huffman
Tree.
Amity School of Engineering & Technology
Problem
A file contains the following characters with the frequencies as
shown. If Huffman Coding is used for data compression,
determine-
• Length of Huffman encoded message (in bits)
• Huffman Code for each character
• Average code length Characters Frequencies
a 10
e 15
i 12
o 3
u 4
s 13
t 1
Amity School of Engineering & Technology
Solution
• Step-01
• Step-02
Amity School of Engineering & Technology
• Step -03
Amity School of Engineering & Technology
• Step -04
Amity School of Engineering & Technology
• Step -05
Amity School of Engineering & Technology
• Step -06
Amity School of Engineering & Technology
• Step -07
Amity School of Engineering & Technology
• Step -08
Amity School of Engineering & Technology
• Step -09
Amity School of Engineering & Technology
• Step- 10
Amity School of Engineering & Technology
Now,
• We assign weight to all the edges of the
constructed Huffman Tree.
• Let us assign weight ‘0’ to the left edges and
weight ‘1’ to the right edges.
Amity School of Engineering & Technology
Rule
• If you assign weight ‘0’ to the left edges, then
assign weight ‘1’ to the right edges.
• If you assign weight ‘1’ to the left edges, then
assign weight ‘0’ to the right edges.
• Any of the above two conventions may be
followed.
• But follow the same convention at the time of
decoding that is adopted at the time of encoding.
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Problem-1
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Knapsack Problem
The problem states-
Which items should be placed into the knapsack
such that-
• The value or profit obtained by putting the items
into the knapsack is maximum.
• The weight limit of the knapsack does not exceed.
Knapsack problem has the following two variants-
Problem - 1
• For the given set of items and knapsack capacity
= 60 kg, find the optimal solution for the
fractional knapsack problem making use of
greedy approach.
Amity School of Engineering & Technology
Solution
Step-01:
Compute the value / weight ratio for each item-
Amity School of Engineering & Technology
Step-02:
Sort all the items in decreasing order of their value /
weight ratio-
I1 I2 I5 I4 I3
(6) (4) (3.6) (3.5) (3)
Step-03:
Start filling the knapsack by putting the items into
it one by one.
Amity School of Engineering & Technology
Now,
• Knapsack weight left to be filled is 20 kg but
item-4 has a weight of 22 kg.
• Since in fractional knapsack problem, even the
fraction of any item can be taken.
• So, knapsack will contain the following items-
• < I1 , I2 , I5 , (20/22) I4 >
Total cost of the knapsack
= 160 + (20/22) x 77
= 160 + 70
= 230 units
Amity School of Engineering & Technology
Problem-2
Consider 5 items along their respective
weights and values: -
I = (I1,I2,I3,I4,I5)
w = (5, 10, 20, 30, 40)
P = (30, 20, 100, 90,160)
The capacity of knapsack W = 60
Answer: 270
Amity School of Engineering & Technology
Prim’s Algorithm
• Prim’s Algorithm is a famous greedy
algorithm.
• It is used for finding the Minimum
Spanning Tree (MST) of a given
graph.
• To apply Prim’s algorithm, the given
graph must be weighted, connected
and undirected.
Amity School of Engineering & Technology
Step-01:
Randomly choose any vertex.
• The vertex connecting to the edge having least weight
is usually selected.
Step-02:
Find all the edges that connect the tree to new vertices.
• Find the least weight edge among those edges and
include it in the existing tree.
• If including that edge creates a cycle, then reject that
edge and look for the next least weight edge.
Step-03:
• Keep repeating step-02 until all the vertices are
included and Minimum Spanning Tree (MST) is
obtained.
Amity School of Engineering & Technology
Problem-01
Amity School of Engineering & Technology
Solution
Problem-02
Amity School of Engineering & Technology
Kruskal’s Algorithm
• Kruskal’s Algorithm is a famous greedy
algorithm.
• It is used for finding the Minimum
Spanning Tree (MST) of a given graph.
• To apply Kruskal’s algorithm, the given
graph must be weighted, connected and
undirected.
Amity School of Engineering & Technology
Problem
Amity School of Engineering & Technology
Solution
Amity School of Engineering & Technology
Dijkstra Algorithm
• Dijkstra Algorithm is a very famous
greedy algorithm.
• It is used for solving the single source
shortest path problem.
• It computes the shortest path from
one particular source node to all other
remaining nodes of the graph.
Amity School of Engineering & Technology
Conditions
It is important to note the following points regarding Dijkstra
Algorithm-
• Dijkstra algorithm works only for connected graphs.
• Dijkstra algorithm works only for those graphs that do not
contain any negative weight edge.
• The actual Dijkstra algorithm does not output the
shortest paths.
• It only provides the value or cost of the shortest paths.
• By making minor modifications in the actual algorithm,
the shortest paths can be easily obtained.
• Dijkstra algorithm works for directed as well as
undirected graphs.
Amity School of Engineering & Technology
Step-02:
For each vertex of the given graph,
two variables are defined as-
• Π[v] which denotes the predecessor
of vertex ‘v’
• d[v] which denotes the shortest path
estimate of vertex ‘v’ from the source
vertex.
Amity School of Engineering & Technology
Step-03:
Edge Relaxation
Now,
Problem
Amity School of Engineering & Technology
Solution
Step-01:
The following two sets are created-
• Unvisited set : {S , a , b , c , d , e}
• Visited set : { }
Step-02:
The two variables Π and d are created
for each vertex and initialized as-
• Π[S] = Π[a] = Π[b] = Π[c] = Π[d] = Π[e] = NIL
• d[S] = 0
• d[a] = d[b] = d[c] = d[d] = d[e] = ∞
Amity School of Engineering & Technology
Step-03:
Vertex ‘S’ is chosen.
• This is because shortest path
estimate for vertex ‘S’ is least.
• The outgoing edges of vertex ‘S’ are
relaxed.
Amity School of Engineering & Technology
• Unvisited set : {a , b , c , d , e}
• Visited set : {S}
Step-04:
Vertex ‘a’ is chosen.
• This is because shortest path estimate for
vertex ‘a’ is least.
• The outgoing edges of vertex ‘a’ are
relaxed.
Amity School of Engineering & Technology
Bellman-Ford algorithm
• The Bellman-Ford algorithm is a suitable
alternative to Dijkstra’s algorithm as it
accommodates negative edge weights.
• Step – 1 Initialize the distance to the source
vertex as 0, and the distance to all other vertices
as infinity.
• Step – 2 Relax all edges in the graph |V| – 1
times, where |V| is the number of vertices in the
graph. For each edge (u, v) with weight w, check
if the distance from the source vertex to v can be
reduced by going through u. If so, update the
distance to v to the new, shorter distance.
Amity School of Engineering & Technology
Negative cycles
What is the shortest path from a to e?
1
1 B D
5
A
10 -10
E
C
3
a. 7
b. 5
Amity School of Engineering & Technology
Bellman-Ford algorithm
10 How many edges is
S A
the shortest path
8 1
from s to:
-4 B
G A:
2
1 1
-2
F C
-1 3
E D
-1
Amity School of Engineering & Technology
Bellman-Ford algorithm
10 How many edges is
S A
the shortest path
8 1
from s to:
-4 B
G A: 3
2
1 1
-2
F C
-1 3
E D
-1
Amity School of Engineering & Technology
Bellman-Ford algorithm
10 How many edges is
S A
the shortest path
8 1
from s to:
-4 B
G A: 3
2
1 1
-2
C B:
F
-1 3
E D
-1
Amity School of Engineering & Technology
Bellman-Ford algorithm
10 How many edges is
S A
the shortest path
8 1
from s to:
-4 B
G A: 3
2
1 1
-2
C B: 5
F
-1 3
E D
-1
Amity School of Engineering & Technology
Bellman-Ford algorithm
10 How many edges is
S A
the shortest path
8 1
from s to:
-4 B
G A: 3
2
1 1
-2
C B: 5
F
-1 3
E D D:
-1
Amity School of Engineering & Technology
Bellman-Ford algorithm
10 How many edges is
S A
the shortest path
8 1
from s to:
-4 B
G A: 3
2
1 1
-2
C B: 5
F
-1 3
E D D: 7
-1
Amity School of Engineering & Technology
Bellman-Ford algorithm
0
10
S A Iteration: 0
8 1
G -4 B
2
1 1
-2
F C
-1 3
E D
-1
Amity School of Engineering & Technology
Bellman-Ford algorithm
0 10
10
S A Iteration: 1
8 1
8 -4 B
G
2
1 1
-2
F C
-1 3
E D
-1
Amity School of Engineering & Technology
Bellman-Ford algorithm
0 10
10
S A Iteration: 2
8 1
8 -4 B
G
2
1 1
-2
F C
9
-1 3
E D
-1
12
Amity School of Engineering & Technology
Bellman-Ford algorithm
0 5
10
S A Iteration: 3
8 1
10
-2
F C
9
-1 3
E D
-1
8
Amity School of Engineering & Technology
Bellman-Ford algorithm
0 5
10
S A Iteration: 4
8 1
6
8 -4 B
G
2
1 1
-2 11
F C
9
-1 3
E D
-1
7
Amity School of Engineering & Technology
Bellman-Ford algorithm
0 5
10
S A Iteration: 5
8 1
5
-2 7
F C
9
-1 3
E D
-1
7 14
Amity School of Engineering & Technology
Bellman-Ford algorithm
0 5
10
S A Iteration: 6
8 1
5
8 -4 B
G
2
1 1
-2 6
F C
9
-1 3
E D
-1
7 10
Amity School of Engineering & Technology
Bellman-Ford algorithm
0 5
10
S A Iteration: 7
8 1
5
-1 3
E D
-1
7 9
Amity School of Engineering & Technology
Convex Hull
• In computational geometry, a convex hull is the
smallest convex polygon that contains a given
set of points. It is a fundamental concept with
applications in various fields such as computer
graphics, robotics, and image processing.
Amity School of Engineering & Technology
Convexity
A set C R2 is convex if for all two points
p,qC the line segment pq is fully contained
in C.
convex non-convex
Amity School of Engineering & Technology
Convex Hull
The convex hull CH(P) of a point set P R2 is the
smallest convex set C P. In other words CH(P) =
C. CP
C convex
P
Amity School of Engineering & Technology
Convex Hull
Observation: CH(P) is the unique convex polygon whose
vertices are points of P and which contains all points of P.
We represent the convex hull as the sequence of
points on the convex hull polygon (the boundary of the
convex hull), in counter-clockwise order.
4
5 3
6
2
0 1
Amity School of Engineering & Technology
Recurrence (cont’d)
(1) if n = 1;
T(n) =
2T(n/2) + (n) if n > 1.
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
T(n)
Amity School of Engineering & Technology
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
T(n/2) T(n/2)
Amity School of Engineering & Technology
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/2 dn/2
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/2 dn/2
(1)
Amity School of Engineering & Technology
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn
dn/2 dn/2
h = log n dn/4 dn/4 dn/4 dn/4
(1)
Amity School of Engineering & Technology
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn dn
dn/2 dn/2
h = log n dn/4 dn/4 dn/4 dn/4
(1)
Amity School of Engineering & Technology
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn dn
dn/2 dn/2 dn
h = log n dn/4 dn/4 dn/4 dn/4
(1)
Amity School of Engineering & Technology
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn dn
dn/2 dn/2 dn
h = log n dn/4 dn/4 dn/4 dn/4 dn
…
(1)
Amity School of Engineering & Technology
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn dn
dn/2 dn/2 dn
h = log n dn/4 dn/4 dn/4 dn/4 dn
…
(1) #leaves = n (n)
Amity School of Engineering & Technology
Recursion tree
Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.
dn dn
dn/2 dn/2 dn
h = log n dn/4 dn/4 dn/4 dn/4 dn
…
(1) #leaves = n (n)
Total (n log n)
Amity School of Engineering & Technology
Job Sequencing With Deadlines
The sequencing of jobs on a single processor
with deadline constraints is called as Job
Sequencing with Deadlines.
Here-
• You are given a set of jobs.
• Each job has a defined deadline and some
profit associated with it.
• The profit of a job is given only when that job is
completed within its deadline.
• Only one processor is available for processing
all the jobs.
• Processor takes one unit of time to complete a
job.
Amity School of Engineering & Technology
Approach to Solution
• A feasible solution would be a subset of
jobs where each job of the subset gets
completed within its deadline.
• Value of the feasible solution would be the
sum of profit of all the jobs contained in
the subset.
• An optimal solution of the problem would
be a feasible solution which gives the
maximum profit.
Amity School of Engineering & Technology
Greedy Algorithm-
Step-01:
Sort all the given jobs in decreasing order of their
profit.
Step-02:
Check the value of maximum deadline.
• Draw a Gantt chart where maximum time on
Gantt chart is the value of maximum deadline.
Step-03:
• Pick up the jobs one by one.
• Put the job on Gantt chart as far as possible
from 0 ensuring that the job gets completed
before its deadline.
Amity School of Engineering & Technology
Problem
• Given the jobs, their deadlines and
associated profits as shown-
Jobs J1 J2 J3 J4 J5 J6
Deadlines 5 3 3 2 4 2
Solution
Step-01:
Sort all the given jobs in decreasing order of
their profit-
Jobs J4 J1 J3 J2 J5 J6
Deadlines 2 5 3 3 4 2
• Step-02:
• Value of maximum deadline = 5.
• So, draw a Gantt chart with maximum time on
Gantt chart = 5 units as shown-
Now,
• We take each job one by one in the order they
appear in Step-01.
• We place the job on Gantt chart as far as
possible from 0.
Amity School of Engineering & Technology
Step-03:
We take job J4. Since its deadline is 2, so we
place it in the first empty cell before deadline 2
as-
Step-04:
• We take job J1. Since its deadline is 5, so we
place it in the first empty cell before deadline
5 as-
Amity School of Engineering & Technology
Step-05:
• We take job J3.
• Since its deadline is 3, so we place it in
the first empty cell before deadline 3 as-
Amity School of Engineering & Technology
Step-06:
• We take job J2.
• Since its deadline is 3, so we place it in
the first empty cell before deadline 3.
• Since the second and third cells are
already filled, so we place job J2 in the
first cell as-
Amity School of Engineering & Technology
Step-07:
• Now, we take job J5.
• Since its deadline is 4, so we place it in
the first empty cell before deadline 4 as-
Now,
• The only job left is job J6 whose deadline is 2.
• All the slots before deadline 2 are already
occupied.
• Thus, job J6 can not be completed.
Amity School of Engineering & Technology
• Part-01:
• The optimal schedule is-
J2 , J4 , J3 , J5 , J1
• This is the required order in which the jobs
must be completed in order to obtain the
maximum profit.
Part-02:
All the jobs are not completed in optimal
schedule.
• This is because job J6 could not be
completed within its deadline.
Amity School of Engineering & Technology
Part-03:
Maximum earned profit
= Sum of profit of all the jobs in optimal
schedule
= Profit of job J2 + Profit of job J4 + Profit of
job J3 + Profit of job J5 + Profit of job J1
= 180 + 300 + 190 + 120 + 200
= 990 units
Amity School of Engineering & Technology
Problem-1
Job J1 J2 J3 J4 J5
Deadline 2 1 3 2 1
Profit 60 100 20 40 20
Amity School of Engineering & Technology
Solution
• The solution is the sequence of jobs (J2,
J1, J3), which are being executed within
their deadline and gives maximum profit.
Problem-2
• JobID Deadline Profit
a 4 20
b 1 10
c 1 40
d 1 30
Answer: c, a
Amity School of Engineering & Technology
Problem-3
JobID Deadline Profit
a 2 100
b 1 19
c 2 27
d 1 25
e 3 15
Answer: c, a, e
Amity School of Engineering & Technology
Algorithm
FLOYD - WARSHALL (W)
n ← rows [W].
D0 ← W
for k ← 1 to n
do for i ← 1 to n
do for j ← 1 to n
do dij(k) ← min (dij(k-1),dik(k-1)+dkj(k-1) )
return D(n)
Amity School of Engineering & Technology
Problem
Step-01:
• Remove all the self loops and parallel edges (keeping the
lowest weight edge) from the graph.
• In the given graph, there are neither self edges nor
parallel edges.
Step-02:
• Write the initial distance matrix.
• It represents the distance between every pair of vertices
in the form of given weights.
• For diagonal elements (representing self-loops), distance
value = 0.
• For vertices having a direct edge between them, distance
value = weight of that edge.
• For vertices having no direct edge between them,
distance value = ∞.
Amity School of Engineering & Technology
Step-03:
Using Floyd Warshall Algorithm, write the following 4
matrices-
Amity School of Engineering & Technology
Problem
Amity School of Engineering & Technology
Problem
Amity School of Engineering & Technology
Problem
Amity School of Engineering & Technology
Problem
Amity School of Engineering & Technology
Step-02:
• Start filling the table row wise top to bottom
from left to right.
• Use the following formula-
Step-03:
To identify the items that must be put into the
knapsack to obtain that maximum profit,
• Consider the last column of the table.
• Start scanning the entries from bottom to top.
• On encountering an entry whose value is not
same as the value stored in the entry
immediately above it, mark the row label of
that entry.
• After all the entries are scanned, the marked
labels represent the items that must be put
into the knapsack.
Amity School of Engineering & Technology
Problem-2
Find the optimal solution for the 0/1 knapsack
problem making use of dynamic programming
approach. Consider-
n=4
C= 5 kg
(w1, w2, w3, w4) = (2, 3, 4, 5)
(b1, b2, b3, b4) = (3, 4, 5, 6)
Amity School of Engineering & Technology
https://www.gatevidyalay.com/0-1-knapsack-problem-using-dynamic-
programming-approach/
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Problem-3
Find the optimal solution for the 0/1
knapsack problem making use of dynamic
programming approach. Consider-
n=5
C= 11 kg
(w1, w2, w3, w4,w5) = (1,2,5,6,7)
(p1,p2,p3,p4,p5) = (1,6,18,22,28)
Amity School of Engineering & Technology
Answer
• Max profit-40
• (0,0,1,1,0)
Amity School of Engineering & Technology
Backtracking
• Backtracking is a problem-solving algorithmic technique
that involves finding a solution incrementally by
trying different options and undoing them if they lead
to a dead end.
• It is commonly used in situations where you need to
explore multiple possibilities to solve a problem, like
searching for a path in a maze or solving puzzles like
Sudoku.
• Problems associated with backtracking can be
categorized into 3 categories:
– Decision Problems: Here, we search for a feasible solution.
– Optimization Problems: For this type, we search for the best
solution.
– Enumeration Problems: We find set of all possible feasible
solutions to the problems of this type.
Amity School of Engineering & Technology
N-Queens Problem
• N - Queens problem is to place n - queens
in such a manner on an n x n chessboard
that no queens attack each other by being
in the same row, column or diagonal.
• It can be seen that for n =1, the problem
has a trivial solution, and no solution exists
for n =2 and n =3. So first we will consider
the 4 queens problem and then generate it
to n - queens problem.
Amity School of Engineering & Technology
Sum of Subsets
• You will be given a set of non-negative integers
and a value of variable sum, and you must
determine if there is a subset of the given set
with a sum equal to a given sum.
• For the recursion based approach, you will look
at two scenarios:
– Start with the last element and work your way up to
the goal sum by subtracting the value of the 'last'
element from the total number of elements.
– 'Final' element will be removed, and the required sum
will equal the target sum, with the number of elements
equal to the total elements minus 1.
Amity School of Engineering & Technology
Amity School of Engineering & Technology
Graph Coloring
• Graph Coloring is the process of assigning colors to
the vertices of a graph in such a way that no two
adjacent vertices have the same color, while
minimizing the total number of colors used.
• This is also called the vertex coloring problem. If
coloring is done using at most m colors, it is called
m-coloring.
Amity School of Engineering & Technology
Chromatic Number
• The minimum number of colors needed to color a
graph is called its chromatic number. For example,
the following can be colored a minimum of 2 colors.
Amity School of Engineering & Technology
Thanks
Further Study:
1. E. Horowitz & S Sahni, "Fundamentals of Computer
Algorithms",
2. Thomas H. Coreman, Charles E. Leiserson and Ronald
L. Rivest, “Introduction to Algorithms”, Printice Hall of
India.
3. www.geeksforgeeks.org
4. https://Javatpoint.com
5. https://www.gatevidyalay.com/
6. https://www.tutorialspoint.com/