You are on page 1of 63

1) Traveling Salesman Problem using Branch and Bound.

https://www.gatevidyalay.com/travelling-salesman-problem-using-branch-an
d-bound-approach/

Branch and Bound


https://iq.opengenus.org/branch-and-bound/
Advantages
1. We do not explore all of the nodes in the tree in a branch and bound algorithm. As a
result, the branch and the bound algorithm have a lower time complexity when compared
to other algorithms.
2. If the problem is not too large and we can do the branching in a reasonable amount of
time, it will find an optimal solution.
3. The branch and the bound algorithm find the shortest path to the best solution for a
given problem. While exploring the tree, it does not repeat nodes.

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.

2) Knapsack problem(fractional) using Greedy method.

Application of Greedy Method:

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

Input: A[] = {{500, 30}}, Total_capacity = 10


Output: 166.67
Explanation: Since the total capacity of the knapsack is 10, consider one-third of
the item.

Brute Force Approach

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/

Frequently Asked Questions

How do you solve fractional knapsack?


Fractional knapsack can be solved using greedy algorithms.
What is the time complexity of fractional knapsack problems?
The time complexity of the fractional knapsack problem is O(NlogN).

Can we solve fractional knapsack using dynamic programming?


Yes, fractional knapsack can be solved using dynamic programming, but it will not be
efficient as it will take memory.

Which approach is the best in knapsack problems?


For 0/1 knapsack, the dynamic programming approach is the best, while for fractional
knapsack, the greedy approach is optimal.

0/1 Knapsack problem using Dynamic Programming


In the above approach we can observe that we are calling recursion for same sub
problems again and again thus resulting in overlapping subproblems thus we can make
use of Dynamic programming to solve 0-1 Knapsack problem.
https://www.gatevidyalay.com/0-1-knapsack-problem-using-dynamic-progra
mming-approach/
3) Divide and Conquer Algorithm
https://www.geeksforgeeks.org/introduction-to-divide-and-conquer-algorithm-data-
structure-and-algorithm-tutorials/

https://www.javatpoint.com/divide-and-conquer-introduction

MCA SLOT SUGGESTION

1. Write down the difference between Algorithms and


Pseudo code.

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

Disadvantages of Merge Sort


● Is slower for smaller lists compared to other sorting algorithms
● Takes up more space
● Requires additional memory for sorting, apart from the given array

Question 1: Is merge sort an in-place sorting algorithm?

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.

3. Write down the components of Greedy Algorithm. What


are the area of Greedy Application.
https://www.geeksforgeeks.org/introduction-to-greedy-algo
rithm-data-structures-and-algorithm-tutorials/

A greedy algorithm is made up of five components:

Selection function: Chooses the best candidate to be added to


the solution.
Feasibility function: Determines if a candidate can be used to
contribute to the solution.
Objective function: Assigns a value to a solution or a partial
solution.
Candidate set: From which a solution is created.
Feasible solution: A subset of given inputs that satisfies all
specified constraints of a problem.
Greedy algorithms may not produce the best result for all
problems. This is because they always go for the local best
choice to produce the global best result.
Other components of a greedy algorithm include:
Optimal solution: The feasible solution that achieves the
desired extremum.

Applications of Greedy Method


CPU Scheduling algorithms.
Minimum spanning trees.
Dijkstra shortest path algorithm.
Fit algorithm in memory management.
Travelling salesman problem.
Fractional knapsack problem.
Egyptian fraction.
Bin packing problem.

4. Write Kruskal’s minimal spanning tree algorithm with a


proper example.

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.

5. Explain Fractional Knapsack problem using Greedy


method with an example.
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

Input: A[] = {{500, 30}}, Total_capacity = 10


Output: 166.67
Explanation: Since the total capacity of the knapsack is 10, consider one-third of
the item.

Brute Force Approach

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.

Time Complexity: O(2N) Auxiliary Space: O(N)


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.

Time Complexity: O(N * logN)


Auxiliary Space: O(N)
https://www.gatevidyalay.com/fractional-knapsack-problem-
using-greedy-approach/
6. 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/
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 :

1) Explain Floyd-Warshall Algorithm with Diagram and


Example.

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/

