CS205 Data Structures
Use arrow keys or buttons to navigate • 26 slides
A graph G = (V, E) consists of:
Unlike trees, graphs have no root, no parent-child hierarchy, and can contain cycles.
People are vertices. Friendships are edges. You can reach anyone through a chain of mutual friends — that’s a path.
Try it: Click empty space to add vertices, click two vertices to connect them with an edge.
Graphs are the most general data structure for modeling relationships. Trees, linked lists, and even arrays are all special cases of graphs!
| Term | Definition |
|---|---|
| Adjacent | Two vertices connected by an edge |
| Incident | An edge is incident to its endpoints |
| Degree | Number of edges touching a vertex |
| Path | Sequence of vertices connected by edges |
| Cycle | A path that starts and ends at same vertex |
| Connected | Every vertex reachable from every other |
| Component | A maximal connected subgraph |
The degree of a vertex is the most fundamental local property — it tells you how "connected" that vertex is.
Examples: Facebook friendships, two-way roads, Ethernet
Examples: Twitter follows, one-way streets, hyperlinks
Undirected = two-way street. Directed = one-way street. In directed graphs, we track in-degree (arrows in) and out-degree (arrows out) separately.
Each edge carries a weight (distance in km). The shortest path minimizes total weight, not edge count.
Weights model real-world costs: distance, time, bandwidth, price. "Shortest path" = minimum total weight.
An unweighted graph is a weighted graph where every edge has weight 1. Shortest path = fewest edges.
Vertices split into two sets. Edges only cross between sets — never within.
Directed, no cycles. Used for dependencies (courses, build systems, scheduling).
Connected + acyclic + undirected. |E| = |V| - 1. Exactly one path between any two nodes.
| Method | Description |
|---|---|
vertices() | Return all vertices |
edges() | Return all edges |
getEdge(u,v) | Return edge from u to v |
degree(v) | Edges incident to v |
adjacentVertices(v) | Neighbors of v |
| Method | Description |
|---|---|
insertVertex(x) | Add vertex with element x |
insertEdge(u,v,x) | Add edge between u and v |
removeVertex(v) | Remove v and its edges |
removeEdge(e) | Remove edge e |
The ADT tells us what a graph supports. The representation (matrix, list, edge list) determines how efficiently each runs.
A 2D array A[V][V] where A[i][j] = 1 if edge from i to j exists.
Always V×V cells regardless of edge count
Store weight instead of 1, ∞ instead of 0
A[i][j]A[i][j]An array of lists. Each vertex stores a list of its neighbors.
Each edge stored twice (once per endpoint). Much better than O(V2) for sparse graphs!
Each person has their own phone contact list. You only store contacts you actually have, not a slot for everyone.
| Matrix | Adj List | Edge List | |
|---|---|---|---|
| Space | O(V2) | O(V+E) | O(E) |
| Check edge | O(1) | O(deg) | O(E) |
| Add edge | O(1) | O(1) | O(1) |
| Neighbors | O(V) | O(deg) | O(E) |
| Best for | Dense | Sparse | Edge proc. |
|E| close to V2 → matrix. |E| « V2 → adjacency list. Just need to iterate all edges? → edge list. Most real-world graphs are sparse, so adjacency list is the default.
The simplest representation: just store a list of all edges as (u, v) pairs.
| Operation | Cost |
|---|---|
| Space | O(E) |
| Check edge | O(E) — scan list |
| Add edge | O(1) — append |
| Remove edge | O(E) — find first |
| Neighbors | O(E) — scan all |
When you need to process all edges (e.g., Kruskal’s MST), or graph is very simple with minimal overhead.
Edge lists are slow for lookups. Cannot quickly check if a specific edge exists or find a vertex’s neighbors.
∑ deg(v) = 2 × |E|
Each edge contributes 1 to the degree of each endpoint, so it’s counted twice.
Path: Sequence of vertices connected by edges.
Simple Path: No vertex repeated.
Cycle: A path that returns to its start.
Connected: Every vertex reachable from every other.
Disconnected: Some vertices cannot reach others.
Strongly connected: Can reach any vertex from any other following directed edges.
A connected component is a maximal set of vertices where every pair is connected by a path.
On Facebook, average separation is ~3.5. Any two people connected through a short chain of friends.
Twitter/Instagram: follow without being followed back = directed graph. In-degree = followers. Out-degree = following.
Given the graph below, answer the questions:
1. What is the degree of vertex C?
2. Is the graph connected?
3. ∑ degrees = ?
4. Does the graph contain a cycle?
This code builds an adjacency matrix for an undirected graph, but has a bug:
What’s the bug?
Every time you use Google Maps, you run a shortest-path algorithm on a massive weighted graph with millions of vertices.
For each scenario, choose the best graph representation:
1. Social network with 1M users, avg 200 friends each. Need to quickly list a user’s friends.
2. Dense flight network (50 airports, most have direct flights). Need O(1) "is there a direct flight?" check.
3. Building a Minimum Spanning Tree with Kruskal’s algorithm (sort edges by weight, process one at a time).
4. Web crawler visiting pages and following links. Need to efficiently iterate all outgoing links from current page.
Now that you understand graph structure, here’s what’s coming next:
Explore level by level (like ripples). Uses a QUEUE. Finds shortest path in unweighted graphs.
Find shortest weighted path from source to all others. Uses a priority queue.
Explore as deep as possible first. Uses a STACK (or recursion). Detects cycles, topological sort.
Order vertices so all edges point "forward." Only works on DAGs.
| Matrix | Adj List | Edge List | |
|---|---|---|---|
| Space | O(V2) | O(V+E) | O(E) |
| Edge check | O(1) | O(deg) | O(E) |
| Add edge | O(1) | O(1) | O(1) |
| Neighbors | O(V) | O(deg) | O(E) |
| Best for | Dense | Sparse (default) | Edge proc. |
Graphs model connections. Choose representation by density & operations needed. Adjacency list is the go-to default.
BFS, DFS, shortest paths, and topological sort — algorithms that unlock the true power of graphs.
Q1. A graph has 6 vertices. What’s the max number of edges (undirected, no self-loops)?
Q2. A graph has 8 edges and Σdeg(v)=16. Using the handshaking lemma, this makes sense because:
Q3. You need O(1) edge lookup in a graph with 50 vertices and ~1000 edges. Best representation?
Given edges: (A,B), (B,C), (A,C), (C,D), (D,A) — build the adjacency list step by step.
What value is printed?