Mathematical definitions¶
Graphs as mathematical structures are an important abstraction for reasoning about systems involving networks. Using a mathematical abstraction allows us to present algorithms in a context-independent way; for instance, a graph search algorithm can be applied to both social networks as well as to digital circuits.
Undirected Graphs¶
Mathematically,
an undirected graph is a pair
is a (usually finite) set of vertices, also called nodes, and
is a set of edges between the vertices. The elements in the set are pairs of the form such that and . In this definition, edges between a vertex and itself, called self-loops, are thus forbidden.
The degree of a vertex is the number of edges incident to it; that is, the number of edges including the vertex.
Example
Consider the graph
the vertices
, and the edges
.
It is illustrated graphically below.
The vertices drawn as circles and edges as lines between the vertices.
The degree of the vertex

A path of length
Example
The graph below
has a non-simple path
of length 5 from to , has a simple path
of length 2 from to , and is connected but deleting the edge
would make it disconnected.

A cycle is a path
Directed Graphs¶
Directed graphs are otherwise as undirected graphs except that the edges have a direction.
Formally, a directed graph, or simply digraph,
is a pair
is a (usually finite) set of vertices, and
is a set of edges between the vertices. A pair denotes an edge from the vertex to the vertex .
Example
Consider the directed graph
the vertices
, and the edges
.
It is illustrated graphically below.
Again, the vertices are drawn as circles.
The edges are drawn as lines with direction arrow heads.
Notice the loop from the vertex

The out-degree of a vertex is the number of edges from it
while the in-degree is the number of edges to it.
A path of length
Directed Acyclic Graphs (DAGs)¶
A directed acyclic graph, or simply a DAG, is a directed graph that does not contain any cycles. Such graphs arise in many applications, see, for instance, this Wikipedia page.
Example
DAGs arise naturally in many time- and order-related areas where something has to be done before something else can be done. In such a situation a cycle would mean that some things cannot be done at all. As a toy example, a dependency graph between clothes for dressing up is shown below (source: the Introduction to Algorithms, 3rd ed. (online via Aalto lib) book).
