You are on page 1of 9

Section B

a) Write and Explain Algorithm for Hamiltonian Cycle with example


n an undirected graph, the Hamiltonian path is a path, that visits each vertex exactly once, and the
Hamiltonian cycle or circuit is a Hamiltonian path, that there is an edge from the last vertex to the
first vertex.
In this problem, we will try to determine whether a graph contains a Hamiltonian cycle or not.
And when a Hamiltonian cycle is present, also print the cycle.
Input:
The adjacency matrix of a graph G(V, E).

Output:
The algorithm finds the Hamiltonian path of the given graph. For
this case it is (0, 1, 2, 4, 3, 0). This graph has some other
Hamiltonian paths.
If one graph has no Hamiltonian path, the algorithm should return
false.
b)

c) Write an Algorithm for 4 queen problem with state space tree using Backtracking
d) Write and Explain Algorithms for Color Problem with example using Backtracking.
Given an undirected graph and a number m, determine if the graph can be coloured with at
most m colours such that no two adjacent vertices of the graph are colored with the same color.

e) Solve following Job sequencing with deadline


i 1 2 3 4 5

p 20 15 15 5 1

d 2 2 1 3 3

The given figure shows the gantt chart:

2 1 4 3 5
0 1 2 3 4 5
The jobs run within time are : 2, 1 and 4
Penalty caused = 15 + 1 =16 (due to activity 3 and 5)

Section C

a) Write the algorithm for the sum of subset problems. Draw the state space tree for
the given set S = {3,4,5,6}, m =9
b) Find the longest common subsequence for the given strings. X = TAGTCACG, Y= AGACTGTC. Write
Algorithm
c) Discuss 0/1 Knapsack problem with respect to Dynamic Programming with
Example? Is Greedy approach is equally applicable for the above problem
0/1 Knapsack Problem-
In 0/1 Knapsack Problem,

● As the name suggests, items are indivisible here.


● We can not take the fraction of any item.
● We have to either take an item completely or leave it completely.
● It is solved using dynamic programming approach.

Consider-
● Knapsack weight capacity = w
● Number of items each having some weight and value = n

0/1 knapsack problem is solved using dynamic programming in the following steps-

Step-01:

● Draw a table say ‘T’ with (n+1) number of rows and (w+1) number of columns.
● Fill all the boxes of 0th row and 0th column with zeroes as shown-

Step-02:
Start filling the table row wise top to bottom from left to right.

Use the following formula-

T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti ) }


Here, T(i , j) = maximum value of the selected items if we can take items 1 to i and have weight
restrictions of j.

● This step leads to completely filling the table.


● Then, value of the last box represents the maximum possible value that can be put into
the knapsack.

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.

0-1 Knapsack cannot be solved by Greedy approach. Greedy approach does not ensure an
optimal solution. In many instances, Greedy approach may give an optimal solution.

You might also like