슬라이드 제목 없음

Download Report

Transcript 슬라이드 제목 없음

Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Chapter #6:
GRAPHS
Fundamentals of
Data Structures in C
Horowitz, Sahni and Anderson-Freed
Computer Science Press
July, 1997
Transparency No. 6-1
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
The Graph Abstract Data Type
Introduction
c
C
d
g
C
g
D
A
e
Kneiphof
c
d
e
A
a
D
b
f
B
a
b
B
f
(a)
(b)
the bridges of Koenigsberg
Transparency No. 6-2
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
Definition
a graph G = (V,E) where
V(G): set of vertices
- finite and nonempty
E(G): set of edges
- finite and possibly empty
undirected graph
- unordered: (vi,vj) = (vj,vi)
directed graph
- ordered: <vi,vj>  <vj,vi>
Transparency No. 6-3
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
The Graph Abstract Data Type
0
0
0
1
1
2
3
3
2
4
5
1
6
2
G1
G2
G3
three sample graphs
V(G1)
E(G1)
V(G2)
E(G2)
V(G3)
=
=
=
=
=
{0,1,2,3}
{(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
{0,1,2,3,4,5,6}
{(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
{0,1,2}
E(G3) = {<0,1>,<1,0>,<1,2>}
Transparency No. 6-4
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
The Graph Abstract Data Type
restrictions on graphs
1)no edge from a vertex, i, back to
itself (no self loop)
- not allowed (vi,vi) or <vi,vi>
2)no multiple occurrences of the
same edge ( multigraph)
0
0
2
1
3
1
2
(a)
(b)
examples of graph with feedback
loops and a multigraph
Transparency No. 6-5
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
complete graph
- the maximum number of
- undirected graph with
max number of edges =
- directed graph with n
max number of edges =
edges
n vertices
n(n-1)/2
vertices
n(n-1)
Transparency No. 6-6
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
adjacent
- vi and vj are adjacent
if (vi,vj)  E(G)
adjacent to(from) for digraphs
- <v0,v1>: a directed edge
- vertex v0 is adjacent to vertex v1
- vertex v1 is adjacent from vertex v0
Transparency No. 6-7
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
incident
- an edge e = (vi,vj) is incident on
vertices vi and vj
- an edge <vi,vj> is incident on
vi and vj
Transparency No. 6-8
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
The Graph Abstract Data Type
subgraph G’ of G
- V(G’)  V(G) and
- E(G’)  E(G)
0
1
0
0
2
1
1
2
2
3
3
(i)
(ii)
(iii)
(iv)
some of the subgraphs of G1
Transparency No. 6-9
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
0
0
1
(i)
(ii)
0
0
1
1
2
2
(iii)
(iv)
some of the subgraphs of G3
Transparency No. 6-10
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
path (from vertex vp to vertex vq)
- a sequence of vertices,
vp, vi1, vi2, ···, vin, vq such that
(vp,vi1),(vi1,vi2),···,(vin,vq) are
edges in an undirected graph or
<vp,vi1>,<vi1,vi2>,···,<vin,vq> are
edges in a directed graph
length of path
- number of edges on the path
Transparency No. 6-11
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
simple path
- a path in which all vertices,
except possibly the first and the
last, are distinct
cycle
- a path in which the first and last
vertices are the same
- simple cycle
for directed graph
- add the prefix “directed” to the
terms cycle and path
- simple directed path
- directed cycle
- simple directed cycle
Transparency No. 6-12
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
connected
- vertex v0 and v1 is connected, if
there is a path from v0 to v1 in an
undirected graph G
- an undirected graph is connected
if, for every pair of vertices vi
and vj, there is a path from vi to
vj
Transparency No. 6-13
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
The Graph Abstract Data Type
connected component
(of an undirected graph)
- maximal connected subgraph
4
H1
H2
0
1
5
2
6
3
7
G4
a graph with two connected components
Transparency No. 6-14
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
The Graph Abstract Data Type
strongly connected
(in a directed graph)
- for every pair of vertices vi, vj
in V(G) there is a directed path
from vi to vj and also from vj to vi
1
4
2
3
strongly connected directed graph
Transparency No. 6-15
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
The Graph Abstract Data Type
strongly connected component
- maximal subgraph that is strongly
connected
0
2
1
strongly connected components of G3
Transparency No. 6-16
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
degree (of a vertex)
- number of edges incident to that
vertex
in-degree (of a vertex v)
- number of edges that have v as the
head (for directed graphs)
out-degree (of a vertex v)
- number of edges that have v as the
tail (for directed graphs)
Transparency No. 6-17
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
The Graph Abstract Data Type
special types of graphs
tree
- an acyclic connected graph
bipartite garph
planar graph
complete graph
Transparency No. 6-18
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Graph Representations
Adjacency matrix
- G = (V,E) with |V| = n(1)
- two-dimensional n  n array,
say adj_mat[][]
- adj_mat[i][j] =
“1” if (vi,vj) is adjacent
“0” otherwise
2
- space complexity: S(n) = n
- symmetric for undirected graphs
- asymmetric for directed graphs
Transparency No. 6-19
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Graph Representations
0
1
2
3
0
0
1
1
1
2
1
1
0
1
1
1
0
1
1
3
1
1
1
0
0
0
1
0
0
1
2
G1
0
1
2
3
4
5
6
7
1
1
0
0
2
0
1
0
G3
0
0
1
1
0
0
0
0
0
1
1
0
0
1
0
0
0
0
2
1
0
0
1
0
0
0
0
3
0
1
1
0
0
0
0
0
4
0
0
0
0
0
1
0
0
5
0
0
0
0
1
0
1
0
6
0
0
0
0
0
1
0
1
7
0
0
0
0
0
0
1
0
G4
adjacency matrices for G1, G3, and G4
Transparency No. 6-20
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Graph Representations
Adjacency lists
- replace n rows of adjacency matirx
with n linked lists
- every vertex i in G has one list
#define MAX_VERTICES 50
typedef struct node *node_ptr;
typdef struct node {
int vertex;
node_ptr link;
};
node_ptr graph[MAX_VERTICES];
int n = 0; /* vertices currently in use */
vertex
link
Transparency No. 6-21
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Graph Representations
headnode vertex link
0
1
2
3
1
0
2
3
2
0
1
3
3
0
1
2
G1
0
1
1
0
2
2
G3
Transparency No. 6-22
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Graph Representations
0
1
2
1
0
3
2
0
3
3
1
2
4
5
5
4
6
6
5
7
7
6
G4
adjacency lists for G1, G3, and G4
Transparency No. 6-23
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Graph Representations
Inverse adjacency lists
- useful for finding in-degree of a
vertex in digraphs
- contain one list for each vertex
- each list contains a node for each
vertex adjacent to the vertex that
the list represents
0
1
1
0
2
1
inverse adjacency list for G3
Transparency No. 6-24
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Graph Representations
Orthogonal representation
- change the node structure of the
adjacency lists
tail head
headnodes
(shown twice)
column link for head
0
2
0 1
0
1
1
row link for tail
1 0
1 2
2
orthogonal representation for graph G3
Transparency No. 6-25
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Graph Representations
vertices may appear in any order
headnode vertex link
0
3
1
2
1
2
0
3
2
3
0
1
3
2
1
0
alternate order adjacency list for G1
Transparency No. 6-26
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Graph Representations
adjacency multilists
- for undirected graph
- lists in which nodes are shared
among several lists
- exactly one node for each edges
- this node is on the adjacency list
for each of the two vertices it is
incident to
Transparency No. 6-27
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Graph Representations
typedef struct edge *edge_ptr;
typedef struct edge {
short int marked;
int vertex1;
int vertex2;
edge_ptr path1;
edge_ptr path2;
};
edge_ptr graph[MAX_VERTICES];
marked
vertex1
vertex2
path1
path2
node structure for adjacency
multilists
Transparency No. 6-28
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Graph Representations
head nodes
0
1
2
3
N1
0 1
N2
N4
edge(0,1)
N2
0 2
N3
N4
edge(0,2)
N3
0 3 NULL
N5
edge(0,3)
N4
1 2
N6
edge(1,2)
N5
1 3 NULL
N6
edge(1,3)
N6
2 3 NULL NULL edge(2,3)
N5
The lists are: vertex 0:N1  N2  N3
vertex 1:N1  N4  N5
vertex 2:N2  N4  N6
vertex 3:N3  N5  N6
adjacency multilists for G1
Transparency No. 6-29
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Graph Representations
weighted edges
- assign weights to edges of a graph
1)distance from one vertex to
another, or
2)cost of going from one vertex to
an adjacent vertex
- modify representation to signify
an edge with the weight of the edge
 for adj matrix : weight instead of 1
