Skip to content

Commit

Permalink
correct DFS, proposal type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
xtof-durr committed Jun 28, 2023
1 parent e38b9a6 commit 7f6c2ea
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
Binary file modified .coverage
Binary file not shown.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
help:
help::
@echo "make pycodestyle run pycodestyle on Python source"
@echo "make pylint run pylint on Python source"

tests:
tests::
python -m unittest

pycodestyle:
pycodestyle::
-@find setup.py tryalgo -type f -name '*.py' | xargs pycodestyle

pylint:
pylint::
-@find setup.py tryalgo -type f -name '*.py' | xargs pylint --disable=C0103 -j 4

.PHONY: help pycodestyle pylint bin docs

docs:
docs::
cd docs/_static && convert logo_W.png logo_B.png +append logo_white.png
sphinx-apidoc -f -o docs/tryalgo tryalgo
sphinx-build docs docs/_build
4 changes: 2 additions & 2 deletions tests/test_tryalgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def isclose(a, b, rel_tol, abs_tol):
from tryalgo.ford_fulkerson import ford_fulkerson
from tryalgo.gale_shapley import gale_shapley
from tryalgo.gauss_jordan import gauss_jordan, GJ_ZERO_SOLUTIONS, GJ_SINGLE_SOLUTION, GJ_SEVERAL_SOLUTIONS
from tryalgo.graph import Graph
from tryalgo.graph import Graph_named_vertices
from tryalgo.graph01 import dist01
from tryalgo.horn_sat import horn_sat
from tryalgo.huffman import huffman
Expand Down Expand Up @@ -774,7 +774,7 @@ def test_gauss_jordan(self):
x, [1, 1, 3]), GJ_ZERO_SOLUTIONS)

def test_graph(self):
G = Graph()
G = Graph_named_vertices()
G.add_node("A")
G.add_node("B")
G.add_node("C")
Expand Down
34 changes: 28 additions & 6 deletions tryalgo/dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
jill-jênn vie et christoph durr - 2015-2019
"""

from typing import List, Optional
from . graph import Graph

# snip{ dfs-recursive
def dfs_recursive(graph, node, seen):
def dfs_recursive(graph: Graph, node: int, seen: List[bool]) -> None:
"""DFS, detect connected component, recursive implementation
:param graph: directed graph in listlist or listdict format
Expand All @@ -25,7 +27,7 @@ def dfs_recursive(graph, node, seen):


# snip{ dfs-iterative
def dfs_iterative(graph, start, seen):
def dfs_iterative_old(graph, start, seen):
"""DFS, detect connected component, iterative implementation
:param graph: directed graph in listlist or listdict format
Expand All @@ -44,9 +46,29 @@ def dfs_iterative(graph, start, seen):
to_visit.append(neighbor)
# snip}

# snip{ dfs-iterative
def dfs_iterative(graph: Graph, start: int, seen: List[int]) -> None:
"""DFS, detect connected component, iterative implementation
:param graph: directed graph in listlist or listdict format
:param int node: to start graph exploration
:param boolean-table seen: will be set true for the connected component
containing node.
:complexity: `O(|V|+|E|)`
"""
to_visit = [start]
while to_visit:
node = to_visit.pop()
if not seen[node]:
for neighbor in reversed(graph[node]):
if not seen[neighbor]:
to_visit.append(neighbor)
seen[node] = True
# snip}


# snip{ dfs-tree
def dfs_tree(graph, start=0):
def dfs_tree(graph: Graph, start: int=0) -> List[int]:
"""DFS, build DFS tree in unweighted graph
:param graph: directed graph in listlist or listdict format
Expand All @@ -66,7 +88,7 @@ def dfs_tree(graph, start=0):
# snip}


def dfs_grid_recursive(grid, i, j, mark='X', free='.'):
def dfs_grid_recursive(grid: List[List[str]], i: int, j: int, mark: str='X', free: str='.') -> None:
"""DFS on a grid, mark connected component, recursive version
:param grid: matrix, 4-neighborhood
Expand All @@ -86,7 +108,7 @@ def dfs_grid_recursive(grid, i, j, mark='X', free='.'):


# snip{ dfs-grid
def dfs_grid(grid, i, j, mark='X', free='.'):
def dfs_grid(grid, i, j, mark='X', free: str='.') -> None:
"""DFS on a grid, mark connected component, iterative version
:param grid: matrix, 4-neighborhood
Expand All @@ -111,7 +133,7 @@ def dfs_grid(grid, i, j, mark='X', free='.'):


# pylint: disable=too-many-nested-blocks, no-else-return
def find_cycle(graph):
def find_cycle(graph: Graph) -> Optional[List[int]]:
"""find a cycle in an undirected graph
:param graph: undirected graph in listlist or listdict format
Expand Down
6 changes: 5 additions & 1 deletion tryalgo/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# pylint: disable=bad-whitespace, line-too-long, missing-docstring
# pylint: disable=dangerous-default-value, too-many-locals, too-many-branches

# from __future__ import annotations
from typing import List, Dict, Type, Union

def readval(file, ty):
"""Reads a line from file with an item of type ty
Expand Down Expand Up @@ -353,7 +355,7 @@ def make_flow_labels(graph, flow, capac):

# pylint: disable=arguments-out-of-order
# snip{ class_graph
class Graph:
class Graph_named_vertices:
def __init__(self):
self.neighbors = []
self.name2node = {}
Expand Down Expand Up @@ -384,3 +386,5 @@ def add_arc(self, name_u, name_v, weight_uv=None):
self.neighbors[u].append(v)
self.weight[u][v] = weight_uv
# snip}

Graph = Union[List[List[int]], List[Dict[int, int]], Graph_named_vertices]

0 comments on commit 7f6c2ea

Please sign in to comment.