Consists of a set of nodes and a set of edges that connect pairs of nodes.
Used in many different domains to model complex relationships and solve various computational problems. Such as social networks, transportation networks, and web page links.
A fundamental point or unit. Aka. vertex. Holds a value.
Connects a pair of nodes. Aka. link. Optionally has an associated weight. Can either be directed (one-way) or undirected (two-way).
Parallel edge
Section titled “Parallel edge”Aka. multi edge. A pair of edges that connect 2 nodes with the same source and destination.
Self edge
Section titled “Self edge”An edge that connects a node to itself.
Adjacency
Section titled “Adjacency”Two nodes are adjacent iff they are connected by an edge.
Degree
Section titled “Degree”Number of edges connected to a vertex. In case of directed graphs, in-degree and out-degree are defined.
In-degree
Section titled “In-degree”Number of edges that point towards a vertex.
Out-degree
Section titled “Out-degree”Number of edges that point away from a vertex.
A sequence of vertices where each adjacent pair is connected by an edge. Number of edges in a path is the length of the graph.
Simple path
Section titled “Simple path”A path that does not visit any vertex more than once.
Closed path
Section titled “Closed path”A path that starts and ends at the same vertex.
A closed path without repeating any edges or vertices.
Path cost
Section titled “Path cost”The sum of the weights of the edges in a path.
Reachability
Section titled “Reachability”A vertex is reachable from vertex iff there exists a path from to .
Complete
Section titled “Complete”A graph is complete iff every pair of distinct vertices is connected by a unique edge.
Subgraph
Section titled “Subgraph”A subset of the vertices and edges of a graph.
Connected component
Section titled “Connected component”A maximal set of vertices such that every pair is connected.
Implementation
Section titled “Implementation”Adjacency Matrix
Section titled “Adjacency Matrix”A 2-dimensional array of size . The rows and columns represent the vertices of the graph. The value at each cell indicates whether there is an edge between the corresponding vertices. Better for dense graph.
Slow to add or remove vertices, because matrix must be resized/copied.
Adjacency List
Section titled “Adjacency List”A collection of linked lists or arrays where each element represents a vertex and contains a list of its adjacent vertices. Better for sparse graph.
Slow to remove vertices and edges, because it needs to find all vertices or edges.
Incidence Matrix
Section titled “Incidence Matrix”A 2-dimensional array of size . The rows represent the vertices and the columns represent the edges. The value at each cell indicates whether the corresponding vertex is incident to the corresponding edge.
Slow to add or remove vertices and edges, because matrix must be resized/copied.
Comparison
Section titled “Comparison”| Adjacency list | Adjacency matrix | Incidence matrix | |
|---|---|---|---|
| Store graph | |||
| Add vertex | |||
| Add edge | |||
| Remove vertex | |||
| Remove edge | |||
| Check adjacency | |||
| Best for | Sparse graphs | Dense graphs | Dense graphs, edge-centric algorithms |
Operations
Section titled “Operations”- Searching
- Shortest Path between 2 vertices
Simple Graph
Section titled “Simple Graph”A graph with no self-loops or multiple edges.
- For directed graphs:
- For undirected graphs:
Connected Graph
Section titled “Connected Graph”When all vertices are reachable from all other vertices.
Dense Graph
Section titled “Dense Graph”A graph is dense iff the number of edges is close to the maximum possible number of edges. Or .
Sparse Graph
Section titled “Sparse Graph”A graph is sparse iff the number of edges is close to the minimum possible number of edges. Or .
Undirected Graph
Section titled “Undirected Graph”Edges have no direction. The edges are not ordered.
Directed Graph
Section titled “Directed Graph”Edges have a direction. The edges are ordered pairs of nodes.
Strongly Connected Graph
Section titled “Strongly Connected Graph”A connected directed graph.
Weakly Connected Graph
Section titled “Weakly Connected Graph”When a directed graph is not connected and its undirected version is connected.
Unweighted Graph
Section titled “Unweighted Graph”All edges have no weights associated with them.
Weighted Graph
Section titled “Weighted Graph”All edges have weights associated with them.
Cyclic Graph
Section titled “Cyclic Graph”A graph with atleast 1 cycle.
Acyclic Graph
Section titled “Acyclic Graph”A graph with no cycles.
A connected, acyclic graph where .
Forest
Section titled “Forest”A collection of Trees.
Bipartite Graph
Section titled “Bipartite Graph”When a graph’s vertices can be divided into two disjoint sets such that every edge connects a vertex in one set to a vertex in the other set. No edge exists between vertices within the same set.
Does not contain odd-length cycles.