programming abstractions - stanford university · graphs topics graphs! 1. basics what are they?...

107
Programming Abstractions Cynthia Lee CS106B

Upload: others

Post on 08-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Programming Abstractions

Cynthia Lee

C S 106B

Page 2: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Graphs Topics

Graphs!

1. Basics

What are they? How do we represent them?

2. Theorems

What are some things we can prove about graphs?

3. Breadth-first search on a graph

Spoiler: just a very, very small change to tree version

4. Dijkstra’s shortest paths algorithm

Spoiler: just a very, very small change to BFS

5. A* shortest paths algorithm

Spoiler: just a very, very small change to Dijkstra’s

6. Minimum Spanning Tree

Kruskal’s algorithm

Page 3: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A BA0?

C

D E

HG

F

I

B C

D E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4 7

1

Page 4: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A BA0?

C

D E

HG

F

I

B C

D E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4

A0?

7

1

Page 5: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A BA0?

C

D E

HG

F

I

B C

D E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4

A0?

7

1

Page 6: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A BA0?

C

D E

HG

F

I

B C

D E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4 7

1

Page 7: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A BA0

C

D E

HG

F

I

B C

D E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4 7

1

Page 8: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

B

D

A B6?

A0

C

D3?

E

HG

F

I

C

E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4 7

1

Page 9: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

B

D

A B6?

A0

C

D3?

E

HG

F

I

C

E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

D3?

7

1

Page 10: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

B

D

A B6?

A0

C

D3?

E

HG

F

I

C

E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

D3?

7

1

Page 11: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

B

D

A B6?

A0

C

D3?

E

HG

F

I

C

E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

D3?

7

1

Page 12: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

B

D

A B6?

A0

C

D3?

E

HG

F

I

C

E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

7

1

Page 13: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

B

D

A B6?

A0

C

D3

E

HG

F

I

C

E

HG

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

7

1

Page 14: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

E

G

B

D

A B6?

A0

C

D3

E4?

HG12?

F

I

C

H

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

7

1

Page 15: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

E

G

B

D

A B6?

A0

C

D3

E4?

HG12?

F

I

C

H

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

7

1

Page 16: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

E

G

B

D

A B6?

A0

C

D3

E4?

HG12?

F

I

C

H

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

E4?

G12?

7

1

Page 17: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

E

G

B

D

A B6?

A0

C

D3

E4?

HG12?

F

I

C

H

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

E4?

G12?

7

1

Page 18: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

E

G

B

D

A B6?

A0

C

D3

E4?

HG12?

F

I

C

H

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

E4?

G12?

7

1

Page 19: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

E

G

B

D

A B6?

A0

C

D3

E4?

HG12?

F

I

C

H

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

G12?

7

1

Page 20: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

E

G

B

D

A B6?

A0

C

D3

E4

HG12?

F

I

C

H

F

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

G12?

7

1

Page 21: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B6?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

G12?

7

1

Page 22: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B6?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

G12?

7

1

Page 23: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B6?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

G12?

H8?

F11?

7

1

Page 24: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B6?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

G12?

H8?

F11?

7

1

Page 25: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B5?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B6?

G12?

H8?

F11?

7

1

Page 26: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B5?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B5?

G12?

H8?

F11?

7

1

Page 27: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B5?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B5?

G12?

H8?

F11?

7

1

Page 28: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B5?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B5?

G12?

H8?

F11?

7

1

Page 29: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B5?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

B5?

G12?

H8?

F11?

7

1

Page 30: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B5?

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

G12?

H8?

F11?

7

1

Page 31: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

H

FE

G

B

D

A B5

A0

C

D3

E4

H8?

G12?

F11?

I

C

I

6

2

1

3

5

7

3

9

2

4

1

4

G12?

H8?

F11?

7

1

Page 32: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

II

6

2

1

3

5

7

3

9

2

4

1

4

G12?

H8?

F11?

7

1

Page 33: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

II

6

2

1

3

5

7

3

9

2

4

1

4G

12?

H8?

F11?

7

1

Page 34: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

II

6

2

1

3

5

7

3

9

2

4

1

4G

12?

H8?

F11?

C8?

7

1

Page 35: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

II

6

2

1

3

5

7

3

9

2

4

1

4G

12?

H8?

F11?

C8?

7

1

Page 36: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

II

6

2

1

3

5

7

3

9

2

4

1

4G

12?

H8?

F11?

C8?

7

1

Page 37: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

II

