You are on page 1of 174

CHAPTER 6

GRAPHS
All the programs in this file are selected from
Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
“Fundamentals of Data Structures in C”,

2021/12/6 Graphs 1
Outline
 The Graph Abstract Data Type
 Elementary Graph Operations
– DFS, BFS, Connected Components, Spanning Trees,
Biconnected Components
 Minimum Cost Spanning Trees
– Kruskal’s Algorithm, Prim’s Algorithm, Sollin’s Algorithm
 Shortest Paths and Transitive Closure
– Single Source/All Destination (nonnegative edge costs,
general weights), All Pairs Shortest Paths, Transitive Closure
 Activity Networks
– AOV Networks, AOE Networks
2021/12/6 Graphs 2
The KÖnigsberg bridge problem (1736): Determine whether,
starting from at one land area, it is possible to walk across all the
bridges exactly once in returning to the starting land area.
c
C d g

Kneiphof
內霍夫島 D
e
A

f
a
B b

(a) C
g Euler’s graph
Section of the river Pregel in KÖnigsberg c d
(b) A D
e
b
a
2021/12/6 Graphs B f 3
Euler answered the KÖnigsberg bridge problem in the
negative.

Euler showed that there is a walk starting at any vertex,


going through each edge exactly once and terminating
at the start vertex iff the degree of each vertex is even.
C vertex degree
g
c d A 5
A D B 3
e
b C 3
a
f D 3
B
There is no Eulerian walk for the KÖnigsberg bridge
problem, as all four vertices of odd degree.
2021/12/6 Graphs 4
Definitions
 A graph G consists of two sets
– a finite, nonempty set of vertices V(G)
– a finite, possible empty set of edges E(G)
– G(V,E) represents a graph
 An undirected graph is one in which the pair of
vertices in a edge is unordered, (v0, v1) = (v1,v0)
 A directed graph is one in which each edge is a
directed pair of vertices, <v0, v1> != <v1,v0>
tail head
2021/12/6 Graphs 5
Examples for Graph
0 0 0

1 2 1 2
1
3
3 4 5 6
G1 2
G2
complete graph incomplete graph G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
2021/12/6 Graphs 6
Complete Graphs
 A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the maximum
number of edges is n(n-1)/2
– for directed graph with n vertices, the maximum
number of edges is n(n-1)
– example: G1 is a complete graph
0
G1 complete graph
1 2
2021/12/6 3 Graphs 7
Adjacent and Incident
 If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent
– The edge (v0, v1) is incident on vertices v0 and v1
 If <v0, v1> is an edge in a directed graph
– v0 is adjacent to v1, and v1 is adjacent from v0
– The edge <v0, v1> is incident on v0 and v1

2021/12/6 Graphs 8
Figure 6.3:Examples of a graphlike structures

0
self edge

0 1 1 3

2 2
multigraph:
(a) (b) multiple occurrences
of the same edge

Figure 6.3
2021/12/6 Graphs 9
Subgraphs and Paths
 A subgraph of G is a graph G’ such that V(G’)
is a subset of V(G) and E(G’) is a subset of E(G)
 A path from vertex vp to vertex vq in a graph G,
is a sequence of vertices, vp, vi1, vi2, ..., vin, vq,
such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges
in an undirected graph
 The length of a path is the number of edges on
it
2021/12/6 Graphs 10
Figure 6.4: subgraphs of G1 and G3
0 0 0 1 2 0

1 2 1 2 3 1 2
G1 3
3
(i) (ii) (iii) (iv)
(a) Some of the subgraphs of G1

0 0 0 0
0
1 1 1
G3 1
2 2
(i) (ii) (iii) (iv)
2 (b) Some of the subgraphs of G3
2021/12/6 Graphs 11
Simple Paths and Cycles
 A simple path is a path in which all vertices
except possibly the first and last are distinct
 A cycle is a simple path in which the first and
last vertices are the same
 In an undirected graph G, two vertices, v0 and v1,
are connected if there is a path in G from v0 to v1
 An undirected graph is connected if, for every
pair of distinct vertices vi, vj, there is a path
from vi to vj
2021/12/6 Graphs 12
Examples
0 0

0 1 2
1
1 2
3 4 5 6
3 2
G2
G1 A tree is a connected acyclic G3
(i.e., has no cycles) graph.
0,1,3,2 is a simple path. 0,1,2,1 is not a path.
0,1,3,1 is not a simple path.
0,1,0 is a cycle.
0,1,2,0 is a cycle.

Graphs G1, G2, and G3 are connected.


2021/12/6 Graphs 13
Connected Component
 A connected component of an undirected graph
is a maximal connected subgraph.
 A tree is a graph that is connected acyclic (i. e.,
has no cycles) graph.
 A directed graph is strongly connected iff for every
pair of distinct vertices vi to vj , there is a directed
path from vi to vj and also
from vj to vi.
 A strongly connected component is a maximal
subgraph that is strongly connected.
2021/12/6 Graphs 14
Figure 6.5: A graph with two connected components
connected component (maximal connected subgraph)

H1 0 H2 4

2 1 5

3 6

7
G4 (not connected)
2021/12/6 Graphs 15
Figure 6.6: Strongly connected components of G3
not strongly connected

0 strongly connected component


(maximal strongly connected subgraph)

1
0 2
2
G3
1

2021/12/6 Graphs 16
Degrees
 The degree of a vertex is the number of edges
incident to that vertex
 For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
– if di is the degree of a vertex i in a graph G with n
vertices and e edges, the number of edges is
n −1

e=( ∑d )/ 2
0
i

2021/12/6 Graphs 17
undirected graph
Examples degree
3 0
0 2
1 2
3 1 2 3 3 3

3 3 4 5 6
3 1 1 1 1
G1 G2
0 in:1, out: 1

directed graph
in-degree 1 in: 1, out: 2
out-degree
G3
2 in: 1, out: 0
2021/12/6 Graphs 18
ADT for Graph
structure Graph is
objects: a nonempty set of vertices and a set of undirected edges, where each
edge is a pair of vertices
functions: for all graph ∈ Graph, v, v1 and v2 ∈ Vertices
Graph Create()::=return an empty graph
Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no
incident edge.
Graph InsertEdge(graph, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2)
is removed
Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE
else return FALSE
List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v
2021/12/6 Graphs 19
Graph Representations
 Adjacency Matrix
 Adjacency Lists

 Adjacency Multilists

2021/12/6 Graphs 20
Adjacency Matrix
 Let G=(V,E) be a graph with n vertices.
 The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
 If the edge (vi, vj) is in E(G), adj_mat[i][j]=1

 If there is no such edge in E(G), adj_mat[i][j]=0

 The adjacency matrix for an undirected graph is


symmetric; the adjacency matrix for a digraph
need not be symmetric
2021/12/6 Graphs 21
Examples of Adjacency Matrix
0 0 4
0
2 1 5
1 2 0 1 2
0 0 1 0 3 6
3 1  
1 1 0 1
G1   G4
2 0 0 0 7
0 1 2 3 2 0 1 2 3 4 5 6 7
0 0 1 1 1
0 0 1 1 0 0 0 0 0
1 1 0 1
 1  G2 1 1 0 0 1 0 0 0 0
 
2 1 1 0 1 2 1 0 0 1 0 0 0 0
   
3 1 1 1 0 3 0 1 1 0 0 0 0 0
symmetric
4 0 0 0 0 0 1 0 0
undirected: n2/2 ( by storing the upper or  
5 0 0 0 0 1 0 1 0
lower triangle of the matrix)
directed: n2
6 0 0 0 0 0 1 0 1
Graphs
 
2021/12/6 7 0 0 0 0 0 0 1 0
Merits of Adjacency Matrix
 From the adjacency matrix, to determine the
connection of vertices is easy n −1

 The degree of a vertex is ∑ adj _ mat[i][ j ]


j =0

 For a digraph, the row sum is the out_degree,


while the column sum is the in_degree
n −1 n −1
ind (vi ) = ∑ A[ j , i ] outd (vi ) = ∑ A[i , j ]
j =0 j =0

2021/12/6 Graphs 23
Data Structures for Adjacency Lists
Each row in adjacency matrix is represented as an adjacency list.

#define MAX_VERTICES 50
typedef struct node *nodePointer;
typedef struct node {
int vertex;
nodePointer link;
};
nodePointer graph[MAX_VERTICES];
int n=0; /* vertices currently in use */
2021/12/6 Graphs 24
0 Adjacency Lists 0 4
2 1 5
1 2 3 6
G1
3 G4 7
0 3 1 2
0 2 1
1 2 3 0 1 3 0
2 1 3 0 2 0 3
3 0 1 2 3 1 2
0 4 5
G3 5 6 4
0 1
6 5 7
1 2 0 1
7 6
2
2021/12/6 2 25

An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Interesting Operations
degree of a vertex in an undirected graph
–# of nodes in adjacency list
# of edges in a graph
–determined in O(n+e) time
out-degree of a vertex in a directed graph
–# of nodes in its adjacency list
in-degree of a vertex in a directed graph
–traverse the whole data structure (a little more
complex) Graphs 26
2021/12/6
Sequential Representation of Graph G4
0 4 int node[n+2*e+1];
node[0] … node[n-1]: starting point for vertices
2 1 5 node[n]: n+2e+1
3 6 node[n+1] … node[n+2e]: head node of edge
G4
7
n = 8, e = 7
[0] 9 [8] 23 (n+2e+1) [16] 2
[1] 11 0 [9] 2 4 [17] 5
[2] 13 [10] 1 5 [18] 6
[3] 15 1 [11] 3 [19] 4
[4] 17 [12] 0 6 [20] 5
[5] 18 2 [13] 0 [21] 7
[6] 20 [14] 3 7 [22] 6
[7] 22 3 [15] 1 [23]
2021/12/6 Graphs 27
Figure 6.10: Inverse adjacency list for G3

0
0  1 NULL

G3 1 1  0 NULL

