A graph is a set of points joined by lines. It is the most general structure for describing connections between things.
Definition
A graph G = (V, E) consists of a set V of vertices (nodes) and a set E of edges, each joining a pair of vertices. If the edges have no direction, the graph is undirected, and an edge is written {u, v}. If each edge has a direction, from u to v, the graph is directed (a digraph), and the edge is written (u, v). If each edge carries a number, such as a distance or cost, the graph is weighted. A road map is a weighted undirected graph, and the links between web pages form a directed graph.
Definition: Graph. A non-linear data structure consisting of a set of vertices and a set of edges, each edge joining a pair of vertices.
Terminology
Two vertices joined by an edge are adjacent. The degree of a vertex is the number of edges at it. In a directed graph, the in-degree counts edges coming in and the out-degree edges going out. A path is a sequence of vertices in which each pair of neighbours is joined by an edge, and its length is the number of edges. A cycle is a path that starts and ends at the same vertex. A graph is connected if there is a path between every pair of vertices. A graph with no cycles is acyclic, and a directed acyclic graph is a DAG. A complete graph has an edge between every pair of vertices, giving n(n − 1)/2 edges. A self-loop joins a vertex to itself, and parallel edges join the same pair twice. A tree is a connected acyclic graph. In any undirected graph, the sum of the degrees equals twice the number of edges.
Applications
Graphs model road and rail networks for route finding, computer networks for routing, social networks, web links for search engines, task dependencies in project planning, circuits, and recommendation systems.
Table 4.1: Graph terms
| Term | Meaning | Example |
|---|---|---|
| Degree | Edges at a vertex | deg(A) = 3 |
| Path | Sequence of joined vertices | A–B–D |
| Cycle | Path back to its start | A–B–C–A |
| Connected | Path between every pair | One piece |
| Weighted | Edges carry values | Distances in km |
Example 4.1: Degrees and edges
Problem: A graph has vertices A, B, C, D and edges AB, AC, AD, BC, CD. Find the degree of each vertex and check the sum.
Solution:
- deg(A) = 3 (AB, AC, AD), deg(B) = 2, deg(C) = 3, deg(D) = 2.
- Sum of degrees = 3 + 2 + 3 + 2 = 10.
- Number of edges = 5, and 2 × 5 = 10.
Result: The degrees are 3, 2, 3, 2, and their sum is twice the number of edges.
#include <stdio.h>
int main(void) {
char e[][2] = {{'A','B'},{'A','C'},{'A','D'},{'B','C'},{'C','D'}};
int deg[4] = {0}, m = 5;
for (int i = 0; i < m; i++) {
deg[e[i][0]-'A']++;
deg[e[i][1]-'A']++;
}
int sum = 0;
for (int v = 0; v < 4; v++) {
printf("%c:%d ", 'A' + v, deg[v]);
sum += deg[v];
}
printf("sum=%d edges=%d\n", sum, m);
return 0;
}
Output
A:3 B:2 C:3 D:2 sum=10 edges=5
Common mistakes
- Confusing a path, which may not repeat vertices, with a walk in general.
- Forgetting that a directed edge counts once in the out-degree and once in the in-degree.
- Treating every graph as connected.
Key takeaways
- A graph has vertices and edges, and may be directed or weighted.
- Degree is the number of edges at a vertex; the sum of degrees is 2|E|.
- A connected acyclic graph is a tree.
- A complete graph on n vertices has n(n − 1)/2 edges.
Practice questions
- Define a graph.
- What is a cycle?
- How many edges does a complete graph on 5 vertices have?
Answers to the practice questions
- A set of vertices and a set of edges joining pairs of vertices.
- A path that starts and ends at the same vertex.
- 5 × 4 / 2 = 10.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit IV
