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