master > master: code py - fügte tarjan im Hauptzyklus hinzu
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
from __future__ import annotations;
|
||||
|
||||
from src.thirdparty.types import *;
|
||||
from src.thirdparty.maths import *;
|
||||
|
||||
from src.core.log import *;
|
||||
from src.models.stacks import *;
|
||||
@@ -40,15 +41,24 @@ def tarjan_algorithm(G: Graph, verbose: bool = False) -> List[Any]:
|
||||
Runs the Tarjan-Algorithm to compute the strongly connected components.
|
||||
'''
|
||||
# initialise state - mark all nodes as UNTOUCHED:
|
||||
ctx = Context(G, verbose=verbose);
|
||||
ctx = Context(G);
|
||||
# loop through all nodes and carry out Tarjan-Algorithm, provided node not already visitted.
|
||||
for u in G.nodes:
|
||||
if ctx.get_state(u) == State.UNTOUCHED:
|
||||
tarjan_visit(G, u, ctx);
|
||||
|
||||
if verbose:
|
||||
repr = ctx.repr();
|
||||
print('');
|
||||
print(f'\x1b[1mZusammenfassung der Ausführung des Tarjan-Algorithmus\x1b[0m');
|
||||
print('');
|
||||
print(repr);
|
||||
print('');
|
||||
print('\x1b[1mStark zshgd Komponenten:\x1b[0m')
|
||||
print('');
|
||||
for component in ctx.components:
|
||||
log_debug(component);
|
||||
print(component);
|
||||
print('');
|
||||
|
||||
return ctx.components;
|
||||
|
||||
@@ -113,13 +123,13 @@ class ContextDefault:
|
||||
max_index: int = field(default=0);
|
||||
verbose: bool = field(default=False);
|
||||
stack: Stack = field(default_factory=lambda: Stack());
|
||||
components: list[list[Any]] = field(default_factory=lambda: []);
|
||||
infos: dict[Any, NodeInformation] = field(default_factory=lambda: dict());
|
||||
components: list[list[Any]] = field(default_factory=list);
|
||||
infos: dict[Any, NodeInformation] = field(default_factory=dict);
|
||||
finished: List[Any] = field(default_factory=list);
|
||||
|
||||
class Context(ContextDefault):
|
||||
def __init__(self, G: Graph, verbose: bool):
|
||||
def __init__(self, G: Graph):
|
||||
super().__init__();
|
||||
self.verbose = verbose;
|
||||
self.infos = { u: NodeInformation(u) for u in G.nodes };
|
||||
|
||||
def push(self, u: Any):
|
||||
@@ -165,7 +175,21 @@ class Context(ContextDefault):
|
||||
return self.get_info(u).index;
|
||||
|
||||
def log_info(self, u: Any):
|
||||
if not self.verbose:
|
||||
return;
|
||||
info = self.get_info(u);
|
||||
log_debug(info);
|
||||
self.finished.append(u);
|
||||
|
||||
def repr(self) -> str:
|
||||
table = pd.DataFrame([ self.infos[u] for u in self.finished ]) \
|
||||
.drop(columns='state')
|
||||
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
||||
repr = tabulate(
|
||||
table,
|
||||
headers = {
|
||||
'Knoten': 'node',
|
||||
'kleinster Idx': 'least_index',
|
||||
'Index': 'index',
|
||||
},
|
||||
showindex = False,
|
||||
stralign = 'center',
|
||||
tablefmt = 'grid',
|
||||
);
|
||||
return repr;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
from __future__ import annotations;
|
||||
|
||||
from models.generated.commands import *;
|
||||
from src.thirdparty.types import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
@@ -28,7 +29,8 @@ class Graph(object):
|
||||
nodes: list[Any];
|
||||
edges: list[tuple[Any,Any]]
|
||||
|
||||
def __init__(self, nodes: list[Any], edges: list[tuple[Any,Any]]):
|
||||
def __init__(self, nodes: list[Any], edges: list[Tuple[Any, Any]]):
|
||||
assert all(len(edge) == 2 for edge in edges);
|
||||
self.nodes = nodes;
|
||||
self.edges = edges;
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user