You are on page 1of 118

Graphs

1843 ORD
SFO

802
4 3

337
7
1
1233
LAX DFW

© 2004 Goodrich, Tamassia Graphs 1


Graphs (§ 12.1)
A graph is a pair (V, E), where
 V is a set of nodes, called vertices
 E is a collection of pairs of vertices, called edges
 Vertices and edges are positions and store elements
Example:
 A vertex represents an airport and stores the three-letter
airport code
 An edge represents a flight route between two airports and
stores the mileage of the route 849 PVD
1843 ORD 2
SFO 14
43 802 LGA
337

1 7 7
8 10
HNL 2555 13 99
1233
LAX DFW 1120
MIA
© 2004 Goodrich, Tamassia Graphs 2
Edge Types
Directed edge
 ordered pair of vertices (u,v)
flight
 first vertex u is the origin ORD AA 1206 PVD
 second vertex v is the
destination
 e.g., a flight
Undirected edge 849
unordered pair of vertices (u,v) ORD PVD

miles
 e.g., a flight route
Directed graph
 all the edges are directed
 e.g., route network
Undirected graph
 all the edges are undirected
 e.g., flight network

© 2004 Goodrich, Tamassia Graphs 3


Applications
cslab1a cslab1b

Electronic circuits math.brown.edu


 Printed circuit board
 Integrated circuit cs.brown.edu
Transportation
networks brown.edu
 Highway network qwest.net
att.net
 Flight network
Computer networks
 Local area network cox.net
 Internet John

 Web Paul
David

Databases
 Entity-relationship
© 2004 Goodrich, Tamassia Graphs 4
Terminology
End vertices (or endpoints)
of an edge
 U and V are the endpoints V
of a a b
Edges incident on a vertex h j
 a, d, and b are incident on U d X Z
V
Adjacent vertices c e i
 U and V are adjacent W g
Degree of a vertex
 X has degree 5 f
Parallel edges Y
 h and i are parallel edges
Self-loop
 j is a self-loop
© 2004 Goodrich, Tamassia Graphs 5
Terminology (cont.)
Path
 sequence of alternating
vertices and edges
V
 begins with a vertex a b
 ends with a vertex P1
 each edge is preceded and d
followed by its endpoints U X Z
P2 h
Simple path c e
 path such that all its
vertices and edges are W g
distinct
Examples f
 P1=(V,b,X,h,Z) is a simple Y
path
 P2=(U,c,W,e,X,g,Y,f,W,d,V)
is a path that is not simple
© 2004 Goodrich, Tamassia Graphs 6
Terminology (cont.)
Cycle
 circular sequence of
alternating vertices and
edges V
a b
 each edge is preceded and
followed by its endpoints d
Simple cycle
U X Z
C2 h
 cycle such that all its e C1
vertices and edges are c
distinct W g
Examples
C1=(V,b,X,g,Y,f,W,c,U,a,↵) is f

Y
a simple cycle
 C =(U,c,W,e,X,g,Y,f,W,d,V,a,
2
↵) is a cycle that is not
simple
© 2004 Goodrich, Tamassia Graphs 7
Properties
Property 1 Notation
Σ v deg(v) = 2m n number of
Proof: each edge is vertices
counted twice m number of edges
Property 2 deg(v) degree of vertex
In an undirected graph vExample
with no self-loops
and no multiple
 n= 4
edges  m= 6
m ≤ n (n − 1)/2
Proof: each vertex has
 deg(v) = 3
degree at most (n −
1)

What is the bound for


a directed
© 2004 Goodrich, Tamassia graph? Graphs 8
Main Methods of the Graph
ADT
Update methods
Vertices and edges  insertVertex(o): insert a
 are positions vertex storing element o
 store elements  insertEdge(v, w, o):
Accessor methods insert an edge (v,w)
storing element o
 endVertices(e): an array
of the two endvertices of
 removeVertex(v):
e remove vertex v (and its
incident edges)
 opposite(v, e): the
vertex opposite of v on e
 removeEdge(e): remove
edge e
 areAdjacent(v, w): true
iff v and w are adjacent Iterator methods
 replace(v, x): replace
 incidentEdges(v): edges
element at vertex v with incident to v
x  vertices(): all vertices in
 replace(e, x): replace the graph
element at edge e with x  edges(): all edges in the
graph

© 2004 Goodrich, Tamassia Graphs 9


Structure (§
12.2.1)
Vertex object u
 element a c
 reference to position b d
in vertex sequence v w z
Edge object
 element
 origin vertex object
 destination vertex
object u v w z
 reference to position
in edge sequence
Vertex sequence
a b c d
 sequence of vertex
objects
Edge sequence
 sequence of edge
© 2004 Goodrich,objects
Tamassia Graphs 10
Adjacency List
Structure (§ 12.2.2)
Edge list structure a v b
Incidence u w
sequence for each
vertex
 sequence of
references to
edge objects of u v w
incident edges
Augmented edge
objects
 references to
associated
positions in
incidence a b
sequences of
end vertices

© 2004 Goodrich, Tamassia Graphs 11


Adjacency Matrix
Structure (§ 12.2.3)
Edge list structure a v b
Augmented vertex u w
objects
 Integer key (index)
associated with
vertex
2D-array adjacency 0 u 1 v 2 w
array
 Reference to edge
