|
1
|
|
|
''' |
|
2
|
|
|
shuffle_graph - This is a graph shuffling package. |
|
3
|
|
|
Copyright (C) 2019 sosei |
|
4
|
|
|
|
|
5
|
|
|
This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
it under the terms of the GNU Affero General Public License as published |
|
7
|
|
|
by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
(at your option) any later version. |
|
9
|
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
|
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
GNU Affero General Public License for more details. |
|
14
|
|
|
|
|
15
|
|
|
You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
''' |
|
18
|
|
|
|
|
19
|
|
|
__all__ = ['calculate_number_of_shuffles_required_under_default_random_function', 'shuffle_graph'] |
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
def calculate_number_of_shuffles_required_under_default_random_function(node_number: int) -> int: |
|
|
|
|
|
|
22
|
|
|
''' |
|
23
|
|
|
Python's random number function USES the Mersenne Twister algorithm, which has a period of 2**19937-1.If the total permutation of graph nodes is larger than the random function period, the card cannot be shuffled only once. |
|
24
|
|
|
The total permutation of a graph node is "factorial of the number of nodes".The number of binary digits of the total number of permutations can be calculated by Stirling's formula. |
|
25
|
|
|
|
|
26
|
|
|
>>> calculate_number_of_shuffles_required_under_default_random_function(1000) |
|
27
|
|
|
1 |
|
28
|
|
|
>>> calculate_number_of_shuffles_required_under_default_random_function(10000) |
|
29
|
|
|
6 |
|
30
|
|
|
''' |
|
31
|
|
|
|
|
32
|
|
|
import math |
|
33
|
|
|
if node_number > 0: |
|
34
|
|
|
bit_length_of_permutation_number = math.ceil(math.log2(2*math.pi*node_number)/2 + math.log2(node_number/math.e)*node_number) |
|
35
|
|
|
shuffle_number = math.ceil(bit_length_of_permutation_number/19937) |
|
36
|
|
|
else: |
|
37
|
|
|
shuffle_number = 0 |
|
38
|
|
|
return shuffle_number |
|
39
|
|
|
|
|
40
|
|
View Code Duplication |
def shuffle_graph(data_graph: 'NetworkXGraphObject', shuffle_number: int, seed: int = None) -> 'NetworkXGraphObject': |
|
|
|
|
|
|
41
|
|
|
''' |
|
42
|
|
|
Returns a new graph, shuffling the order of the nodes in the input data_graph, but the relationship between the nodes remains the same. The data_graph doesn't change. |
|
43
|
|
|
|
|
44
|
|
|
Parameters |
|
45
|
|
|
---------- |
|
46
|
|
|
data_graph : NetworkXGraphObject |
|
47
|
|
|
A NetworkX graph object. |
|
48
|
|
|
|
|
49
|
|
|
shuffle_number : integer |
|
50
|
|
|
Set the number of shuffles. |
|
51
|
|
|
|
|
52
|
|
|
seed : integer, random_state, or None (default) |
|
53
|
|
|
Indicator of random number generation state. |
|
54
|
|
|
|
|
55
|
|
|
Returns |
|
56
|
|
|
------- |
|
57
|
|
|
new_order_data_graph : NetworkXGraphObject |
|
58
|
|
|
Returns a new graph that shuffles the order of nodes but keeps the relationships between them the same. |
|
59
|
|
|
|
|
60
|
|
|
Examples |
|
61
|
|
|
-------- |
|
62
|
|
|
>>> G = Graph({0: {1: {}}, 1: {0: {}, 2: {}}, 2: {1: {}, 3: {}}, 3: {2: {}, 4: {}}, 4: {3: {}}}) |
|
63
|
|
|
>>> shuffle_graph(G, 1, 65535).adj #Set seed to make the results repeatable. |
|
64
|
|
|
AdjacencyView({3: {2: {}, 4: {}}, 4: {3: {}}, 1: {0: {}, 2: {}}, 2: {3: {}, 1: {}}, 0: {1: {}}}) |
|
65
|
|
|
''' |
|
66
|
|
|
|
|
67
|
|
|
import random |
|
68
|
|
|
from networkx.classes.graph import Graph |
|
69
|
|
|
from networkx.classes.digraph import DiGraph |
|
70
|
|
|
from networkx.classes.multigraph import MultiGraph |
|
71
|
|
|
from networkx.classes.multidigraph import MultiDiGraph |
|
72
|
|
|
from networkx.convert import from_dict_of_dicts |
|
73
|
|
|
|
|
74
|
|
|
random.seed(seed) |
|
75
|
|
|
|
|
76
|
|
|
list_of_nodes = list(data_graph.nodes) |
|
77
|
|
|
for _i in range(shuffle_number): |
|
78
|
|
|
random.shuffle(list_of_nodes) |
|
79
|
|
|
new_order_data_graph = dict() |
|
80
|
|
|
for node in list_of_nodes: |
|
81
|
|
|
new_order_data_graph.update({node: data_graph[node]}) |
|
82
|
|
|
if data_graph.is_directed(): |
|
83
|
|
|
if data_graph.is_multigraph(): |
|
84
|
|
|
new_order_data_graph = from_dict_of_dicts(new_order_data_graph, create_using = MultiDiGraph, multigraph_input = True) |
|
85
|
|
|
else: |
|
86
|
|
|
new_order_data_graph = from_dict_of_dicts(new_order_data_graph, create_using = DiGraph) |
|
87
|
|
|
else: |
|
88
|
|
|
if data_graph.is_multigraph(): |
|
89
|
|
|
new_order_data_graph = from_dict_of_dicts(new_order_data_graph, create_using = MultiGraph, multigraph_input = True) |
|
90
|
|
|
else: |
|
91
|
|
|
new_order_data_graph = from_dict_of_dicts(new_order_data_graph, create_using = Graph) |
|
92
|
|
|
return new_order_data_graph |
|
93
|
|
|
|