Skip to main content

basic/
basic.rs

1//! cargo run --example basic
2
3use eigentrust::{eigentrust, PreTrust, TrustEdge};
4
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let names = ["alice", "bob", "carol", "mallory"];
7
8    // who trusts whom, and how much (weights are relative per truster)
9    let local_trust = [
10        TrustEdge::new(0, 1, 3.0), // alice -> bob
11        TrustEdge::new(0, 2, 1.0), // alice -> carol
12        TrustEdge::new(1, 2, 2.0), // bob -> carol
13        TrustEdge::new(2, 0, 1.0), // carol -> alice
14        TrustEdge::new(3, 3, 9.0), // mallory only vouches for herself
15    ];
16    // alice is trusted from the start
17    let pre_trust = [PreTrust::new(0, 1.0)];
18
19    let result = eigentrust(local_trust, pre_trust)?;
20
21    for (peer, score) in result.ranking() {
22        println!("{:<8} {:.4}", names[peer], score);
23    }
24    println!("converged after {} iterations", result.iterations());
25    Ok(())
26}