
Inmathematics, particularlygraph theory, andcomputer science, adirected acyclic graph (DAG) is adirected graph with nodirected cycles. That is, it consists ofvertices andedges (also calledarcs), with each edge directed from one vertex to another, such that following those directions will never form a closed loop. A directed graph is a DAG if and only if it can betopologically ordered, by arranging the vertices as a linear ordering that is consistent with all edge directions. DAGs have numerous scientific and computational applications, ranging from biology (evolution, family trees, epidemiology) to information science (citation networks) to computation (scheduling).
Directed acyclic graphs are also calledacyclic directed graphs[1] oracyclic digraphs.[2]
Agraph is formed byvertices and byedges connecting pairs of vertices, where the vertices can be any kind of object that is connected in pairs by edges. In the case of adirected graph, each edge has an orientation, from one vertex to another vertex. Awalk in a directed graph is a (finite or infinite) sequence of vertices such that each consecutive pair is connected by a directed edge. Apath is a walk where all vertices are distinct. Acycle is a walk where the only repeated vertex is, meaning the last vertex equals the first vertex. A directed acyclic graph is a directed graph that has no cycles.[1][2][3]
Thereachability relation of a DAG can be formalized as apartial order≤ on the vertices of the DAG. In this partial order, two verticesu andv are ordered asu ≤v exactly when there exists a directed path fromu tov in the DAG; that is, whenu can reachv (orv is reachable fromu).[4] However, different DAGs may give rise to the same reachability relation and the same partial order.[5] For example, a DAG with two edgesu →v andv →w has the same reachability relation as the DAG with three edgesu →v,v →w, andu →w. Both of these DAGs produce the same partial order, in which the vertices are ordered asu ≤v ≤w.
Thetransitive closure of a DAG is the graph with the most edges that has the same reachability relation as the DAG. It has an edgeu →v for every pair of vertices (u,v) in the reachability relation≤ of the DAG, and may therefore be thought of as a direct translation of the reachability relation≤ into graph-theoretic terms. The same method of translating partial orders into DAGs works more generally: for every finite partially ordered set(S, ≤), the graph that has a vertex for every element ofS and an edge for every pair of elements in≤ is automatically a transitively closed DAG, and has(S, ≤) as its reachability relation. In this way, every finite partially ordered set can be represented as a DAG.

Thetransitive reduction of a DAG is the graph with the fewest edges that has the same reachability relation as the DAG. It has an edgeu →v for every pair of vertices (u,v) in thecovering relation of the reachability relation≤ of the DAG. It is a subgraph of the DAG, formed by discarding the edgesu →v for which the DAG also contains a longer directed path fromu tov.Like the transitive closure, the transitive reduction is uniquely defined for DAGs. In contrast, for a directed graph that is not acyclic, there can be more than one minimal subgraph with the same reachability relation.[6] Transitive reductions are useful in visualizing the partial orders they represent, because they have fewer edges than other graphs representing the same orders and therefore lead to simplergraph drawings. AHasse diagram of a partial order is a drawing of the transitive reduction in which the orientation of every edge is shown by placing the starting vertex of the edge in a lower position than its ending vertex.[7]
Atopological ordering of a directed graph is an ordering of its vertices into a sequence, such that for every edge the start vertex of the edge occurs earlier in the sequence than the ending vertex of the edge. A graph that has a topological ordering cannot have any cycles, because the edge into the earliest vertex of a cycle would have to be oriented the wrong way. Therefore, every graph with a topological ordering is acyclic. Conversely, every directed acyclic graph has at least one topological ordering. The existence of a topological ordering can therefore be used as an equivalent definition of a directed acyclic graphs: they are exactly the graphs that have topological orderings.[2]In general, this ordering is not unique; a DAG has a unique topological ordering if and only if it has a directed path containing all the vertices, in which case the ordering is the same as the order in which the vertices appear in the path.[8]
The family of topological orderings of a DAG is the same as the family oflinear extensions of the reachability relation for the DAG,[9] so any two graphs representing the same partial order have the same set of topological orders.
Thegraph enumeration problem of counting directed acyclic graphs was studied byRobinson (1973).[10]The number of DAGs onn labeled vertices, forn = 0, 1, 2, 3, … (without restrictions on the order in which these numbers appear in a topological ordering of the DAG) is
These numbers may be computed by therecurrence relation
Eric W. Weisstein conjectured,[11] andMcKay et al. (2004) proved, that the same numbers count the(0,1) matrices for which alleigenvalues are positivereal numbers. The proof isbijective: a matrixA is anadjacency matrix of a DAG if and only ifA + I is a (0,1) matrix with all eigenvalues positive, whereI denotes theidentity matrix. Because a DAG cannot haveself-loops, its adjacency matrix must have a zero diagonal, so addingI preserves the property that all matrix coefficients are 0 or 1.[12]
Amultitree (also called astrongly unambiguous graph or amangrove) is a DAG in which there is at most one directed path between any two vertices. Equivalently, it is a DAG in which the subgraph reachable from any vertex induces anundirected tree.[13]
Apolytree (also called adirected tree) is a multitree formed by orienting the edges of an undirected tree.[14]
Anarborescence is a polytree formed byorienting the edges of an undirected tree away from a particular vertex, called theroot of the arborescence.
Topological sorting is the algorithmic problem of finding a topological ordering of a given DAG. It can be solved inlinear time.[15] Kahn's algorithm for topological sorting builds the vertex ordering directly. It maintains a list of vertices that have no incoming edges from other vertices that have not already been included in the partially constructed topological ordering; initially this list consists of the vertices with no incoming edges at all. Then, it repeatedly adds one vertex from this list to the end of the partially constructed topological ordering, and checks whether its neighbors should be added to the list. The algorithm terminates when all vertices have been processed in this way.[16] Alternatively, a topological ordering may be constructed by reversing apostorder numbering of adepth-first search graph traversal.[15]
It is also possible to check whether a given directed graph is a DAG in linear time, either by attempting to find a topological ordering and then testing for each edge whether the resulting ordering is valid[17] or alternatively, for some topological sorting algorithms, by verifying that the algorithm successfully orders all the vertices without meeting an error condition.[16]
Any undirected graph may be made into a DAG by choosing atotal order for its vertices and directing every edge from the earlier endpoint in the order to the later endpoint. The resultingorientation of the edges is called anacyclic orientation. Different total orders may lead to the same acyclic orientation, so ann-vertex graph can have fewer thann! acyclic orientations. The number of acyclic orientations is equal to|χ(−1)|, whereχ is thechromatic polynomial of the given graph.[18]

