A graph consists of a pair of finite sets, of vertices and of edges. Each edge has a set of either one or two vertices called its endpoints. Graph theory is the study of the mathematics of graphs.
There are two main ways to represent a graph: as a collection of adjacency lists or as an adjacency matrix. List representations are ideal if is much less than , i.e., if the graph is sparse. Matrix representations are ideal for dense graphs, i.e., if is close to .
Quick links:
Basic terminology
An edge with one endpoint is called a loop. Two or more distinct edges with the same endpoints are parallel. An edge connects its endpoints, and two vertices connected by an edge are adjacent. If a vertex is an endpoint of a loop, then it is adjacent to itself. An edge is incident on each of its endpoints. Two edges incident on the same endpoint are adjacent. A vertex with no incident edges is isolated.
A directed graph (or digraph) is where the edges have a direction, i.e., for , is a source and is a target for . An acyclic graph has no loops. Acyclic directed graphs (or dags) are an important type of graph. A weighted graph is one where each edge has an associated weight given by a weight function .
A simple graph doesn’t have any loops or parallel edges. A complete graph on vertices (where ) is a simple graph with vertices and exactly one edge connecting each pair of distinct vertices. A graph is a sub-graph of if every vertex in is also a vertex in , every edge in is also an edge in , and every edge in has the same endpoints as it has in .
The degree of a vertex , , is the number of edges that are incident on (i.e., how many neighbours it has). An edge that is a loop is counted twice. The total degree of is the sum of the degrees of all the vertices of .
A path in the graph is a sequence where (i.e., the start and end of the sequence are vertices). Additionally, for every , if , then . If is a path, the length is the number of edges in the path.
A subgraph is defined as a graph with vertices/edges if and only if , and if , then . In other words, a graph is a subgraph of if all of its vertices are a subset of ‘s vertices, and all of its edges are a subset of ‘s edges. There’s also a restriction that any edge must have its vertices in as well.
More terminology
Graphs with vertices have:
- At least:
- Connected graphs: edges.
- Unconnected graphs: 0 edges.
- At most:
- Simple graphs: .
- Not simple: no upper bound
- In an undirected graph, the number of edges and degrees of the vertices is: , where is the number of edges.
Traversal can be done in a few specific ways:
- A walk is a sequence of vertices between which there are edges, but where vertices and edges can be repeated.
- A trail is a walk with no repeated edges.
- A path is a trail with no repeated vertices, except at the start and end vertex.
- Circuits have trails that begin and end at the same vertex.
- Cycles have paths that begin and end at the same vertex. Graphs with cycles are cyclic. Trees are special acyclic, connected graphs.
There is an isomorphism between two graphs and if there exists an injective correspondence between and that preserves the edge endpoints of both graphs. In other words, is isomorphic to if it is essentially the same graph (same set of vertices, and edges that connect to the same vertices).
Advanced terminology
For a directed graph , a diagram on the graph is a family of sets associated with each vertex and a family of maps associated with each edge such that if , then has domain and codomain .
If is a non-zero length path in a diagram on , the composition along is the map . A diagram is commutative if for every two vertices and any two paths with source and target , the composition along is equal to the composition along .
In other words, if there are two different paths between two vertices, the diagram is commutative if the composition of the maps along each path is equal.
homomorphisms, isomorphisms, automorphisms
Graph problems
The benefit of common graph traversal algorithms is that we can check the properties of graphs. For instance, some simple applications of these algorithms involve:
- Connectivity checks (i.e., are certain nodes connected to other nodes?)
- Finding graph cycles
- Checking for bipartiteness
In general, we should follow the same competitive programming problem-solving workflow. After reading and understanding the problem, we should consider what the best way to model the problem is. In particular, if the graph is given to us in a non-standard representation, we should convert to an adjacency list or matrix.
Sub-pages
- Graph algorithms
- Graph problems
- Graph applications