-4

Suppose I have a boolean matrix: (like this one):

X   0  1  2  3 
0   1, 1, 1, 1
1   1, 1, 1, 0
2   1, 1, 1, 1
3   1, 0, 1, 1 

and I would like to transform it in a graph, in order to find the shortest path between two vertices (I'm going to apply Dijkstra’s algorithm) . I think I allready know how to apply this algorithm in python, the only problem I have is to transform this matrix in a dictionary that could look like this :

graph = {0 : {0:1, 1:1, 2:1, 3:1},
1 : {0:1, 1:1, 2:1},
2 : {0:1, 1:1, 2:1, 3:1},
3 : {0:1, 2:1, 3:1}}

well, I'm not sure that the way I am thinking about it is correct, can somebody help me with this?

abcd
  • 10,215
  • 15
  • 51
  • 85
aB61
  • 17
  • 1
  • 6
  • 5
    What exactly are you trying to do? Please [edit] your question and clarify it, as it stands now it is very unclear what you're asking. – MattDMo Apr 22 '15 at 22:32
  • since you are using 0-5 for your indexes of the first layer of your dict... would it make more sense to just use a list? – TehTris Apr 22 '15 at 22:40
  • That code should work, take a look at [syntax for creating a dictionary into another dictionary in python](http://stackoverflow.com/questions/3817529/syntax-for-creating-a-dictionary-into-another-dictionary-in-python) for other suggestions – LinkBerest Apr 22 '15 at 22:44
  • @MattDMo , I hope my question is more clear, now :) – aB61 Apr 22 '15 at 22:51
  • 1
    @aB61 when you say you "have" this matrix, in what form do you have it? as a `numpy` `array`? as lines in a text file? – abcd Apr 23 '15 at 00:19
  • do you need to transform the matrix into a dictionary? why not transform it into something with a little more computational power, like a `numpy` `array` or `matrix`? – abcd Apr 23 '15 at 00:23

1 Answers1

0

You could create a dictionary as you have in the question, or you could create a list of lists

graph = [
    [1, 1, 1, 1],
    [1, 1, 1, 0],
    [1, 1, 1, 1],
    [1, 0, 1, 1]
]

Using the numpy library (link to pre-built windows installers for convenience) is greatly recommended for operating on matrices in python. The following would read a file containing your data into a numpy matrix, as I believe you are asking (correct me if I'm wrong).

import numpy as np

with open("matrix.txt") as f:
    data = np.genfromtxt((line[3:] for line in f), delimiter=',', skiprows=1)

If print data is called given your example matrix, the following is output:

[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  0.]
 [ 1.  1.  1.  1.]
 [ 1.  0.  1.  1.]]

You'll notice I've used a hard coded value of 3 to strip off the first column - as I don't have your actual data I don't know if this is accurate or not, as I don't know how large row values are formatted. You might want to use line[line.index(' '):] to find the position of the first space instead for greater robustness.

You can then do numpy matrix operations on the data.

enigma
  • 3,476
  • 2
  • 17
  • 30
  • It would be much better if your answer focused on `numpy` and some of its matrix operations... – MattDMo Apr 22 '15 at 23:19
  • @MattDMo yeah, I'll probably update. I'm always wary of focussing on libraries that the asker is not using other than a brief mention, but I can see it may be useful. – enigma Apr 22 '15 at 23:21
  • 1
    @enigma , I want to transform my matrix, that I have in a file and is much more bigger then the one I gave as an example in my question in a dictionary of dictionaries... is it possible to do something like that using some code? – aB61 Apr 22 '15 at 23:21
  • @aB61 and is the much larger matrix in the exact form that you've posted? i.e. with the same heading and left margin entries? – enigma Apr 22 '15 at 23:23
  • @enigma, yes it is . It's a 20x20 matrix – aB61 Apr 22 '15 at 23:26
  • 1
    @aB61 check out NumPy's [matrix](http://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html) structure and associated methods. It's *much* easier to use than trying to do matrix operations on regular Python lists or dicts. – MattDMo Apr 22 '15 at 23:42