fera_graph/algs/
distances.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use fun::max_prop;
6use prelude::*;
7use props::{Color, FnProp};
8use traverse::*;
9
10pub trait Distances: Incidence {
11    fn diameter(&self) -> usize
12    where
13        Self: VertexList + WithVertexProp<usize> + WithVertexProp<Color>,
14    {
15        let mut dist = self.default_vertex_prop(0);
16        self.vertices()
17            .map(|v| {
18                dist.set_values(self.vertices(), usize::max_value());
19                self.dfs(RecordDistance(&mut dist)).root(v).run();
20                max_prop(FnProp(|x| dist[x]), self.vertices()).unwrap()
21            })
22            .max()
23            .unwrap_or(0)
24    }
25}
26
27impl<G: Incidence> Distances for G {}