Sub topic 5
Common graph families: trees, DAGs, bipartite, complete
This chapter assumes you know what vertices, edges, direction, degree, paths, cycles, and connectivity mean. It also assumes basic Python and introductory Big-O. A graph family is a set of graphs that share a structural rule. Knowing the family tells you which algorithms and shortcuts are safe to use.
Trees
A tree is a connected, undirected graph with no cycles. A tree with n vertices has exactly n - 1 edges. Removing any edge disconnects it, and adding any edge creates a cycle. Between any two vertices there is exactly one simple path.
A forest is a graph whose connected components are all trees. Programs often work with a rooted tree, where one vertex is chosen as the root and every other vertex has one parent. File systems and HTML documents are rooted trees.
DAGs
A DAG (directed acyclic graph) is a directed graph with no directed cycle. Edges have a direction, but you can never follow them and return to where you started. Task dependencies, build systems, and course prerequisites are DAGs.
A DAG has a topological order. This is a list of all vertices in which every edge points from an earlier vertex to a later one. A directed graph has such an order if and only if it is a DAG.
Bipartite graphs
A bipartite graph is one whose vertices can be split into two groups so that every edge joins a vertex in one group to a vertex in the other. No edge connects two vertices of the same group. Examples include students and courses, or workers and jobs.
An undirected graph is bipartite exactly when it has no cycle of odd length. You can test this by coloring with two colors during a traversal. Give the start vertex color 0, give each neighbor the opposite color, and report failure if an edge joins two vertices of the same color.
from collections import deque
def is_bipartite(graph):
color = {}
for start in graph:
if start in color:
continue
color[start] = 0
queue = deque([start])
while queue:
node = queue.popleft()
for neighbor in graph[node]:
if neighbor not in color:
color[neighbor] = 1 - color[node]
queue.append(neighbor)
elif color[neighbor] == color[node]:
return False
return True
square = {"A": ["B", "D"], "B": ["A", "C"], "C": ["B", "D"], "D": ["A", "C"]}
triangle = {"A": ["B", "C"], "B": ["A", "C"], "C": ["A", "B"]}
print(is_bipartite(square)) # True
print(is_bipartite(triangle)) # False, it has an odd cycleThe outer loop starts a new search from every uncolored vertex, so the function also works when the graph is disconnected.
Complete graphs
A complete graph on n vertices, written Kn, has an edge between every pair of distinct vertices. An undirected Kn has n(n - 1) / 2 edges. This is the densest simple graph possible, so the adjacency matrix wastes no space on it.
Summary of the families
| Family | Direction | Defining rule | Edge count for n vertices |
|---|---|---|---|
| Tree | Undirected | Connected, no cycles | n - 1 |
| DAG | Directed | No directed cycle | At most n(n - 1) / 2 |
| Bipartite | Undirected | Two groups, edges only between groups | At most (n / 2)^2 |
| Complete (K_n) | Undirected | Every pair joined | n(n - 1) / 2 |
In the diagram, read the arrows as directed edges. Ignoring direction, the graph has a cycle, so it is not a tree. With direction, no path returns to Root, so it is a DAG.
Common errors
- Calling a directed graph a tree because it has no directed cycle. A tree must have no cycle at all when direction is ignored.
- Assuming a bipartite graph must be connected. It need not be, and your test must start from every vertex.
- Confusing "acyclic" with "has few edges." A DAG can have n(n - 1) / 2 edges.
Exercise
An undirected graph has 6 vertices and 5 edges and is connected. Is it a tree? What if it has 6 vertices and 6 edges and is connected?
Show answer
With 5 edges, it is a tree. A connected graph on n vertices with n - 1 edges has no cycle. With 6 edges, it is not a tree, because a connected graph with n edges must contain at least one cycle.
Exercise
How many edges does an undirected complete graph K_7 have?
Show answer
7 * 6 / 2 = 21 edges.