0% found this document useful (0 votes)
134 views5 pages

Discrete Structures Lab 9 Graph: 2.1. Adjacency Matrix - Weighted Matrix

This document discusses representing graphs using matrices and lists of edges. It provides examples of adjacency matrices and weighted matrices to represent both undirected and weighted graphs. Functions are defined to perform operations on matrices like addition, subtraction, multiplication, and transposition. Students are asked to write functions to convert between matrix and list of edges representations of graphs and to represent relationships between biological terms as a graph.

Uploaded by

huy phan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views5 pages

Discrete Structures Lab 9 Graph: 2.1. Adjacency Matrix - Weighted Matrix

This document discusses representing graphs using matrices and lists of edges. It provides examples of adjacency matrices and weighted matrices to represent both undirected and weighted graphs. Functions are defined to perform operations on matrices like addition, subtraction, multiplication, and transposition. Students are asked to write functions to convert between matrix and list of edges representations of graphs and to represent relationships between biological terms as a graph.

Uploaded by

huy phan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TON DUC THANG UNIVERSITY

Faculty of Information Technology

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

2.1. Adjacency Matrix - Weighted Matrix


A graph G(V,E) can be represented by its Adjacency Matrix - Weighted Matrix.

Adjacency Matrix is an 𝑛 × n matrix 𝐴 with 𝐴(𝑖, 𝑗) = 1 if (𝑖, 𝑗) in 𝐸 and the Weighted


Matrix is an 𝑛 × n matrix 𝐴 with 𝐴(𝑖, 𝑗) = weight(i, j) if (𝑖, 𝑗) in 𝐸. In other word, Weighted
matrix is a form of Adjacency Matrix where 1 is replaced with the weight of edge (𝑖, 𝑗)

For these graphs, the Weighted Matrix are:


Elit.tdtu.edu.vn 1
TON DUC THANG UNIVERSITY
Faculty of Information Technology

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]

2.2. List of edges


Another common way to represent a matrix is by using list of edges and their weights (if
available). For example, the 2nd graph from 2.1 can be represented as:

(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.

3. Draw graph on python


To draw graph on python we can use the following method:

import matplotlib.pyplot as plt


import networkx as nx
import numpy as np

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 𝐶𝑖,𝑗 = 𝐴𝑖,𝑗 + 𝐵𝑖,𝑗

b. Write function mMinus(A,B) to calculate the difference of two matrix 𝐴, 𝐵 knowing


that 𝐴 − 𝐵 = 𝐶 where 𝐶𝑖,𝑗 = 𝐴𝑖,𝑗 − 𝐵𝑖,𝑗

c. Write function mMultiply(A,B) to calculate the difference of two matrix 𝐴, 𝐵 knowing


that 𝐴 ∗ 𝐵 = 𝐶 where 𝐶𝑖,𝑗 = ∑𝑛𝑘=1 𝐴𝑖,𝑘 ∗ 𝐵𝑘,𝑗 (remember that python count from 0 not
1)
d. Write function mTranspose(A) to calculate the transpose matrix of 𝐴 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)

4. Write a function toLoE(A) convert a weighted matrix A to its list of edges.


*Note: In this Lab, we only use undirected graph, and the Weighted matrix is in upper
triangle form.

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

You might also like