lecture 33: directed graph connectivity
DESCRIPTION
CSC 213 – Large Scale Programming. Lecture 33: Directed Graph Connectivity. Today’s Goals. Examine new properties of DirectedGraph s What reaching & reachable mean for a Graph How humans go about computing these properties Algorithms for computers to compute these - PowerPoint PPT PresentationTRANSCRIPT
LECTURE 33:DIRECTED GRAPH CONNECTIVITY
CSC 213 – Large Scale Programming
Today’s Goals
Examine new properties of DirectedGraphs What reaching & reachable mean for a Graph
How humans go about computing these properties
Algorithms for computers to compute these Examine what meant by DAG and why
you care Simple ways to develop schedules will be
examined How can algorithm tell when a schedule
impossible
Directed Graph
Only directed edges Replace undirected
edge with 2 directed edges
Relationships go in only one direction One-way streets Flights Scheduling
A
C
E
B
D
Directed Graph
Only directed edges Replace undirected
edge with 2 directed edges
Relationships go in only one direction One-way streets Flights Scheduling Talking to your ex
Directed Graph Properties
Each edge goes one-way (a,b) connects a to b (a,b) does not connect b to a
Can discuss in-edges & out-edges (a,b) is out-edge for a (a,b) is in-edge for b
Adjacency-based Graph classes can change Use 2 Sequences for adjacency-list vertices Define source & target dimension in adjacency-
matrixincidentEdges returns both in-edges & out-edges
a
c
e
b
d
Reachability
e, a, d reachable from c
a
c
e
b
d
f
Reachability
e, a, d reachable from c c reaches e
a
c
e
b
d
f
Reachability
e, a, d reachable from c c reaches e c reaches e & e incident upon d, a
a
c
e
b
d
f
Reachability
e, a, d reachable from c c reaches e c reaches e & e is incident upon d, a d, a out-edges to c only
a
c
e
b
d
f
Reachability
a, c, d, e, f reachable from b
a
c
e
b
d
f
Reachability
a, c, d, e, f reachable from b Path exists from b to every vertex
a
c
e
b
d
f
Reachability
a, c, d, e, f reachable from b Path exists from b to every vertex Actually have multiple paths to most
vertices
a
c
e
b
d
f
Transitive Closure of G
Transitive closure of G usually written as G*
b
a
d
c
e
G G*
Transitive Closure of G
Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however
b
a
d
c
e
G G*
b
a
d
c
e
Transitive Closure of G
Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however
Edge in G* if target reachable from source in G
b
a
d
c
e
G G*
b
a
d
c
e
Transitive Closure of G
Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however
Edge in G* if target reachable from source in G
b
a
d
c
e
G G*
b
a
d
c
e
Transitive Closure of G
Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however
Edge in G* if target reachable from source in G
b
a
d
c
e
G G*
b
a
d
c
e
Transitive Closure of G
Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however
Edge in G* if target reachable from source in G
b
a
d
c
e
G G*
b
a
d
c
e
Transitive Closure of G
Transitive closure of G usually written as G* Identical vertex sets in G & G* G & G* have different edge sets, however
Edge in G* if target reachable from source in G
b
a
d
c
e
G G*
b
a
d
c
e
Computing Transitive Closure Use dynamic programming to compute
this This solution known as Floyd-Warshall
Algorithm But how fast is it?
Floyd-Warshall’s Algorithm
Number G’s vertices from 1 to n Algorithm will compute n directed graphs Set G0=G to initialize this algorithm Graph of transitive closure is end result (Gn
= G*) All n directed graphs have same
vertices Gk contains all edges in Gk-1 (and Gk-2, Gk-3 ,…,
G0) Gk also has edge (vi,vj) if edges (vi,vk) & (vk,vj)
in Gk-1
Takes O(n3) time with adjacency matrix Better to use “brute force” if few edges
exist
Floyd-Warshall’s Algorithm
Number G’s vertices from 1 to n Algorithm will compute n directed graphs Set G0=G to initialize this algorithm Graph of transitive closure is end result (Gn
= G*) All n directed graphs have same
vertices Gk contains all edges in Gk-1 (and Gk-2, Gk-3 ,…,
G0) Gk also has edge (vi,vj) if edges (vi,vk) & (vk,vj)
in Gk-1
Takes O(n3) time with adjacency matrix Better to use “brute force” if few edges
exist
Floyd-Warshall Example – G0
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G1
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G1
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G1
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G1
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G1
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G2
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G2
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G3
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G3
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G3
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G3
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G3
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G4
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G4
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G4
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G4
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G4
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G4
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G5
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G5
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G5
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G5
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G6
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G6
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G7
V6
V7
V5
V4
V1
V3
V2
Floyd-Warshall Example – G*
V6
V7
V5
V4
V1
V3
V2
Directed Acyclic Graph
Often called a DAG Number & sort
vertices Topological order
found Each edge (vi,vj) has
i < j Finds valid
schedules… …or proves cannot
exist!
b
a
d
c
e
G (is a DAG)
b
a
d
c
e
A valid ordering of G
1
2
3
4 5
Directed Acyclic Graph
Often called a DAG Number & sort
vertices Topological order
found Each edge (vi,vj) has
i < j Finds valid
schedules… …or proves cannot
exist!
b
a
d
c
e
G (is a DAG)
b
a
d
c
e
Another valid ordering of G
2
1
3
4 5
Topological Sorting
Edges connect smaller to larger vertex numberswake up
studyeat
class study
homeworkwork
study
go to bed dream about classwork
Professor’s expectation of student’s day
1
2 3
4 5
67
8
9 10
Topological Sort Algorithm
Algorithm topologicalSort(Graph<V,E> G) H // Make a copy of Gm new Vertex[G.numVertices()]stack new …Stack<Vertex>() // Push onto stack any vertex in H with no outgoing edges n G.numVertices() while n ≥ 1 do
v stack.pop() n n – 1m[n] v
foreach e in v.incidentEdges() ≠ 0 do w H.opposite(e, v)
// Remove e from H if w has no outgoing edges then stack.push(w) return m
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
Topological Sorting Example
1
7
4
8
56
2
3
9
For Next Lecture
Weekly assignment out & due tomorrow
Programming assignment #3 plans due Friday Please do not wait to start working on
these ideas For Wednesday, read 13.5.2-13.5.3 &
13.6.2 Find quickest way to get someplace, can it
be done? How can we find the spanning tree that
weighs least?