master > master: code-rust - schönheits op für algorithmus

This commit is contained in:
RD 2022-04-14 11:57:41 +02:00
parent 5ce805c543
commit b30de02973
1 changed files with 16 additions and 12 deletions

View File

@ -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<T>(gph: &Graph<T>, debug: bool) -> Vec<Vec<T>>
fn tarjan_visit<T>(gph: &Graph<T>, v: &T, ctx: &mut Context<T>)
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<T>(gph: &Graph<T>, v: &T, ctx: &mut Context<T>)
// 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<T> Context<T>
}
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);
}
}