Directed graphs, called digraphs for short, provide a handy way to represent how things are connected together and how to get from one thing to another by following those connections. They are usually pictured as a bunch of dots or circles with arrows between some of the dots, as in Figure 9.1. The dots are called nodes or vertices and the lines are called directed edges or arrows; the digraph in Figure 9.1 has 4 nodes and 6 directed edges.
Digraphs appear everywhere in computer science. For example, the digraph in Figure 9.2 represents a communication net, a topic we’ll explore in depth in Chapter 10. Figure 9.2 has three “in” nodes (pictured as little squares) representing locations where packets may arrive at the net, the three “out” nodes representing destination locations for packets, and the remaining six nodes (pictured with little circles) represent switches. The 16 edges indicate paths that packets can take through the router.
Another place digraphs emerge in computer science is in the hyperlink structure of the World Wide Web. Letting the vertices \(x_1, \cdots , x_n\) correspond to web pages, and using arrows to indicate when one page has a hyperlink to another, results in a digraph like the one in Figure 9.3—although the graph of the real World Wide Web would have \(n\) be a number in the billions and probably even the trillions. At first glance, this graph wouldn’t seem to be very interesting. But in 1995, two students at Stanford, Larry Page and Sergey Brin, ultimately became multibillionaires from the realization of how useful the structure of this graph could be in building a search engine. So pay attention to graph theory, and who knows what might happen!
A directed graph, \(G\), consists of a nonempty set, \(V(G)\), called the vertices of \(G\), and a set, \(E(G)\), called the edges of \(G\). An element of \(V(G)\) is called a vertex. A vertex is also called a node; the words “vertex” and “node” are used interchangeably. An element of \(E(G)\) is called a directed edge. A directed edge is also called an “arrow” or simply an “edge.” A directed edge starts at some vertex, \(u\), called the tail of the edge, and ends at some vertex, \(v\), called the head of the edge, as in Figure 9.4. Such an edge can be represented by the ordered pair \((u, v)\). The notation \(\langle u \rightarrow v\rangle\) denotes this edge.
There is nothing new in Definition 9.0.1 except for a lot of vocabulary. Formally, a digraph \(G\) is the same as a binary relation on the set, \(V = V(G)\)—that is, a digraph is just a binary relation whose domain and codomain are the same set, \(V\). In fact, we’ve already referred to the arrows in a relation \(G\) as the “graph” of \(G\). For example, the divisibility relation on the integers in the interval \([1..12]\) could be pictured by the digraph in Figure 9.5.
- 9.1: Vertex Degrees
- The in-degree of a vertex in a digraph is the number of arrows coming into it, and similarly its out-degree is the number of arrows out of it.