• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Depth first search recursive, problem on graph[picture + code]

 
Ranch Hand
Posts: 67
PHP Debian Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i got graph and i have to get path to the number what is searched. Like in example n1.dfs(visited, 15); should end on when 15 is found but it wont currently. As you can see it visits all nodes, but how would i extract from visited nodes the path.
Also when i reach end it keeps visiting sub nodes.

Full graph is here http://img19.imageshack.us/i/graphsz.png/

Node.java


Test.java

 
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of data structure is that? It looks to me like a binary tree. So what is the algorithm for a depth first search? If you are getting the wrong result you are using the wrong algorithm. What do you mean by path? Are you going to have a linked list of nodes to the number sought? Or a String?

You will have to use the very expensive hardware I so often recommend: a Cray occupying a building the size of a cinema pencil, eraser, and sheet of paper. ?Write down how you are going to tell whether you have found a value, and how you are going to add it to a path. Since it is recursive, start with a little tree, like this oneThen take your recursion one level higherand work out how you are to handle something likeGet that sort of thing working, and it becomes easy to scale up for recursion over any kind of tree. You can work out the depth at which you found the value, from the length of the path.
 
mandlar suurla
Ranch Hand
Posts: 67
PHP Debian Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What sort of data structure is that? It looks to me like a binary tree. So what is the algorithm for a depth first search? If you are getting the wrong result you are using the wrong algorithm. What do you mean by path? Are you going to have a linked list of nodes to the number sought? Or a String?

You will have to use the very expensive hardware I so often recommend: a Cray occupying a building the size of a cinema pencil, eraser, and sheet of paper. ?Write down how you are going to tell whether you have found a value, and how you are going to add it to a path. Since it is recursive, start with a little tree, like this oneThen take your recursion one level higherand work out how you are to handle something likeGet that sort of thing working, and it becomes easy to scale up for recursion over any kind of tree. You can work out the depth at which you found the value, from the length of the path.

I am sorry that i draw the binary tree, i forgot to mention the cycles are allowed like node 5 and 10 could be connected. It needs to find path between 2 nodes, no dijkstra and A* algorithms( i dont need to find shortest). Currently my code traverses all the nodes and doesn't deal with path finding. So i tought i need Stack to hold path, but how i know which element is part of path.
 
Campbell Ritchie
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know offhand. It is easy enough to find the shortest path through a binary tree, but I can't remember offhand how you do it through a graph with rejoining.

Again, you will have to write down the algorithm, and convert it to pseudo-code, and show us all that. Then we can see whether your code matches it. And you will have to beware of a graph which runs like this . . . for obvious reasons
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic