A graph is a set of dots joined by lines. Graphs model roads, networks, friendships and dependencies, so graph theory is central to computer science.
Definition and terms
A graph G = (V, E) has a set V of vertices (nodes) and a set E of edges. An edge joins two vertices, which are then adjacent. An edge from a vertex to itself is a loop; two edges between the same pair are parallel. A graph with neither is simple.
- Degree of a vertex: the number of edges meeting it (a loop counts twice).
- Handshaking lemma: the sum of all degrees = 2 × (number of edges). So the number of odd-degree vertices is always even.
Example: A graph with degrees 3, 3, 2, 2, 2 has (3 + 3 + 2 + 2 + 2) / 2 = 6 edges.
Kinds of graphs
| Type | Description |
|---|---|
| Complete graph Kₙ | Every pair of vertices is joined; it has n(n − 1)/2 edges |
| Digraph | Edges have a direction; vertices have in-degree and out-degree |
| Weighted graph | Each edge has a number (distance, cost) |
| Subgraph | A graph made from some vertices and edges of another |
| Bipartite graph | Vertices split into two groups; edges go only between groups |
K₅ has 5 × 4 / 2 = 10 edges.
Paths, cycles and connectivity
- A walk is a sequence of vertices joined by edges. A path is a walk with no repeated vertex.
- A cycle is a closed path that returns to its start, with no other repeats.
- A graph is connected if there is a path between every pair of vertices; otherwise it is disconnected and splits into components.
Representing a graph
An adjacency matrix has a 1 at row i, column j if vertices i and j are joined. An adjacency list stores, for each vertex, the list of its neighbours. Lists use less memory for graphs with few edges.
Worked example
A network has 6 computers and each is directly cabled to every other. It is K₆ with 6 × 5 / 2 = 15 cables. Each computer has degree 5.
Common mistakes
- Counting an undirected edge twice when finding the number of edges from the degree sum. Divide the sum by 2.
- Calling a walk with repeated vertices a path.
Practice questions
- State the handshaking lemma.
- How many edges are there in K₈?
- Differentiate path and cycle.
- Differentiate a digraph and a weighted graph.
- Can a graph have 5 vertices each of degree 3? Why?
Answers to the practice questions
- Sum of degrees equals twice the number of edges.
- 8 × 7 / 2 = 28.
- A path has no repeated vertex; a cycle is a path that returns to its start.
- A digraph has directed edges; a weighted graph has a value on each edge.
- No. The degree sum would be 15, which is odd, but it must equal twice the number of edges.
Sources
- Official BCA syllabus Mathematics Foundations to Computer Science I, course code CC101, Unit III
- Kolman, Busby and Ross, Discrete Mathematical Structures, 6th edition, Pearson