6

2

1

3

5

7

3

9

2

4

1

4G

12?

H8?

F11?

C8?

7

1

You predict the

next queue state:

A. H,C,F,G,I

B. C,F,G,I

C.C,G,F,I

D.Other/none/

more

Page 38: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

II

6

2

1

3

5

7

3

9

2

4

1

4

G12?

F11?

C8?

7

1

Page 39: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8

G12?

F11?

II

6

2

1

3

5

7

3

9

2

4

1

4

G12?

F11?

C8?

7

1

Page 40: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G12?

F11?

C8?

H8

7

1

Page 41: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G12?

F11?

C8?

H8

7I

13?

1

Page 42: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G12?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G12?

F11?

C8?

H8

7I

13?

1

Page 43: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G10?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G12?

F11?

C8?

H8

7I

13?

1

Page 44: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G10?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

F11?

C8?

H8

7I

13?

1

Page 45: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G10?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

F11?

C8?

H8

7I

13?

1

Page 46: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G10?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

F11?

C8?

H8

7I

13?I

13?

1

Page 47: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G10?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

F11?

C8?

H8

7I

13?

1

Page 48: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8?

G10?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

F11?

C8?

H8

7I

13?

1

Page 49: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8

G10?

F11?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

F11?

C8?

7I

13?

1

Page 50: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8

G10?

F11?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F11?

C8?

7I

13?

Page 51: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8?

D3

E4

H8

G10?

F11?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F11?

7

I13?

Page 52: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F11?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F11?

7

I13?

Page 53: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F11?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F11?

7

I13?

Page 54: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F9?

7

I13?

Page 55: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F9?

7

I13?

Page 56: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F9?

7

I13?

Page 57: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F9?

7

I13?

Page 58: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9?

I13?

6

2

1

3

5

7

3

9

1

4

1

4

G10?

F9?

7

I13?

Page 59: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

F9?

7

I13?

1

Page 60: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

F9?

7

I13?

1

Page 61: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9?

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

7

I13?

1

Page 62: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

7

I13?

1

Page 63: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

7

I13?

1

Page 64: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

7

I13?

1

Page 65: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

7

I13?

1

Page 66: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

7

I13?

1

Page 67: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4

G10?

7

I13?

1

Page 68: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10?

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4 7

I13?

1

Page 69: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4 7

I13?

1

Page 70: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4 7

I13?

1

Page 71: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4 7

I13?

1

Page 72: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10

F9

I13?

6

2

1

3

5

7

3

9

2

4

1

4 7

1

Page 73: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10

F9

I13

6

2

1

3

5

7

3

9

2

4

1

4 7

1

Page 74: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

I

C

H

FE

G

B

D

A B5

A0

C8

D3

E4

H8

G10

F9

I13

6

2

1

3

5

7

3

9

2

4

1

4 7

1

Page 75: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Dijkstra's Algorithm

● Split nodes apart into three groups:

● Green nodes, where we already have the shortest path;

● Gray nodes, which we have never seen; and

● Yellow nodes that we still need to process.

● Dijkstra's algorithm works as follows:

● Mark all nodes gray except the start node, which is yellow and has cost 0.

● Until no yellow nodes remain:

– Choose the yellow node with the lowest total cost.

– Mark that node green.

– Mark all its gray neighbors yellow and with the appropriate cost.

– Update the costs of all adjacent yellow nodes by considering the path through

the current node.

Page 76: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

An Important Note

● The version of Dijkstra's algorithm I have just described is not the

same as the version described in the course reader.

● This version is more complex than the book's version, but is much

faster.

● THIS IS THE VERSION YOU MUST USE ON YOUR TRAILBLAZER

ASSIGNMENT!

Page 77: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

How Dijkstra's Works

● Dijkstra's algorithm works by incrementally computing the shortest path

to intermediary nodes in the graph in case they prove to be useful.

● Most of these nodes are completely in the wrong direction.

● No “big-picture” conception of how to get to the destination – the

algorithm explores outward in all directions.

● Could we give the algorithm a hint?

Page 78: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Dijkstra’s: SPIN analysis (shoutout to GSB students)

● Situation:

● Dijkstra's algorithm works by incrementally computing the shortest path to intermediary nodes in the graph in case they prove to be useful.

● Problem:

● No big-picture conception of how to get to the destination – the algorithm explores outward in all directions, “in case.”

● Implication:

● Most of these explored nodes will end up being in completely the wrong direction.

