4.2 Algorithm
5.2.1 Bigraphical structures
As we previously mentioned, a concrete bigraph is represented in memory as two distinct data structures corresponding to its place and link graph. Functions implementing composi-tion and tensor product are also implemented independently on the two structures. Observe that this mirrors the formal definition of bigraphs given in Chapter 3.
We begin by presenting the implementation of concrete place graphs. Our approach consists of representing DAGs with adjacency matrices. These are OCaml values of bool array arraytype. We chose this representation as it allows the implementation of fast in-place modification algorithms. Operations on in-place graphs are encoded by matrix operations.
Formally, given a concrete place graph F : s → r, with |VF| = n, the corresponding (s + n)-by-(n + r) boolean matrix is
F(s+n)×(n+r) =
"
Ss×n Ns×r
Vn×n Rn×r
#
Integers are used as node identifiers, i.e. VF = {0, . . . , n − 1}. Therefore, they can be interpreted as row or column indexes. A zero-based indexing of matrices is used. The four sub-matrices encode different subsets of prntF. Matrix Ss×n corresponds to (sC prntF B VF). It stores the parents of the sites that are nodes. Matrix Ns×rrepresents (sC prntFB r), i.e. the parents of the sites that are roots. Similarly, the other two matrices record the parents
of the nodes of the place graph. Matrices Vn×nand Rn×rencode relations (VFCprntFBVF) and (VF C prntF B r), respectively. Their elements are defined as follows:
mi,j =def
Composition of concrete place graphs follows Definition 3.2.2. It is based on boolean matrix multiplication, i.e. row-by-column multiplication in which summation and product are ∨ and ∧, respectively. Take two concrete bigraphs F : s → r, G : r → r0, with |VF| = n and |VG| = n0. Their run-time representation is given below:
F(s+n)×(n+r) =
The result of the function implementing composition is place graph G ◦ F : s → r0, with
|VG◦F| = n + n0 = m. It is encoded by the following matrix:
Sub-matrix C(s+n)×(n0+r0)is the result of the following multiplication:
C(s+n)×(n0+r0) = by construction, no node in G can have a parent in F . The four sub-matrices encode dif-ferent subsets of prntG◦F. Matrix D(s+n)×n represents prntBV
F, i.e. the places in F that are unaffected by the composition. Analogously, the places in G that do not change are encoded by D0n0×(n0+r0). It corresponds to relation prntCV
G. Matrix C(s+n)×(n0+r0)encodes the edges merged during composition. These are the pairs in relation prnt◦. Finally, 0n0×n encodes
∅. Note that the procedure assures the supports are disjoint. This is because G’s nodes are always below and to the right of F ’s nodes in the matrix representation. This corresponds to adding offset n to all the nodes in G. We illustrate the implementation of composition by
0
Figure 5.3: Example of composition for concrete place graphs with sharing.
showing how it works on the place graphs introduced in Example 3.2.1.
Example 5.2.1. Let concrete place graphs G : 2 → 1, F : 2 → 2 and their composition G ◦ F : 2 → 1 as in Figure 5.3. Their OCaml representation is
where 1 and 0 are short-hands for values true and false. Row indexes are sites and nodes, while column indexes are nodes and roots. Horizontal and vertical delimiters in the representation help to highlight blocks S, N, R and V (clockwise from top left). The representation of composite place graph G ◦ F : 2 → 1 is
[G ◦ F](2+3+3)×(3+3+1) =
In this case, delimiters are introduced to show sub-matrices D, C, D0and 0 (clockwise from top left). The red and blue boxes aid the visualisation of the structure of blocks D and D0,
respectively. Their building blocks in F and G are highlighted with the same colour. The tensor product of concrete place graphs is computed by interleaving the matrices of the two factors as follows:
This is described in the following example:
Example 5.2.2. Take concrete place graphs G : 2 → 1 and F : 2 → 2 defined in Figure 5.3a and Figure 5.3b, respectively. Their run-time representations are given in Example 5.2.1.
The representation of G ⊗ F : 4 → 3 is
Blue boxes indicate the sub-matrices taken from G, while red boxes show the blocks taken
from matrix F.
Support translations are encoded by row and column swapping. For instance, assume (i, j) ∈ ρV, with i 6= j and that G : s → r. When computing ρVG, columns i and j and rows (s + i) and (s + j) are swapped.
We now present the OCaml implementation of concrete link graphs. Our representation of the hyper-graph structure characterising the link map consists of a set of pairs (type Lg.t) in which the first element is a link identifier and the second is the set of its points (type Points.t). Module Set provided by the OCaml standard library is used to implement all the set structures. Link identifiers have variant type
type link = Edg of int | O nam of string .
y
Figure 5.4: Example of composition for concrete link graphs.
This reflects the fact that a link can either be an edge or an outer name. Similarly, points can be ports or inner names. They have type
type point = Port of (int * int) | I nam of string .
The first element in Port is a node index. Note that by Definition 2.2.3 all the point-sets are disjoint.
The tensor product of two concrete link graphs is the union of the two sets of pairs, provided their inner and outer names are disjoint. In order to have disjoint supports, offsets are added to the ports and the edges of the right operand before the union is performed. This is implemented by invoking the library function fold twice. The first time it is used to add the number of edges of the left operand (e offset) to every edge index in the set of pairs representing the right operand. The implementation is given by the following code
(Edg i, p) -> (Edg (i + e offset), p) .
It is executed when iterating over a set of type Lg.t. The second invocation of fold adds the number of nodes of the left operand (v offset) to every port in the point-set of each link. The code executed when iterating over a set of type Points.t is the following:
Port(i, j) -> Port(i + v offset), j) .
Composition of concrete link graphs is implemented according to Definition 2.3.2. The operation can be carried out only when the mediating faces of the two operands in the com-position are equal. This equality check is performed by comparing the strings in the O nam and I nam elements. The first step consists of adding offsets to the left operand in order to have disjoint supports. This operation was described above in the implementation for tensor product. In the next step, each I nam in the point-sets of the right operand is substituted by the point-set associated to the corresponding O nam in the left operand. Finally, all the pairs in the right operand having an Edg as first element are added to the pair set of the left operand. The implementation is explained in the following example.
Example 5.2.3. Consider concrete link graphs G : {y} → {y}, F : {x} → {y} and their composition G ◦ F : {x} → {y} as in Figure 5.4. The two operands are represented in OCaml as two sets of type Lg.t1. The set for link graph G contains only one pair:
[(O nam("y"), [I nam("y"); Port(0,0)])] . Since |VF| = 3, the set after the application of the offset becomes
[(O nam("y"), [ I nam("y") ; Port(3,0)])] . The set for link graph F is
[(O nam("y"), [Port(0,0); Port(1,0)] );
(Edg(0), [I nam("x"); Port(0,1)]);
(Edg(1), [Port(1,1); Port(2,0)])] ]
Composition G ◦ F is the result of adding the edges of F to G and of replacing inner name y with the point-set of outer name y in F . This substitution is highlighted by the coloured boxes in the representations above. The OCaml value of G ◦ F is given by
[(O nam("y"), [[Port(0,0); Port(1,0); Port(3,0)]);
(Edg(0), [I nam("x"); Port(0,1)]);
(Edg(1), [Port(1,1); Port(2,0)])] ]
Support translations are implemented as substitutions of ports and edge-identifiers. An example support translation is the application of an offset we described above.