Fast Algorithm for Determining Path Existence in a Graph?

Click For Summary

Discussion Overview

The discussion revolves around the search for a fast algorithm to determine the existence of a path between two nodes in a graph. Participants explore various algorithmic approaches, including considerations for both finding a path and simply verifying its existence, while addressing the performance implications of different methods.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant inquires about an O(1) or O(log n) algorithm for determining path existence between two nodes in a graph.
  • Another participant suggests that if only the existence of a path is needed, Depth-First Search (DFS) or Breadth-First Search (BFS) can be used, both of which operate in O(n) time.
  • A method involving the conversion of the graph to an adjacency matrix is proposed, where matrix exponentiation could theoretically allow for O(1) queries after preprocessing, though concerns about the O(n^2) size of the matrix are raised.
  • It is noted that for unweighted graphs, BFS can find an optimal path without needing more complex algorithms like Dijkstra's.
  • A participant expresses that the adjacency matrix approach is impractical due to its size and resulting computational complexity.
  • Another participant reiterates the suggestion to perform a DFS or BFS to determine path existence directly.

Areas of Agreement / Disagreement

Participants generally agree that DFS or BFS is a suitable approach for determining path existence. However, there is no consensus on the feasibility or efficiency of the adjacency matrix method, with some participants questioning its practicality.

Contextual Notes

The discussion highlights limitations regarding the size of the adjacency matrix and the computational complexity of matrix operations, which may affect the proposed methods' efficiency.

sid_galt
Messages
502
Reaction score
1
Is there an algorithm which is O(1) or O(log n) (basically a very fast algorithm) which can tell whether there is a path from a node A to a node B in a graph?

Thanks
 
Last edited:
Technology news on Phys.org
Do you have to find a path? Or do you just want to know whether a path exists?

If you want to find a path, I suggest looking at the algorithms listed on the wiki for "shortest path problem". In particular dijkstra [sp]'s algorithm is like O(n log n) if you implement it smartly.

If you just want to know whether one exists... in that case you can just do a normal Depth-First Search or Breadth-First Search, both of which are like O(n).

If you have one graph and you will be performing lots of queries on it there is one trick where, as described here, if you convert the graph to an adjacency matrix, and then take that matrix to the power of N, then the resulting matrix A_{ij} will tell you whether a path of length N exists between i and j. So if you just take the initial adjacency matrix to the powers 1 through [longest path], and save the results as you go to another matrix, then you'll have a nice little cache that you can do O(1) queries from. (I actually don't know if this is the best way to create such a cache, it's just the only thing I can think of off the top of my head! Actually I think matrix multiplication costs O(n^3) or something, so it seems certain that a more direct method could do the same thing much faster.)

EDIT: Also, note that the "shortest path algorithm"s listed are for weighted graphs. For an unweighted graph, a breadth-first search will actually find an optimal path and you won't even have any need for a fancy algorithm ilke dijikstra [sp]'s.
 
Last edited:
Thanks for the reply. I just need a true and false answer as to whether a path exists or not.
Unfortunately the adjacency matrix trick won't work because the adjacency matrix itself is
n^2 big (n being the number of nodes in the graph) making the whole operation atleast
O(n^2).
 
Just go ahead and do a DFS or BFS starting at the beginning of the path then.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
14
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
3
Views
2K