0% found this document useful (0 votes)
134 views

6.2 Sum-Of-Subsets, Hamiltonian Cycles

The document discusses two NP-complete problems: the subset sum problem and the Hamiltonian circuit problem. The subset sum problem involves finding a subset of integers from a given set such that the subset sums to a target value. Backtracking can be used to solve this problem by implicitly searching a binary tree. The Hamiltonian circuit problem involves finding a circuit in a graph that visits each vertex exactly once. Backtracking searches for such a circuit by incrementally building a path and backtracking when a dead end is reached. An example illustrates using backtracking to find a Hamiltonian circuit in a sample graph.

Uploaded by

Komal Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

6.2 Sum-Of-Subsets, Hamiltonian Cycles

The document discusses two NP-complete problems: the subset sum problem and the Hamiltonian circuit problem. The subset sum problem involves finding a subset of integers from a given set such that the subset sums to a target value. Backtracking can be used to solve this problem by implicitly searching a binary tree. The Hamiltonian circuit problem involves finding a circuit in a graph that visits each vertex exactly once. Backtracking searches for such a circuit by incrementally building a path and backtracking when a dead end is reached. An example illustrates using backtracking to find a Hamiltonian circuit in a sample graph.

Uploaded by

Komal Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Sum-of-subsets, Hamiltonian cycles

Subset-Sum Problem

The Subset-Sum Problem is to find a subset's' of the given set S = (S 1 S2 S3...Sn) where the
elements of the set S are n positive integers in such a manner that s'∈S and sum of the
elements of subset's' is equal to some positive integer 'X.'

The Subset-Sum Problem can be solved by using the backtracking approach. In this implicit
tree is a binary tree. The root of the tree is selected in such a way that represents that no
decision is yet taken on any input. We assume that the elements of the given set are arranged
in increasing order:

S1 ≤ S2 ≤ S3... ≤ Sn

The left child of the root node indicated that we have to include 'S 1' from the set 'S' and the
right child of the root indicates that we have to execute 'S 1'. Each node stores the total of the
partial solution elements. If at any stage the sum equals to 'X' then the search is successful
and terminates.

The dead end in the tree appears only when either of the two inequalities exists:

o The sum of s' is too large i.e.

s'+ Si + 1 > X

o The sum of s' is too small i.e.

Example: Given a set S = (3, 4, 5, 6) and X =9. Obtain the subset sum using Backtracking
approach.

Solution:

1. Initially S = (3, 4, 5, 6) and X =9.  
2.           S'= (∅)  

The implicit binary tree for the subset sum problem is shown as fig:

The number inside a node is the sum of the partial solution elements at a particular level.

Thus, if our partial solution elements sum is equal to the positive integer 'X' then at that time
search will terminate, or it continues if all the possible solution needs to be obtained.

Hamiltonian Circuit Problems

Given a graph G = (V, E) we have to find the Hamiltonian Circuit using Backtracking
approach. We start our search from any arbitrary vertex say 'a.' This vertex 'a' becomes the
root of our implicit tree. The first element of our partial solution is the first intermediate
vertex of the Hamiltonian Cycle that is to be constructed. The next adjacent vertex is selected
by alphabetical order. If at any stage any arbitrary vertex makes a cycle with any vertex other
than vertex 'a' then we say that dead end is reached. In this case, we backtrack one step, and
again the search begins by selecting another vertex and backtrack the element from the
partial; solution must be removed. The search using backtracking is successful if a
Hamiltonian Cycle is obtained.
Example: Consider a graph G = (V, E) shown in fig. we have to find a Hamiltonian circuit
using Backtracking method.

Solution: Firstly, we start our search with vertex 'a.' this vertex 'a' becomes the root of our
implicit tree.

Next, we choose vertex 'b' adjacent to 'a' as it comes first in lexicographical order (b, c, d).

Next, we select 'c' adjacent to 'b.'


Next, we select 'd' adjacent to 'c.'

Next, we select 'e' adjacent to 'd.'


Next, we select vertex 'f' adjacent to 'e.' The vertex adjacent to 'f' is d and e, but they have
already visited. Thus, we get the dead end, and we backtrack one step and remove the vertex
'f' from partial solution.
From backtracking, the vertex adjacent to 'e' is b, c, d, and f from which vertex 'f' has already
been checked, and b, c, d have already visited. So, again we backtrack one step. Now, the
vertex adjacent to d are e, f from which e has already been checked, and adjacent of 'f' are d
and e. If 'e' vertex, revisited them we get a dead state. So again we backtrack one step.

Now, adjacent to c is 'e' and adjacent to 'e' is 'f' and adjacent to 'f' is 'd' and adjacent to 'd' is 'a.'
Here, we get the Hamiltonian Cycle as all the vertex other than the start vertex 'a' is visited
only once. (a - b - c - e - f -d - a).
Again Backtrack

Here we have generated one Hamiltonian circuit, but another Hamiltonian circuit can also be
obtained by considering another vertex.
RELEVANT READING MATERIAL AND REFERENCES:

Source Notes:
1. https://www.javatpoint.com/subset-sum-problems
2. https://www.javatpoint.com/hamiltonian-circuit-problems

Lecture Video:

1. https://youtu.be/kyLxTdsT8ws

Online Notes:

1. http://vssut.ac.in/lecture_notes/lecture1428551222.pdf

Text Book Reading:

1. Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of India, 3rd
edition 2012. problem, Graph coloring.

In addition: PPT can be also be given.

You might also like