Euler Circuit in a Directed Graph - algorithm

How to check if a directed graph is eulerian?
1) All vertices with nonzero degree belong to a single strongly connected component.
2) In degree is equal to the out degree for every vertex. Source: geeksforgeeks
Question:
In the given two conditions, is the first one strict? I mean why is it really necessary for the graph to be "strongly" connected graph? What if the graph is just connected?
I learned that condition 1 can be replaced with weakly connected graph. Again, what if the graph is just connected instead of weakly connected?
Will be glad to see some examples.
P.S: Consider condition 2 is always fulfilled in the above discussion. And by "just connected", I mean there exists a vertex in the graph from which all other vertices are reachable.

This is an interesting question. There is, to the best of my knowledge, no standardized meaning of "connected" in the context of a directed graph. The two general notions of connectivity in a directed graph are
strong connectivity, where for any pair of nodes u and v there's a path from u to v and a path from v to u, and
weak connectivity, where the undirected graph formed by ignoring the directionality on the arrowheads is connected.
Your version of the directed graph being "just connected" is slightly different than these definitions, but it's related to strong connectivity. Any directed graph can have its nodes partitioned into strongly connected components (SCCs), groups of nodes that can all reach one another. Those strongly connected components form a DAG, where each strongly connected component is a node and there's an edge from one SCC to another if one of the nodes in the first SCC has an edge to a node in the second SCC.
Your definition of the graph being "just connected" could then be pinned down like this:
"just connected": the DAG of SCCs has a source node that can reach all other nodes.
Notice that "just connected" implies weakly connected, but not the other way around.
It turns out that, in this case, if you have a graph where each node's indegree happens to equal its outdegree, then if the graph is "just connected," then it has an Euler circuit. If your graph is "just connected," then it's weakly connected. Then, we're going to claim that any weakly connected graph with indegrees equal to outdegrees must be strongly connected as well. To see this, pick any SCC in the DAG of SCCs with no incoming edges. Any edge entering any of the nodes in this SCC must come from within that SCC. As a result, if we went through each node in the SCC and added up the number of edges leaving that node, that total would match the number of incoming edges into each node in the SCC. But then, since the sum of indegrees of nodes equals the sum of outdegrees of the nodes, there can't then be any edges starting within this SCC and ending in another, since all edges are accounted for. Therefore, this SCC has no edges leaving it.
We've just shown that any source SCC must have no edges to any other SCCs. And since there is some node in some source SCC that can reach every node, this means that there are no other SCCs in the graph, so the graph has just one SCC and is therefore strongly connected.

Related

Print routes from tickets

Today I encountered a question which I am not able to solve.
A frequent traveler collects all his travel tickets.
A ticket has only 2 attributes, Start Journey Location name and Destination Name. Example from Delhi to NY.
At the end of the year, the traveler gets all his tickets together and tries to map his journey across the year. Print his probable travel route in a readable format. He does not remember his start location. he can visit a location multiple times, and can also go back and forth a place several times.
Initially i thought that it could simply be solved by making a graph(ticket-A to B means a directed edge A->B) and using a simple Depth first traversal from a node with indegree 0(??). but then I realized that its not the correct way to get solution as it could print a random unconnected route.
Please suggest a correct way to proceed.
First you should check whether your graph has an Eulerian trail or an Eulerian cycle.
A directed graph has an Eulerian cycle if and only if every vertex has
equal in degree and out degree, and all of its vertices with nonzero
degree belong to a single strongly connected component. Equivalently,
a directed graph has an Eulerian cycle if and only if it can be
decomposed into edge-disjoint directed cycles and all of its vertices
with nonzero degree belong to a single strongly connected component.
A directed graph has an Eulerian trail if and only if at most one vertex
has (out-degree) − (in-degree) = 1, at most one vertex has (in-degree)
− (out-degree) = 1, every other vertex has equal in-degree and
out-degree, and all of its vertices with nonzero degree belong to a
single connected component of the underlying undirected graph.
If your graph has an Eulerian cycle, you can start your DFS from any arbitrary node and you can be sure that your path will be correct.
If your graph has an Eulerian trail, first find the node with (out-degree) − (in-degree) = 1 and call it source and find the node with (in-degree) − (out-degree) = 1 and call it sink. You should start your DFS from source and avoid going to sink as much as possible. It means whenever there is an option between going to node sink and some other node you should go the other node and use the sink node only when you have no other option (not exactly true but makes it simpler). This way you can be sure that you end up with the correct trail.

What do you call a fully connected directed graph where each node has at most one outbound edge?

