Professional Documents
Culture Documents
https://www.gatevidyalay.com/travelling-salesman-problem-using-branch-an
d-bound-approach/
Disadvantages
1. The branch and bound algorithm take time. The number of nodes in the tree may be
too large in the worst case, depending on the size of the given problem.
2. Also, parallelisation in the branch and bound algorithm is complicated.
Job sequencing with deadline, 0/1 knapsack problem, Minimum cost spanning
trees, Single source shortest path problem.
Fractional Knapsack Problem Statement
Given a set of N items each having value V with weight W and the total capacity of
a knapsack. The task is to find the maximal value of fractions of items that can fit
into the knapsack.
Examples:
Input: A[] = {{60, 20} , {100, 50}, {120, 30}}, Total_capacity = 50
Output: 180.00
Explanation: Take the first item and the third item. Total value = 60 + 120 = 180 with
a total capacity of 20 + 30 = 50
The most basic approach is to try all possible subsets and possible fractions of
the given set and find the maximum value among all such fractions.
The time complexity will be exponential, as you need to find all possible
combinations of the given set.
Efficient Approach(Greedy)
The Fractional Knapsack problem can be solved efficiently using the greedy
algorithm, where you need to sort the items according to their value/weight ratio.
https://www.gatevidyalay.com/fractional-knapsack-problem-using-greedy-ap
proach/
https://www.javatpoint.com/divide-and-conquer-introduction
https://www.geeksforgeeks.org/difference-between-algorith
m-pseudocode-and-program/
2. Explain with example about Merge Sort.
https://www.geeksforgeeks.org/merge-sort/
Advantages of Merge Sort
● Works well for larger lists
● Has a consistent running time
● Preserves the order of equal elements
● Handles slow-to-access sequential data efficiently
Answer: An in-place algorithm processes the input and produces the output in the same
memory location that contains the input without using any auxiliary space. However, a small
extra space for variables is allowed.
And so, merge sort is not an in-place sorting algorithm, because we use an auxiliary array to
hold the merged list temporarily.
https://www.javatpoint.com/kruskal-algorithm
Time Complexity : O(E logE) or O(V logV), where E is the no. of edges, and V is the no. of
vertices.
Examples:
Input: A[] = {{60, 20} , {100, 50}, {120, 30}}, Total_capacity = 50
Output: 180.00
Explanation: Take the first item and the third item. Total value = 60 + 120 = 180 with
a total capacity of 20 + 30 = 50
The most basic approach is to try all possible subsets and possible fractions of
the given set and find the maximum value among all such fractions.
The time complexity will be exponential, as you need to find all possible
combinations of the given set.
The Fractional Knapsack problem can be solved efficiently using the greedy
algorithm, where you need to sort the items according to their value/weight ratio.
https://www.gatevidyalay.com/tag/travelling-salesman-probl
em-using-greedy-approach/
7. Briefly explain Job Sequencing with Deadline using
Greedy method with an example.
https://www.gatevidyalay.com/job-sequencing-with-deadlin
es/
SEMESTER SUGGESTION SOLUTIONS :
https://www.tutorialspoint.com/design_and_analysis_of_alg
orithms/design_and_analysis_of_algorithms_floyd_warshal
l_algorithm.htm
https://www.gatevidyalay.com/floyd-warshall-algorithm-sho
rtest-path-algorithm/
2) Recurrence Equations.
https://www.geeksforgeeks.org/how-to-analyse-complexity-
of-recurrence-relation/
https://www.geeksforgeeks.org/difference-between-0-1-kna
psack-problem-and-fractional-knapsack-problem/
https://byjus.com/gate/difference-between-np-hard-and
-np-complete-problem/
https://www.tutorialspoint.com/design_and_analysis_of
_algorithms/design_and_analysis_of_algorithms_np_h
ard_complete_classes.htm
NP-Complete:
● Definition: A problem is NP-Complete (Non-deterministic Polynomial
Complete) if it belongs to the class of NP problems (problems for which
a given solution can be verified quickly) and any problem in NP can be
polynomial-time reduced to it.
● Characteristics:
● NP-Complete problems are both in NP and as hard as the hardest
problems in NP.
● If a polynomial-time algorithm exists for any NP-Complete
problem, then a polynomial-time algorithm exists for all problems
in NP. However, such an algorithm has not been discovered yet.
● Example:
● The traveling salesman problem is a classic NP-Complete
problem. Given a list of cities and the distances between each pair
of cities, the task is to find the shortest possible route that visits
each city exactly once and returns to the original city.
NP-Hard:
Key Difference:
● All problems that are NP-Complete are also NP-Hard, but not all
NP-Hard problems are necessarily NP-Complete. NP-Complete
problems have the additional property of being in NP, meaning their
solutions can be verified in polynomial time.
https://medium.com/ivymobility-developers/algorithm-a168
afcd3611
https://www.gatevidyalay.com/tag/travelling-salesman-probl
em-using-greedy-approach/
https://www.gatevidyalay.com/travelling-salesman-problem-
using-branch-and-bound-approach/
Answer: An in-place algorithm processes the input and produces the output in the same
memory location that contains the input without using any auxiliary space. However, a small
extra space for variables is allowed.
And so, merge sort is not an in-place sorting algorithm, because we use an auxiliary array to
hold the merged list temporarily.
https://www.geeksforgeeks.org/quick-sort/
a) Complexity
b) Correctness //(correctness and efficiency.)
c) Simplicity
d) Optimization
What is the time complexity of the simple algorithm for finding the
maximum and minimum elements in an array of size n?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(log n)
a) Bellman-Ford algorithm
b) Floyd-Warshall algorithm
c) Depth First Search
d) Kruskal’s algorithm
a) NP
b) P
c) Hard
d) Complete
a) Greedy algorithm
b) Depth First Search
c) Breadth First Search
d) Dijkstra’s shortest path algorithm
a) Quick Sort
b) Dijkstra’s shortest path algorithm
c) Bellman-Ford algorithm
d) Kruskal’s algorithm for minimum spanning tree
Which of the following data structures is best suited for
implementing a recursive algorithm?
a) Array
b) Linked list
c) Stack
d) Queue
a) 1
b) 2
c) 3
d) 4
5 MARKS
https://www.programiz.com/dsa/divide-and-conquer
https://www.tutorialspoint.com/design_and_analysis_of_alg
orithms/design_and_analysis_of_algorithms_floyd_warshal
l_algorithm.htm
https://www.gatevidyalay.com/floyd-warshall-algorithm-sho
rtest-path-algorithm/
Some Advantages and Disadvantages of Floyd-Warshall
Algorithm:
1. It can discover the shortest direction between all pairs of vertices in a weighted graph.
7. It can be less intuitive than different algorithms, which include Dijkstra's algorithm, or the
Bellman-Ford set of rules, making it more difficult to understand for some builders.
2. Airline Networks: The Floyd-Warshall set of rules can also be utilized in airline networks
to locate the shortest path between two cities with the lowest cost. It can assist airways
plan their routes and limit fuel charges.
3. Traffic Networks: The algorithm is used to find the shortest path between points in a
visitors' network. It can help reduce congestion and improve the go with the flow of
visitors in city areas.
https://www.interviewbit.com/blog/difference-between-divid
e-and-conquer-and-dynamic-programming/
https://www.tutorialspoint.com/design_and_analysis_of
_algorithms/design_and_analysis_of_algorithms_dynamic_
programming.htm
Describe Job Sequencing with Deadline with example.
https://www.gatevidyalay.com/job-sequencing-with-deadlin
es/
Explain Hamiltonian Circuit problems.
https://www.javatpoint.com/hamiltonian-circuit-problems
Define Backtracking.
https://www.programiz.com/dsa/backtracking-algorithm
https://www.geeksforgeeks.org/quick-sort-vs-merge-sort/
Advantages of Merge Sort
● Works well for larger lists
● Has a consistent running time
● Preserves the order of equal elements
● Handles slow-to-access sequential data efficiently
In computer science, there exist some problems whose solutions are not
yet found, the problems are divided into classes known as Complexity
Classes. In complexity theory, a Complexity Class is a set of problems
with related complexity. These classes help scientists to group problems
based on how much time and space they require to solve problems and
verify the solutions. It is the branch of the theory of computation that deals
with the resources required to solve a problem.
The common resources are time and space, meaning how much time
the algorithm takes to solve a problem and the corresponding memory
usage.
● The time complexity of an algorithm is used to describe the number
of steps required to solve a problem, but it can also be used to describe
how long it takes to verify the answer.
● The space complexity of an algorithm describes how much memory
is required for the algorithm to operate.
2. NP Class
3. CoNP Class
4. NP-hard
5. NP-complete
P Class
3. Merge Sort
NP Class
The NP in NP class stands for Non-deterministic Polynomial Time. It
is the collection of decision problems that can be solved by a
non-deterministic machine in polynomial time.
Features:
● The solutions of the NP class are hard to find since they are
being solved by a non-deterministic machine but the solutions are
easy to verify.
● Problems of NP can be verified by a Turing machine in
polynomial time.
Example:
Let us consider an example to better understand the NP class.
Suppose there is a company having a total of 1000 employees having
unique employee IDs.
Assume that there are 200 rooms available for them. A selection
of 200 employees must be paired together, but the CEO of the company
has the data of some employees who can’t work in the same room due
to personal reasons.
This is an example of an NP problem. Since it is easy to check if the
given choice of 200 employees proposed by a coworker is satisfactory
or not i.e. no pair taken from the coworker list appears on the list given
by the CEO. But generating such a list from scratch seems to be so
hard as to be completely impractical.
It indicates that if someone can provide us with the solution to the
problem, we can find the correct and incorrect pair in polynomial
time. Thus for the NP class problem, the answer is possible, which
can be calculated in polynomial time.
This class contains many problems that one would like to be
able to solve effectively:
1. Boolean Satisfiability Problem (SAT).
3. Graph coloring.
Co-NP Class
Co-NP stands for the complement of NP Class. It means if the
answer to a problem in Co-NP is No, then there is proof that can be
checked in polynomial time.
Features:
NP-hard class
An NP-hard problem is at least as hard as the hardest problem in NP
and it is a class of problems such that every problem in NP reduces to
NP-hard.
Features:
1. Halting problem.
3. No Hamiltonian cycle.
NP-complete class
A problem is NP-complete if it is both NP and NP-hard. NP-complete
problems are the hard problems in NP.
Features:
2. If one could solve an NP-complete problem in polynomial time, then one could also
solve any NP problem in polynomial time.
1. Hamiltonian Cycle.
2. Satisfiability.
3. Vertex cover.
Complexity Class
Characteristic feature
NP Problem:
The NP problems set of problems whose solutions are hard to find but
easy to verify and are solved by Non-Deterministic Machine in polynomial
time.
NP-Hard Problem:
A Problem X is NP-Hard if there is an NP-Complete problem Y, such that Y
is reducible to X in polynomial time. NP-Hard problems are as hard as
NP-Complete problems. NP-Hard Problem need not be in NP class.
example :
1. Hamiltonian cycle .
2. optimization problem .
3. Shortest path
NP-Complete Problem:
A problem X is NP-Complete if there is an NP problem Y, such that Y is
reducible to X in polynomial time. NP-Complete problems are as hard as
NP problems. A problem is NP-Complete if it is a part of both NP and
NP-Hard Problem. A non-deterministic Turing machine can solve
NP-Complete problem in polynomial time.
time.
Example:
1. Decision problems.
2. Regular graphs.
What is M-coloring problem in Graph theory?
M coloring decision problem - if a graph is given and some
colors are given and we want to know if a graph can be colored or not.
Given an undirected graph and a number m, the task is to color the given graph with
at most m colors such that no two adjacent vertices of the graph are colored with the
same color
Note: Here coloring of a graph means the assignment of colors to all vertices Below is
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}
Input: graph =
{1, 1, 1,1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}
Auxiliary Space: O(V). The Recursive Stack of graph coloring(…) function will require
O(V) space.
● Create a recursive function that takes the graph, current index, number
of vertices, and color array.
● If the current index is equal to the number of vertices. Print the color
configuration in the color array.
● For every assigned color, check if the configuration is safe, (i.e. check if the
adjacent vertices do not have the same color) and recursively call the function with
the next index and number of vertices otherwise, return false
● To color the first node there are 3 choices of colors Red, Green and Blue, so
lets take the red color for first node.
● After Red color for first node is fixed then we have made choice for second
node in similar manner as we did for first node, then for 3rd node and so on.
● There is one important point to remember. while choosing color for the node, it
should not be same as the color of the adjacent node.
● Let’s choose Green color for the first node and explore the
options for the remaining nodes.
● As shown in the above diagram, all the solutions are
shown by coloring the first node in Green.
● Let’s choose Blue color for the first node and explore the
options for the remaining nodes.
6) Briefly explain about 8-Queen Problems with proper diagrams.
https://iq.opengenus.org/8-queens-problem-backtracking/
https://www.prepbytes.com/blog/miscellaneous/asymptotic-no
tation-types-and-uses/
7) Subset Problem.
https://www.javatpoint.com/subset-sum-problems
https://www.geeksforgeeks.org/difference-between-0-1-kna
psack-problem-and-fractional-knapsack-problem/
https://www.gatevidyalay.com/fractional-knapsack-problem-
using-greedy-approach/
10) Write down the Traveling Salesman Problem using
the Greedy method.
https://medium.com/ivymobility-developers/algorithm-a168
afcd3611
https://www.gatevidyalay.com/tag/travelling-salesman-probl
em-using-greedy-approach/