Data Structures and Algorithms • C# examples
Graphs, Vertices, Edges, and Representations
Model relationships with adjacency lists, matrices, or edge lists.
Concept
A graph consists of vertices and edges; edges may be directed, weighted, or parallel depending on the domain. Adjacency lists are memory-efficient for sparse graphs, matrices give constant-time edge checks for dense graphs, and edge lists are convenient for algorithms that process every edge.
How to approach it
Record whether the graph is directed and weighted, and whether vertices use zero-based integer IDs.
Complexity
Adjacency list O(V + E) storage; adjacency matrix O(V²) storage.
C# example
var graph = Enumerable.Range(0, vertexCount)
.Select(_ => new List<int>())
.ToArray();
graph[from].Add(to);
Expected result or behavior
The adjacency list records an outgoing edge from one vertex to another.
Practice exercise
Represent an undirected edge and explain why it must be stored in both endpoint lists.
Key takeaway: Representation determines memory use and the cost of common graph operations.