Network Structure

This module provides a breief description of the decentralized network structure.

NetworkArchitecture

Decentralized network architecture

Description

This class is used to generate different network architectures. Here we add four different network architectures: fully connected, fully disconnected, circular network and star network.

Source code in src/desgld/network.py
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class NetworkArchitecture:
    """Decentralized network architecture

    Description:
        This class is used to generate different network
        architectures. Here we add four different network architectures:
        fully connected, fully disconnected, circular network and star network.
    Args:
        size_w (int): the size of the network
        random_seed (int or None): random seed for reproducibility
    Returns:
        Different network architectures
    """

    def __init__(self, size_w, random_seed=None):
        self.size_w = size_w
        self.random_seed = random_seed
        self.set_seed()

    def set_seed(self):
        if self.random_seed is not None:
            np.random.seed(self.random_seed)

    def fully_connected(self):
        """Fully Connected Network

        Description:
            A fully connected network is a kind of network in which all nodes
            are connected to all other nodes.

        Args:
            deg_mat(np.ndarray): degree matrix of the graph Laplacian
            adj_mat(np.ndarray): adjacency matrix of the graph Laplacian
            laplacian_matrix(np.ndarray): graph Laplacian matrix
            eigenvalues (np.ndarray): eigenvalues of the graph Laplacian
            max_eigenvalue (float): maximum eigenvalue of the graph Laplacian
            delta (float): delta of the graph Laplacian


        Examples:
            >>> w=NetworkArchitecture(size_w=5,random_seed=42)
            >>> w=w.fully_connected()
            >>> print(w)
                [[0.67811004 0.08047249 0.08047249 0.08047249 0.08047249]
                [0.08047249 0.67811004 0.08047249 0.08047249 0.08047249]
                [0.08047249 0.08047249 0.67811004 0.08047249 0.08047249]
                [0.08047249 0.08047249 0.08047249 0.67811004 0.08047249]
                [0.08047249 0.08047249 0.08047249 0.08047249 0.67811004]]
            >>> print("Row sums:", np.sum(w, axis=0))
                Row sums: [1. 1. 1. 1. 1.]
            >>> print("Column sums:", np.sum(w, axis=1))
                Column sums: [1. 1. 1. 1. 1.]
        Returns:
            w (float): a fully connected network matrix
        """
        adj_mat = np.ones((self.size_w, self.size_w)) - np.eye(self.size_w)
        deg_mat = (self.size_w - 1) * np.eye(self.size_w)
        laplacian_matrix = deg_mat - adj_mat
        eigenvalues = np.linalg.eigvals(laplacian_matrix)
        max_eigenvalue = np.max(np.real(eigenvalues))
        delta = random.uniform(0, (2 / max_eigenvalue))
        w = np.eye(self.size_w) - delta * laplacian_matrix
        w = np.abs(w)
        row_sums = np.sum(w, axis=1)
        w = w / row_sums[:, np.newaxis]

        return w

    def circular_network(self):
        """Circular Network

        Description:
            A circular network is a kind of network in which a particular
            node is connected to its left and right nodes only.

        Args:
            deg_mat(np.ndarray): degree matrix of the graph Laplacian
            adj_mat(np.ndarray): adjacency matrix of the graph Laplacian
            laplacian_matrix(np.ndarray): graph Laplacian matrix
            eigenvalues (np.ndarray): eigenvalues of the graph Laplacian
            max_eigenvalue (float): maximum eigenvalue of the graph Laplacian
            delta (float): delta of the graph Laplacian


        Returns:
            w (float): a circular network matrix

        Examples:
            >>> w=NetworkArchitecture(size_w=5,random_seed=42)
            >>> w=net.circular_network()
            >>> print(w)
                [[0.72162653 0.13918674 0.         0.         0.13918674]
                [0.13918674 0.72162653 0.13918674 0.         0.        ]
                [0.         0.13918674 0.72162653 0.13918674 0.        ]
                [0.         0.         0.13918674 0.72162653 0.13918674]
                [0.13918674 0.         0.         0.13918674 0.72162653]]
            >>> print("Row sums:", np.sum(w, axis=0))
                Row sums: [1. 1. 1. 1. 1.]
            >>> print("Column sums:", np.sum(w, axis=1))
                Column sums: [1. 1. 1. 1. 1.]
        """
        adj_mat = np.zeros((self.size_w, self.size_w))
        for i in range(self.size_w):
            for j in range(self.size_w):
                if (i + 1) == j:
                    adj_mat[i, j] = 1
                else:
                    adj_mat[i, j] = adj_mat[i, j]
        adj_mat[0, (self.size_w - 1)] = 1
        adj_mat = adj_mat + np.transpose(adj_mat)
        deg_mat = 2 * np.eye(self.size_w)
        laplacian_matrix = deg_mat - adj_mat
        eigenvalues = np.linalg.eigvals(laplacian_matrix)
        max_eigenvalue = np.max(np.real(eigenvalues))
        delta = random.uniform(0, (2 / max_eigenvalue))
        w = np.eye(self.size_w) - delta * laplacian_matrix
        w = np.abs(w)
        row_sums = np.sum(w, axis=1)
        w = w / row_sums[:, np.newaxis]

        return w

    def fully_disconnected(self):
        """Completely Disconnected Network

        Description:
            A completely disconnected network is a kind of network in which
            all the nodes are disconnected from each other.

        Returns:
            w (float): a disconnected network matrix

        Examples:
            >>> net = NetworkArchitecture(size_w=6)
            >>> w=net.circular()
            >>> print(w)
                [[1,0,0,0,0,0],
                [0,1,0,0,0,0],
                [0,0,1,0,0,0],
                [0,0,0,1,0,0],
                [0,0,0,0,1,0],
                [0,0,0,0,0,1]]

        """
        x = [[0] * self.size_w for _ in range(self.size_w)]
        for i in range(self.size_w):
            for j in range(self.size_w):
                if i == j:
                    x[i][j] = 1
        return np.array(x)

    def star_network(self):
        """Star-like Connected Network

        Description:
            A star-like network is a kind of network in which
            there is a central node and all other nodes are
            connected to the central node. However, the individual
            nodes are not connected to each other.

        Args:
            deg_mat(np.ndarray): degree matrix of the graph Laplacian
            adj_mat(np.ndarray): adjacency matrix of the graph Laplacian
            laplacian_matrix(np.ndarray): graph Laplacian matrix
            eigenvalues (np.ndarray): eigenvalues of the graph Laplacian
            max_eigenvalue (float): maximum eigenvalue of the graph Laplacian
            delta (float): delta of the graph Laplacian

        Returns:
            w (float): a star network matrix

        Examples:
            >>> w=NetworkArchitecture(size_w=5,random_seed=42)
            >>> w=net.star_network()
            >>> print(w)
                [[0.71180893 0.07204777 0.07204777 0.07204777 0.07204777]
                [0.07204777 0.92795223 0.         0.         0.        ]
                [0.07204777 0.         0.92795223 0.         0.        ]
                [0.07204777 0.         0.         0.92795223 0.        ]
                [0.07204777 0.         0.         0.         0.92795223]]
            >>> print("Row sums:", np.sum(w, axis=0))
                Row sums: [1. 1. 1. 1. 1.]
            >>> print("Column sums:", np.sum(w, axis=1))
                Column sums: [1. 1. 1. 1. 1.]
        """
        adj_mat = np.zeros((self.size_w, self.size_w))
        for i in range(self.size_w):
            for j in range(self.size_w):
                if i == 0 or j == 0:
                    adj_mat[i, j] = 1
                    adj_mat[i, i] = 0
                else:
                    adj_mat[i, j] = adj_mat[i, j]
        deg_mat = np.eye(self.size_w)
        deg_mat[0, 0] = self.size_w - 1
        laplacian_matrix = deg_mat - adj_mat
        eigenvalues = np.linalg.eigvals(laplacian_matrix)
        max_eigenvalue = np.max(np.real(eigenvalues))
        delta = random.uniform(0, (2 / max_eigenvalue))
        w = np.eye(self.size_w) - delta * laplacian_matrix
        w = np.abs(w)
        row_sums = np.sum(w, axis=1)
        w = w / row_sums[:, np.newaxis]

        return w

