Professional Documents
Culture Documents
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.
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.
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
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
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
2021/12/6 Graphs 28
Alternate Node Structure for Adjacency
tail head column link for head row link for tail
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 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)
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];
2021/12/6 Graphs 34
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
[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)
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
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]
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
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.
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
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
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”);
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)
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
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
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
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
1500/4
1250/5
∞
2450/3
∞
3250/2
∞ 250/4
∞
1650/5
∞
3350/7
∞
1150/5
∞
– 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];
}
-2 0 1 ∞
− 2 0 1
0 1 2
1 1 ∞ ∞ 0
6
V0 V1
4
3 11 2
V2
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
C1 C10 C11
C8
C2 C12
C7
C3 C13 C14
C4 C5 C6 C15
hypothetical university C2
C3 C7 C13 C14
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 }
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
[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
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
a3=5
a9=4
3 a6=2 5
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
vi1
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
vi1
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
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 .
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
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
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.