2  1 NULL
2

Determine in-degree of a vertex in a fast way.

2021/12/6 Graphs 28
Alternate Node Structure for Adjacency

tail head column link for head row link for tail

This structure is based on a simplified version


of the list structure used for sparse matrix
representation.

2021/12/6 Graphs 29
Figure 6.11: Orthogonal list representation for graph G3
head nodes
(shown twice) 0 1 2

0 0 1 NULL NULL

1 1 0 NULL 1 2 NULL NULL


0 1 2
0
2 NULL
0 0 1 0

1 1 0 1

  1 G3
2 0 0 0
Adjacency matrix
2021/12/6 Graphs 2 30
Alternate order adjacency list for G1
Order is of no significance.
head nodes vertex link
0  3  1  2 NULL

1  2  0  3 NULL
0
2  3  0  1 NULL

3  2  1  0 NULL 1 2

0 3 1 2 3
1 2 3 0
G1
2 1 3 0
3 0 1 2
2021/12/6 Graphs 31
Adjacency Multilists
An edge in an undirected graph is
represented by two nodes in adjacency list
representation.
Adjacency Multilists
–lists in which nodes may be shared among
several lists.
(an edge is shared by two different paths)

marked vertex1 vertex2 link1 link2

2021/12/6 Graphs 32
Adjacency Multlists for G1
Lists: vertex 0: N0->N1->N2, vertex 1: N0->N3->N4
vertex 2: N1->N3->N5, vertex 3: N2->N4->N5
adjLists (1,0)
0 N0 0 1 N1 N3 edge (0,1)
1 (2,0)
2 N1 0 2 N2 N3 edge (0,2)
(3,0)
3 N2 0 3 N4 edge (0,3)
(2,1)

0 N3 1 2 N4 N5 edge (1,2)
(3,1)
G1 N4 1 3 N5 edge (1,3)
1 2 (3,2)
edge (2,3)
N5 2 3
3 six edges
2021/12/6 Graphs 33
Adjacency Multilists
typedef struct edge *edgePointer;
typedef struct edge {
short int marked;
int vertex1, vertex2;
edgePointer link1, link2;
};
edgePointer graph[MAX_VERTICES];

marked vertex1 vertex2 link1 link2

2021/12/6 Graphs 34
Weighted Edges

 The weight may represent the distance from


one vertex to another.
 The weight may represent the cost of going
from one vertex to an adjacent vertex.
 A Network
– A graph with weighted edges

2021/12/6 Graphs 35
Outline
 The Graph Abstract Data Type
 Elementary Graph Operations
– DFS, BFS, Connected Components, Spanning Trees,
Biconnected Components
 Minimum Cost Spanning Trees
– Kruskal’s Algorithm, Prim’s Algorithm, Sollin’s Algorithm
 Shortest Paths and Transitive Closure
– Single Source/All Destination (nonnegative edge costs,
general weights), All Pairs Shortest Paths, Transitive Closure
 Activity Networks
– AOV Networks, AOE Networks
2021/12/6 Graphs 36
Elementary Graph Operations
 Traversal
Given an undirected graph G=(V,E) and a
vertex v ∈ V, find all vertices in V that are
reachable from v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal
 Connected Components
 Spanning Trees
 Biconnected Components

2021/12/6 Graphs 37
Figure 6.16:Graph G and its adjacency lists
depth first search(v0) : v0, v1, v3, v7, v4, v5, v2, v6
0

1 2

3 4 5 6

(a)

adjLists
[0] 1 2 0

[1] 0 3 4 0

[2] 0 5 6 0

[3] 1 7 0

[4] 1 7 0

[5] 2 7 0

[6] 2 7 0

[7] 3 4 5 6

(b)
breadth first search(v0): v0, v1, v2, v3, v4, v5, v6, v7
2021/12/6 38
Depth First Search (DFS) Push 2[0] Push 4[1]
0 v0 v1 v0 v3 v1 v7
in [0] in [1] in [3]
1 2
Pop 5[7] Push 5[7]
3 4 5 6 Push 6[7]
v7 v1 v4 v3
7
v5 in [4] in [7]
in [7]
(a)
Push 7[5]
v2
adjLists
[0] 1 2 0 v0 v5 v6
in [5] in [2]
[1] 0 3 4 0

[2] 0 5 6 0

[3] 1 7 0 Pop 6[7] Pop 7[5]


[4] 1 7 0 v6 v7 v7 v2
[5] 2 7 0 in [7] in [5] in [6]
[6] 2 7 0

[7] 3 4 5 6
Pop 2[0] Pop 4[1]
(b)
Terminate v2 v4
in [1]
Depth First Search dfs(v0): dfs(v0) in [0]
v0, v1, v3, v7, v4, v5, v2, v6
void dfs(int v)
0 1 2 null
{
1 0 3 4 null node_pointer w;
visited[v] = TRUE;
2 0 5 6 null
for (w = graph[v]; w; w = w->link)
3 1 7 null if (!visited[w->vertex]) dfs(w->vertex);
}
4 1 7 null

5 2 7 null

6 2 7 null

7 3 4 5 6 null 0/
01

1/ 2/
1 2
dfs(0): 01
0
0137
01374
013745
0137452
01374526
0,1,3,7,4,5,2,6 2 7

2 3/
3
3
4/
4
5
5/
5
6
6/
6
8
stack 3
7
5
0
1
3
1
3
7
0
1 7/
7
0
1
3
0 4

0
1 Graphs 40
0
Depth First Search of a Graph
#define FALSE 0
#define TRUE 1
short int visited[MAX_VERTICES];
void dfs(int v)
{ /* depth first search of a graph
beginning at v */ Time Complexity
nodePointer w; Data structure graph[v]
visited[v]= TRUE; adjacency list: O(e)
printf(“%5d”, v); adjacency matrix: O(n 2)

for (w=graph[v]; w; w=w->link)


if (!visited[w->vertex])
dfs(w->vertex);
}2021/12/6 Graphs 41
Breadth First Search of a Graph
(using a queue)
The queue definition and the function prototypes used
by bfs are:
typedef struct queue *queuePointer;
typedef struct queue {
int vertex;
queuePointer link;
};
queuePointer front, rear;
void addq(int);
int deleteq();

2021/12/6 Graphs 42
Breadth First Search of a Graph
(Cont’d)
void bfs(int v)
{ adjacency list: O(e)
adjacency matrix: O(n2)
nodePointer w;
front = rear = NULL; /* initialize queue */
printf(“%5d”, v);
visited[v] = TRUE;
addq(v);

2021/12/6 Graphs 43
while (front) {
v= deleteq();
for (w=graph[v]; w; w=w->link)
if (!visited[w->vertex]) {
printf(“%5d”, w->vertex);
addq(w->vertex);
visited[w->vertex] = TRUE;
}
}
}
2021/12/6 Graphs 44
Breadth First Search
addQ delQ addQ addQ delQ
(BFS) 0 v0 v0 v1 v2 v1 v0
in [1]
in [0]
1
addQ
2
delQ addQ addQ
v0 v2
4 5 6
v5
3
v4 v3
7
in [2]
(a)
addQ delQ addQ delQ
[0]
adjLists
1 2 0 v6 v3 v1 v7 v4
in [3]
[1] 0 3 4 0

delQ
[2] 0 5 6 0

[3] 1 7 0
v7 v2 v5 v7 v1
[4] 1 7 0 in [5] in [4]
[5] 2 7 0
delQ v7 delQ
[6] 2 7 0
v6 v2 v7
[7] 3 4 5 6 in [6]
(b)
Terminate v4 v3
Breadth First Search bfs(v0): v6 v5
bfs(v0) in [7]
v0, v1, v2, v3, v4, v5, v6, v7
Breadth First Search
(BFS)

0 1 2 null

1 0 3 4 null

2 0 5 6 null

3 1 7 null
0/1
0
4 1 7 null

5 2 7 null
1/2
1 2/3
2
6 2 7 null

7 3 4 5 6 null
3/4
3 4/5
4 5/6
5 6/7
6

7/8
7
bfs(0):
bfs(0):0
bfs(0):012
bfs(0):01234
bfs(0):0123456
bfs(0):01234567

0
12
234
3456
4567
queue Graphs 46
Connected Components
 Two problems
– Problem 1
• Determining whether or not an undirected
graph is connected
• A solution
– By applying dfs(0) or bfs(0) to the graph
– Problem 2
• Listing all the connected components of an undirected
graph
• A solution
– By making repeatedly calls to dfs(v) or bfs(v) where v is an
unvisited vertex
2021/12/6 Graphs 47
Listing the Connected Components of a Graph
void connected(void)
{
for (i=0; i<n; i++) {
if (!visited[i]) {
dfs(i);
printf(“\n”);
} The time complexity of the function
} connected is dependent on the
data structure.
} adjacency list: O(n+e)
adjacency matrix: O(n2)
2021/12/6 Graphs 48
Spanning Trees
 When graph G is connected, a depth first or
breadth first search starting at any vertex will
visit all vertices in G
– The search implicitly partitions the edges in G into
two sets: T (for tree edges) and N (for nontree
edges)
where T: set of edges used during search
N: set of remaining edges
 A spanning tree is any tree that consists solely
of edges in G and that includes all the vertices
2021/12/6 Graphs 49
Examples of Spanning Tree

0 0 0 0

1 2 1 2 1 2 1 2
3 3 3 3

G1 Possible spanning trees

2021/12/6 Graphs 50
Spanning Trees
 Either dfs or bfs can be used to create a
spanning tree
– When dfs is used, the resulting spanning tree is
known as a depth first spanning tree
– When bfs is used, the resulting spanning tree is
known as a breadth first spanning tree
 While adding a nontree edge into any spanning
tree, this will create a cycle
2021/12/6 Graphs 51
DFS vs BFS Spanning Trees

0 0 0

1 2 1 2 1 2