circular_network()

Circular Network

Description

A circular network is a kind of network in which a particular node is connected to its left and right nodes only.

Parameters:
  • deg_mat(np.ndarray)

    degree matrix of the graph Laplacian

  • adj_mat(np.ndarray)

    adjacency matrix of the graph Laplacian

  • laplacian_matrix(np.ndarray)

    graph Laplacian matrix

  • eigenvalues (ndarray) –

    eigenvalues of the graph Laplacian

  • max_eigenvalue (float) –

    maximum eigenvalue of the graph Laplacian

  • delta (float) –

    delta of the graph Laplacian

Returns:
  • w( float ) –

    a circular network matrix

Examples:

>>> w=NetworkArchitecture(size_w=5,random_seed=42)
>>> w=net.circular_network()
>>> print(w)
    [[0.72162653 0.13918674 0.         0.         0.13918674]
    [0.13918674 0.72162653 0.13918674 0.         0.        ]
    [0.         0.13918674 0.72162653 0.13918674 0.        ]
    [0.         0.         0.13918674 0.72162653 0.13918674]
    [0.13918674 0.         0.         0.13918674 0.72162653]]
>>> print("Row sums:", np.sum(w, axis=0))
    Row sums: [1. 1. 1. 1. 1.]
>>> print("Column sums:", np.sum(w, axis=1))
    Column sums: [1. 1. 1. 1. 1.]