: weight field
 for adj list
network
- a graph with weighted edges
Transparency No. 6-30
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
graph traversals
- visit every vertex in a graph
- what order?
DFS(Depth First Search)
- similar to a preorder tree
traversal
BFS(Breath First Search)
- similar to a level-order tree
traversal
Transparency No. 6-31
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Elementary Graph Operations
v0
v1
v3
v2
v4
v5
v6
v7
(a)
Transparency No. 6-32
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
0
1
2
1
0
3
4
2
0
5
6
3
1
7
4
1
7
5
2
7
6
2
7
7
3
4
5
6
(b)
graph G and its adjacency lists
Transparency No. 6-33
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
depth first search
easy to implement recursively
- stack
a global array visited[MAX_VERTICES]
- initialized to FALSE
- change visited[i] to TRUE when a
vertex i is visited
#define FALSE 0
#define TRUE 1
short int visited[MAX_VERTICES];
Transparency No. 6-34
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Elementary Graph Operations
Ex)
visited:
v0
0
1
2
v1
3
v2
4
5
v3
v4
v5
v6
6
7
v7
v4
v0
v7
v7
v7
v3
v3
v3
v1
v1
v1
v1
v0
v0
v0
v0
Transparency No. 6-35
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
void dfs(int v) {
/* depth first search of a graph beginning with
vertex v */
node_ptr w;
visited[v] = TRUE;
printf(“%5d”, v);
for (w = graph[v]; w; w = w->link)
if (!visited[w->vertex])
dfs(w->vertex);
}
depth first search
- time complexity
representation:
- time complexity
representation:
for adj list
O(e)
for adj matrix
O(n2)
Transparency No. 6-36
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
breadth first search
use a dynamically linked queue
- each queue node contains vertex
and link fields
typedef struct queue *queue_ptr;
typedef struct queue {
int vertex;
queue_ptr link;
};
void insert(queue_ptr *, queue_ptr *, int);
void delete(queue_ptr *);
Transparency No. 6-37
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Elementary Graph Operations
v0
v1
v1 v2
v2
v2 v3 v4
v3 v4 v 5 v6
front
v3
v4
v4 v5 v6 v7
v6
v5
v5 v6 v7
v6 v7
v7
v7
Transparency No. 6-38
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
void bfs(int v) {
node_ptr w; queue_ptr front, rear;
front = rear = NULL;/* initialize queue */
printf(“%5d”, v);
visited[v] = TRUE;
insert(&front, &rear, v);
while (front) {
v = delete(&front);
for (w = graph[v]; w; w = w->link)
if (!visited[w->vertex]) {
printf(“%5d”, w->vertex);
add(&front, &rear, w->vertex);
visited[w->vertex] = TRUE;
}
}
}
breath first search of a graph
Transparency No. 6-39
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
time complexity for bfs()
- time complexity
representation:
- time complexity
representation:
for adj list
O(e)
for adj matrix
O(n2)
Transparency No. 6-40
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
connected components
determine whether or not an
undirected graph is connected
- simply calling dfs(0) or bfs(0)
and then determine if there are
unvisited vertices
list the connected components of a
graph
- make repeated calls to either
dfs(v) or bfs(v) where v is an
unvisited vertex
Transparency No. 6-41
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
void connected(void) {
/* determine the connected components of a
graph */
int i;
for (i = 0; i < n; i++)
if (!visited[i]) {
dfs(i);
printf(“\n”);
}
}
connected components
time complexity: O(n+e)
- total time by dfs: O(e)
- for loop: O(n)
Transparency No. 6-42
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
spanning trees
when graph G is connected dfs or bfs
implicitly partitions the edges
in G into two sets:
 T(for tree edges): set of edges used