3) Algorithm and Pseudocode Difference.


https://www.geeksforgeeks.org/difference-between-algorith
m-pseudocode-and-program/
4) Fractional and 0-1 Knapsack problems and Difference.

https://www.geeksforgeeks.org/difference-between-0-1-kna
psack-problem-and-fractional-knapsack-problem/

5) N-P Complete and N-P Hard short notes.

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:

● Definition: A problem is NP-Hard (Non-deterministic Polynomial Hard) if


it is at least as hard as the hardest problems in NP. Unlike NP-Complete
problems, NP-Hard problems do not necessarily belong to the class NP
and may not have polynomial-time verifiable solutions.
● Characteristics:
● NP-Hard problems are, in a sense, as hard as or harder than
NP-Complete problems.
● Solutions to NP-Hard problems, if they exist, cannot be verified in
polynomial time.
● Example:
● The Halting Problem is an example of an NP-Hard problem. It is a
decision problem that involves determining, given a description of
an arbitrary computer program and an input, whether the program
will eventually halt or continue running indefinitely.

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.

In summary, NP-Complete and NP-Hard are classifications used in

computational complexity theory to describe the difficulty of problems.

NP-Complete problems are a subset of NP-Hard problems and have the

additional property of being in NP, making them particularly challenging. The

study of these classes helps in understanding the inherent complexity of

problem-solving algorithms and the limits of efficient computation.

6) Travelling Salesman Problem.(The Sum from Class


Notes)

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/

7) Explain Complexity of BIG-O, Theta and Omega.


Class notes

8) Divide and Conquer Sums.


Class notes
8. Merge sort, Quick sort and Selection sort algorithms and
examples.
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

Disadvantages of Merge Sort


● Is slower for smaller lists compared to other sorting algorithms
● Takes up more space
● Requires additional memory for sorting, apart from the given array

Question 1: Is merge sort an in-place sorting algorithm?

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/

9. Prim's,Kruskal's and Djikstra's method sums and their


difference with diagrams.

10. Job Sequencing with Deadline sums.


https://www.gatevidyalay.com/job-sequencing-with-deadlin
es/
11. Optimal Merge Problem.

12. Definition of Dynamic programming.

13. Difference between Single Source Shortest Path, Non


Negative Weighted and Arbitrary Weighted Graphs.

14. Briefly explain about Multistage Graph.


15. Definition of Backtracking.

16. 8 Queens Problem.

17. Subset Problem.

18. Hamiltonian Circuit and M Coloring Problem.

19.N-P Complete and N-P Hard short notes.


FINAL QUESTION SET MCA :

What is the primary goal of algorithm design?

a) Complexity
b) Correctness //(correctness and efficiency.)
c) Simplicity
d) Optimization

In the Fractional Knapsack algorithm, what is the Greedy Choice


made at each step?

a) Select the item with the lowest weight


b) Select the item with the highest weight
c) Select the item with the lowest value-to-weight ratio
d) Select the item with the highest value-to-weight ratio

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)

Which of the following algorithms is used to find the shortest path


between all pairs of vertices in a graph?

a) Breadth First Search


b) Depth First Search
c) Dijkstra’s shortest path algorithm
d) Floyd-Warshall algorithm
Which of the following algorithms is used to find the articulation
points in a graph?

a) Bellman-Ford algorithm
b) Floyd-Warshall algorithm
c) Depth First Search
d) Kruskal’s algorithm

___ is the class of decision problems that can be solved by


non-deterministic polynomial algorithms.

a) NP
b) P
c) Hard
d) Complete

Which of the following algorithms is used to find the minimum


number of coins needed to make change for a given amount?

a) Greedy algorithm
b) Depth First Search
c) Breadth First Search
d) Dijkstra’s shortest path algorithm

Which of the following algorithms is an example of a greedy


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

In how many directions do queens attack each other?

a) 1
b) 2
c) 3
d) 4

5 MARKS

1) Explain the basic principle of the Divide and Conquer


algorithmic paradigm. How does it work, and why is it a
powerful problem-solving technique?

https://www.programiz.com/dsa/divide-and-conquer

2) Describe the working of Floyd-Warshall algorithm with proper


