I need to create a network, which is a bipartite graph, and the degree of the vertices obeys power statistics. And then check if its true with
print(bipartite.is_bipartite(G))
In the bipartite.projected_graph() function, I substitute a random graph with a power-law distribution of degrees and a list of vertices. In this case, I do not get bipartite.
And if I insert a bipartite graph into this function and a data set obeying a power-law distribution, it always writes an error.
It's not entirely clear to me how a non-bipartite graph can be obtained using the bipartite.projected_graph() function.
I also tried to make a power-law distribution for vertices, but its randomly gives me True or False if I check if Grapgh is_bipartite.
from networkx.algorithms import bipartite
from networkx.generators.degree_seq import random_degree_sequence_graph
from networkx.algorithms.graphical import is_graphical
from networkx.utils.random_sequence import powerlaw_sequence
n, t = 10, 2
while True: # Continue generating sequences until one of them is graphical
seq = sorted([int(round(d)) for d in powerlaw_sequence(n, t)], reverse=True) # Round to nearest integer to obtain DISCRETE degree sequence
if is_graphical(seq):
break
G_1 = random_degree_sequence_graph(seq, tries=100) # Adjust number of tries as you see fit
print(sorted(d for _, d in G_1.degree()))
print(bipartite.is_bipartite(G_1))
degrees = dict(G_1.degree())
degree_distribution = {}
for d in degrees.values():
if d in degree_distribution:
degree_distribution[d] += 1
else:
degree_distribution[d] = 1
print(degree_distribution)
But also I tried make a hist of degree distribution
degrees = dict(G_1.degree())
degree_distribution = {}
for d in degrees.values():
if d in degree_distribution:
degree_distribution[d] += 1
else:
degree_distribution[d] = 1
x = np.array(list(degree_distribution.keys()))
y = np.array(list(degree_distribution.values()))
plt.loglog(x, y, 'ro')
plt.title('Степенное распределение')
plt.xlabel('Степень вершины')
plt.ylabel('Количество вершин')
plt.show()
Moreover, I tried this code
import networkx as nx
import random
n = 100 # общее количество узлов
m = 4 # количество связей для новых узлов
p = 0.5 # вероятность присоединения к первой доле
G = nx.Graph()
G.add_nodes_from(range(n), bipartite=0) # первая доля
G.add_nodes_from(range(n, 2*n), bipartite=1) # вторая доля
for i in range(n):
for j in range(n, 2*n):
if random.random() < p:
G.add_edge(i, j)
for i in range(2*n, n+m):
targets = []
for j in range(n):
targets += [j] * G.degree(j)
target = random.choice(targets)
G.add_edge(i, target)
targets = []
for j in range(n, 2*n):
targets += [j] * G.degree(j)
target = random.choice(targets)
G.add_edge(i, target)
print(nx.bipartite.is_bipartite(G))
degrees = dict(G.degree())
degree_distribution = {}
for d in degrees.values():
if d in degree_distribution:
degree_distribution[d] += 1
else:
degree_distribution[d] = 1
for d, count in degree_distribution.items():
print(f"Degree {d}: {count} nodes")
And I got this
True
Degree 50: 18 nodes
Degree 54: 5 nodes
Degree 51: 20 nodes
Degree 46: 14 nodes
Degree 49: 17 nodes
Degree 56: 7 nodes
Degree 45: 14 nodes
Degree 40: 4 nodes
Degree 44: 9 nodes
Degree 48: 16 nodes
Degree 53: 11 nodes
Degree 47: 16 nodes
Degree 52: 11 nodes
Degree 61: 4 nodes
Degree 43: 7 nodes
Degree 57: 4 nodes
Degree 55: 9 nodes
Degree 59: 1 nodes
Degree 41: 4 nodes
Degree 42: 3 nodes
Degree 39: 3 nodes
Degree 58: 1 nodes
Degree 38: 1 nodes
Degree 62: 1 nodes