cs106x – programming abstractions in c++ cynthia bailey lee dijkstra’s animation slides by keith...

98
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution - NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.or g .

Upload: shannon-reed

Post on 17-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

CS106X – Programming Abstractions in C++Cynthia Bailey Lee

Dijkstra’s animation slides by Keith Schwarz

              

CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee

 is licensed under a Creative Commons Attribution-

NonCommercial-ShareAlike 4.0 International License.

Permissions beyond the scope of this license may be available at 

http://peerinstruction4cs.org.

Page 2: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

GraphsNote about representation of graphs in Trailblazer assignment (compared to our two canonical graph representation forms: adjacency matrix and adjacency list)

Page 3: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

V1

V2

V3

V4

V5

V6

V1 0 1 1 0 0 0

V2 1 0 1 1 1 0

V3 1 1 0 1 0 1

V4 0 1 1 0 1 1

V5 0 1 0 1 0 1

V6 0 0 1 1 1 0

Representing Graphs: Adjacency Matrix vs. Trailblazer

v1 v2 v3 v4 v5 v6

v7 v8 v9 v10

v11

v12

v13

v14

v15

v16 …

Page 4: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

Breadth-First SearchGraph algorithms

Page 5: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

Breadth-First SearchA B

E F

C D

G H

I J

L

K

Page 6: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

J

L

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

Breadth-First Search

THINGS TO NOTICE:(1)We used a queue(2)What’s left is kind

of a subset of the edges, in the

form of ‘parent’ pointers

(3)If you follow the parent pointers from the desired endpoint, you will

get back to the start point, and it

will be the shortest way to do that.

Page 7: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

J

L

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

Breadth-First Search

THINGS TO NOTICE:(4) We now have the

answer to the question “What is the shortest path to you from F?” for every single node in the

graph!!

Page 8: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

But wait!! What you calculated will find the shortest

path in terms of fewest number of road segments but doesn’t at all take into account how long each road segment is!

Page 9: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

But wait!! This might be useful if you are a fugitive

and want to go from one city to another passing through the fewest other cities on the way…

But what if we want to save gas/fewest miles?

Page 10: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

Dijkstra’s Shortest Paths(Like Breadth-first Search, but takes into account weight/distance between nodes)

Page 11: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

Edsger Dijkstra

“Goto considered harmful” (title given to his letter)

This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license. http://en.wikipedia.org/wiki/File:Edsger_Wybe_Dijkstra.jpg

1930-2002

THE multiprogramming system (operating system) Layers of abstraction!!

Complier for a language that can do recursion

Dining Philosopher’s Problem (resource contention and deadlock)

Dijkstra’s algorithm

Page 12: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia
Page 13: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia
Page 14: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

The Shortest Path Problem● Suppose that you have a graph

representing different locations● Each edge has an associated cost (or

weight, length, etc)● We'll assume costs are nonnegative*● Goal: Find the least-cost (or lowest-

weight, lowest-length, etc) path from some node u to a node v

* else use the Bellman–Ford algorithm

Page 15: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

BA C

D E

HG

F

I

BA C

D E

HG

F

I

6

2

1

3

5

7

3

9

1

7

1

4

Page 16: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

● 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 17: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 18: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 19: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 20: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 21: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 22: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 23: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 24: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 25: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 26: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 27: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 28: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 29: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 30: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 31: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 32: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 33: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 34: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 35: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 36: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 37: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 38: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 39: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 40: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 41: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 42: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 43: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 44: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 45: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 46: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 47: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 48: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 49: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 50: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 51: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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,IB.C,F,G,IC.C,G,F,ID.Other/none/

more

Page 52: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 53: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 54: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 55: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 56: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 57: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 58: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 59: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 60: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 61: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 62: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 63: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 64: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 65: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 66: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 67: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 68: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 69: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 70: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 71: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 72: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 73: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 74: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 75: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 76: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 77: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 78: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 79: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 80: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 81: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 82: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 83: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 84: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 85: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 86: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 87: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 88: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 89: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 90: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 91: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 92: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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.

Page 93: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 94: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 95: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

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 96: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

Close Cousins● Dijkstra's algorithm and A* search are very

closely related:● Dijkstra's uses a node's candidate

distance as its priority.● A* uses a node's candidate distance

plus a heuristic value as its priority.● Interesting fact: If you use the zero heuristic

(which always predicts a node is at distance 0 from the endpoint), A* search is completely identical to Dijkstra's algorithm!

Page 97: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

● 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: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia

● 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