A Little Bit of Classics - Dynamic Programming Over Subsets and Paths in Graphs - Codeforces
A Little Bit of Classics - Dynamic Programming Over Subsets and Paths in Graphs - Codeforces
HOME
RIPATTI
CONTESTS
BLOG
TEAMS
GYM
PROBLEMSET
SUBMISSIONS
GROUPS
GROUPS
TALKS
RATING
API
HELP
BAYAN 2015
CONTESTS
Ripatti's blog
Pay attention
Before contest
Search by tag
RCC
Top rated
Introduction
User
Rating
tourist
3203
Petr
2973
anything on the Internet. It is not known whether the search was not thorough or there's
really nothing out there, but (just in case) the author decided to write his own article on this
WJMZBMR
2831
rng_58
2811
topic.
vepifanov
2740
scott_wu
2719
0O0o00OO0Oo0o0Oo
2718
TankEngineer
2685
bmerry
2684
10
yeputons
After Codeforces Beta Round #11 several participants expressed a wish to read something
about problems similar to problem D of that round. The author of this article, for the purpose
of helping them, tried searching for such information, but to his surprise couldn't find
In some sense this article may be regarded as a tutorial for the problem D from Beta Round
#11.
In this article quite well-known algorithms are considered: the search for optimal Hamiltonian
walks and cycles, finding their number, check for existence and something more. The so-
called "dynamic programming over subsets" is used. This method requires exponential time
and memory and therefore can be used only if the graph is very small - typically 20 vertices
or less.
Top contributors
2665
View all
User
Contrib.
Fefer_Ivan
145
DP over subsets
DmitriyH
141
Consider a set of elements numbered from 0 to N-1. Each subset of this set can be
Petr
139
cgy4ever
131
encoded by a sequence of N bits (we will call this sequence "a mask"). The i-th element
belongs to the subset if and only if the i-th bit of the mask equals 1. For instance, the mask
marat.snowbear
130
00010011 means that the subset of the set [0... 7] consists of elements 0, 1 and 4. There
are totally 2N masks, and so 2N subsets. Each mask is in fact an integer number written in
Xellos
129
LeBron
129
binary notation.
MikhailRubinchik
124
SkidanovAlex
124
BYN
124
The method is to assign a value to each mask (and, therefore, to each subset) and compute
the values for new masks using already computed values. As a rule, to find the value for a
View all
subset A we remove an element in every possible way and use values for obtained subsets
A'1,A'2,... ,A'k to compute the value for A. This means that the values for Ai' must have
been computed already, so we need to establish an ordering in which masks will be
considered. It's easy to see that the natural ordering will do: go over masks in increasing
order of corresponding numbers.
Find user
Handle:
Find
Recent actions
ikatanic COCI Round 2 Season 14/15
The elements of our set will be vertices of the graph. For the sake of simplicity we'll assume
that the graph is undirected. Modification of the algorithms for directed graphs is left as an
exercise for the reader.
We want to find a Hamiltonian walk for which the sum of weights of its edges is minimal.
Let dp[mask][i] be the length of the shortest Hamiltonian walk in the subgraph generated
skrydg ios_base::sync_with_stdio(false);
. If pmin=,
then there is no Hamiltonian walk in the graph. Otherwise it's easy to recover the walk itself.
Let the minimal walk end in the vertex i. Then the vertex ji, for which
, is the previous vertex in the path. Now
remove i from the set and find the vertex previous to j in the same way. Continuing this
process until only one vertex is left, we'll find the whole Hamiltonian walk.
Let the graph G=(V,E) be unweighted. We'll modify the previous algorithm. Let
dp[mask][i] be the number of Hamiltonian walks on the subset mask, which end in the
vertex i. The DP is rewritten in the following way:
dp[mask][i]=1, if count(mask)=1 and bit(i,mask)=1;
, if count(mask)>1
and bit(i,mask)=1;
dp[mask][i]=0 in other cases.
The answer is
boolean value - whether there exists a Hamiltonian walk over the subset mask which ends
in the vertex i. DP is the following:
and bit(i,mask)=1;
, if count(mask)>1
This solution, like solution 2, requires O(2nn) of memory and O(2nn2) of time. It can be
improved in the following way.
Let dp'[mask] be the mask of the subset consisting of those vertices j for which there
exists a Hamiltonian walk over the subset mask ending in j. In other words, we 'compress'
the previous DP: dp'[mask] equals
out n masks M i, which give the subset of vertices incident to the vertex i. That is,
.
DP will be rewritten in the following way:
count(mask)>1;
dp'[mask]=0 in other cases.
Pay special attention to the expression
expression is the subset of vertices j, for which there exists a Hamiltonian walk over the
subset mask minus vertex i, ending in j. The second part of the expression is the set of
vertices incident to i. If these subsets have non-empty intersection (their bitwise AND is non-
zero), then it's easy to see that there exists a Hamiltonian walk in mask ending in the vertex
i.
dp[1][0]=0;
, if i>0,
So dp[mask][i] contains the length of the shortest Hamiltonian walk over the subset
n 2
P.S. This article may be extended and fixed in future. The author would be grateful for
supplementing the Exercises section and for pointing out any mistakes and inaccuracies.
algorithms, dynamic programming, graphs
Ripatti
+22
5 years ago
Comments (14)
14
Write comment?
# |
http://www.spoj.pl/problems/ASSIGN/
5 years ago,
+3
For example
AlexErofeev
Reply
# ^ |
0
, )) ,
5 years ago,
, .
, .
. , , .
Ripatti
:)
Reply
# |
Nice tutorial. You could also add an explanation on how to count
5 years ago,
hamiltonian circuits on a grid of size n x m with (n <= 7 and m <= 10^9) for
example which is also fairly classical.
jaher
Reply
# ^ |
+1
Counting hamiltonian circuits on a grid is quite different from
5 years ago,
Reply
# ^ |
CouDo you have a reference to such a problem?
5 years ago,
Reply
gmark
# ^ |
0
http://www.cs.ust.hk/mjg_lib/Library/Kwong_Ham_94.pdf
5 years ago,
Reply
Ripatti
# ^ |
0
If the size of grid is 12*12 . Can we use DP over Subsets
4 years ago,
Reply
giongto35
4 years ago,
Top up
# ^ |
Reply
giongto35
# |
http://www.codechef.com/problems/TOOLS
4 years ago,
Reply
havaliza
# ^ |
Thanx! Very nice problem)) I added it.
4 years ago,
Reply
Ripatti
5 months ago,
# |
Could you please further explain the recurrence in the first example (finding
a shortest Hamiltonian walk)?
Reply
saadtaame
5 months ago,
# ^ |
Reply
bufferedreader
5 months ago,
# |
Rev. 3
niklasb
A list of optimal algorithms for TSP with better runtime complexity can be
found here.
Reply
3 months ago,
# |
Reply