Graphs — Modeling Connections

CS205 Data Structures

[A]-------[B] / | \ | \ / | \ | \ [F] | [C]----+--[D] \ | / | / \ | / | / [E]-------[G]

Use arrow keys or buttons to navigate • 20 slides

1 / 20

What is a Graph?

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

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

Each edge connects two vertices. 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 is a path in the graph.

Analogy: Road Map

Intersections are vertices. Roads connecting them are edges. Distances are weights on those edges.

A Simple Graph

Vertices: {A, B, C, D, E} Edges: {(A,B),(A,C),(B,C),(B,D),(C,E),(D,E)} [A] / \ / \ [B]---[C] | | | | [D]---[E] |V| = 5 (five vertices) |E| = 6 (six edges)

Key Idea

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

2 / 20

Graph Terminology

degree(A) = 2 path A-B-D = [A, B, D] degree(B) = 3 degree(D) = 2 cycle: A - B - C - A [A] / \ A and B are adjacent / \ Edge (A,B) is incident to A [B]---[C] Degree of B = 3 | | | | [D]---[E]
TermDefinition
Vertex (Node)A fundamental unit in a graph
EdgeA connection between two vertices
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 the same vertex
ConnectedEvery vertex is 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.

3 / 20

Directed vs Undirected Graphs

Undirected Graph

Edges have no direction. If A connects to B, then B connects to A.

[A]------[B] | | | | [C]------[D] Edge (A,B) = Edge (B,A) Symmetric relationship

Examples: Facebook friendships, road networks (two-way streets), computer networks (Ethernet)

Directed Graph (Digraph)

Edges have a direction. An edge from A to B does NOT imply B to A.

[A]----->[B] | | v v [C]<------[D] Edge (A,B) =/= Edge (B,A) In-degree(C) = 2, Out-degree(C) = 0

Examples: Twitter follows, one-way streets, web page hyperlinks, prerequisite courses

Analogy: Streets

Undirected = two-way street: you can drive in both directions. Directed = one-way street: traffic flows in only one direction. Violating the direction is illegal (and impossible in graphs)!

4 / 20

Weighted vs Unweighted Graphs

Unweighted Graph

All edges are "equal" — there is no cost associated with traversing an edge.

[A]------[B] | | | | [C]------[D] All edges cost "1"

Weighted Graph

Each edge carries a weight (cost, distance, time, capacity, etc.).

10 [A]--------[B] | \ | 3 | \ 7 | 2 | \ | [C]------[D] 4

Application: City Distances

350km [NYC]----------[Boston] | \ | | \210km | 150km | \ | 200km [Philly] [Hartford] | | | | 100km | | [DC]-----+ Shortest NYC to DC? Direct: 200km Via Philly: 210 + 100 = 310km Winner: direct route!

Key Idea

Weights let us model real-world costs. The "shortest path" in a weighted graph minimizes total weight, not number of edges.

5 / 20

Special Graphs

Complete Graph K4

Every vertex connects to every other vertex.

[A]------[B] |E| = V(V-1)/2 |\ /| = 4(3)/2 | \ / | = 6 edges | \/ | | /\ | | / \ | [C]------[D]

Bipartite Graph

Vertices split into two sets; edges only cross between sets.

Set L Set R [1] ------- [A] [1] ------- [B] [2] ------- [A] [3] ------- [B] [3] ------- [C] No edges within L or within R

DAG (Directed Acyclic Graph)

Directed graph with no cycles. Used for dependencies.

[CS101]---->[CS201]---->[CS301] \ ^ \ / +->[MATH201]------+ No way to follow arrows and return to where you started (no cycle!)

Tree (as a Graph)

A connected, acyclic, undirected graph.

[A] Properties: / \ - Connected / \ - Acyclic [B] [C] - |E| = |V| - 1 / \ - Exactly one path [D] [E] between any two nodes
6 / 20

Graph ADT (Abstract Data Type)

A Graph ADT defines the operations we need, independent of representation.

Query Operations

