Struct cnetworks::Network [−][src]
pub struct Network { /* fields omitted */ }
Expand description
Network
is the main graph-like data structure.
Implementations
Create a new network of given size and desired model with a random seed. Use
Model::None
if you want a network with no pre-established connections.
Note that this is equivalent to calling Network::with_seed()
with a random alphanumeric
seed of length 16.
Examples
Creating a network of chosen model and random link weight:
let net = Network::new(20, Model::ER { p: 0.4, whole: true }, Weight::Uniform);
assert_eq!(net.size(), &20);
println!("{:?}", net);
Creating a network with no links and establishing them “by hand”:
let mut net = Network::new(10, Model::None, Weight::Constant { c: 0.25 });
net.link(1, 2)?;
net.link(4, 7)?;
assert_eq!(net.get_link(1,2)?, Some(0.25));
assert_eq!(net.get_link(4,7)?, Some(0.25));
println!("{:?}", net);
Same as Network::new()
, but with a specified seed
for the internal random number
generator. This makes the network entirely deterministic, allowing full reproducibility
between simulations.
Examples
let seed = "Boaty McBoatface";
let net1 = Network::with_seed(10, Model::ER { p: 0.75, whole: false, }, Weight::Uniform, seed);
let net2 = Network::with_seed(10, Model::ER { p: 0.75, whole: false, }, Weight::Uniform, seed);
for (node1, node2) in net1.nodes().zip(net2.nodes()) {
assert_eq!(node1.links(), node2.links());
}
Set the model of the network to Model::Custom
variant with the specified string.
Return the network-local rng thread seed.
This is a random alphanumeric string of length 16, unless explicitly specified in
Network::with_seed()
.
Borrow the network-local rng thread from it’s RefCell
. This is effectively
calling RefCell::try_borrow_mut()
.
Return iterator containing the (index, node) pairs.
Note that this is not strictly equivalent to Network::nodes().enumerate()
, since a
node’s index does not always equal it’s position in the iterator, even though nodes
are always ordered by their index.
For example it is possible to have a network with indexes (0, 2, 3) or (100, 101, 150), but not (10, 0, 1).
Return iterator containing the node indexes.
Use Network::nodes()
to iterate over the nodes themselves and Network::iter()
to
use both indexes and values at once.
Keep in mind that nodes are always ordered by their index, but it’s possible to have a network of nodes with non-sequential indexes, such as (0,2,3) or (100, 101, 150), but not (10, 0 1).
Return iterator containing the network nodes, ordered by their index.
Return immutable reference to a given node if it exists, Err
if it does not.
Return the number of connected nodes in the network, ie. ones that have at least one link
Calculate the arithmetical average of vertex degrees (ie. number of closest neighbors) in the network.
Get the total degree of the network, ie. sum of degrees over all of the nodes.
Get the weight of the link between nodes i
and j
.
Returns None
if such link does not exist and Err
if i
and j
are the same or do not
exist.
Establish a link between nodes i
and j
using current network’s weight.
Returns the weight if the requested link already exist, None
if it does not. Returns
Err
if i
and j
are the same or do not exist.
Establish a link between nodes i and j with specified weight
.
Updates the link weight and returns the old value if it already exist, None
otherwise.
Returns Err
if i
and j
are the same or do not exist, or if the weight
is not in the range
(0, 1].
Unsafely remove a link between two nodes. Does not perform a check whether the integrity of the network is preserved.
Returns the weight of the removed connection or None
if the connection does not exist and
Err
if i
and j
are the same or do not exist.
Safely remove a link between two nodes ensuring the network does not split. Performs a BFS search to check whether the integrity of the network is preserved.
Returns the weight of removed link or None
if the link does not exist or cannot be safely
removed. Returns Err
if i and j are the same or do not exist.
Unsafely disconnect a node from the network by removing all of its links. Does not check if the network integrity is perserved.
Returns the removed links as a HashMap
of (target, weight) pairsm Err
if i
and j
are the same or do not exist.
Safely disconnect a node from the network by removing all of its links. Performs a bfs search to ensure that the other nodes can stil all reach each other, ie. the integrity of the network is perserved.
Returns removed links as HashMap
of (target, weight) pairs or None
if node
cannot be
safely disconnected.
Returns Err
if node
does not exist.
Removes ALL links in the network and sets the model
to Model::None
such that
the initial linking process can be conducted again, eg. using a different model in
combination with Network::init_*
.
Attach new nodes with specified indexes. Note that the nodes must be connected to the rest of the network manually, as no links will be automatically established.
Returns Err
if there already exists a node with any of the specified indexes. No nodes
will be attached in that case.
Set a flag with specified name and content. Updates existing flag and returns the old
contents, or None
if the flag was not set. Returns Err
if specified node does not
exist.
Remove a flag with specified name. Returns the contents or None
if flag was not set.
Returns Err
if specified node does not exist.
Get all nodes containing a given flag. Returns HashMap of (node index, flag value) pairs.
Get all nodes not containing a given flag. Returns vector of node indexes.
Clear the flag from all nodes. Returns iterator of the removed (node index, flag value) pairs.
Trait Implementations
Same as Network::new()
with the following properites:
Note that by default the network is empty. Nodes must be manually attached using
Network::attach()
.
Examples
Creating a default network and attaching nodes manually
let mut net = Network::default();
net.attach(&[1, 2])?;
net.link(1,2)?;
assert_eq!(net.get_link(1,2), Ok(Some(1.0)));
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Enables easy non-mutable access to the nodes using the indexing operator.
Auto Trait Implementations
impl !RefUnwindSafe for Network
impl UnwindSafe for Network
Blanket Implementations
Mutably borrows from an owned value. Read more