0% found this document useful (0 votes)
4 views37 pages

Digraphs

The document discusses directed graphs (digraphs), their properties, and applications such as task scheduling and one-way streets. It covers algorithms for traversing digraphs, including directed depth-first search (DFS), strong connectivity, and computing transitive closures using the Floyd-Warshall algorithm. Additionally, it explains topological sorting for directed acyclic graphs (DAGs) and provides an algorithm for achieving this sorting efficiently.

Uploaded by

anikeit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views37 pages

Digraphs

The document discusses directed graphs (digraphs), their properties, and applications such as task scheduling and one-way streets. It covers algorithms for traversing digraphs, including directed depth-first search (DFS), strong connectivity, and computing transitive closures using the Floyd-Warshall algorithm. Additionally, it explains topological sorting for directed acyclic graphs (DAGs) and provides an algorithm for achieving this sorting efficiently.

Uploaded by

anikeit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Presentation for use with the textbook, Algorithm

Design and Applications, by M. T. Goodrich and R.


Tamassia, Wiley, 2015

Directed Graphs BOS

ORD

JFK

SFO

DFW
LAX

MIA

© 2015 Goodrich and Tamassia Directed Graphs 1


Digraphs
 A digraph is a
graph whose E
edges are all
directed D
 Short for “directed
graph” C
 Applications B
 one-way streets
 flights A
 task scheduling

© 2015 Goodrich and Tamassia Directed Graphs 2


E

Digraph Properties D

C
 A graph G=(V,E) such that B
 Each edge goes in one direction: A
 Edge (a,b) goes from a to b, but not b to a
 If G is simple, m < n(n - 1)
 If we keep in-edges and out-edges in
separate adjacency lists, we can perform
listing of incoming edges and outgoing
edges in time proportional to their size

© 2015 Goodrich and Tamassia Directed Graphs 3


Digraph Application
 Scheduling: edge (a,b) means task a must
be completed before b can be started
cs21 cs22 cs46

cs51 cs53 cs52


cs161

cs131 cs141 cs121 cs171

cs151 The good life

© 2015 Goodrich and Tamassia Directed Graphs 4


Directed DFS
 We can specialize the
traversal algorithms (DFS
and BFS) to digraphs by
E
traversing edges only along
their direction D
 In the directed DFS
algorithm, we have four C
types of edges
 discovery edges B
 back edges
 forward edges
 cross edges A
 A directed DFS starting at a
vertex s determines the
vertices reachable from s
© 2015 Goodrich and Tamassia Directed Graphs 5
The Directed DFS
Algorithm

© 2015 Goodrich and Tamassia Directed Graphs 6


Reachability
 DFS tree rooted at v: vertices
reachable from v via directed paths
E D

E D C

A
C F
E D
A B
C F

A B
© 2015 Goodrich and Tamassia Directed Graphs 7
Strong Connectivity
 Each vertex can reach all other
vertices
a
g
c

d
e

b
f

© 2015 Goodrich and Tamassia Directed Graphs 8


Strong Connectivity
Algorithm
 Pick a vertex v in G
a
 Perform a DFS from v in G G: c
g
 If there’s a w not visited, print
“no” d
e
 Let G’ be G with edges b
f
reversed
 Perform a DFS from v in G’
a
 If there’s a w not visited, print g
“no” G’: c

 Else, print “yes” d


e
 Running time: O(n+m)
f b

© 2015 Goodrich and Tamassia Directed Graphs 9


Strongly Connected
Components
 Maximal subgraphs such that each vertex can
reach all other vertices in the subgraph
 Can also be done in O(n+m) time using DFS,
but is more complicated (similar to
biconnectivity).
a
g {a,c,g}
c

d
e
b
{f,d,e,b}
f

© 2015 Goodrich and Tamassia Directed Graphs 10


Transitive Closure
 Given a digraph G, the D E
transitive closure of G is
the digraph G* such that B
 G* has the same G
C
vertices as G
 if G has a directed
A
path from u to v (u 
v), G* has a directed D E
edge from u to v
 The transitive closure B
provides reachability
information about a C
digraph A G*
© 2015 Goodrich and Tamassia Directed Graphs 11
Computing the
Transitive Closure
If there's a way to get
 We can perform from A to B and from
DFS starting at B to C, then there's a
each vertex way to get from A to C.
 O(n(n+m))

Alternatively ... Use


dynamic
programming: The
Floyd-Warshall
Algorithm
© 2015 Goodrich and Tamassia Directed Graphs 12
Floyd-Warshall
Transitive Closure
 Idea #1: Number the vertices 1, 2,
…, n.
 Idea #2: Consider paths that use
only vertices numbered 1, 2, …, k,
as intermediateUses
vertices:
only vertices numbered 1,…,k
(add this edge if it’s not already in)
i

Uses only vertices j


