Goulib.graph module

efficient Euclidian Graphs for networkx and related algorithms

requires:
optional:
  • scipy for delauney triangulation
  • rtree for faster GeoGraph algorithms
class Goulib.graph.index[source]

Bases: object

class Property[source]

Bases: object

set_dimension(n)[source]
class index.Index(properties)[source]

Bases: dict

fallback for rtree.index

__init__(properties)[source]
count(ignored)[source]
insert(k, p, _)[source]
delete(k, _)[source]
nearest(p, num_results, objects='raw')[source]

very inefficient, but remember it’s a fallback...

class Goulib.graph.AGraph[source]

Bases: object

Goulib.graph.to_networkx_graph(data, create_using=None, multigraph_input=False)[source]

Make a NetworkX graph from a known data structure. enhances networkx.convert.to_networkx_graph :param data: any type handled by convert.to_networkx_graph, plus: * scipy.spatial.qhull.Delaunay to enable building a graph from a delauney triangulation

If create_using is a :class:`GeoGraph`and data is a Graph where nodes have a ‘pos’ attribute, then this attribute will be used to rename nodes as (x,y,...) tuples suitable for GeoGraph.

class Goulib.graph.GeoGraph(data=None, nodes=None, **kwargs)[source]

Bases: Goulib.graph._Geo, networkx.classes.multigraph.MultiGraph

Undirected graph with nodes positions can be set to non multiedges anytime with attribute multi=False

Parameters:
  • data – see to_networkx_graph() for valid types
  • kwargs – other parameters will be copied as attributes, especially:
__init__(data=None, nodes=None, **kwargs)[source]
Parameters:
  • data – see to_networkx_graph() for valid types
  • kwargs – other parameters will be copied as attributes, especially:
class Goulib.graph.DiGraph(data=None, nodes=None, **kwargs)[source]

Bases: Goulib.graph._Geo, networkx.classes.multidigraph.MultiDiGraph

directed graph with nodes positions can be set to non multiedges anytime with attribute multi=False

Parameters:
  • data – see to_networkx_graph() for valid types
  • kwargs – other parameters will be copied as attributes, especially:
__init__(data=None, nodes=None, **kwargs)[source]
Parameters:
  • data – see to_networkx_graph() for valid types
  • kwargs – other parameters will be copied as attributes, especially:
Goulib.graph.figure(g, box=None, **kwargs)[source]
Parameters:
  • g – _Geo derived Graph
  • box – optional interval.Box if g has no box
Returns:

matplotlib axis suitable for drawing graph g

Goulib.graph.draw_networkx(g, pos=None, **kwargs)[source]

improves nx.draw_networkx :param g: NetworkX Graph :param pos: can be either :

  • optional dictionary of (x,y) node positions
  • function of the form lambda node:(x,y) that maps node positions.
  • None. in this case, nodes are directly used as positions if graph is a GeoGraph, otherwise nx.draw_shell is used
Parameters:**kwargs

passed to nx.draw method as described in http://networkx.lanl.gov/reference/generated/networkx.drawing.nx_pylab.draw_networkx.html with one tweak:

  • if edge_color is a function of the form lambda data:color string, it is mapped over all edges
Goulib.graph.to_drawing(g, d=None, edges=[])[source]

draws Graph to a Drawing :param g: Graph :param d: existing Drawing to draw onto, or None to create a new Drawing :param edges: iterable of edges (with data) that will be added, in the same order. By default all edges are drawn :return: Drawing

Graph edges with an ‘entity’ property

Goulib.graph.write_dxf(g, filename)[source]

writes networkx.Graph in .dxf format

Goulib.graph.write_dot(g, filename)[source]
Goulib.graph.to_json(g, **kwargs)[source]
Returns:string JSON representation of a graph
Goulib.graph.write_json(g, filename, **kwargs)[source]

write a JSON file, suitable for D*.js representation

Goulib.graph.delauney_triangulation(nodes, qhull_options='', incremental=False, **kwargs)[source]

https://en.wikipedia.org/wiki/Delaunay_triangulation :param nodes: _Geo graph or list of (x,y) or (x,y,z) node positions :param qhull_options: string passed to scipy.spatial.Delaunay(), which passes it to Qhull ( http://www.qhull.org/ ) *‘Qt’ ensures all points are connected *‘Qz’ required when nodes lie on a sphere *‘QJ’ solves some singularity situations

Parameters:kwargs – passed to the GeoGraph constructor
Returns:GeoGraph with delauney triangulation between nodes
Goulib.graph.euclidean_minimum_spanning_tree(nodes, **kwargs)[source]
Parameters:nodes – list of (x,y) nodes positions
Returns:GeoGraph with minimum spanning tree between nodes

see https://en.wikipedia.org/wiki/Euclidean_minimum_spanning_tree

Goulib.graph.points_on_sphere(N)[source]

Classes

efficient Euclidian Graphs for networkx and related algorithms

requires:
optional:
  • scipy for delauney triangulation
  • rtree for faster GeoGraph algorithms
class Goulib.graph.index[source]

Bases: object

class Property[source]

Bases: object

set_dimension(n)[source]
__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__eq__

Return self==value.

__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__

Return hash(self).

__init__

Initialize self. See help(type(self)) for accurate signature.

__le__

Return self<=value.

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

Return repr(self).

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

class index.Index(properties)[source]

Bases: dict

fallback for rtree.index

__init__(properties)[source]
count(ignored)[source]
insert(k, p, _)[source]
delete(k, _)[source]
nearest(p, num_results, objects='raw')[source]

very inefficient, but remember it’s a fallback...

__class__

alias of type

__contains__()

True if D has a key k, else False.

__delattr__

Implement delattr(self, name).

__delitem__

Delete self[key].

__dir__() → list

default dir() implementation

__eq__

Return self==value.

__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__getitem__()

x.__getitem__(y) <==> x[y]

__gt__

Return self>value.

__hash__ = None
__iter__

Implement iter(self).

__le__

Return self<=value.

__len__

Return len(self).

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

Return repr(self).

__setattr__

Implement setattr(self, name, value).

__setitem__

Set self[key] to value.

__sizeof__() → size of D in memory, in bytes
__str__

Return str(self).

clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys()

