Graph
Typical graph problems
Graph traversal problems: these problems require traversing the graph in some specific order, such as DFS or BFS.
Shortest path problems: these problems require finding the shortest path between two vertices in the graph, such as Dijkstra's algorithm or Bellman-Ford algorithm.
Minimum spanning tree problems: these problems require finding a minimum-weight spanning tree of the graph, such as Kruskal's algorithm or Prim's algorithm.
Cycle detection problems: these problems require detecting whether the graph contains a cycle or not, such as Tarjan's algorithm or Kosaraju's algorithm.
Topological sorting problems: these problems require finding a valid topological ordering of the vertices in a directed acyclic graph, such as Kahn's algorithm or DFS-based algorithm.
Bipartite graph problems: these problems require determining whether a graph is bipartite or not, such as the coloring algorithm or BFS-based algorithm.
Connectivity problems: these problems require determining the connectivity of the graph, such as finding strongly connected components or articulation points.
Top Leetcode graph problems
Number of Islands
Course Schedule (and Course Schedule II)
Clone Graph
Word Ladder (and Word Ladder II)
Surrounded Regions
Graph Valid Tree
Pacific Atlantic Water Flow
Redundant Connection (and Redundant Connection II)
Perfect Squares
Shortest Path in a Grid with Obstacles Elimination
Top graph algorithms
Depth-first search (DFS): This algorithm can be used to traverse a graph and explore its vertices and edges. DFS can also be used for topological sorting, cycle detection, and finding strongly connected components.
Breadth-first search (BFS): This algorithm can be used to traverse a graph and explore its vertices and edges, similar to DFS. BFS can also be used for finding the shortest path between two vertices in an unweighted graph.
Dijkstra's algorithm: This algorithm can be used to find the shortest path between two vertices in a weighted graph. It works by iteratively selecting the vertex with the smallest distance from the source vertex and updating the distances of its neighbors.
Bellman-Ford algorithm: This algorithm can be used to find the shortest path between two vertices in a weighted graph, even if the graph contains negative-weight edges. It works by relaxing the edges of the graph repeatedly until the shortest paths are found.
Floyd-Warshall algorithm: This algorithm can be used to find the shortest path between all pairs of vertices in a weighted graph. It works by considering all possible intermediate vertices and updating the shortest distances accordingly.
Kruskal's algorithm: This algorithm can be used to find the minimum spanning tree of a weighted undirected graph. It works by iteratively adding the smallest edge that does not create a cycle until all vertices are connected.
Prim's algorithm: This algorithm can also be used to find the minimum spanning tree of a weighted undirected graph. It works by iteratively adding the smallest edge that connects a vertex in the tree to a vertex outside the tree.
Tarjan's algorithm: This algorithm can be used to find the strongly connected components of a directed graph. It works by performing a DFS and maintaining a stack of visited vertices.
Last updated