convert cyclic to acyclic graph - algorithm

I want to convert a cyclic graph into an acyclic graph. Is there an pseudocode that can do this? I did try to search but most of them returned where mathematical based on markov chain or research articles.
I want to write a program to do it and any directions will be useful. for example consider this below graph.
A->B
B->C
C->A
I saw a solution in an MIT lecture but the lecture skimmed over it with reference to something taught earlier and couldn't catch it. In short it kind of replicates the nodes in layers in a way that the end graph denotes a DAG but conveys the same path information.
[See 46:59]
https://www.youtube.com/watch?v=OQ5jsbhAv_M
Edit:
There is an explanation of turning cyclic graphs into dags in this MIT recitation at 36:57
https://youtu.be/IFrvgSvZA0I
Se also Wikipedia : cycle and forest
Edit:
I want to apply dynamic programming over a problem which is a cyclic graph e.g) say shortest path problem Delta(S,D) where S-> Source node and D->Destination node. Since DP over cyclic graph is infinite algorithm, we first need to convert the cyclic graph into acyclic graph and then apply the dynamic programming technique over it.

I believe you pointed out the wrong video, here it is (46:59): https://www.youtube.com/watch?v=OQ5jsbhAv_M
The idea exposed here, is to make several copies of the graph and to arrange them into layers. Each one represents the state of the graph at a given time, and for every edge traversed you go down by one layer.
I did not find pseudo-code for this, but the way of doing this is detailed here: https://cstheory.stackexchange.com/questions/14591/combinatorics-of-bellman-ford-or-how-to-make-cyclic-graphs-acyclic

Related

Solving The House Drawing Problem With Graph Theory Algorithms

I was recently given the challenge of drawing a house with an x in the middle without lifting my pen, and without retracing any lines. Link to problem
The link above begins to dive into some of the graph theory related to the problem, however there is no mention of how one might go about solving this problem using graph theory algorithms.
What algorithms could be used here, and what would be the correct way to formulate this problem using graph theory language?
Two specific algorithms for constructing an Eulerian path are mentioned in the Wikipedia article on Eulerian paths. These are Fleury's algorithm and Hierholzer's algorithm.
Note that an algorithm that only finds an Euler cycle can also find an Euler trail by augmenting the graph with another edge that joins the 2 vertices that have an odd degree, then rotating the solution so that the added edge is first or last, then removing this edge from the solution found.

Is there a 2D-layout algorithm for DAGs that allows the positions on one axis to be fixed?

I've got a DAG of around 3.300 vertices which can be laid out quite successfully by dot as a more or less simple tree (things get complicated because vertices can have more than one predecessor from a whole different rank, so crossovers are frequent). Each vertex in the graph came into being at a specific time in the original process and I want one axis in the layout to represent time: An edge relation like a -> v, b -> v means that a and b came into being at some specific time before v.
Is there a layout algorithm for DAGs which would allow me to specify the positions (or at least the distances) on one axis and come up with an optimal layout regarding edge crossovers on the other?
You can make a topological sorting of the DAG to have the vertices sorted in a way that for every edge x->y, vertex x comes before than y.
Therefore, if you have a -> v, b -> v, you will get something like a, b, v or b, a, v.
Using this you can easily represents DAGs like this:
Yes, as #Arturo-Menchaca said a topological sorting may help to reduce overlapping count of edges. But it may be not optimal. There is no good algorithm for edge crossing minimization. Problem for crossing minimization is NP-complete. The heuristics are applied for solving this problem.
This StackOverflow link may help you: Drawing Directed Acyclic Graphs: Minimizing edge crossing?
I suppose your problem is related to an aesthetically pleasing way of the graph layout. Some heuristics are described in the articles Overview of algorithms for graph drawing, Force-Directed Drawing Algorithms. May be information about planar graph or almost planar graph can help you also.
Some review of the algorithms for checking and drawing planar graphs are described in the Wiki pages Planar graph, Crossing number (graph theory). The libraries and algorithms for planar graph drawing are described in the StackOverflow question How to check if a Graph is a Planar Graph or not? For example the author in the article GA for straight-line grid drawings of maximal planar graphs uses genetic algorithms for straight-line grid drawing.
Good descriptions for almost planar graphs are given in the articles Straight-Line Drawability of a Planar Graph Plus an Edge, On the Crossing Number of Almost Planar Graphs.
Try to modify the original algoritms using your condition with one axis alignment.
If I understood you correctly then you want to minimize the number of edge-crossings in your graph layout. If so, then the answer is "No", because this problem is proved to be NP-complete in the general case. See this, "Crossing Number is NP-Complete, Garey, Johnson".
If you need a not an optimal but just good enough solution, there are multiple articles on this topic because it is heavily related with circuit layouts. Probably googling "crossing number heuristics" and looking through the abstracts of some papers will solve your task better then me trying to guess blindly your requirements.

