comp251: mid-term review - cs.mcgill.cajeromew/comp251material/...avl trees red-black trees • bst...

97
COMP251: Mid-Term Review Jérôme Waldispühl School of Computer Science McGill University

Upload: others

Post on 22-Apr-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

COMP251:Mid-TermReviewJérôme Waldispühl

SchoolofComputerScienceMcGillUniversity

Page 2: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Overview

Lecture2 HashingLecture3 Heaps&HeapsortLecture4 BSTandAVLtreesLecture5 Red-blacktreesLecture6 DisjointsetsLecture7 Greedyalgorithms(Scheduling,Huffmancoding)Lecture8 Elementarygraphalgorithms

Lecture9 Topologicalsortandstronglyconnectedcomponents

Lecture10 MinimumSpanningTreeLecture11 SinglesourceshortestpathLecture12 BipartitegraphsLecture13 Networkflow1Lecture14 Networkflow2

Page 3: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Techniques

Page 4: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Runningtime

f:runningtime

𝛼·𝑛 :lowerboundonf

𝛽·𝑛 :upperboundonf

• Runningtimeis𝛰 (n)• Runningtimeis𝛺(n)⟹Runningtimeis𝛩(n)

n0

Page 5: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Proofs• Contradiction:Givenaproposition,assumeoppositepropositionistrue,andthenshowsthatitleadstoacontradiction.

• Cutandpaste: Usedwithgraphsandgreedyalgorithms.Oftenusedtoproveanoptimalsolutionofaproblemisbuildfromoptimalsolutionofsub-problem(Optimalsubstructure).Assumeasub-problemisnotoptimal,andreplacewithoptimalsolutiontoshowacontradiction.

• Loopinvariants: Usedtoprovethataloopstructureisdoingwhatitisintendedtodo.Youmustspecify:§ Loopinvariantproperty§ Initialization§ Maintenance§ Termination

Page 6: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

OptimalsubstructureLemmaAnysubpath ofashortestpathisashortestpath.

Proof:

Supposethispathp isashortestpathfromu tov.Thenδ(u,v)=w(p)=w(pux)+w(pxy)+w(pyv).Nowsupposethereexistsashorterpathxy.Thenw(p’xy)<w(pxy).w(pux)+w(p’xy)+w(pyv)<w(pux)+w(pxy)+w(pyv)Contradiction ofthehypothesisthatp istheshortestpath!

p’xy

u x y v

pxy

p’xy

Page 7: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Hashing

Page 8: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Resolutionbychaining• InsertiontimeinO(1)ifweinsertattheheadofthelist.• SearchtimeinO(1)timeinaverage,butnottheworstcase.

Hashfunction: h :U→ {0,1,...,m−1}

h(k1)

h(k4)

Page 9: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Openaddressing

index key1 35523 5674 23356 7997

h(282,0)=3

h(282,1)=1

h(282,2)=5

282

Note:Searchmustusethesameprobesequence.

Illustration:Wheretostorekey282?

Full!

Page 10: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Linear&Quadraticprobing

h(k, i) = h '(k)+ i( )modm

h(k, i) = h '(k)+ c1 ⋅ i+ c2 ⋅ i2( )modm

Note:tendencytocreateclusters.

Remarks:• Wemustensurethatwehaveafullpermutationof⟨

0,…,m-1⟩.• Secondaryclustering:2 distinctkeyshavethesame

hʹvalue,iftheyhavethesameprobesequence.

Linearprobing:

Quadraticprobing:

Page 11: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Trees

Page 12: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Rotations

yx

A B

C

xy

CB

A

Rightrotation

Leftrotation

Rotations:• Changetreestructure• PreservetheBSTproperty.

Page 13: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example:rightrotationaty

yx

A B

C

1 yx

A B

C

2

xy

CB

A4

xy

CB

A

3

Page 14: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

BST&Self-balancedtrees

x

|hleft-hright|≤1

26

17

30 47

38 50

NilNil Nil

Nil

Nil

Nil Nil

41

