You are on page 1of 0

Data Structure

Chapter 6 Graphs
J uinn-Dar Huang, Ph.D.
Assistant Professor
jdhuang@mail.nctu.edu.tw
November 2004
Rev. 2005, 2006
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
1 copyright 2004-2006
Knigsberg Bridge Problem
A
Kneiphof
a
b
c
d
g
C
D
B
f
e
a
b
c
d
g
e
f
A
C
Q: Is it possible to cross each bridge exactly once and
return to the starting points?
A: Euler said no.
B
D
The first recorded problem that was solved using graph in 1736
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
2 copyright 2004-2006
Graphs
Definition
a graph G consists of 2 sets, V and E
V is a finite, nonempty set of vertices
E is a set of pairs of vertices; these pairs are called
edges
Representations
a graph G(V, E)
V(G) represents the set of vertices of G
E(G) represents the set of edges of G
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
3 copyright 2004-2006
Directed or Undirected Graphs
In an undirected graph
the edge is undirected
(u, v) and (v, u) represent the same edge
In a directed graph
the edge is directed
<u, v> and <v, u> represent different edges
for an edge <u, v>, u is the tail and v is the head of the
edge
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
4 copyright 2004-2006
Graph Examples
0
3
1 2
0
1
3 4
2
5 6
G
1
G
2
G
3
V(G
1
) = {0, 1, 2, 3}
E(G
1
) = {(0, 1), (0, 2), (0,
3), (1, 2), (1, 3), (2, 3)}
V(G
2
) = {0, 1, 2, 3, 4, 5, 6}
E(G
2
) = {(0, 1), (0, 2), (1, 3), (1, 4),
(2, 5), (2, 6)}
V(G
3
) = {0, 1, 2}
E(G
3
) = {<0, 1>, <1, 0>,
<1, 2>}
Directed Undirected Undirected
0
1
2
Tail
Head
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
5 copyright 2004-2006
Restrictions on Graphs
A graph cannot have an edge from a vertex v
back to itself
(v, v) and <v, v> are both illegal
these edges are named
self edges or self loops
A graph cannot have multiple occurrences of the
same edge
or it is a multigraph
These restrictions are not necessarily essential
0
2
1
1
2
3
0
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
6 copyright 2004-2006
Complete Graphs
An n-vertex undirected graph
has n*(n-1)/2 distinct edges at most
An n-vertex undirected graph with exactly
n(n-1)/2 edges is a complete graph
e.g., G
1
An n-vertex directed graph
has n*(n-1) distinct edges at most
An n-vertex directed graph with exactly n(n-1)
edges is a complete graph
e.g., G
3
is not complete
0
3
1 2
G
1
0
1
2
G
3
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
7 copyright 2004-2006
Adjacency and Incidence (1/2)
If (u, v) E(G)
u and v are adjacent
(u, v) is incident on u and v
e.g., vertices adjacent to vertex 1 are vertex 0, 3, and 4
e.g., edges incident on vertex 2 are (0,2), (2,5), and (2,6)
0
1
3 4
2
5 6
G
2
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
8 copyright 2004-2006
Adjacency and Incidence (2/2)
If <u, v> E(G)
u is adjacent to v (directed, i.e., v is NOT adjacent to u)
v is adjacent from u
<u, v> is incident to u and v
e.g., edges incident to vertex 1 are
<0,1>, <1,0>, and <1, 2>
0
1
2
G
3
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
9 copyright 2004-2006
Subgraphs
A subgraph of G is a graph G such that
V(G) V(G) and E(G) E(G)
0
0
3
1 2
0
1 2
(i)
(ii)
(iii)
0
3
1 2
G
1
0
1
2
G
3
0
0
1
2
0
1
2
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
10 copyright 2004-2006
Paths
A path from u to v in G
is a sequence of vertices u, i
1
, i
2
, , i
k
, v such that (u, i
1
)
(i
1
, i
2
), , (i
k
, v) are edges in E(G) (undirected graph)
is a sequence of vertices u, i
1
, i
2
, , i
k
, v such that <u, i
1
>
<i
1
, i
2
>, , <i
k
, v> are edges in E(G) (directed graph)
Length of a path
number of the edges on it
Simple path
is a path in which all vertices except possibly for the first
and last are distinct
A cycle is a simple path in which the first and the
last is the same vertex
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
11 copyright 2004-2006
Path Examples
A path 0, 1, 3, 2 in G
1
also a simple path; of length 3
A path 0, 1, 3, 2, 0 in G
1
also a cycle; of length 4
A path 0, 1, 3, 1 in G
1
not a simple path; of length 3
A path 0, 1, 2 in G
3
also a simple path; of length 2
A path 0, 1, 0 in G
3
also a cycle; of length 2
0, 1, 2, 1, is NOT a path in G
3
<2,1> E(G
3
)
0
1
2
G
3
0
3
1 2
G
1
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
12 copyright 2004-2006
Connected Graphs
In an undirected graph G, 2 vertices u and v are
said to be connected iff
there is a path from u to v (or a path from v to u)
An undirected graph G is said to be connected iff
u, v V(G), u v there is a path from u to v
0
1
3 4
2
5 6
G
2
Connected
0
3
1 2
4
7
5 6
G
4
H
1
H
2
Unconnected
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
13 copyright 2004-2006
Connected Components
A connected component, H, of an undirected
graph G is a maximal connected subgraphs
Maximal: G contains no other subgraph that is both
connected and fully contains H
0
3
1 2
4
7
5 6
G
4
H
1
H
2
H1 and H2 are 2 connected components
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
14 copyright 2004-2006
Strongly Connected Graphs
A directed graph G is said to be strongly
connected iff
u, v V(G), u v there is a path from u to v and a
path from v to u (bi-directional)
0
1
2
G
3
not strongly connected
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
15 copyright 2004-2006
Strongly Connected Components
A strongly connected component of a directed
graph is a maximal strongly connected subgraphs
0
1
2
G
3
2 strongly connected components
0
1
2
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
16 copyright 2004-2006
Degree
Degree of a vertex
the number of edges incident to the vertex
For a directed graph (digraph)
in-degree of v: the num of edges for which v is the head
out-degree of v: the num of edges for which v is the tail
0
1
3 4
2
5 6
G
2
0
1
2
G
3
degree: 3
in-degree: 1
out-degree:2
degree: 3
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
17 copyright 2004-2006
ADT Graph
class Graph {
// objects: a nonempty set of vertices and a set of edges
// where each edge is a pair of vertices
public:
Graph(); // ctor
void InsertVertex(Vertex v);
void InsertEdge(Vertex u, Vertex v);
void DeleteVertex(Vertex v);
void DeleteEdge(Vertex u, Vertex v);
bool IsEmpty();
List<Vertex> Adjancent(Vertex v);
};
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
18 copyright 2004-2006
Graph Representations
3 Common representations
Adjacency matrix
Adjacency list
Adjacency multilist
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
19 copyright 2004-2006
Adjacency Matrix (1/2)
Let G = (V, E) with n vertices, n1
The adjacency matrix A of G is an nxn martix in which
A[i][j] = 1 iff (u,v) (or <u,v>) E(G)

