Comparing Single Source Shortest Path and All Pairs Shortest Path Algorithms

 
Single Source Shortest Path Algorithms
BFSDijkstra's AlgorithmBellman Ford Algorithm
Time ComplexityO(V^2)O(V^2)O(E.V)
Space ComplexityO(E)O(V)O(V)
LimitationDoes not work for weighted graphs in general.

If all weight is same in weighted graph or can be made same, then it will work for weighted graph as well
Works for weighted as well as unweighted graph.

Does not work is there is a negative cycle in the graph.
Not able to detect negative cycle.
No limitation for working. Works for all kinds of graph as well as able to detect negative cycle in the graph.

Only disadvantage is it's runtime complexity is highest here as compared to BFS, Dijkstra and Floyd Warshall
Working for Unweighted Graphuse this as it is easy to implementAvoid it as we have to implement a min heap too in this case which will slow this downAvoid it as Time Complexity is not good
Working for Weighted Graph (no negative cycle)Not supportedcan be usedAvoid it as Time Complexity is not good
Working for Weighted Graph ( with negative cycle)Not supportedNot supportedOnly choice


All Pairs Shortest Path Algorithms
 
BFSDijkstra's AlgorithmBellman Ford AlgorithmFloyd Warshall Algorithm
Time ComplexityO(V^3)O(V^3)O(E.V^2)O(V^3)
Space ComplexityO(E.V)O(E.V)O(V^2)O(V^2)
LimitationDoes not work for weighted graphs in general.

If all weight is same in weighted graph or can be made same, then it will work for weighted graph as well
Works for weighted as well as unweighted graph.

Does not work is there is a negative cycle in the graph.
Not able to detect negative cycle.
No limitation for working. Works for all kinds of graph as well as able to detect negative cycle in the graph.

Only disadvantage is it's runtime complexity is highest here as compared to BFS, Dijkstra and Floyd Warshall
Works for weighted as well as unweighted graph.

Does not work is there is a negative cycle in the graph.
Not able to detect negative cycle.
Working for Unweighted Graphuse this as it is easy to implementAvoid it as we have to implement a min heap too in this case which will slow this downAvoid it as Time Complexity is not goodcan be used
Working for Weighted Graph (no negative cycle)Not supportedcan be usedAvoid it as Time Complexity is not goodPreferable Choice as easier to implement
Working for Weighted Graph ( with negative cycle)Not supportedNot supportedOnly choiceNot supported

Comments

Popular posts from this blog

Binary Heap - Introduction

Divide and Conquer - Introduction

Bubble Sort