Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DSS
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Antoine Castillon
DSS
Commits
5e0363e9
Commit
5e0363e9
authored
3 years ago
by
Antoine Castillon
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
929561e6
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
community_graph.py
+112
-0
112 additions, 0 deletions
community_graph.py
with
112 additions
and
0 deletions
community_graph.py
0 → 100644
+
112
−
0
View file @
5e0363e9
import
networkx
as
nx
import
random
as
rd
def
node_in_community
(
n
,
list_comm
):
node_in_comm
=
[[]
for
i
in
range
(
n
)]
no_comm
=
list
(
range
(
n
))
for
i_c
,
comm
in
enumerate
(
list_comm
):
for
u
in
comm
:
node_in_comm
[
u
].
append
(
i_c
)
try
:
no_comm
.
remove
(
u
)
except
:
"
pass
"
return
node_in_comm
,
no_comm
def
rd_disjoint_community
(
n
,
nb_comm
,
size_comm_min
,
size_comm_max
):
vertices
=
list
(
range
(
n
))
list_comm
=
[]
rd
.
shuffle
(
vertices
)
for
i_c
in
range
(
nb_comm
):
comm_size
=
rd
.
randrange
(
size_comm_min
,
size_comm_max
+
1
)
comm
=
vertices
[:
comm_size
]
list_comm
.
append
(
comm
)
vertices
=
vertices
[
comm_size
:]
return
list_comm
def
rd_community
(
n
,
nb_comm
,
size_comm_min
,
size_comm_max
):
prob_community_intersect
=
0.01
prob_common_node
=
0.05
list_comm
=
rd_disjoint_community
(
n
,
nb_comm
,
size_comm_min
,
size_comm_max
)
for
i_c
in
range
(
1
,
nb_comm
):
for
j_c
in
range
(
i_c
):
if
rd
.
random
()
<
prob_community_intersect
:
size_comm_ic
=
len
(
list_comm
[
i_c
])
size_comm_jc
=
len
(
list_comm
[
j_c
])
for
i
in
range
(
size_comm_ic
):
if
rd
.
random
()
<
prob_common_node
:
u
=
list_comm
[
i_c
][
i
]
list_comm
[
j_c
].
append
(
u
)
for
i
in
range
(
size_comm_jc
):
if
rd
.
random
()
<
prob_common_node
:
u
=
list_comm
[
j_c
][
i
]
list_comm
[
i_c
].
append
(
u
)
return
list_comm
def
rd_community_graph
(
n
,
nb_comm
,
size_comm_min
,
size_comm_max
,
prob_edge_comm
=
0.9
,
prob_edge_inter_comm
=
0.1
):
G
=
nx
.
Graph
()
G
.
add_nodes_from
(
range
(
n
))
list_comm
=
rd_community
(
n
,
nb_comm
,
size_comm_min
,
size_comm_max
)
node_in_comm
,
no_comm
=
node_in_community
(
n
,
list_comm
)
for
i_c
in
range
(
len
(
list_comm
)):
for
i
in
range
(
1
,
len
(
list_comm
[
i_c
])):
for
j
in
range
(
i
):
u
=
list_comm
[
i_c
][
i
]
v
=
list_comm
[
i_c
][
j
]
if
(
set
(
range
(
i_c
))
&
set
(
node_in_comm
[
u
])
&
set
(
node_in_comm
[
v
]))
==
set
():
if
rd
.
random
()
<
prob_edge_comm
:
G
.
add_edge
(
u
,
v
)
nb_edges_outside_comm
=
0
for
u
in
range
(
1
,
n
):
for
v
in
range
(
u
):
if
(
set
(
node_in_comm
[
u
])
&
set
(
node_in_comm
[
v
]))
==
set
():
if
rd
.
random
()
<
prob_edge_inter_comm
:
G
.
add_edge
(
u
,
v
)
nb_edges_outside_comm
+=
1
return
G
def
modify_seed
(
s
):
global
seed
seed
=
s
rd
.
seed
(
s
)
seed
=
10
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment