2 Representation en
2 Representation en
Discrete Mathematics 2
2 http://www.ptit.edu.vn
Adjacency Matrix of Undirected Graph
Consider undirected graph 𝐺 =< 𝑉, 𝐸 >, 𝑉 = {1,2, … , 𝑛} is
the set of vertices, 𝐸 = {𝑒1 , 𝑒2 , . . . , 𝑒𝑚 } is a set of edges.
The adjacency matrix of 𝐺 is a square 𝑛 × 𝑛 matrix,
whose elements equal to 0 or 1:
o 𝐴 = {𝑎𝑖𝑗 : 𝑎𝑖𝑗 = 1 𝑖𝑓 (𝑖, 𝑗) ∈ 𝐸, 𝑎𝑖𝑗 = 0 𝑖𝑓 (𝑖, 𝑗) ∉ 𝐸; 𝑖, 𝑗 = 1, 2, . . . , 𝑛}.
3 http://www.ptit.edu.vn
Adjacency Matrix of Undirected Graph
Consider undirected graph 𝐺 =< 𝑉, 𝐸 >, 𝑉 = {1,2, … , 𝑛} is
the set of vertices, 𝐸 = {𝑒1 , 𝑒2 , . . . , 𝑒𝑚 } is a set of edges.
The adjacency matrix of 𝐺 is a square 𝑛 × 𝑛 matrix,
whose elements equal to 0 or 1:
o 𝐴 = {𝑎𝑖𝑗 : 𝑎𝑖𝑗 = 1 𝑖𝑓 (𝑖, 𝑗) ∈ 𝐸, 𝑎𝑖𝑗 = 0 𝑖𝑓 (𝑖, 𝑗) ∉ 𝐸; 𝑖, 𝑗 = 1, 2, . . . , 𝑛}.
0 1
4 http://www.ptit.edu.vn
Properties of Adjacency Matrix
of Undirected Graph
Symmetry: 𝑎𝑖𝑗 = 𝑎𝑗𝑖
Sum of all the elements equals to two times of the number of
edges
𝑛 𝑛
o 𝑖=1 𝑗=1 𝑎𝑖𝑗 = 2|𝐸|
Sum of all the elements in row 𝑢 equals to the degree of
vertex 𝑢
𝑛
o 𝑗=1 𝑎𝑢𝑗 = deg(𝑢)
Sum of all the elements in column 𝑢 equals to the degree of
vertex 𝑢
𝑛
o 𝑖=1 𝑎𝑖𝑢 = deg(𝑢)
𝑝
Let 𝑎𝑖𝑗 (𝑖, 𝑗 = 1,2, … , 𝑛) be the elements of matrix 𝐴𝑝 =
𝑝
𝐴. 𝐴. . . 𝐴 (𝑝 times), then 𝑎𝑖𝑗 is the number of distinct paths
from vertex 𝑖 to vertex 𝑗 through 𝑝 − 1 intermediate vertices
5 http://www.ptit.edu.vn
Adjacency Matrix of Directed Graph
Similar to the adjacency matrix of undirected graph
o There exists a direction on each arrow
o Not symmetric
6 http://www.ptit.edu.vn
Adjacency Matrix of Directed Graph
Similar to the adjacency matrix of undirected graph
o There exists a direction on each arrow
o Not symmetric
7 http://www.ptit.edu.vn
Properties of Adjacency Matrix
of Directed Graph
Sum of all the elements equals to the number of edges
𝑛 𝑛
o 𝑖=1 𝑗=1 𝑎𝑖𝑗 = |𝐸|
Sum of all the elements in row 𝑢 equals to the outdegree
of vertex 𝑢
𝑛
o 𝑗=1 𝑎𝑢𝑗 = 𝑑𝑒𝑔+ (𝑢)
Sum of all the elements in column 𝑢 equals to the
indegree of vertex 𝑢
𝑛
o 𝑖=1 𝑎𝑖𝑢 = 𝑑𝑒𝑔− (𝑢)
𝑝
Let 𝑎𝑖𝑗 (𝑖, 𝑗 = 1,2, … , 𝑛) be the elements of matrix
𝑝
𝐴𝑝 = 𝐴. 𝐴. . . 𝐴 (𝑝 times), then 𝑎𝑖𝑗 is the number of distinct
paths from vertex 𝑖 to vertex 𝑗 through 𝑝 − 1
intermediate vertices
8 http://www.ptit.edu.vn
Weighted (Adjacency) Matrix
A real value 𝑐(𝑒) = 𝑐(𝑢, 𝑣) is assigned to each edge
𝑒 = (𝑢, 𝑣), called the weight of edge e
o In this case the graph is called weighted graph
o Weighted matrix 𝑐 = 𝑐[𝑖, 𝑗], 𝑐[𝑖, 𝑗] = 𝑐(𝑖, 𝑗) if (𝑖, 𝑗) ∈ 𝐸, 𝑐[𝑖, 𝑗] = 𝜃 if
(𝑖, 𝑗) ∉ 𝐸. 𝜃 can received values 0, ∞, −∞ depending on the
problem
9 http://www.ptit.edu.vn
Advantages & Disadvantages
of Adjacency Matrix
Advantages
o Simple, easy to be implemented
Using a two dimensional array to represent
o Easy to check whether two vertices 𝑢, 𝑣 are adjacent or not
Only one comparison operation (𝑎[𝑢][𝑣] ≠ 0?)
Disadvantages
o Waste of memory: we need 𝑛2 memory units despite the number
of edges
o Cannot represent a graph with a large number of vertices
o To find adjacency vertices of vertex 𝑢 we need 𝑛 comparison
operations despite the degree of 𝑢
10 http://www.ptit.edu.vn
Adjacency Matrix Storage
The first line is the number of vertices
The next 𝑛 lines store the adjacency matrix
o Two matrix elements are separated by one or more spaces
11 http://www.ptit.edu.vn
Contents
Graph representation using adjacency matrix
Graph representation using incidence matrix
Graph representation using edge list
Graph representation using adjacency list
12 http://www.ptit.edu.vn
Incidence Matrix of Undirected Graph
Consider undirected graph 𝐺 = (𝑉, 𝐸), 𝑉 = {1,2, … , 𝑛},
𝐸 = {𝑒1 , 𝑒2 , … , 𝑒𝑚 }. Vertex-edge incidence matrix of 𝐺 is a
𝑛 × 𝑚 matrix:
1, 𝑖𝑓 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑎𝑛𝑑 𝑒𝑑𝑔𝑒 𝑗 𝑎𝑟𝑒 𝑖𝑛𝑐𝑖𝑑𝑒𝑛𝑡
𝑎𝑖𝑗 =
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
6
9
1
7
3 5
2 8
4
13 http://www.ptit.edu.vn
Incidence Matrix of Undirected Graph
Consider undirected graph 𝐺 = (𝑉, 𝐸), 𝑉 = {1,2, … , 𝑛},
𝐸 = {𝑒1 , 𝑒2 , … , 𝑒𝑚 }. Vertex-edge incidence matrix of 𝐺 is a
𝑛 × 𝑚 matrix:
1, 𝑖𝑓 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑎𝑛𝑑 𝑒𝑑𝑔𝑒 𝑗 𝑎𝑟𝑒 𝑖𝑛𝑐𝑖𝑑𝑒𝑛𝑡
𝑎𝑖𝑗 =
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
6 1 2 3 4 5 6 7 8 9
9 1 1 1 0 0 0 0 0 0 0
1
7 2 1 0 1 0 1 1 0 0 0
3 5 3 0 1 1 1 0 0 0 0 0
2 8 4 0 0 0 1 1 0 1 1 0
5 0 0 0 0 0 1 1 0 1
4
6 0 0 0 0 0 0 0 1 1
14 http://www.ptit.edu.vn
Incidence Matrix of Directed Graph
Consider directed graph 𝐺 = (𝑉, 𝐸), 𝑉 = {1,2, … , 𝑛},
𝐸 = {𝑒1 , 𝑒2 , … , 𝑒𝑚 }. Vertex-arrow incidence matrix of 𝐺 is
a 𝑛 × 𝑚 matrix:
1, 𝑖𝑓 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑖𝑠 𝑡ℎ𝑒 𝑠𝑡𝑎𝑟𝑡𝑖𝑛𝑔 𝑝𝑜𝑖𝑛𝑡 𝑜𝑓 𝑎𝑟𝑟𝑜𝑤 𝑒𝑗
𝑎𝑖𝑗 = −1, 𝑖𝑓 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑖𝑠 𝑡ℎ𝑒 𝑒𝑛𝑑𝑖𝑛𝑔 𝑝𝑜𝑖𝑛𝑡 𝑜𝑓 𝑎𝑟𝑟𝑜𝑤 𝑒𝑗
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
6
1 9
3 7
5
2 8
4
15 http://www.ptit.edu.vn
Incidence Matrix of Directed Graph
Consider directed graph 𝐺 = (𝑉, 𝐸), 𝑉 = {1,2, … , 𝑛},
𝐸 = {𝑒1 , 𝑒2 , … , 𝑒𝑚 }. Vertex-arrow incidence matrix of 𝐺 is
a 𝑛 × 𝑚 matrix:
1, 𝑖𝑓 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑖𝑠 𝑡ℎ𝑒 𝑠𝑡𝑎𝑟𝑡𝑖𝑛𝑔 𝑝𝑜𝑖𝑛𝑡 𝑜𝑓 𝑎𝑟𝑟𝑜𝑤 𝑒𝑗
𝑎𝑖𝑗 = −1, 𝑖𝑓 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖 𝑖𝑠 𝑡ℎ𝑒 𝑒𝑛𝑑𝑖𝑛𝑔 𝑝𝑜𝑖𝑛𝑡 𝑜𝑓 𝑎𝑟𝑟𝑜𝑤 𝑒𝑗
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
6 1 2 3 4 5 6 7 8 9
1 1 -1 0 0 0 0 0 0 0
1 9
2 -1 0 1 0 1 1 0 0 0
3 7 3 0 1 -1 -1 0 0 0 0 0
5
4 0 0 0 1 -1 0 1 -1 0
2 8
5 0 0 0 0 0 -1 -1 0 1
4 6 0 0 0 0 0 0 0 1 -1
16 http://www.ptit.edu.vn
Contents
Graph representation using adjacency matrix
Graph representation using incidence matrix
Graph representation using edge list
Graph representation using adjacency list
17 http://www.ptit.edu.vn
Edge (Arrow) List
In the case of sparse graphs (𝑚 ≤ 6𝑛), we usually use
edge (arrow) list
o We store all the edges (arrows) of the undirected graph (directed
graph). Each edge (arrow) 𝑒(𝑥, 𝑦) is represented by two variables
𝑠𝑡𝑎𝑟𝑡[𝑒], 𝑒𝑛𝑑[𝑒].
o Advantages: we need only 2𝑚 memory units to store the graph
o Disadvantages: to know which vertices are adjacent to a vertex
we need 𝑚 comparison operations (examine all 𝑚 edges)
o For weighted graph, we need more 𝑚 memory units to store
weights of edges
18 http://www.ptit.edu.vn
Edge List of Undirected Graph
We only list (𝑢, 𝑣), do not list (𝑣, 𝑢)
Should list edges in the ascending order of the starting
points of edges
The number of edges containing 𝑢 (left or right) is the
degree of vertex 𝑢
Starting point Ending point
19 http://www.ptit.edu.vn
Arrow List of Directed Graph
Each arrow is an ordered pair of vertices
o Starting point does not have to be less than ending point
Number of arrows with 𝑢 on the left hand side is 𝑑𝑒𝑔+ (𝑢)
Number of arrows with 𝑢 on the right hand side is
𝑑𝑒𝑔− (𝑢)
Starting point Ending point
20 http://www.ptit.edu.vn
Edge List of Weighted Graph
Add a new column for weights
21 http://www.ptit.edu.vn
Advantages & Disadvantages of Edge List
Advantages
o Save memory in the case of sparse graphs (𝑚 < 6𝑛)
o Convenient for algorithms considering only edges of graphs
Disadvantages
o To now which vertices are adjacent to vertex 𝑢 we have to
examine all the edges of the graph
High computational complexity
22 http://www.ptit.edu.vn
Edge List Storage
The first line stores 𝑁, 𝑀 , the number of vertices and the
number of edges of the graph
o Two numbers are separated by one or more spaces
In the next 𝑀 lines, each line stores an edge of the graph
o Starting point and ending point are separated by one or more
spaces
23 http://www.ptit.edu.vn
Data Structure for Edge List
//Definition of an edge
typedef struct {
int start;
int end;
int weight;
} Edge;
24 http://www.ptit.edu.vn
Contents
Graph representation using adjacency matrix
Graph representation using incidence matrix
Graph representation using edge list
Graph representation using adjacency list
25 http://www.ptit.edu.vn
Adjacency List
For each vertex 𝑢 we store the list of its adjacent vertices
denoted by 𝐴𝑑𝑗(𝑢)
o 𝐴𝑑𝑗(𝑢) = { 𝑣 ∈ 𝑉: (𝑢, 𝑣) ∈ 𝐸}
Adj }
Adj
Adj
Adj
Adj
Adj
26 http://www.ptit.edu.vn
Advantages & Disadvantages
of Adjacency List
Advantages
o Easy to traverse all the vertices of an adjacency list
o Easy to traverse the edges of the graph in each adjacency list
o Optimal representation
Disadvantages
o Difficult for people who are not good at programming
27 http://www.ptit.edu.vn
Adjacency List using Array
Array consists of 𝑛 segments
o The 𝑖 𝑡ℎ segment stores the adjacency list of the 𝑖 𝑡ℎ vertex
o To know where a segment starts or ends, we use another array
storing the starting and ending elements of the segment
Adj }
Adj
Adj
Adj
Adj
Adj
28 http://www.ptit.edu.vn
Adjacency List using Linked List
For each vertex 𝑢 ∈ 𝑉, represents the adjacency list of
the vertex using a linked list 𝐿𝑖𝑠𝑡(𝑢)
29 http://www.ptit.edu.vn
Adjacency List Storage
The first line stores the number of vertices
Next 𝑁 lines store adjacency lists of vertices as follows:
o The first number is the ending position of the segment, other
numbers are list of vertices of the linked list
o Numbers are separated by one or more spaces
30 http://www.ptit.edu.vn
Exercise 1
In a meeting, some visitors make a handshake with some
others. Prove that the total number of handshakes of all
the visitors is even.
31 http://www.ptit.edu.vn
Exercise 2
What is the maximum number of edges that a simple
undirected graph with 𝑛 vertices can has?
32 http://www.ptit.edu.vn
Exercise 3
Represent the undirected graph below using:
1) Adjacency matrix
2) Edge list
3) Adjacency list
2 5
1 7
4
3 6
33 http://www.ptit.edu.vn
Exercise 4
Represent the directed graph below using:
1) Adjacency matrix
2) Edge list
3) Adjacency list
2 5
1 7
4
3 6
34 http://www.ptit.edu.vn
Exercise 5
Represent the weighted graph below using:
1) Weighted matrix
2) Edge list
3
2 5
2 8
6 1
1 2 5
4 7
2
4 7 5
3 4 6
35 http://www.ptit.edu.vn