or traversed during the search
 N(for nontree edges): set of
remaining edges
- edges in T form a tree that includes
all vertices of G
Def) A spanning tree is any tree that
consists solely of edges in G and
that include all the vertices in G
Transparency No. 6-43
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
a complete graph and three of its
spanning trees
Transparency No. 6-44
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
depth first spanning tree
- use dfs to create a spanning tree
breadth first spanning tree
- use bfs to create a spanning tree
Transparency No. 6-45
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Elementary Graph Operations
v0
v0
v1
v3
v2
v4
v5
v1
v6
v7
(a) dfs(0) spanning tree
v3
v2
v4
v5
v6
v7
(b) bfs(0) spanning tree
dfs and bfs spanning trees
Transparency No. 6-46
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
properties of spanning trees
1) if we add a nontree edge into a
spanning tree  cycle
2) spanning tree is a minimal subgraph
G’, of G such that V(G’) = V(G) and
G’ is connected
3) |E(G’)| = n - 1 where |V(G)| = n
 minimum cost spanning trees
Transparency No. 6-47
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
biconnected components and
articulation points(cut-points)
articulation point
- a vertex v of G
- deletion of v, together with all
edges incident on v, produce a
graph, G’, that has at least two
connected components
biconnected graph
- a connected graph that has no
articulation points
Transparency No. 6-48
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Elementary Graph Operations
v0
v1
v3
v2
v4
v5
v6
v7
Example of biconnected graph
Transparency No. 6-49
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Elementary Graph Operations
0
8
1
7
2
3
4
9
5
6
a connected graph which is not
biconnected
 articulation points are 1,3,5,7
Transparency No. 6-50
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
biconnected component
- maximal biconnected subgraph,H ,of
connected undirected graph G
- two biconnected components of the
same graph have no more than one
vertex in common
- no edge can be in two or more
biconnected components of a graph
- biconnected components of a graph
G partition the edges of G
Transparency No. 6-51
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Elementary Graph Operations
0
8
1
7
2
0
3
9
5
4
8
6
(a) connected graph
1
7
1
2
7
7
3
4
9
3
5
(b) biconnected components
5
6
a connected graph and its
biconnected components
Transparency No. 6-52
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
depth first number, or dfn
- sequence in which the vertices are
visited during the depth first
search
- if u is an ancestor of v in the
first depth spanning tree,
dfn(u) < dfn(v)
Transparency No. 6-53
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Elementary Graph Operations
0
4
3
2
9
0
1
2
0
5
3
5
8
7 7
9
8
3
5
1
4
5
2
6
2
4
6
1
6
(a) depth first spanning tree
dfs(3)
6
7
3
1
7
4
8
0
9
9
8
(b)
depth first spanning tree
Transparency No. 6-54
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
back edge
- nontree edge (u,v)
- either u is an ancestor of v or
v is an ancestor of u
- all nontree edges in depth first
spanning tree are back edges
 the root of a depth first spanning