AVLtrees Red-blacktrees

• BST(usedtostorekeys)• Runningtimedependentoftheheight⟹ wetrytokeepthetreesbalanced.• AVL&Red-Blacktreesare2typesofself-balancedtrees• ChallengeistokeeptheAVLorRed-Blacktreepropertyvalidaftereachoperation.

Page 15: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

InsertRBTree– Example7

3

10 20

11 22

NilNil NilNil

Nil Nil

18

Nil8

NilNil

Page 16: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

InsertRBTree– Example7

3

10 20

11 22

NilNilNil

Nil Nil

18

Nil

Insert(T,15)

15

NilNil

8

NilNil

Page 17: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

InsertRBTree– Example7

3

10 20

11 22

NilNilNil

Nil Nil

18

Nil

Recolor10,8&11

15

NilNil

8

NilNil

Page 18: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

InsertRBTree– Example7

3

10 20

11 22

NilNilNil

Nil Nil

18

Nil

15

NilNil

8

NilNil

Rightrotateat18

Page 19: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

InsertRBTree– Example7

3

18Nil Nil

10

20

22

NilNil

Nil

Rightrotateat18(parent&childwithconflictarealigned)

11

Nil 15

NilNil

8

NilNil

Page 20: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

InsertRBTree– Example7

3

18Nil Nil

10

20

22

NilNil

Nil

Leftrotateat7

11

Nil 15

NilNil

8

NilNil

Page 21: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

InsertRBTree– Example

7

3

Nil Nil

10

Leftrotateat7

18

20

22

NilNil

Nil

11

Nil 15

NilNil

8

NilNil

Page 22: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

InsertRBTree– Example

7

3

Nil Nil

10

Recolor10&7(rootmustbeblack!)

18

20

22

NilNil

Nil

11

Nil 15

NilNil

8

NilNil

Page 23: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Case1– uncley isred

• p[p[z]](zʼsgrandparent)mustbeblack,sincezandp[z]arebothredandtherearenootherviolationsofproperty4.•Makep[z]andyblackÞ nowzandp[z]arenotbothred.Butproperty5mightnowbeviolated.•Makep[p[z]]redÞ restoresproperty5.• Thenextiterationhasp[p[z]]asthenewz(i.e.,zmovesup2levels).

z isarightchildhere.Similarstepsifz isaleftchild.

C

A D

Ba

b g

d ez

yp[z]

p[p[z]]

a

b g

d e

new zC

A D

B

Page 24: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Case2– yisblack,z isarightchild

• Leftrotatearoundp[z],p[z]andz switchrolesÞ nowzisaleftchild,andbothzandp[z]arered.• Takesusimmediatelytocase3.

C

A

Ba

b g

z

yp[z]C

B

A

a b

g(new)z

y(new)p[z]D D

δ λ δ λ

Page 25: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Case3– yisblack,z isaleftchild

•Makep[z]blackandp[p[z]]red.• Thenrightrotaterightonp[p[z]](inordertomaintainproperty4).• Nolongerhave2redsinarow.• p[z]isnowblackÞ nomoreiterations.

C

B

A

a b

g

yp[z]

z

D

B

A

a b g

C

Dδ λ

δ λ

p[p[z]]

z

p[z]

Page 26: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Greedyalgorithms

Page 27: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Activity-selectionProblem

012345678910

s6 a6

a5

a4

a3

a2

a1 a7s1

s2

s3

s4

s5

s7f1

f2

f3

f4

f5

f6

f7

i 1 2 3 4 5 6 7si 0 1 2 4 5 6 8fi 2 3 5 6 9 9 10

Activitiessortedbyfinishingtime.

Page 28: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Activity-selectionProblem

012345678910

s6 a6

a5

a4

a3

a2

a1 a7s1

s2

s3

s4

s5

s7f1

f2

f3

f4

f5

f6

f7

i 1 2 3 4 5 6 7si 0 1 2 4 5 6 8fi 2 3 5 6 9 9 10

Activitiessortedbyfinishingtime.

