diff --git a/code/rust/src/graphs/tarjan.rs b/code/rust/src/graphs/tarjan.rs index 06b5c01..fcc7aea 100644 --- a/code/rust/src/graphs/tarjan.rs +++ b/code/rust/src/graphs/tarjan.rs @@ -6,7 +6,7 @@ use std::fmt::Display; use std::hash::Hash; use std::collections::HashMap; -use crate::core::utils; +use crate::value_min; use crate::core::log::log_debug; use crate::stacks::stack::Stack; use crate::graphs::graph::Graph; @@ -44,10 +44,15 @@ pub fn tarjan_algorithm(gph: &Graph, debug: bool) -> Vec> fn tarjan_visit(gph: &Graph, v: &T, ctx: &mut Context) where T: Eq + Hash + Clone + Copy + Display { - if !(ctx.get_state(v) == State::UNTOUCHED) { - return; - } + // do nothing, if v already visited + if !(ctx.get_state(v) == State::UNTOUCHED) { return; } + /**************** + * First visit of v. + * - Place v on stack. + * - Initialise visit-index + component-index. + * - After each rekursiv call, only those all + ****************/ ctx.max_index += 1; ctx.push(v); ctx.set_root(v, ctx.max_index); @@ -57,13 +62,12 @@ fn tarjan_visit(gph: &Graph, v: &T, ctx: &mut Context) // depth first search: for u in gph.successors(&v) { tarjan_visit(gph, &u, ctx); - // remains relevant for v, provided u still in Stack: - if ctx.stack.elements.contains(&u) { - let root = utils::min( - ctx.get_root(&u), - ctx.get_root(v) - ); - ctx.set_root(v, root); + /**** + * u is in the same component as v, if and only if u is still on the stack. + * In this case, update the associated component-index of v: + ****/ + if ctx.stack.contains(&u) { + ctx.set_root(v, value_min!(ctx.get_root(v), ctx.get_root(&u))); } } ctx.set_state(v, State::FINISHED); @@ -169,8 +173,8 @@ impl Context } fn log_info(self: &Self, u: &T) { - if !self.debug { return; } let info = self.get_info(&u); + if !self.debug { return; } log_debug!("node, index, component: ({}, {}, {})", info.node, info.index, info.root); } }