My algorithm:
Construct a new graph G' whereas for every vertex v in V, create two vertices v_0 and v_1 in G', and for every edge (u, v) in E, create two edges (u_0 , v_1) and (u_1,v_0) in G'. Run Dijkstra on G' starting at s_0.
All paths in G' ending at v_0 have an even number of edges so the shortest even-length path to vertex t in G can be found by determining the shortest path from s_0 to t_0 in G'.
How can I prove the correctness of this algorithm?
Assuming start node is S and finish node is F
On the modified graph, if we travel from ui to vj via an edge connecting the two nodes, then
j=(i+1) mod 2
by the way edges are defined.So if we travel from ui to vj via any arbitrary path,
j=(i+n) mod 2
where n is length of the path. (conclusion 1)So if we travel from S0 to F0 via some path,
0 = (0 + n) mod 2
meaning n is evenSo this proves that any path from S0 to F0 traverses even edges (conclusion 2)
Also, based on how edges are defined in the new graph, we can say that for any edge (u,v) in old graph, edge (ui,vj) exists in new graph for some i,j (conclusion 3)
Now, if in the original graph, an even length path exists, let the shortest path be SA1A2...A2m-1F where Ai could be repeated. (Even edges imply odd number of nodes). Then in the new graph, We can start on S0 and traverse S0A1iA2j...A2m-1kFm to land on F (from conclusion 3). Then the subscript on F will be
(0 + 2m-1 +1) mod 2 = 0
(from conclusion 2). Hence, a path will exist on the new graph too containing the same length.Hence, if the solution exists, then there exists a path from S0 to F0 in the new graph too (conclusion 4)
Also, based on how edges are defined in the new graph, for every edge from ui to vj, then there is an edge from u to v in the original graph. Hence, for any path in the new graph from ui to vj, there is a corresponding path from u to v in the old graph having the same length
Therefore if a path exists from S0 to F0, there exists a corresponding path in the original graph having the same length (conclusion 5)
Conclusions 2,4 and 5 together prove that the shortest path from S0 to F0 is the correct answer. If the path cannot be found, then it does not exist in the original graph either (from conclusion 4)
Hence, the algorithm is correct