Returns a new dict with keys from iterable and values equal to value.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem() → (k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() → an object providing a view on D's values
index.__class__

alias of type

index.__delattr__

Implement delattr(self, name).

index.__dir__() → list

default dir() implementation

index.__eq__

Return self==value.

index.__format__()

default object formatter

index.__ge__

Return self>=value.

index.__getattribute__

Return getattr(self, name).

index.__gt__

Return self>value.

index.__hash__

Return hash(self).

index.__init__

Initialize self. See help(type(self)) for accurate signature.

index.__le__

Return self<=value.

index.__lt__

Return self<value.

index.__ne__

Return self!=value.

index.__new__()

Create and return a new object. See help(type) for accurate signature.

index.__reduce__()

helper for pickle

index.__reduce_ex__()

helper for pickle

index.__repr__

Return repr(self).

index.__setattr__

Implement setattr(self, name, value).

index.__sizeof__() → int

size of object in memory, in bytes

index.__str__

Return str(self).

class Goulib.graph.AGraph[source]

Bases: object

__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__eq__

Return self==value.

__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__

Return hash(self).

__init__

Initialize self. See help(type(self)) for accurate signature.

__le__

Return self<=value.

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

Return repr(self).

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

Goulib.graph.to_networkx_graph(data, create_using=None, multigraph_input=False)[source]

Make a NetworkX graph from a known data structure. enhances networkx.convert.to_networkx_graph :param data: any type handled by convert.to_networkx_graph, plus: * scipy.spatial.qhull.Delaunay to enable building a graph from a delauney triangulation

If create_using is a :class:`GeoGraph`and data is a Graph where nodes have a ‘pos’ attribute, then this attribute will be used to rename nodes as (x,y,...) tuples suitable for GeoGraph.

class Goulib.graph.GeoGraph(data=None, nodes=None, **kwargs)[source]

Bases: Goulib.graph._Geo, networkx.classes.multigraph.MultiGraph

Undirected graph with nodes positions can be set to non multiedges anytime with attribute multi=False

Parameters:
  • data – see to_networkx_graph() for valid types
  • kwargs – other parameters will be copied as attributes, especially:
__init__(data=None, nodes=None, **kwargs)[source]
Parameters:
  • data – see to_networkx_graph() for valid types
  • kwargs – other parameters will be copied as attributes, especially:
__bool__()
Returns:True if graph has at least one node
__class__

alias of type

__contains__(n)

Return True if n is a node, False otherwise. Use the expression ‘n in G’.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> 1 in G
True
__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__eq__(other)
Returns:True if self and other are equal
__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__getitem__(n)

Return a dict of neighbors of node n. Use the expression ‘G[n]’.

n : node
A node in the graph.
adj_dict : dictionary
The adjacency dictionary for nodes connected to n.

G[n] is similar to G.neighbors(n) but the internal data dictionary is returned instead of a list.

Assigning G[n] will corrupt the internal graph data structure. Use G[n] for reading data only.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G[0]
{1: {}}
__gt__

Return self>value.

__hash__ = None
__iter__()

Iterate over the nodes. Use the expression ‘for n in G’.

niter : iterator
An iterator over all nodes in the graph.
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
__le__

Return self<=value.

__len__()

Return the number of nodes. Use the expression ‘len(G)’.

nnodes : int
The number of nodes in the graph.
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> len(G)
4
__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__nonzero__()
Returns:True if graph has at least one node
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

Return repr(self).

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__()
Returns:string representation, used mainly for logging and debugging
add_cycle(nodes, **attr)

Add a cycle.

nodes: iterable container
A container of nodes. A cycle will be constructed from the nodes (in order) and added to the graph.
attr : keyword arguments, optional (default= no attributes)
Attributes to add to every edge in cycle.

add_path, add_star

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_cycle([0,1,2,3])
>>> G.add_cycle([10,11,12],weight=7)
add_edge(u, v, k=None, attr_dict=None, **attrs)

add an edge to graph :return: edge data from created or existing edge

add_edges_from(ebunch, attr_dict=None, **attr)

Add all the edges in ebunch.

ebunch : container of edges

Each edge given in the container will be added to the graph. The edges can be:

  • 2-tuples (u,v) or
  • 3-tuples (u,v,d) for an edge attribute dict d, or
  • 4-tuples (u,v,k,d) for an edge identified by key k
attr_dict : dictionary, optional (default= no attributes)
Dictionary of edge attributes. Key/value pairs will update existing data associated with each edge.
attr : keyword arguments, optional
Edge data (or labels or objects) can be assigned using keyword arguments.

add_edge : add a single edge add_weighted_edges_from : convenient way to add weighted edges

Adding the same edge twice has no effect but any edge data will be updated when each duplicate edge is added.

Edge attributes specified in edges take precedence over attributes specified generally.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edges_from([(0,1),(1,2)]) # using a list of edge tuples
>>> e = zip(range(0,3),range(1,4))
>>> G.add_edges_from(e) # Add the path graph 0-1-2-3

Associate data to edges

>>> G.add_edges_from([(1,2),(2,3)], weight=3)
>>> G.add_edges_from([(3,4),(1,4)], label='WN2898')
add_node(p, attr_dict=None, **attr)

add a node or return one already very close :return (x,y,...) node id

add_nodes_from(nodes, **attr)

must be here because Graph.add_nodes_from doesn’t call add_node cleanly as it should...

add_path(nodes, **attr)

Add a path.

nodes : iterable container
A container of nodes. A path will be constructed from the nodes (in order) and added to the graph.
attr : keyword arguments, optional (default= no attributes)
Attributes to add to every edge in path.

add_star, add_cycle

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.add_path([10,11,12],weight=7)
add_star(nodes, **attr)

Add a star.

The first node in nodes is the middle of the star. It is connected to all other nodes.

nodes : iterable container
A container of nodes.
attr : keyword arguments, optional (default= no attributes)
Attributes to add to every edge in star.

add_path, add_cycle

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_star([0,1,2,3])
>>> G.add_star([10,11,12],weight=2)
add_weighted_edges_from(ebunch, weight='weight', **attr)

Add all the edges in ebunch as weighted edges with specified weights.

ebunch : container of edges
Each edge given in the list or container will be added to the graph. The edges must be given as 3-tuples (u,v,w) where w is a number.
weight : string, optional (default= ‘weight’)
The attribute name for the edge weights to be added.
attr : keyword arguments, optional (default= no attributes)
Edge attributes to add/update for all edges.

add_edge : add a single edge add_edges_from : add multiple edges

Adding the same edge twice for Graph/DiGraph simply updates the edge data. For MultiGraph/MultiDiGraph, duplicate edges are stored.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_weighted_edges_from([(0,1,3.0),(1,2,7.5)])
adjacency_iter()

Return an iterator of (node, adjacency dict) tuples for all nodes.

This is the fastest way to look at every edge. For directed graphs, only outgoing adjacencies are included.

adj_iter : iterator
An iterator of (node, adjacency dictionary) for all nodes in the graph.

adjacency_list

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> [(n,nbrdict) for n,nbrdict in G.adjacency_iter()]
[(0, {1: {}}), (1, {0: {}, 2: {}}), (2, {1: {}, 3: {}}), (3, {2: {}})]
adjacency_list()

Return an adjacency list representation of the graph.

The output adjacency list is in the order of G.nodes(). For directed graphs, only outgoing adjacencies are included.

adj_list : lists of lists
The adjacency structure of the graph as a list of lists.

adjacency_iter

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.adjacency_list() # in order given by G.nodes()
[[1], [0, 2], [1, 3], [2]]
adjlist_dict_factory

alias of dict

box()
Returns:nodes bounding box as (xmin,ymin,...),(xmax,ymax,...)
box_size()
Returns:(x,y) size
clear()
closest_edges(p, data=False)
Returns:container of edges close to p and distance
closest_nodes(p, n=1, skip=False)

nodes closest to a given position :param p: (x,y) position tuple :param skip: optional bool to skip n itself :return: list of nodes, minimal distance

contiguity(pts)
Returns:int number of points from pts already in graph
copy()
Returns:copy of self graph
degree(nbunch=None, weight=None)

Return the degree of a node or nodes.

The node degree is the number of edges adjacent to that node.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.
nd : dictionary, or number
A dictionary with nodes as keys and degree as values or a number if a single node is specified.
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.degree(0)
1
>>> G.degree([0,1])
{0: 1, 1: 2}
>>> list(G.degree([0,1]).values())
[1, 2]
degree_iter(nbunch=None, weight=None)

Return an iterator for (node, degree).

The node degree is the number of edges adjacent to the node.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.
nd_iter : an iterator
The iterator returns two-tuples of (node, degree).

degree

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> list(G.degree_iter(0)) # node 0 with degree 1
[(0, 1)]
>>> list(G.degree_iter([0,1]))
[(0, 1), (1, 2)]
dist(u, v)
Returns:float distance between nodes u and v
draw(**kwargs)

draw graph with default params

edge_attr_dict_factory

alias of dict

edge_key_dict_factory

alias of dict

edges(nbunch=None, data=False, keys=False, default=None)

Return a list of edges.

Edges are returned as tuples with optional data and keys in the order (node, neighbor, key, data).

nbunch : iterable container, optional (default= all nodes)
A container of nodes. The container will be iterated through once.
data : bool, optional (default=False)
Return two tuples (u,v) (False) or three-tuples (u,v,data) (True).
keys : bool, optional (default=False)
Return two tuples (u,v) (False) or three-tuples (u,v,key) (True).
edge_list: list of edge tuples
Edges that are adjacent to any node in nbunch, or a list of all edges if nbunch is not specified.

edges_iter : return an iterator over the edges

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

>>> G = nx.MultiGraph()  # or MultiDiGraph
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> G.edges()
[(0, 1), (1, 2), (2, 3)]
>>> G.edges(data=True) # default edge data is {} (empty dictionary)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1))
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> G.edges(keys=True) # default keys are integers
[(0, 1, 0), (1, 2, 0), (2, 3, 0)]
>>> G.edges(data=True,keys=True) # default keys are integers
[(0, 1, 0, {}), (1, 2, 0, {}), (2, 3, 0, {'weight': 5})]
>>> list(G.edges(data='weight',default=1,keys=True))
[(0, 1, 0, 1), (1, 2, 0, 1), (2, 3, 0, 5)]
>>> G.edges([0,3])
[(0, 1), (3, 2)]
>>> G.edges(0)
[(0, 1)]
edges_iter(nbunch=None, data=False, keys=False, default=None)

Return an iterator over the edges.

Edges are returned as tuples with optional data and keys in the order (node, neighbor, key, data).

nbunch : iterable container, optional (default= all nodes)
A container of nodes. The container will be iterated through once.
data : string or bool, optional (default=False)
The edge attribute returned in 3-tuple (u,v,ddict[data]). If True, return edge attribute dict in 3-tuple (u,v,ddict). If False, return 2-tuple (u,v).
default : value, optional (default=None)
Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.
keys : bool, optional (default=False)
If True, return edge keys with each edge.
edge_iter : iterator
An iterator of (u,v), (u,v,d) or (u,v,key,d) tuples of edges.

edges : return a list of edges

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

>>> G = nx.MultiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> [e for e in G.edges_iter()]
[(0, 1), (1, 2), (2, 3)]
>>> list(G.edges_iter(data=True)) # default data is {} (empty dict)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1))
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> list(G.edges(keys=True)) # default keys are integers
[(0, 1, 0), (1, 2, 0), (2, 3, 0)]
>>> list(G.edges(data=True,keys=True)) # default keys are integers
[(0, 1, 0, {}), (1, 2, 0, {}), (2, 3, 0, {'weight': 5})]
>>> list(G.edges(data='weight',default=1,keys=True))
[(0, 1, 0, 1), (1, 2, 0, 1), (2, 3, 0, 5)]
>>> list(G.edges_iter([0,3]))
[(0, 1), (3, 2)]
>>> list(G.edges_iter(0))
[(0, 1)]
get_edge_data(u, v, key=None, default=None)

Return the attribute dictionary associated with edge (u,v).

u, v : nodes

default : any Python object (default=None)
Value to return if the edge (u,v) is not found.
key : hashable identifier, optional (default=None)
Return data only for the edge with specified key.
edge_dict : dictionary
The edge attribute dictionary.

It is faster to use G[u][v][key].

>>> G = nx.MultiGraph() # or MultiDiGraph
>>> G.add_edge(0,1,key='a',weight=7)
>>> G[0][1]['a']  # key='a'
{'weight': 7}

Warning: Assigning G[u][v][key] corrupts the graph data structure. But it is safe to assign attributes to that dictionary,

>>> G[0][1]['a']['weight'] = 10
>>> G[0][1]['a']['weight']
10
>>> G[1][0]['a']['weight']
10
>>> G = nx.MultiGraph() # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.get_edge_data(0,1)
{0: {}}
>>> e = (0,1)
>>> G.get_edge_data(*e) # tuple form
{0: {}}
>>> G.get_edge_data('a','b',default=0) # edge not in graph, return 0
0
has_edge(u, v, key=None)

Return True if the graph has an edge between nodes u and v.

u, v : nodes
Nodes can be, for example, strings or numbers.
key : hashable identifier, optional (default=None)
If specified return True only if the edge with key is found.
edge_ind : bool
True if edge is in the graph, False otherwise.

Can be called either using two nodes u,v, an edge tuple (u,v), or an edge tuple (u,v,key).

>>> G = nx.MultiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.has_edge(0,1)  # using two nodes
True
>>> e = (0,1)
>>> G.has_edge(*e)  #  e is a 2-tuple (u,v)
True
>>> G.add_edge(0,1,key='a')
>>> G.has_edge(0,1,key='a')  # specify key
True
>>> e=(0,1,'a')
>>> G.has_edge(*e) # e is a 3-tuple (u,v,'a')
True

The following syntax are equivalent:

>>> G.has_edge(0,1)
True
>>> 1 in G[0]  # though this gives KeyError if 0 not in G
True
has_node(n)

Return True if the graph contains the node n.

n : node

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.has_node(0)
True

It is more readable and simpler to use

>>> 0 in G
True
html(**kwargs)
is_directed()

Return True if graph is directed, False otherwise.

is_multigraph()

used internally in constructor

length(edges=None)
Parameters:edges – iterator over edges either as (u,v,data) or (u,v,key,data). If None, all edges are taken
Returns:sum of ‘length’ attributes of edges
multi
name
nbunch_iter(nbunch=None)

Return an iterator of nodes contained in nbunch that are also in the graph.

The nodes in nbunch are checked for membership in the graph and if not are silently ignored.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
niter : iterator
An iterator over nodes in nbunch that are also in the graph. If nbunch is None, iterate over all nodes in the graph.
NetworkXError
If nbunch is not a node or or sequence of nodes. If a node in nbunch is not hashable.

Graph.__iter__

When nbunch is an iterator, the returned iterator yields values directly from nbunch, becoming exhausted when nbunch is exhausted.

To test whether nbunch is a single node, one can use “if nbunch in self:”, even after processing with this routine.

If nbunch is not a node or a (possibly empty) sequence/iterator or None, a NetworkXError is raised. Also, if any object in nbunch is not hashable, a NetworkXError is raised.

neighbors(n)

Return a list of the nodes connected to the node n.

n : node
A node in the graph
nlist : list
A list of nodes that are adjacent to n.
NetworkXError
If the node n is not in the graph.

It is usually more convenient (and faster) to access the adjacency dictionary as G[n]:

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge('a','b',weight=7)
>>> G['a']
{'b': {'weight': 7}}
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.neighbors(0)
[1]
neighbors_iter(n)

Return an iterator over all neighbors of node n.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> [n for n in G.neighbors_iter(0)]
[1]

It is faster to use the idiom “in G[0]”, e.g.

>>> G = nx.path_graph(4)
>>> [n for n in G[0]]
[1]
node_dict_factory

alias of dict

nodes(data=False)

Return a list of the nodes in the graph.

data : boolean, optional (default=False)
If False return a list of nodes. If True return a two-tuple of node and node data dictionary
nlist : list
A list of nodes. If data=True a list of two-tuples containing (node, node data dictionary).
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.nodes()
[0, 1, 2]
>>> G.add_node(1, time='5pm')
>>> G.nodes(data=True)
[(0, {}), (1, {'time': '5pm'}), (2, {})]
nodes_iter(data=False)

Return an iterator over the nodes.

data : boolean, optional (default=False)
If False the iterator returns nodes. If True return a two-tuple of node and node data dictionary
niter : iterator
An iterator over nodes. If data=True the iterator gives two-tuples containing (node, node data, dictionary)

If the node data is not required it is simpler and equivalent to use the expression ‘for n in G’.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> [d for n,d in G.nodes_iter(data=True)]
[{}, {}, {}]
nodes_with_selfloops()

Return a list of nodes with self loops.

A node with a self loop has an edge with both ends adjacent to that node.

nodelist : list
A list of nodes with self loops.

selfloop_edges, number_of_selfloops

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.nodes_with_selfloops()
[1]
number_of_edges(u=None, v=None)

Return the number of edges between two nodes.

u, v : nodes, optional (default=all edges)
If u and v are specified, return the number of edges between u and v. Otherwise return the total number of all edges.
nedges : int
The number of edges in the graph. If nodes u and v are specified return the number of edges between those nodes.

size

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.number_of_edges()
3
>>> G.number_of_edges(0,1)
1
>>> e = (0,1)
>>> G.number_of_edges(*e)
1
number_of_nodes(doublecheck=False)
number_of_selfloops()

Return the number of selfloop edges.

A selfloop edge has the same node at both ends.

nloops : int
The number of selfloops.

nodes_with_selfloops, selfloop_edges

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.number_of_selfloops()
1
order()

Return the number of nodes in the graph.

nnodes : int
The number of nodes in the graph.

number_of_nodes, __len__ which are identical

plot(**kwargs)

renders on IPython Notebook (alias to make usage more straightforward)

png(**kwargs)
pos(nodes=None)
Parameters:nodes – a single node, an iterator of all nodes if None
Returns:the position of node(s)
remove_edge(u, v=None, key=None, clean=False)
Parameters:
  • u – Node or Edge (Nodes tuple)
  • v – Node if u is a single Node
  • clean – bool removes disconnected nodes. must be False for certain nx algos to work
Result:

return attributes of removed edge

remove edge from graph. NetworkX graphs do not remove unused nodes

remove_edges_from(ebunch)

Remove all edges specified in ebunch.

ebunch: list or container of edge tuples

Each edge given in the list or container will be removed from the graph. The edges can be:

  • 2-tuples (u,v) All edges between u and v are removed.
  • 3-tuples (u,v,key) The edge identified by key is removed.
  • 4-tuples (u,v,key,data) where data is ignored.

remove_edge : remove a single edge

Will fail silently if an edge in ebunch is not in the graph.

>>> G = nx.MultiGraph() # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> ebunch=[(1,2),(2,3)]
>>> G.remove_edges_from(ebunch)

Removing multiple copies of edges

>>> G = nx.MultiGraph()
>>> G.add_edges_from([(1,2),(1,2),(1,2)])
>>> G.remove_edges_from([(1,2),(1,2)])
>>> G.edges()
[(1, 2)]
>>> G.remove_edges_from([(1,2),(1,2)]) # silently ignore extra copy
>>> G.edges() # now empty graph
[]
remove_node(n)
Parameters:n – node tuple

remove node from graph and rtree

remove_nodes_from(nodes)

Remove multiple nodes.

nodes : iterable container
A container of nodes (list, dict, set, etc.). If a node in the container is not in the graph it is silently ignored.

remove_node

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> e = G.nodes()
>>> e
[0, 1, 2]
>>> G.remove_nodes_from(e)
>>> G.nodes()
[]
render(fmt='svg', **kwargs)

render graph to bitmap stream :param fmt: string defining the format. ‘svg’ by default for INotepads :return: matplotlib figure as a byte stream in specified format

save(filename, **kwargs)

save graph in various formats

selfloop_edges(data=False, keys=False, default=None)

Return a list of selfloop edges.

A selfloop edge has the same node at both ends.

data : bool, optional (default=False)
Return selfloop edges as two tuples (u,v) (data=False) or three-tuples (u,v,datadict) (data=True) or three-tuples (u,v,datavalue) (data=’attrname’)
default : value, optional (default=None)
Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.
keys : bool, optional (default=False)
If True, return edge keys with each edge.
edgelist : list of edge tuples
A list of all selfloop edges.

nodes_with_selfloops, number_of_selfloops

>>> G = nx.MultiGraph()   # or MultiDiGraph
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.selfloop_edges()
[(1, 1)]
>>> G.selfloop_edges(data=True)
[(1, 1, {})]
>>> G.selfloop_edges(keys=True)
[(1, 1, 0)]
>>> G.selfloop_edges(keys=True, data=True)
[(1, 1, 0, {})]
shortest_path(source=None, target=None)
size(weight=None)

Return the number of edges.

weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1.
nedges : int
The number of edges or sum of edge weights in the graph.

number_of_edges

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.size()
3
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge('a','b',weight=2)
>>> G.add_edge('b','c',weight=4)
>>> G.size()
2
>>> G.size(weight='weight')
6.0
stats()
Returns:dict of graph data to use in __repr__ or usable otherwise
subgraph(nbunch)

Return the subgraph induced on nodes in nbunch.

The induced subgraph of the graph contains the nodes in nbunch and the edges between those nodes.

nbunch : list, iterable
A container of nodes which will be iterated through once.
G : Graph
A subgraph of the graph with the same edge attributes.

The graph, edge or node attributes just point to the original graph. So changes to the node or edge structure will not be reflected in the original graph while changes to the attributes will.

To create a subgraph with its own copy of the edge/node attributes use: nx.Graph(G.subgraph(nbunch))

If edge attributes are containers, a deep copy can be obtained using: G.subgraph(nbunch).copy()

For an inplace reduction of a graph to a subgraph you can remove nodes: G.remove_nodes_from([ n in G if n not in set(nbunch)])

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> H = G.subgraph([0,1,2])
>>> H.edges()
[(0, 1), (1, 2)]
svg(**kwargs)
to_directed()

Return a directed representation of the graph.

G : MultiDiGraph
A directed graph with the same name, same nodes, and with each edge (u,v,data) replaced by two directed edges (u,v,data) and (v,u,data).

This returns a “deepcopy” of the edge, node, and graph attributes which attempts to completely copy all of the data and references.

This is in contrast to the similar D=DiGraph(G) which returns a shallow copy of the data.

See the Python copy module for more information on shallow and deep copies, http://docs.python.org/library/copy.html.

Warning: If you have subclassed MultiGraph to use dict-like objects in the data structure, those changes do not transfer to the MultiDiGraph created by this method.

>>> G = nx.Graph()   # or MultiGraph, etc
>>> G.add_path([0,1])
>>> H = G.to_directed()
>>> H.edges()
[(0, 1), (1, 0)]

If already directed, return a (deep) copy

>>> G = nx.DiGraph()   # or MultiDiGraph, etc
>>> G.add_path([0,1])
>>> H = G.to_directed()
>>> H.edges()
[(0, 1)]
to_undirected()

Return an undirected copy of the graph.

G : Graph/MultiGraph
A deepcopy of the graph.

copy, add_edge, add_edges_from

This returns a “deepcopy” of the edge, node, and graph attributes which attempts to completely copy all of the data and references.

This is in contrast to the similar G=DiGraph(D) which returns a shallow copy of the data.

See the Python copy module for more information on shallow and deep copies, http://docs.python.org/library/copy.html.

>>> G = nx.Graph()   # or MultiGraph, etc
>>> G.add_path([0,1])
>>> H = G.to_directed()
>>> H.edges()
[(0, 1), (1, 0)]
>>> G2 = H.to_undirected()
>>> G2.edges()
[(0, 1)]
tol
class Goulib.graph.DiGraph(data=None, nodes=None, **kwargs)[source]

Bases: Goulib.graph._Geo, networkx.classes.multidigraph.MultiDiGraph

directed graph with nodes positions can be set to non multiedges anytime with attribute multi=False

Parameters:
  • data – see to_networkx_graph() for valid types
  • kwargs – other parameters will be copied as attributes, especially:
__init__(data=None, nodes=None, **kwargs)[source]
Parameters:
  • data – see to_networkx_graph() for valid types
  • kwargs – other parameters will be copied as attributes, especially:
__bool__()
Returns:True if graph has at least one node
__class__

alias of type

__contains__(n)

Return True if n is a node, False otherwise. Use the expression ‘n in G’.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> 1 in G
True
__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__eq__(other)
Returns:True if self and other are equal
__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__getitem__(n)

Return a dict of neighbors of node n. Use the expression ‘G[n]’.

n : node
A node in the graph.
adj_dict : dictionary
The adjacency dictionary for nodes connected to n.

G[n] is similar to G.neighbors(n) but the internal data dictionary is returned instead of a list.

Assigning G[n] will corrupt the internal graph data structure. Use G[n] for reading data only.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G[0]
{1: {}}
__gt__

Return self>value.

__hash__ = None
__iter__()

Iterate over the nodes. Use the expression ‘for n in G’.

niter : iterator
An iterator over all nodes in the graph.
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
__le__

Return self<=value.

__len__()

Return the number of nodes. Use the expression ‘len(G)’.

nnodes : int
The number of nodes in the graph.
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> len(G)
4
__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__nonzero__()
Returns:True if graph has at least one node
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

Return repr(self).

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__()
Returns:string representation, used mainly for logging and debugging
add_cycle(nodes, **attr)

Add a cycle.

nodes: iterable container
A container of nodes. A cycle will be constructed from the nodes (in order) and added to the graph.
attr : keyword arguments, optional (default= no attributes)
Attributes to add to every edge in cycle.

add_path, add_star

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_cycle([0,1,2,3])
>>> G.add_cycle([10,11,12],weight=7)
add_edge(u, v, k=None, attr_dict=None, **attrs)

add an edge to graph :return: edge data from created or existing edge

add_edges_from(ebunch, attr_dict=None, **attr)

Add all the edges in ebunch.

ebunch : container of edges

Each edge given in the container will be added to the graph. The edges can be:

  • 2-tuples (u,v) or
  • 3-tuples (u,v,d) for an edge attribute dict d, or
  • 4-tuples (u,v,k,d) for an edge identified by key k
attr_dict : dictionary, optional (default= no attributes)
Dictionary of edge attributes. Key/value pairs will update existing data associated with each edge.
attr : keyword arguments, optional
Edge data (or labels or objects) can be assigned using keyword arguments.

add_edge : add a single edge add_weighted_edges_from : convenient way to add weighted edges

Adding the same edge twice has no effect but any edge data will be updated when each duplicate edge is added.

Edge attributes specified in edges take precedence over attributes specified generally.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edges_from([(0,1),(1,2)]) # using a list of edge tuples
>>> e = zip(range(0,3),range(1,4))
>>> G.add_edges_from(e) # Add the path graph 0-1-2-3

Associate data to edges

>>> G.add_edges_from([(1,2),(2,3)], weight=3)
>>> G.add_edges_from([(3,4),(1,4)], label='WN2898')
add_node(p, attr_dict=None, **attr)

add a node or return one already very close :return (x,y,...) node id

add_nodes_from(nodes, **attr)

must be here because Graph.add_nodes_from doesn’t call add_node cleanly as it should...

add_path(nodes, **attr)

Add a path.

nodes : iterable container
A container of nodes. A path will be constructed from the nodes (in order) and added to the graph.
attr : keyword arguments, optional (default= no attributes)
Attributes to add to every edge in path.

add_star, add_cycle

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.add_path([10,11,12],weight=7)
add_star(nodes, **attr)

Add a star.

The first node in nodes is the middle of the star. It is connected to all other nodes.

nodes : iterable container
A container of nodes.
attr : keyword arguments, optional (default= no attributes)
Attributes to add to every edge in star.

add_path, add_cycle

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_star([0,1,2,3])
>>> G.add_star([10,11,12],weight=2)
add_weighted_edges_from(ebunch, weight='weight', **attr)

Add all the edges in ebunch as weighted edges with specified weights.

ebunch : container of edges
Each edge given in the list or container will be added to the graph. The edges must be given as 3-tuples (u,v,w) where w is a number.
weight : string, optional (default= ‘weight’)
The attribute name for the edge weights to be added.
attr : keyword arguments, optional (default= no attributes)
Edge attributes to add/update for all edges.

add_edge : add a single edge add_edges_from : add multiple edges

Adding the same edge twice for Graph/DiGraph simply updates the edge data. For MultiGraph/MultiDiGraph, duplicate edges are stored.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_weighted_edges_from([(0,1,3.0),(1,2,7.5)])
adjacency_iter()

Return an iterator of (node, adjacency dict) tuples for all nodes.

This is the fastest way to look at every edge. For directed graphs, only outgoing adjacencies are included.

adj_iter : iterator
An iterator of (node, adjacency dictionary) for all nodes in the graph.

adjacency_list

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> [(n,nbrdict) for n,nbrdict in G.adjacency_iter()]
[(0, {1: {}}), (1, {0: {}, 2: {}}), (2, {1: {}, 3: {}}), (3, {2: {}})]
adjacency_list()

Return an adjacency list representation of the graph.

The output adjacency list is in the order of G.nodes(). For directed graphs, only outgoing adjacencies are included.

adj_list : lists of lists
The adjacency structure of the graph as a list of lists.

adjacency_iter

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.adjacency_list() # in order given by G.nodes()
[[1], [0, 2], [1, 3], [2]]
adjlist_dict_factory

alias of dict

box()
Returns:nodes bounding box as (xmin,ymin,...),(xmax,ymax,...)
box_size()
Returns:(x,y) size
clear()
closest_edges(p, data=False)
Returns:container of edges close to p and distance
closest_nodes(p, n=1, skip=False)

nodes closest to a given position :param p: (x,y) position tuple :param skip: optional bool to skip n itself :return: list of nodes, minimal distance

contiguity(pts)
Returns:int number of points from pts already in graph
copy()
Returns:copy of self graph
degree(nbunch=None, weight=None)

Return the degree of a node or nodes.

The node degree is the number of edges adjacent to that node.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.
nd : dictionary, or number
A dictionary with nodes as keys and degree as values or a number if a single node is specified.
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.degree(0)
1
>>> G.degree([0,1])
{0: 1, 1: 2}
>>> list(G.degree([0,1]).values())
[1, 2]
degree_iter(nbunch=None, weight=None)

Return an iterator for (node, degree).

The node degree is the number of edges adjacent to the node.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights.
nd_iter : an iterator
The iterator returns two-tuples of (node, degree).

degree

>>> G = nx.MultiDiGraph()
>>> G.add_path([0,1,2,3])
>>> list(G.degree_iter(0)) # node 0 with degree 1
[(0, 1)]
>>> list(G.degree_iter([0,1]))
[(0, 1), (1, 2)]
dist(u, v)
Returns:float distance between nodes u and v
draw(**kwargs)

draw graph with default params

edge_attr_dict_factory

alias of dict

edge_key_dict_factory

alias of dict

edges(nbunch=None, data=False, keys=False, default=None)

Return a list of edges.

Edges are returned as tuples with optional data and keys in the order (node, neighbor, key, data).

nbunch : iterable container, optional (default= all nodes)
A container of nodes. The container will be iterated through once.
data : bool, optional (default=False)
Return two tuples (u,v) (False) or three-tuples (u,v,data) (True).
keys : bool, optional (default=False)
Return two tuples (u,v) (False) or three-tuples (u,v,key) (True).
edge_list: list of edge tuples
Edges that are adjacent to any node in nbunch, or a list of all edges if nbunch is not specified.

edges_iter : return an iterator over the edges

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

>>> G = nx.MultiGraph()  # or MultiDiGraph
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> G.edges()
[(0, 1), (1, 2), (2, 3)]
>>> G.edges(data=True) # default edge data is {} (empty dictionary)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1))
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> G.edges(keys=True) # default keys are integers
[(0, 1, 0), (1, 2, 0), (2, 3, 0)]
>>> G.edges(data=True,keys=True) # default keys are integers
[(0, 1, 0, {}), (1, 2, 0, {}), (2, 3, 0, {'weight': 5})]
>>> list(G.edges(data='weight',default=1,keys=True))
[(0, 1, 0, 1), (1, 2, 0, 1), (2, 3, 0, 5)]
>>> G.edges([0,3])
[(0, 1), (3, 2)]
>>> G.edges(0)
[(0, 1)]
edges_iter(nbunch=None, data=False, keys=False, default=None)

Return an iterator over the edges.

Edges are returned as tuples with optional data and keys in the order (node, neighbor, key, data).

nbunch : iterable container, optional (default= all nodes)
A container of nodes. The container will be iterated through once.
data : string or bool, optional (default=False)
The edge attribute returned in 3-tuple (u,v,ddict[data]). If True, return edge attribute dict in 3-tuple (u,v,ddict). If False, return 2-tuple (u,v).
keys : bool, optional (default=False)
If True, return edge keys with each edge.
default : value, optional (default=None)
Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.
edge_iter : iterator
An iterator of (u,v), (u,v,d) or (u,v,key,d) tuples of edges.

edges : return a list of edges

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

>>> G = nx.MultiDiGraph()
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> [e for e in G.edges_iter()]
[(0, 1), (1, 2), (2, 3)]
>>> list(G.edges_iter(data=True)) # default data is {} (empty dict)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1))
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> list(G.edges(keys=True)) # default keys are integers
[(0, 1, 0), (1, 2, 0), (2, 3, 0)]
>>> list(G.edges(data=True,keys=True)) # default keys are integers
[(0, 1, 0, {}), (1, 2, 0, {}), (2, 3, 0, {'weight': 5})]
>>> list(G.edges(data='weight',default=1,keys=True))
[(0, 1, 0, 1), (1, 2, 0, 1), (2, 3, 0, 5)]
>>> list(G.edges_iter([0,2]))
[(0, 1), (2, 3)]
>>> list(G.edges_iter(0))
[(0, 1)]
get_edge_data(u, v, key=None, default=None)

Return the attribute dictionary associated with edge (u,v).

u, v : nodes

default : any Python object (default=None)
Value to return if the edge (u,v) is not found.
key : hashable identifier, optional (default=None)
Return data only for the edge with specified key.
edge_dict : dictionary
The edge attribute dictionary.

It is faster to use G[u][v][key].

>>> G = nx.MultiGraph() # or MultiDiGraph
>>> G.add_edge(0,1,key='a',weight=7)
>>> G[0][1]['a']  # key='a'
{'weight': 7}

Warning: Assigning G[u][v][key] corrupts the graph data structure. But it is safe to assign attributes to that dictionary,

>>> G[0][1]['a']['weight'] = 10
>>> G[0][1]['a']['weight']
10
>>> G[1][0]['a']['weight']
10
>>> G = nx.MultiGraph() # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.get_edge_data(0,1)
{0: {}}
>>> e = (0,1)
>>> G.get_edge_data(*e) # tuple form
{0: {}}
>>> G.get_edge_data('a','b',default=0) # edge not in graph, return 0
0
has_edge(u, v, key=None)

Return True if the graph has an edge between nodes u and v.

u, v : nodes
Nodes can be, for example, strings or numbers.
key : hashable identifier, optional (default=None)
If specified return True only if the edge with key is found.
edge_ind : bool
True if edge is in the graph, False otherwise.

Can be called either using two nodes u,v, an edge tuple (u,v), or an edge tuple (u,v,key).

>>> G = nx.MultiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.has_edge(0,1)  # using two nodes
True
>>> e = (0,1)
>>> G.has_edge(*e)  #  e is a 2-tuple (u,v)
True
>>> G.add_edge(0,1,key='a')
>>> G.has_edge(0,1,key='a')  # specify key
True
>>> e=(0,1,'a')
>>> G.has_edge(*e) # e is a 3-tuple (u,v,'a')
True

The following syntax are equivalent:

>>> G.has_edge(0,1)
True
>>> 1 in G[0]  # though this gives KeyError if 0 not in G
True
has_node(n)

Return True if the graph contains the node n.

n : node

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.has_node(0)
True

It is more readable and simpler to use

>>> 0 in G
True
has_predecessor(u, v)[source]

Return True if node u has predecessor v.

This is true if graph has the edge u<-v.

has_successor(u, v)[source]

Return True if node u has successor v.

This is true if graph has the edge u->v.

html(**kwargs)
in_degree(nbunch=None, weight=None)[source]

Return the in-degree of a node or nodes.

The node in-degree is the number of edges pointing in to the node.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.
nd : dictionary, or number
A dictionary with nodes as keys and in-degree as values or a number if a single node is specified.

degree, out_degree, in_degree_iter

>>> G = nx.DiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.in_degree(0)
0
>>> G.in_degree([0,1])
{0: 0, 1: 1}
>>> list(G.in_degree([0,1]).values())
[0, 1]
in_degree_iter(nbunch=None, weight=None)

Return an iterator for (node, in-degree).

The node in-degree is the number of edges pointing in to the node.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.
nd_iter : an iterator
The iterator returns two-tuples of (node, in-degree).

degree, in_degree, out_degree, out_degree_iter

>>> G = nx.MultiDiGraph()
>>> G.add_path([0,1,2,3])
>>> list(G.in_degree_iter(0)) # node 0 with degree 0
[(0, 0)]
>>> list(G.in_degree_iter([0,1]))
[(0, 0), (1, 1)]
in_edges(nbunch=None, keys=False, data=False)

Return a list of the incoming edges.

nbunch : iterable container, optional (default= all nodes)
A container of nodes. The container will be iterated through once.
data : bool, optional (default=False)
If True, return edge attribute dict with each edge.
keys : bool, optional (default=False)
If True, return edge keys with each edge.
in_edges : list
A list of (u,v), (u,v,d) or (u,v,key,d) tuples of edges.

out_edges: return a list of outgoing edges

in_edges_iter(nbunch=None, data=False, keys=False)

Return an iterator over the incoming edges.

nbunch : iterable container, optional (default= all nodes)
A container of nodes. The container will be iterated through once.
data : bool, optional (default=False)
If True, return edge attribute dict with each edge.
keys : bool, optional (default=False)
If True, return edge keys with each edge.
in_edge_iter : iterator
An iterator of (u,v), (u,v,d) or (u,v,key,d) tuples of edges.

edges_iter : return an iterator of edges

is_directed()

Return True if graph is directed, False otherwise.

is_multigraph()

used internally in constructor

length(edges=None)
Parameters:edges – iterator over edges either as (u,v,data) or (u,v,key,data). If None, all edges are taken
Returns:sum of ‘length’ attributes of edges
multi
name
nbunch_iter(nbunch=None)

Return an iterator of nodes contained in nbunch that are also in the graph.

The nodes in nbunch are checked for membership in the graph and if not are silently ignored.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
niter : iterator
An iterator over nodes in nbunch that are also in the graph. If nbunch is None, iterate over all nodes in the graph.
NetworkXError
If nbunch is not a node or or sequence of nodes. If a node in nbunch is not hashable.

Graph.__iter__

When nbunch is an iterator, the returned iterator yields values directly from nbunch, becoming exhausted when nbunch is exhausted.

To test whether nbunch is a single node, one can use “if nbunch in self:”, even after processing with this routine.

If nbunch is not a node or a (possibly empty) sequence/iterator or None, a NetworkXError is raised. Also, if any object in nbunch is not hashable, a NetworkXError is raised.

neighbors(n)

Return a list of successor nodes of n.

neighbors() and successors() are the same function.

neighbors_iter(n)

Return an iterator over successor nodes of n.

neighbors_iter() and successors_iter() are the same.

node_dict_factory

alias of dict

nodes(data=False)

Return a list of the nodes in the graph.

data : boolean, optional (default=False)
If False return a list of nodes. If True return a two-tuple of node and node data dictionary
nlist : list
A list of nodes. If data=True a list of two-tuples containing (node, node data dictionary).
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G.nodes()
[0, 1, 2]
>>> G.add_node(1, time='5pm')
>>> G.nodes(data=True)
[(0, {}), (1, {'time': '5pm'}), (2, {})]
nodes_iter(data=False)

Return an iterator over the nodes.

data : boolean, optional (default=False)
If False the iterator returns nodes. If True return a two-tuple of node and node data dictionary
niter : iterator
An iterator over nodes. If data=True the iterator gives two-tuples containing (node, node data, dictionary)

If the node data is not required it is simpler and equivalent to use the expression ‘for n in G’.

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> [d for n,d in G.nodes_iter(data=True)]
[{}, {}, {}]
nodes_with_selfloops()

Return a list of nodes with self loops.

A node with a self loop has an edge with both ends adjacent to that node.

nodelist : list
A list of nodes with self loops.

selfloop_edges, number_of_selfloops

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.nodes_with_selfloops()
[1]
number_of_edges(u=None, v=None)

Return the number of edges between two nodes.

u, v : nodes, optional (default=all edges)
If u and v are specified, return the number of edges between u and v. Otherwise return the total number of all edges.
nedges : int
The number of edges in the graph. If nodes u and v are specified return the number of edges between those nodes.

size

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.number_of_edges()
3
>>> G.number_of_edges(0,1)
1
>>> e = (0,1)
>>> G.number_of_edges(*e)
1
number_of_nodes(doublecheck=False)
number_of_selfloops()

Return the number of selfloop edges.

A selfloop edge has the same node at both ends.

nloops : int
The number of selfloops.

nodes_with_selfloops, selfloop_edges

>>> G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.number_of_selfloops()
1
order()

Return the number of nodes in the graph.

nnodes : int
The number of nodes in the graph.

number_of_nodes, __len__ which are identical

out_degree(nbunch=None, weight=None)[source]

Return the out-degree of a node or nodes.

The node out-degree is the number of edges pointing out of the node.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node.
nd : dictionary, or number
A dictionary with nodes as keys and out-degree as values or a number if a single node is specified.
>>> G = nx.DiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.out_degree(0)
1
>>> G.out_degree([0,1])
{0: 1, 1: 1}
>>> list(G.out_degree([0,1]).values())
[1, 1]
out_degree_iter(nbunch=None, weight=None)

Return an iterator for (node, out-degree).

The node out-degree is the number of edges pointing out of the node.

nbunch : iterable container, optional (default=all nodes)
A container of nodes. The container will be iterated through once.
weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights.
nd_iter : an iterator
The iterator returns two-tuples of (node, out-degree).

degree, in_degree, out_degree, in_degree_iter

>>> G = nx.MultiDiGraph()
>>> G.add_path([0,1,2,3])
>>> list(G.out_degree_iter(0)) # node 0 with degree 1
[(0, 1)]
>>> list(G.out_degree_iter([0,1]))
[(0, 1), (1, 1)]
out_edges(nbunch=None, keys=False, data=False)

Return a list of the outgoing edges.

Edges are returned as tuples with optional data and keys in the order (node, neighbor, key, data).

nbunch : iterable container, optional (default= all nodes)
A container of nodes. The container will be iterated through once.
data : bool, optional (default=False)
If True, return edge attribute dict with each edge.
keys : bool, optional (default=False)
If True, return edge keys with each edge.
out_edges : list
An listr of (u,v), (u,v,d) or (u,v,key,d) tuples of edges.

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs edges() is the same as out_edges().

in_edges: return a list of incoming edges

out_edges_iter(nbunch=None, data=False, keys=False, default=None)

Return an iterator over the edges.

Edges are returned as tuples with optional data and keys in the order (node, neighbor, key, data).

nbunch : iterable container, optional (default= all nodes)
A container of nodes. The container will be iterated through once.
data : string or bool, optional (default=False)
The edge attribute returned in 3-tuple (u,v,ddict[data]). If True, return edge attribute dict in 3-tuple (u,v,ddict). If False, return 2-tuple (u,v).
keys : bool, optional (default=False)
If True, return edge keys with each edge.
default : value, optional (default=None)
Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.
edge_iter : iterator
An iterator of (u,v), (u,v,d) or (u,v,key,d) tuples of edges.

edges : return a list of edges

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

>>> G = nx.MultiDiGraph()
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> [e for e in G.edges_iter()]
[(0, 1), (1, 2), (2, 3)]
>>> list(G.edges_iter(data=True)) # default data is {} (empty dict)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1))
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> list(G.edges(keys=True)) # default keys are integers
[(0, 1, 0), (1, 2, 0), (2, 3, 0)]
>>> list(G.edges(data=True,keys=True)) # default keys are integers
[(0, 1, 0, {}), (1, 2, 0, {}), (2, 3, 0, {'weight': 5})]
>>> list(G.edges(data='weight',default=1,keys=True))
[(0, 1, 0, 1), (1, 2, 0, 1), (2, 3, 0, 5)]
>>> list(G.edges_iter([0,2]))
[(0, 1), (2, 3)]
>>> list(G.edges_iter(0))
[(0, 1)]
plot(**kwargs)

renders on IPython Notebook (alias to make usage more straightforward)

png(**kwargs)
pos(nodes=None)
Parameters:nodes – a single node, an iterator of all nodes if None
Returns:the position of node(s)
predecessors(n)[source]

Return a list of predecessor nodes of n.

predecessors_iter(n)[source]

Return an iterator over predecessor nodes of n.

remove_edge(u, v=None, key=None, clean=False)
Parameters:
  • u – Node or Edge (Nodes tuple)
  • v – Node if u is a single Node
  • clean – bool removes disconnected nodes. must be False for certain nx algos to work
Result:

return attributes of removed edge

remove edge from graph. NetworkX graphs do not remove unused nodes

remove_edges_from(ebunch)

Remove all edges specified in ebunch.

ebunch: list or container of edge tuples

Each edge given in the list or container will be removed from the graph. The edges can be:

  • 2-tuples (u,v) All edges between u and v are removed.
  • 3-tuples (u,v,key) The edge identified by key is removed.
  • 4-tuples (u,v,key,data) where data is ignored.

remove_edge : remove a single edge

Will fail silently if an edge in ebunch is not in the graph.

>>> G = nx.MultiGraph() # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> ebunch=[(1,2),(2,3)]
>>> G.remove_edges_from(ebunch)

Removing multiple copies of edges

>>> G = nx.MultiGraph()
>>> G.add_edges_from([(1,2),(1,2),(1,2)])
>>> G.remove_edges_from([(1,2),(1,2)])
>>> G.edges()
[(1, 2)]
>>> G.remove_edges_from([(1,2),(1,2)]) # silently ignore extra copy
>>> G.edges() # now empty graph
[]
remove_node(n)
Parameters:n – node tuple

remove node from graph and rtree

remove_nodes_from(nbunch)[source]

Remove multiple nodes.

nodes : iterable container
A container of nodes (list, dict, set, etc.). If a node in the container is not in the graph it is silently ignored.

remove_node

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2])
>>> e = G.nodes()
>>> e
[0, 1, 2]
>>> G.remove_nodes_from(e)
>>> G.nodes()
[]
render(fmt='svg', **kwargs)

