Week03 Search
Week03 Search
CS3233
CompetitiveProgramming
p
g
g
Dr.StevenHalim
Dr.
Steven Halim
Week03 ProblemSolvingParadigms
(FocusonCompleteSearch)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Outline
MiniContest#2+Break+Discussion+Admins
CompleteSearch
Iterative:(Nested)Loops,Permutations,Subsets
RecursiveBacktracking(NQueens),fromeasyto(very)hard
StateSpace Search
StateSpaceSearch
MeetintheMiddle(BidirectionalSearch)
Readathome(willnotbetestedinminicontestA/B):
(
/ )
Sometipstospeedupyoursolution
Greedyalgorithms
DivideandConquer(D&C)algorithms
EspeciallyBinarySearchtheAnswertechnique
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Iterative:(Nested)Loops,Permutations,Subsets
R
RecursiveBacktracking(NQueens),fromeasyto(very)hard
i B kt ki (N Q
) f
t (
)h d
StateSpaceSearch
Meet in the Middle (Bidirectional Search)
MeetintheMiddle(BidirectionalSearch)
COMPLETESEARCH
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
IterativeCompleteSearch
p
Loops(1)
UVa725 Division
Findtwo5digitsnumbers.t. abcde /fghij =N
abcdefghij mustbealldifferent,2<=N<=79
IterativeCompleteSearchSolution(NestedLoops):
p
(
p)
Tryallpossiblefghij (oneloop)
Obtainabcde fromfghij*N
g j
Checkifabcdefghij arealldifferent(another loop)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
IterativeCompleteSearch
p
Loops(2)
Morechallengingvariants:
234Knestedloops
Somepruningarepossible,
bl
e.g.usingcontinue,break,orifstatements
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
IterativeCompleteSearch
p
Permutations
UVa 11742 SocialConstraints
Thereare0<n 8moviegoers
Theywillsitinthefrontrowwithn consecutiveopenseats
Thereare0m 20seatingconstraintsamongthem,
i.e.a andb mustbeatmost(oratleast)c seatsapart
Howmanypossibleseatingarrangementsarethere?
IterativeCompleteSearchSolution(Permutations):
Setcounter=0andthentryallpossiblen! permutations
Increasecounterifapermutationsatisfiesallm constraints
Outputthefinalvalueofcounter
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
IterativeCompleteSearch
p
Subsets
UVa12346 WaterGateManagement
Adamhas1n 20watergatestoletoutwaterwhennecessary,
each water gate has flow rate and damage cost
eachwatergatehasflowrateanddamagecost
Yourtaskistomanagetheopeningofthewatergatesinordertoget
ridofatleast thespecifiedtotalflowrate conditionthatthetotal
d
damagecost
i i i i d!
isminimized!
IterativeCompleteSearchSolution(Subsets):
Try
Tryallpossible2
all possible 2n subsetsofwatergatestobeopened
subsets of water gates to be opened
Foreachsubset,checkifithassufficientflowrate
Ifitis,checkifthetotaldamagecostofthissubsetissmallerthanthe
overallminimumdamagecostsofar
Ifitis,updatetheoverallminimumdamagecostsofar
Outputtheminimumdamagecost
Output the minimum damage cost
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Iterative:(Nested)Loops,Permutations,Subsets
R
RecursiveBacktracking(NQueens),fromeasyto(very)hard
i B kt ki (N Q
) f
t (
)h d
StateSpaceSearch
Meet in the Middle (Bidirectional Search)
MeetintheMiddle(BidirectionalSearch)
COMPLETESEARCH
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Naveways(TimeLimitExceeded)
y (
)
Choose8outof64cells
64C8
=4Billionpossibilities
Insight1:Putonequeenineachcolumn
88 =17Millionpossibilities:O
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Scrutinizethesamplecodeofrecursivebacktracking!
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
// ok
k t
to use global
l b l variables
i bl
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Severalcellsareforbidden
Dothishelps?
p
ncannowbeaslargeasn=14:O??
Howtorun14!algorithminafewseconds?
How to run 14! algorithm in a few seconds?
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Wecanspeedupthispartbyusing2*n1boolean arrays
(or bitset) to test if a certain left/right diagonal can be used
(orbitset)totestifacertainleft/rightdiagonalcanbeused
http://www.comp.nus.edu.sg/~stevenha/
http://www.comp.nus.edu.sg/
stevenha/
visualization/recursion.html
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Is that enough?
Isthatenough?
Unfortunatelyno
Butfortunatelythereisabetterwayofusingdiagonalchecks
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Iterative:(Nested)Loops,Permutations,Subsets
R
RecursiveBacktracking(NQueens),fromeasyto(very)hard
i B kt ki (N Q
) f
t (
)h d
StateSpaceSearch
Meet in the Middle (Bidirectional Search)
MeetintheMiddle(BidirectionalSearch)
COMPLETESEARCH
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
UVa11212 EditingaBook
g
RujiaLiusProblem
Givenn equallengthparagraphsnumberedfrom1ton
ll
h
h
b df
Arrangethemintheorderof1,2,...,n
With the help of a clipboard
Withthehelpofaclipboard,
youcanpressCtrlX(cut)andCtrlV(paste)severaltimes
Youcannotcuttwicebeforepasting,butyoucancutseveralcontiguous
paragraphs at the same time they'llbepastedinorder
paragraphsatthesametime
they'll be pasted in order
Thequestion:Whatistheminimumnumberofstepsrequired?
p
{ , , ( ), , , }
,
Example1:Inordertomake{2,4,(1),5,3,6}sorted,
youcancut1andpasteitbefore2 {1, 2,4,5,(3),6}
thencut3andpasteitbefore4 {1,2,3, 4,5,6} done
Example2:Inordertomake{(3,4,5),1,2}sorted,
Example 2: In order to make {(3 4 5) 1 2} sorted
youcancut{3,4,5}andpasteitafter{1,2} {1,2,3,4,5}
orcut{1,2}andpasteitbefore{3,4,5} {1,2,3,4,5}
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Trivialbutwrongalgorithm:
Ti i lb t
l ith
Cutaparagraphthatisinthewrongposition
Pastethatparagraphinthecorrectposition
Paste that paragraph in the correct position
Afterk1suchcutpaste,wewillhaveasortedparagraph
Thelastwrongpositionwillbeinthecorrectpositionatthisstage
Butthismaynotbetheshortestway
Examples:
{(3)
{(3),2,1}
2 1} {(2),1,3}
{(2) 1 3} {1,2,3}
{1 2 3} 2 steps
{(5),4,3,2,1} {(4),3,2,1,5} {(3),2,1,4,5} {(2),1,3,4,5}
{1,2,3,4,5} 4steps
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
{5,4,3,2,1}
{ , , , , }
Answer:Only3 steps,e.g.
{5,4,(3,2),1} {3,(2,5),4,1} {3,4,(1,2),5} {1,2,3,4, 5}
Howabout{5,4,9,8,7,3,2,1,6}?
Answer:4,butveryhardtocomputemanually
Howabout{9,8,7,6,5,4,3,2,1}?
H
b
{9 8 7 6 5 4 3 2 1}?
Answer:5,butveryhardtocomputemanually
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Some Analysis
SomeAnalysis
Thereareatmostn!permutationsofparagraphs
Withmaximumn=9,thisis9!or362880
Thenumberofverticesisnotthatbigactually
The number of vertices is not that big actually
Givenapermutationoflengthn(avertex)
Thereare
There are nC2 possiblecuttingpoints(indexi,j
possible cutting points (index i j [1..n])
[1 n])
Therearenpossiblepastingpoints(indexk [1..(n(ji+1))])
Therefore,foreachvertex,thereareaboutO(n3)branches
TheworstcasebehaviorifwerunasingleBFSonthisState
Spacegraphis:O(V+E)=O(n!+n!*n3)=O(n!*n3)
Withn=9,thisis9!*93 =264539520~265M,TLE(ormaybeMLE)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Iterative:(Nested)Loops,Permutations,Subsets
R
RecursiveBacktracking(NQueens),fromeasyto(very)hard
i B kt ki (N Q
) f
t (
)h d
StateSpaceSearch
Meet in the Middle (Bidirectional Search)
MeetintheMiddle(BidirectionalSearch)
COMPLETESEARCH
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
DepthLimitedSearch(DLS)+IterativeDLS
A*/IterativeDeepeningA*(IDA*)/MemoryBoundedA*
BranchandBound(BnB)
MaybeinWeek12 or...
WewillnottestanyoftheseinminicontestsproblemA/B
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
Summary
WehaveseensomeCompleteSearchtechniques
Thereare(several)others
Westillneedlotsofpracticethough
WeskippedGreedyandDivide&Conquerthistime
pp
y
q
ReadSection3.33.4byyourself
WewillnottesttheseonminicontestsproblemA/B
p
/
Nextweek,wewillsee(revisit)thefourthparadigm:
DynamicProgramming
Dynamic Programming
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
References
CompetitiveProgramming2.9, Section3.2+8.2
Steven,Felix
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS