File: simple_paths.rs

package info (click to toggle)
rust-petgraph 0.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,820 kB
  • sloc: makefile: 2
file content (148 lines) | stat: -rw-r--r-- 4,323 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![feature(test)]

extern crate petgraph;
extern crate test;

mod common;

use common::ungraph;
use hashbrown::HashSet;
use petgraph::algo::{all_simple_paths, all_simple_paths_multi};
use petgraph::prelude::*;
use test::Bencher;

#[bench]
fn complete_graph_single_bench(bench: &mut Bencher) {
    static NODE_COUNT: usize = 6;
    let mut g = Graph::new_undirected();
    let nodes: Vec<NodeIndex> = (0..NODE_COUNT).map(|_| g.add_node(())).collect();

    // Create a complete undirected graph - every pair of nodes has an edge
    for i in 0..NODE_COUNT {
        for j in i + 1..NODE_COUNT {
            g.add_edge(nodes[i], nodes[j], ());
        }
    }

    let from = nodes[0];
    let targets: Vec<_> = (1..NODE_COUNT).map(|i| nodes[i]).collect();

    bench.iter(|| {
        let mut total_paths = 0;
        for &to in &targets {
            let paths = all_simple_paths::<Vec<_>, _, fxhash::FxBuildHasher>(&g, from, to, 0, None);
            total_paths += paths.count();
        }
        total_paths
    });
}

#[bench]
fn complete_graph_multi_bench(bench: &mut Bencher) {
    static NODE_COUNT: usize = 6;
    let mut g = Graph::new_undirected();
    let nodes: Vec<NodeIndex> = (0..NODE_COUNT).map(|_| g.add_node(())).collect();

    // Create a complete undirected graph - every pair of nodes has an edge
    for i in 0..NODE_COUNT {
        for j in i + 1..NODE_COUNT {
            g.add_edge(nodes[i], nodes[j], ());
        }
    }

    let from = nodes[0];
    let to: HashSet<_, fxhash::FxBuildHasher> = (1..NODE_COUNT).map(|i| nodes[i]).collect();

    bench.iter(|| {
        let paths =
            all_simple_paths_multi::<Vec<_>, _, fxhash::FxBuildHasher>(&g, from, &to, 0, None);
        paths.count()
    });
}

#[bench]
fn petersen_single_bench(bench: &mut Bencher) {
    let g = ungraph().petersen_a();
    let from = NodeIndex::new(0);
    let targets: Vec<_> = (1..g.node_count()).map(NodeIndex::new).collect();

    bench.iter(|| {
        let mut total_paths = 0;
        for &to in &targets {
            let paths = all_simple_paths::<Vec<_>, _, fxhash::FxBuildHasher>(&g, from, to, 0, None);
            total_paths += paths.count();
        }
        total_paths
    });
}

#[bench]
fn petersen_multi_bench(bench: &mut Bencher) {
    let g = ungraph().petersen_a();
    let from = NodeIndex::new(0);
    let to: HashSet<_, fxhash::FxBuildHasher> = (1..g.node_count()).map(NodeIndex::new).collect();

    bench.iter(|| {
        let paths =
            all_simple_paths_multi::<Vec<_>, _, fxhash::FxBuildHasher>(&g, from, &to, 0, None);
        paths.count()
    });
}

#[bench]
fn bipartite_single_bench(bench: &mut Bencher) {
    let g = ungraph().bipartite();
    let from = NodeIndex::new(0);
    let targets: Vec<_> = (1..g.node_count()).map(NodeIndex::new).collect();

    bench.iter(|| {
        let mut total_paths = 0;
        for &to in &targets {
            let paths = all_simple_paths::<Vec<_>, _, fxhash::FxBuildHasher>(&g, from, to, 0, None);
            total_paths += paths.count();
        }
        total_paths
    });
}

#[bench]
fn bipartite_multi_bench(bench: &mut Bencher) {
    let g = ungraph().bipartite();
    let from = NodeIndex::new(0);
    let to: HashSet<_, fxhash::FxBuildHasher> = (1..g.node_count()).map(NodeIndex::new).collect();

    bench.iter(|| {
        let paths =
            all_simple_paths_multi::<Vec<_>, _, fxhash::FxBuildHasher>(&g, from, &to, 0, None);
        paths.count()
    });
}

#[bench]
fn full_single_bench(bench: &mut Bencher) {
    let g = ungraph().full_a();
    let from = NodeIndex::new(0);
    let targets: Vec<_> = (1..g.node_count()).map(NodeIndex::new).collect();

    bench.iter(|| {
        let mut total_paths = 0;
        for &to in &targets {
            let paths = all_simple_paths::<Vec<_>, _, fxhash::FxBuildHasher>(&g, from, to, 0, None);
            total_paths += paths.count();
        }
        total_paths
    });
}

#[bench]
fn full_multi_bench(bench: &mut Bencher) {
    let g = ungraph().full_a();
    let from = NodeIndex::new(0);
    let to: HashSet<_, fxhash::FxBuildHasher> = (1..g.node_count()).map(NodeIndex::new).collect();

    bench.iter(|| {
        let paths =
            all_simple_paths_multi::<Vec<_>, _, fxhash::FxBuildHasher>(&g, from, &to, 0, None);
        paths.count()
    });
}