13.1 Definitions and properties
A graph consists of a set of vertices and a set of edges , such that each edge in is a connection between a pair of vertices in . The number of vertices is written , and the number of edges is written . can range from zero to a maximum of .
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.

An edge connecting vertices and is written . Such an edge is said to be incident with vertices and . The two vertices are said to be adjacent. If the edge is directed from to , then we say that is adjacent to , and is adjacent from . The degree of a vertex is the number of edges it is incident with. For example, vertex 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.

A sequence of vertices forms a path of length if there exist edges from to for . 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 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.

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.

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 is formed from graph by selecting a subset of โs vertices and a subset of โs edges such that for every edge , both vertices of are in . Any subgraph of 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.

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.

A free tree is a connected, undirected graph with no simple cycles. An equivalent definition is that a free tree is connected and has edges.