1 Introduction
A graph is a directed acyclic graph, i.e. , if G is directed and has no cycles. Further if edges do not have any weights then the DAG is unweighted. In this article we consider only unweighted DAG. The algorithms can be easily extended to a weighted DAG. The longest path problem from a given source vertex is hard to compute on an undirected graph; however, the same can be computed in a DAG in time. The diameter in an undirected graph can be directly obtained by executing a BFS from each node in time [1, 2, 3, 4, 5]. We follow the standard terminology and call the maximum distance in a DAG as the diameter of denoted by . Likewise, the length of the longest path in a DAG is called its stretch denoted by . We show that stretch can be computed in linear time.
2 Diameter and Stretch
The following algorithm computes the stretch, i.e. of G. It employs dynamic programming with memoization.
Stretch Algorithm
-
Let denote the maximum length of any path from and is the corresponding function call. Let be the set of vertices with no incoming edges. Note that is the stretch of G.
-
Initialize: ; (all vertices are unvisited).
-
Execute .
-
-
LP(i) {
if () return ();
else
return ;
}
Time Complexity
Since every vertex is visited once, and is updated maximum as many times as the number of outgoing edges has, the overall complexity is
Diameter Algorithm
Let S ()) be set of vertices with no outgoing edges. For every vertex note the vertices from which it has an incoming edge. For every vertex , the set of vertices that can reach is . Let denote the distance from to .
-
While S is not empty
-
For all edges where .
;
if can be reached from where is an immediate successor of then
; -
Among the vertices that have an outgoing edge to some vertex in choose the vertex set in which all the vertices will have no outgoing edges once S is deleted.
-
Time Complexity
For every vertex and , where can be reached from , needs to be updated number of times, where is the number of outgoing edges from . Since, for every , there can vertices it can reach, and total number of edges is , and each edge implies that the child updates the parent in time, the total complexity is .
3 Layering
A DAG in which the lengths of all paths from to are identical is called as a balanced DAG.
Labeling is a many to one function that assigns non-negative integers to the vertices such that the following conditions hold for any pair of vertices. The label of any vertex that has an incoming path length of from a vertex is and the label of a vertex from which there is a path of length to is . The idea of labeling is to assign consistent layer numbers to the vertices. The layer indicates the relative depth of a vertex with respect to a vertex that has the lowest layer number.
The following algorithm labels a balanced DAG.
Label Algorithm1
We consider a DAG on which the stretch has been computed in linear time as shown earlier.
-
Initialize where INV is an invalid value. Let be the set of vertices that do not have an outgoing edge.
-
Label one of the nodes say in where with zero. That is, .
-
Note that such a vertex can be chosen in time by the Stretch Algorithm.
-
We define a priority queue
where each object corresponding to a vertex stores an ordered pair of integers
where is the label number of the vertex . -
.
-
Repeat while
-
Let the object with lowest priority in be . Pop from .
-
If any parent of is not labeled then
; ; -
If any child of is not labeled then
; ;
-
-
Let be
Analysis
The first time a vertex is visited, it is assigned a label.
If a label has been assigned to a vertex hen we know its distance from any vertex where can be reached from and is labeled.
At the end of the algorithm all labels are updated such that they are non-negative integers. A final label is the layer number of the corresponding vertex. Thus, we call this process as layering.
Time Complexity
Each edge is processed only once, and every vertex is pushed into only once. Therefore, accessing all vertices in takes time. Thus, time complexity of the labeling, given that the given graph is balanced DAG is .
Detection of Unbalanced DAG
If we apply the above algorithm to label a DAG. At any point, if we try to label a vertex which is already labeled, and the new label conflicts with the old label, we know that there exists at least one pair such that there are two paths from to with different path lengths. Time complexity of detection, therefore, is the same as the time complexity of labeling, i.e. .
Label Algorithm2
The input to this algorithm is a DAG on which the stretch has been computed. Let be the set of vertices that do not have an outgoing edge.
-
Initialize where INV is an invalid value.
-
Label one of the nodes say in where with zero. That is, .
-
Note that such a vertex can be chosen in time by the Stretch Algorithm.
-
Define
if
if
-
Call
-
Let be
Analysis
The first time a vertex is accessed, it is assigned a label. If at any point, a label is assigned to a vertex , we know it’s distance from any vertex where can be reached by and is labeled. Just before the algorithm terminates, all labels are updated such that they are non-negative integers.
Time Complexity
The Label function is called for every node only once, which means function calls. Also, every function call from a vertex examines its immediate successors’ and immediate descendants’ labels. So, the amortized time complexity for accessing labels = . Therefore total time complexity is .
Detection of Unbalanced DAG
Let the above algorithm be run to label a DAG. If a vertex which is already labeled is attempted to be labelled again and the new label conflicts with the existing label then the DAG is unbalanced. This is so because there exists at least one pair such that there are two paths from to with different path lengths. Time complexity of detection, therefore, is the same as the time complexity of labeling, i.e. .
4 Conclusion
We show that and of DAG can be computed in time and time respectively. Further, we define balanced DAG and design an efficient algorithm to layer a balanced DAG. A DAG that is not balanced does not have a well defined layering. One can either layer a balanced DAG or determine that the given DAG is unbalanced in time.
References
- [1] Cormen, Thomas H. and Leiserson, Charles E. and Rivest, Ronald L. and Stein, Clifford. Introduction to Algorithms, MIT Press, 2011.
- [2] Sedgewick, Robert and Wayne, Kevin. Algorithms, Addison-Wesley Professional, 2011.
- [3] Kleinberg, Jon and Tardos, Eva. Algorithm design, Pearson Education India, 2006.
- [4] Brassard, Gilles and Bratley, Paul. Algorithmics: theory & practice. Prentice-Hall, Inc.1988.
- [5] Dasgupta, Sanjoy and Papadimitriou, Christos H and Vazirani, Umesh. Algorithms, McGraw-Hill, Inc., 2006.
Comments
There are no comments yet.