tree is an articulation point iff it
has at least two children, and
any other vertex u is an articulation
point iff it has at least one child w
such that we can not reach an
ancestor of u using a path that
consists of only w, descendents of w,
and a single back edge
Transparency No. 6-55
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
low(u)
- the lowest dfn 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
dfn
low
0 1
4 3
4 3
2 3
2 0
0 0
4 5
1 5
0 5
6 7
6 7
5 7
8 9
9 8
9 8
dfn and low values for dfs spanning
tree with root = 3
Transparency No. 6-56
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
u is an articulation point iff
- u is the root of the spanning tree
and has two or more children
- u is not the root and u has child
w such that low(w)  dfn(u)
Transparency No. 6-57
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
#define MIN2(x,y) ((x) < (y) ? (x) : (y))
short int dfn[MAX_VERTICES];
short int low[MAX_VERTICES];
int num;
void init(void) {
int i;
for(i = 0; i < n; i++) {
visited[i] = FALSE;
dfn[i] = low[i] = -1;
}
num = 0;
}
initialization of dfn and low
Transparency No. 6-58
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
void dfnlow(int u, int v) {
node_ptr ptr;
int w;
dfn[u] = low[u] = num++;
for(ptr = graph[u]; ptr; ptr = ptr->link)
{
w = ptr->vertex;
if(dfn[w] < 0) {
dfnlow(w, u);
low[u] = MIN2(low[u],low[w]);
}
else if (w != v)
low[u] = MIN2(low[u], dfn[w]);
}
}
determining dfn and low
Transparency No. 6-59
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
void bicon(int u, int v) {
node_ptr ptr;
int w, x, y;
dfn[u] = low[u] = num++;
for(ptr = graph[u];ptr; ptr = ptr->link) {
w = ptr->vertex;
if(v != w && dfn[w] < dfn[u])
push(&top, u, w);
if(dfn[w] < 0) {
bicon(w, u);
low[u] = MIN2(low[u], low[w]);
if(low[w] >= dfn[u]) {
printf(“new biconnected
component: “);
(continue)
Transparency No. 6-60
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Elementary Graph Operations
do {
pop(&top, &x, &y);
printf(“ <%d,%d>“, x, y);
} while(!((x == u) && (y == w)));
printf(“\n”);
}
}
else if (w != v)
low[u] = MIN2(low[u], dfn[w]);
}
}
biconnected components of a graph
Transparency No. 6-61
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
cost of a spanning tree of a weighted
undirected graph
- sum of the costs(weights) of the
edges in the spanning tree
minimum cost spanning tree
- a spanning tree of least cost
- Kruskal’s, Prim’s, and Sollin’s
algorithms
- greedy method
Transparency No. 6-62
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
greedy method
- construct an optimal solution in
stages
- make the best decision at each stage
using some criterion
for spanning tree: least cost criterion
- use only edges within the graph
- use exactly n-1 edges
- may not use edges that would produce
a cycle
Transparency No. 6-63
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
(1) Kruskal’s algorithm
- select the edges for inclusion in T
in nondecreasing order of their
cost
- an edge is added to T if it does
not form a cycle with the edges
that are already in T
- exactly n-1 edges are selected
Transparency No. 6-64
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
T = {};
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;
if ((v,w) does not create a cycle in T)
add(v,w) to T;
else
discard (v,w);
}
if (T contains fewer than n-1 edges)
printf(“no spanning tree\n”);
Kruskal’s algorithm
Transparency No. 6-65
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
choosing a least cost edge (v,w)
from E
- a min heap
- determine and delete the next
least cost edge: O(log2e)
- construction of the heap: O(e)
checking that the new edge, (v,w),
does not form a cycle in T
- use the union-find operations
Transparency No. 6-66
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Minimum Cost Spanning Trees
0
0
28
10
0
1
1
14
16
6
2
5
5
6
10
2
1
5
6
2
24
25
18
4
12
22
3
(a)
4
4
3
(b)
3
(c)
stages in Kruskal’s algorithm
Transparency No. 6-67
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Minimum Cost Spanning Trees
0
0
10
1
0
10
1
10
1
14
5
6
2
4
12
3
(d)
5
6
2
4
12
5
14
16
6
2
4
12
3
(e)
3
(f)
stages in Kruskal’s algorithm
Transparency No. 6-68
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Minimum Cost Spanning Trees
0
0
10
1
10
14
16
6
2
5
4
12
22
3
(g)
1
14
16
6
2
5
25
4
12
22
3
(h)
stages in Kruskal’s algorithm
Transparency No. 6-69
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
edge
----(0,5)
(2,3)
(1,6)
(1,2)
(3,6)
(3,4)
(4,6)
(4,5)
(0,1)
weight
----10
12
14
16
18
22
24
25
28
result
initial
added to tree
added
added
added
discarded
added
discarded
added
not considered
figure
(b)
(c)
(d)
(e)
(f)
(g)
(h)
summary of the Kruskal’s algorithm
Transparency No. 6-70
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
(2) Prim’s algorithm
- begins with a tree, T, that contains
a single vertex
- add a least cost edge (u,v) to T
such that T  {(u,v)} is also a tree
and repeat this step until T
contains n-1 edges
 set of