Any directed graph may be made into a DAG by removing afeedback vertex set or afeedback arc set, a set of vertices or edges (respectively) that touches all cycles. However, the smallest such set isNP-hard to find.[19] An arbitrary directed graph may also be transformed into a DAG, called itscondensation, bycontracting each of itsstrongly connected components into a single supervertex.[20] When the graph is already acyclic, its smallest feedback vertex sets and feedback arc sets areempty, and its condensation is the graph itself.
The transitive closure of a given DAG, withn vertices andm edges, may be constructed in timeO(mn) by using eitherbreadth-first search ordepth-first search to test reachability from each vertex.[21] Alternatively, it can be solved in timeO(nω) whereω < 2.373 is theexponent for matrix multiplication algorithms; this is a theoretical improvement over theO(mn) bound fordense graphs.[22]
In all of these transitive closure algorithms, it is possible to distinguish pairs of vertices that are reachable by at least one path of length two or more from pairs that can only be connected by a length-one path. The transitive reduction consists of the edges that form length-one paths that are the only paths connecting their endpoints. Therefore, the transitive reduction can be constructed in the same asymptotic time bounds as the transitive closure.[23]
Theclosure problem takes as input a vertex-weighted directed acyclic graph and seeks the minimum (or maximum) weight of a closure – a set of verticesC, such that no edges leaveC. The problem may be formulated for directed graphs without the assumption of acyclicity, but with no greater generality, because in this case it is equivalent to the same problem on the condensation of the graph. It may be solved in polynomial time using a reduction to themaximum flow problem.[24]
Some algorithms become simpler when used on DAGs instead of general graphs, based on the principle of topological ordering. For example, it is possible to findshortest paths andlongest paths from a given starting vertex in DAGs in linear time byprocessing the vertices in a topological order, and calculating the path length for each vertex to be the minimum or maximum length obtained via any of its incoming edges.[25] In contrast, for arbitrary graphs the shortest path may require slower algorithms such asDijkstra's algorithm or theBellman–Ford algorithm,[26] and longest paths in arbitrary graphs areNP-hard to find.[27]
Directed acyclic graph representations of partial orderings have many applications inscheduling for systems of tasks with ordering constraints.[28]An important class of problems of this type concern collections of objects that need to be updated, such as the cells of aspreadsheet after one of the cells has been changed, or theobject files of a piece of computer software after itssource code has been changed.In this context, adependency graph is a graph that has a vertex for each object to be updated, and an edge connecting two objects whenever one of them needs to be updated earlier than the other. A cycle in this graph is called acircular dependency, and is generally not allowed, because there would be no way to consistently schedule the tasks involved in the cycle.Dependency graphs without circular dependencies form DAGs.[29]
For instance, when one cell of aspreadsheet changes, it is necessary to recalculate the values of other cells that depend directly or indirectly on the changed cell. For this problem, the tasks to be scheduled are the recalculations of the values of individual cells of the spreadsheet. Dependencies arise when an expression in one cell uses a value from another cell. In such a case, the value that is used must be recalculated earlier than the expression that uses it. Topologically ordering the dependency graph, and using this topological order to schedule the cell updates, allows the whole spreadsheet to be updated with only a single evaluation per cell.[30] Similar problems of task ordering arise inmakefiles for program compilation[30] andinstruction scheduling for low-level computer program optimization.[31]

