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

View File

@ -6,7 +6,7 @@ use std::fmt::Display;
use std::hash::Hash; use std::hash::Hash;
use std::collections::HashMap; use std::collections::HashMap;
use crate::core::utils; use crate::value_min;
use crate::core::log::log_debug; use crate::core::log::log_debug;
use crate::stacks::stack::Stack; use crate::stacks::stack::Stack;
use crate::graphs::graph::Graph; 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>) fn tarjan_visit<T>(gph: &Graph<T>, v: &T, ctx: &mut Context<T>)
where T: Eq + Hash + Clone + Copy + Display where T: Eq + Hash + Clone + Copy + Display
{ {
if !(ctx.get_state(v) == State::UNTOUCHED) { // do nothing, if v already visited
return; 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.max_index += 1;
ctx.push(v); ctx.push(v);
ctx.set_root(v, ctx.max_index); 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: // depth first search:
for u in gph.successors(&v) { for u in gph.successors(&v) {
tarjan_visit(gph, &u, ctx); tarjan_visit(gph, &u, ctx);
// remains relevant for v, provided u still in Stack: /****
if ctx.stack.elements.contains(&u) { * u is in the same component as v, if and only if u is still on the stack.
let root = utils::min( * In this case, update the associated component-index of v:
ctx.get_root(&u), ****/
ctx.get_root(v) if ctx.stack.contains(&u) {
); ctx.set_root(v, value_min!(ctx.get_root(v), ctx.get_root(&u)));
ctx.set_root(v, root);
} }
} }
ctx.set_state(v, State::FINISHED); ctx.set_state(v, State::FINISHED);
@ -169,8 +173,8 @@ impl<T> Context<T>
} }
fn log_info(self: &Self, u: &T) { fn log_info(self: &Self, u: &T) {
if !self.debug { return; }
let info = self.get_info(&u); let info = self.get_info(&u);
if !self.debug { return; }
log_debug!("node, index, component: ({}, {}, {})", info.node, info.index, info.root); log_debug!("node, index, component: ({}, {}, {})", info.node, info.index, info.root);
} }
} }