week03 search
TRANSCRIPT
-
8/9/2019 Week03 Search
1/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.
Com etitivePro rammin
Week03 ProblemSolvingParadigms(FocusonCompleteSearch)
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
2/23
MiniContest#2+Break+Discussion+Admins
Complete
Search Iterative:(Nested)Loops,Permutations,Subsets
RecursiveBacktracking(NQueens),fromeasyto(very)hard
MeetintheMiddle(BidirectionalSearch)
Readathome willnotbetestedinminicontestA B :
Sometipstospeedupyoursolution
Greedyalgorithms
DivideandConquer(D&C)algorithms
EspeciallyBinarySearchtheAnswertechnique
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
3/23
Iterative:(Nested)Loops,Permutations,Subsets
ecurs ve ac rac ng ueens , romeasy o very ar
StateSpaceSearch
Meet in the Middle Bidirectional Search
COMPLETESEARCH
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
4/23
IterativeCom leteSearch
Loops(1)
UVa725 Division
Findtwo5digitsnumbers.t. abcde/fghij=N abcdefghijmustbealldifferent,2
-
8/9/2019 Week03 Search
5/23
IterativeCom leteSearch
Loops(2)
Morechallengingvariants:
2
3
4
K
nested
loops Somepruningarepossi e,
e.g.usingcontinue,break,orifstatements
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
6/23
IterativeCom leteSearch
Permutations
UVa 11742 SocialConstraints
Thereare0
-
8/9/2019 Week03 Search
7/23
IterativeCom leteSearch
Subsets
UVa12346 WaterGateManagement
Adamhas1 n20watergatestoletoutwaterwhennecessary,
Yourtaskistomanagetheopeningofthewatergatesinordertoget
ridofat
leastthespecifiedtotalflowrateconditionthatthetotal
amage
costis
minimize IterativeCompleteSearchSolution(Subsets):
Foreachsubset,checkifithassufficientflowrate
Ifitis,checkifthetotaldamagecostofthissubsetissmallerthanthe
overallminimumdamagecost sofar
Ifitis,updatetheoverallminimumdamagecostsofar
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
8/23
Iterative:(Nested)Loops,Permutations,Subsets
ecurs ve ac rac ng ueens , romeasy o very ar
StateSpaceSearch
Meet in the Middle Bidirectional Search
COMPLETESEARCH
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
9/23
UVa 750 8QueensChessProblem
Put8queensin8x8Chessboard Noqueencanattackotherqueens
Naveways(TimeLimitExceeded)
Choose8outof64cells 64C8=4Billionpossibilities
Insight1:Putonequeenineachcolumn
88 =17Millionpossibilities:O
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
10/23
Betterway,recursivebacktracking
Insight2:alldifferent
constraintfortherowstoo
WeputonequeenineachcolumnANDeachrow
Findingavalidpermutationoutof8!possiblepermutations
earc spacegoes own rom = o =
Insight3:maindiagonalandsecondarydiagonalcheck
QueenA(i,j)attacksQueenB(k,l)iff
abs( i - k) == abs( j - l )
Scrutinizethesamplecodeofrecursivebacktracking!
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
11/23
n r w , , a, , ne oun er o o use g o a var a es
bool pl ace( i nt r , i nt c) {
f or i nt r ev = 0 r ev < c r ev++ check r evi ousl l aced ueens
i f ( r w[ pr ev] == r | | ( abs( r w[ pr ev] - r ) == abs( pr ev - c) ) )
r et ur n f al se; / / shar e same r ow or same di agonal - > i nf easi bl e
r et ur n t r ue; }
voi d backt r ack( i nt c) {
i f ( c == 8 && r w[ b] == a) { / / candi dat e sol , ( a, b) has 1 queen
" "pr n , ne oun er , w ;
f or ( i nt j = 1; j < 8; j ++) pr i nt f ( " %d", r w[ j ] + 1) ;
pr i nt f ( " \ n" ) ; }f or i nt r = 0 r < 8 r ++ / / t r al l ossi bl e r ow
i f ( pl ace( r , c) ) { / / i f can pl ace a queen at t hi s col and r ow
r w[ c] = r ; backt r ack( c + 1) ; / / put t hi s queen her e and r ecur se
} }
CS3233CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
12/23
Maybenot
SeeUVa 11195 AnothernQueenProblem Severalcellsareforbidden
Dothishelps?
ncannowbeaslargeasn=14:O??
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
13/23
Thischeckisslow:
bool pl ace( i nt r , i nt c) {or n pr ev = ; pr ev c; r ev + c ec pr ev ous y p ace queens
i f ( r w[ pr ev] == r | | ( abs( r w[ pr ev] - r ) == abs( pr ev - c) ) )
r et ur n f al se; / / shar e same r ow or same di agonal - > i nf easi bl e
r et ur n t r ue
Wecanspeedupthispartbyusing2*n1boolean arrays
~. . . .
visualization/recursion.html
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
14/23
Unfortunatelyno
But
fortunately
there
is
a
better
way
of
using
diagonal
checks
CS3233CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
15/23
Iterative:(Nested)Loops,Permutations,Subsets
ecurs ve ac rac ng ueens , romeasy o very ar
StateSpaceSearch
Meet in the Middle Bidirectional Search
COMPLETESEARCH
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
16/23
UVa11212 Editin aBookRujia
Lius
Problem
Givennequaengt paragrap snum ere rom1ton
Arrangethemintheorderof1,2,...,n
,
youcanpressCtrlX(cut)andCtrlV(paste)severaltimes
Youcannotcuttwicebeforepasting,butyoucancutseveralcontiguous
'
Thequestion:Whatistheminimumnumberofstepsrequired?
Exam le1:Inordertomake{2,4,(1),5,3,6}sorted,
youcancut1andpasteitbefore2 {1,2,4,5,(3),6}
then
cut
3
and
paste
it
before
4
{1,
2,
3,4,
5,
6}
done
, , , , ,
youcancut{3,4,5}andpasteitafter{1,2} {1,2,3,4,5}
orcut{1,2}andpasteitbefore{3,4,5} {1,2,3,4,5}
CS3233CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
17/23
Answer:k1
Wherekisthenumberofparagraphsinitiallythewrongpositions
r v a u wronga gor m:
Cutaparagraphthatisinthewrongposition
Afterk1such
cut
paste,
we
will
have
asorted
paragraph
Thelastwrongpositionwillbeinthecorrectpositionatthisstage
Butthismaynotbetheshortestway
Examples: , , , , , , 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
CS3233CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
18/23
{3,2,1}
Answer:2steps,e.g.
, , , , , , ,
{3,2,(1)} {1,(3),2,} {1,2,3}
5,4,3,2,1
Answer:Only
3steps,
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,
but
very
hard
to
compute
manually Howa out 9,8,7,6,5,4,3,2,1
Answer:5,butveryhardtocomputemanually
CS3233CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
19/23
Thereareatmostn!permutationsofparagraphs
Withmaximumn=9,thisis9!or362880
Givenapermutationoflengthn(avertex)
n 2 , ..
Thereare
npossible
pasting
points
(index
k[1..(n(ji+1))])
Therefore,foreachvertex,thereareaboutO(n3)branches
TheworstcasebehaviorifwerunasingleBFSonthisState
Spacegraph
is:
O(V+E)
=O(n!
+n!*n3)
=O(n!*n3)
Withn=9,thisis9!*93 =264539520~265M,TLE(ormaybeMLE)
CS3233CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
20/23
Iterative:(Nested)Loops,Permutations,Subsets
ecurs ve ac rac ng ueens , romeasy o very ar
StateSpaceSearch
Meet in the Middle Bidirectional Search
COMPLETE
SEARCH
CS3233CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
21/23
DepthLimitedSearch(DLS)+IterativeDLS
A*/IterativeDeepeningA*(IDA*)/MemoryBoundedA*
BranchandBound(BnB)
MaybeinWeek12 or...
WewillnottestanyoftheseinminicontestsproblemA/B
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
22/23
WehaveseensomeCompleteSearchtechniques
There
are
(several)
others Westillneedlotsofpracticethough
WeskippedGreedyandDivide&Conquerthistime
ReadSection3.33.4byyourself
Wewillnottesttheseonminicontests roblemA B
Nextweek,wewillsee(revisit)thefourthparadigm:
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS
-
8/9/2019 Week03 Search
23/23
CompetitiveProgramming2.9, Section3.2+8.2
Steven,
Felix
CS3233 CompetitiveProgramming,
StevenHalim,SoC,NUS