#### Previous topic

8. connectivity — A class and functions for handling nodal connectivity.

#### Next topic

10. elements — Definition of elements.

# 9. adjacency — A class for storing and handling adjacency tables.¶

This module defines a specialized array class for representing adjacency of items of a single type. This is e.g. used in mesh models, to store the adjacent elements.

A class for storing and handling adjacency tables.

An adjacency table defines a neighbouring relation between elements of a single collection. The nature of the relation is not important, but should be a binary relation: two elements are either related or they are not.

Typical applications in pyFormex are the adjacency tables for storing elements connected by a node, or by an edge, or by a node but not by an edge, etcetera.

Conceptually the adjacency table corresponds with a graph. In graph theory however the data are usually stored as a set of tuples (a,b) indicating a connection between the elements a and b. In pyFormex elements are numbered consecutively from 0 to nelems-1, where nelems is the number of elements. If the user wants another numbering, he can always keep an array with the actual numbers himself. Connections between elements are stored in an efficient two-dimensional array, holding a row for each element. This row contains the numbers of the connected elements. Because the number of connections can be different for each element, the rows are padded with an invalid elements number (-1).

A normalized Adjacency is one where all rows do not contain duplicate nonnegative entries and are sorted in ascending order and where no column contains only -1 values. Also, since the adjacency is defined within a single collection, no row should contain a value higher than the maximum row index.

A new Adjacency table is created with the following syntax

```Adjacency(data=[],dtyp=None,copy=False,ncon=0,normalize=True)
```

Parameters:

• data: should be compatible with an integer array with shape (nelems,ncon), where nelems is the number of elements and ncon is the maximum number of connections per element.
• dtyp: can be specified to force an integer type but is set by default from the passed data.
• copy: can be set True to force copying the data. By default, the specified data will be used without copying, if possible.
• ncon: can be specified to force a check on the plexitude of the data, or to set the plexitude for an empty Connectivity. An error will be raised if the specified data do not match the specified plexitude.
• normalize: boolean: if True (default) the Adjacency will be normalized at creation time.
• allow_self: boolean: if True, connections of elements with itself are allowed. The default (False) will remove self-connections when the table is normalized.

Example:

```>>> print Adjacency([[1,2,-1],
...                  [3,2,0],
...                  [1,-1,3],
...                  [1,2,-1],
...                  [-1,-1,-1]])
[[-1  1  2]
[ 0  2  3]
[-1  1  3]
[-1  1  2]
[-1 -1 -1]]
```
nelems()

Return the number of elements in the Adjacency table.

maxcon()

Return the maximum number of connections for any element.

This returns the row count of the Adjacency.

normalize()

A normalized adjacency table is one where each row:

• does not contain the row index itself,
• does not contain duplicates,
• is sorted in ascending order,

and that has at least one row without -1 value.

By default, an Adjacency is normalized when it is constructed. Performing operations on an Adjacency may however leave it in a non-normalized state. Calling this method will normalize it again. This can obviously also be obtained by creating a new Adjacency with self as data.

Returns: an integer array with shape (adj.shape[0],maxc), with maxc <= adj.shape[1], where row i retains the unique non-negative numbers of the original array except the value i, and is possibly padded with -1 values.

Example:

```>>> a = Adjacency([[ 0,  0,  0,  1,  2,  5],
...                [-1,  0,  1, -1,  1,  3],
...                [-1, -1,  0, -1, -1,  2],
...                [-1, -1,  1, -1, -1,  3],
...                [-1, -1, -1, -1, -1, -1],
...                [-1, -1,  0, -1, -1,  5]])
>>> a.normalize()
[-1,  0,  3],
[-1, -1,  0],
[-1, -1,  1],
[-1, -1, -1],
[-1, -1,  0]])
```
pairs()

Return all pairs of adjacent element.

Returns an integer array with two columns, where each row contains a pair of adjacent elements. The element number in the first columne is always the smaller of the two element numbers.

