• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Depth First Search in java

 
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a graph of 100000 nodes and i have to do a dfs on it. when i do it the code seems to go on forever. Can anybody tell me whats wrong with my code. if not please tell me how to efficiently achieve my objective.


gr is a graph which is a map of integer and arraylist of its neighboring nodes.
thanks in advance.
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

What type of data structure do you use to do your search.
It is not clear from your post.
In java you can use queue(first input first output), deque(doyble-ended queue) or stack(last input first output).
To get these data structures you should use LinkedList's corresponding methods.

If you want to implemenet your own kind of LinkedList then you need to know internal life of LinkedList.

To know more about stack,queue,deque implemented by LinkedList read my tutorial on Internal Life of LinkedList.
 
Sahil Manchanda
Ranch Hand
Posts: 41
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Volodymyr Levytskyi wrote:Hello!

What type of data structure do you use to do your search.
It is not clear from your post.
In java you can use queue(first input first output), deque(doyble-ended queue) or stack(last input first output).
To get these data structures you should use LinkedList's corresponding methods.

If you want to implemenet your own kind of LinkedList then you need to know internal life of LinkedList.

To know more about stack,queue,deque implemented by LinkedList read my tutorial on Internal Life of LinkedList.


i am using the java class Stack
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.Stack is much like ArrayList, indexed based collection.

I thought you need sequantial access collection like LinkedList.

Now I don't see if you search by index or by object.

If you search by index that it is extremely quick!

If you search by Object then you need to get its index at first by indexOf or lastIndexOf and then do get by found index.



 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.Stack is a legacy class that it's better to avoid using. There are other classes that provide a "stack" (small 's') structure - e.g. LinkedList and ArrayDeque. Have a look at the docs for the Deque interface.
reply
    Bookmark Topic Watch Topic
  • New Topic