Compare commits
7 Commits
d5a79e77ce
...
d474b93c10
Author | SHA1 | Date | |
---|---|---|---|
d474b93c10 | |||
34c7d9d0c4 | |||
04cdeb4e18 | |||
2ecf52fe3d | |||
a3538095ba | |||
5be654e5db | |||
dc831a91c7 |
1
code/python/.gitignore
vendored
1
code/python/.gitignore
vendored
@ -10,6 +10,7 @@
|
||||
!/README.md
|
||||
!/LICENSE
|
||||
!/requirements
|
||||
!/pyproject.toml
|
||||
|
||||
################################################################
|
||||
# PROJECT FILES
|
||||
|
@ -21,12 +21,12 @@ endif
|
||||
# Macros
|
||||
################################
|
||||
|
||||
define create_folder_if_not_exists
|
||||
@if ! [ -d "$(1)" ]; then mkdir "$(1)"; fi
|
||||
define create_file_if_not_exists
|
||||
@touch "$(1)";
|
||||
endef
|
||||
|
||||
define create_folder_if_not_exists
|
||||
@touch "$(1)";
|
||||
if ! [ -d "$(1)" ]; then mkdir "$(1)"; fi
|
||||
endef
|
||||
|
||||
define delete_if_file_exists
|
||||
@ -63,13 +63,15 @@ all: setup run
|
||||
################################
|
||||
# TARGETS: testing
|
||||
################################
|
||||
unit-test: unit-tests
|
||||
tests: unit-tests
|
||||
unit-tests:
|
||||
@cd tests && \
|
||||
${PYTHON} -m unittest discover -v \
|
||||
--start-directory "." \
|
||||
--top-level-directory ".." \
|
||||
--pattern "test_*.py"
|
||||
@# For logging purposes (since stdout is rechanneled):
|
||||
@$(call delete_if_file_exists,logs/debug.log)
|
||||
@$(call create_folder_if_not_exists,logs)
|
||||
@$(call create_file_if_not_exists,logs/debug.log)
|
||||
@# for python unit tests:
|
||||
@${PYTHON} -m pytest tests --cache-clear --verbose -k test_
|
||||
@cat logs/debug.log
|
||||
################################
|
||||
# AUXILIARY (INTERNAL TARGETS)
|
||||
################################
|
||||
|
8
code/python/pyproject.toml
Normal file
8
code/python/pyproject.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[tool.pytest.ini_options]
|
||||
minversion = "7.1.1"
|
||||
testpaths = [
|
||||
"tests",
|
||||
]
|
||||
python_files = [
|
||||
"**/test_*.py",
|
||||
]
|
@ -1,4 +1,6 @@
|
||||
pip>=22.0.4
|
||||
pytest>=7.1.1
|
||||
pytest-lazy-fixture>=0.6.3
|
||||
typing>=3.7.4.3
|
||||
numpy>=1.22.3
|
||||
pandas>=1.4.1
|
||||
|
0
code/python/src/core/__init__.py
Normal file
0
code/python/src/core/__init__.py
Normal file
64
code/python/src/core/log.py
Normal file
64
code/python/src/core/log.py
Normal file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
__all__ = [
|
||||
'log_info',
|
||||
'log_debug',
|
||||
'log_warn',
|
||||
'log_error',
|
||||
'log_fatal',
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHODS logging
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def log_info(*text: str):
|
||||
'''
|
||||
Prints an info message
|
||||
'''
|
||||
for line in text:
|
||||
print("[\x1b[94;1mINFO\x1b[0m] {}".format(line));
|
||||
return;
|
||||
|
||||
def log_debug(*text: str):
|
||||
'''
|
||||
Prints a debug message
|
||||
'''
|
||||
for line in text:
|
||||
print("[\x1b[96;1mDEBUG\x1b[95;0m] {}".format(line));
|
||||
return;
|
||||
|
||||
def log_warn(*text: str):
|
||||
'''
|
||||
Prints a warning message
|
||||
'''
|
||||
for line in text:
|
||||
print("[\x1b[93;1mWARNING\x1b[0m] {}".format(line));
|
||||
return;
|
||||
|
||||
def log_error(*text: str):
|
||||
'''
|
||||
Prints an error message
|
||||
'''
|
||||
for line in text:
|
||||
print("[\x1b[91;1mERROR\x1b[0m] {}".format(line));
|
||||
return;
|
||||
|
||||
def log_fatal(*text: str):
|
||||
'''
|
||||
Prints a fatal error message + crashes
|
||||
'''
|
||||
for line in text:
|
||||
print("[\x1b[91;1mFATAL\x1b[0m] {}".format(line));
|
||||
exit(1);
|
@ -5,7 +5,9 @@
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
from __future__ import annotations;
|
||||
|
||||
from src.local.typing import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
@ -23,22 +25,34 @@ class Graph(object):
|
||||
'''
|
||||
a data structure for graphs
|
||||
'''
|
||||
nodes: list[str];
|
||||
edges: list[tuple[str,str]]
|
||||
nodes: list[Any];
|
||||
edges: list[tuple[Any,Any]]
|
||||
|
||||
def __init__(self, nodes: list[str], edges: list[tuple[str,str]]):
|
||||
def __init__(self, nodes: list[Any], edges: list[tuple[Any,Any]]):
|
||||
self.nodes = nodes;
|
||||
self.edges = edges;
|
||||
return;
|
||||
|
||||
def successor(self, u: str):
|
||||
def __len__(self) -> int:
|
||||
return len(self.nodes);
|
||||
|
||||
def subgraph(self, nodes: list[Any]) -> Graph:
|
||||
'''
|
||||
@returns graph induced by subset of nodes
|
||||
'''
|
||||
return Graph(
|
||||
nodes = [ u for u in self.nodes if u in nodes ],
|
||||
edges = [ (u, v) for u, v in self.edges if u in nodes and v in nodes ],
|
||||
);
|
||||
|
||||
def successors(self, u: str):
|
||||
'''
|
||||
@returns
|
||||
list of successor nodes
|
||||
'''
|
||||
return [ v for (u_, v) in self.edges if u == u_ ];
|
||||
|
||||
def predecessor(self, v: str):
|
||||
def predecessors(self, v: str):
|
||||
'''
|
||||
@returns
|
||||
list of predecessor nodes
|
||||
|
@ -5,12 +5,17 @@
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from __future__ import annotations;
|
||||
from enum import Enum;
|
||||
from dataclasses import dataclass;
|
||||
from dataclasses import field
|
||||
from platform import node;
|
||||
|
||||
from src.local.typing import *;
|
||||
|
||||
from src.core.log import *;
|
||||
from src.stacks.stack import *;
|
||||
from src.graphs.graph import *
|
||||
from src.graphs.graph import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# EXPORTS
|
||||
@ -33,59 +38,134 @@ class State(Enum):
|
||||
# Tarjan Algorithm
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
class NodeInformation:
|
||||
root: int;
|
||||
index: int;
|
||||
state: State;
|
||||
|
||||
def __init__(self):
|
||||
self.root = self.index = 0;
|
||||
self.state = State.UNTOUCHED;
|
||||
|
||||
def tarjan_algorithm(G: Graph) -> List[Any]:
|
||||
def tarjan_algorithm(G: Graph, debug: bool = False) -> List[Any]:
|
||||
'''
|
||||
runs the Tarjan-Algorithm to compute the strongly connected components
|
||||
# Tarjan Algorithm #
|
||||
Runs the Tarjan-Algorithm to compute the strongly connected components.
|
||||
'''
|
||||
# initialise state - mark all nodes as UNTOUCHED:
|
||||
ctx = Context(G, debug=debug);
|
||||
# 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);
|
||||
|
||||
return ctx.components;
|
||||
|
||||
def tarjan_visit(G: Graph, u: Any, ctx: Context):
|
||||
'''
|
||||
Recursive depth-first search algorithm to compute strongly components of a graph.
|
||||
'''
|
||||
|
||||
# sonst Stack erstellen und Bearbeitungszustand der Knoten im Speicher notieren:
|
||||
S = Stack();
|
||||
index = 0;
|
||||
infos = { u: NodeInformation() for u in G.nodes };
|
||||
components = [];
|
||||
# Place node on stack + initialise visit-index + component-index.
|
||||
ctx.max_index += 1;
|
||||
ctx.push(u);
|
||||
ctx.set_least_index(u, ctx.max_index);
|
||||
ctx.set_index(u, ctx.max_index);
|
||||
ctx.set_state(u, State.PENDING);
|
||||
|
||||
def tarjan_visit(v: Any):
|
||||
'''
|
||||
recursive depth-first search algorithm to compute components
|
||||
Compute strongly connected components of each child node.
|
||||
NOTE: Child nodes remain on stack, if and only if parent is in same component.
|
||||
'''
|
||||
nonlocal G, S, index, components;
|
||||
for v in G.successors(u):
|
||||
# Visit child node only if untouched:
|
||||
if ctx.get_state(v) == State.UNTOUCHED:
|
||||
tarjan_visit(G, v, ctx);
|
||||
ctx.set_least_index(u, min(ctx.get_least_index(u), ctx.get_least_index(v)));
|
||||
# Otherwise update associated component-index of parent node, if in same component as child:
|
||||
elif ctx.stack_contains(v):
|
||||
ctx.set_least_index(u, min(ctx.get_least_index(u), ctx.get_index(v)));
|
||||
|
||||
if not(infos[v].state == State.UNTOUCHED):
|
||||
return;
|
||||
ctx.set_state(u, State.FINISHED);
|
||||
ctx.log_info(u);
|
||||
|
||||
index += 1;
|
||||
infos[v].index = infos[v].root = index;
|
||||
S.push(v);
|
||||
|
||||
infos[v].state = State.PENDING;
|
||||
# depth first search:
|
||||
for u in G.successor(v):
|
||||
tarjan_visit(u);
|
||||
# remains relevant for v, provided u still in Stack:
|
||||
if u in S:
|
||||
infos[v].root = min(infos[v].root, infos[u].root);
|
||||
infos[v].state = State.FINISHED;
|
||||
|
||||
# if at root of component pop everything from stack up to root and add component:
|
||||
if infos[v].index == infos[v].root:
|
||||
# If at least-index of component pop everything from stack up to least index and add component:
|
||||
if ctx.get_index(u) == ctx.get_least_index(u):
|
||||
component = [];
|
||||
condition_reached_root = False;
|
||||
while not condition_reached_root:
|
||||
u = S.pop();
|
||||
component.append(u);
|
||||
condition_reached_root = (u == v);
|
||||
components.append(component);
|
||||
while True:
|
||||
v = ctx.top();
|
||||
ctx.pop();
|
||||
component.append(v);
|
||||
if u == v:
|
||||
break;
|
||||
ctx.components.append(component);
|
||||
return;
|
||||
|
||||
for v in G.nodes:
|
||||
tarjan_visit(v);
|
||||
return components;
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# AUXILIARY context variables for algorithm
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@dataclass
|
||||
class NodeInformationDefault:
|
||||
node: Any = field(default=None);
|
||||
least_index: int = field(default=0);
|
||||
index: int = field(default=0);
|
||||
state: State = field(default=State.UNTOUCHED, repr=False);
|
||||
|
||||
class NodeInformation(NodeInformationDefault):
|
||||
def __init__(self, u: Any):
|
||||
super().__init__();
|
||||
self.node = u;
|
||||
|
||||
@dataclass
|
||||
class ContextDefault:
|
||||
max_index: int = field(default=0);
|
||||
debug: 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());
|
||||
|
||||
class Context(ContextDefault):
|
||||
def __init__(self, G: Graph, debug: bool):
|
||||
super().__init__();
|
||||
self.debug = debug;
|
||||
self.infos = { u: NodeInformation(u) for u in G.nodes };
|
||||
|
||||
def push(self, u: Any):
|
||||
self.stack.push(u);
|
||||
|
||||
def top(self) -> Any:
|
||||
return self.stack.top();
|
||||
|
||||
def pop(self) -> Any:
|
||||
return self.stack.pop();
|
||||
|
||||
def update_infos(self, u: Any, info: NodeInformation):
|
||||
self.infos[u] = info;
|
||||
|
||||
def set_state(self, u: Any, state: State):
|
||||
info = self.infos[u];
|
||||
info.state = state;
|
||||
self.update_infos(u, info);
|
||||
|
||||
def set_least_index(self, u: Any, least_index: int):
|
||||
info = self.infos[u];
|
||||
info.least_index = least_index;
|
||||
self.update_infos(u, info);
|
||||
|
||||
def set_index(self, u: Any, index: int):
|
||||
info = self.infos[u];
|
||||
info.index = index;
|
||||
self.update_infos(u, info);
|
||||
|
||||
def stack_contains(self, u: Any) -> bool:
|
||||
return self.stack.contains(u);
|
||||
|
||||
def get_info(self, u: Any) -> NodeInformation:
|
||||
return self.infos[u];
|
||||
|
||||
def get_state(self, u: Any) -> State:
|
||||
return self.get_info(u).state;
|
||||
|
||||
def get_least_index(self, u: Any) -> int:
|
||||
return self.get_info(u).least_index;
|
||||
|
||||
def get_index(self, u: Any) -> int:
|
||||
return self.get_info(u).index;
|
||||
|
||||
def log_info(self, u: Any):
|
||||
if not self.debug:
|
||||
return;
|
||||
info = self.get_info(u);
|
||||
log_debug(info);
|
||||
|
@ -11,6 +11,7 @@ import sys;
|
||||
os.chdir(os.path.join(os.path.dirname(__file__), '..'));
|
||||
sys.path.insert(0, os.getcwd());
|
||||
|
||||
from src.core.log import *;
|
||||
from src.graphs.graph import *;
|
||||
from src.graphs.tarjan import *;
|
||||
|
||||
@ -26,14 +27,27 @@ from src.graphs.tarjan import *;
|
||||
|
||||
def enter():
|
||||
## Beispiel aus Seminarblatt 1
|
||||
## TODO -> schieben in config datei
|
||||
G = Graph(
|
||||
nodes=[1,2,3,4,5,6,7],
|
||||
edges=[(1,2), (1,3), (2,3), (3,4), (4,5), (5,2), (5,6), (5,7), (6,7)],
|
||||
);
|
||||
components = tarjan_algorithm(G);
|
||||
nodes = [1,2,3,4,5,6,7,8];
|
||||
edges = [
|
||||
(1,2),
|
||||
(1,3),
|
||||
(2,4),
|
||||
(2,5),
|
||||
(3,5),
|
||||
(3,6),
|
||||
(3,8),
|
||||
(4,5),
|
||||
(4,7),
|
||||
(5,1),
|
||||
(5,8),
|
||||
(6,8),
|
||||
(7,8),
|
||||
(8,6),
|
||||
];
|
||||
G = Graph(nodes=nodes, edges=edges);
|
||||
components = tarjan_algorithm(G, debug=True);
|
||||
for component in components:
|
||||
print(component);
|
||||
log_debug(component);
|
||||
return;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -23,10 +23,10 @@ class Stack:
|
||||
'''
|
||||
A data structure for stacks
|
||||
'''
|
||||
values: list[Any];
|
||||
elements: list[Any];
|
||||
|
||||
def __init__(self):
|
||||
self.values = [];
|
||||
self.elements = [];
|
||||
return;
|
||||
|
||||
def __len__(self):
|
||||
@ -34,16 +34,25 @@ class Stack:
|
||||
@returns
|
||||
number of elements in stack
|
||||
'''
|
||||
return len(self.values);
|
||||
return len(self.elements);
|
||||
|
||||
def __contains__(self, value: Any) -> bool:
|
||||
return value in self.values;
|
||||
return value in self.elements;
|
||||
|
||||
def push(self, value: Any):
|
||||
'''
|
||||
add element to stack
|
||||
'''
|
||||
self.values.append(value);
|
||||
self.elements.append(value);
|
||||
|
||||
def top(self) -> Any:
|
||||
'''
|
||||
@returns
|
||||
top element from stack without removal
|
||||
'''
|
||||
if len(self.elements) == 0:
|
||||
raise Exception('Stack is empty!');
|
||||
return self.elements[-1];
|
||||
|
||||
def pop(self) -> Any:
|
||||
'''
|
||||
@ -51,14 +60,11 @@ class Stack:
|
||||
top element from stack and removes it
|
||||
'''
|
||||
value = self.top();
|
||||
self.values = self.values[:-1];
|
||||
self.elements = self.elements[:-1];
|
||||
return value;
|
||||
|
||||
def top(self) -> Any:
|
||||
def contains(self, element: Any) -> bool:
|
||||
'''
|
||||
@returns
|
||||
top element from stack without removal
|
||||
checks if element in stack:
|
||||
'''
|
||||
if len(self.values) == 0:
|
||||
raise Exception('Stack is empty!');
|
||||
return self.values[-1];
|
||||
return element in self.elements;
|
||||
|
0
code/python/tests/__init__.py
Normal file
0
code/python/tests/__init__.py
Normal file
26
code/python/tests/conftest.py
Normal file
26
code/python/tests/conftest.py
Normal file
@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from unittest import TestCase;
|
||||
from pytest import mark;
|
||||
from pytest import fixture;
|
||||
|
||||
from tests.core.log import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# CONSTANTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# FIXTURES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@fixture(scope='module')
|
||||
def test():
|
||||
return TestCase();
|
0
code/python/tests/core/__init__.py
Normal file
0
code/python/tests/core/__init__.py
Normal file
17
code/python/tests/core/log.py
Normal file
17
code/python/tests/core/log.py
Normal file
@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHODS - logging
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def log(*lines: str):
|
||||
with open('logs/debug.log', 'a') as fp:
|
||||
for line in lines:
|
||||
print(line, end='\n', file=fp);
|
0
code/python/tests/test_core/__init__.py
Normal file
0
code/python/tests/test_core/__init__.py
Normal file
21
code/python/tests/test_core/conftest.py
Normal file
21
code/python/tests/test_core/conftest.py
Normal file
@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from pytest import mark;
|
||||
from pytest import fixture;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# CONSTANTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# FIXTURES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
21
code/python/tests/test_core/test_log.py
Normal file
21
code/python/tests/test_core/test_log.py
Normal file
@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from pytest import mark;
|
||||
from pytest import fixture;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# FIXTURES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# Test logging
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
0
code/python/tests/test_graphs/__init__.py
Normal file
0
code/python/tests/test_graphs/__init__.py
Normal file
24
code/python/tests/test_graphs/conftest.py
Normal file
24
code/python/tests/test_graphs/conftest.py
Normal file
@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from unittest import TestCase;
|
||||
from pytest import mark;
|
||||
from pytest import fixture;
|
||||
|
||||
from tests.core.log import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# CONSTANTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# FIXTURES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
60
code/python/tests/test_graphs/test_graph.py
Normal file
60
code/python/tests/test_graphs/test_graph.py
Normal file
@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from contextlib import nullcontext as does_not_raise
|
||||
from collections import Counter;
|
||||
from pytest import mark;
|
||||
from pytest import fixture;
|
||||
from unittest import TestCase;
|
||||
|
||||
from src.graphs.graph import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# FIXTURES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@fixture(scope='module')
|
||||
def graph1() -> Graph:
|
||||
return Graph(
|
||||
nodes=[1,2,3,4,5,6,7,8],
|
||||
edges=[(1,2), (1, 3), (2,3), (3,4), (4, 5), (5, 6), (6, 2), (6, 7), (6, 8)],
|
||||
);
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# Test Graph
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@mark.usefixtures('test')
|
||||
def test_graph_creation(test: TestCase):
|
||||
with does_not_raise():
|
||||
G = Graph(
|
||||
[5, 7, 8],
|
||||
[(5,7), (7, 8)]
|
||||
);
|
||||
test.assertEqual(len(G), 3);
|
||||
G = Graph(
|
||||
["5", "7", "8", "10"],
|
||||
[("5", "7"), ("7", "8")]
|
||||
);
|
||||
test.assertEqual(len(G), 4);
|
||||
G = Graph(nodes=[], edges=[]);
|
||||
test.assertEqual(len(G), 0);
|
||||
|
||||
@mark.usefixtures('test', 'graph1')
|
||||
def test_graph_subgraph(test: TestCase, graph1: Graph):
|
||||
sub_gph = graph1.subgraph([2,4,5,6,8]);
|
||||
test.assertCountEqual(sub_gph.edges, [(6,2), (4,5), (5,6), (6,8)]);
|
||||
|
||||
@mark.usefixtures('test', 'graph1')
|
||||
def test_graph_successors_and_predecessors(test: TestCase, graph1: Graph):
|
||||
test.assertCountEqual(graph1.successors(1), [2, 3]);
|
||||
test.assertEqual(len(graph1.successors(8)), 0);
|
||||
test.assertCountEqual(graph1.successors(6), [2, 7, 8]);
|
||||
|
||||
test.assertEqual(len(graph1.predecessors(1)), 0);
|
||||
test.assertCountEqual(graph1.predecessors(8), [6]);
|
||||
test.assertCountEqual(graph1.predecessors(6), [5]);
|
119
code/python/tests/test_graphs/test_tarjan.py
Normal file
119
code/python/tests/test_graphs/test_tarjan.py
Normal file
@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from contextlib import nullcontext as does_not_raise
|
||||
from collections import Counter;
|
||||
from pytest import mark;
|
||||
from pytest import fixture;
|
||||
from pytest import lazy_fixture;
|
||||
from unittest import TestCase;
|
||||
|
||||
from src.local.typing import *;
|
||||
from src.graphs.graph import *;
|
||||
from src.graphs.tarjan import *;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# FIXTURES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# TESTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
# @mark.usefixtures('my_data', 'first_entry', 'second_entry')
|
||||
# def test_format_data_for_display(my_data):
|
||||
# assert format_data_for_display(my_data) == [
|
||||
# "Alfonsa Ruiz: Senior Software Engineer",
|
||||
# "Sayid Khan: Project Manager",
|
||||
# ];
|
||||
|
||||
# @mark.usefixtures('my_data', 'first_entry', 'second_entry')
|
||||
# def test_something(my_data):
|
||||
# assert 1+1 == 2;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# Fixtures
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@fixture(scope='module')
|
||||
def graph1() -> Graph:
|
||||
return Graph(
|
||||
nodes=[1,2,3,4],
|
||||
edges=[(1,2), (2,4), (4,2)],
|
||||
);
|
||||
|
||||
@fixture(scope='module')
|
||||
def graph2() -> Graph:
|
||||
return Graph(
|
||||
nodes=[1,2,3,4,5,6,7],
|
||||
edges=[(1,2), (1,3), (2,3), (3,4), (4,5), (5,2), (5,6), (5,7), (6,7)],
|
||||
);
|
||||
|
||||
@fixture(scope='module')
|
||||
def graph3() -> Graph:
|
||||
return Graph(
|
||||
nodes=[1,2,3,4,5,6,7,8],
|
||||
edges=[
|
||||
(1,2),
|
||||
(1,3),
|
||||
(2,4),
|
||||
(2,5),
|
||||
(3,5),
|
||||
(3,6),
|
||||
(3,8),
|
||||
(4,5),
|
||||
(4,7),
|
||||
(5,1),
|
||||
(5,8),
|
||||
(6,8),
|
||||
(7,8),
|
||||
(8,6),
|
||||
],
|
||||
);
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# Test Tarjan-Algorithm
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@mark.parametrize(
|
||||
('G', 'expected'),
|
||||
[
|
||||
(lazy_fixture('graph1'), [[1], [3], [2,4]]),
|
||||
(lazy_fixture('graph2'), [[1], [6], [7], [2,3,4,5]]),
|
||||
(lazy_fixture('graph3'), [[1,2,3,4,5], [7], [6,8]]),
|
||||
],
|
||||
)
|
||||
@mark.usefixtures('test')
|
||||
def test_tarjan(test, G, expected):
|
||||
components = tarjan_algorithm(G, False);
|
||||
assert_components_eq(test, components, expected);
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# AUXILIARY METHODS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def assert_components_eq(test: TestCase, components1: list[list[Any]], components2: list[list[Any]]):
|
||||
result = check_components_eq(test, components1, components2);
|
||||
test.assertTrue(result);
|
||||
|
||||
def check_components_eq(test: TestCase, components1: list[list[Any]], components2: list[list[Any]]) -> bool:
|
||||
if len(components1) != len(components2):
|
||||
return False;
|
||||
for component1 in components1:
|
||||
found = False;
|
||||
for component2 in components2:
|
||||
try:
|
||||
test.assertCountEqual(component1, component2);
|
||||
found = True;
|
||||
break;
|
||||
except:
|
||||
continue;
|
||||
if not found:
|
||||
return False;
|
||||
return True;
|
0
code/python/tests/test_stacks/__init__.py
Normal file
0
code/python/tests/test_stacks/__init__.py
Normal file
15
code/python/tests/test_stacks/conftest.py
Normal file
15
code/python/tests/test_stacks/conftest.py
Normal file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from pytest import mark;
|
||||
from pytest import fixture;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# FIXTURES
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
63
code/python/tests/test_stacks/test_stack.py
Normal file
63
code/python/tests/test_stacks/test_stack.py
Normal file
@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from contextlib import nullcontext as does_not_raise
|
||||
import pytest;
|
||||
from pytest import mark;
|
||||
from unittest import TestCase;
|
||||
|
||||
from src.stacks.stack import Stack;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# Fixtures
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def format_data_for_display(data):
|
||||
return [ '{given_name} {family_name}: {title}'.format(**row) for row in data ];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# Test stack
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@mark.usefixtures('test')
|
||||
def test_stack_initialisation(test: TestCase):
|
||||
stack = Stack();
|
||||
test.assertEqual(len(stack), 0);
|
||||
|
||||
@mark.usefixtures('test')
|
||||
def test_stack_push(test: TestCase):
|
||||
stack = Stack();
|
||||
test.assertEqual(len(stack), 0);
|
||||
stack.push('hallo');
|
||||
test.assertEqual(len(stack), 1);
|
||||
stack.push('welt');
|
||||
test.assertEqual(len(stack), 2);
|
||||
stack.push('!');
|
||||
test.assertEqual(len(stack), 3);
|
||||
|
||||
@mark.usefixtures('test')
|
||||
def test_stack_no_error(test: TestCase):
|
||||
stack = Stack();
|
||||
stack.push('hallo');
|
||||
stack.push('welt');
|
||||
stack.push('!');
|
||||
with does_not_raise():
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
|
||||
@mark.usefixtures('test')
|
||||
def test_stack_error_po(test: TestCase):
|
||||
stack = Stack();
|
||||
stack.push('hallo');
|
||||
stack.push('welt');
|
||||
stack.push('!');
|
||||
with pytest.raises(Exception):
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
stack.pop();
|
@ -19,12 +19,12 @@ endif
|
||||
# Macros
|
||||
################################
|
||||
|
||||
define create_folder_if_not_exists
|
||||
@if ! [ -d "$(1)" ]; then mkdir "$(1)"; fi
|
||||
define create_file_if_not_exists
|
||||
@touch "$(1)";
|
||||
endef
|
||||
|
||||
define create_folder_if_not_exists
|
||||
@touch "$(1)";
|
||||
@if ! [ -d "$(1)" ]; then mkdir "$(1)"; fi
|
||||
endef
|
||||
|
||||
define delete_if_file_exists
|
||||
|
@ -48,7 +48,7 @@ fn tarjan_visit<T>(gph: &Graph<T>, &u: &T, ctx: &mut Context<T>)
|
||||
{
|
||||
// Place node on stack + initialise visit-index + component-index.
|
||||
ctx.max_index += 1;
|
||||
ctx.push(&u);
|
||||
ctx.stack_push(&u);
|
||||
ctx.set_root(&u, ctx.max_index);
|
||||
ctx.set_index(&u, ctx.max_index);
|
||||
ctx.set_state(&u, State::PENDING);
|
||||
@ -63,18 +63,19 @@ fn tarjan_visit<T>(gph: &Graph<T>, &u: &T, ctx: &mut Context<T>)
|
||||
tarjan_visit(gph, &v, ctx);
|
||||
}
|
||||
// Update associated component-index of parent node, if in same component as child:
|
||||
if ctx.stack.contains(&v) {
|
||||
if ctx.stack_contains(&v) {
|
||||
ctx.set_root(&u, value_min!(ctx.get_root(&u), ctx.get_root(&v)));
|
||||
}
|
||||
}
|
||||
ctx.set_state(&u, State::FINISHED);
|
||||
ctx.log_info(&u);
|
||||
|
||||
// If at root of component pop everything from stack up to root and add component:
|
||||
if ctx.get_index(&u) == ctx.get_root(&u) {
|
||||
let mut component: Vec<T> = Vec::new();
|
||||
loop {
|
||||
let v = ctx.top();
|
||||
ctx.pop();
|
||||
let v = ctx.stack_top();
|
||||
ctx.stack_pop();
|
||||
component.push(v.clone());
|
||||
if u == v { break; }
|
||||
}
|
||||
@ -119,15 +120,15 @@ impl<T> Context<T>
|
||||
};
|
||||
}
|
||||
|
||||
fn push(self: &mut Self, u: &T) {
|
||||
fn stack_push(self: &mut Self, u: &T) {
|
||||
self.stack.push(u.clone());
|
||||
}
|
||||
|
||||
fn top(self: &mut Self) -> T {
|
||||
fn stack_top(self: &mut Self) -> T {
|
||||
return self.stack.top();
|
||||
}
|
||||
|
||||
fn pop(self: &mut Self) -> T {
|
||||
fn stack_pop(self: &mut Self) -> T {
|
||||
return self.stack.pop();
|
||||
}
|
||||
|
||||
@ -153,6 +154,10 @@ impl<T> Context<T>
|
||||
self.update_infos(u, info);
|
||||
}
|
||||
|
||||
fn stack_contains(self: &Self, u: &T) -> bool {
|
||||
return self.stack.contains(u);
|
||||
}
|
||||
|
||||
fn get_info(self: &Self, u: &T) -> NodeInformation<T> {
|
||||
return *self.infos.get(u).unwrap();
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ fn fixture_graph3() -> graph::Graph<i32> {
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Test Graph
|
||||
// Test Tarjan-Algorithm
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
#[rstest]
|
||||
|
@ -7,10 +7,6 @@ extern crate closure;
|
||||
|
||||
use ads2::stacks::stack;
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Test regex
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Test stack
|
||||
// ----------------------------------------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user