Reputation: 3
I'm trying to write an abstract algorithm using graph theory.
Given an undirected and unweighted graph G=(V,E), two vertices: m,n∈V and a specific edge e∈E. I want to check if edge e is a part of all the shortest paths between m and n.
My algorithm:
do BFS scan from m until reach to n \\ O(V+E)
return edge e into G. \\ O(1)
Time Complexity analysis: O(V+E)
Memory analysis: extra O(2) for temp and ret.
What do you say? Is it correct, or you have a better idea with less time complexity?
Upvotes: 0
Views: 308
Reputation: 153
It would not be O(V+E). The worst case in a breadth first search is that every edge has been searched, at which point if the node has not been found then the search fails. This gives this section a complexity of O(|E|), since this is the dominant calculation, all others O(1), the complexity is O(|E|).
Upvotes: 1
Reputation: 178451
Your algorithm seems correct, and will indeed find if e
is participating in all unweighted-shortest paths.
Proof:
e
is in all shortest paths -> removing e
will negate all this
paths -> the new shortest path will be longer than the old one -> the algorithm yields the correct answer.e
is not participating in some shortest path (let it be p
) -> by removing e
, the path p
remains, and thus the shortest distance from m
to n
remains the same -> The algorithm yields the correct answer.However, your memory analysis is wrong.
BFS requires O(V)
extra space, for a visited set - so the extra space is O(V)
Upvotes: 0