What is a data structure suited for representing railways with turnouts? - algorithm

I'm trying to represent the paths in a railroad as a data structure but I am having a hard time representing turnouts.
This feels like a graph problem, but there is a difference compared to regular graphs.
A railway turnout is a vertex connected to three other vertices. A, B and C.
But, in a railway system the graph is traversed with a direction.
So, you are able to take the path B -> turnout -> A and C -> turnout -> A, but are not able to take the path B -> turnout -> C.
Is there a (graph) data structure which allows for representing paths with directions?
This data structure would provide the base for a software system to automate a small model railroad.

You can represent the turnout as 2 vertices - one for each state of the turnout. So if you have source A and destinations B and C and turnout which can switch between B and C - you will have 2 vertices for this turnout: TB and TC. Also you will have following edges: A->TB, TB->B, A->TC, TC->C
This allows you to travel from A -> TB -> B and from A -> TC -> C. And since you will have no edge between TB and TC - you will not be able to travel from B -> C directly

Each path can be considered as a vertice and a connection between two paths as an edge.
B ->
A
C ->
This can be represented as a graph in a Go map,
Take a look at the following,
In your example directional connection exists from B -> A and C -> A. This can be represented in a map as follows.
graph := map[string][]string{
"B": []string{"A"},
"C": []string{"A"},
}
Each key in the map represents the starting path of a directional connection. Each value in the array of the corresponding key is the destination path.

Related

In GraphViz, in a strict graph, is it possible to have 2 edges between nodes if they have different attributes?

What I want to achieve is, if I have something like
Strict Digraph G {
a -> b [label = edge1]
a -> b [label = edge2]
a -> b
a -> b
a -> b [label = edge1]
}
then I should get a graph with 2 nodes and 3 edges between them (One with label edge1, one with edge2 and one without a label).
It doesn't seem to be possible, with the strict keyword with this input, it only draws one edge.
Sorry, not "legal". From https://graphviz.org/doc/info/lang.html
A graph may also be described as strict. This forbids the creation of multi-edges, i.e., there can be at most one edge with a given tail node and head node in the directed case.

Weird node placement of dot graph

I have the following source code for a graph in dot:
digraph name {
rankdir="LR";
node [shape="record"];
1 [label="OUTPUT"];
A [label="FWD|<i>i|<r_in>r_in|<r_out>r_out|<o>o"];
B [label="FIFO|<r_in>r_in|<o>o"];
C [label="Cons|<i>i|<r_out>r_out|<o>o"];
A:o:e -> C:i:w;
C:r_out:w -> A:r_in:e;
B:o:e -> A:i:w;
C:o:e -> 1:w;
A:r_out:w -> B:r_in:e;
}
It consists of 4 nodes, which essentially could be placed one after another, in the order B -> A -> C -> OUTPUT. If dot would place the nodes in this order, only few edge would cross between two nodes.
However, calling dot like this:
dot mygraph.dot -Tpng -o mygraph.png
creates the following mess:
Edit: It seems, the order of the nodes in the source is important. However, as the source is generated from a program, outputting its internal signal flow structure, I cannot rely on it to put the nodes in the right order. I thought, dot and its graph layout engine can figure out on its own, which nodes are the first ones, such that the wires do not cross.
Just define the nodes in the desired order:
digraph name {
rankdir="LR";
node [shape="record"];
B [label="FIFO|<r_in>r_in|<o>o"];
A [label="FWD|<i>i|<r_in>r_in|<r_out>r_out|<o>o"];
C [label="Cons|<i>i|<r_out>r_out|<o>o"];
1 [label="OUTPUT"];
A:o:e -> C:i:w;
C:r_out:w -> A:r_in:e;
B:o:e -> A:i:w;
C:o:e -> 1:w;
A:r_out:w -> B:r_in:e;
}
yields

How to cluster similar item together in a 2d Graph

well I am not looking for how to draw items on 2d Graph, Its just a pictorial representation of what the expected output need to be
I have a list like
a=[]
b=['c','d','e']
c=['a','b','d']
d=['a']
e=['b','a']
l=['g','r','p']
g=['r']
r=['g']
p=['l']
now from above it is clear that b is pointing to c , d ,e
a,b,c,d are closely linked , while the l,g,r,p are linked
can any one tell me an algo ( keeping 2d Picture in mind) how these similar items can be reprsented together.
Above is just an example.
The list will be dynamically created
Have you come across Graphviz? It has algorithms for various different forms of graph layout which I imagine would do a nice job of laying out your small example above. It also includes some simple GUIs to allow you to experiment with the different layouts it supports.
Edit: in response to some clarifications:
If you need to find dense subgraphs within your graph, even if it is fully connected, then you are looking for algorithms that find communities in networks. An example of a recently-developed algorithm doing such on large graphs (2 million+ nodes, representing a social network) efficiently can be found in this paper.
Just to extend Alex's answer, here is an example of graphviz use for your graph:
graph.dot:
digraph G
{
b -> c;
b -> d;
b -> e;
c -> a;
c -> b;
c -> d;
d -> a;
e -> b;
e -> a;
l -> g;
l -> r;
l -> p;
g -> r;
r -> g;
p -> l;
}
Output of Graphviz:
If you just want to know what are the clusters in your graph without drawing it, just use this algorithm.

