Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

plot digraph use adjacency matrix

Hi everyone, I got a question here, and I have been trying to debug it for past week, still can't get it sorted out, can anyone please help me? Thanks heaps.

I have a file as my data (will paste at the bottom), basically the vertices are all binary strings with directed edges named "a" or "b", or "c". For example, if in my data, there is line says (0000, a, 1000), that means there is an edge from vertex 0000 to 1000 named a. Similarly, if I have (0000, b, 0000), means there is a self-loop from 0000 to it self named b. So there can be more than one self-loops sometimes.

Now I have written the function digraph as below:

def digraph(n, filename): f = open(filename, 'r') W = Words('01', length=n) S = [str(w) for w in W] M = zero_matrix(ZZ, 2^n) f.readline() for c in f: c = c[1:-2] s1, g, s2 = c.split(', ') i = S.index('word: ' + s1) j = S.index('word: ' + s2) M[i,j] = 1 f.close() return DiGraph(M, vertex_labels=S, loops = True, implementation="c_graph")

So the idea should be pretty simple, create list of words with all binary strings length 4, then search where my data the words are allocated in the list, then assign value to 1 to the entries of my original zero matrix.

But I keep getting error message says change a copy of the matrix instead, I tried to make a copy(M), but still doesn't work, then it says something else like "need more than 1 value unpack" etc.

I really need a hand on this please, can anybody help me please? Also, I can't understand why it is exactly same code, but used to run very well on someone else's linux computer two months ago, and I didn't change anything but it just wouldn't run here and the error messages seem like syntax error.

Here are my data as below

(0000, a, 1000) (0000, b, 0000) (0000, c, 0000) (0001, a, 1001) (0001, b, 0001) (0001, c, 0001) (0010, a, 1010) (0010, b, 0010) (0010, c, 0010) (0011, a, 1011) (0011, b, 0011) (0011, c, 0011) (0100, a, 1110) (0100, b, 0100) (0100, c, 0100) (0101, a, 1111) (0101, b, 0101) (0101, c, 0101) (0110, a, 1100) (0110, b, 0111) (0110, c, 0111) (0111, a, 1101) (0111, b, 0110) (0111, c, 0110) (1000, a, 0000) (1000, b, 1000) (1000, c, 1100) (1001, a, 0001) (1001, b, 1001) (1001, c, 1101) (1010, a, 0010) (1010, b, 1010) (1010, c, 1111) (1011, a, 0011) (1011, b, 1011) (1011, c, 1110) (1100, a, 0110) (1100, b, 1110) (1100, c, 1000) (1101, a, 0111) (1101, b, 1111) (1101, c, 1001) (1110, a, 0100) (1110, b, 1100) (1110, c, 1011) (1111, a, 0101) (1111, b, 1101) (1111, c, 1010)

plot digraph use adjacency matrix

Hi everyone, I got a question here, and I have been trying to debug it for past week, still can't get it sorted out, can anyone please help me? Thanks heaps.

I have a file as my data (will paste at the bottom), basically the vertices are all binary strings with directed edges named "a" or "b", or "c". For example, if in my data, there is line says (0000, a, 1000), that means there is an edge from vertex 0000 to 1000 named a. Similarly, if I have (0000, b, 0000), means there is a self-loop from 0000 to it self named b. So there can be more than one self-loops sometimes.

Now I have written the function digraph as below:

def digraph(n, filename):
    f = open(filename, 'r')
    W = Words('01', length=n)
    S = [str(w) for w in W]
    M = zero_matrix(ZZ, 2^n)
    f.readline()
    for c in f:
        c = c[1:-2]
        s1, g, s2 = c.split(', ')
        i = S.index('word: ' + s1)
        j = S.index('word: ' + s2)
        M[i,j] = 1
    f.close()
    return DiGraph(M, vertex_labels=S, loops = True, implementation="c_graph")

implementation="c_graph")

So the idea should be pretty simple, create list of words with all binary strings length 4, then search where my data the words are allocated in the list, then assign value to 1 to the entries of my original zero matrix.

But I keep getting error message says change a copy of the matrix instead, I tried to make a copy(M), but still doesn't work, then it says something else like "need more than 1 value unpack" etc.

I really need a hand on this please, can anybody help me please? Also, I can't understand why it is exactly same code, but used to run very well on someone else's linux computer two months ago, and I didn't change anything but it just wouldn't run here and the error messages seem like syntax error.