3 4 5 6 3 4 5 6 3 4 5 6
nontree edge 7
7 7 cycle
DFS(0) spanning tree BFS(0) spanning tree

2021/12/6 Graphs 52
Depth First Spanning Trees Push 2[0] Push 4[1]
0 v0 v1 v0 v3 v1 v7
in [0] in [1] in [3]

1 2 Pop 5[7] Push 5[7]


Push 6[7]
v7 v1 v4 v3
v5 in [4] in [7]
3 4 5 6 in [7]
Push 7[5]
0
v2 v0 v5 v6
7 in [5] in [2]
DFS(0)
spanning
1 2
tree Pop 6[7] Pop 7[5]
3 5 6 v6 v7 v7 v2
4 in [7] in [5] in [6]

7 Pop 2[0] Pop 4[1]


Terminate v2 v4
in [1]
Depth First Search dfs(v0): dfs(v0) in [0]
v0, v1, v3, v7, v4, v5, v2, v6
Breadth First Spanning Trees (p.285)
addQ delQ addQ addQ delQ
0 v0 v0 v1 v2 v1 v0
in [1]
in [0]
1 2 addQ delQ addQ addQ
v5 v0 v2 v4 v3
in [2]
3 4 5 6
0 addQ delQ addQ delQ
7 v6 v3 v1 v7 v4
BFS(0) in [3]
spanning
1 2
tree delQ
v7 v2 v5 v7 v1
3 4 5 6 in [5] in [4]

delQ v7 delQ
v6 v2 v7
in [6]
7
Terminate v4 v3
Breadth First Search bfs(v0): v6 v5
bfs(v0) in [7]
v0, v1, v2, v3, v4, v5, v6, v7
A Property of Spanning Trees

A spanning tree is a minimal subgraph, G’, of G


such that V(G’)=V(G) and G’ is connected.

Any connected graph with n vertices must have


at least n-1 edges, and all connected graphs
with n-1 edges are trees. A spanning tree has n-1
edges.

2021/12/6 Graphs 55
Biconnected Graphs
 A biconnected graph is a connected graph that
has no articulation points.
– Articulation points
• An articulation point is a vertex v of G such that the
deletion of v, together with all edges incident on v,
produces a graph, G’, that has at least two connected
components.
0 8 9

Articulation points: 7
1
vertices 1, 3, 5, and 7
2 3 5
It is not a biconnected graph.
2021/12/6 Graphs 56
4 6
Biconnected Graphs
0

1 2

3 4 5 6

7
A biconnected graph is a connected graph that
has no articulation points.

2021/12/6 Graphs 57
0 8 9
Articulation points:
vertices 1, 3, 5, and 7
1 7

2 3 5 connected graph

4 6
two connected components one connected graph

0 8 9 0 8 9

7 1 7
1

2 3 5 2 3 5

2021/12/6
4 6
Graphs
4 658
Definition: Biconnected Components
A biconnected component: a maximal biconnected connected
subgraph H
(no other subgraph that is both biconnected and properly contains H)
0 8 9
0 8 9

1 7 7
7 Have 6 biconnected
1
components
1 7
2 3 5
2 3 3 5 5
4 6
4 6

2021/12/6 Graphs 59
Definition: Biconnected Components
A biconnected component: a maximal biconnected connected
subgraph H
(no other subgraph that is both biconnected and properly contains H)
Biconnected graph
0

1 2
Contain just one biconnected
3 4 5 6 component: the whole graph

2021/12/6 Graphs 60
Biconnected Components
Properties:
1. Two biconnected components of the same graph have
no more than one vertex in common.
2. No edge can be in two or more biconnected components
of a graph.
3. The biconnected components of a graph G partition the
edges of G. 8 9
0
0 8 9
7 7
Have 6 biconnected 1
7 components 7
1 1

2 3 5 2 3 3 5 5
Graphs 61
4 6 4 6
Find biconnected components of a connected undirected graph
by using any depth first spanning tree
dfs(3)
4 0 9 8 9 8
0 8 9
Function 3 1 7 7
7 call dfs(3) 0 5
1
2 2 3 5
2 3 5

dfn: depth 4 6
4 6 1 6
first number

2021/12/6 Graphs 62
Definitions:
A nontree edge (u,v) is a back edge with respect to a
spanning tree T iff either u is an ancestor of v or v is an
ancestor of u.

A nontree edge that is not a back edge is called a cross


edge.

No graph can have cross edges with respect to any of


its depth-first spanning trees.

2021/12/6 Graphs 63
Find biconnected components of a connected undirected graph
by using any depth first spanning tree
nontree
0 edge
dfs(3) 3 (back edge)
4 0 9 8 9 8

nontree 4 1 5 5
3 1 7 7 edge
0 5 (back edge) 2 2 6 6
2 2 3 5
Redrawing 1 3 7 7

8 9
dfn: depth 4 6
0 4 9 8
first number 1 6
Why is cross edge impossible?
(a) depth first spanning tree (b)
If u is an ancestor of v then dfn(u) < dfn(v).
2021/12/6 Graphs 64
Figure 6.21: dfn and low values for dfs spanning tree with root =3

Vertex 0 1 2 3 4 5 6 7 8 9

dfn 4 3 2 0 1 5 6 7 9 8

low 4 0 0 0 0 5 5 5 9 8
dfs(3)
3 0

nontree 4 1 5 5
edge nontree
edge
(back edge) 2 2 6 6
(back edge)
3 7 7
1
dfn: depth
0 4 8 9 8 9 first number
2021/12/6
Definition: A value low(u) for each vertex u of G is the
lowest depth first number that we can reach from u using
a path of descendants followed by at most one back edge:
low(u) = min{ dfn(u), min{low(w)| w is a child of u},
min{dfn(w)|(u,w) is a back edge} }
Vertex 0 1 2 3 4 5 6 7 8 9 3 0

dfn 4 3 2 0 1 5 6 7 9 8
4 1 5 5
low 4 0 0 0 0 5 5 5 9 8
2 2 6 6
Articulation points:
vertices 1, 3, 5, and 7 1 3 7 7

Graphs 0 4 8 669 9 8
2021/12/6
Articulation Points
Properties:
1. The root of a depth first spanning tree is an articulation
point iff it has at least two children.
2. Any other vertex u is an articulation point iff it has
one child w such that low(w) ≥ dfn(u).
3 0
Vertex 0 1 2 3 4 5 6 7 8 9

dfn 4 3 2 0 1 5 6 7 9 8 4 1 5 5

low 4 0 0 0 0 5 5 5 9 8 2 2 6 6
Articulation points:
vertices 1, 3, 5, and 7 1 3 7 7

2021/12/6 Graphs 0 4 8 679 9 8


Articulation Points vertex dfn low child low(child) low(child):dfn
0 4 4 null null null:4
1 3 0 0 4 4≥3 •
2 2 0 1 0 0<2
3 0 0 4,5 0,5 0,5 ≥ 0 •
Articulation points: 4 1 0 2 0 0<1
5 5 5 6 5 5≥5 •
vertices 1, 3, 5, and 7 6 6 5 7 5 5<6
7 7 5 8,9 9,8 9,8 ≥ 7 •
8 9 9 null null null, 9
0 9 8 8 null null null, 8
3
Vertex 0 1 2 3 4 5 6 7 8 9

4 1 5 5 dfn 4 3 2 0 1 5 6 7 9 8

6 6 low 4 0 0 0 0 5 5 5 9 8
2 2

1 3 7 7
2021/12/6 68
8 9 9 8
0 4
Computing dfn and low for each vertex
of a connected undirected graph
The global declarations:
#define MIN2(x,y) ((x) < (y) ? (x) : (y))
short int dfn[MAX_VERTICES];
short int low[MAX_VERTICES];
int num;

2021/12/6 Graphs 69
Computing dfn and low for each vertex
of a connected undirected graph
Program 6.5: Initialization of dfn and low
void init(void)
{
int i;
for (i = 0; i < n; i++) {
visited[i] = FALSE;
dfn[i] = low[i] = -1;
}
num = 0;
}
2021/12/6 Graphs 70
Program 6.4: Determining dfn and low
void dfnlow(int u, int v) Initial call: dfnlow(x,-1)
{ where x is the root of the dfs tree
/* compute dfn and low while performing a dfs search
beginning at vertex u, v is the parent of u (if any) */
nodePointer ptr;
int w;
dfn[u] = low[u] = num++; low[u]=min{dfn(u), …}
for (ptr = graph[u]; ptr; ptr = ptr ->link) {
v w = ptr ->vertex;
v if (dfn[w] < 0) { /*w is an unvisited vertex */
u dfnlow(w, u);
u low[u] = MIN2(low[u], low[w]);
} low[u]=min{…, min{low(w)|w is a child of u},…}
w
else if (w != v) dfn[w]≠0 非第一次,表示back edge
low[u] =MIN2(low[u], dfn[w] );
} low[u]=min{…,…,min{dfn(w)|(u,w) is a back edge}}
}
 low(u)=min{dfn(u),min{low(w)|w is a child of u},min{dfn(w)|(u,w) is a back edge}}

9/9
9/ 8/
8/8
dfn/low 4/4
4/ 0 8 9 low:8 Adjacency Lists
low:4 low:9
low 0 0 1
dfn:3 dfn:7 1 3 0 2
3/ 11
3/0 7
7/
7/5
2 1 4
dfn 2 dfn:0
3 4 1 5
2 33 5 dfn:5
2/0
2/ 0/0
0/ 5/5 4 3 2
5/
5 6 7 3
4 6
1/
1/0 6/
6/5 6 5 7
7 5 9 8 6
dfnlow(3,-1)
8 7
2021/12/6 Graphs 72
9 7
Partitioning the edges of a connected undirected
graph into their biconnected components

 Function call bicon(x,-1)


 Time complexity O(n+e)

2021/12/6 Graphs 73
Finding Biconnected Components of
a Connected Undirected Graph

8 9
3 0

7 7
4 1 5 5 1
7
6 6 1
2 2
2 3 3 5 5
1 3 7 7