● Need:

● Could we give the algorithm a “hint” of which direction to go?

Page 79: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A* and Dijkstra’sClose cousins

Page 80: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Heuristics

● In the context of graph searches, a heuristic function is a function that

guesses the distance from some known node to the destination node.

● The guess doesn't have to be correct, but it should try to be as accurate

as possible.

● Examples: For Google Maps, a heuristic for estimating distance might

be the straight-line “as the crow flies” distance.

Admissible Heuristics

● A heuristic function is called an admissible heuristic if it never

overestimates the distance from any node to the destination.

● In other words:

● predicted-distance ≤ actual-distance

Page 81: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Why Heuristics Matter

● We can modify Dijkstra's algorithm by introducing

heuristic functions.

● Given any node u, there are two associated costs:

● The actual distance from the start node s.

● The heuristic distance from u to the end node t.

● Key idea: Run Dijkstra's algorithm, but use the following

priority in the priority queue:

● priority(u) = distance(s, u) + heuristic(u, t)

● This modification of Dijkstra's algorithm is called the

A* search algorithm.

s tu

Page 82: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A* Search

As long as the heuristic is admissible (and satisfies one other

technical condition), A* will always find the shortest path from

the source to the destination node.

Can be dramatically faster than Dijkstra's algorithm.

Focuses work in areas likely to be productive.

Avoids solutions that appear worse until there is evidence they may be appropriate.

Page 83: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

● Mark all nodes as gray.● Mark the initial node s as yellow and at candidate distance 0.● Enqueue s into the priority queue with priority 0.● While not all nodes have been visited:● Dequeue the lowest-cost node u from the priority queue.● Color u green. The candidate distance d that is currently stored for node u is the length of the

shortest path from s to u.● If u is the destination node t, you have found the shortest path from s to t and are done.● For each node v connected to u by an edge of length L:

– If v is gray:● Color v yellow.● Mark v's distance as d + L.● Set v's parent to be u.● Enqueue v into the priority queue with priority d + L.

– If v is yellow and the candidate distance to v is greater than d + L:● Update v's candidate distance to be d + L.● Update v's parent to be u.● Update v's priority in the priority queue to d + L.

Dijkstra'sAlgorithm

Page 84: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

● Mark all nodes as gray.● Mark the initial node s as yellow and at candidate distance 0.● Enqueue s into the priority queue with priority h(s,t).● While not all nodes have been visited:● Dequeue the lowest-cost node u from the priority queue.● Color u green. The candidate distance d that is currently stored for node u is the length of the

shortest path from s to u.● If u is the destination node t, you have found the shortest path from s to t and are done.● For each node v connected to u by an edge of length L:

– If v is gray:● Color v yellow.● Mark v's distance as d + L.● Set v's parent to be u.● Enqueue v into the priority queue with priority d + L + h(v,t).

– If v is yellow and the candidate distance to v is greater than d + L:● Update v's candidate distance to be d + L.● Update v's parent to be u.● Update v's priority in the priority queue to d + L + h(v,t).

A* Search

Page 85: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A* on two points where the heuristic is slightly misleading due to a wall blocking the way

Page 86: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A* starts with start node yellow, other nodes grey.

Page 87: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A*: dequeue start node, turns green.

Page 88: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 +6?

1 +6?

1 +4?

1 +6?

A*: enqueue neighbors with candidate distance + heuristic distance as the priority value.

Page 89: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 +6?

1 +6?

1

1 +6?

A*: dequeue min-priority-value node.

Page 90: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 +6?

1 +6?

1

1 +6?

2 +5?

???

2 +5?

What goes in the ?

A. 2 + 5?

B. 1 + 6?

C. 2 + 4?

D. Other/none/more

???

Page 91: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 +6?

1 +6?

1

1 +6?

2 +5?

2 +3?

2 +5?

A*: enqueue neighbors.

Page 92: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 +6?

1 +6?

1

1 +6?

2 +5?

2 + 3?

2 +5?

Now we’re done with the green “1” node’s turn.

What is the next node to turn green? (and what would it be if this were Dijkstra’s?)

Page 93: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 +6?

1 +6?

1

1 +6?

2 +5?

2

2 +5?

A*: dequeue next lowest priority value node. Notice we are making a straight line right for the end point, not wasting time with other directions.

Page 94: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 +6?

1 +6?

1

1 +6?

2 +5?

2

2 +5?

3 +4?

3 +4?