How is Matrix Chain Multiplication a special case of shortest path in DAG?

Can DP algorithm for Matrix Chain Multiplication be modeled as shortest path in DAG? I read somewhere that every DP problem is a walk on an implicit DAG but I am unable to visualize those problems in which a transition leads to more than one state ( or sub-state ).
One more example where I fail to visulize the same is UVA 10003. A DP solution of the above is discussed here: Cutting a stick such that cost is minimized.
Imagine that there is a directed edge between two states if we can go from the first state to the second one(of course, a state can consist of several parameters). There are no cycles in this graph, so it is DAG. So visualizing a DAG itself is not hard(you can just write down all states and edges between them). But is not necessary can modeled as a shortest path search. For example, in a problem about cutting a rope the value for a state is a sum of values for two other states, so it is not even a path. Anyway, it might impractical to visualize a solution if the number of parameters is very big. And there is no need to do any visualization to solve a problem and prove the correctness of your solution.

computing vertex connectivity of graph

Is there an algorithm that, when given a graph, computes the vertex connectivity of that graph (the minimum number of vertices to remove in order to separate the graph into two connected graphs). (Note that the graph may be already be disconnected). Thanks!
See:
Determining if a graph is K-vertex-connected
k-vertex connectivity of a graph
When you combine this with binary search you are done.
This book chapter should have everything you need to get started; it is basically a survey over algorithms to determine the edge connectivity and vertex connectivity of graphs, with pseudo code for the algorithms described in it. Page 12 has an overview over the available algorithms alongside complexity analyses and references. Most of the solutions for the general case are flow-based, with the exception of one randomized algorithm. The different general solutions optimize for different properties of the graph, so you can choose the most asymptotically efficient one beforehand. Also, for some classes of graphs, there exist specialized algorithms with better complexity than the general solutions provide.

Shortest path from single source to single destination in a graph

My graph contains no such edges which connect a vertex to itself. There is only one edge between two vertices. From Wikipedia i got to know about some algorithm which are used for calculating shortest path based on the given conditions. One of the most famous algorithm is Dijkstra's algorithm, which finds a shortest paths from source vertex to all other vertices in the graph.
But by using Dijkstra's algorithm, i am unnecessary exploring all the vertices, however my goal is just to find shortest path from single source to single destination. Which strategy should i use here? So that i need not to explore all other vertices.
One of my approach is to use bidirectional bfs. By bidirectional bfs i mean to apply two bfs one from source node, another one from destination node. As soon as for the first time when i find any same child in both tree,i can stop both bfs .Now the path from source to that child union path from child to destination would be my shortest path from source to destination.
But out of all the approaches described by Wikipedia and bidirectional bfs, which one suits best for my graph?
It depends if there's any apparent heuristic function that you could use or if you don't have any further usable information about your graph.
Your options are:
BFS: in general case or if you don't care about computation time that much.
Dijkstra (Dijkstra "is" BFS with priority queue): if your edges have weights/prices (non negative) and you care about CPU time.
A* (A* "is" Dijkstra with heuristic function - e.g. distance on a city map): if you want it to be really fast in average case, but you have to provide good heuristic function.
For some graph problems it may be possible to use Dynamic programming or other algorithmic tools. It depends on a situation. Further information can be found in tutorials from http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=alg_index ...
From Introduction to Algorithms (CLRS) second edition, page 581 :
Find a shortest path from u to v for a given vertices u and v. If we solve the single-source problem with source vertex u, we solve this problem also. Moreover, no algorithms for this problem are known that run asymptotically faster than the best single-source algorithms in the worst case.
So, stick to Dijkstra's algorithm :)
The Wikipedia article spells out the answer for you:
If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 13 if u = target.
You could use Dijkstra's algorithm and optimize it in the way that you stop exploring paths that are already longer than the shortest you found so far. Because those are not going to be shorter (provided that you don't have negative weighs on your edges).

Resources