8 9 9 8 4 6
0 4

2021/12/6 Graphs 74
Program 6.6: Biconnected components of a graph
void bicon(int u, int v)
{
/* compute dfn and low, and output the edges of G by their
biconnected components , v is the parent ( if any) of the u
(if any) in the resulting spanning tree. It is assumed that all
entries of dfn[ ] have been initialized to -1, num has been
initialized to 0, and the stack has been set to empty */
nodePointer ptr;
int w, x, y;
dfn[u] = low[u] = num ++; low[u]=min{dfn(u), …}
for (ptr = graph[u]; ptr; ptr = ptr->link) {
w = ptr ->vertex; (1) dfn[w]=-1 第一次
if ( v != w && dfn[w] < dfn[u] )(2) dfn[w]!=-1非第一次,藉back
push(u, w); /* add edge to stack */ edge

2021/12/6 Graphs 75
if (dfn[w] < 0) { /* w has not been visited */
bicon(w, u); low[u]=min{…, min{low(w)|w is a child of u}, …}
low[u] = MIN2(low[u], low[w]);
if (low[w] >= dfn[u]) { /* u is an articulation point */
printf(“New biconnected component: “);
do { /* delete edge from stack */
pop(&x, &y);
printf(“ <%d, %d>” , x, y);
} while (!(( x = = u) && (y = = w))) ;
printf(“\n”);
} Time Complexity: O(n+e)
}
else if (w != v) low[u] = MIN2(low[u], dfn[w]);
} low[u]=min{…, …, min{dfn(w)|(u,w) is a back edge}}
}
2021/12/6 Graphs 76
Finding Biconnected Components bicon(3,-1)
9/9
9/ 8/8
8/
4/4
4/ 0 8 9
Adjacency Lists
0 1 1 7
3/
3/0 1,0c
7,9
7,8
1 3 0 2 7/
7/5
7,5
2 1 4 1,0c
6,7
2 3 5
3 4 1 5 2/
2/0 1,0c
5,6
0/
0/0 5/
5/5
1,0c
1,0b
1,0
3,5
4 3 2
4 6 1,3
5 6 7 3 1/
1/0 6/
6/5
1,0 2,1
6 5 7 7,9 4,2
7 5 9 8 6 7,8 3,4
8 7 7,5; 6,7; 5,6 Stack
9 7 3,5
2021/12/6 Graphs 1,3; 2,1; 4,2; 3,4 77

Note: Edge(parent,child)
Outline
 The Graph Abstract Data Type
 Elementary Graph Operations
– DFS, BFS, Connected Components, Spanning Trees,
Biconnected Components
 Minimum Cost Spanning Trees
– Kruskal’s Algorithm, Prim’s Algorithm, Sollin’s Algorithm
 Shortest Paths and Transitive Closure
– Single Source/All Destination (nonnegative edge costs,
general weights), All Pairs Shortest Paths, Transitive Closure
 Activity Networks
– AOV Networks, AOE Networks
2021/12/6 Graphs 78
Minimum Cost Spanning Tree
 The cost of a spanning tree of a weighted
undirected graph is the sum of the costs of the
edges in the spanning tree
 A minimum cost spanning tree is a spanning
tree of least cost
 Three different algorithms can be used (greedy
strategy)
– Kruskal
– Prim Select n-1 edges from a weighted graph
of n vertices with minimum cost.
– Sollin
2021/12/6 Graphs 79
Greedy Strategy
 An optimal solution is constructed in stages
 At each stage, the best decision is made at this
time
 Since this decision cannot be changed later,
we make sure that the decision will result in a
feasible solution
 Typically, the selection of an item at each
stage is based on a least cost or a highest profit
criterion
2021/12/6 Graphs 80
Kruskal’s Idea
 Build a minimum cost spanning tree T by
adding edges to T one at a time
 Select the edges for inclusion in T in
nondecreasing order of the cost
 An edge is added to T if it does not form a
cycle
 Since G is connected and has n > 0 vertices,
exactly n-1 edges will be selected
2021/12/6 Graphs 81
Stage Stages
0 10 5
in Kruskal’s Algorithm
1

2 2 12 3 Initialization Stage 1

3 1 14 6 0 0 0
28
4 1 16 2 10 1 1 10 1
14 16
5 3 18 6 5 6 2 5 6 2 5 6 2
24
6 3 22 4 25 18 12 4 4
4
7 4 24 6 22 3 3 3

8 4 25 5
Graphs 82
28
9 0 1
Stage
1 0 10 5 Stages in Kruskal’s Algorithm
2 2 12 3
Stage 2 Stage 3 Stage 4
3 1 14 6
0 0 0
4 1 16 2 1 1
10 1 10 10
16
14 14
18
5 3 6
5 6 2 5 6 2 5 6 2
6 3 22 4
12 12 12
4 4 4
7 4 24 6 3 3
3
8 4 25 5 Stage 5
28 + 3 6
Graphs
9 0 1 cycle
Stage
1 0 10 5 Stages in Kruskal’s Algorithm
2 2 12 3
Stage 5
3 1 14 6
0
4 1 16 2 10 1 + 3 6
16
5 3 18
6
14 cycle
5 6 2
6 3 22 4 X 12
4
7 4 24 6 3
8 4 25 5
28 Graphs 84
9 0 1
Stage
1 0 10 5
Stages in Kruskal’s Algorithm
2 2 12 3
Stage 6 Stage 7 Stage 8

3 1 14 6 0
0 0
4 1
16
2 10 1 10 1 10 1
14 16 14 16 14 16
5 3 18 6 6 6 5 6 2
5 2 5 2
X 25
6 3 22 4 12
4
12
4
12 4
22
7 4 24 6 22
3 22
3 3
25 + 4 6 cost =
8 4 5
cycle 10 +25+22+12+16+14
28 Graphs 85
9 0 1
Kruskal’s Algorithm
T= {}; 目標:取出n-1條edges
while (T contains less than n-1 edges
&& E is not empty) {
choose a least cost edge (v,w) from E;
delete (v,w) from E; choose min heap construction time O(e loge)
and delete O(log e)
if ((v,w) does not create a cycle in T)
add (v,w) to T find find & union O(log e)
else discard (v,w);
} {0,5}, {1,2,3,6}, {4} + edge(3,6) X + edge(3,4) --> {0,5},{1,2,3,4,6}
if (T contains fewer than n-1 edges)
printf(“No spanning tree\n”);

Time complexity: O(e log e)


2021/12/6 Graphs 86
Kruskal’s Algorithm
 Implementation
– Data structure: min heap
• Used for the step
Choose a least cost edge (u,w)from E
• Each insertion/deletion operation takes time O(log e).
– Or sorting all edges by length
– Union/Find operations
• Each operation takes time O(log e).
 Greedy strategy
 Time complexity: O(e log e)

where e is the number of edges in G.


2021/12/6 Graphs 87
Prim’s Algorithm
(tree all the time vs. forest)
T={};
TV={0};
while (T contains fewer than n-1 edges)
{
let (u,v) be a least cost edge such
that u ∈ TV and v ∉ TV
if (there is no such edge ) break;
add v to TV;
add (u,v) to T;
}
if (T contains fewer than n-1 edges)
printf(“No spanning tree\n”);
2021/12/6 Graphs 88
Stages 0
28

in Prim’s Algorithm 10
14
1
16

5 6 2
24
25
18 12
4
Stage 1 Stage 2
22 3 Stage 3

0 28
0 28 0 28
10 1 10 1 10 1

5 6 2 5 6 2 5 6 2
24 24
25 25 25 18 12
4 4 4
3 22 3 22 3
Stages 0
28
in Prim’s Algorithm 10 1
14 16

5 6 2
24
25
18 12
4
Stage 4 Stage 5
22 3 Stage 6
0 28 0 0
10 1 10 1 10 1
16 14 16 14 16

5 6 2 5 6 2 5 6 2
24 24
25 18 12 25 18 12 25
12
4 4 4
22 3 22 3 22 3
Prim’s Algorithm
 Implementation
– Data structure: min heap
• Used for the step
Let (u,v) be a least cost edge such that u∈TV
and v ∉ TV
– Each insertion/deletion operation takes time O(log e).
 Greedy strategy
 Time complexity: O(n +e log e)

where n and e are the numbers of vertices and


edges in G, respectively.
2021/12/6 Graphs 91
Minimum-Cost Spanning Tree Methods

• Can prove that all stated edge selection/rejection result


in a minimum-cost spanning tree.
• Prim’s method is fastest.
 O(n2) using an implementation similar to that of Dijkstra’s
shortest-path algorithm.
 O(e + n log n) using a Fibonacci heap.
• Kruskal’s uses union-find trees to run in O(n + e log e)
time.

2021/12/6 Minimum-Cost Spanning Trees 92


Sollin’s Algorithm

1. Initialize a forest F such that each tree in F just contains one


vertex;
2. While (F is not a spanning forest) do
3. For each tree T in F, do
3.1 Select one minimum cost edge e that has
exactly one vertex in T ;
3.2 Add this edge e to the forest F ;
(Step 3 needs to eliminate multiple copies of edges)

2021/12/6 Graphs 93
0 Sollin’s Algorithm
28
10 1 vertex edge
0 0 -- 10 --> 5, 0 -- 28 --> 1
14 16
1 1 -- 14 --> 6, 1-- 16 --> 2, 1 -- 28 --> 0
5 6 2 2 2 -- 12 --> 3, 2 -- 16 --> 1
24 3 3 -- 12 --> 2, 3 -- 18 --> 6, 3 -- 22 --> 4
25 4 4 -- 22 --> 3, 4 -- 24 --> 6, 4 -- 25 --> 5
18 12
4 5 5 -- 10 --> 0, 5 -- 25 --> 4
6 6 -- 14 --> 1, 6 -- 18 --> 3, 6 -- 24 --> 4
22 3
Initialization Stage 1 Stage 2
0 0 {0,5} 0
28
1 10 1 5 25
4 0 1 10 1
14 14 16
{1,6}
5 6 2 5 6 2 5 6 2
16 28 25
1 2 1 0
12 12
4 4 18 24 4
6 3 6 4
3 22 3 22 3
How to Implement Sollin’s Algorithm

 The data structure used for minimum cost


