0% found this document useful (0 votes)
22 views9 pages

Digraphs

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

Digraphs

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

Digraphs

 A digraph is a graph
Directed Graphs BOS
whose edges are all
directed
E

ORD
 Short for “directed graph” D
JFK

SFO
 Applications C
 one-way streets
LAX
DFW
 flights B
MIA
 task scheduling
A

© 2010 Goodrich, Tamassia Directed Graphs 1 © 2010 Goodrich, Tamassia Directed Graphs 2

Digraph Properties D Digraph Application


C  Scheduling: edge (a,b) means task a must be
 A graph G=(V,E) such that B completed before b can be started
 Each edge goes in one direction: A ics21 ics22 ics23
 Edge (a,b) goes from a to b, but not b to a
 If G is simple, m < n⋅(n − 1)
ics51 ics53 ics52
 If we keep in-edges and out-edges in separate ics161
adjacency lists, we can perform listing of
ics131 ics141 ics121 ics171
incoming edges and outgoing edges in time
proportional to their size
ics151 The good life

© 2010 Goodrich, Tamassia Directed Graphs 3 © 2010 Goodrich, Tamassia Directed Graphs 4
Directed DFS Reachability
 We can specialize the traversal
algorithms (DFS and BFS) to
E
 DFS tree rooted at v: vertices reachable
digraphs by traversing edges
only along their direction from v via directed paths
D E D
 In the directed DFS algorithm,
we have four types of edges
E D C
 discovery edges C
 back edges
A
 forward edges B C F
cross edges
E D

A directed DFS starting at a A A B



vertex s determines the vertices
C F
reachable from s
A B
© 2010 Goodrich, Tamassia Directed Graphs 5 © 2010 Goodrich, Tamassia Directed Graphs 6

Strong Connectivity
Strong Connectivity Algorithm
 Each vertex can reach all other vertices  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”


a 
d
g  Let G’ be G with edges reversed e
c  Perform a DFS from v in G’ f b

 If there’s a w not visited, print “no”


 Else, print “yes” a
d g
 Running time: O(n+m) G’: c
e
d
e
b
f f b

© 2010 Goodrich, Tamassia Directed Graphs 7 © 2010 Goodrich, Tamassia Directed Graphs 8
Strongly Connected
Components Transitive Closure
 Maximal subgraphs such that each vertex can reach  Given a digraph G, the D E
all other vertices in the subgraph transitive closure of G is the
digraph G* such that B
 Can also be done in O(n+m) time using DFS, but is G
 G* has the same vertices C
more complicated (similar to biconnectivity).
as G
A
a  if G has a directed path
g {a,c,g} from u to v (u ≠ v), G*
c
has a directed edge from D E
u to v
d
e  The transitive closure B
{f,d,e,b} provides reachability
b information about a digraph C
f
A G*
© 2010 Goodrich, Tamassia Directed Graphs 9 © 2010 Goodrich, Tamassia Directed Graphs 10

Computing the Floyd-Warshall


Transitive Closure Transitive Closure
If there's a way to get
 We can perform from A to B and from  Idea #1: Number the vertices 1, 2, …, n.
DFS starting at B to C, then there's a  Idea #2: Consider paths that use only
each vertex way to get from A to C.
vertices numbered 1, 2, …, k, as
 O(n(n+m)) intermediate vertices:
Uses only vertices numbered 1,…,k
(add this edge if it’s not already in)
i
Alternatively ... Use
dynamic programming:
The Floyd-Warshall Uses only vertices j
Algorithm numbered 1,…,k-1 Uses only vertices
k numbered 1,…,k-1
© 2010 Goodrich, Tamassia Directed Graphs 11 © 2010 Goodrich, Tamassia Directed Graphs 12
Floyd-Warshall’s Algorithm Floyd-Warshall Example v7
 Number vertices v1 , …, vn Algorithm FloydWarshall(G) BOS
Input digraph G
 Compute digraphs G0, …, Gn Output transitive closure G* of G ORD v4
 G0=G i←1
 Gk has directed edge (vi, vj) for all v ∈ G.vertices() JFK
if G has a directed path denote v as vi v2
i←i+1
v6
from vi to vj with
intermediate vertices in G0 ← G SFO
{v1 , …, vk} for k ← 1 to n do
 We have that Gn = G* G k ← Gk − 1
for i ← 1 to n (i ≠ k) do
 In phase k, digraph Gk is for j ← 1 to n (j ≠ i, k) do DFW
computed from Gk − 1 if Gk − 1.areAdjacent(vi, vk) ∧ LAX
Running time: O(n3), Gk − 1.areAdjacent(vk, vj)
v3
 v1
assuming areAdjacent is O(1) if ¬Gk.areAdjacent(vi, vj) MIA
(e.g., adjacency matrix) Gk.insertDirectedEdge(vi, vj , k)
return Gn v5
© 2010 Goodrich, Tamassia Directed Graphs 13 © 2010 Goodrich, Tamassia Directed Graphs 14

