-2

I have a directed acyclic graph and I need to find a path with an odd number of edges between the starting point and a sink (the graph can contain multiple sinks). In the end, I should have a time complexitxy of O(|V|+|E|).

lukead
  • 1
  • 1

1 Answers1

0

If the given vertices are V, duplicate them giving U. Change the edges to always connect one vertex from V to one from U and add mirrored counterparts from U to V. Start from the starting point's counterpart in U, and do a classical breadth-first search.

The search will alternate between U and V, and as the start is in U and the sinks are in V, you'll only find paths with an odd number of edges.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7