edge selection (Step 3.1)
– Min heaps
– Linear lists
 Duplicate edge elimination (Step 3)
– Union/Find operations
 Time complexity

2021/12/6 Graphs 95
Outline
 The Graph Abstract Data Type
 Elementary Graph Operations
– DFS, BFS, Connected Components, Spanning Trees,
Biconnected Components
 Minimum Cost Spanning Trees
– Kruskal’s Algorithm, Prim’s Algorithm, Sollin’s Algorithm
 Shortest Paths and Transitive Closure
– Single Source/All Destinations (nonnegative edge costs,
general weights), All Pairs Shortest Paths, Transitive Closure
 Activity Networks
– AOV Networks, AOE Networks
2021/12/6 Graphs 96
Shortest Paths and Transitive Closure
 Single source/all destinations: nonnegative edge
costs
– Dijkstra’s algorithm
• A greedy approach
 Single source/all destinations: general weights
– Bellman and Ford algorithm
• A dynamic programming approach
 All-pairs shortest paths: general weights
– A dynamic programming approach

2021/12/6 Graphs 97
Single Source/All Destinations: Nonnegative Edge Costs
Determine the shortest paths from v0 to all the remaining vertices.

45
path length
50 10 1) 0, 3 10
0 1 2
15 2) 0, 3, 4 25
35
10 20 20 30 3) 0, 3, 4, 1 45
4) 0, 2 45
3 4 5
15 3 Shortest paths from 0

Figure 6.26: Graph and shortest paths from v0 to all destinations

2021/12/6 Graphs 98
Example for the Shortest Path Boston
1500 4
San Chicago
Denver 250
Source: Francisco
800
1200 3 1000
Vertex 4 1 2
New
(Boston) 5 York
300 1000 1400

1700 New Orleans 900


0 7 1000
Los Angeles
6 Miami
0 1 2 3 4 5 6 7
0 0
1 300 0
2 1000 800 0
3 1200 0
4 1500 0 250
5 1000 0 900 1400
6 0 1000
7 1700 0
Cost adjacency matrix
Dijkstra’s (a) (b) (c) 4
algorithm 1500 4 1500 4 250
3 250 3 5
250
Source:
5 1000
Vertex 4 5 900
(Boston) 6
選5 4到3由1500改成1250 4到6由∞改成1150

4 4
(d) (f) 4
(e) 3
250
250 250
1000
5 5
7 1400 5
7 1400 7 1400
900
1000 900
6
6
4到7由∞改成1650 選6
4-5-6-7比4-5-7長
2021/12/6 Graphs 100
Dijkstra’s
(g) (h)
algorithm 4 4
3 2
1200
3
250
250
1000
5 1000
7 1400 5
7 1400
900
900
6 6
選3 4到2由∞改成2450

(i) 4 4
2
1200
3 (j)
250
0 250
1000
5 1700 5
7 1400 7 1400
900
6
4到0由∞改成3350
2021/12/6 Graphs 101
選7
Iteration S Vertex LA SF DEN CHI BO NY MIA NO
Selected [0] [1] [2] [3] [4] [5] [6]
Initial -- ---- +∞ +∞ +∞ 1500 0 250 +∞ +∞
(b) (c) (d)
1 {4} (a) 5 +∞ +∞ +∞ 1250 0 250 1150 1650
2 {4,5} (e) 6 +∞ +∞ +∞ 1250 0 250 1150(f)1650
(h)
3 {4,5,6} (g) 3 +∞ +∞ 2450 1250 0 250 1150 1650
4 (j)
{4,5,6,3} (i) 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
7 {4,5,6,3,7,2,1}
Dijkstra’s algorithm Source: Vertex 4 (Boston)
1500 4 Boston
San Chicago
Denver 250
Francisco
800
1200 3 1000
1 2
New
5 York
300 1000 1400

1700 New Orleans 900


0 7 102
1000
Los Angeles 6 Miami
Dijkstra’s algorithm

1500/4
1250/5

2450/3

3250/2
∞ 250/4

1650/5

3350/7

1150/5

2021/12/6 Graphs 103


