cs106x – programming abstractions in c++ cynthia bailey lee bfs animation slides by keith schwarz...

71
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS 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: ariel-sullivan

Post on 31-Dec-2015

224 views

Category:

Documents


3 download

TRANSCRIPT

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

CS106X – Programming Abstractions in C++Cynthia Bailey Lee

BFS 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 BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

GraphsWhat are graphs? What are they good for?

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

A Social Network

Slide by Keith Schwarz

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

Chemical Bonds

http://4.bp.blogspot.com/-xCtBJ8lKHqA/Tjm0BONWBRI/AAAAAAAAAK4/-mHrbAUOHHg/s1600/Ethanol2.gifSlide by Keith Schwarz

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

http://strangemaps.files.wordpress.com/2007/02/fullinterstatemap-web.jpg Slide by Keith Schwarz

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

A graph is a mathematical structure for representing relationships

Consists of: A set V of vertices (or nodes)

Often have an associated label A set E of edges (or arcs)

Consist of two endpoint vertices May be directed (an edge from A to B only

allow you to go from A to B, not B to A) or undirected (an edge between A and B allows travel in both directions)

Often have an associated cost or weight

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

GraphsHow do we represent graphs in code?

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

10

Diagram shows a graph with four vertices:

Apple (outgoing edges to banana and blum)Banana (with outgoing edge to self)Pear (with outgoing edges to banana and plum)Plum (with outgoing edge to banana)

Graph terminology This is a DIRECTED graph

This is an UNDIRECTED graph

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

11

Diagrams each show a graph with four vertices: Apple, Banana, Pear, Plum. Each answer choice has different edge sets.

A: no edgesB: Apple to Plum directed, Apple to banana undirectedC: Apple and Banana point to each other. Two edges point from Plum to Pear

Graph terminology Which of the following is a correct graph?

A. B. C.

D. None of the above/other/more than one of the above

Page 12: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

0 1 1 0 0 0

1 0 1 1 1 0

1 1 0 1 0 1

0 1 1 0 1 1

0 1 0 1 0 1

0 0 1 1 1 0

Representing Graphs: Adjacency Matrix

We can represent a graph as a Grid<bool>

(unweighted) or a Grid<int>

(weighted)

We can represent a graph as a Grid<bool>

(unweighted) or a Grid<int>

(weighted)

Page 13: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

Representing Graphs: adjacency list

Node Connected To

Map<Node*, Set<Node*>> We can represent a graph as a map from

nodes to the set of nodes each node is connected

to.

We can represent a graph as a map from

nodes to the set of nodes each node is connected

to.

Slide by Keith Schwarz

Page 14: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

Common ways of representing graphs

Adjacency list: Map<Node*, Set<Node*>>

Adjacency matrix: Grid<bool> unweighted Grid<int> weighted

How many of the following are true? Adjacency list can be used for directed graphs Adjacency list can be used for undirected graphs Adjacency matrix can be used for directed graphs Adjacency matrix can be used for undirected

graphs(A) 0 (B) 1 (C) 2 (D) 3 (E) 4

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

GraphsTheorems about graphs

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

16

Graphs lend themselves to fun theorems and proofs of said theorems!

Any graph with 6 vertices contains either a triangle (3 vertices with all pairs having an edge) or an empty triangle (3 vertices no two pairs having an edge)

Page 17: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

17

Diagram shows a graph with four vertices, which are all fully connected with each other by undirected edges (6 edges in total).

Eulerian graphs Let G be an undirected graph

A graph is Eulerian if it can drawn without lifting the penand without repeating edges

Is this graph Eulerian?A. YesB. No

Page 18: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

18

Diagram shows a graph with five vertices, four of which are all fully connected with each other by undirected edges (6 edges in total). The 5th vertex is connected to two of the remaining four vertices by an edge. (6 + 2 = 8 edges in total)

Eulerian graphs Let G be an undirected graph

A graph is Eulerian if it can drawn without lifting the penand without repeating edges

What about this graphA. YesB. No

Page 19: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

19

Our second graph theorem Definition: Degree of a vertex:

number of edges adjacent to it

Euler’s theorem: a graph is Eulerian iff the number of vertices with odd degrees is either 0 or 2 (eg all vertices or all but two have even degrees)

Does it work for and ?

Page 20: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

Proving Euler’s theoremThe following slides geek out on theoretical computer science with a proof of the Eulerian graph theorem. If you like this, YOU WILL LOVE CS103!!

Page 21: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

21

Proving Euler’s theorem Euler’s theorem gives a necessary and

sufficient condition for a graph to be Eulerian All degrees are even Two degrees odd, rest are even

Will prove in class that this is necessary Take-home challenge: prove that this is also

sufficient

Page 22: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

22

Proving Euler’s theorem: necessary part Euler’s theorem (necessary part):

If a graph G is Eulerian then all degrees are even; or two degrees are odd and rest are even

Try to prove it first yourself

Page 23: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

23

Proving Euler’s theorem: necessary part Proof of Euler’s theorem (necessary part):

