47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
# IMPORTS
|
||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
|
||
|
#
|
||
|
|
||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
# EXPORTS
|
||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
|
||
|
__all__ = [
|
||
|
'Graph',
|
||
|
];
|
||
|
|
||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
# CLASS Graph
|
||
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||
|
|
||
|
class Graph(object):
|
||
|
'''
|
||
|
a data structure for graphs
|
||
|
'''
|
||
|
nodes: list[str];
|
||
|
edges: list[tuple[str,str]]
|
||
|
|
||
|
def __init__(self, nodes: list[str], edges: list[tuple[str,str]]):
|
||
|
self.nodes = nodes;
|
||
|
self.edges = edges;
|
||
|
return;
|
||
|
|
||
|
def successor(self, u: str):
|
||
|
'''
|
||
|
@returns
|
||
|
list of successor nodes
|
||
|
'''
|
||
|
return [ v for (u_, v) in self.edges if u == u_ ];
|
||
|
|
||
|
def predecessor(self, v: str):
|
||
|
'''
|
||
|
@returns
|
||
|
list of predecessor nodes
|
||
|
'''
|
||
|
return [ u for (u, v_) in self.edges if v == v_ ];
|