Source code in src/desgld/network.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def circular_network(self):
    """Circular Network

    Description:
        A circular network is a kind of network in which a particular
        node is connected to its left and right nodes only.

    Args:
        deg_mat(np.ndarray): degree matrix of the graph Laplacian
        adj_mat(np.ndarray): adjacency matrix of the graph Laplacian
        laplacian_matrix(np.ndarray): graph Laplacian matrix
        eigenvalues (np.ndarray): eigenvalues of the graph Laplacian
        max_eigenvalue (float): maximum eigenvalue of the graph Laplacian
        delta (float): delta of the graph Laplacian


    Returns:
        w (float): a circular network matrix

    Examples:
        >>> w=NetworkArchitecture(size_w=5,random_seed=42)
        >>> w=net.circular_network()
        >>> print(w)
            [[0.72162653 0.13918674 0.         0.         0.13918674]
            [0.13918674 0.72162653 0.13918674 0.         0.        ]
            [0.         0.13918674 0.72162653 0.13918674 0.        ]
            [0.         0.         0.13918674 0.72162653 0.13918674]
            [0.13918674 0.         0.         0.13918674 0.72162653]]
        >>> print("Row sums:", np.sum(w, axis=0))
            Row sums: [1. 1. 1. 1. 1.]
        >>> print("Column sums:", np.sum(w, axis=1))
            Column sums: [1. 1. 1. 1. 1.]
    """
    adj_mat = np.zeros((self.size_w, self.size_w))
    for i in range(self.size_w):
        for j in range(self.size_w):
            if (i + 1) == j:
                adj_mat[i, j] = 1
            else:
                adj_mat[i, j] = adj_mat[i, j]
    adj_mat[0, (self.size_w - 1)] = 1
    adj_mat = adj_mat + np.transpose(adj_mat)
    deg_mat = 2 * np.eye(self.size_w)
    laplacian_matrix = deg_mat - adj_mat
    eigenvalues = np.linalg.eigvals(laplacian_matrix)
    max_eigenvalue = np.max(np.real(eigenvalues))
    delta = random.uniform(0, (2 / max_eigenvalue))
    w = np.eye(self.size_w) - delta * laplacian_matrix
    w = np.abs(w)
    row_sums = np.sum(w, axis=1)
    w = w / row_sums[:, np.newaxis]

    return w

fully_connected()

Fully Connected Network

Description

A fully connected network is a kind of network in which all nodes are connected to all other nodes.

Parameters:
  • deg_mat(np.ndarray)

    degree matrix of the graph Laplacian

  • adj_mat(np.ndarray)

    adjacency matrix of the graph Laplacian

  • laplacian_matrix(np.ndarray)

    graph Laplacian matrix

  • eigenvalues (ndarray) –

    eigenvalues of the graph Laplacian

  • max_eigenvalue (float) –

    maximum eigenvalue of the graph Laplacian

  • delta (float) –

    delta of the graph Laplacian

