# Description

Create a graph representation of a Incidence matrix. An incidence matrix is a matrix that shows the relationship between two classes of objects.

The graph is represented by a matrix of size V (number of vertices) by E (number of edges) where the entry [vertex, edge] contains the edge's endpoint data (simplest case: 1 - incident, 0 - not incident).

# Synopse

Create a graph representation of a Incidence matrix.

• incmxt = iaincmxtcreate(f)
• incmxt: Incidence graph.
• f: Input Image.

# Function Code

``` 1 from numpy import *
2 import StringIO
3 graph = gvgen.GvGen()
4 graph.smart_mode = 1
5
6 def iaincmxtcreate(matrix):
7     graph.styleDefaultAppend("color", "blue")
8
9     # create a node array
10     node = []
11     for i in range(matrix.shape[0]):
12         node.append(graph.newItem(str(i)))
13
14     # auxiliary variable to identify source and destiny nodes
15     aux = 0
16
17
18     for j in range(matrix.shape[1]):
19         aux = 0
20         for i in range(matrix.shape[0]):
21             if matrix[i,j]==1:
22                 if aux ==0:
23                     source_index = i # save the source node
24                     aux = 1
25                 else:
26                     destiny_index = i # save the destiny node
27
28         # create a non-oriented link between nodes
31
32
33
34     fd = StringIO.StringIO()
35     graph.dot(fd)
36     dottext = fd.getvalue()
37
38     return dottext
```

# Graphic Representation of a Incidence Matrix Example

For example the incidence matrix of the undirected graph shown below is a matrix consisting of 4 rows (corresponding to the four vertices, 1-4) and 4 columns (corresponding to the four edges, e1-e4):

``` 1 from numpy import *
2 import StringIO
3 graph = gvgen.GvGen()
4 graph.smart_mode = 1
5
6 matrix = array( [ (1,1,1,0,1,0),
7                   (1,0,0,0,0,1),
8                   (0,1,0,1,0,1),
9                   (0,0,1,1,0,0),
10                   (0,0,0,0,1,0) ] )
11 mmgraphviz(iaincmxtcreate(matrix), title='Graphic Representation of a Incidence Matrix')
```