render graph to bitmap stream :param fmt: string defining the format. ‘svg’ by default for INotepads :return: matplotlib figure as a byte stream in specified format

reverse(copy=True)

Return the reverse of the graph.

The reverse is a graph with the same nodes and edges but with the directions of the edges reversed.

copy : bool optional (default=True)
If True, return a new DiGraph holding the reversed edges. If False, reverse the reverse graph is created using the original graph (this changes the original graph).
save(filename, **kwargs)

save graph in various formats

selfloop_edges(data=False, keys=False, default=None)

Return a list of selfloop edges.

A selfloop edge has the same node at both ends.

data : bool, optional (default=False)
Return selfloop edges as two tuples (u,v) (data=False) or three-tuples (u,v,datadict) (data=True) or three-tuples (u,v,datavalue) (data=’attrname’)
default : value, optional (default=None)
Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.
keys : bool, optional (default=False)
If True, return edge keys with each edge.
edgelist : list of edge tuples
A list of all selfloop edges.

nodes_with_selfloops, number_of_selfloops

>>> G = nx.MultiGraph()   # or MultiDiGraph
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.selfloop_edges()
[(1, 1)]
>>> G.selfloop_edges(data=True)
[(1, 1, {})]
>>> G.selfloop_edges(keys=True)
[(1, 1, 0)]
>>> G.selfloop_edges(keys=True, data=True)
[(1, 1, 0, {})]
shortest_path(source=None, target=None)
size(weight=None)

