cse 326 data structures lecture 21 one last gasp
play

CSE326:DataStructures Lecture#21 OneLastGasp BartNiswonger - PDF document

CSE326:DataStructures Lecture#21 OneLastGasp BartNiswonger SummerQuarter2001 TodaysOutline AlgorithmDesign(fromFriday) DynamicProgramming Randomized Backtracking


  1. CSE�326:�Data�Structures Lecture�#21 One�Last�Gasp Bart�Niswonger Summer�Quarter�2001 Today’s�Outline • Algorithm�Design�(from�Friday) – Dynamic�Programming – Randomized – Backtracking • “Advanced”�Data�Structures 1

  2. Treap�Dictionary�Data�Structure heap�in�yellow;�search�tree�in�blue • Treaps have�the� binary�search�tree 2 9 – binary�tree�property – search�tree�property 6 4 • Treaps�also�have�the� 7 18 heap-order�property! – randomly�assigned� 7 9 10 priorities 8 15 30 Legend: 15 priority key 12 Tree�+�Heap…�Why�Bother? Insert�data�in�sorted�order�into�a�treap;� what�shape�tree�comes�out? insert(7) insert(8) insert(9) insert(12) 6 6 2 2 7 7 9 9 7 6 6 15 8 7 7 12 Legend: 7 7 priority key 8 8 2

  3. Treap�Insert • Choose�a�random�priority • Insert�as�in�normal�BST • Rotate�up�until�heap�order�is�restored insert(15) 2 2 2 9 9 9 6 15 6 15 6 9 7 12 7 12 7 15 7 7 9 7 15 8 8 15 8 12 Treap�Delete 6 • Find�the�key 2 �� 7 9 • Increase�its�value�to� 7 � 6 9 � 8 9 7 15 • Rotate�it�to�the�fringe 9 7 15 • Snip�it�off 15 8 12 6 6 15 7 7 6 12 7 9 7 9 7 8 15 8 15 7 9 15 � 8 15 9 12 15 15 � delete(9) 12 12 9 3

  4. Treap�Summary • Implements�Dictionary�ADT – insert�in�expected�O(log�n)�time – delete�in�expected�O(log�n)�time� – find�in�expected�O(log�n)�time • Memory�use – O(1)�per�node – about�the�cost�of�AVL�trees • Complexity? Multi-D�Search�ADT • Dictionary�operations 5,2 – create 2,5 8,4 – destroy – find 4,4 1,9 8,2 5,7 – insert – delete 4,2 3,6 9,1 – range�queries • Each�item�has� k keys�for�a� k -dimensional� search�tree • Searches�can�be�performed�on�one,�some,�or� all�the�keys�or�on�ranges�of�the�keys 4

  5. Applications�of�Multi-D�Search • Astronomy�(simulation�of�galaxies)�- 3� dimensions • Protein�folding�in�molecular�biology�- 3� dimensions • Lossy�data�compression�- 4�to�64�dimensions • Image�processing�- 2�dimensions • Graphics�- 2�or�3�dimensions • Animation�- 3�to�4�dimensions • Geographical�databases�- 2�or�3�dimensions • Web�searching�- 200�or�more�dimensions Range�Query A� range�query is�a�search�in�a�dictionary� in�which�the�exact�key�may�not�be� entirely�specified. Range�queries�are�the�primary�interface� with�multi-D�data�structures. 5

  6. Range�Query:�Two�Dimensions • Search�for�items�based� on� just�one�key • Search�for�items�based� on� ranges�for�all�keys • Search�for�items�based� on�a�function�of�several� keys:�e.g.,�a� circular� range�query Range�Querying�in�1-D Find�everything�in�the�rectangle…� x 6

  7. Range�Querying�in�1-D:�BST Find�everything�in�the�rectangle…� x 1-D�Range�Querying�in�2-D y x 7

  8. 2-D�Range�Querying�in�2-D y x k -D�Trees • Split�on�the�next�dimension�at�each� succeeding�level • If�building�in�batch,�choose�the�median� along�the�current�dimension�at�each�level – guarantees�logarithmic�height�and�balanced� tree k -D�tree�node • In�general,�add�as�in�a�BST keys value dimension The�dimension�that this�node�splits�on left right 8

  9. Building�a�2-D�Tree�(1/4) y x Building�a�2-D�Tree�(2/4) y x 9

  10. Building�a�2-D�Tree�(3/4) y x Building�a�2-D�Tree�(4/4) y x 10

  11. k -D�Tree a c b d e e f g h i i h j k l m g k f m a b j c d l 2-D�Range�Querying�in�2-D�Trees y Search�every�partition�that�intersects�the�rectangle.� x Check�whether�each�node�(including�leaves)�falls�into�the�range. 11

  12. Other�Shapes�for�Range�Querying y Search�every�partition�that�intersects�the�shape�(circle).� x Check�whether�each�node�(including�leaves)�falls�into�the�shape. Find�in�a� k -D�Tree Node�*&�find(const keyVector &�keys,� Node�*&�root)�{ find(<x 1 ,x 2 ,�…,�x k >,�root) int�dim�=�root->dimension; finds�the�node�which�has�the� if�(root�==�NULL) given�set�of�keys�in�it�or� return�root; returns� null if�there�is�no�such� else�if�(root->keys�==�keys) node return�root; else�if�(keys[dim]�<�root->keys[dim]) return�find(keys,�root->left); else return�find(keys,�root->right); } runtime: 12

  13. k -D�Trees�Can�Suck (but�not�when�built�in�batch!) 5,0 insert(<5,0>) insert(<6,9>) 6,9 insert(<9,3>) 9,3 insert(<6,5>) insert(<7,7>) 6,5 insert(<8,6>) 7,7 suck�factor: 8,6 Find�Example 5,2 find(<3,6>) find(<0,10>) 2,5 8,4 4,4 1,9 8,2 5,7 4,2 3,6 9,1 13

  14. Quad�Trees • Split�on� all (two)�dimensions�at�each�level • Split�key�space�into�equal�size�partitions� (quadrants) • Add�a�new�node�by�adding�to�a�leaf,�and,�if�the� leaf�is�already�occupied,�split�until�only�one�node� per�leaf quadrant quad�tree�node 0,1 1,1 keys value x y Center: 0,0 1,0 Quadrants: 0,01,00,11,1 Center Building�a�Quad�Tree�(1/5) y x 14

  15. Building�a�Quad�Tree�(2/5) y x Building�a�Quad�Tree�(3/5) y x 15

  16. Building�a�Quad�Tree�(4/5) y x Building�a�Quad�Tree�(5/5) y x 16

  17. Quad�Tree�Example a b c a g d d e f e g f b c 2-D�Range�Querying�in�Quad�Trees y x 17

  18. Find�in�a�Quad�Tree find(<x,�y>,�root) finds�the�node�which�has� the�given�pair�of�keys�in�it�or�returns�quadrant� where�the�point�should�be�if�there�is�no�such� node Node�*&�find(Key�x,�Key�y,�Node�*&�root)�{ if�(root�==�NULL) return�root;���//�Empty�tree if�(root->isLeaf) Compares� return�root;���//�Key�may�not�actually�be� against�center;� here always�makes� the�same�choice� int quad�= getQuadrant(x,�y,�root); on�ties. return�find(x,�y,�root->quadrants[quad]); } runtime: Quad�Trees�Can�Suck a b suck�factor: 18

  19. Find�Example find(<10,2>)� (i.e.,�c) find(<5,6>)�� (i.e.,�d) a b c a g d d e f e g f b c Insert�Example insert(<10,7>,x) a b c a g g g … … d e x g f • Find�the�spot�where�the�node�should�go. x g • If�the�space�is�unoccupied,�insert�the�node. • If�it�is�occupied,�split�until�the�existing�node separates�from�the�new�one. 19

  20. Delete�Example delete(<10,2>) (i.e.,�c) a b c a g d e d e f g f • Find�and�delete�the�node. b c • If�its�parent�has�just�one child,�delete�it. • Propagate! Nearest�Neighbor�Search getNearestNeighbor(<1,4>) a b c a g d e d e f g f • Find�a�nearby�node�(do�a�find). • Do�a�circular�range�query. b c • As�you�get�results,�tighten�the�circle. Works�on� • Continue�until�no�closer�node�in�query. k -D�Trees,�too! 20

  21. Quad�Trees�vs.� k -D�Trees • k -D�Trees – Density�balanced�trees – Number�of�nodes�is�O(n)�where� n is�the�number�of�points – Height�of�the�tree�is�O(log�n)� with�batch�insertion – Supports�insert,�find,�nearest�neighbor,�range�queries • Quad�Trees – Number�of�nodes�is�O(n(1+�log( � /n)))�where� n is�the� number�of�points�and� � is�the�ratio�of�the�width�(or�height)� of�the�key�space�and�the�smallest�distance�between�two� points – Height�of�the�tree�is�O(log�n�+�log� � ) – Supports�insert,�delete,�find,�nearest�neighbor,�range� queries To�Do • Project�IV – Package�up�your�executable�and�turn�it�in! • Finish�reading�Chapter�12 • Study�for�the�final! 21

  22. Coming�Up • Course�Discussion • Final�– Friday,�this�week! 22

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