A somewhat different DAG-based formulation of scheduling constraints is used by theprogram evaluation and review technique (PERT), a method for management of large human projects that was one of the first applications of DAGs. In this method, the vertices of a DAG representmilestones of a project rather than specific tasks to be performed. Instead, a task or activity is represented by an edge of a DAG, connecting two milestones that mark the beginning and completion of the task. Each such edge is labeled with an estimate for the amount of time that it will take a team of workers to perform the task. Thelongest path in this DAG represents thecritical path of the project, the one that controls the total time for the project. Individual milestones can be scheduled according to the lengths of the longest paths ending at their vertices.[32]
A directed acyclic graph may be used to represent a network of processing elements. In this representation, data enters a processing element through its incoming edges and leaves the element through its outgoing edges.
For instance, in electronic circuit design, staticcombinational logic blocks can be represented as an acyclic system oflogic gates that computes a function of an input, where the input and output of the function are represented as individualbits. In general, the output of these blocks cannot be used as the input unless it is captured by a register or state element which maintains its acyclic properties.[33] Electronic circuit schematics either on paper or in a database are a form of directed acyclic graphs using instances or components to form a directed reference to a lower level component. Electronic circuits themselves are not necessarily acyclic or directed.
Dataflow programming languages describe systems of operations ondata streams, and the connections between the outputs of some operations and the inputs of others. These languages can be convenient for describing repetitive data processing tasks, in which the same acyclically-connected collection of operations is applied to many data items. They can be executed as aparallel algorithm in which each operation is performed by a parallel process as soon as another set of inputs becomes available to it.[34]
Incompilers, straight line code (that is, sequences of statements without loops or conditional branches) may be represented by a DAG describing the inputs and outputs of each of the arithmetic operations performed within the code. This representation allows the compiler to performcommon subexpression elimination efficiently.[35] At a higher level of code organization, theacyclic dependencies principle states that the dependencies between modules or components of a large software system should form a directed acyclic graph.[36]
Feedforward neural networks are another example.
Graphs in which vertices represent events occurring at a definite time, and where the edges always point from an earlier time vertex to a later time vertex, are necessarily directed and acyclic. The lack of a cycle follows because the time associated with a vertex always increases as you follow any directedpath in the graph, so you can never return to a vertex on a path. This reflects our natural intuition that causality means events can only affect the future, they never affect the past, and thus we have nocausal loops. An example of this type of directed acyclic graph are those encountered in thecausal set approach to quantum gravity though in this case the graphs considered aretransitively complete. In the version history example below, each version of the software is associated with a unique time, typically the time the version was saved, committed or released. In the citation graph examples below, the documents are published at one time and can only refer to older documents.
Sometimes events are not associated with a specific physical time. Provided that pairs of events have a purely causal relationship, that is edges representcausal relations between the events, we will have a directed acyclic graph.[37] For instance, aBayesian network represents a system of probabilistic events as vertices in a directed acyclic graph, in which the likelihood of an event may be calculated from the likelihoods of its predecessors in the DAG.[38] In this context, themoral graph of a DAG is the undirected graph created by adding an (undirected) edge between all parents of the same vertex (sometimes calledmarrying), and then replacing all directed edges by undirected edges.[39] Another type of graph with a similar causal structure is aninfluence diagram, the vertices of which represent either decisions to be made or unknown information, and the edges of which represent causal influences from one vertex to another.[40] Inepidemiology, for instance, these diagrams are often used to estimate the expected value of different choices for intervention.[41][42]
The converse is also true. That is in any application represented by a directed acyclic graph there is a causal structure, either an explicit order or time in the example or an order which can be derived from graph structure. This follows because all directed acyclic graphs have atopological ordering, i.e. there is at least one way to put the vertices in an order such that all edges point in the same direction along that order.