Examples:

>>> w=NetworkArchitecture(size_w=5,random_seed=42)
>>> w=w.fully_connected()
>>> print(w)
    [[0.67811004 0.08047249 0.08047249 0.08047249 0.08047249]
    [0.08047249 0.67811004 0.08047249 0.08047249 0.08047249]
    [0.08047249 0.08047249 0.67811004 0.08047249 0.08047249]
    [0.08047249 0.08047249 0.08047249 0.67811004 0.08047249]
    [0.08047249 0.08047249 0.08047249 0.08047249 0.67811004]]
>>> print("Row sums:", np.sum(w, axis=0))
    Row sums: [1. 1. 1. 1. 1.]
>>> print("Column sums:", np.sum(w, axis=1))
    Column sums: [1. 1. 1. 1. 1.]
Source code in src/desgld/network.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def fully_connected(self):
    """Fully Connected Network

    Description:
        A fully connected network is a kind of network in which all nodes
        are connected to all other nodes.

    Args:
        deg_mat(np.ndarray): degree matrix of the graph Laplacian
        adj_mat(np.ndarray): adjacency matrix of the graph Laplacian
        laplacian_matrix(np.ndarray): graph Laplacian matrix
        eigenvalues (np.ndarray): eigenvalues of the graph Laplacian
        max_eigenvalue (float): maximum eigenvalue of the graph Laplacian
        delta (float): delta of the graph Laplacian


    Examples:
        >>> w=NetworkArchitecture(size_w=5,random_seed=42)
        >>> w=w.fully_connected()
        >>> print(w)
            [[0.67811004 0.08047249 0.08047249 0.08047249 0.08047249]
            [0.08047249 0.67811004 0.08047249 0.08047249 0.08047249]
            [0.08047249 0.08047249 0.67811004 0.08047249 0.08047249]
            [0.08047249 0.08047249 0.08047249 0.67811004 0.08047249]
            [0.08047249 0.08047249 0.08047249 0.08047249 0.67811004]]
        >>> print("Row sums:", np.sum(w, axis=0))
            Row sums: [1. 1. 1. 1. 1.]
        >>> print("Column sums:", np.sum(w, axis=1))
            Column sums: [1. 1. 1. 1. 1.]
    Returns:
        w (float): a fully connected network matrix
    """
    adj_mat = np.ones((self.size_w, self.size_w)) - np.eye(self.size_w)
    deg_mat = (self.size_w - 1) * np.eye(self.size_w)
    laplacian_matrix = deg_mat - adj_mat
    eigenvalues = np.linalg.eigvals(laplacian_matrix)
    max_eigenvalue = np.max(np.real(eigenvalues))
    delta = random.uniform(0, (2 / max_eigenvalue))
    w = np.eye(self.size_w) - delta * laplacian_matrix
    w = np.abs(w)
    row_sums = np.sum(w, axis=1)
    w = w / row_sums[:, np.newaxis]

    return w

fully_disconnected()

Completely Disconnected Network

Description

A completely disconnected network is a kind of network in which all the nodes are disconnected from each other.

Returns:
  • w( float ) –

    a disconnected network matrix

Examples:

>>> net = NetworkArchitecture(size_w=6)
>>> w=net.circular()
>>> print(w)
    [[1,0,0,0,0,0],
    [0,1,0,0,0,0],
    [0,0,1,0,0,0],
    [0,0,0,1,0,0],
    [0,0,0,0,1,0],
    [0,0,0,0,0,1]]
Source code in src/desgld/network.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def fully_disconnected(self):
    """Completely Disconnected Network

    Description:
        A completely disconnected network is a kind of network in which
        all the nodes are disconnected from each other.

    Returns:
        w (float): a disconnected network matrix

    Examples:
        >>> net = NetworkArchitecture(size_w=6)
        >>> w=net.circular()
        >>> print(w)
            [[1,0,0,0,0,0],
            [0,1,0,0,0,0],
            [0,0,1,0,0,0],
            [0,0,0,1,0,0],
            [0,0,0,0,1,0],
            [0,0,0,0,0,1]]

    """
    x = [[0] * self.size_w for _ in range(self.size_w)]
    for i in range(self.size_w):
        for j in range(self.size_w):
            if i == j:
                x[i][j] = 1
    return np.array(x)

