graphs - web.stanford.edu...b 1 d 2 1 2 2 c g h f 3 3 core idea: find everything one hop away from...

202
Graphs Part Two

Upload: others

Post on 03-Jan-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

GraphsPart Two

Page 2: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Outline for Today

● Recap from Last Time● Where are we, again?

● Depth-First Search● A different way to explore a graph.

● DAGs● A useful type of directed graph.

● Topological Sorting● Getting things done in the right order.

Page 3: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Recap from Last Time

Page 4: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A graph is a mathematical structurefor representing relationships.

A graph consists of a set of nodes connected by edges.

Page 5: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A graph is a mathematical structurefor representing relationships.

A graph consists of a set of nodes connected by edges.

Page 6: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A graph is a mathematical structurefor representing relationships.

A graph consists of a set of nodes connected by edges.

Nodes

Page 7: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A graph is a mathematical structurefor representing relationships.

A graph consists of a set of nodes connected by edges.

Edges

Page 8: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Representing Graphs

Node Adjacent To

We can represent a graph as a map from nodes to the list of nodes each node is connected to.

We can represent a graph as a map from nodes to the list of nodes each node is connected to.

Page 9: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A B

D E

C

H

F

G I

Page 10: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A B

D E

C

H

F

G I

Page 11: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A B

D

C

H

F

G I

E

Page 12: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A B

D

C

H

F

G I

E0

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Page 13: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A C

H

F

G I

E0

1

1

B

D

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Page 14: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A C

H

F

G I

E0

B

D1

1

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Page 15: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A

H

F

I

E0

B

D1

1

C

GCore idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Page 16: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A

H

F

I

E0

B

D1

12 2

2

C

GCore idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Page 17: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A

I

E0

B

D1

12 2

2

C

G H

F

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Page 18: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A

I

E0

B

D1

12 2

2

C

G H

F 3

3

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Page 19: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A

I

E0

B

D1

12 2

2

C

G H

F 3

3 4

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Core idea: Find everything one hop away from the start, then two hops away, then three hops

away, etc.

Page 20: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Breadth-First Search

● Breadth-first search (or just BFS for short) is an algorithm for exploring the nodes in a graph in ascending order of distance from some start node.

● In pseudocode:

bfs-from(node v) { make a queue of nodes, initially seeded with v.

while the queue isn't empty: dequeue a node curr. process the node curr.

for each node adjacent to curr: if that node has never been enqueued: enqueue that node.}

bfs-from(node v) { make a queue of nodes, initially seeded with v.

while the queue isn't empty: dequeue a node curr. process the node curr.

for each node adjacent to curr: if that node has never been enqueued: enqueue that node.}

Page 21: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

New Stuff!

Page 22: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Page 23: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 24: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 25: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 26: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 27: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 28: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 29: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 30: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 31: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 32: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 33: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 34: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 35: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 36: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 37: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 38: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 39: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 40: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 41: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 42: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 43: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 44: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 45: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 46: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 47: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 48: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 49: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 50: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 51: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 52: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 53: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 54: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 55: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 56: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 57: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 58: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 59: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 60: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 61: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 62: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 63: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 64: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 65: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 66: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 67: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 68: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 69: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 70: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 71: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 72: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 73: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 74: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 75: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 76: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Rule: Keep trying new experiences! Always go

somewhere new if you can, and only back up if there’s

nothing new to see.

Page 77: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 78: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 79: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 80: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 81: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 82: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 83: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 84: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 85: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 86: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 87: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 88: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 89: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 90: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 91: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 92: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 93: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 94: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 95: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 96: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 97: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 98: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 99: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 100: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 101: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 102: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 103: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 104: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 105: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 106: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 107: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Depth-First Search, Again

Page 108: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

How can we implement this?

Page 109: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

bfs-from(node v) { make a queue of nodes, initially seeded with v.

while the queue isn't empty: dequeue a node curr. process the node curr.

for each node adjacent to curr: if that node has never been enqueued: enqueue that node.}

bfs-from(node v) { make a queue of nodes, initially seeded with v.

while the queue isn't empty: dequeue a node curr. process the node curr.

for each node adjacent to curr: if that node has never been enqueued: enqueue that node.}

Breadth-First Search

Queue: X Q V A L

C

I’ve just beendiscovered! Payattention to me!

These nodes got here first, so they get processed first.

These nodes got here first, so they get processed first.

Page 110: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

dfs-from(node v) { make a stack of nodes, initially seeded with v.

while the stack isn't empty: pop a node curr. process the node curr.

for each node adjacent to curr: if that node has never been pushed: push that node.}

dfs-from(node v) { make a stack of nodes, initially seeded with v.

while the stack isn't empty: pop a node curr. process the node curr.

for each node adjacent to curr: if that node has never been pushed: push that node.}

Depth-First Search

Stack: X Q V A L

C

I’ve just beendiscovered! Payattention to me!

Oooh! A shiny new node! Who cares about these ones?

Oooh! A shiny new node! Who cares about these ones?

Page 111: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

For Comparison

