How do I add an edge to a graphviz neato/fdp graph, that has no influence of the layout. I tried especially weight=0 and w=0 as stated in the documentation, and many other combination with the len attribute and different weights .
I'm aming for a map of nodes with "fixed" position/neighborhood and an overlay of additional relations.
This should be achievable by a two-step approach where you first run graphviz with the nodes and subset of edges you want it to use to compute positions and have it output the node positions (for example, use the -Tdot output). Then add the additional edges and rerun graphviz again.
You may need to pin the nodes, see this question: graphviz - fixed node positions
Related
The objective is to able to create graphs in which a node can be overlapping between two or more subgraphs. Something like in the (erroneous) image below (A->B and A->C are in different subgraphs):
Something similar has been asked in Graphviz: Node in two subgraph and also in Same node in two subgraphs, but there had been no solution. This is something essential in graphs, and there should be an easy way of achieving that.
Overlapping subgraphs is not supported by GraphViz
How can I prevent the lines bunching up and making up unclear what leads to what, as shown in this image:
The most simple way would be to increase ranksep - the minimal distance between two ranks. This should leave more space for the edges, at least if you're using dot.
If the concentration of edges is due to a lot of nodes on the same rank, you may consider using the unflatten utility. Including this step in the generation of the graph allows to distribute nodes on the same rank to different ranks and therefore make the graph narrower (but longer), creating some space between the nodes. The edges to the individual nodes should then be more easily distinguishable.
A complete example on how to use the unflatten utility (with picture) can be found in this answer.
I came across this problem at a coding site and I have no idea on how to solve it. The editorial is not available nor was I able to find any related article online. So I am asking this here.
Problem:
You have a graph G that contains N vertices and M edges. The vertices are numbered from 1 through N. Also each node is colored either Black or White. You want to calculate the shortest path from 1 to N such that the difference of black and white nodes is at most 1.
As obvious as it is, applying straight forward Dijkstra's Algorithm will not work. Any help is appreciated. Thank you!
We can consider a modified graph and run Dijkstra on this one:
For each node in the original graph, the modified graph will have multiple meta vertices (theoretically, infinitely many) that each correspond to a different black-white difference. You only need to create the nodes as you explore the graph with Dijkstra. Thus, you won't need infinitely many nodes.
The edges are then pretty simple (you can also create them while exploring). If you are currently at a node with black-white difference d and the original graph has an edge to a white node, then you create an edge to the respective node with black-white difference d-1. If the original graph has an edge to a black node, you create an edge in the modified graph to the respective node with black-white difference d+1. You don't necessarily need to treat them as different nodes. You can also store the Dijkstra variables in the node grouped by black-white difference.
Running Dijkstra in this way will give you the shortest paths to any node with any black-white difference. As soon as you reach the target node with an acceptable black-white difference, you are done.
An undirected graph is given and first i need to find the least number of edges to make it have odd cycle and second i should find the ways to add these edges
Coloring the graph using black and white, that is, starting from arbitrary point using black, painting the adjacent non-painted nodes using white and works on these white nodes similarly.
Check if there is an edge have nodes with same color. If yes, there is already an odd cycle.
Otherwise, connecting any two nodes with same color makes an odd cycle. That is to say, you need exactly one extra edge and you have C(number_of_black_nodes, 2) + C(number_of_white_nodes, 2) ways to do so.
While I was in the shower today, I had a thought - How difficult would it be to write an algorithm to traverse a weighted di-graph and find the shortest path while allowed to skip a fixed number of edges s. I started thinking about even one skip, and for the brute force method it seems to multiply the problem by the number of edges in your graph, as you have to find the shortest path for each case where an edge is set to 0 cost and then compare across all graphs. I don't know if there are any algorithms that do this, but a cursory search of google didn't show any.
My first question would be for skipping the most costed edge(s), but it's also an interesting problem to examine having to find a path assuming you skip the least costed edge(s).
This is just to satisfy my curiosity, so no rush.
Thanks!
What follows is the logic of how to solve this problem. The way to solve this type of problem is to consider a graph composed of two copies of the original graph you want to traverse, which I'll describe how to create. For your sake, draw a small graph, and then draw it topologically sorted (which helps with the visualization, but is not necessary in the program.) Next, draw a copy of that graph a few inches above the original. You're in the bottom section of this graph when you have not yet used your skip, and you're in the top part when you have used your skip. Let's call the nodes in the bottom graph A1, A2, A3 ... and the nodes in the top graph B1, B2, B3 ... If, in your original graph, node 1 is connected to nodes 2, then your new graph has edges A1->A2, B1->B2, and a free connection, A1->B2 (with edge cost 0).
Consider the following original graph, where you start at the black node, and desire to end up at the blue node.
Your new graph will look like the following, where you again start at black and wish to go to the blue node.
At each location in the bottom half of the graph, you have not used your skip, and thus can either skip (moving to the top part of the graph) or can move normally, going to another node in the bottom graph.
You can then use any of the standard graph traversal algorithms.