This preview shows pages 1–3. Sign up to view the full content.
Lecture Notes
CMSC 251
Observation:
For a digraph
e
≤
n
2
=
O
(
n
2
)
. For an undirected graph
e
≤
(
n
2
)
=
n
(
n

1)
/
2=
O
(
n
2
)
.
A graph or digraph is allowed to have no edges at all. One interesting question is what the minimum
number of edges that a connected graph must have.
We say that a graph is
sparse
if
e
is much less than
n
2
.
For example, the important class of
planar graphs
(graphs which can be drawn on the plane so that no
two edges cross over one another)
e
=
O
(
n
)
. In most application areas, very large graphs tend to be
sparse. This is important to keep in mind when designing graph algorithms, because when
n
is really
large and
O
(
n
2
)
running time is often unacceptably large for realtime response.
Lecture 22: Graphs Representations and BFS
(Thursday, April 16, 1998)
Read: Sections 23.1 through 23.3 in CLR.
Representations of Graphs and Digraphs:
We will describe two ways of representing graphs and digraphs.
First we show how to represent digraphs. Let
G
=(
V,E
)
be a digraph with
n
=

V

and let
e
=

E

.
We will assume that the vertices of
G
are indexed
{
1
,
2
,...,n
}
.
Adjacency Matrix:
An
n
×
n
matrix deﬁned for
1
≤
v,w
≤
n
.
A
[
]=
±
1
if
(
)
∈
E
0
otherwise.
If the digraph has weights we can store the weights in the matrix. For example if
(
)
∈
E
then
A
[
W
(
)
(the weight on edge
(
)
). If
(
)
/
∈
E
then generally
W
(
)
need
not be deﬁned, but often we set it to some “special” value, e.g.
A
(
)=

1
,or
∞
. (By
∞
we mean (in practice) some number which is larger than any allowable weight. In practice, this
might be some machine dependent constant like
MAXINT
.)
Adjacency List:
An array
Adj
[1
...n
]
of pointers where for
1
≤
v
≤
n
,
[
v
]
points to a linked list
containing the vertices which are adjacent to
v
(i.e. the vertices that can be reached from
v
by a
single edge). If the edges have weights then these weights may also be stored in the linked list
elements.
3
1
1
0
1
0
1
1
0
0
2
3
1
2
3
2
1
Adjacency matrix
Adj
Adjacency list
3
2
2
3
1
3
2
1
1
Figure 23: Adjacency matrix and adjacency list for digraphs.
We can represent undirected graphs using exactly the same representation, but we will store each edge
twice. In particular, we representing the undirected edge
{
}
by the two oppositely directed edges
(
)
and
(
w,v
)
. Notice that even though we represent undirected graphs in the same way that we
65
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Lecture Notes
CMSC 251
represent digraphs, it is important to remember that these two classes of objects are mathematically
distinct from one another.
This can cause some complications. For example, suppose you write an algorithm that operates by
marking edges of a graph. You need to be careful when you mark edge
(
v,w
)
in the representation
that you also mark
(
w,v
)
, since they are both the same edge in reality. When dealing with adjacency
lists, it may not be convenient to walk down the entire linked list, so it is common to include
cross links
between corresponding edges.
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 01/13/2012 for the course CMSC 351 taught by Professor Staff during the Fall '11 term at University of Louisville.
 Fall '11
 Staff

Click to edit the document details