How can I give a graph nodes fixed position in graphviz and how can i make the edges not overlapped?

I have seen some similar questions here, but the answers dont solve my problem.
I want to draw a graph. I write some code like this:
digraph {
{rank = same a b c d e f }
a -> b -> c -> d -> e -> f
a -> f
b -> d -> f
b -> f
}
but the result is that some of the edges overlapped each other.
So my question is how can I fix the edge to make it not overlap
and I also wanna know how can I give the node a fixed position? There is no problem this graph. But some times when I wanna a graph with a sequence of
a b c d e f
but when i create some edges and the sequence will change like:
a->e b c d f
You can use the attribute pos of a node or edge to specify coordinates. To see where dot places your nodes and edges you can simply run dot myinputfile.dot without any output parameter. This will produce the dot file with added coordinates (among other additions).
Based on this you can force dot to place some or all nodes at certain coordinates.

Seeking algorithm to invert (reverse? mirror? turn inside-out) a DAG

I'm looking for an algorithm to "invert" (reverse? turn inside-out?) a
DAG:
A* # I can't ascii-art the arrows, so just
/ \ # pretend the slashes are all pointing
B C # "down" (south-east or south-west)
/ / \ # e.g.
G E D # A -> (B -> G, C -> (E -> F, D -> F))
\ /
F
The representation I'm using is immutable truly a DAG (there are no
"parent" pointers). I'd like to traverse the graph in some fashion
while building a "mirror image" graph with equivalent nodes, but with
the direction of relations between nodes inverted.
F*
/ \
G* E D # F -> (E -> C -> A, D -> C -> A), G -> B -> A
\ \ / #
B C # Again, arrows point "down"
\ / #
A #
So the input is a set of "roots" (here, {A}). The output should be a
set of "roots" in the result graph: {G, F}. (By root I mean a node
with no incoming references. A leaf is a node with no outgoing
references.)
The roots of the input become the leaves of the output and visa
versa. The transformation should be an inverse of itself.
(For the curious, I'd like to add a feature to a library I'm using to
represent XML for structural querying by which I can map each node in
the first tree to its "mirror image" in the second tree (and back
again) to provide more navigational flexibility for my query rules.)
Traverse the graph building a set of reversed edges and a list of leaf nodes.
Perform a topological sort of the reversed edges using the leaf (which are now root) nodes to start with.
Construct the reversed graph based on the reversed edges starting from the end of the sorted list. As the nodes are constructed in reverse topological order, you are guaranteed to have constructed the children of a given node before constructing the node, so creating an immutable representation is possible.
This is either O(N) if you use structures for your intermediate representation which track all links in both directions associated with a node, or O(NlnN) if you use sorting to find all the links of a node. For small graphs, or languages which don't suffer from stack overflows, you can just construct the graph lazily rather than explicitly performing the topological sort. So it depends a little what you're implementing it all in how different this would be.
A -> (B -> G, C -> (E -> F, D -> F))
original roots: [ A ]
original links: [ AB, BG, AC, CE, EF, CD, DF ]
reversed links: [ BA, GB, CA, EC, FE, DC, FD ]
reversed roots: [ G, F ]
reversed links: [ BA, CA, DC, EC, FE, FD, GB ] (in order of source)
topologically sorted: [ G, B, F, E, D, C, A ]
construction order : A, C->A, D->C, E->C, F->(D,E), B->A, G->B
Just do a depth-first search marking where you have already been, and each time you traverse an arrow you add the reverse to your result DAG. Add the leaves as roots.
My intuitive suggestion would be to perform a Depth First traversal of your graph, and construct your mirrored graph simultaneously.
When traversing each node, create a new node in the mirrored graph, and create an edge between it and its predecessor in the new graph.
If at any point you reach a node which has no children, mark it as a root.
I solved this with a simple graph traversal. Keep in mind topological sorting will only be useful for directed acyclic graphs.
I used an adjacency list, but you can do a similar thing with an adjacency matrix.
In Python it looks like this:
# Basic Graph Structure
g = {}
g[vertex] = [v1, v2, v3] # Each vertex contains a lists of its edges
To find all the edges for v, you then traverse the list g[v] and that will give you all (v, u) edges.
To build the reversed graph make a new dictionary and build it something like this:
reversed = {}
for v in g:
for e in g[v]:
if e not in reversed:
reversed[e] = []
reversed[e].append(v)
This is very memory intensive for large graphs (doubling your memory usage), but it is a very easy way to work with them and quite quick. There may be more clever solutions out there involving building a generator and using a dfs algorithm of some sort, but I have not put a lot of thought into it.
Depth-first search might be able to generate what you're after: Note your path through the tree and each time you traverse add the reverse to the resulting DAG (leaves are roots).

Resources