Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Dunitrust
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
nodes
rust
Dunitrust
Commits
529cdfeb
Commit
529cdfeb
authored
7 years ago
by
Éloïs
Browse files
Options
Downloads
Patches
Plain Diff
[enh]
#62
impl betweenness centrality computation with Ulrik Brandes algo
parent
94ce417a
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
wotb/lib.rs
+21
-7
21 additions, 7 deletions
wotb/lib.rs
wotb/operations/centrality.rs
+91
-0
91 additions, 0 deletions
wotb/operations/centrality.rs
wotb/operations/mod.rs
+2
-1
2 additions, 1 deletion
wotb/operations/mod.rs
with
114 additions
and
8 deletions
wotb/lib.rs
+
21
−
7
View file @
529cdfeb
...
...
@@ -27,9 +27,11 @@
//! [js-tests]: https://github.com/duniter/wotb/blob/master/wotcpp/webOfTrust.cpp
#![cfg_attr(feature
=
"strict"
,
deny(warnings))]
#![deny(missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces,
unused_qualifications)]
unused_qualifications
)]
extern
crate
bincode
;
extern
crate
byteorder
;
...
...
@@ -47,9 +49,10 @@ pub use data::{NodeId, WebOfTrust};
mod
tests
{
use
super
::
*
;
use
data
::
*
;
use
operations
::
centrality
::
*
;
use
operations
::
distance
::
*
;
use
operations
::
path
::
*
;
use
operations
::
file
::
*
;
use
operations
::
path
::
*
;
/// Test translated from https://github.com/duniter/wotb/blob/master/tests/test.js
///
...
...
@@ -59,8 +62,9 @@ mod tests {
where
W
:
WebOfTrust
+
Sync
,
{
let
path_finder
=
RustyPathFinde
r
{};
let
centralities_calculator
=
UlrikBrandesCentralityCalculato
r
{};
let
distance_calculator
=
RustyDistanceCalculator
{};
let
path_finder
=
RustyPathFinder
{};
let
mut
wot
=
W
::
new
(
3
);
// should have an initial size of 0
...
...
@@ -562,7 +566,17 @@ mod tests {
);
// Test centralities computation in g1_genesis wot
let
wot_size
=
wot3
.size
();
let
centralities
=
centralities_calculator
.betweenness_centralities
(
&
wot3
);
assert_eq!
(
centralities
.len
(),
59
);
assert_eq!
(
centralities
,
vec!
[
148
,
30
,
184
,
11
,
60
,
51
,
40
,
115
,
24
,
140
,
47
,
69
,
16
,
34
,
94
,
126
,
151
,
0
,
34
,
133
,
20
,
103
,
38
,
144
,
73
,
523
,
124
,
23
,
47
,
17
,
9
,
64
,
77
,
281
,
6
,
105
,
54
,
0
,
111
,
21
,
6
,
2
,
0
,
1
,
47
,
59
,
28
,
236
,
0
,
0
,
0
,
0
,
60
,
6
,
0
,
1
,
8
,
33
,
169
,
]
);
/*let wot_size = wot3.size();
let members_count = wot3.get_enabled().len() as u64;
assert_eq!(members_count, 59);
let oriented_couples_count: u64 = members_count * (members_count - 1);
...
...
@@ -593,6 +607,6 @@ mod tests {
for centrality in centralities {
relative_centralities.push((centrality * 100_000 / oriented_couples_count) as usize);
}
assert_eq!
(
relative_centralities
.len
(),
59
);
assert_eq!(relative_centralities.len(), 59);
*/
}
}
This diff is collapsed.
Click to expand it.
wotb/operations/centrality.rs
0 → 100644
+
91
−
0
View file @
529cdfeb
// Copyright (C) 2017-2018 The Duniter Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Provide a trait and implementations to find paths between nodes.
use
data
::
NodeId
;
use
data
::
WebOfTrust
;
use
std
::
collections
::{
HashMap
,
VecDeque
};
/// Find paths between 2 nodes of a `WebOfTrust`.
pub
trait
CentralitiesCalculator
<
T
:
WebOfTrust
>
{
/// Compute betweenness centrality of all members.
fn
betweenness_centralities
(
&
self
,
wot
:
&
T
)
->
Vec
<
u64
>
;
/// Compute stress centrality of all members.
fn
stress_centralities
(
&
self
,
wot
:
&
T
)
->
Vec
<
f64
>
;
}
/// A new "rusty-er" implementation of `WoT` path finding.
#[derive(Debug,
Clone,
Copy)]
pub
struct
UlrikBrandesCentralityCalculator
;
impl
<
T
:
WebOfTrust
>
CentralitiesCalculator
<
T
>
for
UlrikBrandesCentralityCalculator
{
fn
betweenness_centralities
(
&
self
,
wot
:
&
T
)
->
Vec
<
u64
>
{
let
wot_size
=
wot
.size
();
let
mut
centralities
=
vec!
[
0.0
;
wot_size
];
let
enabled
=
wot
.get_enabled
();
for
s
in
enabled
.clone
()
{
let
mut
stack
:
Vec
<
NodeId
>
=
Vec
::
with_capacity
(
wot_size
);
let
mut
p
:
HashMap
<
NodeId
,
Vec
<
NodeId
>>
=
HashMap
::
with_capacity
(
wot_size
);
let
mut
sigma
=
vec!
[
0.0
;
wot_size
];
let
mut
d
:
Vec
<
isize
>
=
vec!
[
-
1
;
wot_size
];
let
mut
q
:
VecDeque
<
NodeId
>
=
VecDeque
::
with_capacity
(
wot_size
);
sigma
[
s
.0
]
=
1.0
;
d
[
s
.0
]
=
0
;
q
.push_back
(
s
);
while
!
q
.is_empty
()
{
let
v
=
q
.pop_front
()
.unwrap
();
stack
.push
(
v
);
for
w
in
wot
.get_links_source
(
v
)
.expect
(
"v don't have any source !"
)
{
if
d
[
w
.0
]
<
0
{
q
.push_back
(
w
);
d
[
w
.0
]
=
d
[
v
.0
]
+
1
;
}
if
d
[
w
.0
]
==
d
[
v
.0
]
+
1
{
sigma
[
w
.0
]
=
sigma
[
w
.0
]
+
sigma
[
v
.0
];
if
p
.contains_key
(
&
w
)
{
p
.get_mut
(
&
w
)
.unwrap
()
.push
(
v
);
}
else
{
p
.insert
(
w
,
vec!
[
v
]);
}
}
}
}
let
mut
gamma
=
vec!
[
0.0
;
wot_size
];
while
!
stack
.is_empty
()
{
let
w
=
stack
.pop
()
.unwrap
();
if
p
.contains_key
(
&
w
)
{
for
v
in
p
.get
(
&
w
)
.expect
(
"Not found w in p !"
)
{
if
enabled
.contains
(
&
w
)
{
gamma
[
v
.0
]
=
gamma
[
v
.0
]
+
((
sigma
[
v
.0
]
/
sigma
[
w
.0
])
*
(
1.0
+
gamma
[
w
.0
]));
}
else
{
gamma
[
v
.0
]
=
gamma
[
v
.0
]
+
((
sigma
[
v
.0
]
/
sigma
[
w
.0
])
*
gamma
[
w
.0
]);
}
}
}
if
w
!=
s
{
centralities
[
w
.0
]
=
centralities
[
w
.0
]
+
gamma
[
w
.0
];
}
}
}
centralities
.into_iter
()
.map
(|
c
|
c
as
u64
)
.collect
()
}
fn
stress_centralities
(
&
self
,
_wot
:
&
T
)
->
Vec
<
f64
>
{
unimplemented!
()
}
}
This diff is collapsed.
Click to expand it.
wotb/operations/mod.rs
+
2
−
1
View file @
529cdfeb
...
...
@@ -15,6 +15,7 @@
//! Provide operation traits and implementations on `WebOfTrust` objects.
pub
mod
path
;
pub
mod
centrality
;
pub
mod
distance
;
pub
mod
file
;
pub
mod
path
;
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