## How to add edges to a directed acyclic graph without creating cycles

topological sort

directed acyclic graph - geeksforgeeks

dag

directed acyclic graph vs tree

directed acyclic graph data structure

topological sort c++

directed acyclic graph example

I have a graph with directed and undirected edges, now I want to get rid of the undirected edges by replacing them with directed edges (each undirected edge becomes one directed edge). There are two possibilies for each undirected edge (replace it with a directed edge in one direction or the other direction).

How to determine the direction of the undirected edges so that my graph stays acyclic?

My approach:

Create a graph with only the directed edges and add the undirected edges 1 by 1 (as directed edges) later on. Now I have a DAG, my problem is reduced to adding directed edges to the graph while maintaining the DAG properties (only directed edges, no cycles).

How to add an edge to a DAG and be sure that the resulting graph is also a DAG?

Let's try to solve this problem by looking at an example:-

Suppose I have the following set of vertices as directed ones:-

(1,2) (2,3) (4,5) (5,6) (7,3)

And we following set of vertices with undirected ones:-

(3,4) (6,7)

So now if we create our graph on paper, it should look something like this

1 -> 2 -> 3 / \ / 4 -> 5 -> 6 / / \ -----------------7

So you can clearly see there are two undirected edges present which we want to replace with directed ones,

So we can pick the first undirected pair as (3,4) and place a vertex 3 -> 4 and after this we will call dfs(1), and if find a cycle then (3,4) is not a valid one, we will then place as 4 -> 3 but in our case 3 -> 4 doesn't cause any cycles.

We move on to our next pair called (6,7) and placing 6 -> 7 causes a cycle, so we place 7 -> 6 and this gives us a DAG.

This is like a brute force solution. Let me give it a more thought, is there any better approach to this problem.

**Assign directions to edges so that the directed graph remains acyclic ,** It is given that the directed edges don't form cycle. How to assign directions to undirected edges so that the graph (with all directed edges) remains acyclic even � Let G = (V,E) be a Directed Acyclic Graph (DAG), with nodes V and edges E, and let U be a subset of V. How do we find a clustering with the following requirements: Only nodes V\U can be clustered; The nodes in a cluster must be connected in the graph induced by V\U; The resulting clustered graph is a DAG;

Build the initial DAG with all directed edges you have. Topo-sort it. Extend the partial order imposed by sorting to a total order (e.g. arrange vertices in a level lattice and enumerate them level by level). Notice that all edges go from the smaller vertex to a larger one.

Now direct your undirected edges according to the total order (from smaller vertex to a larger one). It is easy to see that the resulting graph has no cycles (for a cycle to exist there must be an edge going in the opposite direction).

**Maximum edges that can be added to DAG so that is remains DAG ,** A DAG is given to us, we need to find maximum number of edges that can be added to this DAG, maximal number of edges, adding even single edge will create a cycle in graph. Prints all edges that can be added without making any cycle. Adding an edge (i,j) is simple operation. If i and j wasn't connected before, than for each a from T(i) and each b from F(j) set matrix element (a,b) to true. But operation isn't cheap. In worst case it is O(|V|^2). That is in case of directed line, and adding edge from end to start vertex makes all vertices connected to all other vertices.

This worked out for me:

Do a topological sort on the graph without the undirected edges, add the undirected edges one by one (make them point from larger topo-vlalue to smaller).

This way it is guaranteed that the DAG will stay a DAG after adding an edge.

@user58697, there is no need to extend the partial order since there is a total order present after topologically sorting the graph. Each node's topo-value is comparable to other topo-values.

**Directed Graphs,** A directed acyclic graph (or DAG) is a digraph with no directed cycles. Hint: Create a digraph with an edge from box i to box j if box i nests� 1 If it was an undirected graph, the answer would be n − 1. For a directed graph, you can definitely fit more edges. a simple counterexample is a triangle with two of the edges directed clockwise and one counterclockwise. This has no cycles and n = 33 vertices and 3 edges, more than n − 1 = 2 edges.

**Add a link to a DAG without creating a cycle**

- Pick a random node in a DAG where a new link would direct to. Let this be a
`toNode`

. - Connect
`toNode`

to any other node in the graph that is not in a subtree of that`toNode`

.

It never creates a cycle.

**Directed acyclic graph,** In mathematics, particularly graph theory, and computer science, a directed acyclic graph is a A directed acyclic graph is a directed graph that has no cycles. graph of a DAG is the undirected graph created by adding an ( undirected) edge� A DAG is given to us, we need to find maximum number of edges that can be added to this DAG, after which new graph still remain a DAG that means the reformed graph should have maximal number of edges, adding even single edge will create a cycle in graph. The Output for above example should be following edges in any order.

**Maximum number of edges in a directed graph on $n$ vertices ,** What is the maximum number of edges in a directed graph with n vertices (which has no cycles). Logically it should be n−1, however I don't know how to prove this � A directed acyclic graph is a directed graph that has no cycles. [1][2][3] Adding the red edges to the blue directed acyclic graph produces another DAG, the transitive closureof the blue graph. For each red or blue edge uv, vis reachablefrom u: there exists a blue path starting at uand ending at v.

**Directed Acyclic Graph (DAG): topological ordering,** Directed graphs without cycles are called Directed Accylic Graphs (DAGs) You cannot start the task "build the frame" before completing the task "pour the G1 is also acyclic (we cannot add cycles to a graph by removing edges), and there� A directed acyclic graph is, as its name would suggested, directed, but without any cycles. The golden rule of DAGs is that, if we start at any node in the graph, no sequence of edges will allow

**Google Interview Question: if we had a list of n nodes, ,** edges there can be for a directed acyclic graph? Graph with 3 vertexes, has at most 2 edges before creating a cycle (adding third edge would create a triangle� An Introduction to Directed Acyclic Graphs Malcolm Barrett 2020-02-12. A quick note on terminology: I use the terms confounding and selection bias below, the terms of choice in epidemiology. The terms, however, depend on the field. In some fields, confounding is referred to as omitted variable bias or selection bias.

##### Comments

- This solution is too inefficient.
- I tried topo-sort and then used the values assigned to the nodes to determine the direction (direction is always from smaller value to larger). This didn't work for all of my examples. What do you mean by imposing a total order?
- After a top sort, some vertices remain incomparable (i.e. only a partial order exists). You want a total order (each pair of vertices is comparable), compatible with the partial order you already have. See stackoverflow.com/questions/3420685/… for inspiration.
- Why do some vertices remain incomparable? I don't understand why topo-sort produces a partial order and not a total one (can't I just compare all the values which topo-sort assigns with each other?)
- @AnnaVopureta Consider a graph
`A->B, A->C`

. It doesn't enforce any order between`B`

and`C`

.