Return the number of edges.

weight : string or None, optional (default=None)
The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1.
nedges : int
The number of edges or sum of edge weights in the graph.

number_of_edges

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> G.size()
3
>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge('a','b',weight=2)
>>> G.add_edge('b','c',weight=4)
>>> G.size()
2
>>> G.size(weight='weight')
6.0
stats()
Returns:dict of graph data to use in __repr__ or usable otherwise
subgraph(nbunch)

Return the subgraph induced on nodes in nbunch.

The induced subgraph of the graph contains the nodes in nbunch and the edges between those nodes.

nbunch : list, iterable
A container of nodes which will be iterated through once.
G : Graph
A subgraph of the graph with the same edge attributes.

The graph, edge or node attributes just point to the original graph. So changes to the node or edge structure will not be reflected in the original graph while changes to the attributes will.

To create a subgraph with its own copy of the edge/node attributes use: nx.Graph(G.subgraph(nbunch))

If edge attributes are containers, a deep copy can be obtained using: G.subgraph(nbunch).copy()

For an inplace reduction of a graph to a subgraph you can remove nodes: G.remove_nodes_from([ n in G if n not in set(nbunch)])

>>> G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_path([0,1,2,3])
>>> H = G.subgraph([0,1,2])
>>> H.edges()
[(0, 1), (1, 2)]
successors(n)[source]

