3. Graph
A set of objects where some pairs of objects are
connected by links
road map
airline flights
internet connection
skill tree
Tree is special kind of graph
Vertex
(node)
Edge
(arc)
3
8. Path and Cycle
Path
Sequence of vertices that are connected by edges
[v_1, v_5, v_4, v_6]
Cycle
A path that starts and ends at same vertex
[v_2, v_3, v_4, v_5, v_2]
8
28. Choose Better Next Vertex
Visit hard-to-reach corners early
Use the middle square to hop across board
only when necessary
28
30. General Depth First Search (DFS)
Knight’s Tour is special case of depth first
search
create the depth first tree
General depth first search is to search as
deeply as possible
30
34. P versus NP problem
P: questions for which some algorithms can
provide an answer in polynomial time
NP: questions for which an answer can be
verified in polynomial time
Subset sum problem
{-7, -3, -2, 5, 8} -> {-3, -2, 5}
34
36. NP-complete
Reduction: algorithm for transforming one
problem into another problem
NP-complete: a problem p in NP is NP-
Complete if every other problem in NP can
be transformed into p in polynomial time
hardest problems in NP
36
37. NP-hard
Decision problem: any arbitrary yes-or-no
question on an infinite set of inputs
NP-hard: a decision problem H in NP-hard
when for any problem L in NP, there is
polynomial-time reduction from L to H
at least as hard as the hardest problems in
NP
37
38. Topological Sorting
Linear ordering of all vertices of a directed
graph
(u, v) is an edge of directed graph and u is
before v in ordering
38
42. Lemma
A directed graph G is acyclic if and only if a
depth-first search of G yields no back edges.
Back edge: edge (u, v) connecting vertex u to
an ancestor v in depth-first tree (v ↝ u → v)
=>: back edge exists then cycle exists
<=: cycle exists then back edge exists
42
43. Proof of Implementation
For any pair of distinct vertices u, v ∈ V, if there is an
edge in G from u to v, then f[v] < f[u].
Consider edge (u, v) explored by DFS, v cannot be
gray since then that edge would be back edge.
Therefore v must be white or black.
If v is white, v is descendant of u, and so f[v] < f[u]
If v is black, it has already been finished, so that
f[v] < f[u]
43
44. Strongly Connected Components
G = (V, E) and C ⊂ V
such that ∀ (v_i, v_j) ∈ C, path v_i to v_j
and path v_j to v_i both exist
C is strongly connected components (SCC)
44
46. Implementation
1. Call dfs(g)
2. Compute transpose of g as g_t
3. Call dfs(g_t) but explore each vertex in
decreasing order of finish time
4. Each tree of step 3 is a SCC
original transpose
46
49. Lemma
Let C and C’ be distinct SCCs in directed
graph G = (V, E), let u, v ∈ C, let u’, v’ ∈ C’,
and suppose that there is a path u ↝ u’ in G.
Then there cannot also be a path v’ ↝ v in G.
if v’ ↝ v exists then both u ↝ u’ ↝ v’ and v’
↝ v ↝ u exist
C and C’ are not distinct SCCs
49
50. Lemma
Let C and C’ be distinct SCCs in directed
graph G = (V, E). Suppose that there is an
edge (u, v) ∈ E, where u ∈ C and v ∈ C’. Then
f(C) > f(C’)
from x ∈ C to w ∈ C’
from y ∈ C’ cannot reach any vertex in C
50
51. Corollary
Let C and C’ be distinct SCCs in directed
graph G = (V, E). Suppose that there is an
edge (u, v) ∈ ET, where u ∈ C and v ∈ C’.
Then f(C) < f(C’).
(v, u) ∈ E then f(C’) < f(C)
51
53. Shortest Path Problem
Find the path with the smallest total weight
along which to route any given message
53
54. Dijkstra’s Algorithm
Iterative algorithm providing the shortest
path from one particular starting node to all
other nodes in graph
Path distance, priority queue
54