Catalog / Graph Algorithms Cheatsheet
Graph Algorithms Cheatsheet
A quick reference guide to graph algorithms, commonly used in coding interviews. Covers fundamental algorithms, their complexities, and common use cases.
Graph Representations & Basics
Graph Representations
Adjacency Matrix |
A 2D array where
|
Adjacency List |
An array of lists, where each list
|
Edge List |
A list of tuples, where each tuple
|
Basic Graph Properties
Vertex (Node) |
A fundamental unit in a graph. Represented by a unique identifier. |
Edge |
A connection between two vertices. Can be directed or undirected.
|
Weight |
A value assigned to an edge, representing cost, distance, or other metric. |
Path |
A sequence of vertices connected by edges. |
Cycle |
A path that starts and ends at the same vertex. |
Connected Graph |
A graph where there is a path between every pair of vertices. |
Breadth-First Search (BFS)
BFS Overview
BFS is a graph traversal algorithm that explores the graph level by level, starting from a given source vertex. It uses a queue to maintain the order of vertices to visit.
|
BFS Algorithm Steps
|
BFS Example (Python)
|
Depth-First Search (DFS)
DFS Overview
DFS is a graph traversal algorithm that explores as far as possible along each branch before backtracking. It uses a stack (implicitly through recursion) to keep track of the vertices to visit.
|
DFS Algorithm Steps
|
DFS Example (Python)
|
Dijkstra's Algorithm
Dijkstra's Overview
Dijkstra’s algorithm is used to find the shortest paths from a source vertex to all other vertices in a weighted graph (with non-negative edge weights).
|
Dijkstra's Algorithm Steps
|
Dijkstra's Example (Python)
|