Floyd-Warshall, Iteration 1 v7 Floyd-Warshall, Iteration 2 v7


BOS BOS

ORD v4 ORD v4
JFK JFK
v2 v6 v2 v6
SFO SFO

DFW DFW
LAX LAX
v3 v3
v1 v1
MIA MIA

v5 v5
© 2010 Goodrich, Tamassia Directed Graphs 15 © 2010 Goodrich, Tamassia Directed Graphs 16
Floyd-Warshall, Iteration 3 v7 Floyd-Warshall, Iteration 4 v7
BOS BOS

ORD v4 ORD v4
JFK JFK
v2 v6 v2 v6
SFO SFO

DFW DFW
LAX LAX
v3 v3
v1 v1
MIA MIA

v5 v5
© 2010 Goodrich, Tamassia Directed Graphs 17 © 2010 Goodrich, Tamassia Directed Graphs 18

Floyd-Warshall, Iteration 5 v7 Floyd-Warshall, Iteration 6 v7


BOS BOS

ORD v4 ORD v4
JFK JFK
v2 v6 v2 v6
SFO SFO

DFW DFW
LAX LAX
v3 v3
v1 v1
MIA MIA

v5 v5
© 2010 Goodrich, Tamassia Directed Graphs 19 © 2010 Goodrich, Tamassia Directed Graphs 20
Floyd-Warshall, Conclusion v7
DAGs and Topological Ordering
BOS
 A directed acyclic graph (DAG) is a D E
v4 digraph that has no directed cycles
ORD
 A topological ordering of a digraph B
JFK is a numbering
v2 v1 , …, vn
C
v6
of the vertices such that for every A DAG G
SFO edge (vi , vj), we have i < j
 Example: in a task scheduling v4 v5
digraph, a topological ordering a D E
DFW task sequence that satisfies the v2
precedence constraints
LAX
v3 B
Theorem v3
v1
MIA A digraph admits a topological v1 C
ordering if and only if it is a DAG Topological
v5 A ordering of G
© 2010 Goodrich, Tamassia Directed Graphs 21 © 2010 Goodrich, Tamassia Directed Graphs 22

Topological Sorting Algorithm for Topological Sorting


 Number vertices, so that (u,v) in E implies u < v  Note: This algorithm is different than the
1
wake up A typical student day one in the book
2 3
eat
study computer sci.
Algorithm TopologicalSort(G)
4 5 H←G // Temporary copy of G
nap more c.s. n ← G.numVertices()
7
play while H is not empty do
8 Let v be a vertex with no outgoing edges
write c.s. program 6 Label v ← n
9 work out n←n−1
bake cookies Remove v from H
10
11
sleep
 Running time: O(n + m)
dream about graphs
© 2010 Goodrich, Tamassia Directed Graphs 23 © 2010 Goodrich, Tamassia Directed Graphs 24
Implementation with DFS Topological Sorting Example
 Simulate the algorithm by Algorithm topologicalDFS(G, v)
using depth-first search Input graph G and a start vertex v of G
 O(n+m) time. Output labeling of the vertices of G
in the connected component of v
v.setLabel(VISITED)
Algorithm topologicalDFS(G) for all e ∈ v.outEdges()
Input dag G { outgoing edges }
Output topological ordering of G w ← e.opposite(v)
n ← G.numVertices() if w.getLabel() = UNEXPLORED
for all u ∈ G.vertices() { e is a discovery edge }
u.setLabel(UNEXPLORED) topologicalDFS(G, w)
for all v ∈ G.vertices() else
if v.getLabel() = UNEXPLORED { e is a forward or cross edge }
topologicalDFS(G, v) Label v with topological number n
n←n-1

© 2010 Goodrich, Tamassia Directed Graphs 25 © 2010 Goodrich, Tamassia Directed Graphs 26

Topological Sorting Example Topological Sorting Example

9 9

© 2010 Goodrich, Tamassia Directed Graphs 27 © 2010 Goodrich, Tamassia Directed Graphs 28
Topological Sorting Example Topological Sorting Example

7 7
8 8

9 9

© 2010 Goodrich, Tamassia Directed Graphs 29 © 2010 Goodrich, Tamassia Directed Graphs 30

Topological Sorting Example Topological Sorting Example

4
6 5 6 5

7 7
8 8

9 9

© 2010 Goodrich, Tamassia Directed Graphs 31 © 2010 Goodrich, Tamassia Directed Graphs 32
Topological Sorting Example Topological Sorting Example
2

3 3
4 4
6 5 6 5

7 7
8 8

9 9

© 2010 Goodrich, Tamassia Directed Graphs 33 © 2010 Goodrich, Tamassia Directed Graphs 34

Topological Sorting Example


2
1

3
4
6 5

7
8

© 2010 Goodrich, Tamassia Directed Graphs 35

You might also like