0

I am trying to think of an algorithm such that giving a graph $G(V,E)$, and a weight function $w\colon E \to \mathbb{N}_+$ (which means giving every edge in the graph a positive weight), and a source vertex $S$, the algorithm finds the shortest path between each $v \in V$ and $S$, such that the weight of the path is divided by 3 . (The weight of the path means the sum of all the weights of the edges that are on the path.)

In some solutions, I found that for this problem they built a new auxiliary graph which will "maintain the weight modulo 3" so that when we get from $S$ to any vertex we will be interested in the path that starts and ends with remainder 0 and this is through duplicating the graph to 3 copies which will allow switching between different copies.

I don’t understand how to build such a graph and how it is going to help me with this problem, any help would be appreciated.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
mmolaan
  • 3
  • 1

1 Answers1

0

Given $G=(V,E,w)$ the auxiliary graph you want to build is $H=(V', E', w')$ where:

  • $V' = V \times \{0,1,2\}$
  • Given $(u,i), (v,j) \in V'$, $( (u,i), (v,j) ) \in E'$ if and only if $(u,v) \in E$ and $i+w(u,v) \equiv j \pmod{3}$.
  • For any $e' =( (u,i), (v,j) ) \in E'$, $w'(e') = w(u,v)$.

For any vertex $v \in V$, a shortest path $\pi'$ between $(S,0)$ and $(v, 0)$ in $H$ induces a path $\pi$ from $S$ to $v$ in $G$ that is shortest among those having length divisible by $3$. In details, let $\langle v'_0, v'_1, \dots, v'_k \rangle$ be the vertices traversed by $\pi'$, where $v'_j = (v_j, i_j)$. The path $\pi$ traverses the vertices $\langle v_0, v_1, \dots, v_k\rangle$, in order.

Steven
  • 29,724
  • 2
  • 29
  • 49