COMP 410-‐002: DATA STRUCTURES 4/9/14 Adjacency Matrix
q
A |V|x|V| matrix – [i,j]th entry represents edge from ith to jth vertex
q
Weighted graph – matrix entries denote the edge-weights
q
Some sentinel value for non-existent edges
Ø Depends on application – e.g., shortest-path problems: ∞
q
Values along the diagonal
q
Undirected graph – symmetric along diagonal
q
Memory requirement: O(|V|2)
Ø Ok for dense graphs
Ø Too much for sparse graphs
§ road networks
§ social networks
1 1 2 3 4 1 1 1 2 5 1 6 7 1 3 1 4 1 1 5 1 1 1 6 7 1 10 Adjacency List
q
q
q
q
q
An array of linked lists of max length |V|, with the ith
entry storing edges from the ith vertex
Weighted graph – the list entries contain the edgeweights as well
The order of edges within a list is irrelevant
Undirected graphs – each
edge appears in two lists
Memory requirement:
Ø
Ø
O(|V|+|E|)
Linear in size of graph
1 2 4 2 4 5 -‐-‐ 3 6 -‐-‐ 4 6 7 5 4 7 -‐-‐ 6 -‐-‐ 3 -‐-‐ 3 -‐-‐ 6 7 Copyright © by Jasleen Kaur 11 1 COMP 410-‐002: DATA STRUCTURES 4/9/14 Vertices è Numbers
q
In real-world applications, (airline route-maps)
Ø
Ø
q
How to rapidly translate from names to numbers?
Ø
q
Vertices have names
But matrices/arrays are indexed by non-negative integers
Constant-time translation can be achieved by hashing
What about translating from numbers to names?
12 TOPOLOGICAL ORDERING
Directed Acyclic Graphs
13 Copyright © by Jasleen Kaur 2 COMP 410-‐002: DATA STRUCTURES 4/9/14 Topological Sort
q
Only defined for directed acyclic graphs (DAGs)
Ø
Ø
q
Edges are directed
No (directed) loops exist in the graph
Definition: an ordering of vertices such that
Ø
Ø
if (i,j) is an edge in the graph, then i appears before j in
the ordering
equivalently, if there is a path from i to j, then i appears
before j in the ordering
14 Properties
q
Definition: an ordering of vertices such that
Ø
q
Not possible if graph has a cycle
Ø
q
if there is a path from i to j, then i appears before j in the
ordering
For any two vertices v and w on the cycle, v precedes w
and w precedes v
Ordering need not be unique
Ø
Ø
Ø
Any legal ordering will do
e.g.: v1, v2, v5, v4, v3, v7, v6
e.g.: v1, v2, v5, v4, v7, v3, v6
15 Copyright © by Jasleen Kaur 3 COMP 410-‐002: DATA STRUCTURES 4/9/14 Applications
q
Important in management, logistics, planning
Helps enforce dependencies
Ø
q
e.g., course sequence that does not violate prerequisites
Edge (v,w) indicates that course v must be completed
before course w is attempted
Ø
16 Algorithm
q
Definition: an ordering of vertices such that
Ø
q
Simple approach:
Ø
Ø
Ø
Ø
q
if there is a path from i to j, then i appears before j in the
ordering
Find a vertex with no incoming edges
Print this vertex
Remove it (along with its edges) from the graph
Repeat same strategy with rest of the graph
Indegree of a vertex v – the number of edges (u, v)
Ø
Ø
Compute the indegree of each vertex
Repeat
§ Write out vertex i of indegree zero
§ Update indegrees resulting from deleting i and its edges
– decrement indegrees of all vertices j such that (i,j) is an edge
until each vertex has been listed
17 Copyright © by Jasleen Kaur 4 COMP 410-‐002: DATA STRUCTURES 4/9/14 Code
q
Compute the indegree of each vertex
for (int k=0; k<|V|; k++) indegree[k] = 0;
for each vertex i
for each edge (i,j)
indegree[j] += 1;
q
Repeat until each vertex has been listed:
Ø
Ø
Write out vertex i of indegree zero
Update indegrees resulting from deleting i and its edges
§ decrement indegrees of all vertices j such that (i,j) is an edge
for (int k=0; k<|V|; k++) {
let i be a vertex with (indegree[i]==0); print i;
if no such node, return failure; // cycle !!
for each edge (i,j)
indegree[j] -= 1;
}
18 Example
for (int k=0; k<|V|; k++) indegree[k] = 0;
for each vertex i
for each edge (i,j), indegree[j] += 1;
for (int k=0; k<|V|; k++) {
let i be a vertex with (indegree[i]==0); print i;
if no such node, return failure; // cycle !!
for each edge (i,j), indegree[j] -= 1;
}
19 Copyright © by Jasleen Kaur 5 COMP 410-‐002: DATA STRUCTURES 4/9/14 Analysis: Adjacency Matrix
for (int k=0; k<|V|; k++) indegree[k] = 0;
for each vertex i
for each edge (i,j), indegree[j] += 1;
for (int k=0; k<|V|; k++) {
let i be a vertex with (indegree[i]==0); print i;
if no such node, return failure; // cycle !!
for each edge (i,j), indegree[j] -= 1;
}
q
Adjacency Matrix
Ø
Ø
O(|V|2) to compute the indegrees
In each iteration:
§ O(|V|) to find i
§ O(|V|) to update indegrees
Ø
Total run-time: O(|V|2) + O(|V|2) = O(|V|2)
20 Analysis: Adjacency List
for (int k=0; k<|V|; k++) indegree[k] = 0;
for each vertex i
for each edge (i,j), indegree[j] += 1;
for (int k=0; k<|V|; k++) {
let i be a vertex with (indegree[i]==0); print i;
if no such node, return failure; // cycle !!
for each edge (i,j), indegree[j] -= 1;
}
q
Adjacency List
Ø
Ø
Ø
q
O(|V|+|E|) to compute the indegrees
In each iteration, O(|V|) to find the node i
Total run-time: O(|V|2)
No better than adjacency matrix representation!
21 Copyright © by Jasleen Kaur 6 COMP 410-‐002: DATA STRUCTURES 4/9/14 Improvement
q
If graph is sparse, only a few vertices have indegrees
updated in each iteration
Ø
q
However, search for 0-degree vertex scans all vertices
How to do better with adjacency list representation?
Ø
Ø
Ø
“Box” a node when its indegree is updated to zero
0-degree vertices found in box in subsequent iterations
If “box” is a stack, insertion/deletion takes constant time!
22 Improvement: Code
q
q
“Stack” a node when its indegree becomes zero
0-degree vertex found in stack in subsequent iterations
for (int k=0; k<|V|; k++) indegree[k] = 0;
for each vertex i
for each edge (i,j), indegree[j] += 1;
Stack S = new Stack();
for each vertex k
if (indegree[k] == 0) S.push(k);
for (int k=0; k<|V|; k++) {
let i be a vertex with (indegree[i]==0); print i;
if no such node, return failure; // cycle !!
i = S.pop();
for each edge (i,j),
indegree[j] -= 1;
if (indegree[j] == 0) S.push(j);
}
23 Copyright © by Jasleen Kaur 7 COMP 410-‐002: DATA STRUCTURES 4/9/14 Improvement: Analysis
for (int k=0; k<|V|; k++) indegree[k] = 0;
for each vertex i
for each edge (i,j), indegree[j] += 1;
Stack S = new Stack();
for each vertex k
if (indegree[k] == 0) S.push(k);
for (int k=0; k<|V|; k++) {
i = S.pop();
for each edge (i,j),
indegree[j] -= 1;
if (indegree[j] == 0) S.push(j);
q
Adjacency Lists:
Ø
Ø
Ø
Ø
Ø
q
}
O(|V|+|E|) to compute the indegrees
O(|V|) to initially place vertices in S
In each iteration, O(1) to find node i – total: O(|V|)
Across all iterations, O(|E|) to update the indegrees (and push to S)
Total run-time: O(|V| + |E| + |V| + |V| + |E|) = O(|V| + |E|)
This is the best that can be done (visit each node and edge once)!
24 Copyright © by Jasleen Kaur 8
© Copyright 2026 Paperzz