Graph
Traversal
- Time Complexity: O(V + 2E)
 - Space Complexity: O(V)
 
- Time Complexity: O(V + E)
 - Space Complexity: O(V)
 
Detect Cycle
Refer Detect Cycle Demo for all implementations
- Undirected Graph
- Time Complexity: O(V + 2E)
 - Space Complexity: O(V)
 
 - Directed Graph
- Time Complexity: O(V + E)
 - Space Complexity: O(V)
 
 
- Undirected Graph
- Time Complexity: O(V + 2E)
 - Space Complexity: O(V)
 
 - Directed Graph
- Not supported
 
 
- Undirected Graph
- Not supported (this algo relies on Topological Sort which doesn't work with Undirected Graph)
 
 - Directed Graph
- Time Complexity: O(V + E)
 - Space Complexity: O(V)
 
 
Shorted Path
Refer Shortest Path Demo for all implementations
If a better distance to v is found, consider the shortest distance to v is from u and update the distance.
if (distance[u] + weight < distance[v]) {
  distance[v] = distance[u] + weight
}- Finds the shortest path from single source to all vertices.
 - ❌ Dijkstra's Algorithm will not work with -ve weights and goes into infinite loop.
 - Time Complexity: O(E * logV)
 
- Find the smallest distance using 
PriorityQueueand apply relaxation for each neighbour. - Eventually 
PriorityQueuewould've visited all the vertices via the shortest way possible. 
- Finds the shortest path from single source to all vertices.
 - Only works with Directed Graph. When an Undirected Graph is given, it needs to be converted to a Directed Graph.
 - Works with -ve weights and helps in detecting -ve cycles.
 - To detect a -ve cycle, Vth iteration would still decrease the distance.
 - Time Complexity: O(V * E)
 
- Apply relaxation for each vertex neighbours for 
V - 1times. - In each iteration, a new shortest distance from 
u -> vis found or updated. - Since we have 
Vvertices, looping for at-leastV - 1times is required to find shortest distance between all vertices. 
- Finds the shortest path from multiple sources to all vertices.
 - Only works with Directed Graph. When an Undirected Graph is given, it needs to be converted to a Directed Graph.
 - Works with -ve weights and helps in detecting -ve cycles.
 - To detect a -ve cycle, one of the vertex self loop would be negative.
 - Time Complexity: O(V ^ 3)
 
- Finds the distance between 
utovby going via all other vertices.distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j])
 
Minimum Spanning Tree (MST)
Refer Minimum Spanning Tree Demo for all implementations
- Spanning Tree means a tree having 
Vvertices andV - 1edges and all vertices are reachable from each other. - Spanning Tree will not form a cycle.
 - A graph can have multiple spanning trees.
 - The spanning tree having minimum cost (path sum) is called Minimum Spanning Tree (MST).
 
- Finds MST.
 - This is a Greedy algorithm.
 - Time Complexity: O(E * logE)
 
- Find the smallest distance using 
PriorityQueueand forms an edge to a neighbour having the smallest weight. - Eventually all the vertices will be visited and smallest edge to it's neighbour would be formed.
 - This ensures that each vertex is reachable from each other without forming a cycle.
 
- Finds MST.
 - This is a Greedy algorithm.
 - Time Complexity: O(E * logE) + O(E * 4α * 2)
 
- Sort all edges by weight. Picks the edge having the smallest weight which upon adding, the graph doesn't form a cycle.
 Disjoint Setis used to check upon adding an edge betweenuandvwhether a cycle will be formed.- Eventually all the vertices will be visited and smallest edge to it's neighbour would be formed.
 - This ensures that each vertex is reachable from each other without forming a cycle.