Important Graph Algorithms for Problem solving
Below is the important Graph Algorithms for problem solving
![]() |
Algorithms |
Cycle Detection Algorithm
For Directed graph Cycle can be detected using :
1) DFS-Based Cycle Detection using color marking (visited array)
void dfs(node):
mark visited
for neighbor:
if not visited:
dfs(neighbor)
stack.push(node)
2) Kahn’s Algorithm :
prerequisites
array.true
; otherwise, there’s a cycle.Example Question: https://leetcode.com/problems/course-schedule/description/
For UnDirected graph Cycle can be detected using :
1) DFS to check for back edges
2) Union Find algorithm
Example Question: https://leetcode.com/problems/redundant-connection/description/
Bridge Finding (critical connection)
A bridge (critical connection) is an edge (u, v)
such that removing it increases the number of connected components.
For UnDirected graph Finding bridges:
1) Tarjan’s Algorithm (DFS-Based)
How Tarjan’s Algorithm Works?
- DFS Traversal: Assign each node a discovery time (
disc[]
). - Track the lowest reachable ancestor (
low[]
) for each node. - Bridge Condition: If
low[v] > disc[u]
, then edge(u, v)
is a bridge.
Example Question: https://leetcode.com/problems/critical-connections-in-a-network/description/
Comments
Post a Comment