Week 8: Adjacency Matrices & Edge-Labelled Graphs

3331 words
17 min read
2026-09-12T00:00:00.000Z
Python Week 1: the first filter for runtime behavior
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:
Aij={1,if edge i→j exists0,otherwiseA_{ij}= \begin{cases} 1,&\text{if edge }i\to j\text{ exists}\\ 0,&\text{otherwise} \end{cases}

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:
V={A,B,C}.V=\{A,B,C\}.
Graph:
text
A --- B --- C
Adjacency matrix:
M=[010101010]M= \begin{bmatrix} 0&1&0\\ 1&0&1\\ 0&1&0 \end{bmatrix}
Rows and columns follow the same vertex order:
A,B,C.A,B,C.

2. Meaning of one matrix cell

Under the common convention:
Mij=1M_{ij}=1
means:
there is an edge from vertex ii to vertex jj.
And:
Mij=0M_{ij}=0
means no such edge.

3. Vertex order matters

Suppose matrix rows/columns are ordered:
A,B,C.A,B,C.
Then entry:
M23M_{23}
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=[011100100]M= \begin{bmatrix} 0&1&1\\ 1&0&0\\ 1&0&0 \end{bmatrix}
with order:
A,B,C.A,B,C.
Then:
  • A connected to B
  • A connected to C
  • B not connected to C
Graph:
text
B
 \
  A
 /
C

5. Undirected adjacency matrix

For an undirected graph:
A−BA-B
implies both:
A→BA\to B
and:
B→AB\to A
in matrix representation.
Therefore:
Mij=Mji\boxed{ M_{ij}=M_{ji} }
So the matrix is symmetric.

6. Directed adjacency matrix

For a directed graph:
text
A ---> B
we may have:
MAB=1M_{AB}=1
but:
MBA=0.M_{BA}=0.
So the matrix need not be symmetric.

7. Directed example

Graph:
text
A ---> B ---> C
^
|
C
Edges:
A→BA\to B B→CB\to C C→AC\to A
Matrix:
[010001100]\begin{bmatrix} 0&1&0\\ 0&0&1\\ 1&0&0 \end{bmatrix}

8. Diagonal entries

Entry:
MiiM_{ii}
represents an edge from vertex ii to itself.
So:
  • Mii=1M_{ii}=1 → self-loop exists
  • Mii=0M_{ii}=0 → no self-loop
In a simple graph, diagonal entries are usually zero.

9. Row sum

For a simple undirected graph:
row sum=deg⁡(v)\boxed{ \text{row sum}=\deg(v) }
For a directed graph under the common source-row convention:
row sum=out-degree\boxed{ \text{row sum}=\text{out-degree} }

10. Column sum

For directed graphs:
column sum=in-degree\boxed{ \text{column sum}=\text{in-degree} }
assuming:
Mij=1M_{ij}=1
means:
i→j.i\to j.

11. Example

M=[011001100]M= \begin{bmatrix} 0&1&1\\ 0&0&1\\ 1&0&0 \end{bmatrix}
Row sums:
2,1,1.2,1,1.
So out-degrees:
2,1,1.2,1,1.
Column sums:
1,1,2.1,1,2.
So in-degrees:
1,1,2.1,1,2.

12. Counting edges in an undirected graph

Since each undirected edge appears twice in the matrix:
(i,j)(i,j)
and:
(j,i),(j,i),
we have:
∣E∣=12∑i,jMij\boxed{ |E| = \frac12 \sum_{i,j}M_{ij} }
for simple undirected graphs.

13. Example

M=[011101110]M= \begin{bmatrix} 0&1&1\\ 1&0&1\\ 1&1&0 \end{bmatrix}
Sum of all entries:
6.6.
Therefore:
∣E∣=62=3.|E|=\frac62=3.
Indeed this is:
K3.K_3.

14. Counting edges in a directed graph

Each directed edge appears only once.
So:
∣E∣=∑i,jMij\boxed{ |E| = \sum_{i,j}M_{ij} }
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)(i,j), does a connection exist?
That is why its size is:
n×n\boxed{n\times n}
for nn vertices.

16. Nested iteration over adjacency matrix

Typical conceptual pseudocode:
text
for each vertex i:
    for each vertex j:
        process M[i][j]
This examines every possible ordered pair.
Total cells:
n2.n^2.

17. Find all neighbors from matrix

For a vertex ii, inspect row:
i.i.
Every column jj with:
Mij=1M_{ij}=1
is an outgoing neighbor.
Example row:
[0,1,0,1][0,1,0,1]
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:
[0,0,0,0].[0,0,0,0].
Its degree:
0.0.

19. Universal vertex

In a simple undirected graph with nn vertices, a vertex connected to every other vertex has:
deg⁡(v)=n−1.\deg(v)=n-1.
Its row has:
  • 00 on its own diagonal position
  • 11 everywhere else

20. Edge-labelled graph

