From c0bc69450ce51b45c738ddc9acc8783d520539b6 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Fri, 10 Jun 2022 16:05:15 +0200 Subject: [PATCH] =?UTF-8?q?master=20>=20master:=20code=20py=20-=20f=C3=BCg?= =?UTF-8?q?te=20tarjan=20im=20Hauptzyklus=20hinzu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/python/assets/commands.yaml | 5 ++- code/python/main.py | 14 ++++-- code/python/models/commands-schema.yaml | 16 +------ .../src/algorithms/tarjan/algorithms.py | 44 ++++++++++++++----- code/python/src/models/graphs/graph.py | 4 +- 5 files changed, 53 insertions(+), 30 deletions(-) diff --git a/code/python/assets/commands.yaml b/code/python/assets/commands.yaml index a098eaf..589512f 100644 --- a/code/python/assets/commands.yaml +++ b/code/python/assets/commands.yaml @@ -1,6 +1,9 @@ ## Beispiele für Seminarwoche 2 (Blatt 1) - name: TARJAN - nodes: [1,2,3,4,5,6,7,8] + nodes: [a,b,c] + edges: [[a, c], [c, a], [b, c]] +- name: TARJAN + nodes: [1, 2, 3, 4, 5, 6, 7, 8] edges: [ [1, 2], [1, 3], diff --git a/code/python/main.py b/code/python/main.py index e7a4dac..f606716 100644 --- a/code/python/main.py +++ b/code/python/main.py @@ -17,8 +17,8 @@ from models.generated.config import *; from models.generated.commands import *; from src.core.log import *; from src.setup.config import *; -from src.models.graphs.graph import *; -from src.algorithms.tarjan.algorithms import *; +from src.models.graphs import *; +from src.algorithms.tarjan import *; from src.algorithms.tsp import *; from src.algorithms.hirschberg import *; @@ -34,6 +34,14 @@ from src.algorithms.hirschberg import *; def enter(): for command in COMMANDS: + if isinstance(command, CommandTarjan): + tarjan_algorithm( + G = Graph( + nodes=command.nodes, + edges=list(map(tuple, command.edges)), + ), + verbose = OPTIONS.tarjan.verbose + ); if isinstance(command, CommandTsp): tsp_algorithm( dist = np.asarray(command.dist, dtype=float), @@ -45,7 +53,7 @@ def enter(): X = command.word1, Y = command.word2, once = command.once, - verb = OPTIONS.hirschberg.verbose, + verbose = OPTIONS.hirschberg.verbose, show = OPTIONS.hirschberg.show, ); return; diff --git a/code/python/models/commands-schema.yaml b/code/python/models/commands-schema.yaml index cc27a50..097c3a0 100644 --- a/code/python/models/commands-schema.yaml +++ b/code/python/models/commands-schema.yaml @@ -42,16 +42,7 @@ components: properties: <<: *ref_command_properties nodes: - anyOf: - - type: array - items: - type: string - - type: array - items: - type: integer - - type: array - items: - type: number + type: array edges: type: array items: @@ -59,11 +50,6 @@ components: type: array minItems: 2 maxItems: 2 - items: - anyOf: - - type: integer - - type: number - - type: string # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Command - Algorithm: TSP # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/code/python/src/algorithms/tarjan/algorithms.py b/code/python/src/algorithms/tarjan/algorithms.py index a29233b..c644334 100644 --- a/code/python/src/algorithms/tarjan/algorithms.py +++ b/code/python/src/algorithms/tarjan/algorithms.py @@ -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; diff --git a/code/python/src/models/graphs/graph.py b/code/python/src/models/graphs/graph.py index 530d7f1..79f1692 100644 --- a/code/python/src/models/graphs/graph.py +++ b/code/python/src/models/graphs/graph.py @@ -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;