Graphs — Modeling Connections

CS205 Data Structures

Use arrow keys or buttons to navigate • 26 slides

What is a Graph?

A graph G = (V, E) consists of:

  • V — a set of vertices (nodes)
  • E — a set of edges (connections)

Unlike trees, graphs have no root, no parent-child hierarchy, and can contain cycles.

Analogy: Social Network

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.

|V| = 0   |E| = 0  ·  Click to add vertices

Key Idea

Graphs are the most general data structure for modeling relationships. Trees, linked lists, and even arrays are all special cases of graphs!

Graph Terminology

Click a node to explore its properties
TermDefinition
AdjacentTwo vertices connected by an edge
IncidentAn edge is incident to its endpoints
DegreeNumber of edges touching a vertex
PathSequence of vertices connected by edges
CycleA path that starts and ends at same vertex
ConnectedEvery vertex reachable from every other
ComponentA maximal connected subgraph

Key Idea

The degree of a vertex is the most fundamental local property — it tells you how "connected" that vertex is.

Directed vs Undirected Graphs

Undirected

Examples: Facebook friendships, two-way roads, Ethernet

Directed (Digraph)

Examples: Twitter follows, one-way streets, hyperlinks

Left: edge(A,B) = edge(B,A)    Right: edge(A,B) ≠ edge(B,A)

Analogy: Streets

Undirected = two-way street. Directed = one-way street. In directed graphs, we track in-degree (arrows in) and out-degree (arrows out) separately.

Weighted vs Unweighted Graphs

Click two cities to select start & end, then Find Shortest Path

City Distances

Each edge carries a weight (distance in km). The shortest path minimizes total weight, not edge count.

Key Idea

Weights model real-world costs: distance, time, bandwidth, price. "Shortest path" = minimum total weight.

Unweighted = All Weight 1

An unweighted graph is a weighted graph where every edge has weight 1. Shortest path = fewest edges.

Special Graphs

Complete Graph Kn

n = |E| = 6

Bipartite Graph

Vertices split into two sets. Edges only cross between sets — never within.

DAG (Directed Acyclic Graph)

Directed, no cycles. Used for dependencies (courses, build systems, scheduling).

Tree (as a Graph)

Connected + acyclic + undirected. |E| = |V| - 1. Exactly one path between any two nodes.

Graph ADT (Abstract Data Type)

Try: Add vertices A,B,C,D then edges A,B and B,C etc.

Query Operations

MethodDescription
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

Update Operations

MethodDescription
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

Key Idea

The ADT tells us what a graph supports. The representation (matrix, list, edge list) determines how efficiently each runs.

Representation 1: Adjacency Matrix

A 2D array A[V][V] where A[i][j] = 1 if edge from i to j exists.

Click a cell in the matrix to toggle an edge. Graph updates in real-time.

Space: O(V2)

Always V×V cells regardless of edge count

For Weighted Graphs

Store weight instead of 1, ∞ instead of 0

Adjacency Matrix: Pros and Cons

Advantages

  • O(1) edge lookup — just check A[i][j]
  • O(1) edge insert/remove — just set A[i][j]
  • Simple to implement — just a 2D array
  • Good for dense graphs where |E| ≈ V2

Disadvantages

  • O(V2) space even if very few edges
  • O(V) to find neighbors — must scan entire row
  • O(V2) to add a vertex — must resize matrix
  • Wasteful for sparse graphs
Watch the difference: O(1) lookup vs O(V) neighbor scan

Representation 2: Adjacency List

An array of lists. Each vertex stores a list of its neighbors.

Graph on left, adjacency list on right. Add/remove edges to see both update.

Space: O(V + E)

Each edge stored twice (once per endpoint). Much better than O(V2) for sparse graphs!

Analogy: Contact Lists

Each person has their own phone contact list. You only store contacts you actually have, not a slot for everyone.

Adjacency List: Pros and Cons

Advantages

  • O(V + E) space — proportional to actual size
  • O(1) to add an edge — prepend to list
  • O(deg(v)) to iterate neighbors — just walk the list
  • O(1) to add a vertex — append to array
  • Efficient for sparse graphs

Disadvantages

  • O(deg(v)) edge lookup — must search the list
  • O(deg(v)) edge removal — must find it first
  • No quick way to check if edge exists
Compare with matrix: neighbor iteration is faster here, but edge check is slower

Matrix vs List vs Edge List