0 1 2
object for adjacent
vertices 0 ∅ ∅
 Null for non 1 ∅
nonadjacent
vertices a 2 ∅ ∅ b
The “old fashioned”
version just has 0 for
no edge and 1 for
Graphs 12
edge Tamassia
© 2004 Goodrich,
Asymptotic Performance
n vertices, m edges
E
no parallel edges Adjacency Adjacenc
dge
no self-loops List y Matrix
Bounds are “big- List
Oh”
Space n+m n+m n2
incidentEdges(v) m deg(v) n
areAdjacent (v,
m min(deg(v), deg(w)) 1
w)
insertVertex(o) 1 1 n2
insertEdge(v, w,
1 1 1
o)
removeVertex(v
m deg(v) n2
)
removeEdge(e) 1 1 1

© 2004 Goodrich, Tamassia Graphs 13


Depth-First Search
A

B D E

© 2004 Goodrich, Tamassia Graphs 14


Subgraphs
A subgraph S of a
graph G is a graph
such that
 The vertices of S are a
subset of the vertices of Subgraph
G
 The edges of S are a
subset of the edges of
G
A spanning subgraph
of G is a subgraph that
contains all the Spanning subgraph
vertices of G
© 2004 Goodrich, Tamassia Graphs 15
Connectivity
A graph is
connected if there
is a path between
every pair of Connected graph
vertices
A connected
component of a
graph G is a
maximal
connected Non connected graph with
subgraph of G two connected
components
© 2004 Goodrich, Tamassia Graphs 16
Trees and Forests
A (free) tree is an
undirected graph T
such that
 T is connected
 T has no cycles
Tree
This definition of tree is
different from the one
of a rooted tree
A forest is an
undirected graph
without cycles
The connected
components of a Forest
forest are trees
© 2004 Goodrich, Tamassia Graphs 17
Spanning Trees and
Forests
A spanning tree of a
connected graph is a
spanning subgraph that
is a tree
A spanning tree is not
unique unless the
graph is a tree Graph
Spanning trees have
applications to the
design of
communication
networks
A spanning forest of a
graph is a spanning
subgraph that is a
forest Spanning tree
© 2004 Goodrich, Tamassia Graphs 18
Depth-First Search (§
12.3.1)
Depth-first search DFS on a graph with
(DFS) is a general n vertices and m
technique for edges takes O(n + m )
traversing a graph time
A DFS traversal of a DFS can be further
graph G extended to solve
 Visits all the vertices other graph
and edges of G problems
 Determines whether  Find and report a path
G is connected between two given
 Computes the vertices
connected  Find a cycle in the
components of G graph
 Computes a spanning Depth-first search is
forest of G
© 2004 Goodrich, Tamassia Graphs to graphs what Euler19
DFS Algorithm
The algorithm uses a
mechanism for setting and Algorithm DFS(G, v)
getting “labels” of vertices Input graph G and a start vertex v of G
and edges
Algorithm DFS(G) Output labeling of the edges of G
Input graph G in the connected component of v
as discovery edges and back edges
Output labeling of the edges of G
as discovery edges and setLabel(v, VISITED)
back edges for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w ← opposite(v,e)
for all e ∈ G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
for all v ∈ G.vertices() DFS(G, w)
if getLabel(v) = UNEXPLORED else
DFS(G, v) setLabel(e, BACK)

© 2004 Goodrich, Tamassia Graphs 20


Example
A
A unexplored vertex
A visited vertex
B D E
unexplored edge
discovery edge C
back edge

A A

B D E B D E

C C

© 2004 Goodrich, Tamassia Graphs 21


Example (cont.)
A A

B D E B D E

C C

A A

B D E B D E

C C

© 2004 Goodrich, Tamassia Graphs 22


Properties of DFS
Property 1
DFS(G, v) visits all the
vertices and edges in
the connected A
component of v
Property 2 B D E
The discovery edges
labeled by DFS(G, v)
form a spanning tree C
of the connected
component of v

© 2004 Goodrich, Tamassia Graphs 23


Analysis of DFS
Setting/getting a vertex/edge label takes O(1)
time
Each vertex is labeled twice
 once as UNEXPLORED
 once as VISITED
Each edge is labeled twice
 once as UNEXPLORED
 once as DISCOVERY or BACK
Method incidentEdges is called once for each
vertex
DFS runs in O(n + m) time provided the graph is
represented by the adjacency list structure

Recall that Σ v deg(v) = 2m
© 2004 Goodrich, Tamassia Graphs 24
Path Finding
We can specialize the
DFS algorithm to find a Algorithm pathDFS(G, v, z)
path between two setLabel(v, VISITED)
given vertices u and z S.push(v)
using the template if v = z
method pattern return S.elements()
for all e ∈ G.incidentEdges(v)
We call DFS(G, u) with u if getLabel(e) = UNEXPLORED
as the start vertex w ← opposite(v,e)
We use a stack S to if getLabel(w) = UNEXPLORED
keep track of the path setLabel(e, DISCOVERY)
between the start S.push(e)
vertex and the current pathDFS(G, w, z)
vertex S.pop(e)
As soon as destination else
vertex z is setLabel(e, BACK)
encountered, we S.pop(v)
return the path as the Graphs 25
© 2004 Goodrich, Tamassia
contents of the stack
Cycle Finding
Algorithm cycleDFS(G, v, z)
We can specialize the setLabel(v, VISITED)
DFS algorithm to find S.push(v)
a simple cycle using for all e ∈ G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
the template method
w ← opposite(v,e)
pattern S.push(e)
We use a stack S to if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
keep track of the
pathDFS(G, w, z)
path between the S.pop(e)
start vertex and the else
current vertex T ← new empty stack
repeat
As soon as a back o ← S.pop()
edge (v, w) is T.push(o)
until o = w
encountered, we
return T.elements()
return the cycle as S.pop(v)
the portion of the Graphs 26
© 2004 Goodrich, Tamassia
stack from the top to
Breadth-First Search
L0
A

