Ejemplo n.º 1
0
def make_jtree_from_tri_graph(G):
    """returns JT graph"""

    # clique graph
    CG = nx.Graph()
    # maximal weight spanning tree of clique graph is guaranteed to be a junction tree
    # (i.e., it satisfies running intersection property)
    # where weight is the size of the intersection between adjacent cliques.
    CG.add_weighted_edges_from(
        (tuple(c1), tuple(c2), -c1c2)
        for (c1, c2) in combinations(nx.find_cliques(G), 2)
        for c1c2 in [len(set(c1).intersection(set(c2)))] if c1c2 > 0)
    JT = nx.Graph(nx.mst(CG))  # Minimal weight spanning tree for CliqueGraph
    for src, targ in JT.edges():
        JT[src][targ]["sep"] = tuple(set(src).intersection(set(targ)))

    return JT
Ejemplo n.º 2
0
                                                        y,
                                                        z,
                                                        visualize=True)
    assert (x.shape == new_x.shape)  # check triangulation got everything
    x, y, z = new_x, new_y, new_z

    if nx.__version__ < '0.99':
        raise ImportError('The version of NetworkX must be at least '
                          '0.99 to run this example')

    # Make a NetworkX graph out of our point and edge data
    g = build_geometric_graph(x, y, z, edges)

    # Compute minimum spanning tree using networkx
    # nx.mst returns an edge generator
    start_idx, end_idx, _ = np.array(list(nx.mst(g))).T
    start_idx = start_idx.astype(np.int)
    end_idx = end_idx.astype(np.int)

    # Plot this with Mayavi
    graph_plot(
        x,
        y,
        z,
        start_idx,
        end_idx,
        edge_scalars=z[start_idx],
        opacity=0.8,
        colormap='summer',
        line_width=4,
    )