example.

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:

Advantages of the Floyd-Warshall algorithm include:

1. It can discover the shortest direction between all pairs of vertices in a weighted graph.

2. It is an easy and smooth-to-put algorithm, making it accessible to developers of all skill


ranges.

3. It is appropriate for both dense and sparse graphs.

4. It can be used to discover negative weight cycles in a graph.

5. Easy to understand and implement.

6. Can handle large numbers of nodes.


Disadvantages of the Floyd-Warshall set of rules include:

1. High time complexity.

2. Large memory requirement.

3. Can be slow on large graphs.

4. Doesn't always find the shortest path.

5. Not effective for dynamic graphs.

6. Only provides data on shortest distances.

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.

Applications of Floyd Warshall Algorithm:


The Floyd-Warshall algorithm is a dynamic programming algorithm used for finding the shortest
course among all pairs of vertices in a weighted graph. Here are some of the programs of the
Floyd-Warshall algorithm:

1. Routing Algorithms: The Floyd-Warshall algorithm is broadly utilized in routing


algorithms, along with within the OSPF (Open Shortest Path First) protocol utilized in
Internet routing. It can help decide the shortest route among two nodes in a network and
is useful in locating the least congested path.

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.

4. Computer Networks: The Floyd-Warshall algorithm is likewise utilized in laptop networks


to decide the shortest course between hosts in a network. It can assist in minimizing
community latency and improve community overall performance.
5. Game Development: The set of rules may be used in game development to find the
shortest direction among two items in a sport world. It is beneficial in games in which the
participant desires to navigate through complex surroundings, together with a maze or a
metropolis.

3) Write Down the difference between Algorithm and


Pseudocode.
https://www.geeksforgeeks.org/difference-between-algorith
m-pseudocode-and-program/
15 MARKS

4) Differentiate between divide-and-conquer and dynamic


programming.

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

5) Compare and contrast the Merge Sort and Quick Sort


algorithms.

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

Disadvantages of Merge Sort


● Is slower for smaller lists compared to other sorting algorithms
● Takes up more space
● Requires additional memory for sorting, apart from the given array
QUICK SORT

Advantages of Quick Sort?

● It is an in-place algorithm since it just requires a modest auxiliary stack.


● Sorting n objects takes only n (log n) time.
● Its inner loop is relatively short.
● After a thorough mathematical investigation of this algorithm, you can make a
reasonably specific statement about performance issues.

Disadvantages of Quick Sort.


● It is a recursive process. The implementation is quite tricky, mainly if recursion is not
provided.
● In the worst-case scenario, it takes quadratic (i.e., n2) time.
● It is fragile in the sense that a slight error in implementation can go unreported and
cause it to function poorly.

Define P, NP, NP Hard and NP Complete.

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.

Complexity classes are useful in organizing similar types of problems.

Types of Complexity Classes

This article discusses the following complexity classes:


1. P Class

2. NP Class
3. CoNP Class

4. NP-hard

5. NP-complete

P Class

The P in the P class stands for Polynomial Time. It is the collection of


decision problems(problems with a “yes” or “no” answer) that can be
solved by a deterministic machine in polynomial time.
Features:

● The solution to P problems is easy to find.

● P is often a class of computational problems that are solvable and


tractable. Tractable means that the problems can be solved in theory as
well as in practice. But the problems that can be solved in theory but not in
practice are known as intractable.

This class contains many problems:


1. Calculating the greatest common divisor.

2. Finding a maximum matching.

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).

2. Hamiltonian Path Problem.

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:

● If a problem X is in NP, then its complement X’ is also in CoNP.

● For an NP and CoNP problem, there is no need to verify all the


answers at once in polynomial time, there is a need to verify only one
particular answer “yes” or “no” in polynomial time for a problem to be in
NP or CoNP.
Some example problems for CoNP are:
1. To check prime number.
2. Integer Factorization.

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. All NP-hard problems are not in NP.


2. It takes a long time to check them. This means if a solution for an NP-hard
problem is given then it takes a long time to check whether it is right or not.
3. A problem A is in NP-hard if, for every problem L in NP, there exists a
polynomial-time reduction from L to A.

Some of the examples of problems in Np-hard are:

