I’m designing an algorithm for finding the minimum distance of a given vertex v from a subset of vertices A(that is from an element of this subset).
I need to find the value k such that:
- distance from x to v is k, for some x in A
- distance from y to v is >=k, for all y in A.
My solution consist in:
- getting the transpose graph G’
- visiting G’ starting from v, using BFS.
- find the minimum distance from the vertices in A
And I think this works and it should run in O(|V|+|E|) time.
My question is: there is a better solution to this problem?
No, there isn’t better solution.
Consider the following:
1-2-3-4,A={4},v=1. you will have to iterate all V,E in the graph [you must read all the path], making this problemOmega(V+E). since your algorithm is correct [simple to prove], and isO(V+E)[triviialy, creating G’ and BFS], and the problem isOmega(V+E), your solution is optimal, in terms of big O notation.