0
$\begingroup$

We have a directed graph $G=(V,E)$ and each vertex $v\in V$ has a cost: $price(v)$. Our mission is to find an algorithm that runs in time $\mathcal{O}(|E|+|V|)$ that finds $\forall v\in V$ the minimal price of all vertices $w$ which are reachable from $v$ (all vertives $w$ s.t. there is a path from $v$ to $w$). Note that $v$ can reach itself.

As an example if the graph simply was $3\to 5 \to 1$ then the minimal price of all vertices was $1$, since all vertices can reach $1$ and its minimal.

I tried using a BFS-like algorithm but failed.

$\endgroup$
1
  • $\begingroup$ Welcome to COMPUTER SCIENCE @SE. My spelling checker misses the typo in the title, but flags one in the body of your post. * it's minimal* sadly is out of its scope. $\endgroup$
    – greybeard
    Commented Feb 7, 2022 at 4:38

1 Answer 1

0
$\begingroup$

Consider a strongly connected component $C$ of $G$ and notice that the sought value associated with all vertices $v$ in $C$ is the same. Let's call this value $P(C)$.

Compute all strongly connected components of $G$ (there are several ways to do this in time $O(|V|+|E|)$, see for example Tarjan's algorithm) and let these be $C_1, \dots, C_k$.

Now consider a directed graph $G'=(V', E')$ whose vertex set is $V' = \{ C_1, \dots, C_k\}$ and such that there is a directed edge $(C_i, C_j)$ in $E'$ if and only if $i \neq j$ and $(u,v) \in E$ for some vertex $u$ in $C_i$ and some vertex $v \in C_j$. Notice that $G'$ is a directed acyclic graph, and therefore admits a topological ordering.

Compute such an ordering and assume w.l.o.g. that it is $\langle C_1, \dots, C_k \rangle$ (otherwise we can just rename the components). This can be done in time $O(|V'|+|E'|) = O(|V|+|E|)$ (actually, Tarjan's algorithm already returns the strongly connected components in reverse topological order).

Notice that if some $C_i$ is a sink in $G'$ (i.e., it has no outgoing edges) then $P(C_i)$ can be immediately computed as $\min_{w \in {C_i}} \text{price}(w)$

If $C_i$ is not a sink, then we have: $$ P(C_i) = \min \big\{ \min_{w \in C_i} \text{price}(w), \min_{(C_i, C_j) \in E'} P(C_j) \big\}. $$

By examining the vertices of $G'$ in reverse topological order, we are able to compute all values $P(\cdot)$. Moreover, notice that the time needed to compute $P(C_i)$ is proportional to the number $|C_i|$ of vertices in $C_i$, plus the out-degree $\delta_i$ of $C_i$ in $G'$. Therefore, the overall time complexity of computing $P(\cdot)$ once the topological order is known is upper bounded (up to multiplicative constants) by: $$ \sum_{i=1}^k ( |C_i| + \delta_i ) = \sum_{i=1}^k |C_i| + \sum_{i=1}^k \delta_i = |V| + |E'| \le |V| + |E|. $$

$\endgroup$
3
  • $\begingroup$ when you say a connected component, you mean a set of all vertices where $\forall v,w$ v can be reached from w or w can be reached from v? Or do you mean the set where both v can be reached from w and w from v? Because if it is the second case then the algorithm doesn't seem to be correct. (note: the graph is directed) $\endgroup$
    – Ariel Yael
    Commented Feb 6, 2022 at 19:08
  • $\begingroup$ and if it is the first case, then for each component finding the minimal price doesn't sound like a trivial task (the 2nd case is incorrect because the minimal value can come outside the strong connected component) $\endgroup$
    – Ariel Yael
    Commented Feb 6, 2022 at 19:15
  • $\begingroup$ I missed the fact that the graph was directed. I have now edited my answer. $\endgroup$
    – Steven
    Commented Feb 6, 2022 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.