L1
B C D

L2
E F

© 2004 Goodrich, Tamassia Graphs 27


Breadth-First Search (§
12.3.3)
Breadth-first search BFS on a graph with
(BFS) is a general n vertices and m
technique for edges takes O(n + m )
traversing a graph time
A BFS traversal of a
graph G BFS can be further
 Visits all the vertices extended to solve
and edges of G other graph
 Determines whether problems
G is connected  Find and report a path
 Computes the with the minimum
connected number of edges
components of G between two given
 Computes a spanning vertices
forest of G
© 2004 Goodrich, Tamassia Graphs  Find a simple cycle,28
if
BFS Algorithm
The algorithm uses a Algorithm BFS(G, s)
mechanism for setting L0 ← new empty sequence
and getting “labels” of L0.insertLast(s)
vertices and edges setLabel(s, VISITED)
Algorithm BFS(G) i←0
while ¬Li.isEmpty()
Input graph G
Li +1 ← new empty sequence
Output labeling of the edges
and partition of the for all v ∈ Li.elements()
vertices of G for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
w ← opposite(v,e)
setLabel(u, UNEXPLORED)
if getLabel(w) = UNEXPLORED
for all e ∈ G.edges() setLabel(e, DISCOVERY)
setLabel(e, UNEXPLORED) setLabel(w, VISITED)
for all v ∈ G.vertices() Li +1.insertLast(w)
if getLabel(v) = UNEXPLORED else
BFS(G, v) setLabel(e, CROSS)
i ← i +1
© 2004 Goodrich, Tamassia Graphs 29
Example
L0
A
A unexplored vertex
A visited vertex L1
B C D
unexplored edge
discovery edge E F
cross edge

L0 L0
A A

L1 L1
B C D B C D

E F E F

© 2004 Goodrich, Tamassia Graphs 30


Example (cont.)
L0 L0
A A

L1 L1
B C D B C D

L2
E F E F

L0 L0
A A

L1 L1
B C D B C D

L2 L2
E F E F

© 2004 Goodrich, Tamassia Graphs 31


Example (cont.)
L0 L0
A A

L1 L1
B C D B C D

L2 L2
E F E F

L0
A

L1
B C D

L2
E F

© 2004 Goodrich, Tamassia Graphs 32


Properties
Notation A
Gs: connected component of s
Property 1 B C D
BFS(G, s) visits all the vertices
and edges of Gs
E F
Property 2
The discovery edges labeled by
BFS(G, s) form a spanning tree L0
Ts of Gs A
Property 3 L1
For each vertex v in Li B C D
 The path of Ts from s to v has i
L2
edges E F
 Every path from s to v in Gs has
at least i edges
© 2004 Goodrich, Tamassia Graphs 33
Analysis
Setting/getting a vertex/edge label takes O(1)
time
Each vertex is labeled twice
 once as UNEXPLORED
 once as VISITED
Each edge is labeled twice
 once as UNEXPLORED
 once as DISCOVERY or CROSS
Each vertex is inserted once into a sequence Li
Method incidentEdges is called once for each
vertex
BFS runs in O(n + m) time provided the graph is
represented by the adjacency list structure
Graphs 34
© 2004 Goodrich,
 Tamassia
Recall that Σ deg(v) = 2m
Applications
Using the template method pattern, we
can specialize the BFS traversal of a
graph G to solve the following problems
in O(n + m) time
 Compute the connected components of G
 Compute a spanning forest of G
 Find a simple cycle in G, or report that G is a
forest
 Given two vertices of G, find a path in G
between them with the minimum number of
edges, or report that no such path exists
© 2004 Goodrich, Tamassia Graphs 35
DFS vs. BFS
Applications DFS BFS
Spanning forest,
connected components, √ √
paths, cycles
Shortest paths √

Biconnected components √

L0
A A

L1
B C D B C D

L2
E F E F

DFS BFS
© 2004 Goodrich, Tamassia Graphs 36
DFS vs. BFS (cont.)
Back edge (v,w) Cross edge (v,w)
 w is an ancestor of v in  w is in the same level
the tree of discovery as v or in the next
edges level in the tree of
discovery edges

L0
A A

L1
B C D B C D

L2
E F E F

DFS BFS
© 2004 Goodrich, Tamassia Graphs 37
Directed Graphs BOS

ORD

JFK

SFO

DFW
LAX

MIA

© 2004 Goodrich, Tamassia Graphs 38


Digraphs (§ 12.4)
A digraph is a E
graph whose
edges are all D
directed
 Short for “directed C
graph”
B
Applications
 one-way streets A
 flights
 task scheduling
© 2004 Goodrich, Tamassia Graphs 39
E

