Graph Basics
1Introduction
This lecture defines graphs as representations of relationships between objects and develops adjacency, paths, distance, and connectivity as the concepts required for graph traversal.
In a road network, connectivity matters more than the shapes of intersections. In a communication network, reachability matters more than the appearance of devices. A graph extracts these relationships and allows structurally identical questions to be treated in a common language.
2Terms and definitions
A graph consists of a set of vertices and a set of edges. Vertices represent objects, while edges represent relationships between pairs of objects. An undirected graph in this lecture is a finite simple graph with
Thus it contains neither self-loops nor parallel edges.
For a directed graph, , and an edge is oriented from to . Unless stated otherwise, the remainder of this lecture considers finite simple undirected graphs.
Two vertices are adjacent when an edge directly joins them. The neighborhood of a vertex is
3Paths and distance
A sequence of vertices
is a walk of length when for every . It is a path when are distinct. If a walk from to exists, deleting cycles yields a path, and is reachable from .
In an unweighted graph, the distance is the minimum length of a path from to . If no path exists, set . Here, “unweighted” means that every edge counts as one step.
4Connectivity
An undirected graph is connected when a path exists between every pair of vertices. A disconnected graph decomposes into connected components, each consisting of vertices that are mutually reachable.
For example, if
then form one connected component, while forms a component by itself. Moreover, .
5Representations
An adjacency list stores for each vertex . If the graph has vertices and edges, all adjacency lists of an undirected graph contain entries in total, because every edge appears once at each endpoint.
An adjacency matrix numbers the vertices and records edge existence in matrix entries. It supports fast edge-existence queries but requires space proportional to the square of the number of vertices. Adjacency lists are suitable for sparse graphs; adjacency matrices are candidates for dense graphs or workloads dominated by edge-existence queries.
6Connection to traversal
A graph traversal starts at a vertex, examines adjacent vertices in a specified order, and enumerates reachable vertices. Vertices, edges, adjacency, paths, and distance as defined here form the input and claims of DFS and BFS.
data/lecture/information/algorithm/search/dfs-and-bfs-basics.lecture.n.md7Summary
data/exercise/information/algorithm/search/graph-traversal-and-unweighted-distance.exercise.n.md- A graph represents objects as vertices and relationships as edges.
- A path is a sequence of distinct adjacent vertices, and unweighted distance is the number of edges in a shortest path.
- Connectivity is defined by reachability between every pair of vertices.
- Adjacency lists and adjacency matrices represent the same graph with different computational tradeoffs.