Is the following figure DAG? - data-structures

I have been through multiple definitions of DAG and all of them say that it is a directed graph without cycles. Also, it is said that it has topological ordering.
Now the following figure is directed graph and does not have cycles.
But looking at the ordering of edges (look at edge (2,1)), it is not topologically ordered.
Is this still a DAG or every edge must be topologically ordered for this graph to be a DAG??

Yes this is a DAG. To check: you cannot go from one node and reach it again following the edges in the graph. As a matter of fact, the graph is complete directed acyclic graph in a sense that it holds the maximum number of possible edges in a DAG of 3 nodes. Adding any new edge would turn it into a cyclic digraph.
For complete DAGs, there is only one topological order. In your case it is 0>2>1.
An informal way to generate a topological ordering:
pick the source -- the one with zero incoming edges (in case there
are many, choose anyone).
Remove it from the graph (with its outgoing edges of course).
Repeat until you end up with empty graph.

Related

Cycles between two vertices in a directed graph

I know that in an undirected graph you have to have at least three vertices to form a cycle. My question is, in a directed graph, is it considered a cycle if two vertices have two edges pointing to each other?
Here is an example:
Is this a cyclic graph?
Related questions:
In an undirected graph, the simplest cycle must have 3 nodes?
Existence of cycle between any two vertices of graph
Cycles in an Undirected Graph
A graph has a cycle if there is a non-empty path that originates at some vertex and ends at the same vertex. In your graph above, you have a cycle on path A -> C -> A. Similarly, let's imagine a directed graph with 2 vertices A and B and 2 edges AB and BA (where the first letter is the source vertex). This means that there is a cycle A -> B -> A, thus you can have a cycle in a directed graph of 2 vertices.
I would say it (A-C-A) is a cycle. But I am from a different perspective: you may know that for a directed acyclic graph (dag), there is a topological sorting on it; otherwise, there isn't.
Topological sorting is indeed the linear extension of a partial order <=. Thus, dag is the graphical representation of a partial order <=. Be aware that according to the anti-symmetry property of a partial order <= (i.e., if a<=b and b<=a, then a=b), there is no possibility that two edges (a,b) and (b,a) simultaneously exist between two distinct vertices a and b.
In summary, no cycle => exists topological sorting, since no topological sorting on your digraph, thus there must be a cycle (A-C-A).
No,it is not considered a cycle if two vertices have two edges pointing to each other in directed graph. They are called Parallel Edges.
According to this definition 1:
A circuit is a closed trail with at least one edge
A-C is considered a circuit.
A-C also complies with this definition2:
A cycle is a circuit in which no vertex except the first (which is
also the last) appears more than once.
so it is also a cycle.
1 source: https://proofwiki.org/wiki/Definition:Circuit
2 source: https://proofwiki.org/wiki/Definition:Cycle_(Graph_Theory)

Reason why all DAG have more than one topological sort order