Return the symmetric difference of two adjacency tables.

Parameters:

Returns an adjacency table of the same length, where each row contains all the (nonnegative) numbers of the corresponding rows of self and adj, except those that occur in both.

frontFactory(startat=0, frontinc=1, partinc=1)

Generator function returning the frontal elements.

This is a generator function and is normally not used directly, but via the frontWalk() method.

It returns an int array with a value for each element. On the initial call, all values are -1, except for the elements in the initial front, which get a value 0. At each call a new front is created with all the elements that are connected to any of the current front and which have not yet been visited. The new front elements get a value equal to the last front’s value plus the frontinc. If the front becomes empty and a new starting front is created, the front value is extra incremented with partinc.

Parameters: see frontWalk().

Example:

```>>> A = Adjacency([[1,2,-1],
...                  [3,2,0],
...                  [1,-1,3],
...                  [1,2,-1],
...                  [-1,-1,-1]])
>>> for p in A.frontFactory(): print p
[ 0 -1 -1 -1 -1]
[ 0  1  1 -1 -1]
[ 0  1  1  2 -1]
[0 1 1 2 4]
```
frontWalk(startat=0, frontinc=1, partinc=1, maxval=-1)

Walks through the elements by their node front.

A frontal walk is executed starting from the given element(s). A number of steps is executed, each step advancing the front over a given number of single pass increments. The step number at which an element is reached is recorded and returned.

Parameters:

• startat: initial element numbers in the front. It can be a single element number or a list of numbers.
• frontinc: increment for the front number on each frontal step.
• partinc: increment for the front number when the front
• maxval: maximum frontal value. If negative (default) the walk will continue until all elements have been reached. If non-negative, walking will stop as soon as the frontal value reaches this maximum.

Returns: an array of integers specifying for each element in which step the element was reached by the walker.

Example:

```>>> A = Adjacency([
...       [-1,  1,  2,  3],
...       [-1,  0,  2,  3],
...       [ 0,  1,  4,  5],
...       [-1, -1,  0,  1],
...       [-1, -1,  2,  5],
...       [-1, -1,  2,  4]])
>>> print A.frontWalk()
[0 1 1 1 2 2]
```

An adjacency table is an integer array where each row lists the numbers of the items that are connected to the item with number equal to the row index. Rows are padded with -1 value to create rows of equal length.

This function sorts the rows of the adjacency table in ascending order and removes all columns containing only -1 values.

Paramaters:

• adj: an 2-D integer array with values >=0 or -1

Returns: an integer array with shape (adj.shape[0],maxc), with maxc <= adj.shape[1], where the rows are sorted in ascending order and where columns with only -1 values are removed.

Example:

```>>> a = array([[ 0,  2,  1, -1],
...            [-1,  3,  1, -1],
...            [ 3, -1,  0,  1],
...            [-1, -1, -1, -1]])
array([[ 0,  1,  2],
[-1,  1,  3],
[ 0,  1,  3],
[-1, -1, -1]])
```

An adjacency table is an integer array where each row lists the numbers of the items that are connected to the item with number equal to the row index. Rows are padded with -1 values to create rows of equal length.

A reduced adjacency table is one where each row:

• does not contain the row index itself,
• does not contain duplicates,
• is sorted in ascending order,

and that has at least one row without -1 value.

Paramaters:

• adj: an 2-D integer array with value >=0 or -1

Returns: an integer array with shape (adj.shape[0],maxc), with maxc <= adj.shape[1], where row i retains the unique non-negative numbers of the original array except the value i, and is possibly padded with -1 values.

Example:

```>>> a = array([[ 0,  0,  0,  1,  2,  5],
...            [-1,  0,  1, -1,  1,  3],
...            [-1, -1,  0, -1, -1,  2],
...            [-1, -1,  1, -1, -1,  3],
...            [-1, -1, -1, -1, -1, -1],
...            [-1, -1,  0, -1, -1,  5]])