Return a list of successor nodes of n.

neighbors() and successors() are the same function.

successors_iter(n)[source]

Return an iterator over successor nodes of n.

neighbors_iter() and successors_iter() are the same.

svg(**kwargs)
to_directed()

Return a directed copy of the graph.

G : MultiDiGraph
A deepcopy of the graph.

If edges in both directions (u,v) and (v,u) exist in the graph, attributes for the new undirected edge will be a combination of the attributes of the directed edges. The edge data is updated in the (arbitrary) order that the edges are encountered. For more customized control of the edge attributes use add_edge().

This returns a “deepcopy” of the edge, node, and graph attributes which attempts to completely copy all of the data and references.

This is in contrast to the similar G=DiGraph(D) which returns a shallow copy of the data.

See the Python copy module for more information on shallow and deep copies, http://docs.python.org/library/copy.html.

>>> G = nx.Graph()   # or MultiGraph, etc
>>> G.add_path([0,1])
>>> H = G.to_directed()
>>> H.edges()
[(0, 1), (1, 0)]

If already directed, return a (deep) copy

>>> G = nx.MultiDiGraph()
>>> G.add_path([0,1])
>>> H = G.to_directed()
>>> H.edges()
[(0, 1)]
to_undirected(reciprocal=False)

Return an undirected representation of the digraph.