Let G be a graph with an Euler path: v1,v2,v3,….,vk where (vi,vi+1) are edges in G; vertices may appear more than once; and each edge of G is accounted for exactly once.

The degree of a vertex of G is the number of edges it has. For any internal vertex in the path (eg not v1 or vk), we count 2 edges in the path (one going in and one going out). So, any vertex which is not v1 or vk must have an even degree.

If v1vk then both have odd degrees. If v1=vk is the same vertex this is also has even degree. QED.

Page 24: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

Breadth-First SearchGraph algorithms

Page 25: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

Breadth-First SearchA B

E F

C D

G H

I J

L

K

Page 26: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey
Page 27: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

A B

E F

C D

G H

I J

L

K

A B

E F

C D

G H

I J

L

K

Breadth-First Search

TO START:(1)Color all nodes

GREY(2)Queue is empty

Page 28: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

F

A B

E F

C D

G H

I J

L

K

A B

E

C D

G H

I J

L

K

Breadth-First Search

F

TO START (2):(1)Enqueue the desired start node

(2)Note that anytime we enqueue a

node, we mark it YELLOW

Page 29: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

F

A B

E F

C D

G H

I J

L

K

A B

E

C D

G H

I J

L

K

Breadth-First Search

FLOOP PROCEDURE:(1)Dequeue a node

(2)Mark current node GREEN

(3)Set current node’s GREY neighbors’

parent pointers to current node, then

enqueue them (remember: set them YELLOW)

Page 30: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

F

A B

E F

C D

G H

I J

L

K

A B

E

C D

G H

I J

L

K

Breadth-First Search

F

Page 31: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

A B

E

D

K

F

A B

E F

C D

G H

I J

L

K

C

G H

I J

L

Breadth-First Search

F

Page 32: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

A B

E

D

K

F

A B

E F

C D

G H

I J

L

K

C

G H

I J

L

Breadth-First Search

A B D E K

F

Page 33: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

A B

E

D

K

F

A B

E F

C D

G H

I J

L

K

C

G H

I J

L

Breadth-First Search

B D E K

A

Page 34: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

A B

E

D

K

F

A B

E F

C D

G H

I J

L

K

C

G H

I J

L

Breadth-First Search

B D E K

A

Page 35: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

A B

E

D

K

F

A B

E F

C D

G H

I J

L

K

C

G H

I J

L

Breadth-First Search

B D E K

A

Page 36: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

A B

E

D

K

F

A B

E F

C D

G H

I J

L

K

C

G H

I J

L

Breadth-First Search

D E K

B

Page 37: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

A B

E

D

K

F

A B

E F

C D

G H

I J

L

K

C

G H

I J

L

Breadth-First Search

D E K

B

Page 38: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

I J

L

Breadth-First Search

D E K

B

C H

Page 39: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

I J

L

Breadth-First Search

E K C H

D

Page 40: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

I J

L

Breadth-First Search

E K C H

D

Page 41: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

I J

L

Breadth-First Search

E K C H

D

Page 42: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

I J

L

Breadth-First Search

K C H

E

Page 43: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

I J

L

Breadth-First Search

K C H

E

Page 44: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

J

L

Breadth-First Search

K C H

E

I

Page 45: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

J

L

Breadth-First Search

C H I

K

Page 46: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

J

L

Breadth-First Search

C H I

K

Page 47: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

K

G

J

L

Breadth-First Search

C H I

K

You predict the next slide!

A. K’s neighbors F,G,H are yellow and enqueued and their parents are pointing to K

B. K’s neighbors G,H are yellow and enqueued and their parents are pointing to K

C. Other/none/more

Page 48: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

C H I

K

G

Page 49: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

C H I

K

G

Page 50: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

H I G

C

Page 51: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

H I G

C

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

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

I G

H

Page 53: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

I G

H

Page 54: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

I G

Page 55: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

G

I

Page 56: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

L

Breadth-First Search

G

I

Page 57: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

L

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

Breadth-First Search

G

I

L

Page 58: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

L

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

Breadth-First Search

L

G

Page 59: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

L

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

Breadth-First Search

L

G

Page 60: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

L

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

Breadth-First Search

L

Page 61: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

L

G

I

H

CA B

E

D

K

F

A B

E F

C D

G H

I J

L

KJ

Breadth-First Search

L

Page 62: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

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

LJ

Page 63: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

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

J

Page 64: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

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

J

Page 65: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

Quick question about efficiency… Let’s say that you have a very

geographically diverse extended family—somebody in every city in the western U.S.

Page 66: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

Quick question about efficiency… You’re all going to fly to Yosemite and then

drive home, and you’ve been tasked with making custom driving directions for everyone

Page 67: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

Quick question about efficiency… You calculated the shortest path for yourself

(Yosemite to Palo Alto) and let’s just say that it took time X = O((|E| + |V|)log|V|)

With respect to the number of cities |V|, and the number of road segments |E|

How long will it take you, in total, to calculate the shortest path for ALL of your relatives?

A. O(|V|*X)B. O(|E|*|V|* X)C. XD. Other/none/more

Page 68: CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey

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 a

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

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

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

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?