Page 29: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Activity-selectionProblem

012345678910

s6 a6

a5

a4

a3

a2

a1 a7s1

s2

s3

s4

s5

s7f1

f2

f3

f4

f5

f6

f7

i 1 2 3 4 5 6 7si 0 1 2 4 5 6 8fi 2 3 5 6 9 9 10

Activitiessortedbyfinishingtime.

Page 30: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Activity-selectionProblem

012345678910

s6 a6

a5

a4

a3

a2

a1 a7s1

s2

s3

s4

s5

s7f1

f2

f3

f4

f5

f6

f7

i 1 2 3 4 5 6 7si 0 1 2 4 5 6 8fi 2 3 5 6 9 9 10

Activitiessortedbyfinishingtime.

Page 31: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

GraphAlgorithms

Page 32: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

TopologicalSortWantto“sort” adirectedacyclicgraph(DAG).

B

E

D

C

A

C EDA B

ThinkoforiginalDAGasapartialorder.

Wantatotalorder thatextendsthispartialorder.

Page 33: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/

Page 34: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/

2/

Page 35: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/

2/3

E

2/3

Page 36: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/4

2/3

E

2/31/4

D

Page 37: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/4

2/3

E

2/31/4

D

5/

Page 38: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/4

2/3

E

2/31/4

D

5/

6/

Page 39: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/4

2/3

E

2/31/4

D

5/

6/7

6/7

C

Page 40: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/4

2/3

E

2/31/4

D

5/8

6/7

6/7

C

5/8

B

Page 41: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/4

2/3

E

2/31/4

D

5/8

6/7

6/7

C

5/8

B

9/

Page 42: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example1

LinkedList:

A B D

C E

1/4

2/3

E

2/31/4

D

5/8

6/7

6/7

C

5/8

B

9/10

9/10

A

Page 43: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

MinimumSpanningTrees

Page 44: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

MinimumSpanningTree(MST)

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

• Ithas|V|−1edges.• Ithasnocycles.• Itmightnotbeunique.

Page 45: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Definitions

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

cut partitionsverticesintodisjointsets,S andV – S.

S V- S

Thisedgecrosses thecut.(oneendpointisinS andtheotherisinV– S.)

Alight edgecrossingcut(maynotbeunique)

Acutrespects AifandonlyifnoedgeinAcrossesthecut.

b

a

c

e

Page 46: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

KruskalʼsAlgorithm

1. Startswitheachvertexinitsowncomponent.2. Repeatedlymergestwocomponentsintoonebychoosinga

lightedgethatconnectsthem(i.e.,alightedgecrossingthecutbetweenthem).

3. Scansthesetofedgesinmonotonicallyincreasingorderbyweight.

4. Usesadisjoint-setdatastructuretodeterminewhetheranedgeconnectsverticesindifferentcomponents.

Note:WealsocoveredthePrim’salgorithmtocalculateaMST.

Page 47: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 48: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 49: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 50: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 51: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Reject!

Page 52: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 53: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 54: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 55: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 56: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 57: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 58: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 59: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 60: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

a

b

c

e

d

f h

g

i

10

12

9

8

7

3 3

1

8

5

6

2

11

9

Page 61: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Proof:LetTbeaMSTthatincludesA.Case1: (u,v)inT.Weʼredone.Case2: (u,v)notinT.Wehavethefollowing:

u y

x

v

edgeinA

cut

WeshowedgesinT

Safeedge

Theorem1: Let(S,V-S)beanycutthatrespectsA,andlet(u,v) bealightedgecrossing(S,V-S).Then,(u,v)issafeforA.

(x,y)crossescut.LetT´ =T- {(x,y)}È {(u,v)}.Because(u,v)islightforcut,w(u,v)£ w(x,y).Thus,w(T´)=w(T)-w(x,y)+w(u,v)£w(T).Hence,T´ isalsoaMST.So,(u,v)issafeforA.

Page 62: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Singlesourceshortestpaths

Page 63: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Relaxinganedge