reciprocal : bool (optional)
If True only keep edges that appear in both directions in the original digraph.
G : MultiGraph
An undirected graph with the same name and nodes and with edge (u,v,data) if either (u,v,data) or (v,u,data) is in the digraph. If both edges exist in digraph and their edge data is different, only one edge is created with an arbitrary choice of which edge data to use. You must check and correct for this manually if desired.

This returns a “deepcopy” of the edge, node, and graph attributes which attempts to completely copy all of the data and references.

This is in contrast to the similar D=DiGraph(G) which returns a shallow copy of the data.

See the Python copy module for more information on shallow and deep copies, http://docs.python.org/library/copy.html.

Warning: If you have subclassed MultiGraph to use dict-like objects in the data structure, those changes do not transfer to the MultiDiGraph created by this method.

tol
Goulib.graph.figure(g, box=None, **kwargs)[source]
Parameters:
  • g – _Geo derived Graph
  • box – optional interval.Box if g has no box
Returns:

matplotlib axis suitable for drawing graph g

Goulib.graph.draw_networkx(g, pos=None, **kwargs)[source]

improves nx.draw_networkx :param g: NetworkX Graph :param pos: can be either :

  • optional dictionary of (x,y) node positions
  • function of the form lambda node:(x,y) that maps node positions.
  • None. in this case, nodes are directly used as positions if graph is a GeoGraph, otherwise nx.draw_shell is used
