superflow/tools/adj2dot
2025-03-31 13:25:04 +02:00

55 lines
1.3 KiB
Python
Executable File

#!/usr/bin/python3
# SPDX-FileCopyrightText: 2025 Ämin Baumeler <amin@indyfac.ch>
#
# SPDX-License-Identifier: GPL-3.0-or-later
import sys
import numpy as np
if len(sys.argv) != 1:
print(
sys.argv[0]
+ """
Read adjacency vector of a digraph from stdin, and transform it to
the GraphViz dot language for graph visualization.
The input represents the adjacency vector of a digraph. A 1 in
position (i,j) of a line is an arc i<-j, and a 0 no arc i<-j.
The coordinate i runs from 0 to n-1, the corrdinate j likewise, but
with j != i. The entries are ordered lexicographically."""
)
sys.exit(1)
def print_dot(n, adj, labels):
print('strict digraph "DG" {')
for v in range(n):
print(f'GN{v} [label="{labels[v]}"];')
i = 0
j = 1
for entry in adj:
if entry:
print(f"GN{j} -> GN{i}")
j = j + 1
if i == j:
j = j + 1
if j >= n:
i = i + 1
j = 0
print("}")
for line in sys.stdin:
labels = None
if ":" in line:
labels, line = line.split(":")
labels = tuple(map(int, tuple(labels)))
adj = tuple(map(int, tuple(line.strip())))
n = int(np.ceil(np.sqrt(len(adj))))
if not labels:
labels = tuple(range(n))
print_dot(n, adj, labels)
sys.exit(0)