没有幻灯片标题 - Zhejiang University

Download Report

Transcript 没有幻灯片标题 - Zhejiang University

CHAPTER 9
GRAPH ALGORITHMS
§1 Definitions
 G( V, E ) where G ::= graph, V = V( G ) ::= finite nonempty set of
vertices, and E = E( G ) ::= finite set of edges.
 Undirected graph: ( vi , vj ) = ( vj , vi ) ::= the same edge.
vj  < vj , vi >
 Directed graph (digraph): < vi , vj > ::= vi
tail
 Restrictions :
1
(1) Self loop is illegal. 0
(2) Multigraph is not considered
head
0
1
2
 Complete graph: a graph that has the maximum number of edges
0
1
2
1/7
3 # of V  n 
# of E  C n2  n( n21)
0
1
2
3 # of V  n 
# of E  Pn2  n(n  1)
§1 Definitions

vi
vj vi and vj are adjacent ;
( vi , vj ) is incident on vi and vj

vi
vj vi is adjacent to vj ; vj is adjacent from vi ;
< vi , vj > is incident on vi and vj
 Subgraph G’  G ::= V( G’ )  V( G ) && E( G’ )  E( G )
 Path ( G) from vp to vq ::= { vp, vi1, vi2, , vin, vq } such that ( vp, vi1 ),
( vi1, vi2 ), , ( vin, vq ) or < vp, vi1 >, , < vin, vq > belong to E( G )
 Length of a path ::= number of edges on the path
 Simple path ::= vi1, vi2, , vin are distinct
 Cycle ::= simple path with vp = vq
 vi and vj in an undirected G are connected if there is a path from vi to vj
(and hence there is also a path from vj to vi)
 An undirected graph G is connected if every pair of distinct vi and vj are
connected
2/7
§1 Definitions
 (Connected) Component of an undirected G ::= the maximal connected
subgraph
 A tree ::= a graph that is connected and acyclic
 A DAG ::= a directed acyclic graph
 Strongly connected directed graph G ::= for every pair of vi and vj in
V( G ), there exist directed paths from vi to vj and from vj to vi. If the
graph is connected without direction to the edges, then it is said to be
weakly connected
 Strongly connected component ::= the maximal subgraph that is
strongly connected
 Degree( v ) ::= number of edges incident to v. For a directed G, we have
in-degree and out-degree. For example:
v
in-degree(v) = 3; out-degree(v) = 1; degree(v) = 4
 Given G with n vertices and e edges, then
 n 1 
e    d i  2 whe re d i  de gre e(vi )
 i 0 
3/7
§1 Definitions
 Representation of Graphs
Adjacency Matrix
adj_mat [ n ] [ n ] is defined for G(V, E) with n vertices, n  1 :
vi , v j   E (G )
 1 if (vi , v j ) or I know
adj_mat[i ][Hey
j ]  you
 begin to know
whatme!
you’re about to say:
0 othe rwise

Right. And it wastes time
as
well.
representation
The trick is to store this
the matrix
as a 1-D wastes
array:
we are to find
out whether
or
not
Note: If G is If
undirected,
then
adj_mat[
][
]
is
symmetric.
has
of
adj_mat [ n(n+1)/2 ] =space
{ a11, aif21the
, a22graph
, ..., an1
, ...,a alot
}
nn
G iscan
connected,
we’ll have
to examine
vertices
but
very
few
edges,
Thus we
save
space
by
storing
only
half
of
the
The index for aij is ( i  ( i  1 ) / 2 + j ).
all edges. In this case
right?
matrix.
2
T and S are both O( n )
n 1
de gre e( i )   adj_mat[i ][ j ]
(if G is undire cte)d
j 0
n 1
  adj_mat[ j ][i ]
j 0
4/7
(if G is dire cte d)
§1 Definitions
Adjacency Lists
Replace each row by a linked list
〖Example〗
 0 1 0
adj_mat[3][3]  1 0 1
0 0 0
0
graph[0]
1

1
graph[1] graph[2]
0

2

Note: The order of nodes in
each list does not matter.
For undirected G:
S = n heads + 2e nodes = (n+2e) ptrs+2e ints
5/7
2
§1 Definitions
Degree( i ) = number of nodes in graph[ i ] (if G is undirected).
T of examine E(G) = O( n + e )
If G is directed, we need to find in-degree(v) as well.
Method 1 Add inverse adjacency lists.
〖Example〗
0
1
2
inv[0]
inv[1]
inv[2]
1

0

1

Method 2 Multilist (Ch 3.2) representation for adj_mat[ i ] [ j ]
tail i head j


column for head
6/7
row for tail
§1 Definitions
Adjacency Multilists
In adjacency list, for each ( i, j ) we have two nodes:
graph[i]
j 
…… Now let’s combine the two nodes
graph[j]
i 
graph[j]
…… into one: graph[i]
node
mark
v1 v2
 
next next
v1
v2
Wait a minute ...
Look at the space
taken:
〖Example〗
graph[0]
(n+2e) ptrs + 2e ints
0
and “mark”
is not counted.
graph[1]
What’s the advantage?
1
2
0 1
 
0 2
graph[2]we need to  
Sometimes
3 mark the edge after examine it,
Weighted Edges
and then find
the next edge. 2 3
graph[3]
 
This representation makes
 adj_mat [ i ] [ j ] = weight it easy to do so.
 adjacency lists \ multilists : add a weight field to the node.
7/7