0

Suppose we have an undirected graph, and two nodes A and B. I need to write a method to find a path without cycles between A and B. All edges of this graph have the same weight. The method must terminate as soon as it finds such a path. How can I implement this?

p1100i
  • 3,546
  • 2
  • 25
  • 44
boris
  • 343
  • 4
  • 12
  • 2
    Research Dijkstra's Algorithm (https://en.wikipedia.org/wiki/Dijkstra's_algorithm), which can also be implemented as an instance of A-star (https://en.wikipedia.org/wiki/A*_search_algorithm) with a constant heuristic of 0. – Pieter Geerkens Jun 13 '15 at 14:17
  • 3
    is this your homework? – Noctis Jun 13 '15 at 14:18
  • @Noctis: I'm not a student – boris Jun 13 '15 at 14:27
  • 1
    your question is vague, on a topic that is fairly known, you didn't say what's your problem, and you just ask for a solution. This is not the site for that. Seems that @Margus gave an answer nevertheless :). – Noctis Jun 13 '15 at 14:36
  • Is this a directed or undirected graph? – johnnyRose Jun 13 '15 at 14:48
  • @johnnyRose: undirected – boris Jun 13 '15 at 14:50
  • You use Dijkstra. First you must build the graph. The mark all visited nodes as false. The try every path until you find 1st successful path. As you move through the graph mark visited nodes so you don't go back to a node already tested. You are not asking for shortest path just the first path you find. – jdweng Jun 13 '15 at 14:54
  • Could you **post** what have tried so far ? – hdoghmen Jun 13 '15 at 15:02

2 Answers2

0

Without major limitations you can visit the graph as you like. 2 most common are: enter image description here

For more help i advice you try someting and show some effort on your part.

Community
  • 1
  • 1
Margus
  • 18,332
  • 12
  • 51
  • 101
  • 1
    This is a binary tree, not a directed/undirected graph. There are similarities, but perhaps a different diagram would be beneficial. – johnnyRose Jun 13 '15 at 14:46
  • It is a tree and 2 search techniques. Graph does not have costs and have no cycles therefore visiting the graph simplifies to a tree. Hope you understand now. – Margus Jun 13 '15 at 15:13
  • 1
    The question never stated the **graph** couldn't have cycles. The question states the final **path** between points A and B had to be acyclic. – johnnyRose Jun 13 '15 at 15:14
0

You can find here an implementation of Dijkstra's algorithm Algorithm to :

Find The Shortest Path

hdoghmen
  • 2,525
  • 4
  • 26
  • 28
  • Link-only answers are extremely discouraged. Please summarize an answer to the problem, and provide links for further learning. – johnnyRose Jun 13 '15 at 14:56
  • I don't need to find the shortest path – boris Jun 13 '15 at 14:58
  • 2
    @BORIS So? You only specified that you needed *a* path. The shortest path is definitely a path. It's also pretty easy to find. – Kyle Jun 13 '15 at 15:19
  • 1
    Dijkstra is an overkill for unweighted graph. Breadth first search would work just fine in this case and easier to implement. – n0rd Jun 13 '15 at 17:22