SLIDE 1 CS302 Topic: Graph Wrapup
Tuesday,
SLIDE 2 The Kevin Bacon Game
Link a movie actor to Kevin Bacon via shared movie roles.
- For example:
- Tom Hanks has Bacon number
- f 1 (Hanks and Bacon were in
Apollo 13 together)
- Sally Fields has Bacon number
(she was in Forrest Gump with Hanks, who was in Apollo 13 with Bacon)
- Question:
- How do you find an actor’s
Bacon number? Kevin Bacon
SLIDE 3 The Kevin Bacon Game (con’t)
- Answer:
- Vertex = actor
- Edge (u,v) actors u and v had shared movie role
- Do BFS from Bacon to find other actors’ Bacon
Number
SLIDE 4 Crazy Football Rankings
- Input: list of football game scores
(no ties)
- Proof of “betterness”: Use silly
transitivity to “prove” that a team is better than another
- Question:
- Given scores, and 2 teams (X and
Y), how do you prove that X is better than Y (or vice versa), or indicate that no proof exists?
SLIDE 5 Crazy Football Rankings (con’t.)
- Answer:
- Vertex = team
- Edge (u,v) team u
defeated v
- Given X, Y, find path from X
to Y (or Y to X).
UT LSU FL Vandy AL
SLIDE 6 Word changes
- A word can be changed to another word by a 1-
character substitution.
- Assume a dictionary of 5-letter words exists.
- Question:
- How can we determine if word A can be
transformed to word B by a series of 1-character substitutions (and give the sequence of words)?
- Example:
- A: bleed
- B: blood
- A turns into B through sequence bleed, blend,
blond, blood
SLIDE 7 Word changes (con’t.)
Answer:
- Vertex = word
- Edge from (u,v) Words u and v differ by one
character
- Given X and Y, find path from X to Y
blood bleed blond blend flood bland breed
SLIDE 8 Arbitrage
Input:
- Collection of currencies and their exchange rates
Question:
- Is there a series of exchanges that makes
money instantly?
Example:
X Y Z X 1 0.5 1/ 3 Y 2 1 0.5 Z 3 2 1 300 Z’s buys 100 X’s, which will buy 200 Y’s, which will buy 400 Z’s Made 100 Z’s just through exchanges
SLIDE 9 Arbitrage (con’t.)
Answer:
- Vertex = currency
- Edge (u,v) has weight log C, where exchange
rate from u to v is C.
- Search for negative cycle – this represents
arbitrage play
Note:
multiplication of exchange rates But, when finding paths, we add costs. So, convert to form that works when adding costs: log( ) (log ) each edge is the log of the exchange rate
i i i i i i
r r r ∏ = ∏ = ⇒
∑
Y Z X 0.48
0.3 0.3
SLIDE 10
Team competition
Winning team: 10 points homework extra credit Losing team: 3 points homework extra credit
SLIDE 11
(1) DFS
Give the order that the vertices are visited using DFS on this graph, starting at v0, with the tie- breaking rule being that vertices are visited alphabetically:
v6 v2 v3 v0 v5 v1 v4
Answer: v0 v1 v3 v5 v6 v4 v2
SLIDE 12
(2) BFS
Give the order that the vertices are visited using BFS on this graph, starting at v0, with the tie- breaking rule being that vertices are visited alphabetically:
v6 v2 v3 v0 v5 v1 v4
Answer: v0 v1 v4 v3 v5 v6 v2
SLIDE 13
(3) Airline flights
Suppose you have a directed graph representing all the flights that an airline flies. What algorithm might be used to find the best sequence of connections from one city to another?
a) Breadth first search b) Depth first search c) A cycle-finding algorithm d) A shortest-path algorithm Answer: d
SLIDE 14
(4) Topological Sort
Give a topological sort of the following graph:
v6 v2 v3 v0 v5 v1 v4
Some possible answers: v2 v0 v4 v1 v3 v6 v5 v2 v0 v1 v3 v6 v5 v4 v2 v0 v1 v3 v4 v5 v6
SLIDE 15
(5) Adjacency matrix representation
If G is a directed graph with 20 vertices, how many boolean values will be needed to represent G using an adjacency matrix?
a) 20 b) 40 c) 200 d) 400 Answer: d
SLIDE 16
(6) Adjacency list representation
How many linked lists are used to represent a graph with n nodes and m edges, using an adjacency list representation?
a) m b) n c) m + n d) m * n Answer: b
SLIDE 17
(7) Spanning trees
How many spanning trees does the following graph have?
a) 4 b) 8 c) 3 d) 6
A C F D B E
Answer: a
SLIDE 18
(8) Kruskal’s algorithm
Using Kruskal’s algorithm which edge should we choose second?
a) CD b) AB c) AC d) BD
A D B E C 25 10 5 30 11 22 18 15 8 20
Answer: c
SLIDE 19
(9) Kruskal’s algorithm
Using Kruskal’s algorithm which edge should we choose third?
a) AC b) CD c) AB d) BD
A D B E C 25 10 5 30 11 22 18 15 8 20
Answer: d
SLIDE 20
(10) Trees
Which of the following are trees?
Answer: Graphs 1 and 3
SLIDE 21
(11) Network Flow
What is the value of the maximum flow in the following graph?
s C F t B E 3 2 1 1 4 3 2 3
Answer: 4
SLIDE 22 (12) Dijkstra’s algorithm
stage of Dijkstra’s algorithm operating on this graph.
selected?
v1 v6 v2 v7 v3 v4 v5 4 10 1 3 2 6 4 8 5 1 v1 v2 v3 v4 v5 v6 v7 T F F T F F F 2 3 1 3 9 5 v1 v4 v1 v4 v4 v4 2 Known? dv pv
Answer: v2
2
SLIDE 23 (13) Dijkstra’s algorithm
selected for processing by Dijkstra’s algorithm.
the dv column?
v1 v6 v2 v7 v3 v4 v5 4 10 1 3 2 6 4 8 5 1 2 v1 v2 v3 v4 v5 v6 v7 T F F T F F F 2 3 1 3 9 5 v1 v4 v1 v4 v4 v4 Known? dv pv 2
Answer: no changes made
SLIDE 24
(14) Articulation points
Name all the articulation points of this graph
E B C H J I K F G D A
Answer: C, E, F