# Description

Create a graph representation from an Adjacency matrix. This is an n by n matrix A, where n is the number of vertices in the graph. If there is an edge from a vertex x to a vertex y, then the element A(x,y) is different of Inf. An extension to the Adjacency matrix is to set a value to its elements to represent the arc weight (different from Inf). There is an option to visualize the arc weight. The nodes are numbered sequentially starting from zero.

# Synopse

Create a graph representation from an Adjacency matrix to show using graphviz.

• g = iaadjmxtcreate2(A,label_vector,seeds_id = False,dist=False,label=False)
• g: Output graph representation suitable to display using graphviz.
• A: Adjacency matrix: weighted or not.
• label_vector: Label of each node.
• dist: Display the arc weight (optional).
• label: Display the label of each node (optional).

# Limitation

Limited number of input classes

# Function Code

``` 1 from numpy import *
2 from iaOPF import *
3
4 import StringIO
5
6 def iaadjmxtcreate2(A,label_vector,seeds_id = False,dist=False,label=False):
7     import gvgen
8     graph = gvgen.GvGen()
9     graph.smart_mode = 1
10
11     # MST calculation by Prim's algorithm for visualization purpose
12     MST = iaprim(A)
13
14     # Seeds calculation for visualization purpose
15     seeds = array(iamstseeds(MST,label_vector))
16
17     # Create a node array
18     node = []
19     for i in range(A.shape[0]):
20         node.append (graph.newItem(str(i)))
21
22     # Asign colors to classes
23     if seeds_id:
24         for i in range(A.shape[0]):
25             if label_vector[i]==0:
26                 graph.styleAppend("Post_0", "color", "blue")
27                 graph.styleAppend("Post_0", "style", "filled")
28                 graph.styleApply("Post_0", node[i])
29             else:
30                 graph.styleAppend("Post_1", "color", "red")
31                 graph.styleAppend("Post_1", "style", "filled")
32                 graph.styleApply("Post_1", node[i])
33
34     # Use rectangles to identify seeds
35     if seeds_id:
36         for i in range(seeds.shape[0]):
37             if label_vector[seeds[i]]==0:
38                 graph.styleAppend("Post_2", "color", "blue")
39                 graph.styleAppend("Post_2", "style", "filled")
40                 graph.styleAppend("Post_2", "shape", "rectangle")
41                 graph.styleApply("Post_2", node[seeds[i]])
42             else:
43                 graph.styleAppend("Post_3", "color", "red")
44                 graph.styleAppend("Post_3", "style", "filled")
45                 graph.styleAppend("Post_3", "shape", "rectangle")
46                 graph.styleApply("Post_3", node[seeds[i]])
47
48     ## check if is a symmetric matrix
49
50     symmetric = (A.transpose() == A).all()
51
52     for i in range(A.shape[0]):
53         if symmetric:
54             indice = i+1
55         else:
56             indice = A.shape[1]
57
58         # create graph visualization
59
60         for j in range(indice):
61             if A[j,i]!=float('Inf'):
62                 if dist: # create edge with label (weight)
64                 else: # create edge without label
66                 if symmetric: # non-oriented edge
67                     graph.propertyAppend(link1, "dir", "none")
68                 if MST[j,i]==float('Inf'): # modify MST edges
70
71
72     fd = StringIO.StringIO()
73     graph.dot(fd)
74     dottext = fd.getvalue()
75
76     return dottext
```

# Graph representation

``` 1 from numpy import *
2 from iaOPF import *
3
4 A = array([[Inf,1,Inf,4],
5            [1,Inf,2,5],
6            [Inf,2,Inf,6],
7            [4,5,6,Inf]])
8
9 label_vector = array([0,0,0,1])
10
11 print 'adjacency matrix \n',A
12 B = iaprim(A)
13 print 'MST \n',B
16
17 C = iamstseeds(B,label_vector)
18 print 'list of prototypes \n',C
19
```
```adjacency matrix
[[ Inf   1.  Inf   4.]
[  1.  Inf   2.   5.]
[ Inf   2.  Inf   6.]
[  4.   5.   6.  Inf]]
MST
[[ Inf   1.  Inf   4.]
[  1.  Inf   2.  Inf]
[ Inf   2.  Inf  Inf]
[  4.  Inf  Inf  Inf]]
list of prototypes
[0, 3]
```