How do I prove this algorithm's correctness?

444 views Asked by At

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?

2

There are 2 answers

0
NoShady420 On

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 even

So 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

0
Konstantin Yovkov On

All paths in G' ending at v_0 have an even number of edges

That's not entirely true. v_0 has some incoming edge, say p_1 -> v_0, and that path has odd number of edges (1).

But you're close to the truth, though. If we take the original graph G and for each vertex v we create two vertices v_0 and v_1 in G', then we can split G' in two (disjoint) sets of vertices - 0-vertices and 1-vertices.

And I state that:

All paths in G' that start from any 0-vertex and end at any 0-vertex have even number of edges.

This is true, because G' is bipartite. By definition, a bipartite graph is a graph whose vertices can be divided into two disjoint sets (in our case 0-vertices and 1-vertices) so that every edge connects two vertices from different sets. The way we built G' makes it bipartite, because we never connect vertices that belong to the same set (we always connect u_0 with v_1 and vice-versa)

In a bipartite graph, like G', every path that starts from a vertex from one of the sets and ending at a vertex in the same set, has even number of edges. The only thing left to do is to find out the shortest one (with a shortest path algorithm of your choice).