Sei sulla pagina 1di 23

This course material is now made available for public usage.

Special acknowledgement to School of Computing, National University of Singapore


for allowing Steven to prepare and distribute these teaching materials.

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

Recursive Backtracking (1)


RecursiveBacktracking(1)
UVa 750 8QueensChessProblem
Put8queensin8x8Chessboard
Noqueencanattackotherqueens

Naveways(TimeLimitExceeded)
y (
)
Choose8outof64cells

64C8

=4Billionpossibilities

Insight1:Putonequeenineachcolumn
88 =17Millionpossibilities:O

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Recursive Backtracking (2)


RecursiveBacktracking(2)
Betterway,recursivebacktracking
Insight2:alldifferentconstraint fortherowstoo
WeputonequeenineachcolumnANDeachrow
Findingavalidpermutationoutof8!possiblepermutations
Searchspacegoesdownfrom8
Search space goes down from 88 =17Mto8!=40K!
17M to 8! 40K!

Insight3:main diagonal andsecondarydiagonal check


Another
Anotherwaytoprunethesearchspace
way to prune the search space
QueenA(i,j)attacksQueenB(k,l)iff
abs(i - k) == abs(j - l)

Scrutinizethesamplecodeofrecursivebacktracking!
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Important Code (3)


ImportantCode(3)
i t rw[8],
int
[8] TC
TC, a, b
b, lineCounter;
li C
t

// ok
k t
to use global
l b l variables
i bl

bool place(int r, int c) {


prev++)
)
// check previously placed queens
for (int prev = 0; prev < c; prev
if (rw[prev] == r || (abs(rw[prev] - r) == abs(prev - c)))
return false;
// share same row or same diagonal -> infeasible
return true; }
void backtrack(int c) {
if (c == 8 && rw[b] == a) {
// candidate sol, (a, b) has 1 queen
printf("%2d
i tf("%2d
%d",
%d" ++lineCounter,
++li C
t
rw[0]
[0] + 1)
1);
for (int j = 1; j < 8; j++) printf(" %d", rw[j] + 1);
printf("\n"); }
(int r = 0;
; r < 8;
; r++)
)
// try
y all p
possible row
for (
if (place(r, c)) {
// if can place a queen at this col and row
rw[c] = r; backtrack(c + 1);
// put this queen here and recurse
}
}

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Is that the best n Queens solution?


IsthatthebestnQueenssolution?
Maybenot
SeeUVa 11195 AnothernQueenProblem

Severalcellsareforbidden
Dothishelps?
p

ncannowbeaslargeasn=14:O??
Howtorun14!algorithminafewseconds?
How to run 14! algorithm in a few seconds?

CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS

Speeding Up Diagonal Checks


SpeedingUpDiagonalChecks
Thischeckisslow:
bool place(int r, int c) {
f
for
(i
(int
t prev = 0;
0 prev < c; prev++)
)
// check
h k previously
i
l placed
l
d queens
if (rw[prev] == r || (abs(rw[prev] - r) == abs(prev - c)))
return false;
// share same row or same diagonal -> infeasible
return true; }

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

Loose Upper Bound


LooseUpperBound
Answer:k1
Wherek isthenumberofparagraphsinitiallythewrongpositions

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

The Actual Answers


TheActualAnswers
{3,2,1}
Answer:2steps,e.g.
{(3)
{(3),2,1}
2 1} {(2),1,3}
{(2) 1 3} {1,2,3},or
{1 2 3} or
{3,2,(1)} {1,(3),2,} {1,2,3}

{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

More Search Algorithms


MoreSearchAlgorithms

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

Potrebbero piacerti anche