CS 106X, Lecture 22 Graphs BFS DFS: Programming Abstractions in C++, Chapter 18
CS 106X, Lecture 22 Graphs BFS DFS: Programming Abstractions in C++, Chapter 18
reading:
Programming Abstractions in C++, Chapter 18
This document is copyright (C) Stanford Computer Science and Nick Troccoli, 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, Marty Stepp, Ashley Taylor and others.
Plan For Today
• Recap: Graphs
• Practice: Twitter Influence
• Depth-First Search (DFS)
• Announcements
• Breadth-First Search (BFS)
2
Plan For Today
• Recap: Graphs
• Practice: Twitter Influence
• Depth-First Search (DFS)
• Announcements
• Breadth-First Search (BFS)
3
Graphs
A graph consists of a set of nodes connected by edges.
5
Graphs
A graph consists of a set of nodes connected by edges.
Nodes: degree (# connected edges) 5
Nodes: in-degree (directed, # in-
edges) 3
Nodes: out-degree (directed, # out- 2
edges) 6
6
Graphs
A graph consists of a set of nodes connected by edges.
Nodes: degree (# connected edges) 2
Nodes: in-degree (directed, # in-
edges) 1
Nodes: out-degree (directed, # out- 2
edges) 2
7
Graphs
A graph consists of a set of nodes connected by edges.
Nodes: degree (# connected edges) 3
Nodes: in-degree (directed, # in-
edges) 2
Nodes: out-degree (directed, # out- 0
edges) 4
8
Graphs
A graph consists of a set of nodes connected by edges.
Nodes: degree (# connected edges) A
Nodes: in-degree (directed, # in-
edges) C
Nodes: out-degree (directed, # out- B
edges) D
9
Graphs
A graph consists of a set of nodes connected by edges.
Nodes: degree (# connected edges) A
Nodes: in-degree (directed, # in-
edges) C
Nodes: out-degree (directed, # out- B
edges) D
10
Graphs
A graph consists of a set of nodes connected by edges.
Nodes: degree (# connected edges) A
Nodes: in-degree (directed, # in-
edges) C
Nodes: out-degree (directed, # out- B
edges) D
11
Graphs
A graph consists of a set of nodes connected by edges.
Nodes: degree (# connected edges) A
Nodes: in-degree (directed, # in-
edges) C
Nodes: out-degree (directed, # out- B
edges) D
12
Graph Properties
A graph is connected if every node is reachable from every
other node.
13
Graph Properties
A graph is complete if every node has a direct edge to every
other node.
14
Graph Properties
A graph is acyclic if it does not contain any cycles.
15
Graph Properties
A graph is directed if its edges have direction, or
undirected if its edges do not have direction (aka are
bidirectional).
directed undirected
16
Graph Properties
• Connected or unconnected
• Acyclic
• Directed or undirected
• Weighted or unweighted
• Complete
17
Plan For Today
• Recap: Graphs
• Practice: Twitter Influence
• Depth-First Search (DFS)
• Announcements
• Breadth-First Search (BFS)
18
Twitter Influence
• Twitter lets a user follow another user to see their
posts.
• Following is directional (e.g. I can follow you but you
don’t have to follow me back L)
• Let’s define being influential as having a high number
of followers-of-followers.
– Reasoning: doesn’t just matter how many people follow
you, but whether the people who follow you reach a
large audience.
21
Searching for paths
• Searching for a path from one vertex to another:
– Sometimes, we just want any path (or want to know there is a path).
– Sometimes, we want to minimize path length (# of edges).
– Sometimes, we want to minimize path cost (sum of edge weights).
$50 PVD
$70 ORD
SFO
$2
$130
00
70
$80
$1 LGA
$60
$250 $140
HNL
LAX
$120
100
DFW $110 $
$500 MIA
22
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?
23
Depth-first search (18.4)
• depth-first search (DFS): Finds a path between two vertices by
exploring each possible path as far as possible before backtracking.
– Often implemented recursively.
– Many graph algorithms involve visiting or marking vertices.
A B C D
E F
25
DFS
A B C D
E F
26
DFS
A B C D
E F
27
DFS
A B C D
E F
28
DFS
A B C D
E F
29
DFS
A B C D
E F
30
DFS
A B C D
E F
31
DFS
A B C D
E F
32
DFS
A B C D
E F
33
DFS
A B C D
E F
34
DFS
A B C D
E F
35
DFS
A B C D
E F
36
DFS
A B C D
E F
37
DFS
A B C D
E F
38
DFS
A B C D
E F
39
DFS
A B C D
E F
40
DFS
A B C D
E F
41
DFS
A B C D
E F
42
DFS
A B C D
E F
43
DFS
A B C D
E F
44
DFS
A B C D
E F
45
DFS
A B C D
E F
46
DFS
A B C D
E F
47
DFS
A B C D
E F
48
DFS
A B C D
E F
49
DFS
A B C D
E F
50
DFS
A B C D
E F
51
DFS
A B C D
E F
52
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)
53
DFS that finds path
dfs from v1 to v2:
mark v1 as visited, and add to path. a b c
perform a dfs from each of v1's
unvisited neighbors n to v2: d e f
if dfs(n, v2) succeeds: a path is found! yay!
if all neighbors fail: remove v1 from path.
g h i
54
DFS observations
• discovery: DFS is guaranteed to
find a path if one exists. a b c
d e f
• retrieval: It is easy to retrieve exactly
what the path is (the sequence of g h i
edges taken) if we find it
– choose - explore - unchoose
55
Plan For Today
• Recap: Graphs
• Practice: Twitter Influence
• Depth-First Search (DFS)
• Announcements
• Breadth-First Search (BFS)
56
Announcements
• Assignment 7 will go out this Friday, is due Wed. after break
– Short graphs assignment (Google Maps!), implementing algorithms
from this week
• Assignment 8 will go out the Wed. after break, is due the last day of
class (Fri)
– Graphs and inheritance assignment (Excel!)
57
Plan For Today
• Recap: Graphs
• Practice: Twitter Influence
• Depth-First Search (DFS)
• Announcements
• Breadth-First Search (BFS)
58
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?
59
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
60
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
61
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: a
62
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: e, g
63
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: e, g
64
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: g, f
65
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: g, f
66
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: f, h
67
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: f, h
68
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: h
69
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: h
70
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: i
71
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: i
72
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: c
73
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: c
74
BFS
a b c d
e f
Dequeue a node
g h i
add all its unseen
neighbors to the queue
queue: c
75
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
• DFS uses less memory than BFS, easier to reconstruct the path once
found; but DFS does not always find shortest path. BFS does.
77
Recap
• Recap: Graphs
• Practice: Twitter Influence
• Depth-First Search (DFS)
• Announcements
• Breadth-First Search (BFS)
78
Overflow
79
BFS that finds path
bfs from v1 to v2:
create a queue of vertexes to visit, a b c
initially storing just v1. prev
mark v1 as visited. d e f
while queue is not empty and v2 is not seen:
g h i
dequeue a vertex v from it,
mark that vertex v as visited,
and add each unvisited neighbor n of v to the queue,
while setting n's previous to v.
80