Академический Документы
Профессиональный Документы
Культура Документы
CSC 3102
Digraph
A directed graph or digraph is a graph with
a c
d e
a b c
d e
Digraph - Example
A part-time student needs to take a set of five courses
{C1, C2, C3, C4, C5}, only one course per term, in any order as long as the following course prerequisites are met:
C1 and C2 have no prerequisites C3 requires C1 and C2 C4 requires C3 C5 requires C3 and C4.
C1 C3 C2
C4
The situation can be modeled by a diagraph: Vertices represent courses. Directed edges indicate prerequisite requirements. Topological sorting problem: How to list the vertices
C5
in such an order that, for every edge (u,v) in the graph, the vertex (u) where the edge starts is listed before the vertex (v) where the edge ends.
Find an ordering of digraphs vertices from left to right. Solution is possible only if digraph is a dag. This is a
an ordering of vertices.
Two algorithms for solving the problem. They may give different
(alternative) solutions. DFS-based algorithm Perform DFS traversal and note the order in which vertices become dead ends (that is, are popped of the traversal stack). Reversing this order yields the desired solution, provided that no back edge has been encountered during the traversal. Source removal algorithm Identify a source, which is a vertex with no incoming edges and delete it along with all edges outgoing from it. There must be at least one source to have the problem solved. Repeat this process in a remaining diagraph. The order in which the vertices are deleted yields the desired solution.
CSC 3102
4
C25
C5
DFS traversal stack with the subscript numbers indicating the popping off order. All edges in the sorted list point from left to right. Time efficiency is in O(|V2|) for the adjacency matrix representation and O(|V |+|E |) for the adjacency linked list representation. Since the reversing requires only (|V |) and it can stop before processing the entire digraph if a back edge is encountered.
5
CSC 3102
C5
C5
If there are more than one sources (C1 and C2), break the tie arbitrarily. On each iteration, a vertex with no incoming edges is deleted from the digraph.
6