Parameters:**kwargs

passed to nx.draw method as described in http://networkx.lanl.gov/reference/generated/networkx.drawing.nx_pylab.draw_networkx.html with one tweak:

  • if edge_color is a function of the form lambda data:color string, it is mapped over all edges
Goulib.graph.to_drawing(g, d=None, edges=[])[source]

draws Graph to a Drawing :param g: Graph :param d: existing Drawing to draw onto, or None to create a new Drawing :param edges: iterable of edges (with data) that will be added, in the same order. By default all edges are drawn :return: Drawing

Graph edges with an ‘entity’ property

Goulib.graph.write_dxf(g, filename)[source]

writes networkx.Graph in .dxf format

Goulib.graph.write_dot(g, filename)[source]
Goulib.graph.to_json(g, **kwargs)[source]
Returns:string JSON representation of a graph
Goulib.graph.write_json(g, filename, **kwargs)[source]

write a JSON file, suitable for D*.js representation

Goulib.graph.delauney_triangulation(nodes, qhull_options='', incremental=False, **kwargs)[source]

https://en.wikipedia.org/wiki/Delaunay_triangulation :param nodes: _Geo graph or list of (x,y) or (x,y,z) node positions :param qhull_options: string passed to scipy.spatial.Delaunay(), which passes it to Qhull ( http://www.qhull.org/ ) *‘Qt’ ensures all points are connected *‘Qz’ required when nodes lie on a sphere *‘QJ’ solves some singularity situations

Parameters:kwargs – passed to the GeoGraph constructor
Returns:GeoGraph with delauney triangulation between nodes
Goulib.graph.euclidean_minimum_spanning_tree(nodes, **kwargs)[source]
Parameters:nodes – list of (x,y) nodes positions
Returns:GeoGraph with minimum spanning tree between nodes

see https://en.wikipedia.org/wiki/Euclidean_minimum_spanning_tree

Goulib.graph.points_on_sphere(N)[source]