def check_relationships(branches): ancestors = {b: set() for b in branches} length = len(branches) * (len(branches) - 1) for b1, b2 in ub.ProgIter(it.combinations(branches, 2), length=length): ret = ub.cmd('git merge-base --is-ancestor {} {}'.format(b1, b2))['ret'] if ret == 0: ancestors[b1].add(b2) ret = ub.cmd('git merge-base --is-ancestor {} {}'.format(b2, b1))['ret'] if ret == 0: ancestors[b2].add(b1) print('<key> is an ancestor of <value>') print(ub.repr2(ancestors)) descendants = {b: set() for b in branches} for key, others in ancestors.items(): for o in others: descendants[o].add(key) print('<key> descends from <value>') print(ub.repr2(descendants)) import plottool as pt import networkx as nx G = nx.DiGraph() G.add_nodes_from(branches) for key, others in ancestors.items(): for o in others: # G.add_edge(key, o) G.add_edge(o, key) from networkx.algorithms.connectivity.edge_augmentation import collapse flag = True G2 = G while flag: flag = False for u, v in list(G2.edges()): if G2.has_edge(v, u): G2 = collapse(G2, [[u, v]]) node_relabel = ub.ddict(list) for old, new in G2.graph['mapping'].items(): node_relabel[new].append(old) G2 = nx.relabel_nodes(G2, {k: '\n'.join(v) for k, v in node_relabel.items()}) flag = True break G3 = nx.transitive_reduction(G2) pt.show_nx(G3, arrow_width=1.5, prog='dot', layoutkw=dict(prog='dot')) pt.zoom_factory() pt.pan_factory() pt.plt.show()
def _check_unconstrained_bridge_property(G, info1): # Check Theorem 5 from Eswaran and Tarjan. (1975) Augmentation problems import math bridge_ccs = list(nx.connectivity.bridge_components(G)) # condense G into an forest C C = collapse(G, bridge_ccs) p = len([n for n, d in C.degree() if d == 1]) # leafs q = len([n for n, d in C.degree() if d == 0]) # isolated if p + q > 1: size_target = int(math.ceil(p / 2.0)) + q size_aug = info1['num_edges'] assert size_aug == size_target, ( 'augmentation size is different from what theory predicts')
def _check_unconstrained_bridge_property(G, info1): # Check Theorem 5 from Eswaran and Tarjan. (1975) Augmentation problems import math bridge_ccs = list(nx.connectivity.bridge_components(G)) # condense G into an forest C C = collapse(G, bridge_ccs) p = len([n for n, d in C.degree() if d == 1]) # leafs q = len([n for n, d in C.degree() if d == 0]) # isolated if p + q > 1: size_target = int(math.ceil(p / 2.0)) + q size_aug = info1['num_edges'] assert_equal(size_aug, size_target, ( 'augmentation size is different from what theory predicts'))