A fully connected directed graph where each node has at most a one inbound edge is a tree. Does a fully connected directed graph where each node has at most a single outbound edge have a name? Anti-tree?!
I believe that this does indeed have a name - it is a directed pseudotree.
From the Wikipedia article on pseudoforests:
A directed pseudoforest is a directed graph in which each vertex has
at most one outgoing edge; that is, it has outdegree at most one. A
directed 1-forest – most commonly called a functional graph, sometimes maximal directed pseudoforest – is a directed graph
in which each vertex has outdegree exactly one.[8] If D is a directed
pseudoforest, the undirected graph formed by removing the direction
from each edge of D is an undirected pseudoforest.
Hence, we can call the graph you describe above a directed pseudoforest. You also note that the graph is connected. From the same page:
A pseudotree is a connected pseudoforest.
Hence, the term directed pseudotree.
Here is the proper definition of an undirected pseudoforest, for your information, from Wolfram Alpha:
A pseudoforest is an undirected graph in which every connected
component contains at most one graph cycle. A pseudotree is therefore
a connected pseudoforest and a forest (i.e., not-necessarily-connected
acyclic graph) is a trivial pseudoforest.
Some care is needed when encountering pseudoforests as some authors
use the term to mean "a pseudoforest that is not a forest."

Minimize set of edges in a directed graph keeping connected components

Here is the full question:
Assume we have a directed graph G = (V,E), we want to find a graph G' = (V,E') that has the following properties:
G' has same connected components as G
G' has same component graph as G
E' is minimized. That is, E' is as small as possible.
Here is what I got:
First, run the strongly connected components algorithm. Now we have the strongly connected components. Now go to each strong connected component and within that SCC make a simple cycle; that is, a cycle where the only nodes that are repeated are the start/finish nodes. This will minimize the edges within each SCC.
Now, we need to minimize the edges between the SCCs. Alas, I can't think of a way of doing this.
My 2 questions are: (1) Does the algorithm prior to the part about minimizing edges between SCCs sound right? (2) How does one go about minimizing the edges between SCCs.
For (2), I know that this is equivalent to minimizing the number of edges in a DAG. (Think of the SCCs as the vertices). But this doesn't seem to help me.
The algorithm seems right, as long as you allow for closed walks (i.e. repeating vertices.) Proper cycles might not exist (e.g. in an "8" shaped component) and finding them is NP-hard.
It seems that it is sufficient to group the inter-component edges by ordered pairs of components they connect and leave only one edge in each group.
Regarding the step 2,minimize the edges between the SCCs, you could randomly select a vertex, and run DFS, only keeping the longest path for each pair of (root, end), while removing other paths. Store all the vertices searched in a list L.
Choose another vertex, if it exists in L, skip to the next vertex; if not, repeat the procedure above.

Minimal addition to strongly connected graph

I have a set of nodes and set of directed edges between them. The edges have no weight.
How can I found minimal number of edges which has to be added to make the graph strongly connected (ie. there should be a path from every node to all others)? Does this problem have a name?
It's a really classical graph problem.
Run algorithm like Tarjan-SCC algorithm to find all SCCs. Consider
each SCC as a new vertice, link a edge between these new
vertices according to the origin graph, we can get a new graph.
Obviously, the new graph is a Directed Acyclic Graph(DAG).
In the DAG, find all vertices whose in-degree is 0, we define them
{X}; find all vertices whose out-degree is 0, we define
them {Y}.
If DAG has only one vertice, the answer is 0; otherwise, the answer
is max(|X|, |Y|).
Off the top of my head, it seems the simplest (fewest edges) way to make a directed graph strongly connected would be to just have a cycle involving all nodes; so the minimum number of edges would just be N where N is the number of nodes. If there are already edges, just do something like connect longest existing directed path to the next longest path that doesn't overlap with your current path, until you form a complete cycle (once your path contains all nodes, connect the ends to form the cycle.)
Not sure if there is a more formal definition of any of this, but is seems logical to me.
I would find all weakly connected components, and tie them up in a cycle.
EDIT:
To be more explicit, the idea is if you have WCCs W(1),...,W(n),
make all of W(i%n + 1) reachable from any node in W(i), for i=1 to n.

Weakly connected balanced digraph

How can I prove that if a balanced digraph is weakly connected, then it is also strongly connected? (balanced digraph means that for every node, it's indegree and outdegree is the same and weakly connected means the non-directed version of this graph is connected).
What I can think of so far is: if the graph is balanced, it means it is a union of directed cycles. So if I remove any cycle, it will stay balanced. Also each vertex in the cycle has one edge coming into it and one edge leading out of it..
Then I guess I need to use some contradiction or induction to prove that the graph is strongly connected.. That's where I confused.
If two of those cycles intersect then they form a strongly connected component (since we can travel from any vertex in the cycle to the intersection of them then go around the other cycle and then come back again to complete the figure-8 )
Since the graph is weakly connected we know all the cycles intersect so therefore the graph is strongly connected.
I think you can flesh out the formality I left out.

Resources