An edge-labelled graph attaches information to edges.
Instead of just:
text
A --- B
we may have:
text
A --5--> B
where:
55
is an edge label.

21. What can edge labels represent?

Examples:

Road network

distance in km\text{distance in km}

Flights

ticket price\text{ticket price}

Computer network

latency\text{latency}

Transport

travel time\text{travel time}

Social network

relationship type\text{relationship type}

Capacity network

maximum flow/capacity\text{maximum flow/capacity}

22. Edge label vs vertex label

These are different.

Vertex label

Information attached to a node.
Example:
text
A = Ahmedabad

Edge label

Information attached to a connection.
Example:
text
Ahmedabad --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:
text
A --4-- B --7-- C
Path cost from A to C:
4+7=11.4+7=11.

24. Label need not mean cost

An edge label could be:
text
friend
coworker
parent
or:
text
red
blue
green
So:
edge-labelled graph\boxed{ \text{edge-labelled graph} }
is broader than:
weighted graph\boxed{ \text{weighted graph} }
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:
text
A --5-- B
|
2
|
C
One representation:
W=[052500200]W= \begin{bmatrix} 0&5&2\\ 5&0&0\\ 2&0&0 \end{bmatrix}
Here:
WAB=5W_{AB}=5
and:
WAC=2.W_{AC}=2.

26. Binary adjacency vs weighted matrix

Binary adjacency matrix:
Aij∈{0,1}A_{ij}\in\{0,1\}
answers:
Does an edge exist?
Weighted matrix:
WijW_{ij}
answers:
What value/weight is attached to the edge?

27. Important ambiguity of zero

Suppose edge weight can itself be:
0.0.
Then using zero to mean:
no edge
becomes ambiguous.
A graph representation may instead use something conceptually like:
text
∞
or:
text
None
for missing edges.
Exact representation depends on the problem.

28. Example weighted matrix

W=[047402720]W= \begin{bmatrix} 0&4&7\\ 4&0&2\\ 7&2&0 \end{bmatrix}
This could represent:
text
A --4-- B
 \      |
  7     2
   \    |
      C
Since matrix is symmetric, graph is undirected.

29. Directed weighted graph

Suppose:
text
A --5--> B
B --2--> A
Then:
WAB=5W_{AB}=5
while:
WBA=2.W_{BA}=2.
So weighted matrix can be asymmetric.
Direction and weight are separate properties.

30. Path weight

For a path:
v0→v1→⋯→vk,v_0\to v_1\to\cdots\to v_k,
if edge labels represent additive cost, path weight is typically:
∑i=0k−1w(vi,vi+1)\boxed{ \sum_{i=0}^{k-1} w(v_i,v_{i+1}) }
Example:
text
A --3--> B --4--> C
Total:
3+4=7.3+4=7.

31. Direct edge vs cheaper indirect path

Suppose:
text
A --10--> C
A --3--> B --4--> C
Direct path cost:
10.10.
Indirect path:
3+4=7.3+4=7.
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:
text
A --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:
W=[050509090].W= \begin{bmatrix} 0&5&0\\ 5&0&9\\ 0&9&0 \end{bmatrix}.
Then:
W23=9W_{23}=9
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:
text
total = 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:
Wij=Wji,W_{ij}=W_{ji},
then summing all off-diagonal weights counts every edge twice.
So total edge weight is:
12∑i,jWij\boxed{ \frac12 \sum_{i,j}W_{ij} }
under that representation.

36. Example

W=[035302520]W= \begin{bmatrix} 0&3&5\\ 3&0&2\\ 5&2&0 \end{bmatrix}
Sum:
3+5+3+2+5+2=20.3+5+3+2+5+2=20.
Total unique edge weight:
202=10.\frac{20}{2} = \boxed{10}.
Indeed:
3+5+2=10.3+5+2=10.

37. Edge count vs total edge weight

Do not confuse:
∣E∣|E|
with:
∑weights.\sum \text{weights}.
Example:
Three edges with weights:
2,10,50.2,10,50.
Then:
∣E∣=3|E|=3
but total weight:
62.62.

38. Presence matrix and label matrix

Sometimes it is conceptually useful to separate:

Adjacency matrix

Aij∈{0,1}A_{ij}\in\{0,1\}

Weight/label matrix

WijW_{ij}
This 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:
text
for 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:
W=[038304840].W= \begin{bmatrix} 0&3&8\\ 3&0&4\\ 8&4&0 \end{bmatrix}.
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:
text
best = 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 ii:
  1. inspect row ii
  2. consider valid outgoing edges
  3. keep largest weight
This combines matrix row traversal with max logic.

43. Example

Row for A:
[0,5,2,8][0,5,2,8]
Ignoring diagonal/no-edge conventions, largest outgoing edge weight:
8.8.
So the corresponding destination is the column containing 8.

44. Reconstruct graph from adjacency matrix