Digraph Properties D

A graph G=(V,E) such that B


 Each edge goes in one direction: A
 Edge (a,b) goes from a to b, but not b to a.

If G is simple, m < n*(n-1).


If we keep in-edges and out-edges in
separate adjacency lists, we can perform
listing of in-edges and out-edges in time
proportional to their size.

© 2004 Goodrich, Tamassia Graphs 40


Digraph Application
Scheduling: edge (a,b) means task a must
be completed before b can be started
ics21 ics22 ics23

ics51 ics53 ics52


ics161

ics131 ics141 ics121 ics171

ics151 The good life

© 2004 Goodrich, Tamassia Graphs 41


Directed DFS
We can specialize the
traversal algorithms (DFS
and BFS) to digraphs by E
traversing edges only
along their direction
D
In the directed DFS
algorithm, we have four
types of edges C
 discovery edges
 back edges B
 forward edges
 cross edges
A directed DFS starting a
A
a vertex s determines the
vertices reachable from s

© 2004 Goodrich, Tamassia Graphs 42


Reachability
DFS tree rooted at v: vertices
reachable from v via directed paths
E D

E D C

A
C F
E D
A B
C F

A B
© 2004 Goodrich, Tamassia Graphs 43
Strong Connectivity
Each vertex can reach all other
vertices
a
g
c

d
e

b
f

© 2004 Goodrich, Tamassia Graphs 44


Connectivity
Algorithm
Pick a vertex v in G.
a
Perform a DFS from v in G. G: c
g
 If there’s a w not visited, print
“no”. d
e
Let G’ be G with edges b
f
reversed.
Perform a DFS from v in G’. a
 If there’s a w not visited, print G’: c
g
“no”.
 Else, print “yes”. d
e

f b

Running time: O(n+m).


© 2004 Goodrich, Tamassia Graphs 45
Strongly Connected
Components
Maximal subgraphs such that each vertex can
reach all other vertices in the subgraph
Can also be done in O(n+m) time using DFS,
but is more complicated (similar to
biconnectivity).
a
g {a,c,g}
c

d
e
b
{f,d,e,b}
f

© 2004 Goodrich, Tamassia Graphs 46


Transitive Closure
Given a digraph G, the D E
transitive closure of G is
the digraph G* such that B
 G* has the same G
C
vertices as G
A
 if G has a directed

path from u to v (u ≠ v),


G* has a directed D E
edge from u to v
The transitive closure B
provides reachability
C
information about a
digraph A G*
© 2004 Goodrich, Tamassia Graphs 47
Computing the
Transitive Closure
If there's a way to get
We can perform from A to B and from
DFS starting at B to C, then there's a
each vertex way to get from A to C.
 O(n(n+m))

Alternatively ... Use


dynamic
programming: The
Floyd-Warshall
Algorithm
© 2004 Goodrich, Tamassia Graphs 48
Floyd-Warshall
Transitive Closure
Idea #1: Number the vertices 1, 2,
…, n.
Idea #2: Consider paths that use
only vertices numbered 1, 2, …, k,
as intermediateUses
vertices:
only vertices numbered 1,…,k
(add this edge if it’s not already in)
i

Uses only vertices j


numbered 1,…,k-1 Uses only vertices
k numbered 1,…,k-1
© 2004 Goodrich, Tamassia Graphs 49
Floyd-Warshall’s Algorithm
Floyd-Warshall’s Algorithm FloydWarshall(G)
algorithm numbers the Input digraph G
vertices of G as v1 , …, vn Output transitive closure G* of G
i←1
and computes a series of
for all v ∈ G.vertices()
digraphs G0, …, Gn denote v as vi
 G =G
0 i←i+1
 G has a directed edge (v ,
k i G0 ← G
vj) if G has a directed path for k ← 1 to n do
from vi to vj with Gk ← Gk − 1
intermediate vertices in for i ← 1 to n (i ≠ k) do
the set {v1 , …, vk} for j ← 1 to n (j ≠ i, k) do
We have that Gn = G* if Gk − 1.areAdjacent(vi, vk) ∧
Gk − 1.areAdjacent(vk, vj)
In phase k, digraph Gk is
if ¬ Gk.areAdjacent(vi, vj)
computed from Gk − 1
Gk.insertDirectedEdge(vi, vj , k)
Running time: O(n ), 3
return Gn
© 2004assuming Graphs 50
areAdjacent
Goodrich, Tamassia is
Floyd-Warshall Example v7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2004 Goodrich, Tamassia Graphs 51
Floyd-Warshall, Iteration 1 v 7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2004 Goodrich, Tamassia Graphs 52
Floyd-Warshall, Iteration 2 v 7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2004 Goodrich, Tamassia Graphs 53
Floyd-Warshall, Iteration 3 v 7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2004 Goodrich, Tamassia Graphs 54
Floyd-Warshall, Iteration 4 v 7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2004 Goodrich, Tamassia Graphs 55
Floyd-Warshall, Iteration 5 v 7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2004 Goodrich, Tamassia Graphs 56
Floyd-Warshall, Iteration 6 v 7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2004 Goodrich, Tamassia Graphs 57
Floyd-Warshall, Conclusionv 7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2004 Goodrich, Tamassia Graphs 58
DAGs and Topological
Ordering
A directed acyclic graph (DAG) D E
is a digraph that has no
directed cycles B
A topological ordering of a
digraph is a numbering C
v1 , …, vn A DAG G
of the vertices such that for
every edge (vi , vj), we have i < j v4 v5
Example: in a task scheduling D E
digraph, a topological ordering v2
a task sequence that satisfies B
the precedence constraints v3
Theorem v1 C
A digraph admits a topological Topological
ordering if and only if it is a
A ordering of
DAG Graphs G 59
© 2004 Goodrich, Tamassia
Topological Sorting
Number vertices, so that (u,v) in E implies
u < v wake up 1 A typical student day
2 3
eat
study computer sci.

