A graph G = (V, E) has vertices V and edges E. Two vertices joined by an edge are adjacent. The degree of a vertex is the number of edges touching it. A path is a sequence of vertices connected by edges; a cycle is a path that returns to its start. A graph is connected if every vertex can reach every other. The handshake lemma says the degree sum equals twice the edge count.
Remember
/* 0 --- 1
| /
2 --- 3 V = {0,1,2,3}
E = {0-1, 0-2, 1-2, 2-3} */
Reference
Cheat Sheet
Keep these facts handy while you work with Graph Terminology in C.
#include <stdio.h>
int main(void) {
int edges[][2] = {{0, 1}, {0, 2}, {1, 2}, {2, 3}};
int m = 4, deg[4] = {0}, sum = 0;
for (int i = 0; i < m; i++) {
deg[edges[i][0]]++;
deg[edges[i][1]]++;
}
for (int v = 0; v < 4; v++) {
printf("degree(%d)=%d\n", v, deg[v]);
sum += deg[v];
}
printf("sum of degrees=%d (2 x %d edges)\n", sum, m);
return 0;
}
Output
degree(0)=2
degree(1)=2
degree(2)=3
degree(3)=1
sum of degrees=8 (2 x 4 edges)
How It Works
Each undirected edge is counted at both ends.
The degrees 2, 2, 3, 1 add to 8.
Eight is exactly twice the four edges.
Edge Cases & Pitfalls
Check these before relying on Graph Terminology in real programs.
loop
Self loops
A self loop adds 2 to a vertex's degree in an undirected graph.
multi
Parallel edges
Matrices cannot store multiple edges between the same pair without counts.
Remember
Key Takeaways
G = (V, E).
Degree counts edges.
Cycles and connectivity matter for algorithms.
One line: A graph G = (V, E) has vertices V and edges E.
Frequently Asked Questions
The number of edges incident to that vertex.
A path that starts and ends at the same vertex without repeating edges.
Continue with "C Directed and Undirected Graphs" next. Store directed edges one way and undirected edges both ways. The C Graphs hub lists every topic in order.