0 0 0
1 0 1
0 1 0
2
1
0
2 1 0
0
1
2
no self loops

0 0 1 0
0 0 1 1
1 1 0 1
0 1 1 0
3
2
1
0
3 2 1 0
0
3
1 2
no self loops
symmetric
non-symmetric
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
20 copyright 2004-2006
Adjacency Matrix (2/2)
Need n
2
bits if |V(G)| = n
The adjacency matrix is symmetric for an undirected graph
store only the upper or lower triangle of the matrix
Find degrees
degree of v (undirected graph): either row sum or column sum
in-degree of v (digraph): column sum
out-degree of v (digraph): row sum
How many edges in G? Is G connected?
time complexity is O(n
2
) at least
When the adjacency matrix is sparse
sparse matrix techniques in Chapter 2
time complexity can be reduced to o(n+e) where e << n
2
/2
HINT: How about storing edges only?
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
21 copyright 2004-2006
Adjacency List
0
1
2
G
3
0
3
1 2
G
1
2e nodes
3
2
1
0
1
3
3
1
2 0
0 0
0 0
2 0
[0]
[1]
[2]
[3]
HeadNodes
Number of edges can be determined in O(n+e) time
Easily to find the degree of a node
0
1 0
2 0 0
[0]
[1]
[2]
HeadNodes
e nodes
Easily to find the out-degree of a node
List Representation
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
22 copyright 2004-2006
Sequential Representation
9 11 13 15 17 18 20 22 23 2 1 3 0 0 3 1 2 5 6 4 5 7 6
2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22 0
1
0
3
1 2
4
7
5 6
G
4
H
1
H
2
n+1 entries 2e entries
Advantage small memory usage
Disadvantage dynamic edge insertion/deletion
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
23 copyright 2004-2006
Inverse Adjacency List
0
1 0
2 0 0
[0]
[1]
[2]
HeadNodes
e nodes
Easily to find the out-degree of a node
0
1
2
G
3
Q: How to find the in-degree of a node ?
1 0
0
0
1 0
[0]
[1]
[2]
HeadNodes
e nodes
Inverse adjacency list
How about combining the adjacency list and the inverse adjacency list?
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
24 copyright 2004-2006
Orthogonal Adjacency List
0
1
2
G
3
Tail Head Col link for head row link for tail
( Recall sparse matrix in Chapter 2 )
0
1
2 0
1 0 0
0
0 1 0 0
1 2
1 2 0 0
head nodes
(shown twice)
e nodes
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
25 copyright 2004-2006
Adjacency Multilist
[0]
[1]
[2]
[3]
0 1 N1 N3
0 2 N2 N3
0 3 0 N4
1 2 N4 N5
1 3 0 N5
2 3 0 0
N0
N1
N2
N3
N4
N5
HeadNodes
edge (0, 1)
edge (0, 2
edge (0, 3)
edge (1, 2)
edge (1, 3)
edge (2, 3)
The lists are
Vertex 0: N0 -> N1 -> N2
Vertex 1: N0 -> N3 -> N4
Vertex 2: N1 -> N3 -> N5
Vertex 3: N2 -> N4 -> N5
0
3
1 2
G
1
vertex1 vertex2 list1 list2
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
26 copyright 2004-2006
Weighted Edges
If there is a weight on an edge
represents distance or cost, or
matrix: A[i][j] might be used to keep the non-zero weight
list: an additional field for the weight is required
A graph with weighted edges is called a network
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
27 copyright 2004-2006
Traversals on Graphs
Graph is not like tree
there is no root
there may exist multiple paths from u to v
Given a graph and a vertex v
how to visit all vertices that are reachable from v?
i.e., visit all vertices connected to v
Traversals on graphs
depth-first search (DFS)
breadth-first search (BFS)
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
28 copyright 2004-2006
Depth-First Search (DFS)
void Graph::DFS() {
visited = new bool[n]; // n vertices from 0 to n-1
for(int i = 0; i < n; ++i)
visited[i] = false;
DFS(0); // start search from Vertex 0
delete [] visited;
}
void Graph::DFS(int v) {
visited[v] = true;
// pseudo code
for( each vertex w adjacent to v)
if(! visited[w])
DFS(w); // recursive call
}
3 4
1 2
5 6
0
7
Traversal Order:
0, 1, 3, 7, 4, 5, 2, 6
If G is in adjacency list O(e)
If G is in adjacency matrix O(n
2
)
Stack-Based
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
29 copyright 2004-2006
Breadth-First Search (BFS)
void Graph::BFS(int v) { // starting from Vertex v
visited = new bool[n]; // n vertices from 0 to n-1
for(int i = 0; i < n; ++i)
visited[i] = false;
Queue<int> q;
visited[v] = true;
q.Insert(v);
while(! q.IsEmpty()) {
v = *q.Delete(v);
for( each vertex w adjacent to v)
if(! visited[w]) {
visited[w] = true;
q.Insert(w);
}
}
delete [] visited;
}
3 4
1 2
5 6
0
7
Traversal Order:
0, 1, 2, 3, 4, 5, 6, 7
If G is in adjacency list O(e)
If G is in adjacency matrix O(n
2
)
Queue-Based
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
30 copyright 2004-2006
Spanning Trees (1/3)
A spanning tree of a graph G is a tree that
includes all vertices of G
includes edges of G
spanning
trees
A spanning tree consists of n vertices and n-1 edges
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
31 copyright 2004-2006
Spanning Trees (2/3)
If a graph G is connected
a DFS or BFS starting at any vertex visits all vertices
E(G) are partitioned into two sets, T and N
T (tree edges): edges traversed during the search
N(non-tree edges): remaining edges
V(G) + T spanning tree
A spanning tree resulting from DFS/BFS
depth-first/breadth-first spanning tree
If a non-tree edge is inserted into a spanning tree
a cycle is formed
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
32 copyright 2004-2006
Spanning Trees (3/3)
3 4
1 2
5 6
0
7
3 4
1 2
5 6
0
7
3 4
1 2
5 6
0
7
DFS (0) spanning tree BFS (0) spanning tree
A graph can have a bunch
of spanning trees
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
33 copyright 2004-2006
Articulation Point
A vertex v of a connected graph G is an articulation
point iff the deletion of v and all edges incident to v
will leave behind a graph that has at least 2
connected components
0
1
4
2 3
8
7
6
5
9
Vertices 1, 3, 5, and 7 are articulation points
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
34 copyright 2004-2006
Biconnected Graph
A biconnected graph is a connected graph that
has no articulation points
0
1
4
2 3
8
7
6
5
9
Not biconnected
Biconnected
3 4
1 2
5 6
0
7
Biconnected graph is desirable while representing a communication network
Why?
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
35 copyright 2004-2006
Biconnected Components
A biconnected component of a connected graph G
is a maximal biconnected subgraph H of G
0
1
4
2 3
8
7
6
5
9
1
4
2 3
7
6
5
0
1
3 5
8
7
7
9
A connected graph
Its 6 biconnected components
A biconnected graph has just one biconnected component the whole graph
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
36 copyright 2004-2006
Depth-First Number
Depth-first number: the visit order during the
depth-first search
0
1
4
2 3
8
7
6
5
9
1
2
3
4
5
6
7
8
10 9
3
4
2
1
0
6
7
8
5
9
1
2
3
4
5
6
7
8
9
10
non-tree
edge
starting
vertex
depth-first number, dfn
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
37 copyright 2004-2006
Back Edge and Cross Edge
No cross edges in a depth-first spanning tree
No back edges in a breadth-first spanning tree
3
4
2
1
0
6
7
8
5
9
4
6
10
1
2
3
5
7
8
9
back edge
3 4
1 2
5 6
0
7
cross edge
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
38 copyright 2004-2006
Find Articulation Points by DFS (1/2)
Articulation point
the root of the depth-first spanning tree has at least 2 children
any other vertex u that has at least one child w such that its
impossible to reach ancestor of u using a path composed solely of
w, descendants of w and a back edge
3
4
2
1
0
6
7
8
5
9
1
4
6
10
2
3
5
7
8
9
back edge
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
39 copyright 2004-2006
Find Articulation Points by DFS (2/2)
Articulation point
the root of the depth-first spanning tree has at least 2 children
a vertex u had a child w such that low(w) dfn(u)
3
4
2
1
0
6
7
8
5
9
1
4
6
10
2
3
5
7
8
9
back edge
low(w) = min {
dfn(w),
min{ low(x) | x is a child of w },
min{ dfn(x) | (w, x) is a back edge } }
10 9 6 6 6 1 1 1 1 5
low
10
8 7 6 2 1 3 4 5
dfn
9 8 7 6 5 4 3 2 1 0
vertex
9
6 6 6 1 1 1 1 5
low
8 7 6 2 1 3 4 5
dfn
9 8 7 6 5 4 3 2 1 0
vertex
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
40 copyright 2004-2006
Find dfn & low Values
void Graph::DfnLow(int x) { // apply DFS at Vertex x
num = 0; // num is an int data member of class Graph
dfn = new int[n]; // dfn is also a data member
low = new int[n]; // low is also a data member
for(int i = 0; i < n; ++i) dfn[i] = low[i] = 0;
DfnLow(x, -1);
delete[] dfn; delete[] low;
}
void Graph::DfnLow(int u, int v) {
dfn[u] = low[u] = ++num;
for( each vertex w adjacent from u )
if(dfn[w] == 0) { w is an unvisited vertex
DfnLow(w, u); // recursive
low[u] = min(low[u], low[w]);
} else if (w != v) // (w, v) is a back edge
low[u] = min(low[u], dfn[w]);
}
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
41 copyright 2004-2006
Minimum-Cost Spanning Trees
A weighted, undirected graph may have lots of
valid spanning trees
the cost of a spanning tree is the sum of the costs
(weights) of the edges in the spanning tree
A minimum-cost spanning tree is a spanning tree
of least cost
e.g., road construction
3 algorithms here
Kruskals, Prims, Sollins
all greedy methods
0
5
1
6
4
3
2
10
28
14
16
12
18
22
25
24
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
42 copyright 2004-2006
Kruskals Algorithm (1/3)
Build a minimum-cost spanning tree T
Sort edges in non-decreasing order of their costs
Select an edge in that order for inclusion in T if
the resultant T does not contain a cycle
only n -1 edges are included at last
0
5
1
6
4
3
2
10
28
14
16
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
43 copyright 2004-2006
Kruskals Algorithm (2/3)
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
44 copyright 2004-2006
Kruskals Algorithm (3/3)
// E is the set of all edges in G
T = ;
while((T contains less than n 1 edges) && (E is not empty))
{
choose an edge (v, w) from E of lowest cost;
delete (v, w) from E;
if((v, w) does not create a cycle in T)
add (v, w) to T;
else
discard (v, w);
}
if( T contains few than n 1 edges )
cerr << no spanning tree; // graph is not connected
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
45 copyright 2004-2006
Prims Algorithm (1/3)
Begin with a tree T containing one vertex
any one in the original graph
add a minimum-cost edge (u, v) to T such that
T {(u, v)} is still a tree
at first, (u, v) should be an edge such that
u T and v T
0
5
1
6
4
3
2
10
28
14
16
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
46 copyright 2004-2006
Prims Algorithm (2/3)
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
47 copyright 2004-2006
Prims Algorithm (3/3)
// assume that G has at least one vertex
// TV: vertex set of the spanning tree
// T: edge set of the spanning tree
TV = {0}; // start with Vertex 0 and no edges
for( T = ; T contains fewer than n-1 edges; ) {
let (u, v) be a least-cost edge s.t. u TV and V TV;
if( there is no such edge) break; // G is not connected
add v to TV;
add (u, v) to T;
}
if( T contains few than n 1 edges )
cerr << no spanning tree; // graph is not connected
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
48 copyright 2004-2006
Sollins Algorithm
At each stage, each tree selects a minimum-cost
edge to connect another tree
Repeat the process until the minimum-cost
spanning tree is obtained
16
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
(0, 5), (1, 6), (2, 3), (3, 2)
(4, 3), (5, 0), (6, 1) are selected
at the 1st stage
0
5
1
6
4
3
2
10
28
14
12
18
22
25
24
16
(5, 4), (1, 2), (2, 1)
are selected at the 2nd stage
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
49 copyright 2004-2006
Shortest Path
Give a digraph G and two vertices A and B in G
is there a path from A to B?
if there is more than one path from A to B, which is the shortest
one?
The length/cost/weight of a path is defined as the sum of
the lengths/costs/weights of the edges on that path
For a path
starting vertex: source
last vertex: destination
0
3 4
1 2
5
10 20
50
15
20
10
35
30
3 15
45
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
50 copyright 2004-2006
Single Source/All Destinations (1/4)
Non-negative edge weights
Path
Length
1) 0, 3
2) 0, 3, 4
3) 0, 3, 4, 1
4) 0, 2
10
25
45
45
0
3 4
1 2
5
10 20
50
15
20
10
35
30
3 15
45
Starting Vertex: 0
Increasing
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
51 copyright 2004-2006
Single Source/All Destinations (2/4)
Let S denotes the set of vertices to which the
shortest paths have already been found
(1) if the next shortest path is to vertex u, then the path
begins at v, ends at u, and goes through only vertices
that are in S
(2) the destination of the next path generated must be
the vertex u that has the minimum distance among all
vertices not in S
(3) The vertex u selected in (2) becomes a member of S
The algorithm is first given by Edsger Dijkstra
Therefore, its sometimes called Dijkstra Algorithm
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
52 copyright 2004-2006
Single Source/All Destinations (3/4)
Data Structure
// nmax: maximum number of vertices
class Graph {
int length[nmax][nmax]; // length-adjacency matrix
int dist[nmax]; // min-distance
bool s[nmax];
int choose(int); // used in ShortestPath
public:
void ShortestPath(int, int);
};
// length[i][j] is set to a large number
// if <i, j> is not an edge
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
53 copyright 2004-2006
Single Source/All Destinations (4/4)
void Graph::ShortestPath(int n, int v) {
// n vertices, source vertex = v
for(int i = 0; i < n; ++i) { // initialize
s[i] = false; dist[i] = length[v][i];
}
s[v] = true; dist[v] = 0;
for(int j = 0; j < n-2; ++j) { // determine n-1 paths
int u = choose(n);
// choose u s.t. dist[u] is minimum where s[u] = false
s[u] = true;
for(int w = 0; w < n; ++w) {
// update dist[w] where s[w] is false
if( (! s[w]) && (dist[u] + length[u][w] < dist[w]) )
dist[w] = dist[u] + length[u][w]; // shorter path found
}
}
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
54 copyright 2004-2006
Example (1/2)
0
1
2
3
4
5
6
7
300
800
1700
1000
1000
1400
1200
1500
1000
250
900
San
Francisco
Denver
New
Orleans
New York
Chicago
Los
Angeles
Miami
Boston

0 0 1700
1000 0 0
1400 900 0 1000 0
250 0 1500 0
0 1200
0 800 1000
0 300
0
7
6
5
4
3
2
1
0
7 6 5 4 3 2 1 0
Length-adjacency
matrix
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
55 copyright 2004-2006
Example (2/2)
Distance
LA SF DEN CHI BOST NY MIA NO
[0] [1] [2] [3] [4] [5] [6] [7]
Initial -- --- + + + 1500 0 250 + +
1 {4} 5 + + + 1250 0 250 1150 1650
2 {4,5} 6 + + + 1250 0 250 1150 1650
3 {4,5,6} 3 + + 2450 1250 0 250 1150 1650
4 {4,5,6,3} 7 3350 + 2450 1250 0 250 1150 1650
5 {4,5,6,3,7} 2 3350 3250 2450 1250 0 250 1150 1650
6 {4,5,6,3,7,2} 1 3350 3250 2450 1250 0 250 1150 1650
{4,5,6,3,7,2
,1}
iteration S Vertex
selected
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
56 copyright 2004-2006
Single Source/All Destinations (1/4)
General Weights (Negative weights are allowed)
0 1 2
5
7 -5
Directed graph with a negative-length edge
0 1 2
-2
1 1
Directed graph with a cycle of negative length
no cycle of negative length is allowed
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
57 copyright 2004-2006
Single Source/All Destinations (2/4)
When there is no cycle of negative length, a
shortest path between any 2 vertices of an n-
vertex graph has at most n-1 edges
Let dist
k
[u] be the length of a shortest path from
the source vertex v to vertex u
under the constraint that the path contains at most k
edges
dist
k
[u] = min { dist
k-1
[u],
min{ dist
k-1
[i] + length[i][u]} }
i
Bellman and Ford Algorithm
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
58 copyright 2004-2006
Single Source/All Destinations (3/4)
dist
k
[7] k
0 1 2 3 4 5 6
1 0 6 5 5
2 0 3 3 5 5 4
3 0 1 3 5 2 4 7
4 0 1 3 5 0 4 5
5 0 1 3 5 0 4 3
6 0 1 3 5 0 4 3
dist
k
0
1
2
3
4
5
6
6
5
5
-2
-2
-1
-1
1
3
3
from length-adjacency matrix
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
59 copyright 2004-2006
Single Source/All Destinations (4/4)
void Graph::BellmanFord(int n, int v) {
// n vertices, source vertex = v
for(int i = 0; i < n; ++i)
dist[i] = length[v][i]; // initialize, dist
1
[u]
for(int k = 2; k <= n-1; ++k) // compute dist
k
for(each u s.t. u != v and u has at least one incoming edge)
for( each incoming edge <i, u> )
if(dist[u] > (dist[i] + length[i][u]) )
dist[u] = dist[i] + length[i][u];
}
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
60 copyright 2004-2006
All-Pairs Shortest Paths (1/3)
The problem can be solved as n independent
single-source/all-destinations problems
each iteration, a different vertex as the source vertex
Better idea?
Define A
k
[i][j] to be the length of the shortest path
from i to j going through no vertex of index > k
A
n-1
[i][j] will be the length of the shortest i-to-j path in an
n-vertex graph
A
k
[i][j] = min{ A
k-1
[i][j], A
k-1
[i][k] + A
k-1
[k][j] }, k 0,
where A
-1
[i][j] = length[i][j]
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
61 copyright 2004-2006
All-Pairs Shortest Paths (2/3)
void Graph::AllLengths(int n) {
// length[n][n] is the length-adjacency matrix
// a[i][j] is the length of the shortest path between i and j
for( int i = 0; i < n; ++i)
for ( int j = 0; j < n; ++i)
a[i][j] = length[i][j]; // construct A
-1
for(int k = 0; k < n; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if( a[i][j] > (a[i][k] + a[k][j]) )
a[i][j] = a[i][k] + a[k][j];
}
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
62 copyright 2004-2006
All-Pairs Shortest Paths (3/3)
A
-1
0 1 2
0 0 4 11
1 6 0 2
2 3 0
A
0
0 1 2
0 0 4 11
1 6 0 2
2 3 7 0
A
1
0 1 2
0 0 4 6
1 6 0 2
2 3 7 0
2
0 1
6
2
4
11
3
A
2
0 1 2
0 0 4 6
1 5 0 2
2 3 7 0
A
-1
A
0
A
1
A
2
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
63 copyright 2004-2006
Transitive Closure (1/3)
Given a digraph G with unweighted edges,
determine if there is a path from vertex i to vertex j
positive length: transitive closure
non-negative length: reflexive transitive closure
Transitive closure matrix A
+
A
+
[i][j] = 1 iff there is a path of length > 0 from i to j
Transitive closure matrix A
*
A
*
[i][j] = 1 iff there is a path of length 0 from i to j
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
64 copyright 2004-2006
Transitive Closure (2/3)
To find A
+
similar to the all-pairs shortest paths problem
first, let length matrix be the adjacency matrix, then
void Graph::TransitiveClosure(int n) {
// length[n][n] is the adjacency matrix
for( int i = 0; i < n; ++i)
for ( int j = 0; j < n; ++i)
a[i][j] = length[i][j];
for(int k = 0; k < n; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
a[i][j] = a[i][j] || (a[i][k] && a[k][j]);
}
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
65 copyright 2004-2006
Transitive Closure (3/3)

1 1 1 0 0
1 1 1 0 0
1 1 1 0 0
1 1 1 0 0
1 1 1 1 0
4
3
2
1
0
4 3 2 1 0

0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
4
3
2
1
0
4 3 2 1 0
0 1 2 3 4
Digraph G

1 1 1 0 0
1 1 1 0 0
1 1 1 0 0
1 1 1 1 0
1 1 1 1 1
4
3
2
1
0
4 3 2 1 0
Adjacency matrix
A
+
A
*
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
66 copyright 2004-2006
Activity-on-Vertex (AOV) Networks (1/3)
Course number Course name Prerequisites
C1 Programming I None
C2 Discrete Mathematics None
C3 Data Structures C1, C2
C4 Calculus I None
C5 Calculus II C4
C6 Linear Algebra C5
C7 Analysis of Algorithms C3, C6
C8 Assembly Language C3
C9 Operating Systems C7, C8
C10 Programming Languages C7
C11 Compiler Design C10
C12 Artificial Intelligence C7
C13 Computational Theory C7
C14 Parallel Algorithms C13
C15 Numerical Analysis C5
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
67 copyright 2004-2006
Activity-on-Vertex (AOV) Networks (2/3)
C1
C2
C3
C5 C4 C6 C15
C7
C8
C13
C12
C10
C9
C14
C11
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
68 copyright 2004-2006
Activity-on-Vertex (AOV) Networks (3/3)
A digraph G in which the vertices represent activities and
the edges represent precedence relations between
activities is an activity-on-vertex (AOV) network
In an AOV network
vertex i is a predecessor of vertex j iff there is a path from i to j
i is an immediate predecessor of j iff <i, j> is an edge
j is a successor of i if i is a predecessor of j
j is an immediate successor of i if i is an immediate predecessor
of j
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
69 copyright 2004-2006
Partial Order
A relation is transitive if a b and b c a c
e.g., for integer a, b, c; a > b and b > c a > c
A relation is irreflexive on a set S
if a a for no a S
e.g., you cannot find any integer a that a > a
A relation is a partial order if it is transitive and
irreflexive
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
70 copyright 2004-2006
Precedence Relation
Is precedence relation a partial order?
Precedence relation is transitive
a is a predecessor of b and b is a predecessor of c a
is a predecessor of c
Is precedence relation irreflexive?
it depends on whether the given network contains any
directed cycles
A precedence relation on an network is irreflexive if
the network is a directed acyclic graph (DAG)
i.e., if it is a DAG, the precedence relation is a partial
order on it
topological order
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
71 copyright 2004-2006
Topological Order
A topological order is a linear ordering of the vertices of a
DAG such that, for any 2 vertices i and j, if i is a
predecessor of j, then i precedes j in the linear ordering
There may be several feasible topological orders
e.g.,
C1, C2, C4, C5, C3, C6 ,
C4, C5, C2, C1, C6, C3,
C1
C2
C3
C5 C4 C6 C15
C7
C8
C13
C12
C10
C9
C14
C11
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
72 copyright 2004-2006
Topological Sorting (1/3)
// pseudo code here
Input the AOV network. Let n be the number of vertices.
for(int i = 0; i < n; ++i) {
if(every vertex has a predecessor) return;
// network has a cycle and thus is infeasible
pick a vertex v that has no predecessors;
// if there are multiple vertices, pick one arbitrarily
cout << v;
delete v and all outgoing edges of v from the network;
}
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
73 copyright 2004-2006
Topological Sorting (2/3)
0
1
2
3
4
5
1
2
3
4
5
1
2 4
5
1
4
5
1
4
4
Initial
Vertex 0 deleted Vertex 3 deleted
Vertex 2 deleted Vertex 5 deleted
Vertex 1 deleted
one of feasible topological orders: 0, 3, 2, 5, 1, 4
Example
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
74 copyright 2004-2006
Topological Sorting (3/3)
Internal data structure
1
4 0
4
5
2
5 0
4 0
[0]
[1]
[2]
[3]
[4]
0
1
1
1
3 0
2 0 [5]
3 0
count first
data link
Indicate how many immediate predecessors
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
75 copyright 2004-2006
Activity-on-Edge (AOE) Networks
event interpretation
0 Start of project
1 Completion of activity a
1
4 Completion of activities a
4
and a
5
7 Completion of activities a
8
and a
9
8 Completion of project
1
0 4
2
6
8
7
3
5
start
finish
a
1
= 6 a
4
= 1
a
2
= 4
a
5
= 1
a
7
= 9
a
10
= 2
a
3
= 5
a
6
= 2
a
9
= 4
a
11
= 4
No activity can be launched
until its predecessors are
all completed
activity
event
Q1:
How fast can the project be done?
Q2:
What are the critical paths?
Q3:
How to speedup the project?
a
8
= 7
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
76 copyright 2004-2006
Critical Path (1/2)
Critical path
a path of longest length
e.g., 0, 1, 4, 6, 8 or 0, 1, 4, 7, 8 of length 18
The earliest time that an event i can start is the length of
the longest path from the start vertex 0 to vertex i
The earliest time of an event determines the earliest start
time for all activities represented by edges outgoing from
that vertex
denoted by e(i) for activity a
i
The latest time of an activity a
i
, l(i)
the latest time a
i
can start without increasing the final project
duration
An activity a
i
is a critical activity if e(i) = l(i)
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
77 copyright 2004-2006
Critical Path (2/2)
Meaning of l(i) e(i)
criticality of an activity a
i
Speedup non-critical activities will not reduce the
project duration
Speedup a critical activity may not reduce the
project duration
unless that activity is on all critical paths
Critical path analysis helps
evaluate project performance
identify project bottlenecks
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
78 copyright 2004-2006
Earliest Time Calculation (1/2)
For an event j
ee[j] : earliest event time
le[j] : latest event time
For an activity a
i
represented by <k, l>
e[i] = ee[k]
l[i] = le[l] duration of activity a
i
How to calculate ee[j]?
let P(j) be the set of all js immediate predecessors
in what order? topological order
ee[j] = max { ee[i] + duration of <i, j> }
iP(j)
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
79 copyright 2004-2006
Earliest Time Calculation (2/2)
adjacency list with in-degree count
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
80 copyright 2004-2006
Latest Time Calculation (1/2)
How to calculate le[j]?
let S(j) be the set of all js immediate successors
in what order? reverse topological order
le[j] = min { le[i] - duration of <j, i> }
iS(j)
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
81 copyright 2004-2006
Latest Time Calculation (2/2)
inverse adjacency list with out-degree count
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
82 copyright 2004-2006
Critical Activities
e = l critical activity
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
83 copyright 2004-2006
Critical Network
All paths from 0 to 8 are critical
Speed up a
1
and a
4
can reduce the project time
G
r
a
p
h
s
J
u
i
n
n
-
D
a
r

H
u
a
n
g





j
d
h
u
a
n
g
@
m
a
i
l
.
n
c
t
u
.
e
d
u
.
t
w
84 copyright 2004-2006
Final Review
Terminology
Representations
adjacency matrix/list/multilist
Graph traversals
DFS and BFS
Connected and biconnected components
Minimum-cost spanning tree
Shortest path
non-negative/general weights
single-source-all destinations and all pairs
transitive closure
Topological order/sorting
AOV and AOE networks

You might also like