4 5
nap more c.s.
7
play
8
write c.s. program 6
9 work out
make cookies
for professors
10
sleep 11
dream about graphs
© 2004 Goodrich, Tamassia Graphs 60
Algorithm for Topological
Sorting
Note: This algorithm is different than
the one in Goodrich-Tamassia
Method TopologicalSort(G)
H← G // Temporary copy of G
n ← G.numVertices()
while H is not empty do
Let v be a vertex with no outgoing edges
Label v ← n
n← n-1
Remove v from H

Running time: O(n + m). How…?


© 2004 Goodrich, Tamassia Graphs 61
Topological Sorting
Algorithm using DFS
Simulate the algorithm by Algorithm topologicalDFS(G, v)
using depth-first search Input graph G and a start vertex v of G
Algorithm topologicalDFS(G) Output labeling of the vertices of G
in the connected component of v
Input dag G
setLabel(v, VISITED)
Output topological ordering of G
n ← G.numVertices() for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w ← opposite(v,e)
for all e ∈ G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
for all v ∈ G.vertices() topologicalDFS(G, w)
if getLabel(v) = UNEXPLORED else
topologicalDFS(G, v) {e is a forward or cross edge}
Label v with topological number n
O(n+m) time. n←n-1

© 2004 Goodrich, Tamassia Graphs 62


Topological Sorting
Example

© 2004 Goodrich, Tamassia Graphs 63


Topological Sorting
Example

© 2004 Goodrich, Tamassia Graphs 64


Topological Sorting
Example

© 2004 Goodrich, Tamassia Graphs 65


Topological Sorting
Example

7
8

© 2004 Goodrich, Tamassia Graphs 66


Topological Sorting
Example

7
8

© 2004 Goodrich, Tamassia Graphs 67


Topological Sorting
Example

6 5

7
8

© 2004 Goodrich, Tamassia Graphs 68


Topological Sorting
Example

4
6 5

7
8

© 2004 Goodrich, Tamassia Graphs 69


Topological Sorting
Example

3
4
6 5

7
8

© 2004 Goodrich, Tamassia Graphs 70


Topological Sorting
Example
2

3
4
6 5

7
8

© 2004 Goodrich, Tamassia Graphs 71


Topological Sorting
Example
2
1

3
4
6 5

7
8

© 2004 Goodrich, Tamassia Graphs 72


Shortest Paths
0
8 A 4
2
8 7 2 1 3
B C D

5 3 9 8
2 5
E F

© 2004 Goodrich, Tamassia Graphs 73


Weighted Graphs (§ 12.5)
In a weighted graph, each edge has an associated
numerical value, called the weight of the edge
Edge weights may represent, distances, costs, etc.
Example:
 In a flight route graph, the weight of an edge represents
the distance in miles between the endpoint airports

849 PVD
1843 ORD 42
SFO 1

802

1205
43 LGA
337

7
1 7
8 10
HNL 2555 13 99
1233
LAX DFW 1120
MIA
© 2004 Goodrich, Tamassia Graphs 74
Shortest Paths (§ 12.6)
Given a weighted graph and two vertices u and v, we
want to find a path of minimum total weight between u
and v.
 Length of a path is the sum of the weights of its edges.
Example:
 Shortest path between Providence and Honolulu
Applications
 Internet packet routing
 Flight reservations
849 PVD
 Driving directions 1843 ORD 2
SFO 14
802

1205
43 LGA
337

1 7 7
8 10
HNL 2555 13 99
1233
LAX DFW 1120
MIA
© 2004 Goodrich, Tamassia Graphs 75
Shortest Path Properties
Property 1:
A subpath of a shortest path is itself a shortest path
Property 2:
There is a tree of shortest paths from a start vertex to all the
other vertices
Example:
Tree of shortest paths from Providence

849 PVD
1843 ORD 2
SFO 14
802

1205
43 LGA
337

1 7 7
8 10
HNL 2555 13 99
1233
LAX DFW 1120
MIA
© 2004 Goodrich, Tamassia Graphs 76
Dijkstra’s Algorithm (§
12.6.1)
The distance of a We grow a “cloud” of
vertex v from a vertex vertices, beginning with s
s is the length of a and eventually covering all
shortest path the vertices
between s and v We store with each vertex v
Dijkstra’s algorithm a label d(v) representing the
computes the distance of v from s in the
distances of all the subgraph consisting of the
vertices from a given cloud and its adjacent
start vertex s vertices
Assumptions: At each step
 the graph is  We add to the cloud the
connected vertex u outside the cloud
 the edges are with the smallest distance
undirected label, d(u)
Graphs 77
© 2004 Goodrich,
 theTamassia
