-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount_cycles_sample.py
65 lines (48 loc) · 1.86 KB
/
count_cycles_sample.py
1
2
3
4
5
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
# Cuenta cuantos ciclos no dirigidos hay para cada orden y cuantos ciclos
# dirigidos que correspondan con los no dirigidos anteriores.
# Esto es útil cuando sólo tenemos una muestra del conjunto total de
# ciclos.
import numpy as np
import networkx as nx
import collections
from lib import *
# Parameters
# ========================================
max_order = 10
# Function definitions
# ========================================
def rotate(l, shift):
return l[shift:] + l[:shift]
# Load data
undirected_cycles = loadcycles("sevaseviene_undirectedcycles.dat")
directed_cycles = loadcycles("sevaseviene_directedcycles.dat")
#
n_undirected = len(undirected_cycles)
# Array to store the number of undirected cycles found of each order
nk_undirected = np.zeros(max_order + 1, dtype=int)
nk_directed = np.zeros(max_order + 1, dtype=int)
# For each undirected cycle find the corresponding directed cycle
for j_cycle, undirected_cycle in enumerate(undirected_cycles):
print("{0}/{1}".format(j_cycle, n_undirected))
cycle_order = len(undirected_cycle)
nk_undirected[cycle_order] += 1
found = False
for directed_cycle in directed_cycles:
# Check if it is a candidate
candidate = (
(len(directed_cycle) == len(undirected_cycle)) and
(collections.Counter(directed_cycle)
== collections.Counter(undirected_cycle)))
if not candidate:
continue
for shift in range(cycle_order):
found = (undirected_cycle == rotate(directed_cycle, shift))
if found:
nk_directed[cycle_order] += 1
break
if found:
break
# Save data to file
data = np.vstack(
(np.arange(max_order + 1, dtype=int), nk_undirected, nk_directed))
np.savetxt("cycledistribution_sevaseviene.dat", data, fmt="%6d")