Skip to content

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 Tonnetz

distances_from

function

The 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 dist

diameter

function

The 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))

Every number on this site comes from data produced by the Python core and is checked by tests.

TÜBİTAK 2204-A mathematics project