This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
11
CSC401 – Analysis of Algorithms Lecture Notes 15
Directed GraphsDirected Graphs
Objectives:Objectives: Introduce directed graphs and weighted Introduce directed graphs and weighted
graphsgraphs Present algorithms performed on directed Present algorithms performed on directed
A A digraphdigraph is a is a graph whose edges graph whose edges are all directedare all directed– Short for “directed Short for “directed
graph”graph”
ApplicationsApplications– one-way streetsone-way streets– flightsflights– task schedulingtask scheduling A
C
E
B
D
33
Digraph PropertiesDigraph Properties
A graph G=(V,E) such thatA graph G=(V,E) such that– Each edge goes in one direction:Each edge goes in one direction:
Edge Edge (a,b(a,b) goes ) goes from a to bfrom a to b, but , but not b to a.not b to a.
If G is simple, m If G is simple, m << n*(n-1). n*(n-1).
If we keep in-edges and out-edges in If we keep in-edges and out-edges in separate adjacency lists, we can perform separate adjacency lists, we can perform listing of in-edges and out-edges in time listing of in-edges and out-edges in time proportional to their size.proportional to their size.
A
C
E
B
D
44
Digraph ApplicationDigraph ApplicationScheduling:Scheduling: edge edge (a,b(a,b) means task ) means task a must a must be completed before b can be startedbe completed before b can be started
The good life
ics141ics131 ics121
ics53 ics52ics51
ics23ics22ics21
ics161
ics151
ics171
55
Directed DFSDirected DFSWe can specialize the We can specialize the traversal algorithms (DFS traversal algorithms (DFS and BFS) to digraphs by and BFS) to digraphs by traversing edges only along traversing edges only along their directiontheir directionIn the directed DFS In the directed DFS algorithm, we have four algorithm, we have four types of edgestypes of edges– discovery edgesdiscovery edges– back edgesback edges– forward edgesforward edges– cross edgescross edges
A directed DFS starting a a A directed DFS starting a a vertex vertex ss determines the determines the vertices reachable from vertices reachable from ss
A
C
E
B
D
66
ReachabilityReachability DFS DFS treetree rooted at v: vertices rooted at v: vertices reachable from v via directed reachable from v via directed pathspaths
A
C
E
B
D
F
A
C
E D
A
C
E
B
D
F
Strong Strong ConnectivityConnectivity
Each vertex Each vertex can reach all can reach all other verticesother vertices
a
d
c
b
e
f
g
77
Pick a vertex v in G.Pick a vertex v in G.
Perform a DFS from v in G.Perform a DFS from v in G.– If there’s a w not visited, print If there’s a w not visited, print
“no”.“no”.
Let G’ be G with edges Let G’ be G with edges reversed.reversed.
Perform a DFS from v in G’.Perform a DFS from v in G’.– If there’s a w not visited, print If there’s a w not visited, print
Maximal subgraphs such that each vertex Maximal subgraphs such that each vertex can reach all other vertices in the subgraphcan reach all other vertices in the subgraph
Can also be done in O(n+m) time using DFS, Can also be done in O(n+m) time using DFS, but is more complicated (similar to but is more complicated (similar to biconnectivity).biconnectivity).
Given a digraph Given a digraph GG, the , the transitive closure of transitive closure of GG is is the digraph the digraph G*G* such that such that– G*G* has the same has the same
vertices as vertices as GG– if if GG has a directed has a directed
path from path from uu to to v v ((u u vv), ), G*G* has a directed has a directed edge from edge from uu to to vv
The transitive closure The transitive closure provides reachability provides reachability information about a information about a digraphdigraph
B
A
D
C
E
B
A
D
C
E
G
G*
1010
Computing the Transitive ClosureComputing the Transitive ClosurePerform DFS starting at each vertex: O(n(n+m))Perform DFS starting at each vertex: O(n(n+m))Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm– If there's a way to get from A to B and from B to C, then
there's a way to get from A to C.– Idea #1: Number the vertices 1, 2, …, n.Idea #1: Number the vertices 1, 2, …, n.– Idea #2: Consider paths that use only vertices numbered 1, Idea #2: Consider paths that use only vertices numbered 1,
2, …, k, as intermediate vertices:2, …, k, as intermediate vertices:
k
j
i
Uses only verticesnumbered 1,…,k-1 Uses only vertices
numbered 1,…,k-1
Uses only vertices numbered 1,…,k(add this edge if it’s not already in)
1111
Floyd-Warshall’s AlgorithmFloyd-Warshall’s AlgorithmFloyd-Warshall’s algorithm Floyd-Warshall’s algorithm numbers the vertices of numbers the vertices of GG as as vv1 1 , …, v, …, vnn and computes a and computes a series of digraphs series of digraphs GG00, …, G, …, Gnn
– GG00==GG
– GGkk has a directed edge has a directed edge ((vvii, v, vjj) ) if if G G has a directed path has a directed path from from vvii to to vvjj with with intermediate vertices in the intermediate vertices in the set set {{vv1 1 , …, v, …, vkk}}
We have that We have that GGn n = = G*G*
In phase In phase kk, digraph , digraph GGkk is is computed from computed from GGk k 11
Running time: O(nRunning time: O(n33), ), assuming areAdjacent is assuming areAdjacent is O(1) (e.g., adjacency O(1) (e.g., adjacency matrix)matrix)
Algorithm FloydWarshall(G)Input digraph GOutput transitive closure G* of Gi 1for all v G.vertices()
denote v as vi
i i + 1G0 Gfor k 1 to n do
Gk Gk 1
for i 1 to n (i k) dofor j 1 to n (j i, k) do
if Gk 1.areAdjacent(vi, vk) Gk 1.areAdjacent(vk, vj)
if Gk.areAdjacent(vi, vj) Gk.insertDirectedEdge(vi, vj , k)
DAGs and Topological OrderingDAGs and Topological OrderingA directed acyclic graph (DAG) A directed acyclic graph (DAG) is a digraph that has no directed is a digraph that has no directed cyclescycles
A topological ordering of a A topological ordering of a digraph is a numbering digraph is a numbering
vv1 1 , …, v, …, vnn
of the vertices such that for of the vertices such that for every edge every edge ((vvi i , v, vjj)), we have , we have i i j j
Example: in a task scheduling Example: in a task scheduling digraph, a topological ordering digraph, a topological ordering a task sequence that satisfies a task sequence that satisfies the precedence constraintsthe precedence constraints
TheoremTheorem
A digraph admits a topological A digraph admits a topological ordering if and only if it is a ordering if and only if it is a DAGDAG
B
A
D
C
E
DAG G
B
A
D
C
E
Topological ordering of
G
v1
v2
v3
v4 v5
2121
write c.s. program
play
Topological SortingTopological SortingNumber vertices, so that (u,v) in E implies Number vertices, so that (u,v) in E implies u < vu < v
wake up
eat
nap
study computer sci.
more c.s.
work out
sleep
dream about graphs
A typical student day1
2 3
4 5
6
7
8
9
1011
make cookies for professors
2222
Note: This algorithm is different than Note: This algorithm is different than the one in Goodrich-Tamassiathe one in Goodrich-Tamassia
Running time: O(n + m). How…?Running time: O(n + m). How…?
Algorithm for Topological SortingAlgorithm for Topological Sorting
Method 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 edgesLabel v nn n - 1Remove v from H
2323
Topological Sorting Algorithm using DFSTopological Sorting Algorithm using DFS
Simulate the algorithm by Simulate the algorithm by using depth-first searchusing depth-first search
O(n+m) time.O(n+m) time.
Algorithm topologicalDFS(G, v)Input graph G and a start vertex v of G Output labeling of the vertices of G
in the connected component of v setLabel(v, VISITED)for all e G.incidentEdges(v)