From e0808a7ea8a46e9f28d6d747bfeae4dcd6d4cb7f Mon Sep 17 00:00:00 2001 From: Naman-Vasudev Date: Tue, 21 Oct 2025 16:11:57 +0530 Subject: [PATCH 1/4] Create adjacency_representation.py --- genetic_algorithm/adjacency_representation.py | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 genetic_algorithm/adjacency_representation.py diff --git a/genetic_algorithm/adjacency_representation.py b/genetic_algorithm/adjacency_representation.py new file mode 100644 index 000000000000..2bcd68249964 --- /dev/null +++ b/genetic_algorithm/adjacency_representation.py @@ -0,0 +1,72 @@ +# Wikipedia URL- https://en.wikipedia.org/wiki/Adjacency_list +""" +Closed Tour Adjacency Representation +------------------------------------ + +This converts a path representation (like A→L→G→C→...) into an +adjacency vector form, where each position corresponds to a city in +alphabetical order and the value indicates the next city in the tour. + +A closed tour means the last city connects back to the first. + +Usage +----- +Run doctests with: + python -m doctest -v closed_tour_adjacency.py +""" + +from typing import List, Dict + + +def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]: + """ + Generate adjacency vector for a closed tour. + + Each position corresponds to a city (from `nodes`) and contains + the next city in the tour. The last city connects back to the first. + + Parameters + ---------- + path : List[str] + Ordered list of cities representing the tour. + nodes : List[str] + Fixed node order (e.g., ['A', 'B', 'C', ...]). + + Returns + ------- + List[str] + Adjacency vector aligned with the node order. + + Examples + -------- + >>> path = list("ALGCFJHEKIBD") + >>> nodes = sorted(set(path)) + >>> adjacency_vector_closed(path, nodes) + ['L', 'D', 'F', 'A', 'K', 'J', 'C', 'E', 'B', 'H', 'I', 'G'] + + >>> adjacency_vector_closed(list("ABCD"), list("ABCD")) + ['B', 'C', 'D', 'A'] + """ + + next_city_map: Dict[str, str] = {} + total_cities = len(path) + + for index, city in enumerate(path): + # The last city connects to the first (closed tour) + next_city = path[(index + 1) % total_cities] + next_city_map[city] = next_city + + return [next_city_map.get(city, '-') for city in nodes] + + +if __name__ == "__main__": + sample_path = list("ALGCFJHEKIBD") + ordered_nodes = sorted(set(sample_path)) + adjacency = adjacency_vector_closed(sample_path, ordered_nodes) + + print("Adjacency Representation (alphabetical order):") + for city, next_city in zip(ordered_nodes, adjacency): + print(f"{city} → {next_city}") + + print("\nVector form:") + print(" ".join(adjacency)) \ No newline at end of file From e43ebc595efa3b0f40369564f817ba3d2d84cf17 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Oct 2025 10:44:16 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- genetic_algorithm/adjacency_representation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/genetic_algorithm/adjacency_representation.py b/genetic_algorithm/adjacency_representation.py index 2bcd68249964..7c8b8e6c61a9 100644 --- a/genetic_algorithm/adjacency_representation.py +++ b/genetic_algorithm/adjacency_representation.py @@ -56,7 +56,7 @@ def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]: next_city = path[(index + 1) % total_cities] next_city_map[city] = next_city - return [next_city_map.get(city, '-') for city in nodes] + return [next_city_map.get(city, "-") for city in nodes] if __name__ == "__main__": @@ -69,4 +69,4 @@ def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]: print(f"{city} → {next_city}") print("\nVector form:") - print(" ".join(adjacency)) \ No newline at end of file + print(" ".join(adjacency)) From 4446412b34e75c57ffc66f3cff7ac158310c627f Mon Sep 17 00:00:00 2001 From: Naman-Vasudev Date: Tue, 21 Oct 2025 16:21:44 +0530 Subject: [PATCH 3/4] Update adjacency_representation.py --- genetic_algorithm/adjacency_representation.py | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/genetic_algorithm/adjacency_representation.py b/genetic_algorithm/adjacency_representation.py index 2bcd68249964..b050c6b6a9c8 100644 --- a/genetic_algorithm/adjacency_representation.py +++ b/genetic_algorithm/adjacency_representation.py @@ -1,24 +1,18 @@ -# Wikipedia URL- https://en.wikipedia.org/wiki/Adjacency_list """ Closed Tour Adjacency Representation ------------------------------------ -This converts a path representation (like A→L→G→C→...) into an -adjacency vector form, where each position corresponds to a city in -alphabetical order and the value indicates the next city in the tour. +Converts a path representation (like A→L→G→C→...) into an adjacency +vector form, where each position corresponds to a city in alphabetical +order and the value indicates the next city in the tour. A closed tour means the last city connects back to the first. -Usage ------ -Run doctests with: - python -m doctest -v closed_tour_adjacency.py +Reference: +https://en.wikipedia.org/wiki/Adjacency_list """ -from typing import List, Dict - - -def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]: +def adjacency_vector_closed(path: list[str], nodes: list[str]) -> list[str]: """ Generate adjacency vector for a closed tour. @@ -27,14 +21,14 @@ def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]: Parameters ---------- - path : List[str] + path : list[str] Ordered list of cities representing the tour. - nodes : List[str] + nodes : list[str] Fixed node order (e.g., ['A', 'B', 'C', ...]). Returns ------- - List[str] + list[str] Adjacency vector aligned with the node order. Examples @@ -48,11 +42,10 @@ def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]: ['B', 'C', 'D', 'A'] """ - next_city_map: Dict[str, str] = {} + next_city_map: dict[str, str] = {} total_cities = len(path) for index, city in enumerate(path): - # The last city connects to the first (closed tour) next_city = path[(index + 1) % total_cities] next_city_map[city] = next_city @@ -69,4 +62,4 @@ def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]: print(f"{city} → {next_city}") print("\nVector form:") - print(" ".join(adjacency)) \ No newline at end of file + print(" ".join(adjacency)) From 4b44751ab462a1ab07cc5d54a570f095ec8942cb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Oct 2025 10:52:34 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- genetic_algorithm/adjacency_representation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/genetic_algorithm/adjacency_representation.py b/genetic_algorithm/adjacency_representation.py index ac0953117bd0..65eeb6dbce47 100644 --- a/genetic_algorithm/adjacency_representation.py +++ b/genetic_algorithm/adjacency_representation.py @@ -12,6 +12,7 @@ https://en.wikipedia.org/wiki/Adjacency_list """ + def adjacency_vector_closed(path: list[str], nodes: list[str]) -> list[str]: """ Generate adjacency vector for a closed tour.