Beispiel #1
0
 def filter_nan_edges(edges=edges, weight=weight):
     sign = 1 if minimum else -1
     for u, v, d in edges:
         wt = d.get(weight, 1) * sign
         if isnan(wt):
             if ignore_nan:
                 continue
             msg = "NaN found as an edge weight. Edge %s"
             raise ValueError(msg % ((u, v, d), ))
         yield wt, u, v, d
    def test_spring_init_pos(self):
        # Tests GH #2448
        # import math
        from networkx.compat import isnan

        G = nx.Graph()
        G.add_edges_from([(0, 1), (1, 2), (2, 0), (2, 3)])

        init_pos = {0: (0.0, 0.0)}
        fixed_pos = [0]
        pos = nx.fruchterman_reingold_layout(G, pos=init_pos, fixed=fixed_pos)
        has_nan = any(isnan(c) for coords in pos.values() for c in coords)
        assert_false(has_nan, 'values should not be nan')
Beispiel #3
0
    def best_edge(component):
        """Returns the optimum (minimum or maximum) edge on the edge
        boundary of the given set of nodes.

        A return value of ``None`` indicates an empty boundary.

        """
        sign = 1 if minimum else -1
        minwt = float('inf')
        boundary = None
        for e in nx.edge_boundary(G, component, data=True):
            wt = e[-1].get(weight, 1) * sign
            if isnan(wt):
                if ignore_nan:
                    continue
                msg = "NaN found as an edge weight. Edge %s"
                raise ValueError(msg % (e, ))
            if wt < minwt:
                minwt = wt
                boundary = e
        return boundary
 def test_effective_size_borgatti_isolated(self):
     G = self.G.copy()
     G.add_node(1)
     effective_size = nx.effective_size(G)
     assert_true(isnan(effective_size[1]))
 def test_effective_size_isolated(self):
     G = self.G.copy()
     G.add_node(1)
     nx.set_edge_attributes(G, self.G_weights, 'weight')
     effective_size = nx.effective_size(G, weight='weight')
     assert_true(isnan(effective_size[1]))
 def test_constraint_isolated(self):
     G = self.G.copy()
     G.add_node(1)
     constraint = nx.constraint(G)
     assert_true(isnan(constraint[1]))
Beispiel #7
0
def prim_mst_edges(G,
                   minimum,
                   weight='weight',
                   keys=True,
                   data=True,
                   ignore_nan=False):
    """Iterate over edges of Prim's algorithm min/max spanning tree.

    Parameters
    ----------
    G : NetworkX Graph
        The graph holding the tree of interest.

    minimum : bool (default: True)
        Find the minimum (True) or maximum (False) spanning tree.

    weight : string (default: 'weight')
        The name of the edge attribute holding the edge weights.

    keys : bool (default: True)
        If `G` is a multigraph, `keys` controls whether edge keys ar yielded.
        Otherwise `keys` is ignored.

    data : bool (default: True)
        Flag for whether to yield edge attribute dicts.
        If True, yield edges `(u, v, d)`, where `d` is the attribute dict.
        If False, yield edges `(u, v)`.

    ignore_nan : bool (default: False)
        If a NaN is found as an edge weight normally an exception is raised.
        If `ignore_nan is True` then that edge is ignored instead.

    """
    is_multigraph = G.is_multigraph()
    push = heappush
    pop = heappop

    nodes = list(G)
    c = count()

    sign = 1 if minimum else -1

    while nodes:
        u = nodes.pop(0)
        frontier = []
        visited = [u]
        if is_multigraph:
            for v, keydict in G.adj[u].items():
                for k, d in keydict.items():
                    wt = d.get(weight, 1) * sign
                    if isnan(wt):
                        if ignore_nan:
                            continue
                        msg = "NaN found as an edge weight. Edge %s"
                        raise ValueError(msg % ((u, v, k, d), ))
                    push(frontier, (wt, next(c), u, v, k, d))
        else:
            for v, d in G.adj[u].items():
                wt = d.get(weight, 1) * sign
                if isnan(wt):
                    if ignore_nan:
                        continue
                    msg = "NaN found as an edge weight. Edge %s"
                    raise ValueError(msg % ((u, v, d), ))
                push(frontier, (wt, next(c), u, v, d))
        while frontier:
            if is_multigraph:
                W, _, u, v, k, d = pop(frontier)
            else:
                W, _, u, v, d = pop(frontier)
            if v in visited:
                continue
            # Multigraphs need to handle edge keys in addition to edge data.
            if is_multigraph and keys:
                if data:
                    yield u, v, k, d
                else:
                    yield u, v, k
            else:
                if data:
                    yield u, v, d
                else:
                    yield u, v
            # update frontier
            visited.append(v)
            nodes.remove(v)
            if is_multigraph:
                for w, keydict in G.adj[v].items():
                    if w in visited:
                        continue
                    for k2, d2 in keydict.items():
                        new_weight = d2.get(weight, 1) * sign
                        push(frontier, (new_weight, next(c), v, w, k2, d2))
            else:
                for w, d2 in G.adj[v].items():
                    if w in visited:
                        continue
                    new_weight = d2.get(weight, 1) * sign
                    push(frontier, (new_weight, next(c), v, w, d2))