RELAX(u,v,w)if d[v]>d[u]+w(u,v) thend[v] ß d[u]+w(u,v)π[v]ßu

4 93 vu

4 7

Relax

4 63 vu

4 6

Page 64: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Dijkstra’s algorithm

DIJKSTRA(V, E,w,s)INIT-SINGLE-SOURCE(V,s)S ← ∅Q ← Vwhile Q ≠ ∅ do

u ← EXTRACT-MIN(Q)S ← S ∪ {u}for each vertex v ∈ Adj[u] do

RELAX(u,v,w)

Page 65: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

0

10

5

1

2

6

2 724

3s

z

x

y

t

s t y x zQ

Page 66: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

0

10

5

10

5

1

2

6

2 724

3s

z

x

y

t

y t x zQ

Page 67: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

0

6

5

9

11

10

5

1

2

6

2 724

3s

z

x

y

t

t x zQ

Page 68: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

0

6

5

8

11

10

5

1

2

6

2 724

3s

z

x

y

t

x zQ

Page 69: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

0

6

5

8

10

10

5

1

2

6

2 724

3s

z

x

y

t

zQ

Page 70: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

0

6

5

8

10

10

5

1

2

6

2 724

3s

z

x

y

t

Q

Page 71: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

0

6

5

8

10

10

5

1

2

6

2 724

3s

z

x

y

t

Page 72: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Dijkstra’s algorithm

DIJKSTRA(V, E,w,s):INIT-SINGLE-SOURCE(V,s)S ← ∅Q ← Vwhile Q ≠ ∅ do

u ← EXTRACT-MIN(Q)S ← S ∪ {u}for each vertex v∈Adj[u] do

RELAX(u,v,w)

LoopInvariant:Atthestartofeachiteration:d[v]=δ(s,v)∀v∈ S.

• Variableusedtocalculateshortestpath:d• Propertyusedtocalculateshortestpath:d[v]=δ(s,v)

InitializationInitially,S=∅⟹ True

Termination:StopswhenQ=∅ ⇒d[v]=δ(s,v)∀ v∈ V(byLoopInvariantProperty)

Maintenance:Showthatd[u]=δ(s,u)whenuisaddedtoSineachIteration.

Page 73: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Bipartitegraphs

Page 74: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Alphabet Baidu Campbell

Yulia Baidu Alphabet Campbell

Zoran Alphabet Baidu Campbell

Candidates’preferences1st 2nd 3rd

Alphabet Yulia Xavier Zoran

Baidu Xavier Yulia Zoran

Campbell Xavier Yulia Zoran

Companies’preferences

Q:IsX-C,Y-B,Z-Aagoodassignment?

Candidates

Page 75: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Alphabet Baidu Campbell

Yulia Baidu Alphabet Campbell

Zoran Alphabet Baidu Campbell

Candidates’preferences1st 2nd 3rd

Alphabet Yulia Xavier Zoran

Baidu Xavier Yulia Zoran

Campbell Xavier Yulia Zoran

Companies’preferences

Q:IsX-C,Y-B,Z-Aagoodassignment?A:No!XavierandBaidubothprefertobematchedtogether…

Candidates

Page 76: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Gale-ShapleyalgorithmForeachα∈A,letpref[α]betheorderingofitspreferencesinBForeachβ∈B,letpref[β]betheorderingofitspreferencesinALetmatchingbeasetofcrossingedgesbetweenAandB

matching¬Æwhile thereisα∈Anotyetmatcheddo

β¬pref[α].removeFirst()ifβnotyetmatchedthen

matching¬matchingÈ{(α,β)}else

γ¬β’scurrentmatchif βprefersαoverγ then

matching¬matching-{(γ,β)}È{(α,β)}return matching

Page 77: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

BCandidates Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferencesCandidates’preferences

Page 78: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferences

Candidates

Candidates’preferences

Page 79: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferences

Candidates

Candidates’preferences

Page 80: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferences

Candidates

Candidates’preferences

Page 81: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferences

