CSE326:DataStructures Lecture#21 OneLastGasp BartNiswonger - - PDF document

cse 326 data structures lecture 21 one last gasp
SMART_READER_LITE
LIVE PREVIEW

CSE326:DataStructures Lecture#21 OneLastGasp BartNiswonger - - PDF document

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


slide-1
SLIDE 1

1

CSE326:DataStructures Lecture#21 OneLastGasp

BartNiswonger SummerQuarter2001

Today’sOutline

  • AlgorithmDesign(fromFriday)

– DynamicProgramming – Randomized – Backtracking

  • “Advanced”DataStructures
slide-2
SLIDE 2

2

TreapDictionaryDataStructure

  • Treaps havethe

binarysearchtree

– binarytreeproperty – searchtreeproperty

  • Treapsalsohavethe

heap-orderproperty!

– randomlyassigned priorities 15 12 10 30 9 15 7 8 4 18 6 7 2 9 heapinyellow;searchtreeinblue

priority key

Legend:

Tree+Heap…WhyBother?

Insertdatainsortedorderintoatreap; whatshapetreecomesout?

6 7 insert(7) 6 7 insert(8) 7 8 6 7 insert(9) 7 8 2 9 6 7 insert(12) 7 8 2 9 15 12

priority key

Legend:

slide-3
SLIDE 3

3

TreapInsert

  • Choosearandompriority
  • InsertasinnormalBST
  • Rotateupuntilheaporderisrestored

6 7 insert(15) 7 8 2 9 15 12 6 7 7 8 2 9 15 12 9 15 6 7 7 8 2 9 9 15 15 12

TreapDelete

  • Findthekey
  • Increaseitsvalueto
  • Rotateittothefringe
  • Snipitoff

delete(9) 6 7 7 8 2 9 9 15 15 12 7 8 6 7

  • 9

9 15 15 12 7 8 6 7 9 15

  • 9

15 12 7 8 6 7 9 15 15 12

  • 9

7 8 6 7 9 15 15 12

slide-4
SLIDE 4

4

TreapSummary

  • ImplementsDictionaryADT

– insertinexpectedO(logn)time – deleteinexpectedO(logn)time – findinexpectedO(logn)time

  • Memoryuse

– O(1)pernode – aboutthecostofAVLtrees

  • Complexity?

Multi-DSearchADT

  • Dictionaryoperations

– create – destroy – find – insert – delete – rangequeries

  • Eachitemhask keysforak-dimensional

searchtree

  • Searchescanbeperformedonone,some,or

allthekeysoronrangesofthekeys

9,1 3,6 4,2 5,7 8,2 1,9 4,4 8,4 2,5 5,2

slide-5
SLIDE 5

5

ApplicationsofMulti-DSearch

  • Astronomy(simulationofgalaxies)- 3

dimensions

  • Proteinfoldinginmolecularbiology- 3

dimensions

  • Lossydatacompression- 4to64dimensions
  • Imageprocessing- 2dimensions
  • Graphics- 2or3dimensions
  • Animation- 3to4dimensions
  • Geographicaldatabases- 2or3dimensions
  • Websearching- 200ormoredimensions

RangeQuery

Arangequery isasearchinadictionary inwhichtheexactkeymaynotbe entirelyspecified. Rangequeriesaretheprimaryinterface withmulti-Ddatastructures.

slide-6
SLIDE 6

6

RangeQuery:TwoDimensions

  • Searchforitemsbased
  • njustonekey
  • Searchforitemsbased
  • nrangesforallkeys
  • Searchforitemsbased
  • nafunctionofseveral

keys:e.g.,acircular rangequery x

RangeQueryingin1-D

Findeverythingintherectangle…

slide-7
SLIDE 7

7 x

RangeQueryingin1-D:BST

Findeverythingintherectangle… x y

1-DRangeQueryingin2-D

slide-8
SLIDE 8

8 x y

2-DRangeQueryingin2-D k-DTrees

  • Splitonthenextdimensionateach

succeedinglevel

  • Ifbuildinginbatch,choosethemedian

alongthecurrentdimensionateachlevel

– guaranteeslogarithmicheightandbalanced tree

  • Ingeneral,addasinaBST

k-Dtreenode dimension left right keys value Thedimensionthat thisnodesplitson

slide-9
SLIDE 9

9 x y

Buildinga2-DTree(1/4)

x y

Buildinga2-DTree(2/4)

slide-10
SLIDE 10

10 x y

Buildinga2-DTree(3/4)

x y

Buildinga2-DTree(4/4)

slide-11
SLIDE 11

11

k-DTree

m f k g h i e c j d l a b

a d l c b e h f m j g i k x y

2-DRangeQueryingin2-DTrees