1. Halting problem.

2. Ǫualified Boolean formulas.

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:

1. NP-complete problems are special as any problem in NP class can be transformed or


reduced into NP-complete problems in polynomial time.

2. If one could solve an NP-complete problem in polynomial time, then one could also
solve any NP problem in polynomial time.

Some example problems include:

1. Hamiltonian Cycle.

2. Satisfiability.
3. Vertex cover.

Complexity Class
Characteristic feature

P Easily solvable in polynomial time.

NP Yes, answers can be checked in polynomial time.

Co-NP No, answers can be checked in polynomial time.

All NP-hard problems are not in NP and it takes a long


NP-hard
time to check them.

NP-complete A problem that is NP and NP-hard is NP-complete.

Difference between NP hard and NP complete problem

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.

If every problem of NP can be polynomial time reduced to it called as


NP Hard.

A lot of times takes the particular problem solve and reducing


different problems.

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.

A problem is np-complete when it is both np and np hard combines

together. this means np complete problems can be verified in polynomial

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.

M coloring optimization problem - if a graph is given and some


colors are given and we want to know Minimum how many colors are
required for that graph.

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

an example of a graph that can be colored with 3 different colors:


Examples:

Input: graph = {0, 1, 1, 1},

{1, 0, 1, 0},

{1, 1, 0, 1},

{1, 0, 1, 0}

Output: Solution Exists: Following are the assigned colors: 1 2 3 2

Explanation: By coloring the vertices


with following colors,
adjacent vertices does
not have same colors

Input: graph =

{1, 1, 1,1},

{1, 1, 1, 1},
{1, 1, 1, 1},

{1, 1, 1, 1}

Output: Solution does not exist

Explanation: No solution exits

Naive Approach for M-Coloring Problem:


Generate all possible configurations of colors. Since each node can be colored using any
of the m available colors, the total number of color configurations possible is mV. After
generating a configuration of color, check if the adjacent vertices have the same color or
not. If the conditions are met, print the combination.

Time Complexity: O(mV). There is a total O(mV) combination of colors

Auxiliary Space: O(V). The Recursive Stack of graph coloring(…) function will require
O(V) space.

M-Coloring Problem using Backtracking:


Assign colors one by one to different vertices, starting from vertex 0. Before assigning a
color, check for safety by considering already assigned colors to the adjacent vertices i.e
check if the adjacent vertices have the same color or not. If there is any color assignment
that does not violate the conditions, mark the color assignment as part of the solution. If
no assignment of color is possible then backtrack and return false

Follow the given steps to solve the problem:

● 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.

● Assign a color to a vertex from the range (1 to m).

● 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

● If any recursive function returns true then return true

● If no recursive function returns true then return false


Illustration:

● To color the graph, color each node one by one.

● 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.

● As shown in the above diagram, all the solutions


are shown by coloring the first node in Red.

● 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/

Explain Big Oh, Omega and Theta Notation with proper


diagram.

https://www.prepbytes.com/blog/miscellaneous/asymptotic-no
tation-types-and-uses/
7) Subset Problem.
https://www.javatpoint.com/subset-sum-problems

8) Write down the components of Greedy Algorithm. What


are the area of Greedy Application.

A greedy algorithm is made up of five components:

Selection function: Chooses the best candidate to be added to the


solution.
Feasibility function: Determines if a candidate can be used to
contribute to the solution.
Objective function: Assigns a value to a solution or a partial solution.
Candidate set: From which a solution is created.
Feasible solution: A subset of given inputs that satisfies all specified
constraints of a problem.
Greedy algorithms may not produce the best result for all problems. This
is because they always go for the local best choice to produce the
global best result.

Other components of a greedy algorithm include:


Optimal solution: The feasible solution that achieves the desired
extremum.

Applications of Greedy Method


CPU Scheduling algorithms.
Minimum spanning trees.
Dijkstra shortest path algorithm.
Fit algorithm in memory management.
Travelling salesman problem.
Fractional knapsack problem.
Egyptian fraction.
Bin packing problem.

9) Fractional and 0-1 Knapsack problems and Difference.

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/

11) Briefly explain about Multistage Graph.

You might also like