Skip to content

Commit 4446412

Browse files
committed
Update adjacency_representation.py
1 parent e0808a7 commit 4446412

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
# Wikipedia URL- https://en.wikipedia.org/wiki/Adjacency_list
21
"""
32
Closed Tour Adjacency Representation
43
------------------------------------
54
6-
This converts a path representation (like A→L→G→C→...) into an
7-
adjacency vector form, where each position corresponds to a city in
8-
alphabetical order and the value indicates the next city in the tour.
5+
Converts a path representation (like A→L→G→C→...) into an adjacency
6+
vector form, where each position corresponds to a city in alphabetical
7+
order and the value indicates the next city in the tour.
98
109
A closed tour means the last city connects back to the first.
1110
12-
Usage
13-
-----
14-
Run doctests with:
15-
python -m doctest -v closed_tour_adjacency.py
11+
Reference:
12+
https://en.wikipedia.org/wiki/Adjacency_list
1613
"""
1714

18-
from typing import List, Dict
19-
20-
21-
def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]:
15+
def adjacency_vector_closed(path: list[str], nodes: list[str]) -> list[str]:
2216
"""
2317
Generate adjacency vector for a closed tour.
2418
@@ -27,14 +21,14 @@ def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]:
2721
2822
Parameters
2923
----------
30-
path : List[str]
24+
path : list[str]
3125
Ordered list of cities representing the tour.
32-
nodes : List[str]
26+
nodes : list[str]
3327
Fixed node order (e.g., ['A', 'B', 'C', ...]).
3428
3529
Returns
3630
-------
37-
List[str]
31+
list[str]
3832
Adjacency vector aligned with the node order.
3933
4034
Examples
@@ -48,11 +42,10 @@ def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]:
4842
['B', 'C', 'D', 'A']
4943
"""
5044

51-
next_city_map: Dict[str, str] = {}
45+
next_city_map: dict[str, str] = {}
5246
total_cities = len(path)
5347

5448
for index, city in enumerate(path):
55-
# The last city connects to the first (closed tour)
5649
next_city = path[(index + 1) % total_cities]
5750
next_city_map[city] = next_city
5851

@@ -69,4 +62,4 @@ def adjacency_vector_closed(path: List[str], nodes: List[str]) -> List[str]:
6962
print(f"{city}{next_city}")
7063

7164
print("\nVector form:")
72-
print(" ".join(adjacency))
65+
print(" ".join(adjacency))

0 commit comments

Comments
 (0)