I am wondering as to why all Directed Acyclic Graph have more than one topological sort order.
I have searched up google and saying most of it just breeze through the fact that they have at least one topo sort. But i am thinking along the lines of how a singly linked list is implemented :
A -> B -> C -> D
This might mean that there is only one way the toposort can technically go through - D, C, B, A...
However, it may be the case that that is not a directed acyclic graph but i am not sure how to refute the case since it is directed (A to B, etc) , Acyclic (There are no cycles back to any start) Graph (it is technically a tree)..
Thank you so much for any clarifications provided !
It's not true that all DAGs have more than one topological sort. Remember that we can construct a topological sort by removing vertices with no incoming edges in order.
Consider a DAG that contains a continuous path that connects all its vertices (Note that this path does not form a cycle, otherwise it won't be a DAG). We can start by removing a vertex with no incoming edge and repeat. We'll find that the topological sort has an edge between each consecutive pair of vertices. If we wanted to form another topological sort, we could have started by removing some other vertex with no incoming edge, but this would mean that there are at least 2 edges with no incoming edges and in that case, it would be impossible to start a path from one vertex and connect all others.
Since we started with a DAG having a path connecting all the vertices, we are met with a contradiction. Hence, it is proven that a DAG with a path connecting all the vertices will have a unique topological sort.

Can there be multiple parents AND multiple roots for a Directed Acyclic Graph?

Can there be multiple parents and/or multiple roots for a DAG?
A DAG is a graph that flows in one direction, where no element can be a child of itself. You can still have multiple children and multiple parents for a single node of the graph.
A graph is formed by a collection of vertices and edges, where the
vertices are structureless objects that are connected in pairs by
edges. In the case of a directed graph, each edge has an orientation,
from one vertex to another vertex. A path in a directed graph can be
described by a sequence of edges having the property that the ending
vertex of each edge in the sequence is the same as the starting vertex
of the next edge in the sequence; a path forms a cycle if the starting
vertex of its first edge equals the ending vertex of its last edge. A
directed acyclic graph is a directed graph that has no cycles.
Source: Wikipedia
At the very minimum, a directed acyclic graph must have:
Nodes: A place to store the data.
Directed Edges: Arrows that point in one direction (the thing that makes this data structure different)
Some great ancestral node with no parents. (Fun fact: Most ancestry trees are actually DAGs and not actually trees because cousins at some point get married to each other.)
Leaves: Nodes with no children
Nothing in a DAG prevents a node from having more than one parent. Similarly, nothing prevents a DAG from having multiple roots. Thus, yes, you can have these two features in a DAG.

How can I convert an undirected graph to directed graph with no cycle (Directed acyclic graph)

I have an undirected graph and I wish to transform it to a directed graph. I will have few constraint, such as already having some directed relations.
You are actually trying to build a DAG from your infrastructure graph. Note that a directed graph is a DAG if and only if it can be topologically sorted.
So, let's go from the end to the beginning. First create the topological sort, and then connect nodes in a way that obey the sort.
First, remove all "undetermined" edges, and keep only those you already have directions for.
Then, do topological sort on the graph and find some topological sort (If there is none, the problem cannot be solved with the given restrictions).
Based on it, from first to last set the direction of each edge such that the source node is the one with the lower topological sort.
Time complexity of the algorithm is linear in the number of nodes and edges.

Determining whether or not a directed or undirected graph is a tree

I would like to know of a fast algorithm to determine if a directed or undirected graph is a tree.
This post seems to deal with it, but it is not very clear; according to this link, if the graph is acyclic, then it is a tree. But if you consider the directed and undirected graphs below: in my opinion, only graphs 1 and 4 are trees. I suppose 3 is neither cyclic, nor a tree.
What needs to be checked to see if a directed or undirected graph is a tree or not, in an efficient way? And taking it one step ahead: if a tree exists then is it a binary tree or not?
For a directed graph:
Find the vertex with no incoming edges (if there is more than one or no such vertex, fail).
Do a breadth-first or depth-first search from that vertex. If you encounter an already visited vertex, it's not a tree.
If you're done and there are unexplored vertices, it's not a tree - the graph is not connected.
Otherwise, it's a tree.
To check for a binary tree, additionally check if each vertex has at most 2 outgoing edges.
For an undirected graph:
Check for a cycle with a simple depth-first search (starting from any vertex) - "If an unexplored edge leads to a node visited before, then the graph contains a cycle." If there's a cycle, it's not a tree.
If the above process leaves some vertices unexplored, it's not a tree, because it's not connected.
Otherwise, it's a tree.
To check for a binary tree, if the graph has more than one vertex, additionally check that all vertices have 1-3 edges (1 to the parent and 2 to the children).
Checking for the root, i.e. whether one vertex contains 1-2 edges, is not necessary as there has to be vertices with 1-2 edges in an acyclic connected undirected graph.
Note that identifying the root is not generically possible (it may be possible in special cases) as, in many undirected graphs, more than one of the nodes can be made the root if we were to make it a binary tree.
If an undirected given graph is a tree:
the graph is connected
the number of edges equals the number of nodes - 1.
An undirected graph is a tree when the following two conditions are true:
The graph is a connected graph.
The graph does not have a cycle.
A directed graph is a tree when the following three conditions are true:
The graph is a connected graph.
The graph does not have a cycle.
Each node except root should have exactly one parent.

Resources