star_network()

Star-like Connected Network

Description

A star-like network is a kind of network in which there is a central node and all other nodes are connected to the central node. However, the individual nodes are not connected to each other.

Parameters:
  • deg_mat(np.ndarray)

    degree matrix of the graph Laplacian

  • adj_mat(np.ndarray)

    adjacency matrix of the graph Laplacian

  • laplacian_matrix(np.ndarray)

    graph Laplacian matrix

  • eigenvalues (ndarray) –

    eigenvalues of the graph Laplacian

  • max_eigenvalue (float) –

    maximum eigenvalue of the graph Laplacian

  • delta (float) –

    delta of the graph Laplacian

Returns:
  • w( float ) –

    a star network matrix

Examples:

>>> w=NetworkArchitecture(size_w=5,random_seed=42)
>>> w=net.star_network()
>>> print(w)
    [[0.71180893 0.07204777 0.07204777 0.07204777 0.07204777]
    [0.07204777 0.92795223 0.         0.         0.        ]
    [0.07204777 0.         0.92795223 0.         0.        ]
    [0.07204777 0.         0.         0.92795223 0.        ]
    [0.07204777 0.         0.         0.         0.92795223]]
>>> print("Row sums:", np.sum(w, axis=0))
    Row sums: [1. 1. 1. 1. 1.]
>>> print("Column sums:", np.sum(w, axis=1))
    Column sums: [1. 1. 1. 1. 1.]
Source code in src/desgld/network.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def star_network(self):
    """Star-like Connected Network

    Description:
        A star-like network is a kind of network in which
        there is a central node and all other nodes are
        connected to the central node. However, the individual
        nodes are not connected to each other.

    Args:
        deg_mat(np.ndarray): degree matrix of the graph Laplacian
        adj_mat(np.ndarray): adjacency matrix of the graph Laplacian
        laplacian_matrix(np.ndarray): graph Laplacian matrix
        eigenvalues (np.ndarray): eigenvalues of the graph Laplacian
        max_eigenvalue (float): maximum eigenvalue of the graph Laplacian
        delta (float): delta of the graph Laplacian

    Returns:
        w (float): a star network matrix

    Examples:
        >>> w=NetworkArchitecture(size_w=5,random_seed=42)
        >>> w=net.star_network()
        >>> print(w)
            [[0.71180893 0.07204777 0.07204777 0.07204777 0.07204777]
            [0.07204777 0.92795223 0.         0.         0.        ]
            [0.07204777 0.         0.92795223 0.         0.        ]
            [0.07204777 0.         0.         0.92795223 0.        ]
            [0.07204777 0.         0.         0.         0.92795223]]
        >>> print("Row sums:", np.sum(w, axis=0))
            Row sums: [1. 1. 1. 1. 1.]
        >>> print("Column sums:", np.sum(w, axis=1))
            Column sums: [1. 1. 1. 1. 1.]
    """
    adj_mat = np.zeros((self.size_w, self.size_w))
    for i in range(self.size_w):
        for j in range(self.size_w):
            if i == 0 or j == 0:
                adj_mat[i, j] = 1
                adj_mat[i, i] = 0
            else:
                adj_mat[i, j] = adj_mat[i, j]
    deg_mat = np.eye(self.size_w)
    deg_mat[0, 0] = self.size_w - 1
    laplacian_matrix = deg_mat - adj_mat
    eigenvalues = np.linalg.eigvals(laplacian_matrix)
    max_eigenvalue = np.max(np.real(eigenvalues))
    delta = random.uniform(0, (2 / max_eigenvalue))
    w = np.eye(self.size_w) - delta * laplacian_matrix
    w = np.abs(w)
    row_sums = np.sum(w, axis=1)
    w = w / row_sums[:, np.newaxis]

    return w