Directed and Undirected Graphs

Learn about the difference between directed and undirected graphs.

We'll cover the following

Before we start diving into graph algorithms, let’s introduce some of the basic concepts of graph theory that we’ll use throughout the course.

Directed graphs

We’ll start with the most basic question: what even is a graph? Let’s look at an example.

g a a b b a->b c c a->c b->c c->b
A graph with three nodes and four edges.

The above graph consists of three nodes (named aa, bb, cc) and four edges (drawn as arrows between nodes). The edges have a direction, indicated by the arrowhead. Hence, we call such a graph a directed graph:

  • aba \to b
  • aca \to c
  • bcb \to c
  • cbc \to b

The actual positioning of the nodes in the graph does not matter. In other words, it has no significance that in the above drawing aa is “above” bb. In fact, here is the same graph drawn in a different layout:

g a a b b a->b c c a->c b->c c->b
The same graph as above, just drawn differently.

In other words, a graph is completely described by its set of nodes and edges.

By convention, the capital letter VV (for vertices) is used to denote the set of nodes. In our example graph, we have

V={a,b,c}.V = \{a, b, c\}.

To denote the set of edges, we use the capital letter EE. Each edge is a pair of two vertices, its source and target. In our example graph,

E={(a,b),(a,c),(b,c),(c,b)}.E = \{(a, b), (a, c), (b, c), (c, b)\}.

In total, a graph GG can then be written as:

G=(V,E) with EV×V.G = (V, E) \quad\text{ with }\quad E \subseteq V \times V.

For our example graph,

G=({a,b,c},{(a,b),(a,c),(b,c),(c,b)}.G = (\{a, b, c\}, \{(a, b), (a, c), (b, c), (c, b)\}.

In this course, we’ll only consider graphs whose edges fulfill the following properties:

  • There are never two edges with the same source and the same target.
  • There are no “self-loop” edges of the form (v,v)(v, v).

Such graphs are called simple graphs. The following figure shows the two forbidden situations that were described above.

g a a a->a b b c c b->c b->c
Edges that cannot exist in simple graphs.

Undirected graphs

So far, the graphs we’ve considered have had directed edges with a source and target. However, many times we’ll find that all of the connections between vertices are bidirectional. One example is a traffic network, where the intersections are the nodes and the roads are the edges. Usually, the roads can all be traversed in both directions.

We can describe such a graph as a directed graph, for example.

g a a b b a->b c c a->c b->a d d b->d c->a d->b
A directed graph where all connections are bidirectional.

The above graph has the set of edges:

E={(a,b),(b,a),(a,c),(c,a),(b,d),(d,b)}.E = \{(a, b), (b, a), (a, c), (c, a), (b, d), (d, b)\}.

But, if we know that all connections are bidirectional anyway, there is no need to draw two arrows between each pair of connected nodes. To simplify the drawing, we can just draw a straight line between connected nodes, implying they are connected bidirectionally.

g a a b b a--b c c a--c d d b--d
The same graph, drawn with undirected edges.

We call such a graph an undirected graph.

How should we define the set of edges EE of an undirected graph? We could use a new definition where every edge is not an ordered pair of source and origin, but instead is an unordered set of two vertices. However, it would be inconvenient to have two different definitions of graphs.

The more pragmatic solution is to use the same definition as before: edges are a pair of source and origin. But in an undirected graph, we always require that if: (u,v)(u, v) is an edge in the graph, then also its reverse (v,u)(v, u) is an edge. Formally,

for all u,vV:(u,v)E(v,u)E.\text{for all } u, v\in V:\quad (u, v) \in E \to (v, u) \in E.

So, the set of edges of our undirected example graph is still

E={(a,b),(b,a),(a,c),(c,a),(b,d),(d,b)}.E = \{(a, b), (b, a), (a, c), (c, a), (b, d), (d, b)\}.

The graph itself has not changed, but the way we have drawn it and interpreted it is different! We can think of the undirected connection between aa and bb as being represented by the two “half-edges” (a,b)(a, b) and (b,a)(b, a).