61b midterm review2

32
7/21/2019 61B Midterm Review2 http://slidepdf.com/reader/full/61b-midterm-review2 1/32 CSM Midterm Review

Upload: karen-li

Post on 05-Mar-2016

229 views

Category:

Documents


0 download

DESCRIPTION

PDF For reference for Data Structures

TRANSCRIPT

Page 1: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 1/32

CSM Midterm Review

Page 2: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 2/32

1. Hashing

2. Heaps3. Sorting

Page 3: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 3/32

What is a Hash Code?

A method which returns an integer “hashcode

!o""ows the hashcode “contract

#eterminstic $a.hashCode$% && a.hashCode$%%

a.e'ua"s$(% &) a.hashCode$% && (.hashCode$%

*his is +,* (idirectiona"- une'ua" o( ects ma/ have the same hashcode0

*his imp"ies i /ou override the e'ua"s method /ou M S* override hashCode as we"

What is it good or?

Page 4: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 4/32

Hashmaps

We want a dictionar/ "i4e rom 5/thon. 6n other words we want 4e/ va"ue p

7e/s and va"ues are (oth o( ects0

We want this data structure to have ,$1% "oo4up and ,$1% add time

What other data structure has ,$1% "oo4up and add time?

Arra/s0

8ut arra/ 4e/s are "imited to integers

What "et9s us turn ,( ects into integers?

Page 5: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 5/32

Hashmaps: *he Recipe

*he interna" data structure is an arra/

get$7 4e/%: arra/;4e/.hashCode$% < arra/."ength= gives /ou the va"ue $reca""rom wor4sheet wh/ we mod%

Remem(er0 #i erent o( ects can have the same hashcode- this is ca""ed a co""ision

So"ution: “(uc4ets. >ach entr/ o arra/ is a in4ed ist o >ntr/ o( ects that store 4e

va"ue pairs so we can ind the right 4e/ va"ue pairing

add$7 4e/ @ va"ue%: add new >ntr/$4e/ va"ue% to end o "in4ed "ist atarr;4e/.hashCode$% < arr."ength=

Page 6: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 6/32

Hashmaps: *he runtime

We said ear"ier that we wanted ,$1% "oo4up and ,$1% add

8ut "oo4up and add ta4e ,$m% where m is the (uc4et si e0

Strong c"aim: m does not sca"e with the num(er o e"ements in hashmap

Stronger c"aim: m is constant

So am 6 "/ing to /ou?

Page 7: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 7/32

Hashmap: get$% runtime

We need 1 modi ication to ma4e get$% constant time0

de ine varia("e ina" int ,A#B!AC*,R. et9s sa/ it9s 2.

When adding i map.si e$% D arr."ength ) ,A#B!AC*,R we resi e0

*his means i our (uc4ets to e"ements ratio is too (ig we reduce the ratio

5rocedure: Ma4e new arra/ o dou("e si e- go through each (uc4et and “rehash it $ ind th4e/.hashCode$% < arr."ength and p"ace it in the right (uc4et%

*his means our maEimum (uc4et si e shou"d a"wa/s (e around 2. 0 $Ma4ingsome strong assumptions a(out the distri(ution o our hashcodes%

8ut resi e ta4es ,$n%0 *hen add ta4es ,$n%0 69m a "iar.

Page 8: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 8/32

Hashmap: Amorti ed Runtime

Worst case scenario or add$% is ,$n%. H,W>@>R it “pa/s itse" o .

7eep in mind that when we resi e we reduce the (uc4et si e- add shou"d a"sta4e ,$m% ignoring resi es.

So i we resi e we ta4e ,$n% to ma4e an arra/ o si e 2n.

*hat means the neEt ,A#B!AC*,R F n adds wi"" a"so (e in constant time0

So we do an ,$n% a"gorithm once and ma4e the neEt n ca""s to the method ,*hen it wou"d (e the same as i we did dou("e the computations per ca"" tadd $which is sti"" ,$1%0 So ,$n% D n G ,$1% “amorti ed runtime

#on9t worr/ i this seems "i4e hoodoo magic0 6t9s pro(a("/ not going to (eeatured heavi"/ on the midterm.

Page 9: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 9/32

Hashmap: Misce""aneous

7e/ assumption: >"ements are distri(uted even"/ among (uc4ets

*his re"ies on good hash codes0

We t/pica""/ want 4e/s to (e immuta("e $unchanging% since i we changeproperties we ris4 changing the hash code and there ore (eing una("e toretrieve our e"ements. $Strings ma4e antastic 4e/s or this reason%

Some things don9t hash we""0 !or eEamp"e #+A strings wou"d have on"/ Aand as characters so the/9"" pro(a("/ have a "ot o simi"ar hash codes0 6nthis case we get ,$n% search time since ever/thing ends in the same (uc4e

HashCode$% gets ca""ed a "ot- ma4e sure it runs ast or /ou "ose the speed ohashmap

Page 10: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 10/32

Hashmap: 7e/ ta4eawa/s

HashMaps are ust g"ori ied Arra/ ists0

And Arra/ ists are ust g"ori ied arra/sI

7e/ di erence: Arra/sDArra/ ists use integers as 4e/s. HashMaps can$theoretica""/% use an/ o( ect as a 4e/.

6 /ou understand the runtime ana"/sis o hashmaps /ou pro(a("/ understandever/thing a(out hashmaps

Page 11: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 11/32

Heaps

6 want to get the maEimum $or minimum% e"ement rom a group o e"emenrea""/ rea""/ 'uic4"/. How?

A heap is a (inar/ tree with the condition that ever/ parent is greater than ore'ua" to its chi"d $"ess than or e'ua" to or a maE heap%

Page 12: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 12/32

Heaps: Representation

We can ma4e heaps more easi"/ using arra/s.

arr;J= is the root node

*he "e t chi"d o arr;i= is arr;2i K 1= and the right chi"d is arr;2i K 2= $conever/ i has uni'ue chi"dren0%

& ;2 13 1L 3=

Page 13: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 13/32

Heaps: Heapi /

What does it mean?

6 an/ chi"d o a node at A;i= is (igger than A;i= then swap it with A;i=

*his ensures we 4eep the preconditionwe set $that parents are a"wa/s (igger than chi"dren%

Page 14: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 14/32

Heaps: Add removeMaE

Add: Add the new e"ement to the neEt avai"a("e chi"d at the "owest "eve" $"e

most position at "owest "eve"%

*hen ust heapi /

removeMaE: Return A;J=. 8ut we need to remove it too so: Set A;J= to the chat the "owest "eve" $where /ou wou"d add%

*hen ust heapi /

Page 15: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 15/32

Heaps: Runtime

et + (e the num(er o e"ements in our heap

6 /ou see the heapi / a"gorithm /ou can see that we on"/ need to ma4e swaps down one ch$so the parent is (igger%. 6 we ma4e swaps unti" we hit the (ottom then we have madswaps where H is the height o the (inar/ tree which represents our heap so ,$H%

What is H with respect to +? We"" at the root node we cou"d potentia""/ visit a"" + e"emewe go down a chi"d we can visit +D2. *hen +DNI So how man/ times do we have to+ (/ 2 to get 1 $down to a "ea node%?

Reca"" that i /ou have some num(er 2Om and /ou divide (/ 2 m times $i.e. 2Om%So how do we get m?

Runtime o heapi / ,$"og n%

+ote that remove maE and add are constant a"gorithms $ or an arra/

representation% o""owed (/ heapi / so the/ must run in ,$"og n% as we""

Page 16: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 16/32

Heaps: 7e/ *a4eawa/s

Heaps are ust g"ori ied (inar/ trees.

When /ou get a treeP"i4e structure the height o the structure is "og n.$Assuming the tree is we"" (a"anced%

Heaps can (e represented as arra/s. *his saves a "ot o grie

Heapi / is the 4e/ a"gorithm /ou shou"d 4now or heaps0

Page 17: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 17/32

Sorting: Se"ection Sort

A"gorithm

Assume a;J= to a;i P 1= is sorted $(asica""/ ever/thing /ou9ve iterated through (e ore%

!ind minimum e"ement rom a;i= to a;a."ength P 1= and swap it with a;i=

Runtime:

!inding minimum e"ement o a "ist:

,$n%

How man/ times do we ind the min?

,$n%

,$nO2%

Page 18: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 18/32

Sorting: 6nsertion Sort

A"gorithm:

Assume a;J= to a;i P 1= is sorted

et & i- whi"e a; = Q a; P 1= swap a; = and a; P 1= and decrement

Runtime:

8est case:

ist is a"read/ sorted

We do no swaps $(ecause o whi"e "oop condition%

So we ust ma4e n comparison to see the "ist is sorted

,$n%

Page 19: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 19/32

Heapsort

A"gorithm

*hrow ever/thing into a minPheap $assuming we9re sorting sma""est irst%

!i"" an arra/ with heap.removeMin$% unti" heap is empt/

Runtime

How "ong does it ta4e to add to a heap?

,$"og n%

How man/ times do we add?

,$n%

So step one ta4es ,$n "og n%

Page 20: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 20/32

Merge Sort

A"gorithm:

Recursive"/ divide "ist in ha" unti" we get "ists o si e 1

Remem(er: ists o si e 1 are $trivia""/% sorted

Merge and sort each "ist going up the recursive tree

Remem(er: merge and sort ta4es ,$n% i /ou have a sorted "ist

Page 21: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 21/32

Merge Sort: >Eamp"e

Page 22: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 22/32

Merge Sort: Runtime

We 4ept dividing the "ists in 2 unti" we get a tree structure in our recursion.

Sounds ami"iar? What9s the height o that tree?,$"og n%

How much does merge and sort cost at each "eve" o the tree?

We"" at each "eve" o the tree there are n e"ements

merge and sort costs ,$n% so i there are n e"ementsI

,$n%

,$n "og n%

Page 23: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 23/32

uic4sort

A"gorithm:

Choose some e"ement that we ca"" the pivot p

Start pointers at (oth ends o the "ist $i & J- & arr."ength P 1%

6ncrease i unti" arr;i= ) p

#ecrease unti" arr; = Q p

swap arr;i= arr; =

Continue unti" i ) $or ever/thing (igger than pivot is to right and sma""er than pivot is to "e

Repeat or "e t ha" and right ha"

Page 24: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 24/32

uic4sort: >Eamp"e

*his is ust an eEamp"e or one su("ist

Page 25: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 25/32

uic4sort: Runtime

8est case:

We sp"it "ist even"/ in ha" ever/ time

Moving ever/thing (igger and sma""er than pivot ta4es ,$n%

'uic4sort (rea4s things down into a tree so height is ,$"og n%

,$n "og n%

Worst Case:

5ivot is (iggest or sma""est e"ement o "ist ever/ time $there ore we get 1 recursive case on P 1%

*hen we recurse n times instead o "og n times0

Sti"" have to go through "ist to see where pivot goes. So n comparisons then nP1 nP2 ...

Page 26: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 26/32

uic4sort: Wh/?

uic4sort has worst case ,$nO2%0 Wh/ don9t we ust use heapsort or merge

which are guaranteed ,$n "og n%

We"" 'uic4sort is genera""/ aster in practice.

We can a"so ma4e it ver/ un"i4e"/ to run in ,$nO2%

Sa/ we choose a random e"ement as our pivot. What9s the pro(a(i"it/ we choose the minimum

e"ement?

1 D n

*hen we get 1D$n P 1% 1D$n P 2% I 1D1. $since i we pic4 minimum e"ement we P 1 then n P 2 and so orth%

Reca"" rom pro(a(i"it/ that the pro(a(i"it/ o two di erent things happening is their

F F

Page 27: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 27/32

8uc4et SortDCounting Sort

A"gorithm:

Sa/ we have an arra/ o distinct e"ements E J E1 I Em. $m is iEed%

*hen ust create an arra/ where we arr;i= is how man/ times we see E i $this is co

!or (uc4et sort create “(uc4ets (uc4et i has a"" the E i9s

8uc4ets can (e "in4ed "ists $or arra/"ists%

8uc4ets can a"so (e /ou ust 4eep the indeE where E i starts in the arra

*hen we recreate the "ist

Runtime:

Creating a"" the (uc4ets or counts ta4es ,$n%

Page 28: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 28/32

8uc4et SortDCounting Sort: *H6S 6S AMA 6+ 00

We got an ,$n% sort0 +ot even 'uic4sort cou"d do it0 And 'uic4 is in the name

'uic4sort0Wh/ don9t we a"wa/s use it?

7eep in mind that we have to predetermine the (uc4ets or what e"ements we9re counting

So this on"/ wor4s i we 4now eEact"/ what we wi"" (e sorting

>Eamp"es:

“>nums $sort +orth South >ast West directions%

imited Range integers $i.e. high schoo" student ages are (etween 13 and 1T%

8oo"eans

What won9t this wor4 on?

Page 29: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 29/32

RadiE Sort

What i we cou"d (uc4et sort somehow?

A"gorithm:

oo4 at a digit and (uc4et sort $remem(er digits go rom JPT%

*hen "oo4 at neEt (igger digit and (uc4et sort again

4e/ point: (uc4et sort must (e sta("e- that is to sa/ i two things go to the same (uc4ethe/ maintain ordering rom (e ore

7eep going unti" we9re done with (iggest digit

Runtime:

8uc4et sort ta4es ,$n%. We run (uc4et sort m times where m is the num(er o digits

, nm

Page 30: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 30/32

RadiE Sort: Wh/ not a"wa/s do it?

RadiE sort doesn9t wor4 or ever/thing :$

We need to (e a("e to (rea4 down our sorting order into sma""er pro("ems $"i4e sorting (/ dig

So we need some sort o “radiE or which the sorting order ma4es sense.

What does radiE sort wor4 on?

Strings $each character is a “radiE %

An/ num(er $int "oat dou("e (/te etc.%

What doesn9t it wor4 on?

An/thing that we want to sort on comparisons

RadiE sort is ver/ ast though and usua""/ we want to sort on some sort o

Page 31: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 31/32

Sorts: Comparison

Sort 8estCaseRuntime

WorstCaseRuntime

SpaceComp"eEit/

Se"ectionSort

,$nO2%

,$nO2%

,$n%

6nsertionSort

,$nO2%

,$nO2%

,$1%

Heap Sort ,$n"og n%

,$n"og n%

,$1%

MergeSort

,$n"og n%

,$n"og n%

,$n%

uic4sort ,$n

"og n%

,$nO2

%

,$"og

n%

Page 32: 61B Midterm Review2

7/21/2019 61B Midterm Review2

http://slidepdf.com/reader/full/61b-midterm-review2 32/32

Sorts: 7e/ *a4eawa/s

6 recommend having imp"emented each sort at "east once $eEcept radiE sort%

Uou might want to ta4e radiE sort code or pseudoPcode into eEam with /ou though

8e com orta("e with the idea o modi /ing sorts and running sorts (/ hand

nderstand runtime ana"/sis so i /ou modi / the sort or are given a conditiona(out the "ist $e.g. insertion sort on an a"most sorted "ist% /ou 4now how

runtime is changed

RadiE sort is coo" (ut hasn9t t/pica""/ (een tested on too eEtensive"/ in the pas$this ma/ not necessari"/ re "ect this /ear however%