-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathadjacency_representation.py
More file actions
66 lines (49 loc) · 1.88 KB
/
adjacency_representation.py
File metadata and controls
66 lines (49 loc) · 1.88 KB
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
"""
Closed Tour Adjacency Representation
------------------------------------
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.
Reference:
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.
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):
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))