Chapter-6
Directed Graphs
5/9/2022 1
Directed Graph
Definition: When the edges in a graph have a direction,
the graph is called directed (or digraph).
In a directed graph(digraph), each edge is associated
with an ordered pair of vertices.
Example1:
A labeled simple graph:
Vertex set V = {2,3,5,7,8,9,10,11}
Edge set E = {{3,8}, {3,10}, {5,11}, {7,8}, {7,11},
5/9/2022
{8,9},{11,2},{11,9},{11,10}}. 2
Directed Graph
Example2: Let V={a,b,c,d} and
E={(a,b),(a,c),(b,a),(c,c),(d,c).
The outdegree of a vertex a is the number of edges
that initiate at a. The indegree of a is the number of
edges that terminate at a.
A vertex with indegree 0 is a source, and a vertex
with outdegree 0 is a sink. In the above digraph
outdeg(a)=2 and indeg(a)=1. Also c is a sink and d
is a source.
5/9/2022 3
Cont’d
Example3:
5/9/2022 4
Directed Graph
5/9/2022 5
Cont’d
Theorem: Let G = (V, E) be a graph with directed
edges. Then:
vV deg-(v) = vV deg+(v) = |E|
This is easy to see, because every new edge increases
both the sum of in-degrees and the sum of out-degrees
by one.
Directed paths, however, are slightly different since
one may travel only in the direction the edges have.
For instance if abcd is a directed path, then there
must be an edge from a to b (not merely an edge
between a and b).
5/9/2022 6
Cont’d
5/9/2022 7
Matrix Representation of Didraphs
Adjacency matrix representation
for directed graph with vertices numbered
1, 2, … n defined as n by n matrix named adj
the [i,j] entry set to
• 1 (true) if vertex j is adjacent to vertex i
(there is a directed arc from i to j)
• 0 (false) otherwise.
5/9/2022 8
Cont’d
5/9/2022 9
Paths and Connectivity
Routing problems – finding an optimal path in a
network.
– a shortest path in a graph/digraph.
– a cheapest path in a weighted graph/digraph.
Example – a directed graph that models an airline
network
– vertices represent cities
– direct arcs represent flights connecting cities
Task: find most direct route (least flights).
Most direct route equivalent to
– finding length of shortest path
5/9/2022 10
Cont’d
– finding minimum number of arcs from start
vertex to destination vertex.
A digraph is connected if its underlying graph is
connected.
It is strongly connected if there is a directed path
from every vertex to every other vertex.
• As an example we see below a digraph and its
underlying graph. Clearly the digraph is connected.
It is not strongly connected, however, since for
instance there is no directed path from a to d (or
even from c to d).
5/9/2022 11
Cont’d
5/9/2022 12
Weighted Graphs and Their Applications
Definition:
Weighted graphs are graphs that have a number
assigned to each edge.
The length of a path in a weighted graph is
the sum of the weights of the edges of this
path(or The length of a path is defined as the
number of edges in the path).
Short-Path is the path of least sum of the weights
between two vertices in a weighted graph.
5/9/2022 13
Cont’d
5/9/2022 14
Minimum Spinning Tree
A spanning tree of an undirected graph G is a
subgraph of G that is a tree containing all the
vertices of G.
In a weighted graph, the weight of a subgraph is the
sum of the weights of the edges in the subgraph.
• A minimum spanning tree in a connected weighted
graph is a spanning tree that has the
• smallest possible sum of weights of its edges
5/9/2022 15
Cont’d
A graph may have many spanning trees.
Suppose that the edges are weighted.
A minimum spanning tree is a spanning tree with the
minimum total weights.
For example, the trees in Figures (b), (c), (d) are
spanning trees for the graph in Figure (a).
• The trees in Figures (c) and (d) are minimum
spanning trees.
10 10
6 6 6
7 5 8 5 5 5
7
8 10
5 7 7 8 5 7 7 8 5 7 8 5 7 7 8
12
a) b) c) d)
5/9/2022 16
Minimum Spanning Tree Algorithm
Define a minimum Spanning Tree:
Let V denote the set of vertices in the graph;
Let T be a set for the vertices in the spanning tree;
Initially, add the starting vertex to T;
while size of T < n:
find u in T and v in V – T with the smallest weight
on the edge (u, v), as shown in Figure below;
add v to T; Vertices not currently in
Vertices already in V-T
the spanning tree the spanning tree
T
v
5/9/2022 17
Shortest Path Problems
• We can assign weights to the edges of graphs, for
example to represent the distance between cities in a
railway network:
Toronto
650
700 Boston
Chicago
200
600
New York
5/9/2022 18
Cont’d
Such weighted graphs can also be used to model
computer networks with response times or costs as
weights.
One of the most interesting questions that we can
investigate with such graphs is:
What is the shortest path between two vertices in
the graph, that is, the path with the minimal sum of
weights along the way?
• This corresponds to the shortest train connection or
the fastest connection in a computer network.
5/9/2022 19
Dijkstra’s Algorithm
Dijkstra’s algorithm is an iterative procedure that
finds the shortest path between two vertices a and z
in a weighted graph.
It proceeds by finding the length of the shortest path
from a to successive vertices and adding these
vertices to a distinguished set of vertices S.
The algorithm terminates once it reaches the vertex
z.
5/9/2022 20
Example
Use Dijkstra’s algorithm to find the length of a
shortest path between a and z in the weighted
4(a)
graph.
b 5 d b 5 d b 5 d
4 6 Soln: 4 6 4 6
a
8
z
0
a 1
8
z 0
a 1
8
z
1 2 2 2
3 2 3 2 3
2
e c 10 e c 10 e
c 10 2(a)
3(c) 3(c) 8(b)
10(c) 3(c) 8(b)
b 5 d b 5 d
b 5 d
4 6 6
4 6
4 0 8
0 8 0 8 a z 14
a 1 z a z 1 2
2 1 (d)
2 3
2 2 3
2 3
e c 10 e
c 10
12(c) c 10 e 2(a) 10(d)
2(a) 2(a) 12(c)
5/9/2022 21
Cont’d
3(c) 8(b) 3(c) 8(b)
b 5 d b 5 d
4 6 4 6
0
a 1
8
2
z 14(d) 0
a 1
8
2
z 13(e)
2 3 2 3
c 10 e c 10 e
2(a) 10(d) 2(a) 10(d)
3(c) 8(b)
b 5 d
4 6 path: a, c, b, d, e, z
0 8
a 1 2
z 13(e) length: 13
2 3
c 10 e
2(a) 10(d)
5/9/2022 22
Exercise
Use Dijkstra’s algorithm to find the length of a
shortest path between a and z in the following
weighted graph.
5/9/2022 23
The Traveling Salesman Problem
The traveling salesman problem is one of the classical
problems in computer science.
A traveling salesman wants to visit a number of cities
and then return to his starting point. Of course he wants
to save time and energy, so he wants to determine the
shortest path for his trip.
We can represent the cities and the distances between
them by a weighted, complete, undirected graph.
• The problem then is to find the circuit of minimum
total weight that visits each vertex exactly one.
5/9/2022 24
Cont’d
Example: What path would the traveling
salesman take to visit the following cities?
Toronto
650 550
700
Boston
Chicago
700
200
600
New York
Solution: The shortest path is Boston, New York,
Chicago, Toronto, Boston (2,000 miles).
5/9/2022 25
Cont’d
Question: Given n vertices, how many different
cycles Cn can we form by connecting these vertices
with edges?
Solution: We first choose a starting point. Then we
have (n – 1) choices for the second vertex in the cycle,
(n – 2) for the third one, and so on, so there are (n –
1)! choices for the whole cycle.
However, this number includes identical cycles that
were constructed in opposite directions. Therefore,
the actual number of different cycles Cn is (n – 1)!/2.
5/9/2022 26
Cont’d
Unfortunately, no algorithm solving the traveling
salesman problem with polynomial worst-case time
complexity has been devised yet.
This means that for large numbers of vertices, solving
the traveling salesman problem is impractical.
In these cases, we can use efficient approximation
algorithms that determine a path whose length may be
slightly larger than the traveling salesman’s path.
5/9/2022 27