dancing links dancing links
play

Dancing Links Dancing Links x x Doubly linked list - PDF document

7/14/10 Dancing Links x Doubly linked list nodes have references L to left node L and A


  1. 7/14/10 ¡ Dancing ¡Links ¡ x ¡  Doubly ¡linked ¡list ¡ ¡ ¡ ¡ ¡ ¡ ¡nodes ¡have ¡references ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡L ¡to ¡left ¡node ¡L ¡and ¡ A ¡backtrack ¡data ¡structure ¡and ¡algorithm ¡ ¡ ¡ ¡ ¡ ¡ ¡R ¡to ¡right ¡node ¡ ¡ by ¡Donald ¡Knuth ¡ (wim ¡bohm ¡cs.colostate.edu) ¡ Except ¡as ¡otherwise ¡noted, ¡the ¡content ¡of ¡this ¡presentation ¡is ¡licensed ¡under ¡the ¡Creative ¡Commons ¡Attribution ¡2.5 ¡license. ¡ Dancing ¡Links ¡ Dancing ¡Links ¡ x ¡ x ¡  Doubly ¡linked ¡list ¡  Doubly ¡linked ¡list ¡  Remove ¡x ¡  Remove ¡x ¡ x ¡ x ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡R[L[x]]=R[x] ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡R[L[x]]=R[x] ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡L[R[x]]=L[x] ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡L[R[x]]=L[x] ¡  Put ¡x ¡back ¡ x ¡ ¡notice ¡that ¡x ¡still, ¡via ¡its ¡L ¡and ¡R, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡R[L[x]]=x ¡ ¡points ¡at ¡left ¡and ¡right ¡nodes, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡L[R[x]]=x ¡ and ¡thus ¡we ¡can ¡easily ¡... ¡ 1 ¡

  2. 7/14/10 ¡ Using ¡Dancing ¡Links ¡ Why ¡reverse ¡order? ¡ y ¡ x ¡  O(1) ¡put ¡x ¡back ¡operation ¡  Works ¡in ¡following ¡ BackTrack ¡scenario ¡ ¡ ¡ ¡ ¡ ¡ ¡. ¡ we ¡have ¡created ¡a ¡“space ¡to ¡be ¡searched” ¡as ¡a ¡global ¡ ¡ ¡ ¡ ¡ x ¡ y ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡doubly ¡linked ¡data ¡structure ¡ ¡ ¡ ¡ ¡ ¡ ¡. ¡ we ¡search ¡this ¡space ¡by ¡DFS, ¡selecting: ¡taking ¡out ¡ ¡ remove ¡x ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡certain ¡options ¡and ¡putting ¡them ¡back ¡in ¡ reverse ¡order ¡  What ¡does ¡not ¡work ¡ ¡ ¡ ¡ ¡ ¡. ¡ adding ¡completely ¡new ¡options ¡ ¡ ¡ ¡ ¡ ¡. ¡ putting ¡options ¡back ¡in ¡other ¡than ¡reverse ¡order ¡ ¡ ¡ ¡ ¡ ¡ ¡ Why ¡reverse ¡order? ¡ What ¡happens ¡if ¡x ¡is ¡put ¡back ¡first? ¡ x ¡ y ¡ x ¡ y ¡ x ¡ y ¡ x ¡ y ¡ remove ¡x ¡ remove ¡x ¡ x ¡ y ¡ x ¡ y ¡ remove ¡y ¡ remove ¡y ¡ 2 ¡

  3. 7/14/10 ¡ Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ What ¡happens ¡if ¡x ¡is ¡put ¡back ¡first? ¡  Given ¡a ¡matrix ¡of ¡0-­‑s ¡and ¡1-­‑s, ¡find ¡a ¡subset ¡of ¡rows ¡with ¡ x ¡ y ¡ exactly ¡one ¡1 ¡in ¡each ¡column ¡of ¡A ¡  Backtrack ¡approach ¡ remove ¡y ¡  Pick ¡a ¡column ¡c, ¡pick ¡a ¡row ¡r ¡with ¡1 ¡in ¡c, ¡remove ¡columns ¡c ¡ and ¡j ¡with ¡1 ¡in ¡r ¡and ¡rows ¡with ¡a ¡1 ¡coinciding ¡with ¡1-­‑s ¡in ¡r ¡ a b c d e f g reducing the problem 1 0 0 1 0 1 1 0 eg c in row 1 x ¡ y ¡ 2 1 0 0 1 0 0 1 selects subset {c,e,f} put ¡back ¡x ¡ 3 0 1 1 0 0 1 0 so remove columns c,e,f x ¡points ¡at ¡y ¡ 4 1 0 0 1 0 0 0 and row 1 and row 3 and ¡y ¡at ¡x, ¡ ¡ 5 0 1 0 0 0 0 1 because it overlaps with row 1 but ¡y ¡is ¡not ¡in ¡the ¡chain!! ¡ Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ ¡ ¡Backtrack ¡approach ¡ ¡ ¡Backtrack ¡approach ¡  Pick ¡column ¡c, ¡ ¡row ¡1 ¡ ¡subset ¡{c,e,f} ¡  Pick ¡column ¡c, ¡ ¡row ¡1 ¡ ¡{c,e,f} ¡ ¡rows ¡1,3 ¡and ¡cols ¡c,e,f ¡disappear ¡  ¡which ¡rows ¡and ¡cols ¡disappear? ¡ ¡ ¡ ¡ ¡Pick ¡column ¡a, ¡row ¡2 ¡ ¡{a,d,g} ¡which ¡rows, ¡cols ¡disappear? ¡ a b c d e f g a b d g 1 0 0 1 0 1 1 0 2 1 0 0 1 0 0 1 2 1 0 1 1 3 0 1 1 0 0 1 0 4 1 0 0 1 0 0 0 4 1 0 1 0 5 0 1 0 0 0 0 1 5 0 1 0 1 3 ¡

  4. 7/14/10 ¡ Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ ¡ ¡Backtrack ¡approach ¡ ¡ ¡Backtrack ¡approach ¡  Pick ¡column ¡c, ¡ ¡row ¡1 ¡ ¡ ¡{c,e,f} ¡  Pick ¡column ¡c, ¡ ¡row ¡1 ¡ ¡ ¡{c,e,f} ¡ ¡ ¡ ¡ ¡Pick ¡column ¡a, ¡row ¡2 ¡ ¡ ¡{a,d,g} ¡ ¡ all ¡rows/cols ¡disappear ¡ ¡ ¡ ¡ ¡ ¡Pick ¡column ¡a, ¡row ¡2 ¡ ¡ ¡{a,d,g} ¡ ¡ all ¡rows/cols ¡disappear ¡ ¡ what is the problem? b is not covered, so backtrack Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ ¡ ¡Backtrack ¡ ¡ ¡Backtrack ¡  Pick ¡column ¡c, ¡ ¡row ¡1 ¡ ¡{c,e,f} ¡ ¡rows ¡1,3 ¡and ¡cols ¡c,e,f ¡disappear ¡  Pick ¡column ¡c, ¡ ¡row ¡1 ¡ ¡{c,e,f} ¡ ¡rows ¡1,3 ¡and ¡cols ¡c,e,f ¡disappear ¡ ¡ ¡ ¡ ¡Pick ¡column ¡a, ¡row ¡4 ¡ ¡{a,d} ¡ ¡ ¡ ¡which ¡rows, ¡cols ¡disappear ¡now? ¡ ¡ ¡ ¡ ¡Pick ¡column ¡a, ¡row ¡4 ¡ ¡{a,d} ¡ ¡ ¡ ¡rows ¡2,4 ¡cols ¡a,d ¡disappear ¡ a b d g b g 2 1 0 1 1 4 1 0 1 0 5 0 1 0 1 5 1 1 4 ¡

  5. 7/14/10 ¡ Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ Exact ¡sub-­‑set ¡/ ¡cover ¡problem ¡ ¡ ¡Backtrack ¡ ¡ ¡Subsets ¡/ ¡rows ¡ ¡ 1,4,5 ¡ ¡ ¡provide ¡exact ¡cover! ¡  Pick ¡column ¡c, ¡ ¡row ¡1 ¡ ¡{c,e,f} ¡ ¡rows ¡1,3 ¡and ¡cols ¡c,e,f ¡disappear ¡ ¡ ¡ ¡ ¡Pick ¡column ¡a, ¡row ¡4 ¡ ¡{a,d} ¡ ¡ ¡ ¡rows ¡2,4 ¡cols ¡a,d ¡ ¡ a b c d e f g ¡ ¡ ¡ ¡Pick ¡column ¡b, ¡row ¡5 ¡ ¡{b,g} ¡ ¡ ¡ ¡row ¡5 ¡disappears ¡ 1 0 0 1 0 1 1 0 2 1 0 0 1 0 0 1 ¡ ¡ ¡ ¡ ¡ Exact ¡cover ¡accomplished! ¡ ¡ 3 0 1 1 0 0 1 0 ¡ ¡ ¡ ¡ ¡matrix ¡is ¡empty ¡ 4 1 0 0 1 0 0 0 5 0 1 0 0 0 0 1 Exact ¡set ¡cover ¡ Pentominoes ¡  A ¡pentomino ¡is ¡a ¡size ¡5 ¡n-­‑omino ¡composed ¡  The ¡columns: ¡ elements ¡of ¡a ¡universe ¡ of ¡n ¡congruent ¡squares ¡connected ¡  The ¡rows: ¡ subsets ¡of ¡elements ¡in ¡the ¡universe ¡ orthogonally ¡by ¡side ¡(not ¡point ¡wise) ¡  Find ¡a ¡set ¡of ¡subsets ¡that ¡has ¡each ¡element ¡exactly ¡once ¡  Union ¡of ¡the ¡set ¡is ¡the ¡Universe ¡  Intersection ¡of ¡any ¡two ¡subsets ¡is ¡empty ¡  There ¡are ¡12 ¡different ¡pentominoes ¡when ¡  Finding ¡an ¡exact ¡cover ¡is ¡“tough” ¡ rotation, ¡and ¡mirroring ¡are ¡allowed ¡  NP-­‑Complete ¡e.g. ¡when ¡each ¡subset ¡has ¡three ¡elements ¡  Great ¡candidate ¡for ¡backtrack ¡search ¡  There ¡are ¡18 ¡pentominoes ¡when ¡only ¡  Representation: ¡ row ¡column ¡doubly ¡linked ¡sparse ¡matrix ¡ rotation ¡is ¡allowed ¡ ¡ ¡ ¡ ¡only ¡containing ¡1-­‑s ¡  Use ¡dancing ¡links ¡to ¡remove ¡and ¡put ¡back ¡candidates ¡ 5 ¡

  6. 7/14/10 ¡ The ¡12 ¡pentominoes ¡ Early ¡implementaKon ¡  Dana ¡Scott ¡wrote ¡a ¡backtrack ¡program ¡in ¡ 1958 ¡for ¡the ¡ Maniac ¡(4000 ¡instructions ¡/ ¡ sec) ¡ tiling ¡a ¡chessboard ¡with ¡a ¡2x2 ¡hole ¡in ¡ the ¡middle ¡with ¡the ¡12 ¡pentominoes, ¡using ¡ F ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡I ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡L ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡N ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡P ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡T ¡ each ¡pentomino ¡exactly ¡once ¡  The ¡code ¡ran ¡in ¡~3.5 ¡hours: ¡(50 ¡million ¡ instructions) ¡ U ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡V ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡W ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡X ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Y ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Z ¡ ¡ ¡ ¡ ¡ ¡ ¡ Board ¡shapes ¡  Tiling ¡different ¡board ¡shapes ¡  Chessboard ¡with ¡2x2 ¡hole ¡in ¡middle ¡ ¡ ¡ ¡ ¡or ¡with ¡4 ¡holes ¡in ¡arbitrary ¡places ¡  Rectangles ¡ ¡  6 ¡x ¡10 ¡  5 ¡x ¡12 ¡  4 ¡x ¡15 ¡ One ¡of ¡the ¡65 ¡solutions ¡  3 ¡x ¡20 ¡  3D ¡ ¡boxes ¡ 6 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend