3

First, I won't lie. This is my homework. I'm tring to solve this question for too many hours and I have no clue.

I need to write algorithm ( efficient) that find all the vertices with a even-length path from a given vertex to all other vertices.

I know its probably something with DFS uses...

Please give me some guidance!

Vladi
  • 583
  • 5
  • 13
Bobbbaa
  • 199
  • 2
  • 12
  • Your question is not clear. Are you looking for a subset `U` in `V` such that for each `u` in `U` there is *a* path/*shortest* path [with the same length]? And to where? To all vertices in `U` or in `V`? – amit Apr 08 '12 at 12:37
  • I'm given a vertex s. I need to find all the vertices that can be reached from s by an even length path. – Bobbbaa Apr 08 '12 at 12:38
  • Is this for a directed or an undirected graph? Is it a simple graph? – gcbenison Apr 18 '12 at 03:46

3 Answers3

4

Since it is homework, I am only providing some hints:

  1. If you do a DFS up to a certain depth, without maintaining a visited set - all the vertices you "discover" has path from the source, with length equals to the current depth.
  2. If you do a DFS up to depth 2|V|, all the vertices with even-length paths from the source will be discovered in some even depth level. [convince yourself why: what happens for odd-length cycle? what happens for even-length cycle?]

Beware: running time is exponential in the number of vertices [doubled].

amit
  • 175,853
  • 27
  • 231
  • 333
  • hi Amit. thank you for your answer but its not very clear to me// what do you mean by "DFS up to depth 2|v|"?? – Bobbbaa Apr 08 '12 at 13:11
  • @AnatHershkovitz Keep going with DFS until you reached a depth of `2|V|`, where `|V|` is the number of vertices in the graph. – amit Apr 08 '12 at 13:16
  • still not so clear but ill try to figure this out.. thank anyway – Bobbbaa Apr 08 '12 at 13:39
  • Couldn't you speed this up by adding a 'visited by even' property to each vertex like @cloudygoose does? – gcbenison Apr 18 '12 at 03:43
  • @gcbenison: I don't like changing algorithms, I'd rather reduce the problem and use an existing algorithm. [This question](http://stackoverflow.com/q/10190643/572670) is very similar to here, but it also asks for an *efficient* algorithm. I suggested a solution there to reduce the graph `G`, and use DFS, as it is - without changing it - it makes analyzing and proving correctness much easier. – amit Apr 18 '12 at 07:38
2

For each node i, create 3 boolean states
(i,0):unreached
(i,1):can be reached by odd length
(i,2):can be reached by even length
initially they are all zeros
then, you do the dfs, can within the dfs change the states of the node that you visit
if you find that you won't change the node's state then stop this thread.
Because there are totally 3*n states that you can change, so the max time you need is
O(m) with is the number of the edges~

cloudygoose
  • 608
  • 1
  • 6
  • 16
  • If you reach a node having state 'even' with an odd number of steps, you wouldn't want to change its state to 'odd' at that point, would you? (Yet your rules as stated seem to say that you would) – gcbenison Apr 18 '12 at 03:42
0

Are you concerned about running time? Has that been discussed yet? Have you talked about efficient data structures for sets? If no, then amit's hint should help you.

If yes, then you should maybe be more clever. You talked about maintaining a set of previously visited vertices when you discussed DFS, yes?

You could instead maintain more than one set with each set meaning something slightly different. You might envision your visited set being the union of these separate sets, but critically you might need to revisit a vertex if it appears in one and then appears in another later.

Ask yourself : What sets might I put vertices into? If a vertex is already joined some set by being visited, when might I need to revisit it? Be careful, you can easily make a mistake here.

Jeff Burdges
  • 4,204
  • 23
  • 46