of the
- form
- form
selected edges at each stage
algorithm
a tree in Prim’s alg.
a forest in Kruskal’s alg.
Transparency No. 6-71
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
T = {};
TV = {0}; /* start with vertex 0 and no edge*/
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”);
Prim’s algorithm
Transparency No. 6-72
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Minimum Cost Spanning Trees
0
0
10
1
5
6
10
2
3
(a)
1
5
25
4
0
6
10
2
5
25
4
3
1
6
2
4
22
(b)
3
(c)
stages in Prim’s algorithm
Transparency No. 6-73
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Minimum Cost Spanning Trees
0
0
10
1
0
10
1
10
1
16
5
25
6
2
4
12
22
3
(d)
5
25
6
2
4
12
22
3
14
16
6
2
5
25
4
12
22
(e)
3
(f)
stages in Prim’s algorithm
Transparency No. 6-74
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Minimum Cost Spanning Trees
(3) Sollin’s algorithm
select several edges for inclusion in
T at each stage
1)select one edge for each tree in
the forest during a stage
- two trees in the forest could
select the same edge
2)eliminate multiple copies of edges
3)repeat step 1) and 2)
and terminate when
- there is only one tree at the end
of a stage, or
- no edges remain for selection
Transparency No. 6-75
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Minimum Cost Spanning Trees
0
0
10
1
10
1
14
5
6
2
4
12
22
3
(a)
14
16
6
2
5
25
4
12
22
3
(b)
stages in Sollin’s algorithm
Transparency No. 6-76
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
length of a path
- the sum of the weights of the
edges on the path
single source all destinations
45
50
v0
20
10
v2
v1
15
10
v4
35
20
15
v3
(a)
30
3
v5
path
length
1) v0 v2
10
2) v0 v2 v3
25
3) v0 v2 v3 v1
45
4) v0 v4
45
(b)
graph and shortest paths from v0
Transparency No. 6-77
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
v0 : source vertex
S : set of vertices, including v0,
whose shortest paths have been
found
w : any vertex( S)
distance[w]
- the length of the shortest path
starting from v0, going through
vertices only in S, and ending
in w
 generate the paths in nondecreasing
order
Transparency No. 6-78
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
1) shortest path to vertex u
v0
w
u
S
w  S
- S is generated in nondecreasing
order of path length
2) choose u which has the minimum
distance, distance[u], among all
the vertices not in S
Transparency No. 6-79
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
3) select u and generate the shortest
path from v0 to u
- u becomes a member of S
 shortest path from v0 to w can be
