Discrete Structures Lab 9 Graph: 2.1. Adjacency Matrix - Weighted Matrix
Discrete Structures Lab 9 Graph: 2.1. Adjacency Matrix - Weighted Matrix
DISCRETE STRUCTURES
Lab 9
Graph
1. Introduction
In this tutorial, we will practice graph presentation on computer and matrix techniques.
2. Graph’s Presentations
Read following lecture notes of Discrete Structures on site elit.tdtu.edu.vn
Week14_Graphs_and_Trees_1.pdf
0 2 6 0 1
0 3 5 2
2 0 0 3 0
𝐴1 = [3 0 2 0] 𝐴2 = 6 0 0 0 0
5 2 0 2 0 3 0 0 2
2 0 2 0 [1 0 0 2 0]
Since our graph are not directed graph, the Weighted Matrix can also be represented as only
the upper or lower triangle since they are symmetric about the matrix diagonal:
0 2 6 0 1
0 3 5 2
0 0 0 3 0
𝐴1 = [0 0 2 0] 𝐴2 = 0 0 0 0 0
0 0 0 2
0 0 0 0 2
0 0 0 0 [0 0 0 0 0]
(A,B,2)
(A,C,6)
(A,E,1)
(B,D,3)
(C,D,2)
(D,E,2)
An edge on list often represented in the format of start point, end point then its weight.
Elit.tdtu.edu.vn 2
TON DUC THANG UNIVERSITY
Faculty of Information Technology
A1=np.array([[0,3,5,2],
[0,0,2,0],
[0,0,0,3],
[0,0,0,0]])
G1 = nx.from_numpy_matrix(A1)
pos=nx.spring_layout(G1)
nx.draw_networkx(G1,pos=pos,with_labels=True,labels={a:b for
a,b in enumerate('abcd')})
edge_labels = nx.draw_networkx_edge_labels(G1,font_size=6,
pos=pos,label_pos=0.5)
plt.axis('equal')
plt.show()
The above code shows how to draw the graph of 𝐴1 Weighted Matrix from 2.1
4. Exercise
1. Matrix practice:
a. Write function mPlus(A,B) to calculate the sum of two matrix 𝐴, 𝐵 knowing that 𝐴 +
𝐵 = 𝐶 where 𝐶𝑖,𝑗 = 𝐴𝑖,𝑗 + 𝐵𝑖,𝑗
e. Test your functions with A is the blue matrix and B is the yellow matrix
Elit.tdtu.edu.vn 3
TON DUC THANG UNIVERSITY
Faculty of Information Technology
f. Test your multiply and transpose function with the following A and B matrices
2. Calculate the Weighted Matrices of the following Graphs and graph them on your
computer (the shape of graph might be difference)
3. Calculate the Weighted Matrices of the following List of Edges and graph them on your
computer (the shape of graph might be difference)
a) b)
(A,C,5) (A,C,2)
(A,D,3) (A,D,3)
(B,C,3) (A,E,3)
(B,D,2) (B,C,3)
(C,D,1) (B,D,2)
(C,E,3) (C,D,2)
Elit.tdtu.edu.vn 4
TON DUC THANG UNIVERSITY
Faculty of Information Technology
(D,E,1) (C,E,8)
(D,F,3) (C,F,6)
(E,F,4) (D,F,5)
(E,F,3)
5. We know that:
Monkeys, Apes, Gorillas are Primates.
Mice, Squirrels, Beavers are Rodents.
Crocodiles, Komodo dragons, Lizards are Reptiles.
Coconut trees, Grasses, Oaks are Plants
Mushrooms, Molds, Yeasts are Fungi.
And:
Primates and Rodents are Mammals
Mammals, Rodents, Reptiles are Animals
Animals, Plant, Mushrooms, Molds are Multicellular organisms
Yeasts are Unicellular organisms.
Draw a graph to represent the relation of the above terms.
Elit.tdtu.edu.vn 5