patternMinor
Which graph algorithm should I use?
Viewed 0 times
graphalgorithmshouldwhichuse
Problem
I need to find the shortest path in a Directed Unweighted Cyclic graph. And it has to be optimal (find a path if exists one) and also optimal in terms of space and time complexity, being time complexity the most important.
I think BFS is the solution. But am not sure.
What I am doing is mapping database tables into a graph data structure. And I need the shortest path for querying the tables with joins. Am storing information in the edges related to the nature of the relationship between the tables, like foreign key field and type of relation (one2many, one2one, etc).
I think BFS is the solution. But am not sure.
What I am doing is mapping database tables into a graph data structure. And I need the shortest path for querying the tables with joins. Am storing information in the edges related to the nature of the relationship between the tables, like foreign key field and type of relation (one2many, one2one, etc).
Solution
You can use bidirectional BFS. It will find a shortest path in an unweighted graph much faster than the traditional BFS.
The only additional requirement in using a bidirectional search is that you can efficiently compute the parent nodes of any given node.
If you ask why, the argument is simple: BFS runs in $\mathcal{O}(\sum_{ = 0}^N b^i)$, whereas the bidirectional BFS runs in $\mathcal{O}(2 \sum_{i = 0}^{\lceil N / 2 \rceil}b^i)$, where $b$ is the average node degree and $N$ is the length of a shortest path. So by carefully choosing the graph statistics, you can make bidirectional BFS arbitrarily faster than traditional BFS. Actually, twice I have seen a speed up of 1000.
The only additional requirement in using a bidirectional search is that you can efficiently compute the parent nodes of any given node.
If you ask why, the argument is simple: BFS runs in $\mathcal{O}(\sum_{ = 0}^N b^i)$, whereas the bidirectional BFS runs in $\mathcal{O}(2 \sum_{i = 0}^{\lceil N / 2 \rceil}b^i)$, where $b$ is the average node degree and $N$ is the length of a shortest path. So by carefully choosing the graph statistics, you can make bidirectional BFS arbitrarily faster than traditional BFS. Actually, twice I have seen a speed up of 1000.
Context
StackExchange Computer Science Q#76159, answer score: 2
Revisions (0)
No revisions yet.