Dijkstra’s algorithm:
Data Structures for Single Source/All Destinations
#define MAX_VERTICES 6 adjacency matrix
int cost[][MAX_VERTICES]=
{{ 0, 50, 10, 1000, 45, 1000},
{1000, 0, 15, 1000, 10, 1000},
{ 20, 1000, 0, 15, 1000, 1000},
{1000, 20, 1000, 0, 35, 1000},
{1000, 1000, 30, 1000, 0, 1000},
{1000, 1000, 1000, 3, 1000, 0}};
int distance[MAX_VERTICES];
short int found{MAX_VERTICES];
int n = MAX_VERTICES;
2021/12/6 Graphs 104
Dijkstra’s algorithm: Single Source All Destinations
void shortestpath(int v, int
cost[][MAX_ERXTICES], int distance[], int n,
short int found[])
{
/* distance[i] respresents the shortest path
vertex v to I, found[i] =0 if the shortest
path does not exist, and found[i]=1 if the
shortest path exists. */
int i, u, w;
for (i=0; i<n; i++) {
found[i] = FALSE; Time: O(n)
distance[i] = cost[v][i];
}
found[v] = TRUE;
distance[v] = 0;
2021/12/6 Graphs 105
for (i=0; i<n-2; i++) {determine n-1 paths from v
u = choose(distance, n, found);
found[u] = TRUE;
for (w=0; w<n; w++)
if (!found[w]) 與u相連的端點w
Time: O(n2) if (distance[u]+cost[u][w]<distance[w])
distance[w] = distance[u]+cost[u][w];
}
}
Time complexity of algorithm shortestPath: O(n2)
2021/12/6 Graphs 106
Choosing the least cost edge
int choose(int distance[], int n, short int
found[])
{ /* find the smallest distance
not yet checked */
int i, min, minpos;
min = INT_MAX;
minpos = -1;
for (i = 0; i < n; i++) {
if(distance[i] < min && !found[i]){
min = distance[i];
minpos = i;
}
return minpos;
Time Complexity: O(n)
}
2021/12/6 Graphs 107
Analysis of Dijkstra’s Algorithm
 By using length-adjacency matrices, the Dijkstra’s
algorithm for the single-source/all-destinations
problem has complexity O(n2).
 By using Fibonacci heaps and adjacency lists, the
algorithm for the single-source/all-destinations
problem can be implemented to have complexity
O(nlogn+e).
– Obviously, this implementation is good for sparse
graphs.

2021/12/6 Graphs 108


Single source/all destinations:
general weights
 Can Dijkstra’s algorithm correctly solve the
single source/all destination shortest path
problem on the following graph?
5 Applying Dijkstra’s algorithm,
dist[1]=7 and dist[2]=5
Source The shortest path from vertex 0 to
0 1 2
vertex 7 -5 vertex 2 is 0, 1, 2. Its path length
is 2. That is not dist[2] (=5).
negative weight
2021/12/6 Graphs 109
Bellman and Ford algorithm
 Let distl[u] be the length of a shortest path
from the source vertex v to vertex u under
the constraint that the shortest path contains
at most l edges.
– dist1[u]=legnth[v][u]
 Our goal is to compute distn-1[u] for all u.
– This can be done by using the dynamic
programming methodology.
2021/12/6 Graphs 110
Bellman and Ford algorithm
(Cont’d)

 The following recurrence can be used to compute distk[u] from


distk-1 [u].
dist k [u ] = min{ dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i

– If the shortest path from v to u with at most k edges has no more than k-1
edges, then distk[u]=distk-1[u].
– If the shortest path from v to u with at most k edges has exactly k edges, then
it is comprised of a shortest path from v to some vertex i followed by the edge
<i,u>. i
at most k-1 edges select the
u
shorter path
...
... at most k-1 edges
v
2021/12/6 Graphs 111
Bellman and Ford algorithm
Example Source vertex: v0
6 ∞ distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 5 1 2
0
5 2 6 3
5
-2 4
3 ∞
5
3 5
-1 6
5 ∞
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 112
Bellman and Ford algorithm
Example Source vertex: v0
6 ∞ distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 5 1 2
0
5 2 6 3
5
-2 4
3 ∞
5
3 5
-1 6
5 ∞
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 113
Bellman and Ford algorithm
Example Source vertex: v0
3 𝟓𝟓 distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 3 1 2 0 3 3 5 5 4 ∞
0
5 2 6 3
5
-2 4
3 ∞
5
3 5
-1 6
5 𝟒𝟒
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 114
Bellman and Ford algorithm
Example Source vertex: v0
3 5 distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 3 1 2 0 3 3 5 5 4 ∞
0
5 2 6 3
5
-2 4
3 ∞
5
3 5
-1 6
5 4
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 115
Bellman and Ford algorithm
Example Source vertex: v0
1 𝟐𝟐 distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 3 1 2 0 3 3 5 5 4 ∞
0
5 2 6 3 0 1 3 5 2 4 7
5
-2 𝟕𝟕 4
3
5
3 5
-1 6
5 4
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 116
Bellman and Ford algorithm
Example Source vertex: v0
1 2 distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 3 1 2 0 3 3 5 5 4 ∞
0
5 2 6 3 0 1 3 5 2 4 7
5
-2 7 4
3
5
3 5
-1 6
5 4
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 117
Bellman and Ford algorithm
Example Source vertex: v0
1 𝟎𝟎 distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 3 1 2 0 3 3 5 5 4 ∞
0
5 2 6 3 0 1 3 5 2 4 7
5
-2 𝟓𝟓 4 0 1 3 5 0 4 5
3
5
3 5
-1 6
5 4
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 118
Bellman and Ford algorithm
Example Source vertex: v0
1 0 distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 3 1 2 0 3 3 5 5 4 ∞
0
5 2 6 3 0 1 3 5 2 4 7
5
-2 5 4 0 1 3 5 0 4 5
3
5
3 5
-1 6
5 4
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 119
Bellman and Ford algorithm
Example Source vertex: v0
1 0 distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 3 1 2 0 3 3 5 5 4 ∞
0
5 2 6 3 0 1 3 5 2 4 7
5
-2 𝟑𝟑 4 0 1 3 5 0 4 5
3
5 0 1 3 5 0 4 3
3 5
-1 6
5 4
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 120
Bellman and Ford algorithm
Example Source vertex: v0
1 0 distk
-1 k
1 4 0 1 2 3 4 5 6
1 0 6 5 5 ∞ ∞ ∞
6 -2 3
0 3 1 2 0 3 3 5 5 4 ∞
0
5 2 6 3 0 1 3 5 2 4 7
5
-2 3 4 0 1 3 5 0 4 5
3
5 0 1 3 5 0 4 3
3 5
-1 6 0 1 3 5 0 4 3
5 4
dist k [u ] = min{dist k −1[u ], min{dist k −1[i ] + length[i ][u ]}}
i
2021/12/6 Graphs 121
Bellman and Ford algorithm
NOTE: we can use the same memory location dist[u] for distk[u],k=1,...,n-1.
void BellmanFord(const int n,const int v) Finding single source /
{ int i,k; /* source vertex: v */ all destinations shortest
for(i = 0; i < n; i++) dist[i]=cost[v][i]; paths with negative edge
for(k=2; k<=n-1;k++) weights
for(each u such that u!=v and u has at least one incoming edge)
for(each<i,u> in the graph)
if (dist[u]>dist[i]+cost[i][u]) dist[u] = dist[i]+cost[i][u];
}

O(n2) when adjacency matrices are used


O(e) when adjacency lists are used
2021/12/6 Graphs 122
Analysis of Bellman and Ford Algorithm
 The algorithm can terminate either after n-1
iterations or after the first iteration in which
no dist values are changed.
 The overall complexity is O(n3) when
adjacency matrices are used and O(ne)
when adjacency lists are used.
 This algorithm can be improved by using a
queue.

2021/12/6 Graphs 123


All Pairs Shortest Paths
Find the shortest paths between all pairs of vertices.
Solution 1
–Apply shortest path n times with each vertex as source.
O(n3)
Solution 2
–Represent the graph G by its cost adjacency matrix with
cost[i][j]
–If the edge <i,j> is not in G, the cost[i][j] is set to some
sufficiently large number
–A[i][j] is the cost of the shortest path form i to j, using only
those intermediate vertices with an index <= k
2021/12/6 Graphs 124
All Pairs Shortest Paths (Cont’d)
n-1
 The cost of the shortest path from i to j is A [i][j],
as no vertex in G has an index greater than n-1
-1
 A [i][j]=cost[i][j]
0 1 2 n-1 -1
 Calculate the A, A, A, ..., A from A iteratively
k k-1 k-1 k-1
 A [i][j]=min{A [i][j], A [i][k]+A [k][j]}, k>=0

Assumption: Graph G has no cycles with a negative length.

2021/12/6 Graphs 125


Graph with negative cycle

-2  0 1 ∞
− 2 0 1 
0 1 2  
1 1  ∞ ∞ 0 

(a) Directed graph (b) A-1

Graph G has a cycle 0,1,0 with a negative length (-1).


A1 [0][2] ≠ min{A0[0][2], A0[0][1]+A0 [1][2]}=2
because A1[0][2]= -∝.
The length of the shortest path from vertex 0 to vertex 2 is -∝.
0, 1, 0, 1,0, 1, …, 0, 1, 2
2021/12/6 Graphs 126
Algorithm for All Pairs Shortest Paths
void allcosts(int cost[][MAX_VERTICES],
int distance[][MAX_VERTICES], int n)
{
int i, j, k; Cost[ ][ ]: adjacency matrix
for (i=0; i<n; i++)
for (j=0; j<n; j++)
distance[i][j] = cost[i][j];
for (k=0; k<n; k++)
for (i=0; i<n; i++) Time Complexity: O(n3)
for (j=0; j<n; j++)
if (distance[i][k]+distance[k][j]
< distance[i][j])
distance[i][j]=
distance[i][k]+distance[k][j];
}
2021/12/6 Graphs 127
Example for All-Pairs Shortest-Paths Problem

6
V0 V1
4
3 11 2

V2

(a)Digraph G (b)Cost adjacency matrix for G

2021/12/6 Graphs 128


Example for All-Pairs Shortest-Paths Problem

A-1 0 1 2
6 A0 0 1 2
0 0 4 11 V0 V1 0 0 4 11 A0[2][1]=min{A-1[2][1],
4 1 6 0 2
1 6 0 2 3 11 2 A-1[2][0]+A-1[0][1]}
2 3 ∞ 0 2 3 7 0 =min{∞,3+4}
V2 =7

A1 0 1 2 A2 0 1 2
0 0 4 6 A1[0][2]=min{A0[0][2], 0 0 4 6 A2[1][0]=min{A1[1][0],
1 6 0 2 A0[0][1]+A0[1][2]} 1 5 0 2 A1[1][2]+A1[2][0]}
=min{11,4+2} =min{6,2+3}
2 3 7 0 =6 2 3 7 0 =5

2021/12/6 Graphs 129


Floyd’s Algorithm
• Time complexity is Θ(n3) time.
• Works so long as there is no cycle whose length
is < 0.
• When there is a cycle whose length is < 0, some
shortest paths aren’t finite.
 If vertex 1 is on a cycle whose length is -2, each time
you go around this cycle once you get a 1 to 1 path
that is 2 units shorter than the previous one.
• Simpler to code, smaller overheads.

2021/12/6 All-Pairs Shortest Paths 130


Floyd’s Shortest Paths Algorithm
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
c(i,j) = min{c(i,j), c(i,k) + c(k,j)};

 Time complexity is Θ(n3).


 Θ(n2) space is needed for c array.

2021/12/6 All-Pairs Shortest Paths 131


Transitive Closure
Goal: given a graph with unweighted edges, determine if there is a path
from i to j for all i and j.
(1) Require positive path (> 0) lengths. transitive closure matrix
(2) Require nonnegative path (≥0) lengths. reflexive transitive closure matrix
0 0 1 0 0 0 
1 0 0 1 0 0
2 0 0 0 1 0 
 
0 1 2 3 4 3  0 0 0 0 1 
4 0 0 1 0 0

(a) Digraph G (b) Adjacency matrix A for G
0 1 2 3 4 0 1 2 3 4
0 0 1 1 1 1 0 1 1 1 1 1
1 0
 0 1 1 1 1 0 1 1 1 1

2 0 0 1 1 1 2 0 0 1 1 1
   
3 0 0 1 1 1 3 0 0 1 1 1
4 0 0 1 1 1 cycle reflexive
4 0 0 1 1 1

(c) transitive closure matrix A+ (d) reflexive transitive closure matrix A*


There is a path of length > 0 There is a path of length ≥0
Transitive Closure
Definition. The transitive closure matrix, denoted A+, of a
graph, G, is a matrix such that A+[i][j] =1 if there is a path of
length>0 from i to j; otherwise, A+[i][j]=0.

Definition. The reflexive transitive closure matrix, denoted


A*, of a graph, G, is a matrix such that A*[i][j] =1 if there is a
path of length≥0 from i to j; otherwise, A*[i][j]=0.
– The only difference between A* and A+ is in the terms on the
diagonal.
– A+[i][i]=1 iff there is a cycle of length>1 containing vertex i,
whereas A*[i][i] is always one, as there is a path of length 0 from
i to i.

2021/12/6 Graphs 133


Transitive Closure
 We can use function allcosts to compute A+.
– We begin with length[i][j]=1 if <i,j> is an edge in G and
length[i][j]=0 if <i,j> is not in G.
– The line “if (a[i][k]+a[k][j]<a[i][j]) a[i][j]=a[i][k]+a[k][j]”
of allcosts is replaced by a[i][j] = a[i][j] || (a[i][k]&&a[k][j]);
– Upon termination of AllLengths, the final matrix a is A+.
 A* can be obtained from A+ by setting all diagonal
elements equal to 1.
 The total time is O(n3).

2021/12/6 Graphs 134


Transitive Closure
 The transitive closure of an undirected graph G
can be found more easily from its connected
component.
– A+ can be determined in O(n2) time by first determining
the connected components of the graph.
• if i and j are distinct and in the same connected
component, A+[i][j]=1
• if i and j are in different connected components,
A+[i][j]=0.
• if the connected component containing i has at least two
vertices, A+[i][i]=1.

2021/12/6 Graphs 135


Outline
 The Graph Abstract Data Type
 Elementary Graph Operations
– DFS, BFS, Connected Components, Spanning Trees,
Biconnected Components
 Minimum Cost Spanning Trees
– Kruskal’s Algorithm, Prim’s Algorithm, Sollin’s Algorithm
 Shortest Paths and Transitive Closure
– Single Source/All Destination (nonnegative edge costs,
general weights), All Pairs Shortest Paths, Transitive Closure
 Activity Networks
– AOV Networks, AOE Networks
2021/12/6 Graphs 136
Activity-on-Vertex (AOV)
Networks
Definition. A directed graph G in which the
vertices represent tasks or activities and the
edges represent precedence relations between
tasks is an activity-on-vertex network or AOV
network.
– The project represented by an AOV network
is feasible if the AOV network is an acyclic
graph.
2021/12/6 Graphs 137
AOV Networks (Cont’d)
Definition. Vertex i in an AOV network G is
a predecessor of vertex j iff there is a
directed path from vertex i to vertex j.
– i is an immediate predecessor of j iff <i,j> is
an edge in G.
– If i is a predecessor of j, then j is a successor
of i.
– If i is an immediate predecessor of j, then j is
an immediate successor of i.

2021/12/6 Graphs 138


Example
C3 and C6 are immediate predecessors of C7.
C9,C10,C12 and C13 are immediate successors of C7.
C14 is a successor, but not an immediate successor of C3
C9

C1 C10 C11

C8
C2 C12

C7
C3 C13 C14

C4 C5 C6 C15

2021/12/6 Graphs 139


Partial order
Definition.
A relation • is transitive iff it is the case that for all
triples i,j,k, i • j and j • k⇒ i •k.

A relation is irreflexive on a set S if for no element x


in S it is the case that x •x.

A precedence relation that is both transitive and


irreflexive is a partial order.

2021/12/6 Graphs 140


Course number Course name Prerequisites
Topological order:
C1 Programming I None
linear ordering of vertices
C2 Discrete Mathematics None
C3 Data Structures C1, C2
of a graph
C4 Calculus I None
∀i, j if i is a predecessor of
C5 Calculus II C4 j, then i precedes j in the
C6 Linear Algebra C5 linear ordering
C7 Analysis of Algorithms C3, C6 Two topological orders:
C8 Assembly Language C3
C9 Operating Systems C7, C8 1. C1, C2, C4, C5, C3, C6, C8,C7, C10, C13,
C10 Programming Languages C7 C12, C14, C15, C11, C9
C11 Compiler Design C10
C12 Artificial Intelligence C7
2. C4, C5, C2, C1, C6, C3, C8, C9

C13 Computational Theory C7


C15, C7, C9, C10, C11,
C14 Parallel Algorithms C13
C13, C12, C14
C10 C11
C15 Numerical Analysis C1 C5

Courses needed for a computer C8


science degree at a C12

hypothetical university C2

C3 C7 C13 C14

Figure 6.37: An AOV network


2021/12/6 141

C4 C5 C6 C15
Design of an algorithm for topological sorting
1 Input the AOV network. Let n be the number of vertices.
2 for (i = 0; i <n; i++) { /* output the vertices */
3 if (every vertex has a predecessor) return;
4 /* network has a cycle and is infeasible */
5 pick a vertex v that has no predecessors;
6 output v;
7 delete v and all edges leading out of v;
8 }

2021/12/6 Graphs 142


Topological Sort: Example
Initial 1
1 1

Vertex 0 4 Vertex 3
2 4 2 2 4
0 deleted deleted

3 5 3 5 5

Vertex 2
deleted
1
Vertex 1 1 Vertex 5
4 deleted deleted
4
4
Topological order generated: 0, 3, 2, 5, 1, 4
(Shaded vertices represent candidates for deletion) 5
Issues in Data Structure
Consideration

Decide whether a vertex has any predecessors.


–Each vertex has a count.
Decide a vertex together with all its incident
edges.
–Adjacency list

2021/12/6 Graphs 144


Internal representation used by topological sorting algorithm
headnodes node
count link vertex link
[0] 0  1  2  3 NULL

[1] 1  4 NULL

1
[2] 1  4  5 NULL

0 2 4
[3] 1  5  4 NULL

[4] 3 NULL 3 5

[5] 2 NULL

2021/12/6 Graphs 145


Some declarations used in function topSort

typedef struct node *nodePointer;


typedef struct node {
int vertex;
nodePointer link;
};
typedef struct {
int count; /* in-degree of the vertex */
nodePointer link; /* a pointer to the first node */
} hdnodes;
hdnodes graph[MAX_VERTICES];

2021/12/6 Graphs 146


Topological sort
void topsort (hdnodes graph [] , int n)
{
int i, j, k, top;
nodePointer ptr;
/* create a stack of vertices with no predecessors */
top = -1;
for (i = 0; i < n; i++)
if (!graph[i].count) {no predecessors, stack is linked through
O(n) time graph[i].count = top; count field
top = i;
}
for (i = 0; i < n; i++)
if (top == -1) {
fprintf(stderr, “\n Network has a cycle. Sort terminated. \n”);
exit(EXIT_FAILURE);
}
2021/12/6 Graphs 147
} Continued
else {
j = top; /* unstack a vertex */
top = graph[top].count;
printf(“v%d, “, j);
for (ptr = graph [j]. link; ptr ;ptr = ptr ->link ){
/* decrease the count of the successor vertices of j */
k = ptr ->vertex;
O(e) time graph[k].count --;
if (!graph[k].count) {
/* add vertex k to the stack*/
graph[k].count = top;
top = k;
}
}
}
Time complexityof algorithm topSort: O(e+n)
Graphs 148
}
Activity-on-Edge (AOE) Networks
 The tasks to be performed on a project are
represented by directed edges.
 Vertices in the network represent events.
– Events signal the completion of certain activities.
– Activities represented by edges leaving a vertex cannot
be started until the event at that vertex has occurred.
– An event occurs only when all activities entering it
have been completed.

2021/12/6 Graphs 149


An AOE Network

1 6
a4=1 a7=9 a10=2
a1=6
start finish
4 8
0
a2=4
a5=1 a8=7 7 a11=4
2

a3=5
a9=4
a6=2
3 5

0,1,4,6,8
are critical paths of length 18.
0,1,4,7,8

2021/12/6 Graphs 150


Application of AOE Network
Performance Evaluation
–minimum amount of time
–activity whose duration time should be shortened
–…
Critical path
–a path that has the longest length
–minimum time required to complete the project
–v0, v1, v4, v7, v8 or v0, v1, v4, v6, v8 (18)

2021/12/6 Graphs 151


Critical-Path Analysis
 The purpose of critical-path analysis is to identify
critical activities so that resources may be
concentrated on these activities in an attempt to
reduce project finish time.
– Be valuable in evaluating project performance and
identifying bottlenecks.
– Speeding a critical activity will not result in a reduced
project length unless that activity is on all critical paths.
• Deleting all noncritical activities for which e(i)≠l(i) from
the AOE network, all critical paths may be found by just
generating all paths from the start-to-finish vertex.

2021/12/6 Graphs 152


Other Factors
Earliest time that vi can occur
–the length of the longest path from v0 to vi
–the earliest start time for all activities leaving vi
–early(7) = early(8) = 7
Latest time of activity
–the latest time the activity may start without increasing
the project duration 1 6
a4=1 a7=9 a10=2
–late(6)=8, late(8)=7 a =6
start 1 finish
4 8
–early(6)=5, early(8)=7 0
a =4
2
2 a5=1 a8=7 7 a11=4

a3=5
a9=4
3 a6=2 5

2021/12/6 Graphs 153


Other Factors
Critical activity
–an activity for which early(i)=late(i)
–early(7)=late(7) (=7)
late(i)-early(i)
–measure of how critical an activity is
–late(5)-early(5)=6-4=2
1 6
a4=1 a7=9 a10=2
a1=6 finish
start
0 4 8
a2=4
2 a5=1 a8=7 7 a11=4

a3=5
a9=4
3 a6=2 5

2021/12/6 Graphs 154


earliest, early, latest, late
16
6
1 6 6 16
0 7 a9=2
a0=6 6 a3=1 16
0 7 a6=9 16 18
0 6
7
0 4 8
0 14
0 a1=4 4 7 7 18
4 a4=1 a7=7 14 a10=4
2
6 7 7 14
2
0 6 14
a2=5
3 7
a8=4
7 10
5
5 5
3
a5=2
8 10
8
2021/12/6 Graphs 155
Calculation of early and late
activity times
 the earliest event time, ee[i], and the latest
event time, le[i], for all events, i, in the
network.
ee[ j ] = max{ee[i ] + duration of < i, j >} (6.2)
i∈P ( j )

le[ j ] = min{le[i ] − duration of < j , i >} (6.3)


i∈S ( j )

if activity ai is represented by the edge < k , l >,


(6.1)
e[i ] = ee[k ] and l[i ] = le[l ] − duration of activity ai .
2021/12/6 Graphs 156
1 6
a4=1 a7=9 a10=2
a =6 finish
start 1 4 8
0
a2=4
2 a5=1 a8=7 7 a11=4
a3=5 ee le 1
6 6 6
a9=4 0 0 a4=1 a7=9 a10=2 18 18
3 a6=2 5 a1=6 16 16
finish
start 4 8
0 4 6
a2=4 7 7
2 a8=7 7
a5=1 a11=4
14 14
a3=5
a9=4
3 a6=2 5

5 8 7 10

1. The earliest time that an event i can occur (ee) is the length of the longest
path from the start vertex 0 to the vertex i.
2. The earliest time an event can occur determine the earliest start time for all
activities represented by edges leaving that vertex. Denote this time by e(i)
for activity ai.
3. The latest time for activity i, l(i), is the time that activity i may start without
increasing the project duration 157
4. All activities for which e(i)=l(i) are called critical activities.
Calculation of Early Event Times

The earliest event time for event j (ee[j])


ee[0]=0
ee[j] = max{ee[i] + duration of <i,j>}
i ∈p(j)

vi1

forward stage vi2 vj


vin
if (earliest[k] < earliest[j]+ptr->duration)
earliest[k]=earliest[j]+ptr->duration
2021/12/6 Graphs 158
Computation of earliest time (ee) from
Equation (6.2) using a topological order
(0, 3, 5, 2, 1, 4, 7, 6, 8)

ee le 1
6 6 6
a4=1 a7=9 a10=2 18 18
0 0
ee[0]=0 start
a1=6
4
16 16
8
finish
0 4 6
ee[3]=max{ee[0] + 5}=5 a2=4 7 7
7
2 a5=1 a8=7 a11=4
ee[5]=max{ee[3] + 2}=7 14 14
ee[2]=max{ee[0]+4 }=4 a3=5
a9=4
ee[1]=max{ee[0] + 6}=6 3 a6=2 5

ee[4]=max{ee[1]+1;ee[2]+1}=7 5 8 7 10

ee[7]=max{ee[4]+7;ee[5]+4}=14
ee[6]=max{ee[4] + 9}=16
ee[8]=max{ee[6]+ 2; ee[7]+4}=18

2021/12/6 Graphs 159


Calculation of Latest Event Times
 The latest event time for event j (le[j])
le[n-1]=ee[n-1]
le[j] = min{le[i] - duration of <j,i>}
i ∈s(j)

vi1

vj vi2 backward stage

vin .
.
if (latest[k] > latest[j]-ptr->duration)
.
latest[k]=latest[j]-ptr->duration
2021/12/6 Graphs 160
Computation of latest time (le) from Equation
(6.3) using a reverse topological order
(8, 6, 7, 4, 1, 2, 5, 3, 0)

le[8]=ee[8]=18 ee le 1
6 6 6
le[6]=min{le[8] - 2}=16 0 0
a =6
a4=1 a7=9
16 16
a10=2 18 18

start 1 finish
le[7]=min{le [8] - 4}=14 0 4 6
4 8
a =4
le[4]=min{le [6] - 9;le [7] -7}=7 2
2
a5=1
7 7
a8=7 7
a11=4
le[1]=min{le [4] - 1}=6 a =53
14 14

le[2]=min{le [4] - 1}=6 3 a6=2 5


a9=4

le[5]=min{le [7] - 4}=10 5 8 7 10


le[3]=min{le [5] - 2}=8
le[0]=min{le [1] - 6; le [2]- 4; le [3] -5}=0

2021/12/6 Graphs 161


Finding critical paths
Compute ee for each event
ee[ j ] = max{ee[i ] + duration of < i, j >}
i∈P ( j )

Compute le for each event le[ j ] = min{le[i ] − duration of < j , i >}


i∈S ( j )

Compute e and l for each if activity ai is represented by the edge < k , l >,
activity e[i ] = ee[k ] and l[i ] = le[l ] − duration of activity ai .

Determine critical If we want to reduce the project length


e[i]=l[i] by speeding up one activity, the activity on
activities
all critical paths should be chosen.
Delete noncritical Find all paths from the start-
activities from the graph vertex to the finish-vertex

2021/12/6 Graphs 162


Example
early late slack critical
activity time time
e l l-e l-e=0 if activity ai is represented by the edge < k , l >,
a1 0 0 0 Yes e[i ] = ee[k ] and l[i ] = le[l ] − duration of activity ai .
a2 0 2 2 No
a3 0 3 3 No ee le 6 6 6
1
a4 6 6 0 Yes 0 0
a1=6
a4=1 a7=9
16 16
a10=2 18 18

start finish
4 8
a5 4 6 2 No 0 4 6
a2=4 7 7
7
a6 5 8 3 No 2 a5=1 a8=7 a11=4
14 14
a7 7 7 0 Yes a3=5
a9=4
a8 7 7 0 Yes 3 a6=2 5

a9 7 10 3 No 5 8 7 10

a10 16 16 0 Yes
a11 14 14 0 Yes 163
Data structure for AOE networks
count first vertex dur
[0] 0 1 6 2 4 3 5 null

[1] 1 4 1 null

[2] 1 4 1 null

[3] 1 5 2 null

[4] 2 6 9 7 7 null

[5] 1 7 4 null

[6] 1 8 2 null

[7] 2 8 4 null

[8] 2 0

2021/12/6 Graphs 164


Computation of ee
ee [0] [1] [2] [3] [4] [5] [6] [7] [8] Stack
initial 0 0 0 0 0 0 0 0 0 [0]
output 0 0 6 4 5 0 0 0 0 0 [3,2,1]
output 3 0 6 4 5 0 7 0 0 0 [5,2,1]
output 5 0 6 4 5 0 7 0 11 0 [2,1]
output 2 0 6 4 5 5 7 0 11 0 [1]
output 1 0 6 4 5 7 7 0 11 0 [4]
output 4 0 6 4 5 7 7 16 14 0 [7,6]
output 7 0 6 4 5 7 7 16 14 18 [6]
output 6 0 6 4 5 7 7 16 14 18 [8]
output 8

2021/12/6 Graphs 165


ee le
ee 1
0
6 ∞ 6
0 ∞ a =6
a4=1 a7=9 a10=2 180 ∞
1 16
0 ∞ finish
start
4 8
0 0
4 ∞
a2=4 0
5
7
2 a5=1 a∞8=7 7 a11=4
14
11
0 ∞
a3=5
a9=4
3 a6=2 5
[0]
[321]
[521]
[21]
[1]
[4]
[76]
[6]
[8]
[]
0
5 ∞ 0
7 ∞

2021/12/6 Graphs 166


A Modified Algorithm of Topological Sort
for Computing ee
void Modifiedtopsort (hdnodes graph [] , int n)
{
int i, j, k, top;
nodePointer ptr;
/* create a stack of vertices with no predecessors */
top = -1;
for (i = 0; i < n; i++) { ee[i]=0; // initialize ee array to zero
if (!graph[i].count) {no predecessors, stack is linked through
O(n) time graph[i].count = top; count field
top = i; }
}
for (i = 0; i < n; i++)
if (top == -1) {
fprintf(stderr, “\n Network has a cycle. Sort terminated. \n”);
exit(EXIT_FAILURE);
}
2021/12/6 Graphs 167
} Continued
else {
j = top; /* unstack a vertex */
top = graph[top].count;
printf(“v%d, “, j);
for (ptr = graph [j]. link; ptr ;ptr = ptr ->link ){
/* decrease the count of the successor vertices of j */
k = ptr ->vertex;
if (ee[k]<ee[j]+ptr->duration) ee[k]= ee[j]+ptr->duration;
O(e) time graph[k].count --; ee[j] = max{ee[i] + duration of <i,j>}
if (!graph[k].count) { i ∈p(j)
/* add vertex k to the stack*/
graph[k].count = top;
top = k;
}
} Time complexityof algorithm topSort: O(e+n)
} Graphs 168
}
Computation of le
le [0] [1] [2] [3] [4] [5] [6] [7] [8] Stack
initial 18 18 18 18 18 18 18 18 18 [8]
output 8 18 18 18 18 18 18 16 14 18 [7,6]
output 7 18 18 18 18 7 10 16 14 18 [5,6]
output 5 18 18 18 8 7 10 16 14 18 [3,6]
output 3 3 18 18 8 7 10 16 14 18 [6]
output 6 3 18 18 8 7 10 16 14 18 [4]
output 4 3 6 6 5 7 10 16 14 18 [2,1]
output 2 2 6 6 8 7 10 16 14 18 [1]
output 1 0 6 6 8 7 10 16 14 18 [0]
output 0