changed by adding u to S
- going through vertices only in S
- w is not currently in S
distance[u] + length(<u,w>)
Transparency No. 6-80
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
S
w
distance[w]
cost[u][w]
v0
distance[u]
u
min{distance[w], distance[u]+cost[u][w]}
found
f
f
distance
0
50
f
f
f
f
10 1000 45 1000
Transparency No. 6-81
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
#define MAX_VERTICES 6
int cost[][MAX_VERTICES] =
{{
0,
50,
10, 1000,
45,
{1000,
0,
15, 1000,
10,
{ 20, 1000,
0,
15, 1000,
{1000,
20, 1000,
0,
35,
{1000, 1000,
30, 1000,
0,
{1000, 1000, 1000,
3, 1000,
int distance[MAX_VERTICES];
short int found[MAX_VERTICES];
int n = MAX_VERTICES;
1000},
1000},
1000},
1000},
1000},
0}};
declarations for the shortest path
algorithm
Transparency No. 6-82
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
void shortestpath(int v, int cost[][MAX_VERTICES],
int distance[], int n, short int found[]) {
int i, u, w;
for (i = 0; i < n; i++) {
found[i] = FALSE;
distance[i] = cost[v][i];
}
found[v] = TRUE;
distance[v] = 0;
for (i = 0; i < n-2; i++) {
u = choose(distance, n, found);
found[u] = TRUE;
for (w = 0; w < n; w++)
if (!found[w])
if (distance[u] + cost[u][w] < distance[w])
distance[w] = distance[u] + cost[u][w];
}
}
(to be continued)
Transparency No. 6-83
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
int choose(int distance[], int n, int found[]) {
/* find 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;
}
Transparency No. 6-84
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
Example)
find the shortest path from Boston
(vertex 4) to each of the other
cities on the graph
Boston
Chicago
San
Francisco
1
Denver
800
1200
4
1500
250
1000
3
2
1000
300
5
1400
0
Los Angeles
1700
7
New
York
900
1000
New Orleans
6 Miami
(a) digraph of hypothetical airline routes
Transparency No. 6-85
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
0
0
300
1000
0
1
2
3
4
5
6
7
1
0
800
2
0
1200
3
4
5
0
1500
1000
0
250
0
6
7
900
0
1400
1000
0
1700
(b) cost adjacency matrix
1500
S
3
4
250
5
choose 5
Transparency No. 6-86
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
3
1000
S
250
900
6
5
4
1400
7
choose 6
S
1000
4
250
900
5
7
6
1000
3
choose 3
Transparency No. 6-87
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
S
7
1000
250
900
6
5
4
1000
2
3
1200
choose 7
S
250
1000
3
1700
5
4
900
6
0
7
1000
1200
2
choose 2
...
Transparency No. 6-88
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
iteration
S
vertex
selected
initial
--
----
1
2
3
{4}
{4,5}
{4,5,6}
5
6
3
4
5
{4,5,6,3}
{4,5,6,3,7}
7
2
6
{4,5,6,3,7,2}
{4,5,6,3,7,2,1}
1
LA
[0]
+
+
+
+



distance
SF DEN
[1]
[2]
+
+
+
+
+
+
+ 
+ 
 
 
CHI
[3]
1500
1250
1250
1250
1250
1250
1250
BOST
[4]
0
0
0
0
0
0
0
NY
[5]
250
250
250
250
250
250
250
MIA
[6]
+






action of shortestpath
Transparency No. 6-89
NO
[7]
+






Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
all pairs shortest paths
find the shortest paths between all
pairs of vertices, vi, vj, i  j
cost adjacency matrix
- cost[i][j] = 0, where i = j
- cost[i][j] = , where
<i,j>  E(G), i  j
k
A [i][j]
- cost of the shortest path from i
to j
- using only those intermediate
vertices with an index  k
Transparency No. 6-90
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
n-1
A [i][j]
- cost of shortest path from i to j:
no vertex in G has an index
greater than n-1
-1
A [i][j] = cost[i][j]
- i to j paths allowed have no
intermediate vertices on them
basic idea
-1
1)begin with the matrix A
2)successively generate the matrices
0
1
2
n-1
A , A , A , ···, A
Transparency No. 6-91
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
k
generate A
k-1
- assume that A
is generated
- two cases
k
k-1
[i][j]
k
k-1
[i][k] + A
1) A [i][j] = A
2) A [i][j] = A
k-1
[k][j]
Transparency No. 6-92
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
i
···
···
···
j
in case rule 1)
vertices with index < k
i
···
k
···
j
in case rule 2)
vertices with index < k
k
vertices with index < k
k-1
A [i][j] = min { A [i][j],
k-1
k-1
A [i][k] + A [k][j] }, k  0
-1
A [i][j] = cost[i][j]
Transparency No. 6-93
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
Example)
-2
0
1
1
0 1 
-2 0 1
  0
2
1
(a) directed graph
(b) A
-1
graph with negative cycle
1
1
0
0
A [0][2]  min{A [0][2], A [0][1]+A [1][2]} = 2
1
A [0][2] = -
0,1,0,1,0,1,0,1,···,0,1,2
- a cycle 0,1,0 that has a negative
length(-1)
Transparency No. 6-94
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
int distance[MAX_VERTICES][MAX_VERTICES];
computations are done in place
k
k-1
- A [i][k] = A [i][k]
k
k-1
- A [k][j] = A [k][j]
Transparency No. 6-95
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Shortest Paths
void allcosts(int cost[][MAX_VERTICES],
int distance[][MAX_VERTICES], int n) {
int i, j, k;
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++)
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];
}
all pairs, shortest paths function
Transparency No. 6-96
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
Example)
6
V0
3
V1
4
2
11
0
1
2
0
0
6
3
1
4
0

2
11
2
0
V2
(a) digraph G
(b) cost adjacency matrix for G
directed graph and its cost matrix
Transparency No. 6-97
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Shortest Paths
A-1
0
1
2
0
0
6
3
1
4
0

A0
0
1
2
2
11
2
0
0
-1
0
0
6
3
1
4
0
7
-1
2
11
2
0
-1
A [2][1]= min{A [2][1],A [2][0]+A [0][1]}
= min{, 3+4} = 7
A1
0
1
2
1
0
0
6
3
1
4
0

A2
0
1
2
2
6
2
0
0
0
0
0
5
3
1
4
0
7
2
6
2
0
0
A [0][2]=min{A [0][2],A [0][1]+A [1][2]}
= min{11, 4+2} = 6
matrices A
k
produced by allcosts
Transparency No. 6-98
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Transitive Closure
transitive closure
Def)transitive closure matrix of a
directed graph G
+
- A
- if there is path of length > 0
from i to j
+
A [i][j] = 1
- otherwise
+
A [i][j] = 0
 can find A
+
by using allcosts
Transparency No. 6-99
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Transitive Closure
Def)reflexive transitive closure
matrix of a directed graph G
*
- A
- if there is path of length  0
from i to j
*
A [i][j] = 1
- otherwise
*
A [i][j] = 0
+
*
 A and A differ only on the diagonal
+
- A [i][i] = 1
iff a cycle of length > 1
*
- A [i][i] = 1 (always)
Transparency No. 6-100
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Transitive Closure
0
1
2
3
4
(a) digraph G
0
1
2
3
4
0
0
0
0
0
1
0
0
0
0
1
1
1
1
1
(c) A
1
1
1
1
1
+
0
1
2
3
4
0
0
0
0
0
1
0
0
0
0
0
1
0
0
1
0
0
1
0
0
0
0
0
1
0
(b) adjacency matrix A for G
1
1
1
1
1
0
1
2
3
4
1
0
0
0
0
1
1
0
0
0
1
1
1
1
1
(d) A
1
1
1
1
1
1
1
1
1
1
*
graph G and its adjacency matrix A,
+
*
A , A
Transparency No. 6-101
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOV)
activity on vertex(AOV) networks
Def) AOV network
- directed graph
- vertices represent tasks or activites
- edges represent precedence relations
between tasks
Transparency No. 6-102
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOV)
course number
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
C11
C12
C13
C14
C15
course name
Programming I
Discrete Mathematics
Data Structures
Calculus I
Calculus II
Linear Algebra
Analysis of Algorithms
Assembly Language
Operating Systems
Programming Languages
Compiler Design
Artificial Intelligence
Computational Theory
Parallel Algorithms
Numerical Analysis
prerequisites
None
None
C1, C2
None
C4
C5
C3, C6
C3
C7, C8
C7
C10
C7
C7
C13
C5
courses needed for a computer science degree at a hypothetical university
Transparency No. 6-103
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOV)
C9
C8
C1
C10
C3
C7
C2
C4
C11
C12
C5
C13
C6
C14
C15
AOV network representing courses as vertices and edges as prerequisites
Transparency No. 6-104
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOV)
Def) predecessor, i, of vertex j
- a directed path from vertex i to
vertex j exist
- j is a successor of i
Def) immediate predecessor, i, of j
- <i,j> is an edge in G
- j is an immediate successor of i
i
immediate predecessor
j
immediate successor
Transparency No. 6-105
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOV)
Def) a relation · is transitive
- i·j and j·k  i·k
Def) a relation · irreflexive on a
set S
- i·i is false for all elements,
i, in S
Def) partial order
- a precedence relation
- transitive and irreflexive
Transparency No. 6-106
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOV)
topological order
- a linear order of the vertices of
a graph
- for any two vertices, i, j, if i
is a precedessor of j in the
network then i precedes j in the
linear ordering
C1, C2, C4, C5, C3, C6, C8, C7, C10, C13,
C12, C14, C15, C11, C9
C4, C5, C2, C1, C6, C3, C8, C15, C7, C9, C10,
C11, C12, C13, C14
Transparency No. 6-107
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOV)
V1
V0
V1
V1
V2
V4
V2
V4
V3
V5
V3
V5
(a) initial
V1
V2
V4
V5
(b) V0
(c) V3
V1
V4
V4
V4
V5
(d) V2
(e) V5
(f) V1
(g) V4
(example of topological sort)
Transparency No. 6-108
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOV)
for (i = 0; i < n; i++) {
if (every vertex has a predecessor) {
fprintf(stderr,”network has a cycle.\n”);
exit(1);
}
pick a vertex v that has no predecessors;
output v;
delete v and all edges leading out of v from
the network;
}
topological sort
Transparency No. 6-109
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOV)
- operations that we wish to perform
1) determine if a vertex has any
predecessors
2) delete a vertex and all of its
incident edges
- declarations
typedef struct node *node_ptr;
typedef struct node {
int vertex;
node_ptr link;
};
typedef struct {
int count;
node_ptr link;
} hdnodes;
hdnodes graph[MAX_VERTICES];
Transparency No. 6-110
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOV)
V1
V0
top = -1
headnodes
count link
V0 0
V2
V4
V3
V5
vertex link
1
2
V1
1
4
V2
1
4
5
V3
1
5
4
V4
3
V5
2
3
(a) initial
Transparency No. 6-111
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOV)
void topsort(hdnodes graph[], int n) {
int i, j, k, top;
node_ptr ptr;
top = -1;
for (i = 0; i < n; i++)
if (!graph[i].count) {
graph[i].count = top;
top = i;
}
for (i = 0; i < n; i++)
if (top == -1) {
fprintf(stderr,”\nNetwork has a cycle.\n”);
exit(1);
}
else {
j = top; /* unstack a vertex */
top = graph[top].count;
print(“v%d, “, j);
for (ptr=graph[j].link; ptr; ptr=ptr->link) {
k = ptr->vertex;
graph[k].count--;
if (!graph[k].count) {
graph[k].count = top;
top = k;
}
}
}
}
Transparency No. 6-112
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOV)
Time complexity of topsort()
- O(n+e)
Transparency No. 6-113
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
AOE(activity on edge) networks
- directed edge represent tasks or
activities
- vertices represent events which
signal the completion of certain
activities
Transparency No. 6-114
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOE)
(AOE network example)
V1
a0=6
a3=1
a6=9
V6
a9=2
V4
V0
a1=4
a4=1
V8
a7=7
V7
V2
a2=5
a10=4
a8=4
V3
V5
a5=2
AOE network (activity graph of a hypothetical project)
Transparency No. 6-115
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
event
interpretation
v0
start of project
v1
completion of activity a0
v4
completion of activities a3 and a4
v7
completion of activities a7 and a8
v8
completion of project
(interpretation of some of the events in the above activity graph)
Transparency No. 6-116
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOE)
dummy activites
- time is zero
- additional constraints on the
activites
V1
a0=6
a3=1
a6=9
V6
a9=2
V4
V0
a1=4
a4=1
V8
a7=7
V7
V2
a11=0
a2=5
V3
a10=4
a8=4
V5
a5=2
Transparency No. 6-117
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
critical path
- minimum time required to complete
the project
- length of the longest path from
the start vertex to the finish
vertex
- v0, v1, v4, v7, v8
- v0, v1, v4, v6, v8
Transparency No. 6-118
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
earliest time an event, vi, can occur
- length of the longest path from
the start vertex v0 to vertex vi
- early(i) for activity ai
latest time of activity ai
- the least time the activity may
start without increasing the
project duration
- late(i) for activity ai
critical activity
- an activity for which
early(i) = late(i)
Transparency No. 6-119
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOE)
calculation of earliest times
obtain earliest[j] for event j
- the earliest event occurrence time
- initialize with zero
obtain latest[j] for event j
- the latest event occurrence time
k
ai
l
early(i) = earliest[k]
late(i) = latest[l] - duration of activity ai
Transparency No. 6-120
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
calculation of earliest[j] and
latest[j]
earliest[0] = 0
earliest[j] = max{earliest[i] + durations of <i,j>}
i  P(j)
(carry out this computation in topological order)
P(j)
- set of immediate predecessors of j
time complexity
- O(n+e)
Transparency No. 6-121
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
count link vertex dur link
V0 0
1 6
V1
1
4 1
V2
1
4 1
V3
1
5 2
V4
2
6 9
V5
1
4 4
V6
1
5 5
V7
2
6 6
V8
2
2 4
3 5
7 7
(adjacency lists)
Transparency No. 6-122
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOE)
V1
a0=6
a3=1
a6=9
V6
a9=2
V4
V0
a1=4
a4=1
V8
a7=7
V7
V2
a2=5
a10=4
a8=4
V3
V5
a5=2
AOE network (activity graph of a hypothetical project)
Transparency No. 6-123
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
earliest
initial
output V0
output V3
output V5
output V2
output V1
output V4
output V7
output V6
output V8
[0] [1] [2] [3] [4] [5] [6] [7] [8] stack
0
0
0
0
0
0
0
0
0 [0]
0
6
4
5
0
0
0
0
0 [3,2,1]
0
6
4
5
0
7
0
0
0 [5,2,1]
0
6
4
5
0
7
0 11 0 [2,1]
0
6
4
5
5
7
0 11 0 [1]
0
6
4
5
7
7
0 11 0 [4]
0
6
4
5
7
7 16 14 0 [7,6]
0
6
4
5
7
7 16 14 18 [6]
0
6
4
5
7
7 16 14 18 [8]
(computation of earliest)
computing earliest from topological
sort
Transparency No. 6-124
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
calculation of latest times
latest[n-1] = earliest[n-1]
latest[j] = min{latest[i] - duration of <j,i>}
i  S(j)
(carry out this computation in reverse topological order)
S(j)
- set of vertices adjacent from vertex j
- immediate successors of j
- use inverse adjacency list
Transparency No. 6-125
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
count link vertex dur link
V0 3
V1
1
0 6
V2
1
0 4
V3
1
0 5
V4
2
1 1
V5
1
3 2
V6
1
4 9
V7
1
4 7
5 4
V8
0
6 2
7 4
2 1
(inverse adjacency lists for AOE network)
Transparency No. 6-126
Copyright(c) 1997, Sungkyunkwan University
Data Structure in C
Activity Networks (AOE)
V1
a0=6
a3=1
a6=9
V6
a9=2
V4
V0
a1=4
a4=1
V8
a7=7
V7
V2
a2=5
a10=4
a8=4
V3
V5
a5=2
AOE network (activity graph of a hypothetical project)
Transparency No. 6-127
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
latest
initial
output V8
output V7
output V5
output V3
output V6
output V4
output V2
output V1
[0] [1] [2] [3] [4] [5] [6] [7] [8] stack
18 18 18 18 18 18 18 18 18 [8]
18 18 18 18 18 18 16 14 18 [7,6]
18 18 18 18 7 10 16 14 18 [5,6]
18 18 18 18 7 10 16 14 18 [3,6]
3 18 18 8
7 10 16 14 18 [6]
3 18 18 8
7 10 16 14 18 [4]
3
6
6
8
7 10 16 14 18 [2,1]
2
6
6
8
7 10 16 14 18 [1]
0
6
6
8
7 10 16 14 18 [0]
(computation of latest)
Transparency No. 6-128
Data Structure in C
Copyright(c) 1997, Sungkyunkwan University
Activity Networks (AOE)
latest[8]
latest[6]
latest[7]
latest[4]
latest[1]
latest[2]
latest[5]
latest[3]
latest[0]
=
=
=
=
=
=
=
=
=
earliest[8] =
min{latest[8]
min{latest[8]
min{latest[6]
latest[7]
min{latest[4]
min{latest[4]
min{latest[7]
min{latest[5]
min{latest[1]
latest[2]
latest[3]
18
- 2}
- 4}
- 9;
- 7}
- 1}
- 1}
- 4}
- 2}
- 6;
- 4;
- 5}
= 16
= 14
=
=
=
=
=
0
6
6
10
8
= 0
(computation of least using a reverse topological order)
Transparency No. 6-129