edge weights are  We update the labels of the
Edge Relaxation
Consider an edge e = (u,z)
such that d(u) = 50
10 d(z) = 75
 u is the vertex most e
recently added to the u
s z
cloud
 z is not in the cloud

The relaxation of edge e


updates distance d(z) as
follows: d(u) = 50
10 d(z) = 60
d(z) ← min{d(z),d(u) + weight(e)} u e
s z

© 2004 Goodrich, Tamassia Graphs 78


Example
0 0
8 A 4 8 A 4
2 2
8 7 2 1 4 8 7 2 1 3
B C D B C D

∞ 3 9 ∞ 5 3 9 8
2 5 2 5
E F E F

0 0
8 A 4 8 A 4
2 2
8 7 2 1 3 7 7 2 1 3
B C D B C D

5 3 9 11 5 3 9 8
2 5 2 5
E F E F
© 2004 Goodrich, Tamassia Graphs 79
Example (cont.)
0
8 A 4
2
7 7 2 1 3
B C D

5 3 9 8
2 5
E F
0
8 A 4
2
7 7 2 1 3
B C D

5 3 9 8
2 5
E F

© 2004 Goodrich, Tamassia Graphs 80


Dijkstra’s Algorithm
A priority queue Algorithm DijkstraDistances(G, s)
stores the vertices Q ← new heap-based priority queue
for all v ∈ G.vertices()
outside the cloud
if v = s
 Key: distance setDistance(v, 0)
 Element: vertex else
Locator-based setDistance(v, ∞)
methods l ← Q.insert(getDistance(v), v)
 insert(k,e) returns a setLocator(v,l)
locator while ¬Q.isEmpty()
 replaceKey(l,k) u ← Q.removeMin()
changes the key of for all e ∈ G.incidentEdges(u)
an item { relax edge e }
z ← G.opposite(u,e)
We store two labels r ← getDistance(u) + weight(e)
with each vertex: if r < getDistance(z)
 Distance (d(v) label) setDistance(z,r)
 locator in priority Q.replaceKey(getLocator(z),r)
queue Graphs 81
© 2004 Goodrich, Tamassia
Analysis of Dijkstra’s
Algorithm
Graph operations
 Method incidentEdges is called once for each vertex
Label operations
 We set/get the distance and locator labels of vertex z
O(deg(z)) times
 Setting/getting a label takes O(1) time
Priority queue operations
 Each vertex is inserted once into and removed once from
the priority queue, where each insertion or removal takes
O(log n) time
 The key of a vertex in the priority queue is modified at most
deg(w) times, where each key change takes O(log n) time
Dijkstra’s algorithm runs in O((n + m) log n) time provided
the graph is represented by the adjacency list structure

Recall that Σ v deg(v) = 2m
The running time can also be expressed as O(m log n)
sinceTamassia
© 2004 Goodrich, the graph is connected
Graphs 82
Shortest Paths Tree
Using the template Algorithm DijkstraShortestPathsTree(G, s)
method pattern, we
can extend …
Dijkstra’s algorithm
to return a tree of for all v ∈ G.vertices()
shortest paths from …
setParent(v, ∅)
the start vertex to

all other vertices
We store with each for all e ∈ G.incidentEdges(u)
vertex a third label: { relax edge e }
 parent edge in the z ← G.opposite(u,e)
shortest path tree r ← getDistance(u) + weight(e)
In the edge if r < getDistance(z)
relaxation step, we setDistance(z,r)
update the parent setParent(z,e)
Q.replaceKey(getLocator(z),r)
label
© 2004 Goodrich, Tamassia Graphs 83
Why Dijkstra’s Algorithm
Works
Dijkstra’s algorithm is based on the
greedy method. It adds vertices by
increasing distance.
Suppose it didn’t find all
shortest distances. Let F be 0
the first wrong vertex the 8 A 4
algorithm processed. 2
 When the previous node, D, on
7 7 2 1 3
B C D
the true shortest path was
3 9
considered, its distance was 5 8
2 5
correct. E F
 But the edge (D,F) was

relaxed at that time!


 Thus, so long as d(F)>d(D), F’s

distance cannot be wrong.


That is, there is no wrong
Graphs 84
© 2004 Goodrich, Tamassia
vertex.
Why It Doesn’t Work for
Negative-Weight Edges
Dijkstra’s algorithm is based on the
greedy method. It adds vertices by
increasing distance.
0
8 A 4
 If a node with a negative 6
incident edge were to be 7 7 5 1 4
added late to the cloud, it B C D
could mess up distances for 0 -8
5 9
vertices already in the 2 5
E F
cloud.

C’s true distance is 1,


but it is already in the
cloud with d(C)=5!
© 2004 Goodrich, Tamassia Graphs 85
Bellman-Ford
Algorithm (not in book)
Works even with Algorithm BellmanFord(G, s)
negative-weight edges for all v ∈ G.vertices()
Must assume directed if v = s
edges (for otherwise we setDistance(v, 0)
would have negative- else
weight cycles) setDistance(v, ∞)
for i ← 1 to n-1 do
Iteration i finds all for each e ∈ G.edges()
shortest paths that use i { relax edge e }
edges. u ← G.origin(e)
Running time: O(nm). z ← G.opposite(u,e)
Can be extended to r ← getDistance(u) + weight(e)
detect a negative- if r < getDistance(z)
weight cycle if it exists setDistance(z,r)
 How?

