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)
|
## Beispiele für Seminarwoche 2 (Blatt 1)
|
||||||
- name: TARJAN
|
- 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: [
|
edges: [
|
||||||
[1, 2],
|
[1, 2],
|
||||||
[1, 3],
|
[1, 3],
|
||||||
|
@ -17,8 +17,8 @@ from models.generated.config import *;
|
|||||||
from models.generated.commands import *;
|
from models.generated.commands import *;
|
||||||
from src.core.log import *;
|
from src.core.log import *;
|
||||||
from src.setup.config import *;
|
from src.setup.config import *;
|
||||||
from src.models.graphs.graph import *;
|
from src.models.graphs import *;
|
||||||
from src.algorithms.tarjan.algorithms import *;
|
from src.algorithms.tarjan import *;
|
||||||
from src.algorithms.tsp import *;
|
from src.algorithms.tsp import *;
|
||||||
from src.algorithms.hirschberg import *;
|
from src.algorithms.hirschberg import *;
|
||||||
|
|
||||||
@ -34,6 +34,14 @@ from src.algorithms.hirschberg import *;
|
|||||||
|
|
||||||
def enter():
|
def enter():
|
||||||
for command in COMMANDS:
|
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):
|
if isinstance(command, CommandTsp):
|
||||||
tsp_algorithm(
|
tsp_algorithm(
|
||||||
dist = np.asarray(command.dist, dtype=float),
|
dist = np.asarray(command.dist, dtype=float),
|
||||||
@ -45,7 +53,7 @@ def enter():
|
|||||||
X = command.word1,
|
X = command.word1,
|
||||||
Y = command.word2,
|
Y = command.word2,
|
||||||
once = command.once,
|
once = command.once,
|
||||||
verb = OPTIONS.hirschberg.verbose,
|
verbose = OPTIONS.hirschberg.verbose,
|
||||||
show = OPTIONS.hirschberg.show,
|
show = OPTIONS.hirschberg.show,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
@ -42,16 +42,7 @@ components:
|
|||||||
properties:
|
properties:
|
||||||
<<: *ref_command_properties
|
<<: *ref_command_properties
|
||||||
nodes:
|
nodes:
|
||||||
anyOf:
|
type: array
|
||||||
- type: array
|
|
||||||
items:
|
|
||||||
type: string
|
|
||||||
- type: array
|
|
||||||
items:
|
|
||||||
type: integer
|
|
||||||
- type: array
|
|
||||||
items:
|
|
||||||
type: number
|
|
||||||
edges:
|
edges:
|
||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
@ -59,11 +50,6 @@ components:
|
|||||||
type: array
|
type: array
|
||||||
minItems: 2
|
minItems: 2
|
||||||
maxItems: 2
|
maxItems: 2
|
||||||
items:
|
|
||||||
anyOf:
|
|
||||||
- type: integer
|
|
||||||
- type: number
|
|
||||||
- type: string
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# Command - Algorithm: TSP
|
# Command - Algorithm: TSP
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
from __future__ import annotations;
|
from __future__ import annotations;
|
||||||
|
|
||||||
from src.thirdparty.types import *;
|
from src.thirdparty.types import *;
|
||||||
|
from src.thirdparty.maths import *;
|
||||||
|
|
||||||
from src.core.log import *;
|
from src.core.log import *;
|
||||||
from src.models.stacks 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.
|
Runs the Tarjan-Algorithm to compute the strongly connected components.
|
||||||
'''
|
'''
|
||||||
# initialise state - mark all nodes as UNTOUCHED:
|
# 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.
|
# loop through all nodes and carry out Tarjan-Algorithm, provided node not already visitted.
|
||||||
for u in G.nodes:
|
for u in G.nodes:
|
||||||
if ctx.get_state(u) == State.UNTOUCHED:
|
if ctx.get_state(u) == State.UNTOUCHED:
|
||||||
tarjan_visit(G, u, ctx);
|
tarjan_visit(G, u, ctx);
|
||||||
|
|
||||||
if verbose:
|
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:
|
for component in ctx.components:
|
||||||
log_debug(component);
|
print(component);
|
||||||
|
print('');
|
||||||
|
|
||||||
return ctx.components;
|
return ctx.components;
|
||||||
|
|
||||||
@ -113,13 +123,13 @@ class ContextDefault:
|
|||||||
max_index: int = field(default=0);
|
max_index: int = field(default=0);
|
||||||
verbose: bool = field(default=False);
|
verbose: bool = field(default=False);
|
||||||
stack: Stack = field(default_factory=lambda: Stack());
|
stack: Stack = field(default_factory=lambda: Stack());
|
||||||
components: list[list[Any]] = field(default_factory=lambda: []);
|
components: list[list[Any]] = field(default_factory=list);
|
||||||
infos: dict[Any, NodeInformation] = field(default_factory=lambda: dict());
|
infos: dict[Any, NodeInformation] = field(default_factory=dict);
|
||||||
|
finished: List[Any] = field(default_factory=list);
|
||||||
|
|
||||||
class Context(ContextDefault):
|
class Context(ContextDefault):
|
||||||
def __init__(self, G: Graph, verbose: bool):
|
def __init__(self, G: Graph):
|
||||||
super().__init__();
|
super().__init__();
|
||||||
self.verbose = verbose;
|
|
||||||
self.infos = { u: NodeInformation(u) for u in G.nodes };
|
self.infos = { u: NodeInformation(u) for u in G.nodes };
|
||||||
|
|
||||||
def push(self, u: Any):
|
def push(self, u: Any):
|
||||||
@ -165,7 +175,21 @@ class Context(ContextDefault):
|
|||||||
return self.get_info(u).index;
|
return self.get_info(u).index;
|
||||||
|
|
||||||
def log_info(self, u: Any):
|
def log_info(self, u: Any):
|
||||||
if not self.verbose:
|
self.finished.append(u);
|
||||||
return;
|
|
||||||
info = self.get_info(u);
|
def repr(self) -> str:
|
||||||
log_debug(info);
|
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 __future__ import annotations;
|
||||||
|
|
||||||
|
from models.generated.commands import *;
|
||||||
from src.thirdparty.types import *;
|
from src.thirdparty.types import *;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -28,7 +29,8 @@ class Graph(object):
|
|||||||
nodes: list[Any];
|
nodes: list[Any];
|
||||||
edges: list[tuple[Any,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.nodes = nodes;
|
||||||
self.edges = edges;
|
self.edges = edges;
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user