Title: Directed Graphs
1Directed Graphs
- In this kind of graph, edges connecting nodes are
one-way. - Examples
- Manufacturing line. Nodes are jobs, and edges
order the jobs.
K
J
L
M
2Directed graphs
3Internal representations
Adjacency matrices we maintain a full matrix as
usual, with a 1 bit in row x and column y (but
not necessarily in row y and column x) if there
is an edge from x to y. Adjacency lists each
edge appears only once, as according to the
direction of the edge. The direction from x to y
is represented as a list node containing y in the
linked list corresponding to x.
4Internal representations
Note that now the order in which the vertices
appear when specifying edges in the graph makes a
difference! Below, we specify edge AB and not
edge BA for example. We can talk of edges HI and
IH since both edges exist in the graph.
5Depth-First Search
DFS works properly for directed graphs as
specified earlier, however the search tree has a
somewhat more complicated structure.
Undirected
Directed
6Depth-First Search
Recall the search tree for the undirected graph
(from a previous discussion)
One kind of dotted edge one that connected a
vertex with some ancestor in the tree.
7Depth-First Search
Three kinds of dotted edges up edges point from
a vertex to some ancestor in the tree. down edges
point from a vertex to some descendant in the
tree. cross edges point from a vertex to another
vertex that is neither a descendant nor an
ancestor.
8Depth-First Search
9Goals
Wed like to answer questions like Is there a
directed path from vertex x to vertex y (a path
that follows edges only in the indicated
direction)? Which vertices can we get to from
vertex x with a directed path? Is there a
directed path fro, vertex x to vertex y and a
directed path from y to x? We can answer such
question by appropriately modifying the basic DFS.
10Transitive Closure
In directed graphs, were often interested in the
set of vertices that can be reached from a given
vertex by traversing edges from the graph in the
indicated direction. A recursive visit from the
DFS (discussed earlier) visits all the nodes that
can be reached from the start node.
11Transitive Closure
However, it is not necessarily true that each
tree in the DFS search forest contains all the
nodes that can be reached from the root of that
tree all the nodes in the graph can be visited
from H, not just I.
12Transitive Closure
To get all nodes in the graph that can be visited
from each edge node, we simply call visit V
times, once for each node
for (k0 kltv k) id 0 for (j0
jltV j) valj 0 cout ltlt endl
visit(k)
13Transitive Closure
The code fragment produces this output
A F E D B G J K L M C B C A F E D B G J K L M D F
E E D F F E D G J K L M C A F E D B H G J K L M C
A F E D B I I H G J K L M C A F E D B J K L G C A
F E D B M KL G J K M C A F E D B M L G J K C A F
E D B
14Transitive Closure
A F E D B G J K L M C B C A F E D B G J K L M D F
E E D F F E D G J K L M C A F E D B H G J K L M C
A F E D B I I H G J K L M C A F E D B J K L G C A
F E D B M KL G J K M C A F E D B M L G J K C A F
E D B
From this table, we can determine if there is a
way to get from x to y and if so, we could mark
that by adding a direct edge. (cf. union-finds)
15Transitive Closure
A F E D B G J K L M C B C A F E D B G J K L M D F
E E D F F E D G J K L M C A F E D B H G J K L M C
A F E D B I I H G J K L M C A F E D B J K L G C A
F E D B M KL G J K M C A F E D B M L G J K C A F
E D B
The graph that results from adding all edges of
this nature to a directed graph is known ad the
transitive closure of the graph. Typically, we
use an adjacency matrix to store the graph, since
there normally are a large number of edges. The
structure is then used to answer the question is
there a way to get from x to y?
16Performance
Since we are using DFS to compute the transitive
closure, and recalling that DFS for a graph
expressed with adjacency lists uses O(VE) and
that expressed with adjacency matrices uses
O(V2), then Transitive closure requires
O(V(VE)) steps for a sparse graph, and O(V3) for
a dense graph.