13.1 Definitions and properties

A graph ๐†=(๐•,๐„)\mathbf{G} = (\mathbf{V}, \mathbf{E}) consists of a set of vertices ๐•\mathbf{V} and a set of edges ๐„\mathbf{E}, such that each edge in ๐„\mathbf{E} is a connection between a pair of vertices in ๐•\mathbf{V}. The number of vertices is written |๐•||\mathbf{V}|, and the number of edges is written |๐„||\mathbf{E}|. |๐„||\mathbf{E}| can range from zero to a maximum of |๐•|2โˆ’|๐•||\mathbf{V}|^2 - |\mathbf{V}|.

Note: Some graph applications require that a given pair of vertices can have multiple or parallel edges connecting them, or that a vertex can have an edge to itself. However, the applications discussed here do not require either of these special cases. To simplify our graph API, we will assume that there are no duplicate edges.

A graph whose edges are not directed is called an undirected graph, as shown in part (a) of the following figure. A graph with edges directed from one vertex to another (as in (b)) is called a directed graph or digraph. A graph with labels associated with its vertices (as in (c)) is called a labeled graph. Associated with each edge may be a cost or weight. A graph whose edges have weights (as in (c)) is said to be a weighted graph. These are depicted in Figureย 13.1.

Figure 13.1: Some types of graphs

An edge connecting vertices aa and bb is written (a,b)(a, b). Such an edge is said to be incident with vertices aa and bb. The two vertices are said to be adjacent. If the edge is directed from aa to bb, then we say that aa is adjacent to bb, and bb is adjacent from aa. The degree of a vertex is the number of edges it is incident with. For example, vertex ee in Figureย 13.2 has a degree of three.

In a directed graph, the out degree for a vertex is the number of neighbours adjacent from it (or the number of edges going out from it), while the in degree is the number of neighbours adjacent to it (or the number of edges coming in to it). In Figureย 13.1 (c), the in degree of vertex 1 is two, and its out degree is one.

Figure 13.2: Neighbors and incidence

A sequence of vertices v1,v2,...,vnv_1, v_2, ..., v_n forms a path of length nโˆ’1n-1 if there exist edges from viv_i to vi+1v_{i+1} for 1โ‰คi<n1 \leq i < n. A path is a simple path if all vertices on the path are distinct. The length of a path is the number of edges it contains. A cycle is a path of length three or more that connects some vertex v1v_1 to itself. A cycle is a simple cycle if the path is simple, except for the first and last vertices being the same. These are depicted in Figureย 13.3.

Figure 13.3: Different types of paths

An undirected graph is a connected graph if there is at least one path from any vertex to any other. The maximally connected subgraphs of an undirected graph are called connected components. For example, Figureย 13.4 shows an undirected graph with three connected components.

Figure 13.4: An undirected graph with three connected components

A graph with relatively few edges is called a sparse graph, while a graph with many edges is called a dense graph. A graph containing all possible edges is said to be a complete graph. A subgraph ๐’\mathbf{S} is formed from graph ๐†\mathbf{G} by selecting a subset ๐•s\mathbf{V}_s of ๐†\mathbf{G}โ€™s vertices and a subset ๐„s\mathbf{E}_s of ๐†\mathbf{G} โ€™s edges such that for every edge eโˆˆ๐„se \in \mathbf{E}_s, both vertices of ee are in ๐•s\mathbf{V}_s. Any subgraph of VV where all vertices in the graph connect to all other vertices in the subgraph is called a clique. See Figureย 13.5 for examples of these kinds of graphs.

Figure 13.5: Sparse, dense and complete graphs

A graph without cycles is called an acyclic graph. Thus, a directed graph without cycles is called a directed acyclic graph or DAG. They are shown in Figureย 13.6.

Figure 13.6: Acyclic graph types

A free tree is a connected, undirected graph with no simple cycles. An equivalent definition is that a free tree is connected and has |๐•|โˆ’1|\mathbf{V}| - 1 edges.