Struct NegCycleFinder

Source
pub struct NegCycleFinder<'a, V, D> {
    pub digraph: &'a DiGraph<V, D>,
    pub pred: HashMap<NodeIndex, (NodeIndex, EdgeReference<'a, D>)>,
}
Expand description

The NegCycleFinder struct is used to find negative cycles in a directed graph.

Properties:

  • digraph: The digraph property is a reference to a directed graph (DiGraph) that the NegCycleFinder is operating on. It is annotated with a lifetime 'a, indicating that the reference is valid for a certain scope.
  • pred: The pred property is a HashMap that maps a NodeIndex to a tuple containing the previous node index and an EdgeReference. This is used to keep track of the predecessor node and the edge that leads to that node during the process of finding negative cycles in a directed graph

Fields§

§digraph: &'a DiGraph<V, D>§pred: HashMap<NodeIndex, (NodeIndex, EdgeReference<'a, D>)>

Implementations§

Source§

impl<'a, V, D> NegCycleFinder<'a, V, D>
where D: Add<Output = D> + PartialOrd + Copy,

Source

pub fn new(digraph: &'a DiGraph<V, D>) -> Self

The new function creates a new NegCycleFinder object with an empty predecessor map.

Arguments:

  • digraph: A reference to a directed graph (DiGraph) that the NegCycleFinder will operate on.

Returns:

The new function is returning an instance of the NegCycleFinder<V, D> struct. Creates a new NegCycleFinder<V, D>.

Source

pub fn find_cycle(&self) -> Option<NodeIndex>

The find_cycle function in Rust returns the first node in a cycle found in a directed graph.

Returns:

The function find_cycle returns an Option<NodeIndex>.

Source

pub fn relax<F>(&mut self, dist: &mut [D], get_weight: F) -> bool
where F: Fn(EdgeReference<'_, D>) -> D,

The relax function updates the distances between nodes in a graph based on the weights of the edges, and returns a boolean indicating whether any distances were changed.

Arguments:

  • dist: dist is a mutable reference to a slice of type D. It represents the distances from a source node to each node in a graph.
  • get_weight: The get_weight parameter is a closure that takes an EdgeReference<D> as input and returns a value of type D. This closure is used to calculate the weight of each edge in the graph. The EdgeReference<D> represents a reference to an edge in the graph, and

Returns:

a boolean value.

Source

pub fn howard<F>( &mut self, dist: &mut [D], get_weight: F, ) -> Option<Vec<EdgeReference<'a, D>>>
where F: Fn(EdgeReference<'_, D>) -> D,

The howard function implements Howard’s algorithm for finding negative cycles in a directed graph.

Arguments:

  • dist: dist is a mutable reference to an array of type D. This array is used to store the distances from the source vertex to each vertex in the graph. The algorithm will update the distances during the execution.
  • get_weight: get_weight is a closure that takes an EdgeReference<D> and returns the weight of that edge. The howard function uses this closure to get the weight of each edge in the graph.

Returns:

The howard function returns an Option<Vec<EdgeReference<'a, D>>>. Howard’s algorithm for finding negative cycles

§Examples
use petgraph::prelude::*;
use netoptim_rs::neg_cycle::NegCycleFinder;
let digraph = DiGraph::<(), i32>::from_edges([
    (0, 1, 1),
    (0, 2, 1),
    (0, 3, 1),
    (1, 3, 1),
    (2, 1, 1),
    (3, 2, -3),
]);
let mut ncf = NegCycleFinder::new(&digraph);
let mut dist = [0, 0, 0, 0];
let result = ncf.howard(&mut dist, |e| { *e.weight()});
assert!(result.is_some());

Trait Implementations§

Source§

impl<'a, V: Clone, D: Clone> Clone for NegCycleFinder<'a, V, D>

Source§

fn clone(&self) -> NegCycleFinder<'a, V, D>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, V: Debug, D: Debug> Debug for NegCycleFinder<'a, V, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, V, D> Freeze for NegCycleFinder<'a, V, D>

§

impl<'a, V, D> RefUnwindSafe for NegCycleFinder<'a, V, D>

§

impl<'a, V, D> Send for NegCycleFinder<'a, V, D>
where V: Sync, D: Sync,

§

impl<'a, V, D> Sync for NegCycleFinder<'a, V, D>
where V: Sync, D: Sync,

§

impl<'a, V, D> Unpin for NegCycleFinder<'a, V, D>

§

impl<'a, V, D> UnwindSafe for NegCycleFinder<'a, V, D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.