1
votes

Here is my problem I found in an online judge website:

I have an undirected graph (with loops). We have k different classes of vertices in the graph. You can think of class 1 vertex being colored green, class 2 vertices colored red and so on. There is also a special class of vertices colored white (more later).

Now, the user will specify a source vertex, a destination vertex, and a sequence of distinct vertex classes (non-white) eg.

We are given source vertex 10, destination vertex 40, and a sequence: red->blue->black.

We have to find the shortest path such that the path starts from vertex 10, touches 1 red vertex followed by 1 blue and 1 black vertex and then reaches vertex 40. The path, however, can have as many white vertices as needed. It can also traverse a white vertex twice.

So a solution can be: 10->20(white)->35(red)->21(white)->22(white)->30(blue)->34(black)->40

Incorrect:

10->20(white)->30(blue)->21(white)->22(white)->35(red)->34(black)->40 (goes to blue before red)

10->20(white)->35(red)->56(red)->21(white)->22(white)->30(blue)->34(black)->40 (goes through red twice)

3
Due to the additional constraints, the shortest path may go through the same vertex twice. For example 20(white)->35(red)->20(white)->30(black). Is this allowed? - Henry
Yes, that is allowed. Sorry should have mentioned that. - Bruce
Is there any constraint available for vertex quantity ? If yes please add it, it may have critical importance for solving online judge problem - Grigor Gevorgyan
No constraint on vertex size. Colored vertex should be traversed only once though. - Bruce

3 Answers

2
votes

I can suggest an O(n*(n+m)) solution based on a simple graph modification completed with a bfs modification. Let me describe it step by step.

Graph modification.

  1. To avoid any troubles, color source and white vertexes in some unique color.
  2. Make graph weighted. Weight of originally present edges is 1.
  3. For each pair of colored vertexes u, v add an edge (u,v) which has a weight equal to the shortest white path from u to v. A white path is a path which passes only over white vertexes. If there is no such white path, don't add an edge.
  4. Remove all white vertexes and their adjacent edges.

The second point can be accomplished by running a bfs from each vertex that passes only over white vertexes. This will run in O( n*(n+m)).

Equivalence.

Now we have a weighted colored graph without white vertexes, and it's easy to see that the problem remains unchanged after modification - we still have to find a shortest path (now in terms of edges weight sum) from source to target vertex.

Search algorithm.

To solve the problem on this graph, run a variation of bfs, which layers correspond the provided path's colors. This means, if you're given path red->blue->black, the first moves bfs will do into all red vertexes adjacent to source, then to all blue adjacent to those red marked, then to all black adjacent to those blue marked, and finally to the target. When some vertex is pushed into bfs queue, remember the length of path to it for future use.

Pseudocode.

  1. currentVertexes = { (source,0) } // vertex and path length
  2. for i from 0 to sizeof( givenPath ) do
    1. nextColor = givenPath[ i ]
    2. nextVertexes = {}
    3. for (v,len) in currentVertexes do
      for all u s.t. exists edge e := (v,u) and u.color = nextColor
      nextVertexes.insert( (u, len + e.length))
    4. currentVertexes = nextVertexes
  3. choose the minimum of lengths stored in currentVertexes, as there are only (target, length) pairs.

Complexity:

Graph modification takes O(n*(n+m)) running time and the second part will run O(n*n), because the length of given path can be no longer than n (there's no color duplicates as said in statement), and on each step there can be at most n vertexes in currentVertexes. Total complexity is O(n*(n+m)) + O(n*n) = O(n*(n+m))

1
votes

You may use dijkstra algorithm with additional flag showing if the path to the vertex fufills the condition.

1
votes

Assumption A1 (lifted later): No color exists twice in the sequence S.

Then we can use the following Algorithm B1:

  1. Transform the graph G in the following way in a directed graph G':
    • G' has the same nodes as G
    • if v is a white node and (v,u) exists in G, insert (v,u) and (u,v) into G'
    • if (v,u) exist in G and (color(v),color(u)) exist in S, insert (v,u) in G'
    • all other edges (including loops) of G are ignored.
  2. Assign the same weight to all edges
  3. Perform a shortest path algorithm on G', e.g., Bellman-Ford

Now we relax A1: Assumption A2: only one color exists more than once in S

Algorithm B2:

  1. wl.o.g., c0 is the color that exists several times in S, more precisely n times.
  2. Divide S into subsequences S1, S2, ... with S1=(c1,c2,...,c0), S2=(c3,...,c0)...
  3. Generate graphs G1, G2, ... by transformation G regarding step 1. of B1 for each S_i
  4. Form a graph G' by connecting G1, G2,... Gn
    • connect each node with color c0 in G1 with each node of color c3 and any white node in G2
    • do the same with G2 and G3, etc.
  5. Apply a shortest path algortihm with source in G1 and destination in Gn

Extension to several repeating colors in the sequence:

Split the sequence into sequences without a repeating color and apply B2 analogously.