1
$\begingroup$

I'm trying to solve a graph problem ( it's not for homework, just to practise my skills ).

A dag $G(V,E)$ is given, where $V$ is the set of vertices and $E$ the edges. The graph is represented as an adjacency list, so $A_i$ is a set containing all the connections of $i$.

My task is to find which vertices are reachable from each vertex $v\in V$.

The solution I use has a complexity of $O(V^3)$, with transitive closure, but i read that in a blog it can be faster, although it didn't reveal how. Could anyone tell me an other way ( with better complexity ) to solve the transitive closure problem in a dag? Thanks in advance.

$\endgroup$
  • $\begingroup$ What is $f(\cdot)$? $\endgroup$ – Dimitris Dec 6 '12 at 18:18
  • $\begingroup$ @JɛffE Sorry I edited it. I misunderstood what the blog said. $\endgroup$ – Rontogiannis Aristofanis Dec 6 '12 at 18:45
  • 5
    $\begingroup$ i think this is for cs@stackexchange $\endgroup$ – Sasho Nikolov Dec 6 '12 at 19:28
  • 1
    $\begingroup$ Check this out: sciencedirect.com/science/article/pii/0304397588900321 $\endgroup$ – George Dec 6 '12 at 20:57
  • $\begingroup$ @Kaveh isn't the problem exactly transitive closure here? $\endgroup$ – Sasho Nikolov Dec 13 '12 at 8:26
2
$\begingroup$

Here is an $O(VE)$ algorithm, which is substantially better than $O(V^3)$ if the graph is sparse. First do a topological sort, in time $O(V+E)$. Now work your way backwards, storing which vertices are reachable. At a vertex of outdegree $d$, this requires $O(dV)$ work, so in total you get $O(VE)$.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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