I have created a list from the input and from the list I have created a matrix also. list-
('A', 'B', 3)
('A', 'D', 4)
('B', 'D', 4)
('B', 'H', 5)
('C', 'L', 2)
('D', 'F', 1)
('F', 'H', 3)
('G', 'H', 2)
('G', 'Y', 2)
('I', 'J', 6)
('I', 'K', 4)
matrix-
['A' 'B' '3']
['A' 'D' '4']
['B' 'D' '4']
['B' 'H' '5']
['C' 'L' '2']
['D' 'F' '1']
['F' 'H' '3']
['G' 'H' '2']
['G' 'Y' '2']
['I' 'J' '6']
['I' 'K' '4']
However I want to create a distance matrix from the above matrix or the list and then print the distance matrix. what will be the correct approach to implement it. In the above matrix the first 2 nodes represent the starting and ending node and the third one is the distance. I am ready to give any further clarification if required.A sample distance matrix is -
[[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]]
I think this question can't be homework because it has a specific format like ("A" , "B" , 3) isn't orthonormal to me, so I decided to help you. But it's better to implement another idea for solving it to help yourself in coding. One approach is to assign a number to each char, then you can specify rows and columns of distance matrix with numbers that are associated with letters! for example impute 'A' to 1, 'B' to 2, and so on:
A┌ 0 3 0 ┐ 1┌ 0 3 0 ┐
B| 3 0 0 |────► 2| 3 0 0 |
C└ 0 0 1 ┘ 3└ 0 0 0 ┘
So in this example, 1 stands for "A" and so on. SO how this is going to help? Example: We have a pattern like ('A', 'B', 3)
, And I consider it as (1, 2, 3)
then I can use the first two values of each tuple for index addressing in a distance matrix:
2
┌─────────────┐
Distance Matrix 1│ 3 ... │
('A', 'B', 3)─────► (1, 2, 3) ───────────────► │. . │
│. . │
│. . │
└─────────────┘
So first of all I'll create an input list as you mentioned. I'll name it lis
:
lis = [('A', 'B', 3),
('A', 'D', 4),
('B', 'D', 4),
('B', 'H', 5),
('C', 'L', 2),
('D', 'F', 1),
('F', 'H', 3),
('G', 'H', 2),
('G', 'Y', 2),
('I', 'J', 6),
('I', 'K', 4)]
Then I detect unique letters in lis
using set
and set.union
. consider we have letters in the first and second argument of each tuple:
items = set.union(set([item[0].upper() for item in lis]) , set([item[1].upper() for item in lis]))
Then I'll make a dictionary to assign values to each letter considering the order of English letters:
value = dict(zip(sorted(items), range(26)))
Then I'll create a zero matrix using numpy.zeros
:
import numpy as np
dist_matrix = np.zeros((len(items) , len(items)))
The last step is assigning the third value of each tuple, to a related position in the distance matrix:
for i in range(len(lis)):
# for Upper triangular
dist_matrix[value[lis[i][0]] , value[lis[i][1]]] = lis[i][2]
# for lower triangular
dist_matrix[value[lis[i][1]] , value[lis[i][0]]] = lis[i][2]
"""
Example:
[0 3 0]
[3 0 0]
[0 0 0]
"""
dist_matrix
This gives me:
array([[0., 3., 0., 4., 0., 0., 0., 0., 0., 0., 0., 0.],
[3., 0., 0., 4., 0., 0., 5., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 2., 0.],
[4., 4., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 3., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 2., 0., 0., 0., 0., 2.],
[0., 5., 0., 0., 3., 2., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 6., 4., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 6., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 4., 0., 0., 0., 0.],
[0., 0., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 2., 0., 0., 0., 0., 0., 0.]])
All code at one glance:
import numpy as np
lis = [('A', 'B', 3),
('A', 'D', 4),
('B', 'D', 4),
('B', 'H', 5),
('C', 'L', 2),
('D', 'F', 1),
('F', 'H', 3),
('G', 'H', 2),
('G', 'Y', 2),
('I', 'J', 6),
('I', 'K', 4)]
items = set.union(set([item[0].upper() for item in lis]) , set([item[1].upper() for item in lis]))
value = dict(zip(sorted(items), range(26)))
dist_matrix = np.zeros((len(items) , len(items)))
for i in range(len(lis)):
# for Upper triangular
dist_matrix[value[lis[i][0]] , value[lis[i][1]]] = lis[i][2]
# for lower triangular
dist_matrix[value[lis[i][1]] , value[lis[i][0]]] = lis[i][2]
"""
Example:
[0 3 0]
[3 0 0]
[0 0 0]
"""
dist_matrix