Candidates

Candidates’preferences

Page 82: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferences

Candidates

Candidates’preferences

Page 83: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

Men’spreferences1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferences

Candidates

Page 84: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferences

Candidates

Candidates’preferences

Page 85: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

Z

X

Y

A

C

B Companies

1st 2nd 3rd

Xavier Baidu Alphabet Campbell

Yulia Baidu Campbell Alphabet

Zoran Alphabet Campbell Baidu

1st 2nd 3rd

Alphabet Zoran Xavier Yulia

Baidu Yulia Zoran Xavier

Campbell Xavier Yulia Zoran

Companies’preferences

Candidates

Candidates’preferences

Page 86: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Flownetworks

Page 87: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

DefinitionsPositiveflow:Afunctionp:V× V→Rsatisfying.

Capacityconstraint:Forallu,v∈ V,0≤p(u,v)≤c(u,v),��

Flowconservation:Forallu∈ V−{s,t}, p(v,u)v∈V∑ = p(u,v)

v∈V∑

Flowintou Flowoutofu

1/2

Positiveflow Capacity

0/2 2/22/3

1/1 1/2Flowin:0+2+1=3Flowout:2+1=3

Page 88: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

s

1/3

2/2

1/3

2/2

2/3

0/10/21/3

1/1

2/3

t1/2

• Flowoutofsources ==Flowinthesinkt

• Flow=

• Objective: findmaximumflow

f (s,v)v∈V∑

Maxflow

Page 89: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

s

0/2

3/3

0/2

0/3

3/43/3

t

0/2

FlowinG

ResidualgraphGf

s

2

3

2

3

33

t

2

1

Page 90: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

ResidualgraphGf

FlowinGf

s

2

3

2

3

33

t

2

1

s

2

0

2

2

20

t

2

0

Page 91: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example

G

Gf s

2

0

2

2

20

t

2

0

s

0/2

3/3

0/2

0/3

3/43/3

t

0/2

s

2/2

3/3

2/2

2/3

1/43/3

t

2/2

|f|=3

|f|=5

β=2

Question:Howdoweknowifaflowismaximum?

Page 92: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Flowthroughacut

Claim:Givenaflownetwork.LetfbeaflowandA,Bbeas-tcut.Then,

Notation:|f|=fout(A) – fin(A)

f = f (e)− f (e)e∈cut (B,A)∑

e∈cut (A,B)∑

t3

s

5

1

2

4A

B

1

|f|=9-4=5

Page 93: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Maxflow=Mincut

• Ford-Fulkersonterminateswhenthereisnopaths-tintheresidualgraphGf

• Thesetofnodesaccessiblefroms inGf definesacutinG

• Ford-Fulkersonflow=

=capacityofcut(A,B)

c(e)− 0e∈cut (A,B)∑

f = f out (A)− f in (A)

= f (e)e∈cut (A,B)∑ − f (e)

e∈cut (B,A)∑

Page 94: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example:MinCut

s t

s t

s t

s t

Notacut!

Notacut!

Notamincut!

mincut!

Note:Alledgeshaveacapacityof1.

Page 95: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example:CalculateMinCut

s t

Tofindamincutcomputeamaxflow.

s t

backwardforward

1

0 0 0 0

1

1

1 1

1

Flow

Residualgraph

Page 96: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Example:CalculateMinCutTofindthecutrunBFS(orDFS)fromsontheresidualgraph.Thereachableverticesdefinethe(min)cut.

Mincut(inG!)

ResidualgraphwithDFS

s t

s t

Page 97: COMP251: Mid-Term Review - cs.mcgill.cajeromew/COMP251material/...AVL trees Red-black trees • BST (used to store keys) • Running time dependent of the height we try to keep the

Announces

• Officehoursextendeduntil3pmtoday.

• Officehours(Carlos&Roman)onwednesday from2pmto4pminTR3110.

• Mid-termexamscheduledat11h30(regularclasshours)inADAMSAuditorium.

• Onecribsheet(2pages)allowed.