2021/12/6 Graphs 169


ee le
ee 6 6
∞ 6
ee le 1
1 a6 =1 6
4 a7=9 6 a10=2 18 18
0 ∞3 a =6
2
0 a4=1 a7=9 16 16 ∞ a10=2 finish
0 1 a =6 18 18
start
0 16 16
start0 1 4 ∞ 6
4 8 finish
4 8
0a2=4 4 6 7 ∞ 7
a2=42 a5=1 7 8=7
a 7 7 a11=4
2 a5=1 a 8 =7 7 a11=4
14 14 ∞
a3=5 14 14
a3=5
a9=4
3 a6=2 5 a9=4
3 a6=2 5 [8]
[76]
[56]
[36]
[6]
[4]
[2,1]
[1]
[0]
5 8
∞ 7 10
∞ []
5 8 7 10

2021/12/6 Graphs 170


Computation of e and l
early late slack critical
activity time time
e l l-e l-e=0
a1 0 0 0 Yes if activity ai is represented by the edge < k , l >,
e[i ] = ee[k ] and l[i ] = le[l ] − duration of activity ai .
a2 0 2 2 No
a3 0 3 3 No
6 6 6
1
a4 6 6 0 Yes 0 0 a4=1 a7=9 a10=2 18 18
a1=6 16 16
start finish
a5 4 6 2 No 0 4 6
4 8
a2=4
a6 5 8 3 No 2 a5=1
7 7
a8=7 7
a11=4
a7 7 7 0 Yes a3=5
14 14

