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.