Here are my data as below

(0000, a, 1000)
(0000, b, 0000)
(0000, c, 0000)
(0001, a, 1001)
(0001, b, 0001)
(0001, c, 0001)
(0010, a, 1010)
(0010, b, 0010)
(0010, c, 0010)
(0011, a, 1011)
(0011, b, 0011)
(0011, c, 0011)
(0100, a, 1110)
(0100, b, 0100)
(0100, c, 0100)
(0101, a, 1111)
(0101, b, 0101)
(0101, c, 0101)
(0110, a, 1100)
(0110, b, 0111)
(0110, c, 0111)
(0111, a, 1101)
(0111, b, 0110)
(0111, c, 0110)
(1000, a, 0000)
(1000, b, 1000)
(1000, c, 1100)
(1001, a, 0001)
(1001, b, 1001)
(1001, c, 1101)
(1010, a, 0010)
(1010, b, 1010)
(1010, c, 1111)
(1011, a, 0011)
(1011, b, 1011)
(1011, c, 1110)
(1100, a, 0110)
(1100, b, 1110)
(1100, c, 1000)
(1101, a, 0111)
(1101, b, 1111)
(1101, c, 1001)
(1110, a, 0100)
(1110, b, 1100)
(1110, c, 1011)
(1111, a, 0101)
(1111, b, 1101)
(1111, c, 1010)

1010)
click to hide/show revision 3
retagged

plot digraph use adjacency matrix

Hi everyone, I got a question here, and I have been trying to debug it for past week, still can't get it sorted out, can anyone please help me? Thanks heaps.

I have a file as my data (will paste at the bottom), basically the vertices are all binary strings with directed edges named "a" or "b", or "c". For example, if in my data, there is line says (0000, a, 1000), that means there is an edge from vertex 0000 to 1000 named a. Similarly, if I have (0000, b, 0000), means there is a self-loop from 0000 to it self named b. So there can be more than one self-loops sometimes.

Now I have written the function digraph as below:

def digraph(n, filename):
    f = open(filename, 'r')
    W = Words('01', length=n)
    S = [str(w) for w in W]
    M = zero_matrix(ZZ, 2^n)
    f.readline()
    for c in f:
        c = c[1:-2]
        s1, g, s2 = c.split(', ')
        i = S.index('word: ' + s1)
        j = S.index('word: ' + s2)
        M[i,j] = 1
    f.close()
    return DiGraph(M, vertex_labels=S, loops = True, implementation="c_graph")

So the idea should be pretty simple, create list of words with all binary strings length 4, then search where my data the words are allocated in the list, then assign value to 1 to the entries of my original zero matrix.

But I keep getting error message says change a copy of the matrix instead, I tried to make a copy(M), but still doesn't work, then it says something else like "need more than 1 value unpack" etc.

I really need a hand on this please, can anybody help me please? Also, I can't understand why it is exactly same code, but used to run very well on someone else's linux computer two months ago, and I didn't change anything but it just wouldn't run here and the error messages seem like syntax error.

Here are my data as below

(0000, a, 1000)
(0000, b, 0000)
(0000, c, 0000)
(0001, a, 1001)
(0001, b, 0001)
(0001, c, 0001)
(0010, a, 1010)
(0010, b, 0010)
(0010, c, 0010)
(0011, a, 1011)
(0011, b, 0011)
(0011, c, 0011)
(0100, a, 1110)
(0100, b, 0100)
(0100, c, 0100)
(0101, a, 1111)
(0101, b, 0101)
(0101, c, 0101)
(0110, a, 1100)
(0110, b, 0111)
(0110, c, 0111)
(0111, a, 1101)
(0111, b, 0110)
(0111, c, 0110)
(1000, a, 0000)
(1000, b, 1000)
(1000, c, 1100)
(1001, a, 0001)
(1001, b, 1001)
(1001, c, 1101)
(1010, a, 0010)
(1010, b, 1010)
(1010, c, 1111)
(1011, a, 0011)
(1011, b, 1011)
(1011, c, 1110)
(1100, a, 0110)
(1100, b, 1110)
(1100, c, 1000)
(1101, a, 0111)
(1101, b, 1111)
(1101, c, 1001)
(1110, a, 0100)
(1110, b, 1100)
(1110, c, 1011)
(1111, a, 0101)
(1111, b, 1101)
(1111, c, 1010)