Thread: Shortest path problem for undirected graph, with no weights per edge

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also, it sounds like the way you want to store your nodes is in an adjacency matrix/list. Or maybe, some partial version of that, only concerned with nodes that are close to the current node.

    One more good reference, to help get you using the right language: Glossary of graph theory - Wikipedia, the free encyclopedia.

    Note, if time is of little concern, but space is, you can use DFS to exhaustively search every possible path.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by anduril462 View Post
    Note, if time is of little concern, but space is, you can use DFS to exhaustively search every possible path.
    Why prefer DFS over BFS?

    The goal is the time. The faster, the better!

    As for the algorithms, I think that I should avoid those that have E in their time complexity and only check the ones left and find the "optimal" from them (the ones that have only V inside them).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by std10093 View Post
    Why prefer DFS over BFS?
    I'd query that too, without knowing more about the specific problem. Each has tradeoffs.

    Quote Originally Posted by std10093 View Post
    As for the algorithms, I think that I should avoid those that have E in their time complexity and only check the ones left and find the "optimal" from them (the ones that have only V inside them).
    I'd look a bit more carefully than that. Some of the algorithm with complexity only in terms of V have higher powers (for example, because the number of edges is a more-than-linear function of number of vertices).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by grumpy View Post
    I'd query that too, without knowing more about the specific problem. Each has tradeoffs.
    Didn't I state the specific problem? What more should I state?

    Quote Originally Posted by grumpy View Post
    Some of the algorithm with complexity only in terms of V have higher powers (for example, because the number of edges is a more-than-linear function of number of vertices).
    What do you mean by "higher powers"? Probably I am lost in the language here. Also the example is not clear to me too. Sorry :/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by std10093 View Post
    Why prefer DFS over BFS?


    The goal is the time. The faster, the better!
    You made no mention of runtime needs, yet did mention that the size of the graph was large, and that it may exceed memory. That, combined with your "background" paragraph -- particularly the part about now wanting to hold the entire graph in memory -- led me to believe you were more concerned with memory than speed, which you didn't mention at all. Note, most people optimize for time nowadays, but optimizing for space is still something that people do.
    Quote Originally Posted by std10093 View Post
    Didn't I state the specific problem? What more should I state?
    Given that grumpy found it unclear, and I misunderstood what you were getting at, it may be underspecified. Now we know you're optimizing for speed. But we don't know how much bigger |E| is than |V|.
    Quote Originally Posted by std10093 View Post
    As for the algorithms, I think that I should avoid those that have E in their time complexity and only check the ones left and find the "optimal" from them (the ones that have only V inside them).
    Quote Originally Posted by std10093 View Post
    What do you mean by "higher powers"? Probably I am lost in the language here. Also the example is not clear to me too. Sorry :/
    By higher power, grumpy means "order" or "exponent". What grumpy is saying is that, you may find an algorithm where there is only |V| in the time complexity, but it may still be slower than one with |V| and |E|, even if |E| > |V|. For example, assume |V| is large (millions of nodes) and |E| is on the order of 100 |V|.
    Now, imagine you have two algorithms, one with just |V| in the time complexity:
    O(|V|3)
    and another with both |E| and |V| in it
    O(|E| * |V|)
    Which is faster in this case? The second one. The better algorithm will depend on just how much bigger |E| is than |V|. In the above, as long as |E| < |V|2 (which I think it must be -- you cannot have |V|2 edges in an undirected graph), the second algorithm is faster. This is one area where you could have specified better: just how big is |V| and how much bigger is |E| than |V|?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-02-2012, 06:44 AM
  2. shortest path problem
    By bananorama in forum C Programming
    Replies: 7
    Last Post: 05-24-2011, 12:38 PM
  3. Undirected graph and Binary Tree problem
    By arafat21 in forum C Programming
    Replies: 3
    Last Post: 05-02-2008, 05:52 AM
  4. Shortest path problem
    By Digitalxero in forum C++ Programming
    Replies: 0
    Last Post: 10-25-2005, 05:32 PM
  5. Help!!! Shortest path graph
    By hansy32 in forum C Programming
    Replies: 5
    Last Post: 03-01-2002, 06:39 PM