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
http://codeforces.com/blog/entry/337 1/5
codestrophe|Logout
Sponsoredby
YouhaveWow!Youhave+178!
Searchbytag
HOME CONTESTS GYM PROBLEMSET GROUPS RATING API HELP RCC
Ripatti'sblog
Alittlebitofclassics:dynamicprogrammingover
subsetsandpathsingraphs
AuthorthanksadamaxfortranslationthisarticleintoEnglish.
Introduction
AfterCodeforcesBetaRound#11severalparticipantsexpressedawishtoreadsomething
aboutproblemssimilartoproblemDofthatround.Theauthorofthisarticle,forthepurpose
ofhelpingthem,triedsearchingforsuchinformation,buttohissurprisecouldn'tfind
anythingontheInternet.Itisnotknownwhetherthesearchwasnotthoroughorthere's
reallynothingoutthere,but(justincase)theauthordecidedtowritehisownarticleonthis
topic.
InsomesensethisarticlemayberegardedasatutorialfortheproblemDfromBetaRound
#11.
Inthisarticlequitewellknownalgorithmsareconsidered:thesearchforoptimal
Hamiltonianwalksandcycles,findingtheirnumber,checkforexistenceandsomething
more.Thesocalled"dynamicprogrammingoversubsets"isused.Thismethodrequires
exponentialtimeandmemoryandthereforecanbeusedonlyifthegraphisverysmall
typically20verticesorless.
DPoversubsets
Considerasetofelementsnumberedfrom0toN1.Eachsubsetofthissetcanbe
encodedbyasequenceofNbits(wewillcallthissequence"amask").Theithelement
belongstothesubsetifandonlyiftheithbitofthemaskequals1.Forinstance,themask
00010011meansthatthesubsetoftheset[0...7]consistsofelements0,1and4.There
aretotally2 masks,andso2 subsets.Eachmaskisinfactanintegernumberwrittenin
binarynotation.
Themethodistoassignavaluetoeachmask(and,therefore,toeachsubset)and
computethevaluesfornewmasksusingalreadycomputedvalues.Asarule,tofindthe
valueforasubsetAweremoveanelementineverypossiblewayandusevaluesfor
obtainedsubsetsA' ,A' ,...,A' tocomputethevalueforA.Thismeansthatthevalues
forA 'musthavebeencomputedalready,soweneedtoestablishanorderinginwhich
maskswillbeconsidered.It'seasytoseethatthenaturalorderingwilldo:goovermasksin
increasingorderofcorrespondingnumbers.
Wewillusethefollowingnotation:
bit(i,mask)theithbitofmask
count(mask)thenumberofnonzerobitsinmask
first(mask)thenumberofthelowestnonzerobitinmask
(a?b:c)returnsbifaholds,orcotherwise.
Theelementsofoursetwillbeverticesofthegraph.Forthesakeofsimplicitywe'llassume
thatthegraphisundirected.Modificationofthealgorithmsfordirectedgraphsisleftasan
exerciseforthereader.
1.SearchfortheshortestHamiltonianwalk
LetthegraphG=(V,E)havenvertices,andeachedge haveaweight
Beforecontest
CodeforcesRound#252(Div.2)
3days
Payattention
41peoplelikethis.Bethefirstofyour
friends.
Like
codestrophe
Rating:1489
Contribution:0
Settings
Blog
Teams
Submissions
Favourites
Talks
Contests
codestrophe
# User Rating
1 tourist 3228
2 rng_58 2795
3 Petr 2787
4 scott_wu 2767
5 WJMZBMR 2733
6 0O0o00OO0Oo0o0Oo 2718
7 vepifanov 2712
8 SergeyRogulenko 2703
9 yeputons 2690
10 meret 2655
Countries| Cities| Organizations Viewall
Toprated
# User Contrib.
1 DmitriyH 146
2 Fefer_Ivan 142
3 Petr 133
4 Sereja 131
5 MikhailRubinchik 129
5 elfus0 129
7 Egor 128
8 Zlobober 123
9 Nickolas 122
10 Xellos 120
10 dalex 120
Viewall
Topcontributors
Handle:
Finduser
ByRipatti,4yearsago,translation, ,
N N
1 2 k
i
RIPATTI BLOG TEAMS SUBMISSIONS GROUPS TALKS CONTESTS
6/5/2014 A little bit of classics: dynamic programming over subsets and paths in graphs - Codeforces
http://codeforces.com/blog/entry/337 2/5
d(i,j).WewanttofindaHamiltonianwalkforwhichthesumofweightsofitsedgesis
minimal.
Letdp[mask][i]bethelengthoftheshortestHamiltonianwalkinthesubgraphgenerated
byverticesinmask,thatendsinthevertexi.
TheDPcanbecalculatedbythefollowingformulas:
dp[mask][i]=0,ifcount(mask)=1andbit(i,mask)=1
,if
count(mask)>1andbit(i,mask)=1
dp[mask][i]=inothercases.
Nowthedesiredminimallengthis .Ifp =,
thenthereisnoHamiltonianwalkinthegraph.Otherwiseit'seasytorecoverthewalkitself.
Lettheminimalwalkendinthevertexi.Thenthevertexji,forwhich
,isthepreviousvertexinthepath.
Nowremoveifromthesetandfindthevertexprevioustojinthesameway.Continuing
thisprocessuntilonlyonevertexisleft,we'llfindthewholeHamiltonianwalk.
ThissolutionrequiresO(2 n)ofmemoryandO(2 n )oftime.
2.FindingthenumberofHamiltonianwalks
LetthegraphG=(V,E)beunweighted.We'llmodifythepreviousalgorithm.Let
dp[mask][i]bethenumberofHamiltonianwalksonthesubsetmask,whichendinthe
vertexi.TheDPisrewritteninthefollowingway:
dp[mask][i]=1,ifcount(mask)=1andbit(i,mask)=1
,ifcount(mask)>1and
bit(i,mask)=1
dp[mask][i]=0inothercases.
Theansweris .
ThissolutionrequiresO(2 n)ofmemoryandO(2 n )oftime.
3.Findingthenumberofsimplepaths
CalculatetheDPfromthepreviousparagraph.Theansweris
.Thecoefficient1/2isrequiredbecause
eachsimplepathisconsideredtwiceinbothdirections.Alsonotethatonlypathsof
positivelengtharetakenintoaccount.Youcanaddnzerolengthpaths,ofcourse.
ThissolutionrequiresO(2 n)ofmemoryandO(2 n )oftime.
4.CheckforexistenceofHamiltonianwalk
Wecanusesolution2replacingthesumwithbitwiseOR.dp[mask][i]willcontaina
booleanvaluewhetherthereexistsaHamiltonianwalkoverthesubsetmaskwhichends
inthevertexi.DPisthefollowing:
dp[mask][i]=1,ifcount(mask)=1andbit(i,mask)=1
,ifcount(mask)>1and
bit(i,mask)=1
dp[mask][i]=0inothercases.
Thissolution,likesolution2,requiresO(2 n)ofmemoryandO(2 n )oftime.Itcanbe
improvedinthefollowingway.
Letdp'[mask]bethemaskofthesubsetconsistingofthoseverticesjforwhichthere
existsaHamiltonianwalkoverthesubsetmaskendinginj.Inotherwords,we'compress'
thepreviousDP:dp'[mask]equals .ForthegraphGwrite
outnmasksM ,whichgivethesubsetofverticesincidenttothevertexi.Thatis,
.
DPwillberewritteninthefollowingway:
dp'[mask]=2 ,ifcount(mask)=1andbit(i,mask)=1
Find
RodionGorkMysiteforgreycoders
CodeAbbey
praveen123CodeforcesRound#251Editorial
AkshajKVeryweirdbug
praveen123CodeforcesRound#251
LLintSpojProblem
Fefer_IvanCodeforcesAPI
byte_gamblerCodeforcesround250
byte_gamblerSPOJFibosum
alaudoCodeexecutionerroronalengthyinput
howtodebugthisissue?
huzecongCodeforcesRound#248Editorial
alliumnskbisIhaveaproblem
ping128Loopingthroughallsubsetsofasetof
Nelements
ravelo1991AboutRound#251badthings
happenonjudge
ShadekRound251DIV2ProblemC
yak_exColorizestandingsbyusedprogramming
language
yak_exMultipleratinggraphwithotheraccounts
TparsaProblemC(Div.2)Round#94
t.janssen52numberofsolvedsubmissions
wrong?
mobitOnlydiv2contests
EgorCHelper3.9
ErdemKirezGoodSolutionFor440A
RupaiSPOJEPALINTLE
MikeMirzayanovFrequentlyAskedQuestions
alpha_RAUClassesofgraphisomorphism
yashar_sb_sbblitzcontests
Detailed
Recentactions
min
n n 2
n n 2
n n 2
n n 2
i
i
6/5/2014 A little bit of classics: dynamic programming over subsets and paths in graphs - Codeforces
http://codeforces.com/blog/entry/337 3/5
,if
count(mask)>1
dp'[mask]=0inothercases.
Payspecialattentiontotheexpression .Thefirstpartofthe
expressionisthesubsetofverticesj,forwhichthereexistsaHamiltonianwalkoverthe
subsetmaskminusvertexi,endinginj.Thesecondpartoftheexpressionisthesetof
verticesincidenttoi.Ifthesesubsetshavenonemptyintersection(theirbitwiseANDis
nonzero),thenit'seasytoseethatthereexistsaHamiltonianwalkinmaskendinginthe
vertexi.
Thefinaltestistocomparedp[2 1]to0.
ThissolutionusesO(2 )ofmemoryandO(2 n)oftime.
5.FindingtheshortestHamiltoniancycle
Sincewedon'tcareatwhichvertexthecyclestarts,assumethatitstartsat0.Nowuse
solution1forthesubsetofvertices,changingtheformulasinthefollowingway:
dp[1][0]=0
,ifi>0,
bit(0,mask)=1andbit(i,mask)=1
dp[mask][i]=inothercases.
Sodp[mask][i]containsthelengthoftheshortestHamiltonianwalkoverthesubsetmask,
startingat0andendingati.
Therequiredminimumiscalculatedbytheformula
.Ifitequals,thereisnoHamiltonian
cycle.OtherwisetheHamiltoniancyclecanberestoredbyamethodsimilartosolution1.
6.FindingthenumberofHamiltoniancycles
Usingideasfromsolutions5and2onecanderiveaDPcalculatingthenumberof
HamiltoniancyclesrequiringO(2 n )oftimeandO(2 n)ofmemory.
7.Findingthenumberofsimplecycles
Letdp[mask][i]bethenumberofHamiltonianwalksoverthesubsetmask,startingatthe
vertexfirst(mask)andendingatthevertexi.DPlookslikethis:
dp[mask][i]=1,ifcount(mask)=1andbit(i,mask)=1
,ifcount(mask)>1,
bit(i,mask)=1andifirst(mask)
dp[mask][i]=0otherwise.
Theansweris .
ThissolutionrequresO(2 n )oftimeandO(2 n)ofmemory.
8.CheckingforexistenceofHamiltoniancycle
Wecanmodifysolution5and,usingthetrickfromsolution4,obtainanalgorithmrequiring
O(2 n)oftimeandO(2 )ofmemory.
Exercises
CFBR11D
CCTOOLS
P.S.Thisarticlemaybeextendedandfixedinfuture.Theauthorwouldbegratefulfor
supplementingtheExercisessectionandforpointingoutanymistakesandinaccuracies.
algorithms, dynamicprogramming, graphs
n
n n
n 2 n
n 2 n
n n
+22
Ripatti 4yearsago 13
6/5/2014 A little bit of classics: dynamic programming over subsets and paths in graphs - Codeforces
http://codeforces.com/blog/entry/337 4/5
Comments(13)
AlexErofeev
Ripatti
jaher
Ripatti
gmark
Ripatti
giongto35
giongto35
Writecomment?
4yearsago, # | +3
http://www.spoj.pl/problems/ASSIGN/
Forexample
Reply
4yearsago, # ^ | 0
,)),
,.
,.
.,,.
:)
Reply
4yearsago, # | 0
Nicetutorial.Youcouldalsoaddanexplanationonhowtocount
hamiltoniancircuitsonagridofsizenxmwith(n<=7andm<=10^9)for
examplewhichisalsofairlyclassical.
Reply
4yearsago, # ^ | +1
Countinghamiltoniancircuitsonagridisquitedifferentfrom
problemswhichwasviewedthere.InarticleIviewforemost
masksoversubsetsandpathsingraphs.Butinyourproblem
motzkinwordsandmatrixmultiplicationareused:)
MaybeIwillviewyourprobleminfiturearticle:)
Reply
4yearsago, # ^ | 0
CouDoyouhaveareferencetosuchaproblem?
Reply
4yearsago, # ^ | 0
http://www.cs.ust.hk/mjg_lib/Library/Kwong_Ham_94.pdf
itisveryoldpaper:)
Reply
3yearsago, # ^ | 0
Ifthesizeofgridis12*12.CanweuseDPoverSubsets
Reply
3yearsago, # ^ | 0
Topup
Reply
4yearsago, # | 0
http://www.codechef.com/problems/TOOLS
6/5/2014 A little bit of classics: dynamic programming over subsets and paths in graphs - Codeforces
http://codeforces.com/blog/entry/337 5/5
Codeforces(c)Copyright20102014MikeMirzayanov
TheonlyprogrammingcontestsWeb2.0platform
Servertime:2014060511:42:28(c4).
havaliza
Ripatti
saadtaame
bufferedreader
niklasb
http://www.codechef.com/problems/TOOLS
isanexampleproblemforpart5.
Reply
4yearsago, # ^ | 0
Thanx!Veryniceproblem))Iaddedit.
Reply
new,3daysago, # | 0
Couldyoupleasefurtherexplaintherecurrenceinthefirstexample(finding
ashortestHamiltonianwalk)?
Reply
new,3daysago, # ^ | 0
Formetoo,please.
Reply
new,3daysago, # | Rev.3 0
Thefollowingpaperispotentiallyusefulasreference:
"ADynamicProgrammingApproachtoSequencingProblems"byMichael
HeldandRichardM.Karp,1962.[citation,download]
ItcontainslotsofpreliminaryanalysisandatleasttheDPapproaches
describedin1.and5.ofyourpost.Ibelievethealgorithmisactuallycalled
"HeldKarpalgorithm"inacademicliterature,butitwasdescribed
independentlybyBellmanetal.inanotherpaper.
AlistofoptimalalgorithmsforTSPwithbetterruntimecomplexitycanbe
foundhere.
Reply