-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_searching.py
94 lines (70 loc) · 2.57 KB
/
_searching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Suchalgorithmen für `List[ComparableContentT]`."""
from __future__ import annotations
__all__: Final[list[str]] = [
"breadth_first_search",
"depth_first_search",
"linear_search",
]
from typing import TYPE_CHECKING, Final, TypeVar
from nrw.datastructures._list import List
if TYPE_CHECKING:
from nrw.datastructures._edge import Edge
from nrw.datastructures._graph import Graph
from nrw.datastructures._vertex import Vertex
_T = TypeVar("_T")
def linear_search(lst: List[_T], element: _T) -> int:
lst.to_first()
index: int = 0
while lst.has_access:
if lst.content == element:
return index
index += 1
lst.next()
return index
def _depth_first_search_impl(graph: Graph, vertex: Vertex) -> List[Vertex]:
assert graph.get_vertex(vertex.id) is not None
result: List[Vertex] = List()
if vertex.is_marked:
return result
result.append(vertex)
vertex.mark = True
neighbours: List[Vertex] = graph.get_neighbours(vertex)
neighbours.to_first()
while neighbours.has_access:
current: Vertex | None = neighbours.content
assert current is not None
result.concat(_depth_first_search_impl(graph, current))
neighbours.next()
return result
def depth_first_search(graph: Graph, vertex: Vertex) -> List[Vertex]:
if graph.is_empty or graph.get_vertex(vertex.id) is None:
return List()
graph.set_all_vertex_marks(False)
graph.set_all_edge_marks(False)
return _depth_first_search_impl(graph, vertex)
def breadth_first_search(graph: Graph, vertex: Vertex) -> List[Vertex]:
result: List[Vertex] = List()
if graph.is_empty or graph.get_vertex(vertex.id) is None:
return result
graph.set_all_vertex_marks(False)
graph.set_all_edge_marks(False)
vertex.mark = True
result.append(vertex)
result.to_first()
while result.has_access:
current: Vertex | None = result.content
assert current is not None
neighbours: List[Vertex] = graph.get_neighbours(current)
neighbours.to_first()
while neighbours.has_access:
current_neighbour: Vertex | None = neighbours.content
assert current_neighbour is not None
if not current_neighbour.is_marked:
edge: Edge | None = graph.get_edge(current_neighbour, current)
assert edge is not None
edge.mark = True
current_neighbour.mark = True
result.append(current_neighbour)
neighbours.next()
result.next()
return result