MethodDescription
vertices()Return all vertices
edges()Return all edges
numVertices()Return |V|
numEdges()Return |E|
getEdge(u, v)Return edge from u to v (or null)
degree(v)Number of edges incident to v
adjacentVertices(v)Return neighbors of v

Update Operations

MethodDescription
insertVertex(x)Add a new vertex with element x
insertEdge(u, v, x)Add edge between u and v with element x
removeVertex(v)Remove vertex v and all its edges
removeEdge(e)Remove edge e

Key Idea

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

7 / 20

Representation 1: Adjacency Matrix

A 2D array A[V][V] where A[i][j] = 1 if there is an edge from vertex i to vertex j.

The Graph

[0]------[1] | / | | / | | / | [2]------[3] V = {0, 1, 2, 3} E = {(0,1),(0,2),(1,2),(1,3),(2,3)}

Its Adjacency Matrix

0 1 2 3 +--+--+--+--+ 0 | 0 1 1 0 | +--+--+--+--+ 1 | 1 0 1 1 | +--+--+--+--+ 2 | 1 1 0 1 | +--+--+--+--+ 3 | 0 1 1 0 | +--+--+--+--+ Symmetric for undirected graphs! A[i][j] == A[j][i]

Key Idea

Space: O(V2) regardless of how many edges exist. For a graph with 10,000 vertices, this matrix has 100,000,000 cells!

For Weighted Graphs

Store the weight instead of 1, and use infinity (or a sentinel) instead of 0 for non-edges.

8 / 20

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| is close to V2
Is there an edge from 2 to 3? 0 1 2 3 +--+--+--+--+ 2 | 1 1 0 1 | <-- A[2][3] = 1 +--+--+--+--+ YES! O(1)

Disadvantages

  • O(V2) space — Even if graph has very few edges
  • O(V) to find neighbors — Must scan entire row
  • O(V2) to add a vertex — Must resize entire matrix
  • Wasteful for sparse graphs (most real-world graphs)
Sparse graph with V=1000, E=50: Matrix: 1,000,000 cells allocated Only 100 cells are "1" (edges) Wasted: 99.99% of space!

When to Use Adjacency Matrix

Use when the graph is dense (many edges), when you need constant-time edge lookup, or when V is small enough that O(V2) space is acceptable.

9 / 20

Representation 2: Adjacency List

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

The Same Graph

[0]------[1] | / | | / | | / | [2]------[3] V = {0, 1, 2, 3} E = {(0,1),(0,2),(1,2),(1,3),(2,3)}

Its Adjacency List

0: [1] -> [2] -> null 1: [0] -> [2] -> [3] -> null 2: [0] -> [1] -> [3] -> null 3: [1] -> [2] -> null Each vertex keeps a linked list of its neighbors.

Key Idea