bfs-from(node v) { make a queue of nodes, initially seeded with v.

while the queue isn't empty: dequeue a node curr. process the node curr.

for each node adjacent to curr: if that node has never been enqueued: enqueue that node.}

bfs-from(node v) { make a queue of nodes, initially seeded with v.

while the queue isn't empty: dequeue a node curr. process the node curr.

for each node adjacent to curr: if that node has never been enqueued: enqueue that node.}

Page 112: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

For Comparison

dfs-from(node v) { make a stack of nodes, initially seeded with v.

while the stack isn't empty: pop a node curr. process the node curr.

for each node adjacent to curr: if that node has never been pushed: push that node.}

dfs-from(node v) { make a stack of nodes, initially seeded with v.

while the stack isn't empty: pop a node curr. process the node curr.

for each node adjacent to curr: if that node has never been pushed: push that node.}

Page 113: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

When you see a stack-based algorithm,think recursion!

Page 114: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

Recursive DFS

dfs-from(node v) { make a stack of nodes, initially seeded with v.

while the stack isn't empty: pop a node curr. process the node curr.

for each node adjacent to curr: if that node has never been pushed: push that node.}

dfs-from(node v) { make a stack of nodes, initially seeded with v.

while the stack isn't empty: pop a node curr. process the node curr.

for each node adjacent to curr: if that node has never been pushed: push that node.}

Page 115: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

DFS Efficiency

● We visit each node with DFS at most once.● Each time we visit a node, we do

● some fixed work processing the node for the first time, then

● some additional work processing its neighbors.

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

Page 116: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

DFS Efficiency

● On average, each node has roughly ᵐ/ₙ neighbors.

Work done by DFS is

= #nodes × average-work-per-node

= n × (O(ᵐ/ₙ) + O(1))

= O(m + n)

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

Page 117: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

DFS Efficiency

● On average, each node has roughly ᵐ/ₙ neighbors.● Work done by DFS is

= #nodes × average-work-per-node

= n × (O(ᵐ/ₙ) + O(1))

= O(m + n)

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

Page 118: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

DFS Efficiency

● On average, each node has roughly ᵐ/ₙ neighbors.● Work done by DFS is

= #nodes × average-work-per-node

= n × (O(ᵐ/ₙ) + O(1))

= O(m + n)

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

Page 119: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

DFS Efficiency

● On average, each node has roughly ᵐ/ₙ neighbors.● Work done by DFS is

= #nodes × average-work-per-node

= n × (O(ᵐ/ₙ) + O(1))

= O(m + n)

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

Page 120: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

DFS Efficiency

● On average, each node has roughly ᵐ/ₙ neighbors.● Work done by DFS is

= #nodes × average-work-per-node

= n × (O(ᵐ/ₙ) + O(1))

= n · O(ᵐ/ₙ) + n · O(1)

= O(m + n).

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

Page 121: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

DFS Efficiency

● On average, each node has roughly ᵐ/ₙ neighbors.● Work done by DFS is

= #nodes × average-work-per-node

= n × (O(ᵐ/ₙ) + O(1))

= n · O(ᵐ/ₙ) + n · O(1)

= O(m + n).

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

