-
Notifications
You must be signed in to change notification settings - Fork 0
Иванова Алина 28.11. Задание "Обход в глубину" #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ialina07
wants to merge
1
commit into
main
Choose a base branch
from
hm8
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| class Graph: | ||
| def __init__(self): | ||
| """Инициализация неориентированного графа""" | ||
| self.adjacency_list = {} | ||
|
|
||
| def add_vertex(self, vertex): | ||
| """Добавление вершины в граф""" | ||
| if vertex not in self.adjacency_list: | ||
| self.adjacency_list[vertex] = [] | ||
|
|
||
| def add_edge(self, vertex1, vertex2): | ||
| """Добавление ребра между vertex1 и vertex2""" | ||
| # Добавляем вершины, если их еще нет | ||
| self.add_vertex(vertex1) | ||
| self.add_vertex(vertex2) | ||
|
|
||
| # Добавляем двусторонние связи для графа | ||
| if vertex2 not in self.adjacency_list[vertex1]: | ||
| self.adjacency_list[vertex1].append(vertex2) | ||
| if vertex1 not in self.adjacency_list[vertex2]: | ||
| self.adjacency_list[vertex2].append(vertex1) | ||
|
|
||
| def dfs(self, start_vertex=None): | ||
| """ | ||
| Обход графа в глубину(DFS) | ||
|
|
||
| start_vertex: Начальная вершина. Если None, берется первая вершина | ||
|
|
||
| Возвращает список посещенных вершин в порядке обхода | ||
| """ | ||
| if not self.adjacency_list: | ||
| return [] | ||
|
|
||
| # Если начальная вершина не указана, берем первую | ||
| if start_vertex is None: | ||
| start_vertex = next(iter(self.adjacency_list)) | ||
|
|
||
| # Проверяем, что начальная вершина существует | ||
| if start_vertex not in self.adjacency_list: | ||
| raise ValueError(f"Вершина {start_vertex} не существует в графе") | ||
|
|
||
| visited = [] | ||
| stack = [start_vertex] | ||
|
|
||
| while stack: | ||
| vertex = stack.pop() | ||
| if vertex not in visited: | ||
| visited.append(vertex) | ||
| # Добавляем соседей в стек в обратном порядке | ||
| # для сохранения естественного порядка обхода | ||
| for neighbor in reversed(self.adjacency_list[vertex]): | ||
| if neighbor not in visited: | ||
| stack.append(neighbor) | ||
|
|
||
| return visited | ||
|
|
||
| def dfs_recursive(self, start_vertex=None): | ||
| """ | ||
| Рекурсивная версия обхода в глубину | ||
|
|
||
| start_vertex: Начальная вершина. Если None, берется первая вершина | ||
|
|
||
| Возвращает список посещенных вершин в порядке обхода | ||
| """ | ||
| if not self.adjacency_list: | ||
| return [] | ||
|
|
||
| if start_vertex is None: | ||
| start_vertex = next(iter(self.adjacency_list)) | ||
|
|
||
| if start_vertex not in self.adjacency_list: | ||
| raise ValueError(f"Вершина {start_vertex} не существует в графе") | ||
|
|
||
| visited = [] | ||
|
|
||
| def _dfs_recursive(vertex): | ||
| visited.append(vertex) | ||
| for neighbor in self.adjacency_list[vertex]: | ||
| if neighbor not in visited: | ||
| _dfs_recursive(neighbor) | ||
|
|
||
| _dfs_recursive(start_vertex) | ||
| return visited | ||
|
|
||
| def __iter__(self): | ||
| """Делает граф итерируемым (используется DFS для порядка обхода)""" | ||
| self._dfs_order = self.dfs() | ||
| self._index = 0 | ||
| return self | ||
|
|
||
| def __next__(self): | ||
| """Возвращает следующую вершину при итерации""" | ||
| if self._index < len(self._dfs_order): | ||
| vertex = self._dfs_order[self._index] | ||
| self._index += 1 | ||
| return vertex | ||
| raise StopIteration | ||
|
|
||
| def __str__(self): | ||
| """Строковое представление графа""" | ||
| result = [] | ||
| for vertex, neighbors in self.adjacency_list.items(): | ||
| result.append(f"{vertex}: {neighbors}") | ||
| return "\n".join(result) | ||
|
|
||
| def __contains__(self, vertex): | ||
| """Проверка наличия вершины в графе""" | ||
| return vertex in self.adjacency_list | ||
|
|
||
| def get_vertices(self): | ||
| """Возвращает список всех вершин графа""" | ||
| return list(self.adjacency_list.keys()) | ||
|
|
||
| def get_edges(self): | ||
| """Возвращает список всех ребер графа""" | ||
| edges = set() | ||
| for vertex, neighbors in self.adjacency_list.items(): | ||
| for neighbor in neighbors: | ||
| # Храним ребра без дубликатов | ||
| edge = tuple(sorted((vertex, neighbor))) | ||
| edges.add(edge) | ||
| return list(edges) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем? Что за естественный порядок обхода?