Matrix Adj List Edge List
SpaceO(V2)O(V+E)O(E)
Check edgeO(1)O(deg)O(E)
Add edgeO(1)O(1)O(1)
NeighborsO(V)O(deg)O(E)
Best forDenseSparseEdge proc.

Rule of Thumb

|E| close to V2matrix. |E| « V2adjacency list. Just need to iterate all edges? → edge list. Most real-world graphs are sparse, so adjacency list is the default.

Representation 3: Edge List

The simplest representation: just store a list of all edges as (u, v) pairs.

Edge lists are simple but slow for lookups — must scan all edges

Complexity

OperationCost
SpaceO(E)
Check edgeO(E) — scan list
Add edgeO(1) — append
Remove edgeO(E) — find first
NeighborsO(E) — scan all

When to Use

When you need to process all edges (e.g., Kruskal’s MST), or graph is very simple with minimal overhead.

Warning

Edge lists are slow for lookups. Cannot quickly check if a specific edge exists or find a vertex’s neighbors.

Graph Properties & Formulas

Handshaking Lemma Explorer

Handshaking Lemma

∑ deg(v) = 2 × |E|
Each edge contributes 1 to the degree of each endpoint, so it’s counted twice.

Maximum Edges Calculator

V =
Undirected: V(V-1)/2    Directed: V(V-1)

Paths and Connectivity

Click two nodes to select start & end, then Find Path. Or click Find Cycle.

Types of Paths

Path: Sequence of vertices connected by edges.

Simple Path: No vertex repeated.

Cycle: A path that returns to its start.

Connectivity

Connected: Every vertex reachable from every other.

Disconnected: Some vertices cannot reach others.

Strong Connectivity (Directed)

Strongly connected: Can reach any vertex from any other following directed edges.

Key Idea

A connected component is a maximal set of vertices where every pair is connected by a path.

Application: Social Networks

Click two people to find their degrees of separation

Graph Metrics in Social Networks

  • Degree = number of friends (popularity)
  • Shortest path = degrees of separation
  • Connected component = social circle

Six Degrees of Separation

On Facebook, average separation is ~3.5. Any two people connected through a short chain of friends.

Directed Social Networks

Twitter/Instagram: follow without being followed back = directed graph. In-degree = followers. Out-degree = following.

Challenge: Graph Properties

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?

Application: The Internet

Challenge: Fix the Bug

This code builds an adjacency matrix for an undirected graph, but has a bug:

void addEdge(int[][] matrix, int u, int v) { matrix[u][v] = 1; // done! }

What’s the bug?

Application: Maps and Navigation

Click two locations to select start & end

Key Idea

Every time you use Google Maps, you run a shortest-path algorithm on a massive weighted graph with millions of vertices.

Select start and end to compare routes

Challenge: Pick the Representation

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.

Preview: Graph Algorithms

Now that you understand graph structure, here’s what’s coming next:

BFS (Breadth-First Search)

Explore level by level (like ripples). Uses a QUEUE. Finds shortest path in unweighted graphs.

Dijkstra’s Shortest Path

Find shortest weighted path from source to all others. Uses a priority queue.

DFS (Depth-First Search)

Explore as deep as possible first. Uses a STACK (or recursion). Detects cycles, topological sort.

Topological Sort

Order vertices so all edges point "forward." Only works on DAGs.

Summary & Cheat Sheet

Matrix Adj List Edge List
SpaceO(V2)O(V+E)O(E)
Edge checkO(1)O(deg)O(E)
Add edgeO(1)O(1)O(1)
NeighborsO(V)O(deg)O(E)
Best forDenseSparse (default)Edge proc.

Key Takeaway

Graphs model connections. Choose representation by density & operations needed. Adjacency list is the go-to default.

Coming Up Next

BFS, DFS, shortest paths, and topological sort — algorithms that unlock the true power of graphs.

Quiz: Test Your Knowledge

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?

Trace: Build an Adjacency List

Given edges: (A,B), (B,C), (A,C), (C,D), (D,A) — build the adjacency list step by step.

Click "Add Next Edge" to process edges one by one

Predict: What Does This Code Print?

int[][] adj = new int[4][4]; // vertices 0-3 adj[0][1]=1; adj[1][0]=1; // edge 0-1 adj[0][2]=1; adj[2][0]=1; // edge 0-2 adj[1][3]=1; adj[3][1]=1; // edge 1-3 adj[2][3]=1; adj[3][2]=1; // edge 2-3 int count = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) if (adj[i][j] == 1) count++; System.out.println(count);

What value is printed?