CS 3110 Lecture 22
Directed graph representations and traversals
A directed graph
G
is an ordered pair (
V
,
E
) of a set of vertices
V
={
v1,.
..,v
n
}, and a set of edges
E
={(
vi
,
vj
) :
vj
,
vj
∈
V
}. Generally we denote the number of vertices by 
V
 and the number of edges by 
E
.
Directed graphs are commonly represented as an
adjacency list
, which comprises an array or list of vertices,
where each vertex
v
i
stores a list of all the adjacent vertices (i.e., a list of all the vertices for which there is an
edge originating at
v
i
). Another common representation is an
adjacency matrix
, which is a twodimensional
array, where A[i,j] is nonzero when there is an edge from
v
i
to
v
j
. In practice many graphs are sparse, in the
sense that most of the possible edges between pairs of vertices do not exist, i.e. 
E
 << Ω 
V

2
. In such cases an
adjacency list is generally preferable to an adjacency matrix representation.
In this lecture we will consider an adjacency list representation for directed graphs, where each edge can
additionally have an integer weight (which for now we will always take to be 1).
Here is a simple directed graph of four vertices (or nodes),
V
={
v
1
,v
2
,v
3
,v
4
} and four edges
E
={(
v
1
,v
2
),
(v
2
,v
3
),(v
3
,v
1
), (v
3
,v
4
)
},
[1] < [3]

>

/

v /
v
[2]
[4]
Consider the following abstract data type for a directed graph with weighted edges. Note that while this
specification does not explicitly require any particular implementation, the required running times of some of
these functions constrain the implementation in various ways. For instance, a naive adjacency matrix
implementation would take Θ(
V

2
) time to consider every array entry in producing a list of all the edges.
However the
edges
function is required to do this in in O(
V
+
E
) time.
(* A signature for directed graphs.*)
module type WGRAPH = sig
type graph
(* A directed graph consisting of a set of vertices
* V and directed edges E with integer weights. *)
type vertex (* A vertex, or node, of the graph *)
type edge
(* A edge of the graph *)
(* create an empty graph *)
val create: unit > graph
(* return the id of the specified vertex *)
val vertex_id: vertex > int
(* compare two vertices, returning 0 if their id's are equal, 1 if
the first has a smaller id and +1 if first has a larger id.
Suitable for comparators in sets and maps. *)
val compare: vertex > vertex > int
(* For an edge return (src,dst,w) where src is the source vertex of
* the edge, dst is the destination vertex, and w is the edge
* weight. *)
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Documentval edge_info: edge > vertex * vertex * int
(* True if the given graph is empty (has no vertices).
* Run time O(1). *)
val is_empty: graph > bool
(* A list of all vertices in the graph, without duplicates, in the order
* they were added.
* Run time: O(V). *)
This is the end of the preview. Sign up
to
access the rest of the document.
 '07
 GIAMBATTISTA,A

Click to edit the document details