cs 106b, lecture 23 graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... ·...
TRANSCRIPT
![Page 1: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/1.jpg)
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.
CS 106B, Lecture 23Graphs
This document is copyright (C) Stanford Computer Science and Ashley Taylor, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Marty Stepp, Chris Gregg, Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others
![Page 2: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/2.jpg)
2
Plan for Today• Graphs!
– How to model problems using a graph
![Page 3: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/3.jpg)
3
ADT Flowchart
![Page 5: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/5.jpg)
5
Molecules
http://pngimg.com/uploads/molecule/molecule_PNG50.png
![Page 6: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/6.jpg)
6
Introducing: The Graph• A graph is a mathematical structure for representing relationships• Consists of nodes (aka vertices) and edges (aka arcs)
– edges are the relationships, nodes are the items
• Examples:– Map: cities (nodes) are connected by roads (edges)– Molecules: atoms (nodes) are connected by bonds (edges)
![Page 7: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/7.jpg)
7
Graph examples• For each, what are the nodes and what are the edges?
– Web pages with links– Functions in a program that call each other– Airline routes– Facebook friends– Course pre-requisites– Family trees– Paths through a maze
![Page 8: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/8.jpg)
8
Boggle as a graph• Q: If a Boggle board is a graph, what is a node? What is an edge?
A. Node = letter cube, Edge = Dictionary (lexicon)B. Node = dictionary word; Edge = letter cubeC. Node = letter; Edge = between each letter that is part of a wordD. Node = letter cube; Edge = connection to neighboring cubeE. None of the above
![Page 9: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/9.jpg)
9
Undirected vs. Directed• Some relationships are
mutual– Facebook
• Some are one-way– Twitter/Instagram– Doesn't mean that all
relationships are non-mutual
![Page 10: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/10.jpg)
10
Representing Graphs• Two main ways:
– Have each node store the nodes it's connected to (adjacency list)– Have a list of all the edges (edge list)
• The choice depends on the problem you're trying to solve• You can sometimes represent graphs implicitly instead of explicitly
storing the edges and nodes– e.g. Boggle, WordLadder– draw a picture to see the graph more clearly!
• Was the backtracking (wiki links) problem on the midterm a graph problem? How did we represent the graph?
![Page 11: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/11.jpg)
11
Adjacency List• Map<Node, Vector<Node>>
– or Map<Node, Set<Node>> Node Set<Node>
![Page 12: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/12.jpg)
12
Adjacency Matrix• Store a boolean grid, rows/columns correspond to nodes
– Alternative to Adjacency List
F T F T F
F F F F T
T F F F T
F F T F F
F F T F F
![Page 13: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/13.jpg)
13
Edge List• Store a Vector<Edge> (or Set<Edge>)
– Edge struct would have the two nodesVector<Edge>
![Page 14: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/14.jpg)
14
Edge Properties• Not all edges are created equally
– Some have greater weight• Real life examples:
– Flight costs– Miles on a road– Time spent on a road
• Store a number with each edge corresponding to its weight
Source: https://www.google.com/maps
![Page 15: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/15.jpg)
15
Paths• I want a job at Google. Do I know anyone who works there? What
about someone who knows someone?• I want to find this word on a board made of letters "next to" each
other (Boggle)• A path is a sequence of nodes with edges between them
connecting two nodes– Could store edges instead of nodes (why?)– You know Jane. Jane knows
Sally. Sally knows knows Sergey Brin, the founder of Google, so the path is:You->Jane->Sally->Sergey
![Page 16: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/16.jpg)
16
Other graph properties• reachable: Vertex u is reachable from v
if a path exists from u to v.
• connected: A graph is connected if everyvertex is reachable from every other.
• complete: If every vertex has a directedge to every other.
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
a
c
b
d
a
c
b
d
e
![Page 17: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/17.jpg)
17
Loops and cycles• cycle: A path that begins and ends at the same node.
– example: {b, g, f, c, a} or {V, X, Y, W, U, V}.– example: {c, d, a} or {U, W, V, U}.
– acyclic graph: One that doesnot contain any cycles.
• loop: An edge directly froma node to itself.– Many graphs don't allow loops.
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
![Page 18: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/18.jpg)
18
Types of Graphs• Boggle?
– undirected, unweighted, cyclic, connected• A molecule?
– undirected, weighted, potentially cyclic, connected• A map of flights?
– directed, weighted, cyclic, perhaps not connected• A tree?
– directed, acyclic graph, not connected
![Page 19: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/19.jpg)
19
Announcements• Assn. 6 is due Thursday
![Page 20: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/20.jpg)
20
Finding Paths• Easiest way: Depth-First Search (DFS)
– Recursive backtracking!• Finds a path between two nodes if it exists
– Or can find all the nodes reachable from a node• Where can I travel to starting in San Francisco?• If all my friends (and their friends, and so on) share my post, how many will
eventually see it?
![Page 21: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/21.jpg)
21
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 22: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/22.jpg)
22
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 23: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/23.jpg)
23
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 24: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/24.jpg)
24
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 25: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/25.jpg)
25
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 26: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/26.jpg)
26
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 27: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/27.jpg)
27
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 28: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/28.jpg)
28
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 29: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/29.jpg)
29
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 30: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/30.jpg)
30
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 31: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/31.jpg)
31
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 32: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/32.jpg)
32
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 33: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/33.jpg)
33
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 34: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/34.jpg)
34
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 35: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/35.jpg)
35
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 36: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/36.jpg)
36
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 37: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/37.jpg)
37
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 38: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/38.jpg)
38
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 39: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/39.jpg)
39
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 40: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/40.jpg)
40
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 41: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/41.jpg)
41
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 42: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/42.jpg)
42
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 43: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/43.jpg)
43
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 44: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/44.jpg)
44
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 45: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/45.jpg)
45
DFS
If we've seen the node before, stopOtherwise, visit all the unvisited nodes from this node
![Page 46: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/46.jpg)
46
DFS Details• In an n-node, m-edge graph, takes O(m + n) time with an adjacency
list– Visit each edge once, visit each node at most once
• Pseudocode:dfs from v1:
mark v1 as seen.for each of v1's unvisited neighbors n:dfs(n)
• How could we modify the pseudocode to look for a specific path?– Recursive Backtracking– Look at maze example from earlier in the course
![Page 47: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/47.jpg)
47
Finding Shortest Paths• We can find paths between two nodes, but how can we find the
shortest path?– Fewest number of steps to complete a task?– Least amount of edits between two words?
• When have we solved this problem before?
![Page 48: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/48.jpg)
48
Breadth-First Search (BFS)• Idea: processing a node involves knowing we need to visit all its
neighbors (just like DFS)• Need to keep a TODO list of nodes to process• Which node from our TODO list should we process first if we want
the shortest path?– The first one we saw?– The last one we saw?– A random node?
![Page 49: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/49.jpg)
49
Breadth-First Search (BFS)• Keep a Queue of nodes as our TODO list• Idea: dequeue a node, enqueue all its neighbors• Still will return the same nodes as reachable, just might have
shorter paths
![Page 50: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/50.jpg)
50
BFS
a b c d
fe
g h i
queue: a
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 51: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/51.jpg)
51
BFS
a b c d
fe
g h i
queue: e, g
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 52: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/52.jpg)
52
BFS
a b c d
fe
g h i
queue: e, g
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 53: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/53.jpg)
53
BFS
a b c d
fe
g h i
queue: g, f
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 54: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/54.jpg)
54
BFS
a b c d
fe
g h i
queue: g, f
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 55: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/55.jpg)
55
BFS
a b c d
fe
g h i
queue: f, h
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 56: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/56.jpg)
56
BFS
a b c d
fe
g h i
queue: f, h
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 57: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/57.jpg)
57
BFS
a b c d
fe
g h i
queue: h
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 58: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/58.jpg)
58
BFS
a b c d
fe
g h i
queue: h
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 59: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/59.jpg)
59
BFS
a b c d
fe
g h i
queue: i
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 60: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/60.jpg)
60
BFS
a b c d
fe
g h i
queue: i
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 61: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/61.jpg)
61
BFS
a b c d
fe
g h i
queue: c
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 62: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/62.jpg)
62
BFS
a b c d
fe
g h i
queue: c
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
![Page 63: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/63.jpg)
63
BFS
Dequeue a nodeOtherwise, add all its unseen neighbors to the queue
a b c d
fe
g h i
queue: c
![Page 64: CS 106B, Lecture 23 Graphs - web.stanford.eduweb.stanford.edu/class/archive/cs/cs106b/cs106b... · A. Node = letter cube, Edge = Dictionary (lexicon) B. Node = dictionary word; Edge](https://reader034.vdocument.in/reader034/viewer/2022042212/5eb5aa55c1aae45a730b8b28/html5/thumbnails/64.jpg)
64
BFS Details• In an n-node, m-edge graph, takes O(m + n) time with an adjacency
list– Visit each edge once, visit each node at most once
• Pseudocode:bfs from v1:
add v1 to the queue.while queue is not empty:dequeue a node nenqueue n's unseen neighbors
• How could we modify the pseudocode to look for a specific path?