In order to get a head start on these requirements, the decision for a graph representation has to be made: list or matrix? Both work well, but for explanatory reasons, the examples will go with an adjacency list built on top of a vector of vectors:
pub struct InternetOfThings {
adjacency_list: Vec<Vec<Edge>>,
nodes: Vec<KeyType>,
}
As previously mentioned, it makes sense to keep the actual values, identifiers, or even entire objects in their own list and simply work with indices of the usize type. The edge structure in this example could be represented as a tuple just as well, but it's way more readable this way:
#[derive(Clone, Debug)]
struct Edge {
weight: u32,
node: usize,
}
Having those two structures in place, adding nodes (or... things) to the graph can be done with only a few lines:
fn get_node_index(&self, node: KeyType) -> Option<usize> {
self.nodes.iter().position(|n| n == &node)
}
pub fn set_edges...