-1

Consider a graph with V vertices and n routes (cars). Each path (car) starts from one vertex and traverses the V vertices just one time (there is not any loop in each route).

In each iteration (i.e., time), just one route (car) can be in each of the vertices (no collision between vehicles). If it is not feasible (two cars collide each other), the route (car) sticks in its vertex on that iteration (car wait on that vertex). How can we find the minimum number of cycles in which all of the routes (cars) can traverse the graph?

Example: An example with 5 vertices, and 2 routes

In this example, there are 5 vertices: Blue, Black, Purple, Orange, Green The first route is: Blue, Black, Green, Purple, Orange (Green Line) The second route is: Green, Orange, Black, Purple, Blue (Black Line)

Minimum number of iterations: 5

The 1st route is: Blue, Black, Green, Wait, Purple, Orange (Green Line)

The 2nd route is: Green, Orange, Black, Purple, Blue, Wait (Black Line)

In this example, the first car waits on iteration #4 to prohibit the collision with the second car on the Purple vertex.

1 Answers1

1

You can use the A* algorithm. In the problem that this algorithm solves, a node would represent a possible state of the all the cars. So the starting node would represent the initial state of the cars and the target node the state where all cars are in their destination.

A* chooses the next node n by minimising the following function:

        f(n) = g(n) + h(n)

If there are no possible collisions, there will be only one node n to add to the collection of nodes to consider, but if there is a collision to avoid, there will be choices (i.e. different nodes) to choose from, each time with a different set of car(s) that have to wait.

g(n) is the number of the iteration for that particular node. It is important to realise that during this algorithm, the set of nodes to consider may not all be in the same iteration.

h(n) is a heuristic function. For the purposes of this problem, h(n) should be admissible, and so we can define it as the maximum of the number of iterations each car would still need (without collisions) to reach its destination. h(n) gives an optimistic count of iterations as it assumes there will be no further collisions to deal with.

As always with A*, you need a priority queue (like a min heap) to store the nodes. It starts with only the starting node. Then each neighbor node (next iteration) is generated from it, and pushed unto the heap. Like stated before, if there are no collisions, there is only one neighbor node to push. If however a trivial move of each car would lead to one or more collisions, several neighbors can be considered and pushed unto the heap. Then a node is popped from the heap, and the process continues.

When a node is pushed unto the heap one additional information should be added to it: the number of iterations to get to that node. This is the value of g(n) for that node. When the destination node is reached, this will be the solution to return.

trincot
  • 317,000
  • 35
  • 244
  • 286