Family trees may be seen as directed acyclic graphs, with a vertex for each family member and an edge for each parent-child relationship.[43] Despite the name, these graphs are not necessarily trees because of the possibility of marriages between relatives (so a child has a common ancestor on both the mother's and father's side) causingpedigree collapse.[44] The graphs ofmatrilineal descent (mother-daughter relationships) andpatrilineal descent (father-son relationships) are trees within this graph. Because no one can become their own ancestor, family trees are acyclic.[45]
The version history of adistributed revision control system, such asGit, generally has the structure of a directed acyclic graph, in which there is a vertex for each revision and an edge connecting pairs of revisions that were directly derived from each other. These are not trees in general due to merges.[46]
In manyrandomizedalgorithms incomputational geometry, the algorithm maintains ahistory DAG representing the version history of a geometric structure over the course of a sequence of changes to the structure. For instance in arandomized incremental algorithm forDelaunay triangulation, the triangulation changes by replacing one triangle by three smaller triangles when each point is added, and by "flip" operations that replace pairs of triangles by a different pair of triangles. The history DAG for this algorithm has a vertex for each triangle constructed as part of the algorithm, and edges from each triangle to the two or three other triangles that replace it. This structure allowspoint location queries to be answered efficiently: to find the location of a query pointq in the Delaunay triangulation, follow a path in the history DAG, at each step moving to the replacement triangle that containsq. The final triangle reached in this path must be the Delaunay triangle that containsq.[47]
In acitation graph the vertices are documents with a single publication date. The edges represent the citations from the bibliography of one document to other necessarily earlier documents. The classic example comes from the citations between academic papers as pointed out in the 1965 article "Networks of Scientific Papers"[48] byDerek J. de Solla Price who went on to produce the first model of a citation network, thePrice model.[49] In this case thecitation count of a paper is just the in-degree of the corresponding vertex of the citation network. This is an important measure incitation analysis.Court judgements provide another example as judges support their conclusions in one case by recalling other earlier decisions made in previous cases. A final example is provided by patents which must refer to earlierprior art, earlier patents which are relevant to the current patent claim. By taking the special properties of directed acyclic graphs into account, one can analyse citation networks with techniques not available when analysing the general graphs considered in many studies usingnetwork analysis. For instancetransitive reduction gives new insights into the citation distributions found in different applications highlighting clear differences in the mechanisms creating citations networks in different contexts.[50] Another technique ismain path analysis, which traces the citation links and suggests the most significant citation chains in a givencitation graph.
ThePrice model is too simple to be a realistic model of acitation network but it is simple enough to allow for analytic solutions for some of its properties. Many of these can be found by using results derived from the undirected version of thePrice model, theBarabási–Albert model. However, sincePrice's model gives a directed acyclic graph, it is a useful model when looking for analytic calculations of properties unique to directed acyclic graphs. For instance,the length of the longest path, from the n-th node added to the network to the first node in the network, scales as[51].
Directed acyclic graphs may also be used as acompact representation of a collection of sequences. In this type of application, one finds a DAG in which the paths form the given sequences. When many of the sequences share the same subsequences, these shared subsequences can be represented by a shared part of the DAG, allowing the representation to use less space than it would take to list out all of the sequences separately. For example, thedirected acyclic word graph is adata structure in computer science formed by a directed acyclic graph with a single source and with edges labeled by letters or symbols; the paths from the source to the sinks in this graph represent a set ofstrings, such as English words.[52] Any set of sequences can be represented as paths in a tree, by forming a tree vertex for every prefix of a sequence and making the parent of one of these vertices represent the sequence with one fewer element; the tree formed in this way for a set of strings is called atrie. A directed acyclic word graph saves space over a trie by allowing paths to diverge and rejoin, so that a set of words with the same possible suffixes can be represented by a single tree vertex.[53]
The same idea of using a DAG to represent a family of paths occurs in thebinary decision diagram,[54][55] a DAG-based data structure for representing binary functions. In a binary decision diagram, each non-sink vertex is labeled by the name of a binary variable, and each sink and each edge is labeled by a 0 or 1. The function value for anytruth assignment to the variables is the value at the sink found by following a path, starting from the single source vertex, that at each non-sink vertex follows the outgoing edge labeled with the value of that vertex's variable. Just as directed acyclic word graphs can be viewed as a compressed form of tries, binary decision diagrams can be viewed as compressed forms ofdecision trees that save space by allowing paths to rejoin when they agree on the results of all remaining decisions.[56]
{{cite web}}: CS1 maint: overridden setting (link){{cite book}}: CS1 maint: overridden setting (link) Section 22.4, Topological sort, pp. 549–552.{{cite web}}: CS1 maint: overridden setting (link)