Given:
M=[010101010],M= \begin{bmatrix} 0&1&0\\ 1&0&1\\ 0&1&0 \end{bmatrix},
read every 1.
Edges:
A−BA-B B−C.B-C.
Do not list both:
A−BA-B
and:
B−AB-A
separately for an undirected graph.

45. Reconstruct weighted graph from matrix

Given:
W=[060603030],W= \begin{bmatrix} 0&6&0\\ 6&0&3\\ 0&3&0 \end{bmatrix},
graph:
text
A --6-- B --3-- C
assuming zero means no edge and diagonal zero means no self-loop.

46. Symmetry check

For undirected weighted graph:
Wij=Wji.W_{ij}=W_{ji}.
If:
W12=5W_{12}=5
but:
W21=7,W_{21}=7,
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:
text
A --friend--> B
B --coworker--> C
A matrix could conceptually store:
text
friend
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:
text
distance = 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 nn vertices, an adjacency matrix stores:
n2n^2
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 ii to jj?
is conceptually direct:
text
M[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:
R⊆V×V.R\subseteq V\times V.
If:
(i,j)∈R,(i,j)\in R,
then:
Mij=1.M_{ij}=1.
This connects graph representation back to Maths 1 relations.

53. Symmetric relation connection

For an undirected graph:
i∼j⇒j∼i.i\sim j \Rightarrow j\sim i.
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:
Mii=1M_{ii}=1
for all ii.
This resembles a reflexive relation.
Again, graph matrices and relation matrices are closely connected.

55. Edge-labelled path example

Graph:
text
A --4--> B --5--> C
 \               ^
  \------12------/
Possible A→C paths:

Direct

1212

Via B

4+5=94+5=9
So if labels represent cost:
A→B→C\boxed{ A\to B\to C }
is cheaper.

Common mistake: matrix entry direction reversed
If:
Mij=1M_{ij}=1
means:
i→j,i\to j,
then row = source, column = destination.
Do not accidentally interpret:
MijM_{ij}
as:
j→i.j\to i.
Always confirm convention.

Common mistake: symmetry in directed graphs
A directed graph may legitimately have:
Mij=1M_{ij}=1
and:
Mji=0.M_{ji}=0.
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:
text
0
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:
3,5,2.3,5,2.
Total additive cost:
10.10.
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=[011100100]M= \begin{bmatrix} 0&1&1\\ 1&0&0\\ 1&0&0 \end{bmatrix}
Algorithm:
text
C = 0

for each i:
    for each j:
        if M[i][j] == 1:
            C = C + 1
Final:
C=4.C=4.
But unique undirected edges:
2.\boxed{2}.
This distinction is important.

65. Trace example — row degree

For:
M=[0110100110010110],M= \begin{bmatrix} 0&1&1&0\\ 1&0&0&1\\ 1&0&0&1\\ 0&1&1&0 \end{bmatrix},
first row sum:
2.2.
Therefore:
deg⁡(A)=2.\deg(A)=2.

66. Trace example — weighted filter

Suppose:
W=[027204740]W= \begin{bmatrix} 0&2&7\\ 2&0&4\\ 7&4&0 \end{bmatrix}
Count unique undirected edges with weight ≤4\leq4.
Unique edges:
  • A-B → 2
  • A-C → 7
  • B-C → 4
Qualifying:
  • A-B
  • B-C
So:
2\boxed{2}

67. Efficient unique-edge scan

For an undirected matrix, instead of examining both halves:
text
for 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:
[−aba−cbc−],\begin{bmatrix} - & a & b\\ a & - & c\\ b & c & - \end{bmatrix},
unique undirected edges correspond to:
a,b,ca,b,c
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:
i<j.i<j.
Week 8 unique undirected edges in a matrix use the same idea:
i<j\boxed{ i<j }
This is the exact same pairwise structure in a different representation.

70. Week 8 conceptual map

text
GRAPH
  ↓
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:
graph structure→matrix representation→edge attributes\boxed{ \text{graph structure} \rightarrow \text{matrix representation} \rightarrow \text{edge attributes} }

72. What you should actually master

By the end of CT Week 8, you should be able to:
  1. Construct an adjacency matrix from a graph.
  2. Reconstruct a graph from an adjacency matrix.
  3. Interpret MijM_{ij} correctly.
  4. Distinguish directed and undirected adjacency matrices.
  5. Recognize matrix symmetry for undirected graphs.
  6. Identify self-loops from diagonal entries.
  7. Compute degree from row sums.
  8. Compute in-degree/out-degree from columns/rows in directed graphs.
  9. Count edges correctly without double-counting.
  10. Explain what an edge-labelled graph is.
  11. Distinguish edge labels from vertex labels.
  12. Distinguish labelled graphs from specifically weighted graphs.
  13. Read numerical edge weights from a matrix.
  14. Compute additive path weight when that is the defined meaning.
  15. Understand hop count vs weighted path cost.
  16. Handle missing-edge representation carefully.
  17. Use i<ji<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

Active Recall Session

Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.