View
239
Download
1
Category
Preview:
Citation preview
7/27/2019 Data Structure - Adjacency
1/85
Data StructureChapter 6 Graphs
Ju inn -Dar Huang , Ph.D.
As sis tant Professo rjdhuang@mail.n c tu .edu .tw
November 2004
Rev . 2005, 2006
7/27/2019 Data Structure - Adjacency
2/85
G
raphs
Juinn-DarH
uang
jdhua
ng@mail.nctu.edu.tw
copyright 2004-2006
Knigsberg Bridge Problem
A
Kneiphof
ab
c d gC
D
Bf
e
a b
c dg
e
f
A
C
Q: Is it possible to cross each bridge exactly once and
return to the starting points?
A: Euler said no.B
D
The first recorded problem that was solved using graph in 1736
7/27/2019 Data Structure - Adjacency
3/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu.edu.tw
copyright 2004-2006
Graphs
Definition
a graph G consists of 2 sets, V and E
V is a finite, nonempty set ofvertices E is a set ofpairs of vertices; these pairs are called
edges
Representations a graph G(V, E)
V(G) represents the set of vertices of G
E(G) represents the set of edges of G
7/27/2019 Data Structure - Adjacency
4/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu.edu.tw
copyright 2004-2006
Directed or Undirected Graphs
In an undirected graph
the edge is undirected
(u, v) and (v, u) represent the same edge
In a directed graph
the edge is directed
and represent different edges
for an edge , u is the tail and v is the head of the
edge
7/27/2019 Data Structure - Adjacency
5/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu.edu.tw
copyright 2004-2006
Graph Examples
0
3
1 2
0
1
3 4
2
5 6
G1 G2 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) = {, ,
}
DirectedUndirectedUndirected
0
1
2
Tail
Head
7/27/2019 Data Structure - Adjacency
6/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu.edu.tw
copyright 2004-2006
Restrictions on Graphs
A graph cannot have an edge from a vertex v
back to itself
(v, v) and are both illegal these edges are named
self edges orself loops
A graph cannot have multiple occurrences of thesame edge
or it is a multigraph
These restrictions are not necessarily essential
0
2
1
1
2
30
7/27/2019 Data Structure - Adjacency
7/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu.edu.tw
copyright 2004-2006
Complete Graphs
An n-vertex undirected graph
has n*(n-1)/2 distinct edges at most
An n-vertex undirected graph with exactlyn(n-1)/2 edges is a complete graph
e.g., G1
An n-vertex directed graph
has n*(n-1) distinct edges at most An n-vertex directed graph with exactly n(n-1)
edges is a complete graph
e.g., G3 is not complete
0
3
1 2
G1
0
1
2G3
7/27/2019 Data Structure - Adjacency
8/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu.edu.tw
copyright 2004-2006
Adjacency and Incidence (1/2)
If (u, v) E(G)
u and v are adjacent
(u, v) is incident on u and v e.g., vertices adjacent to vertex 1 are vertex 0, 3, and 4
e.g., edges incident on vertex 2 are (0,2), (2,5), and (2,6)
0
1
3 4
2
5 6
G2
7/27/2019 Data Structure - Adjacency
9/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu
.edu.tw
copyright 2004-2006
Adjacency and Incidence (2/2)
If E(G)
u is adjacent to v (directed, i.e., v is NOT adjacent to u)
v is adjacent from u is incident to u and v
e.g., edges incident to vertex 1 are
, , and 0
1
2
G3
7/27/2019 Data Structure - Adjacency
10/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu
.edu
.tw
copyright 2004-2006
Subgraphs
A subgraph of G is a graph G such that V(G) V(G) and E(G) E(G)
0 0
3
1 2
0
1 2
(i) (ii) (iii)
0
3
1 2
G1
0
1
2G3
0 0
1
2
0
1
2
7/27/2019 Data Structure - Adjacency
11/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu
.edu
.tw
1copyright 2004-2006
Paths
A path from u to v in G
is a sequence of vertices u, i1, i2, , ik, v such that (u, i1)
(i1
, i2
), , (ik
, v) are edges in E(G) (undirected graph)
is a sequence of vertices u, i1, i2, , ik, v such that
, , are edges in E(G) (directed graph)
Length of a path number of the edges on it
Simple path
is a path in which all vertices except possibly for the firstand last are distinct
A cycle is a simple path in which the first and the
last is the same vertex
7/27/2019 Data Structure - Adjacency
12/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu
.edu
.tw
1copyright 2004-2006
Path Examples
A path 0, 1, 3, 2 in G1 also a simple path; of length 3
A path 0, 1, 3, 2, 0 in G1
also a cycle; of length 4
A path 0, 1, 3, 1 in G1
not a simple path; of length 3 A path 0, 1, 2 in G3
also a simple path; of length 2
A path 0, 1, 0 in G3 also a cycle; of length 2
0, 1, 2, 1, is NOT a path in G3 E(G3)
0
1
2
G3
0
3
1 2
G1
7/27/2019 Data Structure - Adjacency
13/85
G
raphs
Juinn-DarHuang
jdhua
ng@mail.nctu
.edu
.tw
1copyright 2004-2006
Connected Graphs
In an undirected graph G, 2 vertices u and v are
said to be connected iff
there is a path from u to v (or a path from v to u) An undirected graph G is said to be connected iff
u, v V(G), u v there is a path from u to v
0
1
3 4
2
5 6
G2
Connected
0
3
1 2
4
7
5 6
G4
H1 H2
Unconnected
7/27/2019 Data Structure - Adjacency
14/85
G
raphs
Juinn-DarHuang
jdhuang@mail.nctu
.edu
.tw
1copyright 2004-2006
Connected Components
A connected component, H, of an undirected
graph G is a maximal connected subgraphs
Maximal: G contains no other subgraph that is bothconnected and fully contains H
0
3
1 2
4
7
5 6
G4
H1 H2
H1 and H2 are 2 connected components
7/27/2019 Data Structure - Adjacency
15/85
G
raphs
Juinn-DarHuang
jdhuang@mail.nctu
.edu
.tw
1copyright 2004-2006
Strongly Connected Graphs
A directed graph G is said to be strongly
connected iff
u, v V(G), u v there is a path from u to v and apath from v to u (bi-directional)
0
1
2
G3
not strongly connected
7/27/2019 Data Structure - Adjacency
16/85
G
raphs
Juinn-DarHuang
jdhuang@mail.nctu
.edu
.tw
1copyright 2004-2006
Strongly Connected Components
A strongly connected component of a directed
graph is a maximal strongly connected subgraphs
0
1
2
G32 strongly connected components
0
1 2
7/27/2019 Data Structure - Adjacency
17/85
G
raphs
Juinn-DarHuang
jdhuang@mail.nctu
.edu
.tw
1copyright 2004-2006
Degree
Degree of a vertex
the number of edges incident to the vertex
For a directed graph (digraph) in-degree of v: the num of edges for which v is the head
out-degree of v: the num of edges for which v is the tail
0
1
3 4
2
5 6
G2
0
1
2
G3
degree: 3
in-degree: 1
out-degree:2
degree: 3
7/27/2019 Data Structure - Adjacency
18/85
G
raphs
Juinn-DarHuang
jdhuang@mail.nctu
.edu
.tw
1copyright 2004-2006
ADT Graph
class Graph {
// objects: a nonempty set of vertices and a set of edges
// where each edge is a pair of vertices
public:Graph(); // ctor
void InsertVertex(Vertex v);
void InsertEdge(Vertex u, Vertex v);
void DeleteVertex(Vertex v);void DeleteEdge(Vertex u, Vertex v);
bool IsEmpty();
List Adjancent(Vertex v);
};
7/27/2019 Data Structure - Adjacency
19/85
G
raphs
Juinn-DarHuang
jdhuang@mail.nctu
.edu
.tw
1copyright 2004-2006
Graph Representations
3 Common representations
Adjacency matrix
Adjacency list
Adjacency multilist
7/27/2019 Data Structure - Adjacency
20/85
G
raphs
Juinn-DarHuang
jdhuang@mail.nctu
.edu
.tw
1copyright 2004-2006
Adjacency Matrix (1/2)
Let G = (V, E) with n vertices, n1
The adjacency matrix A of G is an nxn martix in which
A[i][j] = 1 iff (u,v) (or ) E(G)
000
101
010
2
1
0
210
0
1
2
no self loops
0010
0011
1101
0110
3
2
1
0
3210
0
3
1 2
no self loops
symmetric
non-symmetric
7/27/2019 Data Structure - Adjacency
21/85
G
raphs
Juinn-DarHuang
jdhuang@mail.nctu
.edu
.tw
2copyright 2004-2006
Adjacency Matrix (2/2)
Need n2 bits if |V(G)| = n
The adjacency matrix is symmetric for an undirected graph
store only the upper or lower triangle of the matrix
Find degrees
degree of v (undirected graph): either row sum or column sum
in-degree of v (digraph): column sum
out-degree of v (digraph): row sum
How many edges in G? Is G connected?
time complexity is O(n2) at least
When the adjacency matrix is sparse
sparse matrix techniques in Chapter 2
time complexity can be reduced to o(n+e) where e
7/27/2019 Data Structure - Adjacency
22/85
G
raphs
Juinn-DarHu
ang
jdhuang@mail.nctu
.edu
.tw
2copyright 2004-2006
Adjacency List
0
1
2
G3
0
3
1 2
G1
2e nodes
3
2
1
0
1
3
3
1
2 0
0 0
0 0
2 0
[0]
[1]
[2]
[3]
HeadNodes
Number of edges can be determined in O(n+e) time
Easily to find the degree of a node
0
1 0
2 0 0
[0]
[1][2]
HeadNodese nodes
Easily to find the out-degree of a node
List Representation
7/27/2019 Data Structure - Adjacency
23/85
G
raphs
Juinn-DarHu
ang
jdhuang@mail.nctu
.edu
.tw
2copyright 2004-2006
Sequential Representation
9 11 13 15 17 18 20 22 23 2 1 3 0 0 3 1 2 5 6 4 5 7 6
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 220 1
0
3
1 2
4
7
5 6
G4
H1 H2
n+1 entries 2e entries
Advantage small memory usageDisadvantage dynamic edge insertion/deletion
7/27/2019 Data Structure - Adjacency
24/85
G
raphs
Juinn-DarHu
ang
jdhuang@mail.nctu
.edu
.tw
2copyright 2004-2006
Inverse Adjacency List
0
1 0
2 0 0
[0]
[1]
[2]
HeadNodes e nodes
Easily to find the out-degree of a node
0
1
2
G3
Q: How to find the in-degree of a node ?
1 0
0 0
1 0
[0]
[1]
[2]
HeadNodese nodes
Inverse adjacency list
How about combining the adjacency list and the inverse adjacency list?
7/27/2019 Data Structure - Adjacency
25/85
G
raphs
Juinn-DarHu
ang
jdhuang@mail.nctu
.edu
.tw
2copyright 2004-2006
Orthogonal Adjacency List
0
1
2
G3
Tail Head Col link for head row link for tail
( Recall sparse matrix in Chapter 2 )
0
1
2 0
1 0 0
0
0 1 0 0
1 2
1 2 0 0
head nodes(shown twice)
e nodes
Adj M l ili
7/27/2019 Data Structure - Adjacency
26/85
Graphs
Juinn-DarHu
ang
jdhuang@mail.nctu
.edu
.tw
2copyright 2004-2006
Adjacency Multilist
[0]
[1]
[2][3]
0 1 N1 N3
0 2 N2 N3
0 3 0 N4
1 2 N4 N5
1 3 0 N5
2 3 0 0
N0
N1
N2
N3
N4
N5
HeadNodes
edge (0, 1)
edge (0, 2
edge (0, 3)
edge (1, 2)
edge (1, 3)
edge (2, 3)
The lists are
Vertex 0: N0 -> N1 -> N2
Vertex 1: N0 -> N3 -> N4
Vertex 2: N1 -> N3 -> N5
Vertex 3: N2 -> N4 -> N50
3
1 2
G1vertex1 vertex2 list1 list2
W i ht d Ed
7/27/2019 Data Structure - Adjacency
27/85
Graphs
Juinn-DarHu
ang
jdhuang@mail.nctu
.edu
.tw
2copyright 2004-2006
Weighted Edges
If there is a weight on an edge
represents distance or cost, or
matrix: A[i][j] might be used to keep the non-zero weight list: an additional field for the weight is required
A graph with weighted edges is called a network
0
5
1
6
43
2
10
28
14
1218
22
2524
T l G h
7/27/2019 Data Structure - Adjacency
28/85
Graphs
Juinn-DarHu
ang
jdhuang@mail.nctu.edu.tw
2copyright 2004-2006
Traversals on Graphs
Graph is not like tree
there is no root
there may exist multiple paths from u to v
Given a graph and a vertex v
how to visit all vertices that are reachable from v?
i.e., visit all vertices connected to v
Traversals on graphs
depth-first search (DFS)
breadth-first search (BFS)
D th Fi t S h (DFS)
7/27/2019 Data Structure - Adjacency
29/85
Graphs
Juinn-DarHu
ang
jdhuang@mail.nctu.edu.tw
2copyright 2004-2006
Depth-First Search (DFS)
void Graph::DFS() {visited = new bool[n]; // n vertices from 0 to n-1
for(int i = 0; i < n; ++i)
visited[i] = false;
DFS(0); // start search from Vertex 0delete [] visited;
}
void Graph::DFS(int v) {visited[v] = true;
// pseudo code
for( each vertex w adjacent to v)
if(! visited[w])DFS(w); // recursive call
}
3 4
1 2
5 6
0
7
Traversal Order:
0, 1, 3, 7, 4, 5, 2, 6
If G is in adjacency list O(e)If G is in adjacency matrix O(n2)
Stack-Based
B dth Fi t S h (BFS)
7/27/2019 Data Structure - Adjacency
30/85
Graphs
Juinn-DarHu
ang
jdhuang@mail.nctu.edu.tw
2copyright 2004-2006
Breadth-First Search (BFS)
void Graph::BFS(int v) { // starting from Vertex vvisited = new bool[n]; // n vertices from 0 to n-1for(int i = 0; i < n; ++i)visited[i] = false;
Queue q;visited[v] = true;q.Insert(v);
while(! q.IsEmpty()) {v = *q.Delete(v);
for( each vertex w adjacent to v)if(! visited[w]) {visited[w] = true;q.Insert(w);
}}delete [] visited;
}
3 4
1 2
5 6
0
7
Traversal Order:
0, 1, 2, 3, 4, 5, 6, 7
If G is in adjacency list O(e)If G is in adjacency matrix O(n2)
Queue-Based
Spanning Trees (1/3)
7/27/2019 Data Structure - Adjacency
31/85
Graphs
Juinn-DarHu
ang
jdhuang@mail.nctu.edu.tw
3copyright 2004-2006
Spanning Trees (1/3)
A spanning tree of a graph G is a tree that
includes all vertices of G
includes edges of G
spanning
trees
A spanning tree consists ofn vertices and n-1 edges
Spanning Trees (2/3)
7/27/2019 Data Structure - Adjacency
32/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Spanning Trees (2/3)
If a graph G is connected a DFS or BFS starting at any vertex visits all vertices
E(G) are partitioned into two sets, T and N
T (tree edges): edges traversed during the search
N(non-tree edges): remaining edges
V(G) + T spanning tree
A spanning tree resulting from DFS/BFS
depth-first/breadth-first spanning tree
If a non-tree edge is inserted into a spanning tree
a cycle is formed
Spanning Trees (3/3)
7/27/2019 Data Structure - Adjacency
33/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Spanning Trees (3/3)
3 4
1 2
5 6
0
7
3 4
1 2
5 6
0
7
3 4
1 2
5 6
0
7
DFS (0) spanning tree BFS (0) spanning tree
A graph can have a bunch
of spanning trees
Articulation Point
7/27/2019 Data Structure - Adjacency
34/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Articulation Point
A vertex v of a connected graph G is an articulation
point iff the deletion of v and all edges incident to v
will leave behind a graph that has at least 2connected components
0
1
4
2 3
8
7
6
5
9
Vertices 1, 3, 5, and 7 are articulation points
Biconnected Graph
7/27/2019 Data Structure - Adjacency
35/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Biconnected Graph
A biconnected graph is a connected graph that
has no articulation points
0
1
4
2 3
8
7
6
5
9
Not biconnectedBiconnected
3 4
1 2
5 6
0
7
Biconnected graph is desirable while representing a communication network
Why?
Biconnected Components
7/27/2019 Data Structure - Adjacency
36/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Biconnected Components
A biconnected component of a connected graph Gis a maximal biconnected subgraph H of G
0
1
4
2 3
8
7
6
5
9
1
4
2 3
7
6
5
0
1
3 5
8
7 7
9
A connected graph Its 6 biconnected components
A biconnected graph has just one biconnected component the whole graph
Depth-First Number
7/27/2019 Data Structure - Adjacency
37/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Depth-First Number
Depth-first number: the visit order during the
depth-first search
0
1
4
2 3
8
7
6
5
9
1
2
3
4
5
6
7
8
109
3
4
2
1
0
6
7
8
5
9
1
2
3
4
5
6
7
8
9 10
non-tree
edge
starting
vertex
depth-first number, dfn
Back Edge and Cross Edge
7/27/2019 Data Structure - Adjacency
38/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Back Edge and Cross Edge
No cross edges in a depth-first spanning tree
No back edges in a breadth-first spanning tree
3
4
2
1
0
6
7
8
5
9
4
6
10
1
2
3
5
7
8
9
back edge
3 4
1 2
5 6
0
7
cross edge
Find Articulation Points by DFS (1/2)
7/27/2019 Data Structure - Adjacency
39/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Find Articulation Points by DFS (1/2)
Articulation point
the root of the depth-first spanning tree has at least 2 children
any other vertex u that has at least one child w such that its
impossible to reach ancestor of u using a path composed solely ofw, descendants of w and a back edge
3
4
2
1
0
6
7
8
5
9
1
4
6
10
2
3
5
7
8
9
back edge
Find Articulation Points by DFS (2/2)
7/27/2019 Data Structure - Adjacency
40/85
Graphs
Juinn-DarHu
ang
jdhuan
g@mail.nctu.edu.tw
3copyright 2004-2006
Find Articulation Points by DFS (2/2)
Articulation point
the root of the depth-first spanning tree has at least 2 children
a vertex u had a child w such that low(w) dfn(u)
3
4
2
1
0
6
7
8
5
9
1
4
6
10
2
3
5
7
8
9
back edge
low(w) = min {
dfn(w),
min{ low(x) | x is a child of w },
min{ dfn(x) | (w, x) is a back edge } }
10966611115low
1087621345dfn
9876543210vertex
9
66611115low
87621345dfn
9876543210vertex
Find dfn & low Values
7/27/2019 Data Structure - Adjacency
41/85
Graphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
Find dfn & low Values
void Graph::DfnLow(int x) { // apply DFS at Vertex xnum = 0; // num is an int data member of class Graph
dfn = new int[n]; // dfn is also a data member
low = new int[n]; // low is also a data member
for(int i = 0; i < n; ++i) dfn[i] = low[i] = 0;DfnLow(x, -1);
delete[] dfn; delete[] low;
}
void Graph::DfnLow(int u, int v) {dfn[u] = low[u] = ++num;
for( each vertex w adjacent from u )
if(dfn[w] == 0) { w is an unvisited vertex
DfnLow(w, u); // recursivelow[u] = min(low[u], low[w]);
} else if (w != v) // (w, v) is a back edge
low[u] = min(low[u], dfn[w]);
}
Minimum-Cost Spanning Trees
7/27/2019 Data Structure - Adjacency
42/85
Graphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
Minimum Cost Spanning Trees
A weighted, undirected graph may have lots of
valid spanning trees
the cost of a spanning tree is the sum of the costs(weights) of the edges in the spanning tree
A minimum-cost spanning tree is a spanning tree
ofleast cost e.g., road construction
3 algorithms here
Kruskals, Prims, Sollins
all greedy methods
0
5
1
6
4
3
2
10
28
14 16
1218
22
2524
Kruskals Algorithm (1/3)
7/27/2019 Data Structure - Adjacency
43/85
Gr
aphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
Kruskal s Algorithm (1/3)
Build a minimum-cost spanning tree T Sort edges in non-decreasing order of their costs
Select an edge in that order for inclusion in T ifthe resultant T does not contain a cycle
only n -1 edges are included at last
0
5
1
6
4
3
2
10
28
14 16
1218
22
2524
16
0
5
1
6
4
3
2
10
28
14
1218
22
2524
16
0
5
1
6
4
3
2
10
28
14
1218
22
2524
Kruskals Algorithm (2/3)
7/27/2019 Data Structure - Adjacency
44/85
Gr
aphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
Kruskal s Algorithm (2/3)
0
5
1
6
43
2
10
28
14
1218
22
2524
16
0
5
1
6
43
2
10
28
14
1218
22
2524
16
0
5
1
6
43
2
10
28
14
1218
22
2524
16
0
5
1
6
43
2
10
28
14
1218
22
2524
16
Kruskals Algorithm (3/3)
7/27/2019 Data Structure - Adjacency
45/85
Gr
aphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
g ( )
// E is the set of all edges in G
T = ;while((T contains less than n 1 edges) && (E is not empty))
{
choose an edge (v, w) from E of lowest cost;
delete (v, w) from E;
if((v, w) does not create a cycle in T)
add (v, w) to T;else
discard (v, w);
}
if( T contains few than n 1 edges )
cerr
7/27/2019 Data Structure - Adjacency
46/85
Gr
aphs
Juinn-DarHuang
jdhuan
g@mail.nctu.
edu.tw
4copyright 2004-2006
g ( )
Begin with a tree T containing one vertex any one in the original graph
add a minimum-cost edge (u, v) to T such thatT {(u, v)} is still a tree
at first, (u, v) should be an edge such that
u T and v T0
5
1
6
43
2
10
28
1416
1218
22
2524
16
0
5
1
6
43
2
10
28
14
1218
22
2524
16
0
5
1
6
43
2
10
28
14
1218
22
2524
Prims Algorithm (2/3)
7/27/2019 Data Structure - Adjacency
47/85
Gr
aphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
g ( )
0
5
1
6
43
2
10
28
14
1218
22
2524
16
16
0
5
1
6
43
2
10
28
14
1218
22
2524
16
0
5
1
6
43
2
10
28
14
1218
22
2524
16
0
5
1
6
43
2
10
28
14
1218
22
2524
Prims Algorithm (3/3)
7/27/2019 Data Structure - Adjacency
48/85
Gr
aphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
g ( )
// assume that G has at least one vertex// TV: vertex set of the spanning tree
// T: edge set of the spanning tree
TV = {0}; // start with Vertex 0 and no edges
for( T = ; T contains fewer than n-1 edges; ) {let (u, v) be a least-cost edge s.t. u TV and V TV;if( there is no such edge) break; // G is not connectedadd v to TV;
add (u, v) to T;
}
if( T contains few than n 1 edges )
cerr
7/27/2019 Data Structure - Adjacency
49/85
Gr
aphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
g
At each stage, each tree selects a minimum-costedge to connect another tree
Repeat the process until the minimum-cost
spanning tree is obtained
16
0
5
1
6
43
2
10
28
14
1218
22
25
24
(0, 5), (1, 6), (2, 3), (3, 2)
(4, 3), (5, 0), (6, 1) are selectedat the 1st stage
0
5
1
6
43
2
10
28
14
1218
22
25
24
16
(5, 4), (1, 2), (2, 1)
are selected at the 2nd stage
Shortest Path
7/27/2019 Data Structure - Adjacency
50/85
Gr
aphs
Juinn-DarHuang
jdhuan
g@mail.nctu.edu.tw
4copyright 2004-2006
Give a digraph G and two vertices A and B in G
is there a path from A to B?
if there is more than one path from A to B, which is the shortest
one?
The length/cost/weight of a path is defined as the sum of
the lengths/costs/weights of the edges on that path
For a path starting vertex: source
last vertex: destination 0
3 4
1 2
5
10 20
50
15
20
10
35
30
315
45
Single Source/All Destinations (1/4)
7/27/2019 Data Structure - Adjacency
51/85
Gr
aphs
Juinn-DarHuang
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
Non-negative edge weights
Path Length
1) 0, 3
2) 0, 3, 4
3) 0, 3, 4, 1
4) 0, 2
10
25
45
45
0
3 4
1 2
5
10 20
50
15
20
10
35
30
315
45
Starting Vertex: 0
Increasing
Single Source/All Destinations (2/4)
7/27/2019 Data Structure - Adjacency
52/85
Gr
aphs
Juinn-DarHuang
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
Let S denotes the set of vertices to which theshortest paths have already been found
(1) if the next shortest path is to vertex u, then the path
begins at v, ends at u, and goes through only vertices
that are in S
(2) the destination of the next path generated must be
the vertex u that has the minimum distance among all
vertices not in S
(3) The vertex u selected in (2) becomes a member of S
The algorithm is first given by Edsger Dijkstra
Therefore, its sometimes called Dijkstra Algorithm
Single Source/All Destinations (3/4)
7/27/2019 Data Structure - Adjacency
53/85
Gr
aphs
Juinn-DarHuang
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
Data Structure
// nmax: maximum number of vertices
class Graph {int length[nmax][nmax]; // length-adjacency matrix
int dist[nmax]; // min-distance
bool s[nmax];
int choose(int); // used in ShortestPath
public:
void ShortestPath(int, int);
};
// length[i][j] is set to a large number
// if is not an edge
Single Source/All Destinations (4/4)
7/27/2019 Data Structure - Adjacency
54/85
Gr
aphs
Juinn-DarHuang
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
void Graph::ShortestPath(int n, int v) {// n vertices, source vertex = v
for(int i = 0; i < n; ++i) { // initialize
s[i] = false; dist[i] = length[v][i];
}
s[v] = true; dist[v] = 0;
for(int j = 0; j < n-2; ++j) { // determine n-1 paths
int u = choose(n);// choose u s.t. dist[u] is minimum where s[u] = false
s[u] = true;
for(int w = 0; w < n; ++w) {
// update dist[w] where s[w] is false
if( (! s[w]) && (dist[u] + length[u][w] < dist[w]) )
dist[w] = dist[u] + length[u][w]; // shorter path found
}
}
Example (1/2)
7/27/2019 Data Structure - Adjacency
55/85
Gr
aphs
Juinn-DarHuang
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
0
1 2
34
5
67
300
800
17001000
1000
1400
1200
1500
1000250
900
San
Francisco
Denver
New
Orleans
New York
Chicago
Los
Angeles
Miami
Boston
001700
100000
1400900010000
250015000
01200
08001000
0300
0
7
6
5
4
3
2
1
0
76543210
Length-adjacencymatrix
Example (2/2)
7/27/2019 Data Structure - Adjacency
56/85
Gr
aphs
Juinn-DarHuang
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
Distance
LA SF DEN CHI BOST NY MIA NO
[0] [1] [2] [3] [4] [5] [6] [7]
Initial -- --- + + + 1500 0 250 + +
1 {4} 5 + + + 1250 0 250 1150 1650
2 {4,5} 6 + + + 1250 0 250 1150 1650
3 {4,5,6} 3 + + 2450 1250 0 250 1150 1650
4 {4,5,6,3} 7 3350 + 2450 1250 0 250 1150 1650
5 {4,5,6,3,7} 2 3350 3250 2450 1250 0 250 1150 1650
6 {4,5,6,3,7,2} 1 3350 3250 2450 1250 0 250 1150 1650
{4,5,6,3,7,2
,1}
iteration S Vertexselected
Single Source/All Destinations (1/4)
7/27/2019 Data Structure - Adjacency
57/85
Gr
aphs
Juinn-DarHuang
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
General Weights (Negative weights are allowed)
0 1 2
5
7 -5
Directed graph with a negative-length edge
0 1 2
-2
1 1
Directed graph with a cycle of negative length
no cycle of negative length is allowed
Single Source/All Destinations (2/4)
7/27/2019 Data Structure - Adjacency
58/85
Gr
aphs
Juinn-DarHuang
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
When there is no cycle of negative length, ashortest path between any 2 vertices of an n-
vertex graph has at most n-1 edges
Let distk[u] be the length of a shortest path from
the source vertex v to vertex u
under the constraint that the path contains at most kedges
distk[u] = min { distk-1[u],
min{ distk-1[i] + length[i][u]} }i
Bellman and Ford Algorithm
Single Source/All Destinations (3/4)
7/27/2019 Data Structure - Adjacency
59/85
Gr
aphs
Juinn-DarHua
ng
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
distk[7]k
0 1 2 3 4 5 6
1 0 6 5 5
2 0 3 3 5 5 4
3 0 1 3 5 2 4 7
4 0 1 3 5 0 4 5
5 0 1 3 5 0 4 3
6 0 1 3 5 0 4 3
distk
0
1
2
3
4
5
6
6
5
5
-2
-2
-1
-1
1
3
3
from length-adjacency matrix
Single Source/All Destinations (4/4)
7/27/2019 Data Structure - Adjacency
60/85
Gr
aphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.edu.tw
5copyright 2004-2006
void Graph::BellmanFord(int n, int v) {
// n vertices, source vertex = v
for(int i = 0; i < n; ++i)
dist[i] = length[v][i]; // initialize, dist1[u]
for(int k = 2; k (dist[i] + length[i][u]) )
dist[u] = dist[i] + length[i][u];
}
All-Pairs Shortest Paths (1/3)
7/27/2019 Data Structure - Adjacency
61/85
Gr
aphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.edu.tw
6copyright 2004-2006
The problem can be solved as n independentsingle-source/all-destinations problems
each iteration, a different vertex as the source vertex
Better idea?
Define Ak[i][j] to be the length of the shortest path
from i to j going through no vertex of index > k An-1[i][j] will be the length of the shortest i-to-j path in an
n-vertex graph
Ak[i][j] = min{ Ak-1[i][j], Ak-1[i][k] + Ak-1[k][j] }, k 0,
where A-1[i][j] = length[i][j]
All-Pairs Shortest Paths (2/3)
7/27/2019 Data Structure - Adjacency
62/85
Gr
aphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.edu.tw
6copyright 2004-2006
void Graph::AllLengths(int n) {// length[n][n] is the length-adjacency matrix
// a[i][j] is the length of the shortest path between i and j
for( int i = 0; i < n; ++i)
for ( int j = 0; j < n; ++i)
a[i][j] = length[i][j]; // constructA-1
for(int k = 0; k < n; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if( a[i][j] > (a[i][k] + a[k][j]) )a[i][j] = a[i][k] + a[k][j];
}
All-Pairs Shortest Paths (3/3)
7/27/2019 Data Structure - Adjacency
63/85
Graphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.edu.tw
6copyright 2004-2006
A-1 0 1 2
0 0 4 11
1 6 0 22 3 0
A0 0 1 2
0 0 4 11
1 6 0 22 3 7 0
A1 0 1 2
0 0 4 6
1 6 0 2
2 3 7 0
2
0 1
6
2
4
11
3
A2 0 1 2
0 0 4 6
1 5 0 2
2 3 7 0
A-1 A0
A1 A2
Transitive Closure (1/3)
7/27/2019 Data Structure - Adjacency
64/85
Graphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.edu.tw
6copyright 2004-2006
Given a digraph G with unweighted edges,determine if there is a path from vertex i to vertex j
positive length: transitive closure
non-negative length: reflexive transitive closure
Transitive closure matrix A+
A+[i][j] = 1 iff there is a path oflength > 0 from i to j
Transitive closure matrix A*
A*[i][j] = 1 iff there is a path oflength 0 from i to j
Transitive Closure (2/3)
7/27/2019 Data Structure - Adjacency
65/85
Graphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.e
du.tw
6copyright 2004-2006
To find A+ similar to the all-pairs shortest paths problem
first, let length matrix be the adjacency matrix, then
void Graph::TransitiveClosure(int n) {// length[n][n] is the adjacency matrixfor( int i = 0; i < n; ++i)
for ( int j = 0; j < n; ++i)a[i][j] = length[i][j];
for(int k = 0; k < n; ++k)for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)a[i][j] = a[i][j] || (a[i][k] && a[k][j]);
}
Transitive Closure (3/3)
7/27/2019 Data Structure - Adjacency
66/85
Graphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.e
du.tw
6copyright 2004-2006
11100
11100
11100
11100
11110
4
3
2
1
0
43210
00000
10000
01000
00100
00010
4
3
2
1
0
43210
0 1 2 3 4
Digraph G
11100
11100
11100
11110
11111
4
3
2
1
0
43210
Adjacency matrix
A+
A*
Activity-on-Vertex (AOV) Networks (1/3)
7/27/2019 Data Structure - Adjacency
67/85
Graphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.e
du.tw
6copyright 2004-2006
Course number Course name Prerequisites
C1 Programming I None
C2 Discrete Mathematics None
C3 Data Structures C1, C2
C4 Calculus I None
C5 Calculus II C4
C6 Linear Algebra C5
C7 Analysis of Algorithms C3, C6C8 Assembly Language C3
C9 Operating Systems C7, C8
C10 Programming Languages C7
C11 Compiler Design C10
C12 Artificial Intelligence C7
C13 Computational Theory C7
C14 Parallel Algorithms C13
C15 Numerical Analysis C5
Activity-on-Vertex (AOV) Networks (2/3)
7/27/2019 Data Structure - Adjacency
68/85
Graphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.e
du.tw
6copyright 2004-2006
C1
C2
C3
C5C4 C6 C15
C7
C8
C13
C12
C10
C9
C14
C11
Activity-on-Vertex (AOV) Networks (3/3)
7/27/2019 Data Structure - Adjacency
69/85
Graphs
J
uinn-DarHua
ng
jdhuang@mail.nctu.e
du.tw
6copyright 2004-2006
A digraph G in which the vertices represent activities andthe edges represent precedence relations between
activities is an activity-on-vertex (AOV) network
In an AOV network
vertex i is a predecessorof vertex j iff there is a path from i to j
i is an immediate predecessorof j iff is an edge j is a successorof i if i is a predecessor of j
j is an immediate successorof i if i is an immediate predecessor
of j
Partial Order
7/27/2019 Data Structure - Adjacency
70/85
Graphs
J
uinn-DarHua
ng
jdhuang
@mail.nctu.e
du.tw
6copyright 2004-2006
A relation is transitive if a b and b c a c e.g., for integer a, b, c; a > b and b > c a > c
A relation
is irreflexive on a set Sif a a for no a S
e.g., you cannot find any integer a that a > a
A relation is a partial orderif it is transitive andirreflexive
Precedence Relation
7/27/2019 Data Structure - Adjacency
71/85
Graphs
J
uinn-DarHua
ng
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
Is precedence relation a partial order? Precedence relation is transitive
a is a predecessor of b and b is a predecessor of c a
is a predecessor of c
Is precedence relation irreflexive?
it depends on whether the given network contains anydirected cycles
A precedence relation on an network is irreflexive if
the network is a directed acyclic graph (DAG) i.e., if it is a DAG, the precedence relation is a partial
order on it
topological order
Topological Order
7/27/2019 Data Structure - Adjacency
72/85
Graphs
J
uinn-DarHua
ng
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
A topological order is a linear ordering of the vertices of aDAG such that, for any 2 vertices i and j, if i is a
predecessor of j, then i precedes j in the linear ordering
There may be several feasible topological orders e.g.,
C1, C2, C4, C5, C3, C6 ,
C4, C5, C2, C1, C6, C3,
C1
C2
C3
C5C4 C6 C15
C7
C8
C13
C12
C10
C9
C14
C11
Topological Sorting (1/3)
7/27/2019 Data Structure - Adjacency
73/85
Graphs
J
uinn-DarHua
ng
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
// pseudo code here
Input the AOV network. Let n be the number of vertices.
for(int i = 0; i < n; ++i) {
if(every vertex has a predecessor) return;
// network has a cycle and thus is infeasible
pick a vertex v that has no predecessors;
// if there are multiple vertices, pick one arbitrarily
cout
7/27/2019 Data Structure - Adjacency
74/85
Graphs
J
uinn-DarHua
ng
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
0
1
2
3
4
5
1
2
3
4
5
1
2 4
5
1
4
5
1
44
Initial Vertex 0 deleted Vertex 3 deleted
Vertex 2 deleted Vertex 5 deleted Vertex 1 deleted
one of feasible topological orders: 0, 3, 2, 5, 1, 4
Example
Topological Sorting (3/3)
7/27/2019 Data Structure - Adjacency
75/85
Graphs
Juinn-DarHua
ng
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
Internal data structure
1
4 04
5
2
5 0
4 0
[0]
[1][2]
[3]
[4]
0
11
1
3 02 0[5]
3 0
count first data link
Indicate how many immediate predecessors
Activity-on-Edge (AOE) Networks
7/27/2019 Data Structure - Adjacency
76/85
Graphs
Juinn-DarHua
ng
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
event interpretation
0 Start of project
1 Completion of activity a1
4 Completion of activities a4 and a5
7 Completion of activities a8 and a9
8 Completion of project
1
0 4
2
6
8
7
3 5
start finish
a1 = 6 a4 = 1
a2 = 4
a5
= 1
a7= 9
a10 = 2
a3 = 5
a6 = 2
a9 = 4a11 = 4
No activity can be launched
until its predecessors are
all completedactivityevent
Q1:
How fast can the project be done?
Q2:What are the critical paths?
Q3:
How to speedup the project?
a8= 7
Critical Path (1/2)
7/27/2019 Data Structure - Adjacency
77/85
Graphs
Juinn-DarHua
ng
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
Critical path a path oflongest length
e.g., 0, 1, 4, 6, 8 or 0, 1, 4, 7, 8 of length 18
The earliest time that an event i can start is the length ofthe longest path from the start vertex 0 to vertex i
The earliest time of an event determines the earliest start
time for all activities represented by edges outgoing fromthat vertex
denoted by e(i) for activity ai
The latest time of an activity ai, l(i) the latest time ai can start without increasing the final project
duration
An activity aiis a critical activity ife(i) = l(i)
Critical Path (2/2)
7/27/2019 Data Structure - Adjacency
78/85
Graphs
Juinn-DarHuang
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
Meaning of l(i) e(i) criticality of an activity ai
Speedup non-critical activities will not reduce theproject duration
Speedup a critical activity may not reduce the
project duration unless that activity is on all critical paths
Critical path analysis helps
evaluate project performance
identify project bottlenecks
Earliest Time Calculation (1/2)
7/27/2019 Data Structure - Adjacency
79/85
Graphs
Juinn-DarHuang
jdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
For an event j ee[j] : earliest event time
le[j] : latest event time
For an activity ai represented by
e[i] = ee[k]
l[i] = le[l] duration of activity ai
How to calculate ee[j]?
let P(j) be the set of all js immediate predecessors
in what order? topological order
ee[j] = max { ee[i] + duration of }
iP(j)
Earliest Time Calculation (2/2)
7/27/2019 Data Structure - Adjacency
80/85
Gra
phs
Juinn-DarHuangjdhuang
@mail.nctu.e
du.tw
7copyright 2004-2006
adjacency list with in-degree count
Latest Time Calculation (1/2)
7/27/2019 Data Structure - Adjacency
81/85
Gra
phs
Juinn-DarHuangjdhuang
@mail.nctu.e
du.tw
8copyright 2004-2006
How to calculate le[j]? let S(j) be the set of all js immediate successors
in what order? reverse topological order
le[j] = min { le[i] - duration of }
iS(j)
Latest Time Calculation (2/2)
7/27/2019 Data Structure - Adjacency
82/85
Gra
phs
Juinn-DarHuangjdhuang
@mail.nctu.e
du.tw
8copyright 2004-2006
inverse adjacency list with out-degree count
Critical Activities
7/27/2019 Data Structure - Adjacency
83/85
Gra
phs
Juinn-DarHuangjdhuang
@mail.nctu.e
du.tw
8copyright 2004-2006
e = l
critical activity
Critical Network
7/27/2019 Data Structure - Adjacency
84/85
Gra
phs
Juinn-DarHuangjdhuang
@mail.nctu.edu.tw
8copyright 2004-2006
All paths from 0 to 8 are critical
Speed up a1 and a4 can reduce the project time
Final Review
7/27/2019 Data Structure - Adjacency
85/85
Gra
phs
Juinn-DarHuangjdhuang
@mail.nctu.edu.tw
8copyright 2004-2006
Terminology Representations
adjacency matrix/list/multilist
Graph traversals DFS and BFS
Connected and biconnected components
Minimum-cost spanning tree
Shortest path
non-negative/general weights
single-source-all destinations and all pairs
transitive closure
Topological order/sorting
AOV and AOE networks
Recommended