A*: enqueue neighbors—uh-oh, wall blocks us from continuing forward.

Page 95: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 21

2

21

1

3

323 +8?

3 +8?

23 +8?

3 +8?

4

5 +6?

2

3 +8?

3

4 +7?

2

3 +8?

3 +8?

2

3

4 +7?

4

5 +6?

5

6 +5?

6

7 +4?

5

6 +5?

78 +1?

7

8 +3?

8 +3?

8

7 +2?

6

7 +4?

7 +2?

A*: eventually figures out how to go around the wall, with some waste in each direction.

Page 96: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

1 21

2

21

1

3

32

2

42 3

2

2

3 4

5 6

5

7

7

8

6

For Comparison: What Dijkstra's Algorithm Would Have Searched

64

4 53

3

3

3

3

3

3

5

5

64

4

4

4

4

4

4

4

4

4 55

5

5

5

5

5

5

56

6

6

6

6

6 6

6

7

7

7

78

8 7

7

7

7

7

7 8

8

8

8

8

8

8

8 9?

9?

9?

9?

9?

9?

9?

9?

Page 97: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

● Mark all nodes as gray.● Mark the initial node s as yellow and at candidate distance 0.● Enqueue s into the priority queue with priority 0.● While not all nodes have been visited:● Dequeue the lowest-cost node u from the priority queue.● Color u green. The candidate distance d that is currently stored for node u is the length of the

shortest path from s to u.● If u is the destination node t, you have found the shortest path from s to t and are done.● For each node v connected to u by an edge of length L:

– If v is gray:● Color v yellow.● Mark v's distance as d + L.● Set v's parent to be u.● Enqueue v into the priority queue with priority d + L.

– If v is yellow and the candidate distance to v is greater than d + L:● Update v's candidate distance to be d + L.● Update v's parent to be u.● Update v's priority in the priority queue to d + L.

Dijkstra'sAlgorithm

Page 98: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

● Mark all nodes as gray.● Mark the initial node s as yellow and at candidate distance 0.● Enqueue s into the priority queue with priority h(s,t).● While not all nodes have been visited:● Dequeue the lowest-cost node u from the priority queue.● Color u green. The candidate distance d that is currently stored for node u is the length of the

shortest path from s to u.● If u is the destination node t, you have found the shortest path from s to t and are done.● For each node v connected to u by an edge of length L:

– If v is gray:● Color v yellow.● Mark v's distance as d + L.● Set v's parent to be u.● Enqueue v into the priority queue with priority d + L + h(v,t).

– If v is yellow and the candidate distance to v is greater than d + L:● Update v's candidate distance to be d + L.● Update v's parent to be u.● Update v's priority in the priority queue to d + L + h(v,t).

A* Search

Page 99: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Minimum Spanning Tree

Page 100: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

A spanning tree in an undirected graph is a set of edges

with no cycles that connects all nodes.

A minimum spanning tree (or MST) is a spanning tree

with the least total cost.

Page 101: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

B D

ECA

F

How many distinct minimum

spanning trees are in this

graph?

A. 0-1

B. 2-3

C.4-5

D.6-7

E. >7

3

3

1

3

3

7

Edges:

(A,B)=1

(A,C)=3

(B,C)=6

(B,D)=3

(C,E)=3

(D,E)=3

(D,F)=7

Page 102: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Kruskal’s algorithm

Remove all edges from graph

Place all edges in a PQ based on length/weight

While !PQ.isEmpty():

Dequeue edge

If the edge connects previous disconnected

nodes or groups of nodes, keep the edge

Otherwise discard the edge

Page 103: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Kruskal’s algorithm

Page 104: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

The Good Will Hunting Problem

Page 105: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

Video Clip

https://www.youtube.com/watch?v=N7b0cLn-wHU

Page 106: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

“Draw all the homeomorphically irreducible trees with n=10.”

Page 107: Programming Abstractions - Stanford University · Graphs Topics Graphs! 1. Basics What are they? How do we represent them? 2. Theorems What are some things we can prove about graphs?

“Draw all the homeomorphically irreducible trees with n=10.”

In this case “trees” simply means graphs with no cycles

“with n = 10” (i.e., has 10 nodes)

“homeomorphically irreducible”

No nodes of degree 2 allowed in your solutions

› For this problem, nodes of degree 2 are useless in

terms of tree structure—they just act as a blip on an

edge—and are therefore banned

Have to be actually different

› Ignore superficial changes in rotation or angles of

drawing