© 2004 Goodrich, Tamassia Graphs 86


Bellman-Ford Example
Nodes are labeled with their d(v) values
8 0 4 8 0 4
-2 -2
7 1 8 7 -2 1 4
∞ ∞ ∞ ∞ ∞ ∞
3 9 3 9
-2 5 -2 5
∞ ∞ ∞ ∞

8 0 4 8 0 4
-2 -2
5 8 7 1 -1 7 1
-2 4 5 -2 -1
1 3 9 3 9
-2 6 9 5 -2 4 5
∞ ∞ 1 9
© 2004 Goodrich, Tamassia Graphs 87
DAG-based Algorithm
(not in book)
Algorithm DagDistances(G, s)
for all v ∈ G.vertices()
Works even with if v = s
negative-weight setDistance(v, 0)
edges else
Uses topological setDistance(v, ∞)
order Perform a topological sort of the vertices
Doesn’t use any for u ← 1 to n do {in topological order}
fancy data structures for each e ∈ G.outEdges(u)
{ relax edge e }
Is much faster than z ← G.opposite(u,e)
Dijkstra’s algorithm r ← getDistance(u) + weight(e)
Running time: if r < getDistance(z)
O(n+m). setDistance(z,r)

© 2004 Goodrich, Tamassia Graphs 88


DAG Example
Nodes are labeled with their d(v) values
1 1
8 0 4 8 0 4
-2 -2
3 7 2 1 4 3 8 7 2 -2 1 4 4
∞ ∞ ∞ ∞ ∞ ∞
3 9 3 9
-5 5 -5 5
∞ ∞ ∞ ∞
6 5 6 5

1 1

8 0 4 8 0 4
-2 -2
3 5 7 2 1 4
-1 3 7 2 1 4
8 -2 4 5 -2 -1
3 9 3 9
1 7 0 4
-5 5 -5 5
∞ ∞ 1 7
6 5 6 5
Graphs (two steps) 89
© 2004 Goodrich, Tamassia
Minimum Spanning Trees
2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 90


Minimum Spanning Trees (§
12.7)
Spanning subgraph
 Subgraph of a graph G ORD 10
containing all the vertices
1
of G PIT
Spanning tree DEN 6 7
 Spanning subgraph that is 9
itself a (free) tree 3 DCA
Minimum spanning tree (MST) STL
4
 Spanning tree of a
weighted graph with 8 5 2
minimum total edge
weight
DFW ATL
Applications
 Communications networks
 Transportation networks
© 2004 Goodrich, Tamassia Graphs 91
Cycle Property
f 8
Cycle Property: 4
 Let T be a minimum C 9
spanning tree of a 2 6
weighted graph G 3 e
 Let e be an edge of G 8 7
that is not in T and C
let be the cycle 7
formed by e with T
Replacing f with e yields
 For every edge f of C,
a better spanning tree
weight(f) ≤ weight(e)
Proof: f 8
 By contradiction 4
 If weight(f) > weight(e) we
C 9
can get a spanning 2 6
3 e
tree of smaller weight 7
by replacing e with f 8
7
© 2004 Goodrich, Tamassia Graphs 92
Partition Property U V
f 7
Partition Property: 4
 Consider a partition of the 9
vertices of G into subsets U and 2 5
V 8
 Let e be an edge of minimum 8 e 3
weight across the partition
 There is a minimum spanning
7
tree of G containing edge e Replacing f with e yields
Proof: another MST
 Let T be an MST of G
U V
 If T does not contain e, consider f 7
the cycle C formed by e with T 4
and let f be an edge of C across 9
the partition 2 5
 By the cycle property,
8
weight(f) ≤ weight(e) 8 e 3
 Thus, weight(f) = weight(e)
7
 We obtain another MST by
© 2004 Goodrich, Tamassia Graphs 93
replacing f with e
Kruskal’s Algorithm (§
12.7.1)
A priority queue Algorithm KruskalMST(G)
stores the edges for each vertex V in G do
define a Cloud(v) of  {v}
outside the cloud let Q be a priority queue.
 Key: weight Insert all edges into Q using their
weights as the key
 Element: edge T∅
At the end of the while T has fewer than n-1 edges do
algorithm edge e = T.removeMin()
Let u, v be the endpoints of e
 We are left with one if Cloud(v) ≠ Cloud(u) then
cloud that Add edge e to T
encompasses the Merge Cloud(v) and Cloud(u)
MST return T
 A tree T which is our
MST

© 2004 Goodrich, Tamassia Graphs 94


Data Structure for Kruskal
Algortihm (§ 10.6.2)
The algorithm maintains a forest of trees
An edge is accepted it if connects distinct trees
We need a data structure that maintains a
partition, i.e., a collection of disjoint sets, with
the operations:
-find(u): return the set storing u
-union(u,v): replace the sets storing u and v
with their union

© 2004 Goodrich, Tamassia Graphs 95


Representation
of a Partition
Each set is stored in a sequence
Each element has a reference back to the
set
 operation find(u) takes O(1) time, and returns the
set of which u is a member.
 in operation union(u,v), we move the elements of
the smaller set to the sequence of the larger set
and update their references
 the time for operation union(u,v) is min(nu,nv),
