0

I have a simple example of graph:

ListDigraph g;
ListDigraph::ArcMap<int> length(g);
ListDigraph::ArcMap<string> color(g);
build_graph(g, length, color); // function building the graph

The map length contains the weights of the graph, while the map color contains the color of the arcs.

I would like to solve shortest path using Dijkstra, but in a constrained way: for example, I want to avoid two consecutive red arcs in the path.

Dijkstra in LEMON can be called simply by:

Dijkstra<ListDigraph, ListDigraph::ArcMap> dijkstra_test(g,length);
dijkstra_test.run(s);

How can I add a constrain the shortest path computation ?

  • Create a graph g' with duplicate vertices that have only non-red arcs pointing back to g. Redirect all red arcs in g to g'. Now run Dijkstra on the combined g + g'. – beaker Jul 07 '23 at 01:20
  • can you be more detailed ? – Claudio Tomasi Jul 07 '23 at 09:34
  • For g with vertices {1, 2, 3, ..., n}, create a new g' with vertices {1', 2', 3', ..., n'}. For each red edge s->t in g, change the target to the corresponding node in g', i.e., s->t'. This means that traversing a red edge will put us in g'. For each non-red edge s->t in g, create a new edge from s' in g' back to t in g. If we are in g', we know that the last edge we traversed was red and we can only traverse a non-red edge next. By traversing any edge back to g, we know we have a non-red edge after the red edge and can again choose any color edge. – beaker Jul 07 '23 at 14:23

2 Answers2

0

Use a modified BFS to search through all paths between start and end. Stop the search when a path is found that meets the constraints.

  • Start by putting the start vertex on top of a stack.
  • LOOP
    • IF stack empty
      • Output 'NO PATH' and STOP
    • Take the top vertex off the stack and add it to the visited list and the current path
    • Add adjacent vertices which aren't in the visited list to the top of the stack.
    • IF the destination has been reached
      • IF current path meets constraints
        • Output path and STOP
      • Add current path to output
      • Backtrack along path to vertex adjacent to vertex on top of stack, marking vertices as unvisited

Notice that this does not give the shortest path that meets the constraints. If you need that, then the search must continue until exhaustion, replacing the path to be output whenever a shorter one is found that meets the constraints.

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
  • no, I do not want to code the library itself. LEMON is a commonly used library for fast and efficient OR problems, mostly on graph. It would make no sense to write from the beginning. I'm a programmer, I know how the code could be written: the question asks a way to do it with LEMON, not an algorithm for doing it. Furthermore, stopping the search does not guarantee the shortest path property, only the constrain. And I have a weighted graph – Claudio Tomasi Jul 06 '23 at 13:23
  • Graph theory libraries are great for toy problems and training wheels for newbies. At some point, if you need to do serious work, you will have to learn how to spin up your own code. – ravenspoint Jul 06 '23 at 13:26
  • Yes, but I find it hard to believe that a large library like LEMON does not provide something so simple – Claudio Tomasi Jul 06 '23 at 13:27
  • No library can hope to include everything human ingenuity can come up with. This is why it is a bad idea to become dependent on one. – ravenspoint Jul 06 '23 at 13:29
  • thanks, but the question was not about the fundamental way of thinking of coders in 21st century. If someone knows how to do it, will answer. I did not ask if it was better doing it with lemon or not. I asked HOW to do it with lemon – Claudio Tomasi Jul 06 '23 at 13:36
0

Use a product construction. See https://cs.stackexchange.com/q/118977/755. Construct a new graph, where you double the number of vertices; for each vertex v in the original graph, you have v0 (indicating that you entered v via an arc that was not red) and v1 (indicating that you entered v via an arc that was red). You should be able to fill in the edges in this graph. Then, use any algorithm for reachability in this graph, e.g., DFS, BFS, etc.

D.W.
  • 3,382
  • 7
  • 44
  • 110