dfs-from(node v) { if this is first time we've called dfs-from(v): process node v for each node adjacent to v: call dfs-from on that node }

Page 122: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

BFS and DFS

● Running BFS or DFS from a node in a graph will visit the same set of nodes, but probably in a different order.

● BFS will visit nodes in increasing order of distance.

● DFS does visit nodes in some interesting order, but not order of distance.● More on that later on…

Page 123: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

A Whimsical Application

Page 124: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Mazes as Graphs

Page 125: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Creating a Maze with DFS

● Create a grid graph of the appropriate size.

● Starting at any node, run a depth-first search, choosing neighbor orderings at random.

● The resulting DFS tree is a maze with one solution.

Page 126: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Time Out for Announcements!

Page 127: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS21SI: AI for Social Good is a 2-unit student-taught class in which students learn about and apply cutting-edge artificial intelligence techniques to real-world social good spaces, such as healthcare, government, education, and the environment.

The course alternates between lectures on machine learning theory and discussions with invited speakers, who will challenge students to apply techniques in their social good domains. You can learn more about the class here.

Apply by 11:59pm on Sunday, March 17 at bit.ly/AIForGoodApp.

Page 128: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Apply to the Blueprint Datathon, SHIFT's annual health datathon, to apply big data analytics to challenges in healthcare and compete for large

cash prizes. Our 2019 theme is Infectious Diseases.

Apply @ blueprint.stanford.edu - spots are limited, and applicants will be accepted on a rolling basis. Applications are due March 22 for non-Stanford

affiliates and April 1 for Stanford students.

Blueprint will take place from April 12 - 14 at Hewlett-Packard Building.

Bring your best ideas and tell your friends!

Page 129: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Assignment 6

● Assignment 6 (MiniBrowser) is due this Friday.● The following are True Facts:

● This is the last assignment on which you can use late days. No late submissions will be accepted for Assignment 7. (Sorry, that’s university policy.)

● You should be careful about using late days here, as that will eat into the time for Assignment 7.

● YEAH Hours for Assignment 7 will be held this Friday at 3PM in room 380-380Y. Slides, as usual, will be posted on the website afterward.

Page 130: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

We Have to Go Deeper!

Page 131: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Prerequisite Structures

Page 132: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B

CS103 CS109

CS107 CS110

CS143

CS161

Page 133: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything
Page 134: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Wake Up InThe Morning

Feel LikeP Diddy

Brush Teeth WithBottle of Jack

Leave

ClothesPedicurePlay Favorite

CDs

Pull up to Party

FightGet Crunk,

CrunkPolice Shut

Down, Down

See the Sunlight

BlowSpeakers Up

Page 135: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything
Page 136: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything
Page 137: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything
Page 138: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

UnderstandRecursion

Page 139: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

UnderstandRecursion

Page 140: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Modeling Prerequisites

● We can model prerequisites as a graph with the following properties:● The graph has to be directed, since we have

to be able to distinguish “A depends on B” from “B depends on A.”

● The graph has to be acyclic (containing no cycles), since otherwise there is no way to accomplish all the tasks.

● A graph with this property is called a directed acyclic graph, or DAG.

Page 141: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Ordering Prerequisites

● Imagine we have a DAG representing a collection of tasks. We can only start a task once its prerequisite tasks have been completed.

● What order should we do the tasks in?

CS106A

CS106B

CS103 CS109

CS107 CS110

CS143

CS161

Page 142: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Ordering Prerequisites

● A topological ordering of the nodes in a DAG is a list of the nodes so that each node is placed after the nodes that point to it.

● An algorithm for finding a topological ordering is called a topological sorting algorithm.

CS106A

CS106B

CS103 CS109

CS107 CS110

CS143

CS161

CS106A CS106B CS107 CS103 CS110 CS109 CS161 CS143

Page 143: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Ordering Prerequisites

● A topological ordering of the nodes in a DAG is a list of the nodes so that each node is placed after the nodes that point to it.

● An algorithm for finding a topological ordering is called a topological sorting algorithm.

CS106A

CS106B

CS103 CS109

CS107 CS110

CS143

CS161

CS106A CS106B CS103 CS109 CS161 CS107 CS143 CS110

Page 144: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Adventures in Topological SortingDFS Topological Sorting

Page 145: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

Page 146: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

Page 147: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

Page 148: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

Page 149: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

Page 150: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

Page 151: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

Page 152: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

Page 153: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

Page 154: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

Page 155: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

Page 156: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

Page 157: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

Page 158: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

Page 159: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

Page 160: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

Page 161: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

Page 162: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

Page 163: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

Page 164: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

Page 165: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

Page 166: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Page 167: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Page 168: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Page 169: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Page 170: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Page 171: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Page 172: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

Page 173: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

Page 174: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

Page 175: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

Page 176: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

Page 177: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

Page 178: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

Page 179: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

Page 180: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

Page 181: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

Page 182: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

Page 183: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

Page 184: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

Page 185: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

Page 186: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

Page 187: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

Page 188: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

Page 189: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

Page 190: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

CS106B

Page 191: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

CS106B

Page 192: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

CS106B

Page 193: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

CS106B

Page 194: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

CS106B

Page 195: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

CS106B

Page 196: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

CS106B

CS106A

Page 197: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS161

CS109

CS154

CS103

Math 51

CS147

CS110

CS107

CS106B

CS106A

Page 198: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

CS106A

CS106B CS103 Math 51

CS109CS107

CS110 CS161

CS166

CS154CS147

CS166

CS154

CS161

CS109

CS103

Math 51

CS106B

CS110

CS147

CS107

CS106ACS166

CS154

CS161

CS109

CS103

Math 51

CS106B

CS110

CS147

CS107

CS106A

Page 199: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Topological Sort via DFS

dfs-topological-sort() { result = [] for each node in the graph, in whatever order sparks joy: run a recursive DFS starting from that node. when you finish visiting a node, append it to result. return the reverse of result

dfs-topological-sort() { result = [] for each node in the graph, in whatever order sparks joy: run a recursive DFS starting from that node. when you finish visiting a node, append it to result. return the reverse of result

● Here’s a beautiful algorithm for topologically sorting a graph:

● If you’re clever with how you implement this, the runtime ends up being O(m + n).

Page 200: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Why Does This Work?

● Intuition: Given a node v, a topological ordering needs to place v before a topological ordering of everything that depends on v.

● Running a DFS orders everything that depends on v before ordering v.

● Reversing things at the end is equivalent to always prepending v to the result rather than appending v to the result.

Page 201: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Your Action Items

● Aim to finish MiniBrowser.● Again, you can use late days, but be careful

about doing so.● Read Chapter 18 of the textbook.

● There’s a bunch of goodies in there about graph representations and graph algorithms.

Page 202: Graphs - web.stanford.edu...B 1 D 2 1 2 2 C G H F 3 3 Core idea: Find everything one hop away from the start, then two hops away, then three hops away, etc. Core idea: Find everything

Next Time

● More Graph Algorithms● Which ones? Wait and see!