where nu and nv are the sizes of the sets storing u
and v
Whenever an element is processed, it goes
into a set of size at least double, hence each
element
© 2004 Goodrich, is processedGraphs
Tamassia at most log n times 96
Partition-Based
Implementation
A partition-based version of Kruskal’s Algorithm
performs cloud merges as unions and tests as
finds. Kruskal(G):
Algorithm
Input: A weighted graph G.
Output: An MST T for G.
Let P be a partition of the vertices of G, where each vertex forms a separate set.
Let Q be a priority queue storing the edges of G, sorted by their weights
Let T be an initially-empty tree
while Q is not empty do
(u,v) ← Q.removeMinElement()
if P.find(u) != P.find(v) then Running time:
Add (u,v) to T
O((n+m)log n)
P.union(u,v)
return T
© 2004 Goodrich, Tamassia Graphs 97
Kruskal
Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 98


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 99


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 100


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 101


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 102


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 103


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 104


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 105


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 106


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 107


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 108


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 109


Example 2704
867 BOS

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 110


Example 2704
BOS
867

849 PVD
ORD 187
740 144
1846 621 JFK
184 1258
802
SFO BWI
1391
1464
337 1090
DFW 946
LAX 1235
1121
MIA
2342

© 2004 Goodrich, Tamassia Graphs 111


Prim-Jarnik’s Algorithm (§
12.7.2)
Similar to Dijkstra’s algorithm (for a connected
graph)
We pick an arbitrary vertex s and we grow the
MST as a cloud of vertices, starting from s
We store with each vertex v a label d(v) = the
smallest
At weight of an edge connecting v to a
each step:
vertex into
 We add the
thecloud
cloud the
vertex u outside the cloud
with the smallest distance
label
 We update the labels of

the vertices adjacent to u

© 2004 Goodrich, Tamassia Graphs 112


Prim-Jarnik’s Algorithm
(cont.)
A priority queue Algorithm PrimJarnikMST(G)
stores the vertices Q ← new heap-based priority queue
s ← a vertex of G
outside the cloud for all v ∈ G.vertices()
 Key: distance if v = s
setDistance(v, 0)
 Element: vertex else
Locator-based setDistance(v, ∞)
methods setParent(v, ∅)
l ← Q.insert(getDistance(v), v)
 insert(k,e) returns a setLocator(v,l)
locator while ¬Q.isEmpty()
 replaceKey(l,k) changes u ← Q.removeMin()
the key of an item for all e ∈ G.incidentEdges(u)
z ← G.opposite(u,e)
We store three labels r ← weight(e)
with each vertex: if r < getDistance(z)
 Distance setDistance(z,r)
setParent(z,e)
 Parent edge in MST Q.replaceKey(getLocator(z),r)
 Locator in priority
queue Graphs 113
© 2004 Goodrich, Tamassia
Example
∞ 7
7 D 7 D
2 2
B 4 B 4
8 9 ∞ 5 9 ∞
2 5 F 2 5 F
C C
8 8
8 3 8 3
E E
A 7 A 7
0 7 0 7

7 7
7 D D
2 2 7
B 4 B 4
5 9 ∞ 9 4
2 5 F 5 5
C 2 F
8 C
3 8
8 8 3
E E
A 7 A
0 7 7 7
0
© 2004 Goodrich, Tamassia Graphs 114
Example (contd.)
7
7 D
2
B 4
5 9 4
2 5 F
C
8
8 3
E
A 3 7
0 7
7 D
2
B 4
5 9 4
2 5 F
C
8
8 3
E
A 3
0 7

© 2004 Goodrich, Tamassia Graphs 115


Analysis
Graph operations
 Method incidentEdges is called once for each vertex
Label operations
 We set/get the distance, parent and locator labels of vertex z
O(deg(z)) times
 Setting/getting a label takes O(1) time
Priority queue operations
 Each vertex is inserted once into and removed once from the
priority queue, where each insertion or removal takes O(log n)
time
 The key of a vertex w in the priority queue is modified at most
deg(w) times, where each key change takes O(log n) time
Prim-Jarnik’s algorithm runs in O((n + m) log n) time provided
the graph is represented by the adjacency list structure

Recall that Σ v deg(v) = 2m
The running time is O(m log n) since the graph is connected
© 2004 Goodrich, Tamassia Graphs 116
Traveling Salesperson
Problem
A tour of a graph is a spanning
cycle (e.g., a cycle that goes
through all the vertices) 7 D
A traveling salesperson tour of a B 4
weighted graph is a tour that is 2
5 F
simple (i.e., no repeated vertices 2 C
or edges) and has has minimum 8
6 3
weight E
A 1
No polynomial-time algorithms
are known for computing
traveling salesperson tours Example of traveling
The traveling salesperson salesperson tour
problem (TSP) is a major open (with weight 17)
problem in computer science
 Find a polynomial-time algorithm
computing a traveling
salesperson tour or prove that
none
© 2004 Goodrich, exists
Tamassia Graphs 117
TSP Approximation
We can approximate a TSP
tour with a tour of at most
twice the weight for the case
of Euclidean graphs
 Vertices are points in the
plane
 Every pair of vertices is
connected by an edge
 The weight of an edge is the
length of the segment joining
the points
Approximation algorithm
 Compute a minimum spanning
tree
 Form an Eulerian circuit

around the MST


 Transform the circuit into a

© 2004 Goodrich,tour
Tamassia Graphs 118

You might also like