Graph Theory Basics

Core Concepts

TermDefinition
Vertex (Node)Fundamental unit of a graph
EdgeConnection between two vertices
Directed Graph (Digraph)Edges have direction (Aโ†’B โ‰  Bโ†’A)
Undirected GraphEdges have no direction
Weighted GraphEdges have numeric weights/costs
DegreeNumber of edges connected to a vertex
PathSequence of vertices connected by edges
CyclePath that starts and ends at same vertex
Connected GraphPath exists between every pair of vertices
TreeConnected acyclic undirected graph
DAGDirected Acyclic Graph โ€” no directed cycles
Spanning TreeTree that connects all vertices

Algorithm Complexity Reference

BFS โ€” Breadth-First Search
Time: O(V + E)
Space: O(V)

Shortest path in unweighted graphs

DFS โ€” Depth-First Search
Time: O(V + E)
Space: O(V)

Cycle detection, topological sort

Dijkstra's Algorithm
Time: O((V+E) log V)
Space: O(V)

Shortest path, non-negative weights

Bellman-Ford
Time: O(V ยท E)
Space: O(V)

Handles negative weights

Floyd-Warshall
Time: O(Vยณ)
Space: O(Vยฒ)

All-pairs shortest paths

Kruskal's MST
Time: O(E log E)
Space: O(V)

Minimum spanning tree

Prim's MST
Time: O(E log V)
Space: O(V)

Dense graphs preferred

Topological Sort
Time: O(V + E)
Space: O(V)

Ordering of DAG vertices