From b2984bd3ddd13a1b7122e2c3caa4c0a10c1e662f Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Sun, 14 Dec 2025 17:15:46 +0300 Subject: [PATCH 1/3] Implemented a graph class --- src/Graph/graph.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/Graph/graph.py diff --git a/src/Graph/graph.py b/src/Graph/graph.py new file mode 100644 index 0000000..15aad95 --- /dev/null +++ b/src/Graph/graph.py @@ -0,0 +1,23 @@ +class Graph: + def __init__(self, directed: bool = False) -> None: + self.adjacency_list: dict[str, list[str]] = {} + self.directed: bool = directed + self.vertices: set[str] = set() + + def add_edge(self, vertex_1: str, vertex_2: str): + if vertex_1 not in self.adjacency_list: + self.adjacency_list[vertex_1] = [] + self.adjacency_list[vertex_1].append(vertex_2) + self.vertices.add(vertex_1) + self.vertices.add(vertex_2) + + if vertex_2 not in self.adjacency_list: + self.adjacency_list[vertex_2] = [] + + if not self.directed: + self.adjacency_list[vertex_2].append(vertex_1) + + def add_vertex(self, vertex: str): + if vertex not in self.adjacency_list: + self.adjacency_list[vertex] = [] + self.vertices.add(vertex) From 9d97a80bf65ff3d902b612fb3dc0884f4b6df2c4 Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Sun, 14 Dec 2025 17:16:08 +0300 Subject: [PATCH 2/3] Implemented a DFS algorithm --- src/Graph/graph.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/Graph/graph.py b/src/Graph/graph.py index 15aad95..d92d420 100644 --- a/src/Graph/graph.py +++ b/src/Graph/graph.py @@ -21,3 +21,49 @@ def add_vertex(self, vertex: str): if vertex not in self.adjacency_list: self.adjacency_list[vertex] = [] self.vertices.add(vertex) + + def dfs(self, start_vertex) -> list[str]: + if start_vertex not in self.vertices: + return [] + + visited = [] + stack = [start_vertex] + seen = set() + + while stack: + vertex = stack.pop() + + if vertex not in seen: + visited.append(vertex) + seen.add(vertex) + + neighbors = self.adjacency_list.get(vertex, []) + + for index in range(len(neighbors) - 1, -1, -1): + neighbor = neighbors[index] + if neighbor not in seen: + stack.append(neighbor) + + return visited + + def __iter__(self): + all_vertices = list(self.vertices) + + if not all_vertices: + return iter([]) + + dfs_order = self.dfs(all_vertices[0]) + + return iter(dfs_order) + + def __str__(self) -> str: + result = [] + result.append(f"Graph (oriented: {self.directed})") + result.append("Adjacency list:") + + vertices = sorted(self.adjacency_list.keys()) + for vertex in vertices: + neighbors = self.adjacency_list[vertex] + result.append(f" {vertex}: {neighbors}") + + return "\n".join(result) From f21444a84479d12a10b30b7baa7f0d8969f25bc1 Mon Sep 17 00:00:00 2001 From: Pavel Kuliaka Date: Sun, 14 Dec 2025 17:16:38 +0300 Subject: [PATCH 3/3] Formatted file --- src/Graph/graph.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Graph/graph.py b/src/Graph/graph.py index d92d420..d6a216a 100644 --- a/src/Graph/graph.py +++ b/src/Graph/graph.py @@ -3,67 +3,67 @@ def __init__(self, directed: bool = False) -> None: self.adjacency_list: dict[str, list[str]] = {} self.directed: bool = directed self.vertices: set[str] = set() - + def add_edge(self, vertex_1: str, vertex_2: str): if vertex_1 not in self.adjacency_list: self.adjacency_list[vertex_1] = [] self.adjacency_list[vertex_1].append(vertex_2) self.vertices.add(vertex_1) self.vertices.add(vertex_2) - + if vertex_2 not in self.adjacency_list: self.adjacency_list[vertex_2] = [] - + if not self.directed: self.adjacency_list[vertex_2].append(vertex_1) - + def add_vertex(self, vertex: str): if vertex not in self.adjacency_list: self.adjacency_list[vertex] = [] self.vertices.add(vertex) - + def dfs(self, start_vertex) -> list[str]: if start_vertex not in self.vertices: return [] - + visited = [] stack = [start_vertex] seen = set() - + while stack: vertex = stack.pop() - + if vertex not in seen: visited.append(vertex) seen.add(vertex) - + neighbors = self.adjacency_list.get(vertex, []) - + for index in range(len(neighbors) - 1, -1, -1): neighbor = neighbors[index] if neighbor not in seen: stack.append(neighbor) - + return visited - + def __iter__(self): all_vertices = list(self.vertices) - + if not all_vertices: return iter([]) - + dfs_order = self.dfs(all_vertices[0]) - + return iter(dfs_order) - + def __str__(self) -> str: result = [] result.append(f"Graph (oriented: {self.directed})") result.append("Adjacency list:") - + vertices = sorted(self.adjacency_list.keys()) for vertex in vertices: neighbors = self.adjacency_list[vertex] result.append(f" {vertex}: {neighbors}") - + return "\n".join(result)