numbered 1,…,k-1 Uses only vertices
k numbered 1,…,k-1
© 2015 Goodrich and Tamassia Directed Graphs 13
Floyd-Warshall’s
Algorithm: High-Level View
 Number vertices v1 , …, vn
 Compute digraphs G0, …, Gn
 G0=G
 Gk has directed edge (vi, vj) if G has a directed
path from vi to vj with intermediate vertices
in
{v1 , …, vk}
 We have that Gn = G*
 In phase k, digraph Gk is computed from
Gk - 1
© 2015 Goodrich and Tamassia Directed Graphs 14
 3
The Floyd-Warshall
Algorithm

 The running time is clearly O(n3).


© 2015 Goodrich and Tamassia Directed Graphs 15
Floyd-Warshall Example v7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2015 Goodrich and Tamassia Directed Graphs 16
Floyd-Warshall, Iteration 1 v7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2015 Goodrich and Tamassia Directed Graphs 17
Floyd-Warshall, Iteration 2 v7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2015 Goodrich and Tamassia Directed Graphs 18
Floyd-Warshall, Iteration 3 v7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2015 Goodrich and Tamassia Directed Graphs 19
Floyd-Warshall, Iteration 4 v7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2015 Goodrich and Tamassia Directed Graphs 20
Floyd-Warshall, Iteration 5 v7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2015 Goodrich and Tamassia Directed Graphs 21
Floyd-Warshall, Iteration 6 v7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2015 Goodrich and Tamassia Directed Graphs 22
Floyd-Warshall, Conclusion v 7
BOS

ORD v4
JFK
v2 v6
SFO

DFW
LAX
v3
v1
MIA

v5
© 2015 Goodrich and Tamassia Directed Graphs 23
DAGs and Topological
Ordering
 A directed acyclic graph (DAG) D E
is a digraph that has no
directed cycles B
 A topological ordering of a
digraph is a numbering C
v1 , …, vn A DAG G
of the vertices such that for
every edge (vi , vj), we have i < j v4 v5
 Example: in a task scheduling D E
digraph, a topological ordering v2
a task sequence that satisfies
B
the precedence constraints v3
Theorem
v1 C
A digraph admits a topological Topological
ordering if and only if it is a A ordering of
DAG
© 2015 Goodrich and Tamassia Directed Graphs G 24
Topological Sorting
 Number vertices, so that (u,v) in E implies
u < v wake up 1 A typical student day
2 3
eat
study computer sci.

4 5
nap more c.s.
7
play
8
write c.s. program 6
9 work out
bake cookies
10
sleep 11
dream about
© 2015 Goodrich and Tamassia Directed Graphs
graphs 25
Algorithm for Topological
Sorting
 Note: This algorithm is different than
the one in the book
Algorithm TopologicalSort(G)
HG // Temporary copy of G
n  G.numVertices()
while H is not empty do
Let v be a vertex with no outgoing edges
Label v  n
nn-1
Remove v from H

 Running time: O(n + m)


© 2015 Goodrich and Tamassia Directed Graphs 26
Implementation with
DFS
 Simulate the algorithm by Algorithm topologicalDFS(G, v)
using depth-first search Input graph G and a start vertex
 O(n+m) time. v of G
Output labeling of the vertices
of G
Algorithm topologicalDFS(G) in the connected
Input dag G component of v
Output topological ordering setLabel(v, VISITED)
of G for all e  G.outEdges(v)
n  G.numVertices() { outgoing edges }
for all u  G.vertices() w  opposite(v,e)
setLabel(u, if getLabel(w) =
UNEXPLORED) UNEXPLORED
for all v  G.vertices() { e is a discovery
if getLabel(v) = edge }
UNEXPLORED topologicalDFS(G,
w)
topologicalDFS(G, v) else
© 2015 Goodrich and Tamassia Directed Graphs 27
{ e is a forward or
Topological Sorting
Example

© 2015 Goodrich and Tamassia Directed Graphs 28


Topological Sorting
Example

9
© 2015 Goodrich and Tamassia Directed Graphs 29
Topological Sorting
Example

9
© 2015 Goodrich and Tamassia Directed Graphs 30
Topological Sorting
Example

7
8

9
© 2015 Goodrich and Tamassia Directed Graphs 31
Topological Sorting
Example

7
8

9
© 2015 Goodrich and Tamassia Directed Graphs 32
Topological Sorting
Example

6 5

7
8

9
© 2015 Goodrich and Tamassia Directed Graphs 33
Topological Sorting
Example

4
6 5

7
8

9
© 2015 Goodrich and Tamassia Directed Graphs 34
Topological Sorting
Example

3
4
6 5

7
8

9
© 2015 Goodrich and Tamassia Directed Graphs 35
Topological Sorting
Example
2

3
4
6 5

7
8

9
© 2015 Goodrich and Tamassia Directed Graphs 36
Topological Sorting
Example
2
1

3
4
6 5

7
8

9
© 2015 Goodrich and Tamassia Directed Graphs 37

You might also like