CSC 213 – Large Scale Programming
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 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
Directed Graph Properties Each edge goes one-way (a,b) connects a to b does not connect (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-matrix incidentEdges 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 GG*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 GG*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 GG*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 GG*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 GG*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 GG*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 GG*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 G 0 =G to initialize this algorithm Graph of transitive closure is end result ( G n = G* ) All n directed graphs have same vertices G k contains all edges in G k-1 (and G k-2, G k-3,…, G 0 ) G k also has edge ( v i, v j ) if edges ( v i, v k ) & ( v k, v j ) in G k-1 Takes O(n 3 ) 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 G 0 =G to initialize this algorithm Graph of transitive closure is end result ( G n = G* ) All n directed graphs have same vertices G k contains all edges in G k-1 (and G k-2, G k-3,…, G 0 ) G k also has edge ( v i, v j ) if edges ( v i, v k ) & ( v k, v j ) in G k-1 Takes O(n 3 ) time with adjacency matrix Better to use “brute force” if few edges exist
Floyd-Warshall Example – G 0 V6V6 V7V7 V5V5 V4V4 V1V1 V3V3 V2V2
V6V6 V7V7 V5V5 V4V4 V3V3 V2V2
V6V6 V7V7 V5V5 V4V4 V3V3 V2V2
V6V6 V7V7 V5V5 V4V4 V3V3 V2V2
V6V6 V7V7 V5V5 V4V4 V3V3 V2V2
V6V6 V7V7 V5V5 V4V4 V1V1 V3V3 V2V2
V6V6 V7V7 V5V5 V4V4 V1V1 V3V3
V6V6 V7V7 V5V5 V4V4 V1V1 V3V3
V6V6 V7V7 V5V5 V4V4 V1V1 V2V2
V6V6 V7V7 V5V5 V4V4 V1V1 V2V2
V6V6 V7V7 V5V5 V4V4 V1V1 V2V2
V6V6 V7V7 V5V5 V4V4 V1V1 V2V2
V6V6 V7V7 V5V5 V4V4 V1V1 V2V2
V6V6 V7V7 V5V5 V1V1 V3V3 V2V2
V6V6 V7V7 V5V5 V1V1 V3V3 V2V2
V6V6 V7V7 V5V5 V1V1 V3V3 V2V2
V6V6 V7V7 V5V5 V1V1 V3V3 V2V2
V6V6 V7V7 V5V5 V1V1 V3V3 V2V2
V6V6 V7V7 V5V5 V1V1 V3V3 V2V2
V6V6 V7V7 V4V4 V1V1 V3V3 V2V2
V6V6 V7V7 V4V4 V1V1 V3V3 V2V2
V6V6 V7V7 V4V4 V1V1 V3V3 V2V2
V6V6 V7V7 V4V4 V1V1 V3V3 V2V2
V7V7 V5V5 V4V4 V1V1 V3V3 V2V2
V7V7 V5V5 V4V4 V1V1 V3V3 V2V2
V6V6 V5V5 V4V4 V1V1 V3V3 V2V2
Floyd-Warshall Example – G * V6V6 V7V7 V5V5 V4V4 V1V1 V3V3 V2V2
DAG Directed Acyclic Graph Often called a DAG Number & sort vertices Topological order found Each edge ( v i, v j ) 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
DAG Directed Acyclic Graph Often called a DAG Number & sort vertices Topological order found Each edge ( v i, v j ) 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
Topological Sorting Edges connect smaller to larger vertex numbers wake up study eat class study homework work study go to bed dream about classwork Professor’s expectation of student’s day
Topological Sort Algorithm Algorithm topologicalSort( Graph G) H // Make a copy of G m new Vertex[ G.numVertices() ] stack new … Stack () // Push onto stack any vertex in H with no outgoing edges n G.numVertices() while n ≥ 1 do v stack.pop() n n – 1 m[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
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 & Find quickest way to get someplace, can it be done? How can we find the spanning tree that weighs least?