# Distances in and Layering of a DAG

The diameter of an undirected unweighted graph G=(V,E) is the maximum value of the distance from any vertex u to another vertex v for u,v ∈ V where distance i.e. d(u,v) is the length of the shortest path from u to v in G. DAG, is a directed graph without a cycle. We denote the diameter of an unweighted DAG G=(V,E) by δ (G). The stretch of a DAG G is the length of longest path from u to v in G, for all choices of (u, v) ∈ V denoted by Δ (G). The diameter of an undirected graph can be computed in O(|V|(|V|+|E|)) time by executing breadth first search |V| times. We show that stretch and diameter of a DAG can be computed in O(|V|+|E|) time and O(|V||E|) time respectively. A DAG is balanced if and only if a consistent assignment of level numbers to all vertices is possible. Layering refers to such an assignment. A balanced DAG is defined. An efficient algorithm that either detects whether a given DAG is unbalanced or layers it otherwise is designed with a running time of O(|V|+|E|). Key words: Diameter, directed acyclic graph, longest directed path, graph algorithms, complexity.

Comments

There are no comments yet.

## Authors

• 4 publications
• 1 publication
01/10/2022

### Detours in Directed Graphs

We study two "above guarantee" versions of the classical Longest Path pr...
02/27/2018

### Parameterized Complexity of Diameter

Diameter--the task of computing the length of a longest shortest path---...
03/08/2022

### Oriented Diameter of Planar Triangulations

The diameter of an undirected or a directed graph is defined to be the m...
01/07/2021

### 4 vs 7 sparse undirected unweighted Diameter is SETH-hard at time n^4/3

We show, assuming the Strong Exponential Time Hypothesis, that for every...
05/18/2021

### On Oriented Diameter of (n, k)-Star Graphs

Assignment of one of the two possible directions to every edge of an und...
11/23/2019

### Oriented Diameter of Star Graphs

An orientation of an undirected graph G is an assignment of exactly one...
05/31/2020

### Filling in pattern designs for incomplete pairwise comparison matrices: (quasi-)regular graphs with minimal diameter

Multicriteria Decision Making problems are important both for individual...
##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 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.