Data Structures & Algorithms: Patterns of Graph Problems

Introduction

A deep dive on algorithms pertinent to graph problems within the realm of data structures & algorithms.

    ♦️ BFS

    Shortest path in unweighted graphs, level-order traversal, finding the minimum number of steps.

    Here are the Key Features sections for the requested algorithms:

    • Key Features:

      • Explores nodes level by level (breadth-first).
      • Guarantees the shortest path in unweighted graphs.
      • Uses a queue for traversal.
      • Commonly used for:
        • Finding the shortest path in unweighted graphs.
        • Level-order traversal of trees.
        • Solving problems involving minimum steps or distances.
    • LeetCode Problems:


    ♦️ DFS

    Detecting cycles, connected components, topological sorting.

    • Key Features:

      • Explores as far as possible along each branch before backtracking (depth-first).
      • Uses a stack (explicit or recursion) for traversal.
      • Commonly used for:
        • Detecting cycles in graphs.
        • Finding connected components.
        • Topological sorting in Directed Acyclic Graphs (DAGs).
        • Solving maze and path finding problems.
    • LeetCode Problems:


    ♦️ Priority Queue

    Dijkstra’s algorithm for shortest path in weighted graphs, Prim’s algorithm for Minimum Spanning Tree (MST).


    🔸 Dijkstra's Algorithm

    Dijkstra's algorithm is used to find the shortest path from a source node to all other nodes in a weighted, non-negative graph. It uses a priority queue (min-heap) to always extend the path with the lowest cost first. It is optimal for graphs with non-negative weights but does not handle negative cycles.

    • Key Features:

      • Finds the shortest path from a source node to all other nodes in a weighted graph.
      • Works only with non-negative edge weights.
      • Uses a priority queue (min-heap) to extend the path with the lowest cost first.
      • Inefficient for graphs with negative weights (use Bellman-Ford instead).
      • Commonly used for:
        • Network routing.
        • Pathfinding in weighted graphs.
    • LeetCode Problems:


    🔷 A* Search Algorithm

    The A* Search Algorithm is a heuristic-based algorithm used for pathfinding and graph traversal. It is widely used in AI and game development.


    🔸 Prim's Algorithm

    Prim's algorithm is used to find the Minimum Spanning Tree (MST) of a connected, weighted, and undirected graph. It builds the MST by greedily choosing the minimum edge that connects a vertex in the tree to a vertex outside the tree, using a priority queue for efficiency.


    ♦️ Union-Find

    Finding connected components, cycle detection in undirected graphs, Kruskal’s MST.

    • Key Features:

      • A data structure used to efficiently manage and merge disjoint sets.
      • Supports two main operations:
      • Find: Determines which set a particular element belongs to.
      • Union: Merges two sets into one.
      • Commonly used for:
        • Detecting cycles in undirected graphs.
        • Finding connected components.
        • Supporting Kruskal’s algorithm for Minimum Spanning Tree (MST).
    • LeetCode Problems:


    🔸 Kruskal's Algorithm

    Kruskal's algorithm is also used to find the Minimum Spanning Tree (MST) of a graph. It works by sorting all edges by weight and adding them one by one to the MST, ensuring that no cycles are formed (using Union-Find for cycle detection).


    ♦️ Topological Sort

    Topological Sort is used to order nodes in a Directed Acyclic Graph (DAG) such that for every directed edge U -> V, node U appears before node V in the ordering. It's particularly useful for task scheduling and dependency resolution.


    ♦️ Bellman-Ford Algorithm

    The Bellman-Ford Algorithm is used to find the shortest path from a single source to all other vertices in a graph. It works with graphs that have negative weight edges but cannot handle negative weight cycles.


    ♦️ Floyd-Warshall Algorithm

    The Floyd-Warshall Algorithm is used to find the shortest paths between all pairs of vertices in a graph. It works for both directed and undirected graphs and handles negative weights but not negative weight cycles.


    ♦️ Tarjan's Algorithm

    The Tarjan's Algorithm is used to find strongly connected components (SCCs) in a directed graph. It uses depth-first search (DFS) and is highly efficient.


    ♦️ Kosaraju's Algorithm

    The Kosaraju's Algorithm is another method to find SCCs in a directed graph. It involves two passes of DFS: one on the original graph and one on the transposed graph.


    ♦️ Edmonds-Karp Algorithm

    The Edmonds-Karp Algorithm is an implementation of the Ford-Fulkerson method for finding the maximum flow in a flow network. It uses BFS to find augmenting paths.


    ♦️ Hopcroft-Karp Algorithm

    The Hopcroft-Karp Algorithm is used to find the maximum matching in a bipartite graph. It alternates between BFS and DFS to find augmenting paths.


    ♦️ Johnson's Algorithm

    The Johnson's Algorithm is used to find shortest paths between all pairs of vertices in a sparse graph with negative weights. It combines Bellman-Ford and Dijkstra's algorithms.


    Conclusion

    Mastery of these algorithms will develop your graph problem solving intuition substantially.