Week 8: Adjacency Matrices & Edge-Labelled Graphs
3331 words
17 min read
2026-09-12T00:00:00.000Z
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
Adjacency matrix representation, edge weights, edge labels, degree matrices, and multi-step path reachability. # Computational Thinking — Week 8: Adjacency Matrices & Edge-Labelled Graphs IITM’s CT Week 8 covers **Adjacency Matrices and Edge-Labelled Graphs**.

Computational Thinking — Week 8: Adjacency Matrices & Edge-Labelled Graphs
IITM’s CT Week 8 covers Adjacency Matrices and Edge-Labelled Graphs.
TL;DR
Week 7 introduced graphs and matrices separately.
Week 8 combines them directly.
The two main ideas are:
Adjacency matrix
A graph can be stored as a matrix where each cell tells whether an edge exists.
For example:
Edge-labelled graph
An edge can carry extra information, such as:
- distance
- cost
- time
- capacity
- relationship type
So instead of merely asking:
Is A connected to B?
we can ask:
What value/label is attached to the connection from A to B?
1. Adjacency matrix recap
Suppose graph vertices are:
Graph:
textA --- B --- C
Adjacency matrix:
Rows and columns follow the same vertex order:
2. Meaning of one matrix cell
Under the common convention:
means:
there is an edge from vertex i to vertex j.
And:
means no such edge.
3. Vertex order matters
Suppose matrix rows/columns are ordered:
Then entry:
means:
connection from B to C.
If the vertex ordering changes, the matrix changes too.
So an adjacency matrix must always be interpreted together with its vertex ordering.
4. Example
M=011100100with order:
Then:
- A connected to B
- A connected to C
- B not connected to C
Graph:
textB \ A / C
5. Undirected adjacency matrix
For an undirected graph:
implies both:
and:
in matrix representation.
Therefore:
So the matrix is symmetric.
6. Directed adjacency matrix
For a directed graph:
textA ---> B
we may have:
but:
So the matrix need not be symmetric.
7. Directed example
Graph:
textA ---> B ---> C ^ | C
Edges:
Matrix:
8. Diagonal entries
Entry:
represents an edge from vertex i to itself.
So:
- Mii=1 → self-loop exists
- Mii=0 → no self-loop
In a simple graph, diagonal entries are usually zero.
9. Row sum
For a simple undirected graph:
For a directed graph under the common source-row convention:
10. Column sum
For directed graphs:
assuming:
means:
11. Example
M=001100110Row sums:
So out-degrees:
Column sums:
So in-degrees:
12. Counting edges in an undirected graph
Since each undirected edge appears twice in the matrix:
and:
we have:
for simple undirected graphs.
13. Example
M=011101110Sum of all entries:
Therefore:
Indeed this is:
14. Counting edges in a directed graph
Each directed edge appears only once.
So:
assuming a binary matrix without special multiplicities.
15. Adjacency matrix as pairwise table
An adjacency matrix is essentially a table answering:
For every ordered pair of vertices (i,j), does a connection exist?
That is why its size is:
for n vertices.
16. Nested iteration over adjacency matrix
Typical conceptual pseudocode:
textfor each vertex i: for each vertex j: process M[i][j]
This examines every possible ordered pair.
Total cells:
17. Find all neighbors from matrix
For a vertex i, inspect row:
Every column j with:
is an outgoing neighbor.
Example row:
means connections to vertices 2 and 4.
18. Isolated vertex from matrix
In an undirected graph, an isolated vertex has a row of all zeros.
Example:
Its degree:
19. Universal vertex
In a simple undirected graph with n vertices, a vertex connected to every other vertex has:
Its row has:
- 0 on its own diagonal position
- 1 everywhere else
20. Edge-labelled graph
An edge-labelled graph attaches information to edges.
Instead of just:
textA --- B
we may have:
textA --5--> B
where:
is an edge label.
21. What can edge labels represent?
Examples:
Road network
distance in kmFlights
ticket priceComputer network
latencyTransport
travel timeSocial network
relationship typeCapacity network
maximum flow/capacity22. Edge label vs vertex label
These are different.
Vertex label
Information attached to a node.
Example:
textA = Ahmedabad
Edge label
Information attached to a connection.
Example:
textAhmedabad --216 km--> Rajkot
Keep object attributes separate from relationship attributes.
23. Weighted graph intuition
If edge labels are numerical values used to measure path cost, the graph is often called a weighted graph.
Example:
textA --4-- B --7-- C
Path cost from A to C:
24. Label need not mean cost
An edge label could be:
textfriend coworker parent
or:
textred blue green
So:
is broader than:
A weight is typically numerical.
A label can be more general.
25. Matrix representation with edge labels
For numerical edge labels, a matrix can store the label instead of just 0/1.
Example graph:
textA --5-- B | 2 | C
One representation:
Here:
and:
26. Binary adjacency vs weighted matrix
Binary adjacency matrix:
answers:
Does an edge exist?
Weighted matrix:
answers:
What value/weight is attached to the edge?
27. Important ambiguity of zero
Suppose edge weight can itself be:
Then using zero to mean:
no edge
becomes ambiguous.
A graph representation may instead use something conceptually like:
text∞
or:
textNone
for missing edges.
Exact representation depends on the problem.
28. Example weighted matrix
W=047402720This could represent:
textA --4-- B \ | 7 2 \ | C
Since matrix is symmetric, graph is undirected.
29. Directed weighted graph
Suppose:
textA --5--> B B --2--> A
Then:
while:
So weighted matrix can be asymmetric.
Direction and weight are separate properties.
30. Path weight
For a path:
if edge labels represent additive cost, path weight is typically:
Example:
textA --3--> B --4--> C
Total:
31. Direct edge vs cheaper indirect path
Suppose:
textA --10--> C A --3--> B --4--> C
Direct path cost:
Indirect path:
So fewer edges does not necessarily mean lower cost in a weighted graph.
32. Hop count vs weighted distance
Two different notions:
Hop count
Number of edges.
Weighted distance
Sum of edge weights.
Example:
textA --100--> C A --1--> B --1--> C
Direct:
- hops = 1
- cost = 100
Indirect:
- hops = 2
- cost = 2
So the "shortest" path depends on what metric the problem asks for.
33. Labelled-edge lookup
Suppose matrix:
Then:
means edge between vertices 2 and 3 has label/weight 9 under this convention.
34. Matrix traversal with weights
Suppose we want total of all directed edge weights:
texttotal = 0 for each i: for each j: if edge exists: total = total + W[i][j]
For undirected graphs, this may double-count each edge.
35. Undirected weighted sum
If symmetric weighted matrix stores each edge twice:
then summing all off-diagonal weights counts every edge twice.
So total edge weight is:
under that representation.
36. Example
W=035302520Sum:
Total unique edge weight:
Indeed:
37. Edge count vs total edge weight
Do not confuse:
with:
Example:
Three edges with weights:
Then:
but total weight:
38. Presence matrix and label matrix
Sometimes it is conceptually useful to separate:
Adjacency matrix
Aij∈{0,1}Weight/label matrix
WijThis avoids ambiguity between:
- no edge
- edge with numerical label 0
39. Label comparison
Suppose edges represent distances.
You may want:
all edges shorter than 5.
Then:
textfor each i: for each j: if edge exists AND W[i][j] < 5: process edge
This combines:
- nested iteration
- adjacency
- edge labels
- Boolean filtering
40. Example
Suppose:
Edges with weight less than 5:
- A-B with 3
- B-C with 4
A-C with 8 is excluded.
41. Find minimum-weight edge
Conceptually:
textbest = first valid edge for each edge: if weight(edge) < weight(best): best = edge
This is just Week 2's minimum pattern applied to graph edges.
42. Find maximum-weight outgoing edge
For one vertex i:
- inspect row i
- consider valid outgoing edges
- keep largest weight
This combines matrix row traversal with max logic.
43. Example
Row for A:
Ignoring diagonal/no-edge conventions, largest outgoing edge weight:
So the corresponding destination is the column containing 8.
44. Reconstruct graph from adjacency matrix
Given:
read every 1.
Edges:
Do not list both:
and:
separately for an undirected graph.
45. Reconstruct weighted graph from matrix
Given:
graph:
textA --6-- B --3-- C
assuming zero means no edge and diagonal zero means no self-loop.
46. Symmetry check
For undirected weighted graph:
If:
but:
then the representation cannot be an ordinary symmetric undirected weighted graph unless the problem defines some special meaning.
47. Edge-labelled graph with strings
Suppose:
textA --friend--> B B --coworker--> C
A matrix could conceptually store:
textfriend coworker
instead of numbers.
That shows why "labelled" is more general than "weighted."
48. Multiple labels on an edge
In richer systems, one connection might have several attributes:
textdistance = 10 time = 15 cost = 50
Conceptually the edge value could itself be a record.
For Week 8, the main idea is simply that an edge can carry information beyond existence.
49. Matrix storage cost intuition
For n vertices, an adjacency matrix stores:
entries regardless of actual edge count.
For sparse graphs, most entries may be zero/no-edge.
For dense graphs, matrix representation can be natural.
50. Dense vs sparse recap
Dense
Many possible edges exist.
Sparse
Few possible edges exist.
Adjacency matrix:
- straightforward lookup
- potentially wasteful for sparse graphs
This is a representation tradeoff.
51. Constant-time lookup intuition
With an adjacency matrix, asking:
Is there an edge from i to j?
is conceptually direct:
textM[i][j]
No full scan through an edge list is needed.
This is one major advantage of matrix representation.
52. Graph matrix as relation
A binary adjacency matrix can also be seen as encoding a binary relation:
If:
then:
This connects graph representation back to Maths 1 relations.
53. Symmetric relation connection
For an undirected graph:
That is exactly symmetry of a relation.
Matrix symmetry reflects this mathematically.
54. Self-loop and reflexivity connection
If every vertex has a self-loop:
for all i.
This resembles a reflexive relation.
Again, graph matrices and relation matrices are closely connected.
55. Edge-labelled path example
Graph:
textA --4--> B --5--> C \ ^ \------12------/
Possible A→C paths:
Direct
12Via B
4+5=9So if labels represent cost:
is cheaper.
Common mistake: matrix entry direction reversed
If:
means:
then row = source, column = destination.
Do not accidentally interpret:
as:
Always confirm convention.
Common mistake: symmetry in directed graphs
A directed graph may legitimately have:
and:
Do not force symmetry.
Common mistake: divide directed edge count by 2
For directed graph matrices, each edge normally appears once.
So do not divide total 1s by 2.
That rule belongs to symmetric undirected adjacency matrices.
Common mistake: confuse no edge with weight 0
If zero-weight edges are possible, then:
text0
cannot safely mean both:
- no edge
- an edge with weight zero
Read the problem's representation convention carefully.
Common mistake: count diagonal automatically
If diagonal entries represent self-loops, they may need special treatment.
If the graph is simple, diagonal should usually be zero.
Do not blindly include diagonal entries in ordinary pair-edge counts.
Common mistake: path cost = number of vertices
In a weighted graph, path cost is determined by edge labels according to the problem's rule.
Example weights:
Total additive cost:
Number of vertices or edges alone does not determine it.
Common mistake: smaller weight always means better
Only if the problem defines weight as a cost/distance/time to minimize.
If label represents:
- capacity
- reliability
- bandwidth
then larger may be preferable.
Interpret the semantics of the label.
Common mistake: assume label is numeric
Edge-labelled graph does not require numeric labels.
A label can be categorical.
So do not automatically add or compare labels unless the problem gives meaningful operations.
64. Trace example — edge count
M=011100100Algorithm:
textC = 0 for each i: for each j: if M[i][j] == 1: C = C + 1
Final:
But unique undirected edges:
This distinction is important.
65. Trace example — row degree
For:
first row sum:
Therefore:
66. Trace example — weighted filter
Suppose:
Count unique undirected edges with weight ≤4.
Unique edges:
- A-B → 2
- A-C → 7
- B-C → 4
Qualifying:
- A-B
- B-C
So:
67. Efficient unique-edge scan
For an undirected matrix, instead of examining both halves:
textfor i: for j > i: process M[i][j]
This visits only one triangular half of the matrix.
It naturally avoids duplicate edges and the diagonal.
68. Upper-triangle idea
For:
unique undirected edges correspond to:
in the upper triangle.
No need to inspect mirrored lower-triangle entries separately.
69. Why this connects to Week 4
Week 4 unique-pair iteration used:
Week 8 unique undirected edges in a matrix use the same idea:
This is the exact same pairwise structure in a different representation.
70. Week 8 conceptual map
textGRAPH ↓ Adjacency Matrix ├── 0 = no edge ├── 1 = edge ├── symmetric for undirected └── row/column sums reveal degrees ↓ Edge-Labelled Graph ├── edge has extra information ├── numerical label → weight ├── weighted matrix └── path labels may be combined
71. Weeks 7 → 8 connection
Week 7 asked:
What are the vertices and edges?
Week 8 asks:
How do we encode those edges efficiently, and what extra information can each edge carry?
So:
72. What you should actually master
By the end of CT Week 8, you should be able to:
- Construct an adjacency matrix from a graph.
- Reconstruct a graph from an adjacency matrix.
- Interpret Mij correctly.
- Distinguish directed and undirected adjacency matrices.
- Recognize matrix symmetry for undirected graphs.
- Identify self-loops from diagonal entries.
- Compute degree from row sums.
- Compute in-degree/out-degree from columns/rows in directed graphs.
- Count edges correctly without double-counting.
- Explain what an edge-labelled graph is.
- Distinguish edge labels from vertex labels.
- Distinguish labelled graphs from specifically weighted graphs.
- Read numerical edge weights from a matrix.
- Compute additive path weight when that is the defined meaning.
- Understand hop count vs weighted path cost.
- Handle missing-edge representation carefully.
- Use i<j or one triangular half for unique undirected edges.
If those are comfortable, Computational Thinking Week 8 is under control.
Quick Recall & Flashcards
Test your active recall before proceeding to practice problems. Format:
Question :: Answer.- Week 8 Diagnostic: In an edge-weighted graph, what does entry A[i][j] in the adjacency matrix store when an edge exists between vertex i and j? :: Answer: A — In a weighted graph, non-zero matrix entries represent the corresponding edge weights.
- Week 8 Diagnostic: If vertex 1 has outgoing directed edges to vertices 2 and 4, how many non-zero entries exist in row 1 of the directed adjacency matrix? :: Answer: 2 — Row 1 encodes the outgoing edges from vertex 1, which corresponds to 2 non-zero entries (at columns 2 and 4).
Practice & Self-Check
Attempt these diagnostic questions to verify your operational understanding.
1
Question 1
In an edge-weighted graph, what does entry A[i][j] in the adjacency matrix store when an edge exists between vertex i and j?
MCQ
3 Unit Assessment
A. The weight of edge (i, j)
B. Always 1
C. The degree of vertex i
D. The sum of all neighbors
2
Question 2
If vertex 1 has outgoing directed edges to vertices 2 and 4, how many non-zero entries exist in row 1 of the directed adjacency matrix?
NAT
3 Unit Assessment