graph.py
The file math/src/tonnetz/graph.py. 27 lines, 2 definitions.
Distances and diameter in the Tonnetz graph.
Code, comments and printed output are in Turkish. The explanations are translated.
Top of file: imports
"""Tonnetz grafında uzaklıklar ve çap."""from __future__ import annotationsfrom collections import dequefrom .core import Tonnetzdistances_from
functionThe shortest path lengths from node x to all nodes (BFS).
def distances_from(t: Tonnetz, x: int) -> list[int]: """x düğümünden bütün düğümlere en kısa yol uzunlukları (BFS).""" dist = [-1] * t.N dist[x % t.N] = 0 queue = deque([x % t.N]) while queue: u = queue.popleft() for v in t.neighbors(u): if dist[v] < 0: dist[v] = dist[u] + 1 queue.append(v) return distdiameter
functionThe diameter of the graph: the largest shortest-path length between two nodes.
def diameter(t: Tonnetz) -> int: """Grafın çapı: iki düğüm arasındaki en büyük en kısa yol uzunluğu.""" # Graf ötelemeler altında simetriktir, tek kaynaktan BFS yeter. return max(distances_from(t, 0))