Space: O(V + E). We store each edge exactly twice (once in each endpoint's list) for undirected graphs. Much better than O(V2) for sparse graphs!

Analogy: Contact Lists

Each person (vertex) has their own phone contact list (linked list of neighbors). You only store contacts you actually have, not a slot for every person on Earth.

10 / 20

Adjacency List: Pros and Cons

Advantages

  • O(V + E) space — Proportional to actual size
  • O(1) to add an edge — Prepend to list
  • O(degree(v)) to iterate neighbors — Just walk the list
  • O(1) to add a vertex — Append to array
  • Efficient for sparse graphs
Neighbors of vertex 1: 1: [0] -> [2] -> [3] -> null ^ ^ ^ | | | Just walk the list: O(deg(1)) = O(3)

Disadvantages

  • O(degree(v)) edge lookup — Must search the list
  • O(degree(v)) edge removal — Must find it first
  • Slightly more complex to implement
  • No quick way to check if edge exists
Is there an edge from 2 to 3? 2: [0] -> [1] -> [3] -> null ^ ^ ^ no no YES! Found it. Had to scan 3 entries: O(deg(2))

Key Idea

Most real-world graphs are sparse (|E| much less than V2), so adjacency lists are the default choice for graph representation in practice.

11 / 20

Adjacency Matrix vs Adjacency List

Side-by-side comparison for the same graph:

[0]------[1] Matrix List | / | 0 1 2 3 | / | +--+--+--+--+ 0: [1] -> [2] | / | 0 | 0 1 1 0 | 1: [0] -> [2] -> [3] [2]------[3] 1 | 1 0 1 1 | 2: [0] -> [1] -> [3] 2 | 1 1 0 1 | 3: [1] -> [2] 3 | 0 1 1 0 |
Operation Adjacency Matrix Adjacency List
Space O(V2) O(V + E)
Check edge (u,v) O(1) O(min(deg(u), deg(v)))
Add edge O(1) O(1)
Remove edge O(1) O(deg(u))
Iterate neighbors of v O(V) O(deg(v))
Add vertex O(V2) O(1)
Best for Dense graphs Sparse graphs

Rule of Thumb

If |E| is close to V2 → use matrix. If |E| is much less than V2 → use list. Most real-world graphs are sparse, so adjacency list is the default.

12 / 20

Representation 3: Edge List

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

[0]------[1] | / | | / | | / | [2]------[3] Edge List: +-------+-------+ | u | v | +-------+-------+ | 0 | 1 | | 0 | 2 | | 1 | 2 | | 1 | 3 | | 2 | 3 | +-------+-------+

Complexity

OperationCost
SpaceO(E)
Check edge (u,v)O(E) — must scan list
Add edgeO(1) — append to list
Remove edgeO(E) — must find it
Iterate neighborsO(E) — must scan all

When to Use Edge Lists

When you need to process all edges (e.g., Kruskal's MST algorithm), or when the graph is very simple and you want minimal overhead.

Warning

Edge lists are slow for lookups. You cannot quickly check if a specific edge exists or find a vertex's neighbors. Use adjacency list or matrix for general-purpose graphs.

13 / 20

Graph Properties & Formulas

Handshaking Lemma

[A]------[B] deg(A) = 2 | / | deg(B) = 3 | / | deg(C) = 2 | / | deg(D) = 3 [C]------[D] deg(E) = 2 | -------- | Sum = 12 [E] |E| = 6 Sum of degrees = 2|E| edges 12 = 2(6) = 12 ✓

Handshaking Lemma

Sum of all vertex degrees = 2 × |E|
Each edge contributes 1 to the degree of each of its two endpoints, so it is counted twice in the sum.

Maximum Number of Edges

UNDIRECTED graph with V vertices: Max |E| = V(V-1) / 2 V=4: Max edges = 4(3)/2 = 6 (complete graph K4) DIRECTED graph with V vertices: Max |E| = V(V-1) V=4: Max edges = 4(3) = 12 (each undirected edge becomes two directed edges)

Connected Components

[A]--[B] [D]--[E] [G] | | [C]--+ [F]--+ 3 components: {A, B, C} {D, E, F} {G}

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

14 / 20

Paths and Connectivity

Types of Paths

Path: A sequence of vertices where each consecutive pair is connected by an edge. [A]--[B]--[C]--[D] Path: A, B, C, D (length = 3) Simple Path: No vertex is repeated. A -> B -> C -> D ✓ Simple A -> B -> C -> B ✗ Not simple (B repeated) Cycle: A path that returns to start. [A]--[B] \ / Cycle: A, B, C, A [C] (length = 3)

Connectivity (Undirected)

Connected graph: Every vertex reachable from every other. [A]--[B]--[C] \ / ✓ Connected +--[D]-+ Disconnected graph: [A]--[B] [C]--[D] ✗ No path A to C

Strong Connectivity (Directed)

Strongly connected: Can reach any vertex from any other following directed edges. [A]-->[B] ^ | ✓ Strongly connected | v (A->B->C->A all possible) [C]<---+ Not strongly connected: [A]-->[B]-->[C] No way from C back to A!
15 / 20

Application: Social Networks

[Alice]---[Bob]---[Carol] | / | \ \ | / | \ [Dave] | / | \ | [Eve] [Frank] [Grace]-+ Vertices = People Edges = Friendships (undirected)

Graph Metrics in Social Networks

  • Degree = number of friends (popularity)
  • Shortest path length = degrees of separation
  • Connected component = a social circle
  • Clustering coefficient = how interconnected your friends are

Six Degrees of Separation

You -> Friend -> Friend -> ... -> Anyone On Facebook (avg): ~3.5 degrees of separation On LinkedIn: Connections up to 3rd degree shown Shortest path algorithms can find the minimum degrees of separation between any two people!

Analogy: Directed Social Network

Twitter/Instagram: You can follow someone without them following you back. This is a directed graph. In-degree = followers. Out-degree = following.

16 / 20

Application: The Internet

Network Topology

[Router A] / | \ / | \ [Host1] [Host2] [Router B] / \ / \ [Host3] [Router C] | [Host4] Vertices = Routers / Hosts Edges = Network links Weights = Bandwidth or latency

Key Idea

Internet routing is fundamentally a shortest-path problem on a weighted graph. Protocols like OSPF use Dijkstra's algorithm!

The World Wide Web

[Page A]---->[Page B] | | v v [Page C]<---[Page D] | v [Page E] Vertices = Web pages Edges = Hyperlinks (directed!) Google's PageRank algorithm treats the web as a directed graph. More incoming links = more important.

Two Different Graphs

Physical Internet: undirected, weighted (bandwidth). World Wide Web: directed, unweighted (hyperlinks). Same real-world system, two different graph models!

17 / 20

Application: Maps and Navigation

Road Network as a Graph

5 mi [Home]--------[Store] | \ | 3mi | \ 8mi | 2mi | \ | [Park] [School]-[Library] | 4mi | 6mi | | 3mi | | [Gym]------------[Cafe] 7mi Vertices = Locations Edges = Roads Weights = Distances (miles)

GPS Shortest Path

Home to Library: Route 1: Home -> Store -> Library 5 + 2 = 7 mi Route 2: Home -> School -> Library 8 + 4 = 12 mi Route 3: Home -> Park -> Gym -> Cafe -> Library 3 + 6 + 7 + 3 = 19 mi Winner: Route 1 (7 mi)! Dijkstra's algorithm finds this optimal path automatically.

Key Idea

Every time you use Google Maps or GPS navigation, you are running a shortest-path algorithm on a massive weighted graph with millions of vertices.

18 / 20

Preview: Graph Algorithms

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

BFS (Breadth-First Search)

Explore level by level (like ripples) Level 0: [A] Level 1: [B] [C] Level 2: [D] [E] [F] Uses a QUEUE. Finds shortest path in unweighted graphs.

DFS (Depth-First Search)

Explore as deep as possible first [A] -> [B] -> [D] (dead end!) backtrack... [B] -> [E] -> [F] Uses a STACK (or recursion). Detects cycles, topological sort.

Dijkstra's Shortest Path

Find shortest weighted path from a source to all other vertices. 2 3 [S]---->[A]---->[T] | ^ +--------->-----+ 10 Shortest S to T: S->A->T = 2+3 = 5 (not 10!)

Topological Sort

Order vertices so all edges point "forward." Only works on DAGs! [CS101]->[CS201]->[CS301] \ ^ +->[MATH201]---+ Valid order: CS101, MATH201, CS201, CS301
19 / 20

Summary & Cheat Sheet

Core Concepts

G = (V, E) |V| = number of vertices |E| = number of edges FORMULAS: ───────────────────────────────── Handshaking: Σ deg(v) = 2|E| Max edges (undirected): V(V-1)/2 Max edges (directed): V(V-1) Tree edges: |E| = |V| - 1 ───────────────────────────────── TYPES: ───────────────────────────────── Directed vs Undirected Weighted vs Unweighted Dense vs Sparse Connected vs Disconnected Cyclic vs Acyclic ─────────────────────────────────

Representation Cheat Sheet

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

Key Takeaway

Graphs model connections. Choose your representation based on graph density and which operations you need most. Adjacency list is the go-to default for most real-world problems.

Coming Up Next

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

20 / 20