#!/usr/bin/env python3

import numpy as np
from os import path
from urllib import request

fn = 'edges.txt'

# download graph data if not present
if not path.isfile(fn):
    request.urlretrieve(
        'https://gitlab.tugraz.at/-/snippets/266/raw/main/edges.txt',
        fn)

# load edges
edges = np.loadtxt(fn, dtype=int)

# build adjacency matrix from edge list
a = np.zeros((edges.max()+1, edges.max()+1))
a[edges[:,0], edges[:,1]] = 1

print(f'loaded {np.sum(a, dtype=int)} edges')

# A^2, A^3
aa = a@a
aaa = aa@a

# tests
if np.trace(a) == 0:
    print(f'tr A¹ = 0')
else: raise Exception('graph is not simple')

if np.trace(aa) == 0:
    print(f'tr A² = 0')
else: raise Exception('graph is not oriented')

if np.trace(aaa) == 0:
    print(f'tr A³ = 0')
else: raise Exception('graph has 3-cycles')

if np.all(aa>=a):
    print(f'A <= A²')
else: raise Exception('graph contains unblocked edges')

print('all checks passed! graph is FBD')