master > master: code py - fügte tarjan im Hauptzyklus hinzu
This commit is contained in:
parent
3e8b3c157d
commit
c0bc69450c
@ -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],
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user