Searcheverypartitionthatintersectstherectangle. Checkwhethereachnode(includingleaves)fallsintotherange.

slide-12
SLIDE 12

12 x y

OtherShapesforRangeQuerying

Searcheverypartitionthatintersectstheshape(circle). Checkwhethereachnode(includingleaves)fallsintotheshape.

Findinak-DTree

find(<x1,x2,…,xk>,root)

findsthenodewhichhasthe givensetofkeysinitor returnsnull ifthereisnosuch node

Node*&find(const keyVector &keys, Node*&root){ intdim=root->dimension; if(root==NULL) returnroot; elseif(root->keys==keys) returnroot; elseif(keys[dim]<root->keys[dim]) returnfind(keys,root->left); else returnfind(keys,root->right); }

runtime:

slide-13
SLIDE 13

13

k-DTreesCanSuck

(butnotwhenbuiltinbatch!)

insert(<5,0>) insert(<6,9>) insert(<9,3>) insert(<6,5>) insert(<7,7>) insert(<8,6>)

6,9 5,0 6,5 9,3 8,6 7,7 suckfactor:

FindExample

find(<3,6>) find(<0,10>) 5,7 8,2 1,9 4,4 8,4 2,5 5,2 9,1 3,6 4,2

slide-14
SLIDE 14

14

QuadTrees

  • Splitonall (two)dimensionsateachlevel
  • Splitkeyspaceintoequalsizepartitions

(quadrants)

  • Addanewnodebyaddingtoaleaf,and,ifthe

leafisalreadyoccupied,splituntilonlyonenode perleaf

quadtreenode Quadrants:

0,1 1,1 0,0 1,0

quadrant 0,01,00,11,1 keys value Center x y Center: x y

BuildingaQuadTree(1/5)

slide-15
SLIDE 15

15 x y

BuildingaQuadTree(2/5)

x y

BuildingaQuadTree(3/5)

slide-16
SLIDE 16

16 x y

BuildingaQuadTree(4/5)

x y

BuildingaQuadTree(5/5)

slide-17
SLIDE 17

17

QuadTreeExample

a g b e f d c g a f e d c b

2-DRangeQueryinginQuadTrees

x y

slide-18
SLIDE 18

18

Node*&find(Keyx,Keyy,Node*&root){ if(root==NULL) returnroot;//Emptytree if(root->isLeaf) returnroot;//Keymaynotactuallybe here int quad= getQuadrant(x,y,root); returnfind(x,y,root->quadrants[quad]); }

FindinaQuadTree

find(<x,y>,root) findsthenodewhichhas

thegivenpairofkeysinitorreturnsquadrant wherethepointshouldbeifthereisnosuch node

runtime:

Compares againstcenter; alwaysmakes thesamechoice

  • nties.

QuadTreesCanSuck

b a suckfactor:

slide-19
SLIDE 19

19

FindExample

a g b e f d c g a f e d c b find(<10,2>)(i.e.,c) find(<5,6>)(i.e.,d)

InsertExample

g g g a insert(<10,7>,x)

… …

a g b e f d c x g x

  • Findthespotwherethenodeshouldgo.
  • Ifthespaceisunoccupied,insertthenode.
  • Ifitisoccupied,splituntiltheexistingnode

separatesfromthenewone.

slide-20
SLIDE 20

20

DeleteExample

a g b e f d c g a f e d c b delete(<10,2>)(i.e.,c)

  • Findanddeletethenode.
  • Ifitsparenthasjustone

child,deleteit.

  • Propagate!

NearestNeighborSearch

g a f e d c b getNearestNeighbor(<1,4>) g b f d c

  • Findanearbynode(doafind).
  • Doacircularrangequery.
  • Asyougetresults,tightenthecircle.
  • Continueuntilnoclosernodeinquery.

a e Workson k-DTrees,too!

slide-21
SLIDE 21

21

QuadTreesvs.k-DTrees

  • k-DTrees

– Densitybalancedtrees – NumberofnodesisO(n)wheren isthenumberofpoints – HeightofthetreeisO(logn)withbatchinsertion – Supportsinsert,find,nearestneighbor,rangequeries

  • QuadTrees

– NumberofnodesisO(n(1+log(/n)))wheren isthe numberofpointsand istheratioofthewidth(orheight)

  • fthekeyspaceandthesmallestdistancebetweentwo

points – HeightofthetreeisO(logn+log) – Supportsinsert,delete,find,nearestneighbor,range queries

ToDo

  • ProjectIV

– Packageupyourexecutableandturnitin!

  • FinishreadingChapter12
  • Studyforthefinal!
slide-22
SLIDE 22

22

ComingUp

  • CourseDiscussion
  • Final– Friday,thisweek!