ExamReviewandSolving HardProblems ExamReview • Anyquestions? • Anytopicsyouwanttoreview? Solvinghardproblems • Threekeymethods: • Backtracking • Branch-and-bound • Approximation Backtracking • Consideratreeofallchoicesthathavebeenmadesofar • Specifically,decomposetheproblemintoaseriesofchoiceswhereat eachstagethereareanumberofoptionstochoosefrom • Eachnodemaybeeitherpromisingornon-promising • Promisingnodesmayleadtoasolution • Non-promisingnodescannotleadto asolution • Examinepromisingnodes,creatingchildrenuntilasolutionisfound • OftendoneinaDFSmanner n-Queens • Givennqueensandannxnchessboard,positionthequeenssuch thatnotwoqueenscanattackeachother n-Queens:keyobservation • Eachrowwillonlyhaveasinglequeenonit • Theproblemofplacingnqueensonannxnboardgivesusnchoices: • • • • Whatcolumntoplacethequeeninrow1 Whatcolumntoplacethequeeninrow2 ... Whatcolumntoplacethequeeninrown • Let'slookatanexampleforn=4... n-Queens:keyobservation • Eachrowwillonlyhaveasinglequeenonit • Theproblemofplacingnqueensonannxnboardgivesusnchoices: • • • • Whatcolumntoplacethequeeninrow1 Whatcolumntoplacethequeeninrow2 ... Whatcolumntoplacethequeeninrown • Let'slookatanexampleforn=5... Hamiltoniancircuit • Visitallnodesinagraphstartingandendingatthesamevertex,and visitingnovertexmorethanonce(asidefromthestartingvertex) • Itdoesn'tmatterwhatvertexwestartat • Let'sgothroughanexample... Backtracking • Backtrackingworkswhenwehaveanumberofstepsandeachstep wehaveanumberofoptionstochoosebetween • Workswellforfindinga solution • Doesn'tworkwellforfindinganoptimalsolution Branch-and-bound • Similartobacktracking,butforsituationswherewehaveanobjective functionwearetryingtooptimize • Ateachstep,determinealowerbound(inthecaseofminimization) orupperbound(inthecaseofmaximization)ifachoiceismade • Ifwe'vealreadyseenasolutionbetterthanthis,noneedtoexamine thispath Assignmentproblem • Givenagroupofpeopleandagroupofjobs,andagridspecifying howmuchitwouldcostforeachpersontoperformthejob,findthe bestcombination Assignmentproblem • Givenagroupofpeopleandagroupofjobs,andagridspecifying howmuchitwouldcostforeachpersontoperformthejob,findthe bestcombination Job1 Job2 Job3 Job4 Aragorn 9 2 7 8 Beren 6 4 3 7 Círdan 5 8 1 8 Déagol 7 6 9 4 (0-1)Knapsackproblem • Givenasetofitemswhereeachitemhasaweightandavalue associatedwithit,pickupblahblahblahyouknowthisalready. weightlimit=10 Weight Value w/v Diamond 4 $40 10 Beryl 7 $42 6 Agate 5 $25 5 Coal 3 $12 4
© Copyright 2026 Paperzz