a8 7 7 0 Yes 3 a6=2 5
a9=4

a9 7 10 3 No 5 8 7 10

a10 16 16 0 Yes
a2021/12/6
11 14 14 0 Yes 171
Determine Critical Paths
Delete all noncritical activities
Generate all the paths from the start to

finish vertex.
1 6
a4 a7 a10
start
a1
finish
0 4 8

Two critical paths: a8 a11


a1 , a4 , a7 , a10 7
a1 , a4 , a8 , a11
Figure 6.43:Graph with noncritical activities deleted 172
Figure 6.46: AOE network with unreachable activities

a5
4 5

1 a7
a6 ee[i]=0
a1 a3

0 3

a2 a4
2
When a critical-path analysis is carried out on such
networks, there will be several vertices with ee[i]=0.
2021/12/6 Graphs 173
Exercise 2. (1) Find e(i) and l(i) for each activity i;
ee (2) What is the earliest time the project can finish? 23
e/l i (3) Which activities are critical? a2, a4, a6, a8, a9,
a10, a11, a12, a14
le 5 5/9 12 12/15 16
a3=3 a7=4 16/19
1 3 5
0/4 a13=4
a1=5 9 12 19
a8=4
12/12 16 21 23
0 a4=6 a11=5 a14=2
0 a6=3 6 16/16
8 9 finish
start0 6/6 21/21
12/12 15/15 16 21 23
6
a9=1 a12=2
a2=6 15 19
19/19
0/0 2 4 7
a5=3 a10=4
6 6/12 15 15/15 19 An AOE Network
3 critical paths:
a2, a4, a8, a11, a14 Speeding up any one of a2 , a4 ,
a2, a4, a6, a9, a11, a14 and a14 will reduce the project 174
a2, a4, a6, a10, a12, a14 length.

You might also like