GraphView#
- class graph_tool.GraphView(g, vfilt=None, efilt=None, directed=None, reversed=False, skip_properties=False, skip_vfilt=False, skip_efilt=False)[source]#
Bases:
Graph
A view of selected vertices or edges of another graph.
This class uses shared data from another
Graph
instance, but allows for local filtering of vertices and/or edges, edge directionality or reversal. See Graph views for more details and examples.The existence of a
GraphView
object does not affect the original graph, except if the graph view is modified (addition or removal of vertices or edges), in which case the modification is directly reflected in the original graph (and vice-versa), since they both point to the same underlying data. Because of this, instances ofPropertyMap
can be used interchangeably with a graph and its views.The argument
g
must be an instance of aGraph
class. If specified,vfilt
andefilt
select which vertices and edges are filtered, respectively. These parameters can either be a boolean-valuedPropertyMap
ornumpy.ndarray
, which specify which vertices/edges are selected, or an unary function that returnsTrue
if a given vertex/edge is to be selected, orFalse
otherwise.The boolean parameter
directed
can be used to set the directionality of the graph view. Ifdirected is None
, the directionality is inherited fromg
.If
reversed == True
, the direction of the edges is reversed.If
vfilt
orefilt
is anything other than aPropertyMap
instance, the instantiation running time is \(O(V)\) and \(O(E)\), respectively. Otherwise, the running time is \(O(1)\).If either
skip_properties
,skip_vfilt
orskip_efilt
isTrue
, then the internal properties, vertex filter or edge filter of the original graph are ignored, respectively.Methods
add_edge
(source, target[, add_missing])Add a new edge from
source
totarget
to the graph, and return it.add_edge_list
(edge_list[, hashed, ...])Add a list of edges to the graph, given by
edge_list
, which can be an iterator of(source, target)
pairs where bothsource
andtarget
are vertex indices (or can be so converted), or anumpy.ndarray
of shape(E,2)
, whereE
is the number of edges, and each line specifies a(source, target)
pair.add_vertex
([n])Add a vertex to the graph, and return it.
clear
()Remove all vertices and edges from the graph.
Remove all edges from the graph.
Remove vertex and edge filters, and set the graph to the unfiltered state.
clear_vertex
(vertex)Remove all in and out-edges from the given vertex.
copy
()Return a deep copy of self.
copy_property
(src[, tgt, value_type, g, full])Copy contents of
src
property totgt
property.degree_property_map
(deg[, weight])Create and return a vertex property map containing the degree type given by
deg
, which can be any of"in"
,"out"
, or"total"
.edge
(s, t[, all_edges, add_missing])Return the edge from vertex
s
tot
, if it exists.edges
()Return an
iterator
over the edges.get_all_edges
(v[, eprops])Return a
numpy.ndarray
containing the in- and out-edges of vertex v, and optional edge properties listeprops
.get_all_neighbors
(v[, vprops])Return a
numpy.ndarray
containing the in-neighbors and out-neighbors of vertexv
, and optional vertex properties listvprops
.get_all_neighbours
(v[, vprops])Return a
numpy.ndarray
containing the in-neighbors and out-neighbors of vertexv
, and optional vertex properties listvprops
.Return a tuple with the edge filter property and bool value indicating whether or not it is inverted.
get_edges
([eprops])Return a
numpy.ndarray
containing the edges, and optional edge properties listeprops
.Return whether the fast \(O(1)\) removal of edges is currently enabled.
Return a copy of the filter state of the graph.
get_in_degrees
(vs[, eweight])Return a
numpy.ndarray
containing the in-degrees of vertex listvs
.get_in_edges
(v[, eprops])Return a
numpy.ndarray
containing the in-edges of vertexv
, and optional edge properties listeprops
.get_in_neighbors
(v[, vprops])Return a
numpy.ndarray
containing the in-neighbors of vertexv
, and optional vertex properties listvprops
.get_in_neighbours
(v[, vprops])Return a
numpy.ndarray
containing the in-neighbors of vertexv
, and optional vertex properties listvprops
.get_out_degrees
(vs[, eweight])Return a
numpy.ndarray
containing the out-degrees of vertex listvs
.get_out_edges
(v[, eprops])Return a
numpy.ndarray
containing the out-edges of vertexv
, and optional edge properties listeprops
.get_out_neighbors
(v[, vprops])Return a
numpy.ndarray
containing the out-neighbors of vertexv
, and optional vertex properties listvprops
.get_out_neighbours
(v[, vprops])Return a
numpy.ndarray
containing the out-neighbors of vertexv
, and optional vertex properties listvprops
.get_total_degrees
(vs[, eweight])Return a
numpy.ndarray
containing the total degrees (i.e. in- plus out-degree) of vertex listvs
.Return a tuple with the vertex filter property and bool value indicating whether or not it is inverted.
get_vertices
([vprops])Return a
numpy.ndarray
containing the vertex indices, and optional vertex properties listvprops
.Get the directedness of the graph.
Return
True
if the edges are reversed, andFalse
otherwise.iter_all_edges
(v[, eprops])Return an iterator over the in- and out-edge
`(source, target)
pairs for vertexv
, and optional edge properties listeprops
.iter_all_neighbors
(v[, vprops])Return an iterator over the in- and out-neighbors of vertex
v
, and optional vertex properties listvprops
.iter_all_neighbours
(v[, vprops])Return an iterator over the in- and out-neighbors of vertex
v
, and optional vertex properties listvprops
.iter_edges
([eprops])Return an iterator over the edge
`(source, target)
pairs, and optional edge properties listeprops
.iter_in_edges
(v[, eprops])Return an iterator over the in-edge
`(source, target)
pairs for vertexv
, and optional edge properties listeprops
.iter_in_neighbors
(v[, vprops])Return an iterator over the in-neighbors of vertex
v
, and optional vertex properties listvprops
.iter_in_neighbours
(v[, vprops])Return an iterator over the in-neighbors of vertex
v
, and optional vertex properties listvprops
.iter_out_edges
(v[, eprops])Return an iterator over the out-edge
`(source, target)
pairs for vertexv
, and optional edge properties listeprops
.iter_out_neighbors
(v[, vprops])Return an iterator over the out-neighbors of vertex
v
, and optional vertex properties listvprops
.iter_out_neighbours
(v[, vprops])Return an iterator over the out-neighbors of vertex
v
, and optional vertex properties listvprops
.iter_vertices
([vprops])Return an iterator over the vertex indices, and optional vertex properties list
vprops
.Print a list of all internal properties.
load
(file_name[, fmt, ignore_vp, ignore_ep, ...])Load graph from
file_name
(which can be either a string or a file-like object).new_edge_property
(value_type[, vals, val])Create a new edge property map of type
value_type
, and return it.new_ep
(value_type[, vals, val])Alias to
new_edge_property()
.new_gp
(value_type[, val])Alias to
new_graph_property()
.new_graph_property
(value_type[, val])Create a new graph property map of type
value_type
, and return it.new_property
(key_type, value_type[, vals])Create a new (uninitialized) vertex property map of key type
key_type
(v
,e
org
), value typevalue_type
, and return it.new_vertex_property
(value_type[, vals, val])Create a new vertex property map of type
value_type
, and return it.new_vp
(value_type[, vals, val])Alias to
new_vertex_property()
.num_edges
([ignore_filter])Get the number of edges.
num_vertices
([ignore_filter])Get the number of vertices.
own_property
(prop)Return a version of the property map 'prop' (possibly belonging to another graph) which is owned by the current graph.
Remove all edges of the graph which are currently being filtered out.
purge_vertices
([in_place])Remove all vertices of the graph which are currently being filtered out.
Reset the edge indices so that they lie in the [0,
num_edges()
- 1] range.remove_edge
(edge)Remove an edge from the graph.
remove_vertex
(vertex[, fast])Remove a vertex from the graph.
save
(file_name[, fmt])Save graph to
file_name
(which can be either a string or a file-like object).set_directed
(is_directed)Set the directedness of the graph.
set_edge_filter
(prop[, inverted])Set the edge boolean filter property.
set_fast_edge_removal
([fast])If
fast == True
the fast \(O(1)\) removal of edges will be enabled.set_filter_state
(state)Set the filter state of the graph.
set_filters
(eprop, vprop[, inverted_edges, ...])Set the boolean properties for edge and vertex filters, respectively.
set_reversed
(is_reversed)Reverse the direction of the edges, if
is_reversed
isTrue
, or maintain the original direction otherwise.set_vertex_filter
(prop[, inverted])Set the vertex boolean filter property.
Force the physical capacity of the underlying containers to match the graph's actual size, potentially freeing memory back to the system.
vertex
(i[, use_index, add_missing])Return the vertex with index
i
.vertices
()Return an
iterator
over the vertices.Attributes
Base graph.
Edge index map.
The size of the range of edge indices.
Dictionary of internal edge properties.
Alias to
edge_properties
.Alias to
graph_properties
.Dictionary of internal graph properties.
Dictionary of internal properties.
Vertex index map.
Dictionary of internal vertex properties.
Alias to
vertex_properties
.- add_edge(source, target, add_missing=True)#
Add a new edge from
source
totarget
to the graph, and return it. This operation is \(O(1)\).If
add_missing == True
, the source and target vertices are included in the graph if they don’t yet exist.
- add_edge_list(edge_list, hashed=False, hash_type='string', eprops=None)#
Add a list of edges to the graph, given by
edge_list
, which can be an iterator of(source, target)
pairs where bothsource
andtarget
are vertex indices (or can be so converted), or anumpy.ndarray
of shape(E,2)
, whereE
is the number of edges, and each line specifies a(source, target)
pair. If the list references vertices which do not exist in the graph, they will be created.Optionally, if
hashed == True
, the vertex values in the edge list are not assumed to correspond to vertex indices directly. In this case they will be mapped to vertex indices according to the order in which they are encountered, and a vertex property map with the vertex values is returned. The optionhash_type
will determine the expected type used by the hash keys, and they can be any property map value type (seePropertyMap
), unlessedge_list
is anumpy.ndarray
, in which case the value of this option is ignored, and the type is determined automatically.If
hashed == False
and the target value of an edge corresponds to the maximum interger value (sys.maxsize
, or the maximum integer type of thenumpy.ndarray
object), or is anumpy.nan
ornumpy.inf
value, then only the source vertex will be added to the graph.If
hashed == True
, and the target value corresponds toNone
, then only the source vertex will be added to the graph.If given,
eprops
should specify an iterable containing edge property maps that will be filled with the remaining values at each row, if there are more than two. Alternatively,eprops
can contain a list of(name, value_type)
pairs, in which case new internal dege property maps will be created with the corresponding name name and value type.Note
If
edge_list
is anumpy.ndarray
object, the execution of this function will be done entirely in C++, and hence much faster.Examples
>>> edge_list = [(0, 1, .3, 10), (2, 3, .1, 0), (2, 0, .4, 42)] >>> g = gt.Graph() >>> eweight = g.new_ep("double") >>> elayer = g.new_ep("int") >>> g.add_edge_list(edge_list, eprops=[eweight, elayer]) >>> print(eweight.fa) [0.3 0.1 0.4] >>> g.get_edges() array([[0, 1], [2, 3], [2, 0]])
- add_vertex(n=1)#
Add a vertex to the graph, and return it. If
n != 1
,n
vertices are inserted and an iterator over the new vertices is returned. This operation is \(O(n)\).
- clear()#
Remove all vertices and edges from the graph.
- clear_edges()#
Remove all edges from the graph.
- clear_filters()#
Remove vertex and edge filters, and set the graph to the unfiltered state.
Note
This is a \(O(1)\) operation that does not modify the storage of the graph.
Warning
Clearing vertex and edge filters will invalidate existing vertex and edge descriptors.
- clear_vertex(vertex)#
Remove all in and out-edges from the given vertex.
- copy()#
Return a deep copy of self. All internal property maps are also copied.
- copy_property(src, tgt=None, value_type=None, g=None, full=True)#
Copy contents of
src
property totgt
property. Iftgt
is None, then a new property map of the same type (or with the type given by the optionalvalue_type
parameter) is created, and returned. The optional parameterg
specifies the source graph to copy properties from (defaults to the graph than owns src). Iffull == False
, then in the case of filtered graphs only the unmasked values are copied (with the remaining ones taking the type-dependent default value).Note
In case the source property map belongs to different graphs, this function behaves as follows.
For vertex properties, the source and target graphs must have the same number of vertices, and the properties are copied according to the index values.
For edge properties, the edge index is not important, and the properties are copied by matching edges between the different graphs according to the source and target vertex indices. In case the graph has parallel edges with the same source and target vertices, they are matched according to their iteration order. The edge sets do not have to be the same between source and target graphs, as the copying occurs only for edges that lie at their intersection.
- degree_property_map(deg, weight=None)#
Create and return a vertex property map containing the degree type given by
deg
, which can be any of"in"
,"out"
, or"total"
. If provided,weight
should be an edgeEdgePropertyMap
containing the edge weights which should be summed.
- edge(s, t, all_edges=False, add_missing=False)#
Return the edge from vertex
s
tot
, if it exists. Ifall_edges=True
then a list is returned with all the parallel edges froms
tot
, otherwise only one edge is returned.If
add_missing == True
, a new edge is created and returned, if none currently exists.This operation will take \(O(\min(k(s), k(t)))\) time, where \(k(s)\) and \(k(t)\) are the out-degree and in-degree (or out-degree if undirected) of vertices \(s\) and \(t\).
- edges()#
Return an
iterator
over the edges.Note
The order of the edges traversed by the iterator does not necessarily correspond to the edge index ordering, as given by the
edge_index
property map. This will only happen afterreindex_edges()
is called, or in certain situations such as just after a graph is loaded from a file. However, further manipulation of the graph may destroy the ordering.
- get_all_edges(v, eprops=[])#
Return a
numpy.ndarray
containing the in- and out-edges of vertex v, and optional edge properties listeprops
. The shape of the array will be(E, 2 + len(eprops))
, whereE
is the number of edges, and each line will contain the source, target and the edge property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_all_edges(66, [g.edge_index]) array([[ 66, 63, 5266], [ 66, 20369, 5267], [ 66, 13980, 5268], [ 66, 8687, 5269], [ 66, 38674, 5270], [ 8687, 66, 179681], [ 20369, 66, 255033], [ 38674, 66, 300230]])
- get_all_neighbors(v, vprops=[])#
Return a
numpy.ndarray
containing the in-neighbors and out-neighbors of vertexv
, and optional vertex properties listvprops
. Ifvprops
is not empty, the shape of the array will be(V, 1 + len(eprops))
, whereV
is the number of vertices, and each line will contain a vertex and its property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_all_neighbors(66) array([ 63, 20369, 13980, 8687, 38674, 8687, 20369, 38674])
- get_all_neighbours(v, vprops=[])#
Return a
numpy.ndarray
containing the in-neighbors and out-neighbors of vertexv
, and optional vertex properties listvprops
. Ifvprops
is not empty, the shape of the array will be(V, 1 + len(eprops))
, whereV
is the number of vertices, and each line will contain a vertex and its property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_all_neighbors(66) array([ 63, 20369, 13980, 8687, 38674, 8687, 20369, 38674])
- get_edge_filter()#
Return a tuple with the edge filter property and bool value indicating whether or not it is inverted.
- get_edges(eprops=[])#
Return a
numpy.ndarray
containing the edges, and optional edge properties listeprops
. The shape of the array will be(E, 2 + len(eprops))
, whereE
is the number of edges, and each line will contain the source, target and the edge property values.Note
The order of the edges is identical to
edges()
.Examples
>>> g = gt.random_graph(6, lambda: 1, directed=False) >>> g.get_edges([g.edge_index]) array([[3, 2, 0], [4, 1, 2], [5, 0, 1]])
- get_fast_edge_removal()#
Return whether the fast \(O(1)\) removal of edges is currently enabled.
- get_filter_state()#
Return a copy of the filter state of the graph.
- get_in_degrees(vs, eweight=None)#
Return a
numpy.ndarray
containing the in-degrees of vertex listvs
. If supplied, the degrees will be weighted according to the edgeEdgePropertyMap
eweight
.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_in_degrees([42, 666]) array([20, 39], dtype=uint64)
- get_in_edges(v, eprops=[])#
Return a
numpy.ndarray
containing the in-edges of vertexv
, and optional edge properties listeprops
. The shape of the array will be(E, 2 + len(eprops))
, whereE
is the number of edges, and each line will contain the source, target and the edge property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_in_edges(66, [g.edge_index]) array([[ 8687, 66, 179681], [ 20369, 66, 255033], [ 38674, 66, 300230]])
- get_in_neighbors(v, vprops=[])#
Return a
numpy.ndarray
containing the in-neighbors of vertexv
, and optional vertex properties listvprops
. Ifvprops
is not empty, the shape of the array will be(V, 1 + len(eprops))
, whereV
is the number of vertices, and each line will contain a vertex and its property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_in_neighbors(66) array([ 8687, 20369, 38674])
- get_in_neighbours(v, vprops=[])#
Return a
numpy.ndarray
containing the in-neighbors of vertexv
, and optional vertex properties listvprops
. Ifvprops
is not empty, the shape of the array will be(V, 1 + len(eprops))
, whereV
is the number of vertices, and each line will contain a vertex and its property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_in_neighbors(66) array([ 8687, 20369, 38674])
- get_out_degrees(vs, eweight=None)#
Return a
numpy.ndarray
containing the out-degrees of vertex listvs
. If supplied, the degrees will be weighted according to the edgeEdgePropertyMap
eweight
.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_out_degrees([42, 666]) array([20, 38], dtype=uint64)
- get_out_edges(v, eprops=[])#
Return a
numpy.ndarray
containing the out-edges of vertexv
, and optional edge properties listeprops
. The shape of the array will be(E, 2 + len(eprops))
, whereE
is the number of edges, and each line will contain the source, target and the edge property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_out_edges(66, [g.edge_index]) array([[ 66, 63, 5266], [ 66, 20369, 5267], [ 66, 13980, 5268], [ 66, 8687, 5269], [ 66, 38674, 5270]])
- get_out_neighbors(v, vprops=[])#
Return a
numpy.ndarray
containing the out-neighbors of vertexv
, and optional vertex properties listvprops
. Ifvprops
is not empty, the shape of the array will be(V, 1 + len(eprops))
, whereV
is the number of vertices, and each line will contain a vertex and its property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_out_neighbors(66) array([ 63, 20369, 13980, 8687, 38674])
- get_out_neighbours(v, vprops=[])#
Return a
numpy.ndarray
containing the out-neighbors of vertexv
, and optional vertex properties listvprops
. Ifvprops
is not empty, the shape of the array will be(V, 1 + len(eprops))
, whereV
is the number of vertices, and each line will contain a vertex and its property values.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_out_neighbors(66) array([ 63, 20369, 13980, 8687, 38674])
- get_total_degrees(vs, eweight=None)#
Return a
numpy.ndarray
containing the total degrees (i.e. in- plus out-degree) of vertex listvs
. If supplied, the degrees will be weighted according to the edgeEdgePropertyMap
eweight
.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> g.get_total_degrees([42, 666]) array([40, 77], dtype=uint64)
- get_vertex_filter()#
Return a tuple with the vertex filter property and bool value indicating whether or not it is inverted.
- get_vertices(vprops=[])#
Return a
numpy.ndarray
containing the vertex indices, and optional vertex properties listvprops
. Ifvprops
is not empty, the shape of the array will be(V, 1 + len(vprops))
, whereV
is the number of vertices, and each line will contain the vertex and the vertex property values.Note
The order of the vertices is identical to
vertices()
.Examples
>>> g = gt.Graph() >>> g.add_vertex(5) <...> >>> g.get_vertices() array([0, 1, 2, 3, 4])
- is_directed()#
Get the directedness of the graph.
- is_reversed()#
Return
True
if the edges are reversed, andFalse
otherwise.
- iter_all_edges(v, eprops=[])#
Return an iterator over the in- and out-edge
`(source, target)
pairs for vertexv
, and optional edge properties listeprops
.Note
This mode of iteration is more efficient than using
all_edges()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for s, t, i in g.iter_all_edges(66, [g.edge_index]): ... print(s, t, i) 66 63 5266 66 20369 5267 66 13980 5268 66 8687 5269 66 38674 5270 8687 66 179681 20369 66 255033 38674 66 300230
- iter_all_neighbors(v, vprops=[])#
Return an iterator over the in- and out-neighbors of vertex
v
, and optional vertex properties listvprops
.Note
This mode of iteration is more efficient than using
all_neighbors()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for u, i in g.iter_all_neighbors(66, [g.vp.uid]): ... print(u, i) 63 ['paul wilders <webmaster@wilders.org>'] 20369 ['Zhen-Xjell <zhen-xjell@teamhelix.net>'] 13980 ['Hooman <Hooman@iname.com>'] 8687 ['H. Loeung (howe81) <howe81@unixque.com>', 'howe81 <howe81@bigpond.net.au>', 'Howie L (howe81) <howe81@bigpond.net.au>'] 38674 ['Howie L (howe81) <howe81@bigpond.net.au>'] 8687 ['H. Loeung (howe81) <howe81@unixque.com>', 'howe81 <howe81@bigpond.net.au>', 'Howie L (howe81) <howe81@bigpond.net.au>'] 20369 ['Zhen-Xjell <zhen-xjell@teamhelix.net>'] 38674 ['Howie L (howe81) <howe81@bigpond.net.au>']
- iter_all_neighbours(v, vprops=[])#
Return an iterator over the in- and out-neighbors of vertex
v
, and optional vertex properties listvprops
.Note
This mode of iteration is more efficient than using
all_neighbors()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for u, i in g.iter_all_neighbors(66, [g.vp.uid]): ... print(u, i) 63 ['paul wilders <webmaster@wilders.org>'] 20369 ['Zhen-Xjell <zhen-xjell@teamhelix.net>'] 13980 ['Hooman <Hooman@iname.com>'] 8687 ['H. Loeung (howe81) <howe81@unixque.com>', 'howe81 <howe81@bigpond.net.au>', 'Howie L (howe81) <howe81@bigpond.net.au>'] 38674 ['Howie L (howe81) <howe81@bigpond.net.au>'] 8687 ['H. Loeung (howe81) <howe81@unixque.com>', 'howe81 <howe81@bigpond.net.au>', 'Howie L (howe81) <howe81@bigpond.net.au>'] 20369 ['Zhen-Xjell <zhen-xjell@teamhelix.net>'] 38674 ['Howie L (howe81) <howe81@bigpond.net.au>']
- iter_edges(eprops=[])#
Return an iterator over the edge
`(source, target)
pairs, and optional edge properties listeprops
.Note
This mode of iteration is more efficient than using
edges()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["karate"] >>> for s, t, i in g.iter_edges([g.edge_index]): ... print(s, t, i) ... if s == 5: ... break 1 0 0 2 0 1 2 1 2 3 0 3 3 1 4 3 2 5 4 0 6 5 0 7
- iter_in_edges(v, eprops=[])#
Return an iterator over the in-edge
`(source, target)
pairs for vertexv
, and optional edge properties listeprops
.Note
This mode of iteration is more efficient than using
in_edges()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for s, t, i in g.iter_in_edges(66, [g.edge_index]): ... print(s, t, i) 8687 66 179681 20369 66 255033 38674 66 300230
- iter_in_neighbors(v, vprops=[])#
Return an iterator over the in-neighbors of vertex
v
, and optional vertex properties listvprops
.Note
This mode of iteration is more efficient than using
in_neighbors()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for u, i in g.iter_in_neighbors(66, [g.vp.uid]): ... print(u, i) 8687 ['H. Loeung (howe81) <howe81@unixque.com>', 'howe81 <howe81@bigpond.net.au>', 'Howie L (howe81) <howe81@bigpond.net.au>'] 20369 ['Zhen-Xjell <zhen-xjell@teamhelix.net>'] 38674 ['Howie L (howe81) <howe81@bigpond.net.au>']
- iter_in_neighbours(v, vprops=[])#
Return an iterator over the in-neighbors of vertex
v
, and optional vertex properties listvprops
.Note
This mode of iteration is more efficient than using
in_neighbors()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for u, i in g.iter_in_neighbors(66, [g.vp.uid]): ... print(u, i) 8687 ['H. Loeung (howe81) <howe81@unixque.com>', 'howe81 <howe81@bigpond.net.au>', 'Howie L (howe81) <howe81@bigpond.net.au>'] 20369 ['Zhen-Xjell <zhen-xjell@teamhelix.net>'] 38674 ['Howie L (howe81) <howe81@bigpond.net.au>']
- iter_out_edges(v, eprops=[])#
Return an iterator over the out-edge
`(source, target)
pairs for vertexv
, and optional edge properties listeprops
.Note
This mode of iteration is more efficient than using
out_edges()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for s, t, i in g.iter_out_edges(66, [g.edge_index]): ... print(s, t, i) 66 63 5266 66 20369 5267 66 13980 5268 66 8687 5269 66 38674 5270
- iter_out_neighbors(v, vprops=[])#
Return an iterator over the out-neighbors of vertex
v
, and optional vertex properties listvprops
.Note
This mode of iteration is more efficient than using
out_neighbors()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for u, i in g.iter_out_neighbors(66, [g.vp.uid]): ... print(u, i) 63 ['paul wilders <webmaster@wilders.org>'] 20369 ['Zhen-Xjell <zhen-xjell@teamhelix.net>'] 13980 ['Hooman <Hooman@iname.com>'] 8687 ['H. Loeung (howe81) <howe81@unixque.com>', 'howe81 <howe81@bigpond.net.au>', 'Howie L (howe81) <howe81@bigpond.net.au>'] 38674 ['Howie L (howe81) <howe81@bigpond.net.au>']
- iter_out_neighbours(v, vprops=[])#
Return an iterator over the out-neighbors of vertex
v
, and optional vertex properties listvprops
.Note
This mode of iteration is more efficient than using
out_neighbors()
, as descriptor objects are not created.Examples
>>> g = gt.collection.data["pgp-strong-2009"] >>> for u, i in g.iter_out_neighbors(66, [g.vp.uid]): ... print(u, i) 63 ['paul wilders <webmaster@wilders.org>'] 20369 ['Zhen-Xjell <zhen-xjell@teamhelix.net>'] 13980 ['Hooman <Hooman@iname.com>'] 8687 ['H. Loeung (howe81) <howe81@unixque.com>', 'howe81 <howe81@bigpond.net.au>', 'Howie L (howe81) <howe81@bigpond.net.au>'] 38674 ['Howie L (howe81) <howe81@bigpond.net.au>']
- iter_vertices(vprops=[])#
Return an iterator over the vertex indices, and optional vertex properties list
vprops
.Note
This mode of iteration is more efficient than using
vertices()
, as descriptor objects are not created.Examples
>>> g = gt.Graph() >>> g.add_vertex(5) <...> >>> for v in g.iter_vertices(): ... print(v) 0 1 2 3 4
- list_properties()#
Print a list of all internal properties.
Examples
>>> g = gt.Graph() >>> g.properties[("e", "foo")] = g.new_edge_property("vector<double>") >>> g.vertex_properties["foo"] = g.new_vertex_property("double") >>> g.vertex_properties["bar"] = g.new_vertex_property("python::object") >>> g.graph_properties["gnat"] = g.new_graph_property("string", "hi there!") >>> g.list_properties() gnat (graph) (type: string, val: hi there!) bar (vertex) (type: python::object) foo (vertex) (type: double) foo (edge) (type: vector<double>)
- load(file_name, fmt='auto', ignore_vp=None, ignore_ep=None, ignore_gp=None)#
Load graph from
file_name
(which can be either a string or a file-like object). The format is guessed fromfile_name
, or can be specified byfmt
, which can be either “gt”, “graphml”, “xml”, “dot” or “gml”. (Note that “graphml” and “xml” are synonyms).If provided, the parameters
ignore_vp
,ignore_ep
andignore_gp
, should contain a list of property names (vertex, edge or graph, respectively) which should be ignored when reading the file.Warning
The only file formats which are capable of perfectly preserving the internal property maps are “gt” and “graphml”. Because of this, they should be preferred over the other formats whenever possible.
- new_edge_property(value_type, vals=None, val=None)#
Create a new edge property map of type
value_type
, and return it. If provided, the values will be initialized byvals
, which should be sequence or byval
which should be a single value.
- new_ep(value_type, vals=None, val=None)#
Alias to
new_edge_property()
.
- new_gp(value_type, val=None)#
Alias to
new_graph_property()
.
- new_graph_property(value_type, val=None)#
Create a new graph property map of type
value_type
, and return it. Ifval
is not None, the property is initialized to its value.
- new_property(key_type, value_type, vals=None)#
Create a new (uninitialized) vertex property map of key type
key_type
(v
,e
org
), value typevalue_type
, and return it. If provided, the values will be initialized byvals
, which should be a sequence.
- new_vertex_property(value_type, vals=None, val=None)#
Create a new vertex property map of type
value_type
, and return it. If provided, the values will be initialized byvals
, which should be sequence or byval
which should be a single value.
- new_vp(value_type, vals=None, val=None)#
Alias to
new_vertex_property()
.
- num_edges(ignore_filter=False)#
Get the number of edges.
If
ignore_filter == True
, edge filters are ignored.Note
If the edges are being filtered, and
ignore_filter == False
, this operation is \(O(E)\). Otherwise it is \(O(1)\).
- num_vertices(ignore_filter=False)#
Get the number of vertices.
If
ignore_filter == True
, vertex filters are ignored.Note
If the vertices are being filtered, and
ignore_filter == False
, this operation is \(O(V)\). Otherwise it is \(O(1)\).
- own_property(prop)#
Return a version of the property map ‘prop’ (possibly belonging to another graph) which is owned by the current graph.
- purge_edges()#
Remove all edges of the graph which are currently being filtered out. This operation is not reversible.
- purge_vertices(in_place=False)#
Remove all vertices of the graph which are currently being filtered out. This operation is not reversible.
If the option
in_place == True
is given, the algorithm will remove the filtered vertices and re-index all property maps which are tied with the graph. This is a slow operation which has an \(O(V^2)\) complexity.If
in_place == False
, the graph and its vertex and edge property maps are temporarily copied to a new unfiltered graph, which will replace the contents of the original graph. This is a fast operation with an \(O(V + E)\) complexity. This is the default behaviour if no option is given.
- reindex_edges()#
Reset the edge indices so that they lie in the [0,
num_edges()
- 1] range. The index ordering will be compatible with the sequence returned by theedges()
function.Warning
Calling this function will invalidate all existing edge property maps, if the index ordering is modified! The property maps will still be usable, but their contents will still be tied to the old indices, and thus may become scrambled.
- remove_edge(edge)#
Remove an edge from the graph.
Note
This operation is normally \(O(k_s + k_t)\), where \(k_s\) and \(k_t\) are the total degrees of the source and target vertices, respectively. However, if
set_fast_edge_removal()
is set to True, this operation becomes \(O(1)\).Warning
The relative ordering of the remaining edges in the graph is kept unchanged, unless
set_fast_edge_removal()
is set to True, in which case it can change.
- remove_vertex(vertex, fast=False)#
Remove a vertex from the graph. If
vertex
is an iterable, it should correspond to a sequence of vertices to be removed.Note
If the option
fast == False
is given, this operation is \(O(V + E)\) (this is the default). Otherwise it is \(O(k + k_{\text{last}})\), where \(k\) is the (total) degree of the vertex being deleted, and \(k_{\text{last}}\) is the (total) degree of the vertex with the largest index.Warning
This operation may invalidate vertex descriptors. Vertices are always indexed contiguously in the range \([0, N-1]\), hence vertex descriptors with an index higher than
vertex
will be invalidated after removal (iffast == False
, otherwise only descriptors pointing to vertices with the largest index will be invalidated).Because of this, the only safe way to remove more than one vertex at once is to sort them in decreasing index order:
# 'del_list' is a list of vertex descriptors for v in reversed(sorted(del_list)): g.remove_vertex(v)
Alternatively (and preferably), a list (or iterable) may be passed directly as the
vertex
parameter, and the above is performed internally (in C++).Warning
If
fast == True
, the vertex being deleted is ‘swapped’ with the last vertex (i.e. with the largest index), which will in turn inherit the index of the vertex being deleted. All property maps associated with the graph will be properly updated, but the index ordering of the graph will no longer be the same.
- save(file_name, fmt='auto')#
Save graph to
file_name
(which can be either a string or a file-like object). The format is guessed from thefile_name
, or can be specified byfmt
, which can be either “gt”, “graphml”, “xml”, “dot” or “gml”. (Note that “graphml” and “xml” are synonyms).Warning
The only file formats which are capable of perfectly preserving the internal property maps are “gt” and “graphml”. Because of this, they should be preferred over the other formats whenever possible.
- set_directed(is_directed)#
Set the directedness of the graph.
Note
This is a \(O(1)\) operation that does not modify the storage of the graph.
Warning
Changing directedness will invalidate existing vertex and edge descriptors, which will still point to the original graph.
- set_edge_filter(prop, inverted=False)#
Set the edge boolean filter property. Only the edges with value different than
False
are kept in the filtered graph. If theinverted
option is supplied with valueTrue
, only the edges with valueFalse
are kept. If the supplied property isNone
, the filter is replaced by an uniform filter allowing all edges.Note
This is a \(O(1)\) operation that does not modify the storage of the graph.
Warning
Setting edge filters will invalidate existing vertex and edge descriptors, which will still point to the unfiltered graph.
- set_fast_edge_removal(fast=True)#
If
fast == True
the fast \(O(1)\) removal of edges will be enabled. This requires an additional data structure of size \(O(E)\) to be kept at all times. Iffast == False
, this data structure is destroyed.
- set_filter_state(state)#
Set the filter state of the graph.
- set_filters(eprop, vprop, inverted_edges=False, inverted_vertices=False)#
Set the boolean properties for edge and vertex filters, respectively. Only the vertices and edges with value different than
False
are kept in the filtered graph. If either theinverted_edges
orinverted_vertex
options are supplied with the valueTrue
, only the edges or vertices with valueFalse
are kept. If any of the supplied property isNone
, an empty filter is constructed which allows all edges or vertices.Note
This is a \(O(1)\) operation that does not modify the storage of the graph.
Warning
Setting vertex or edge filters will invalidate existing vertex and edge descriptors, which will still point to the unfiltered graph.
- set_reversed(is_reversed)#
Reverse the direction of the edges, if
is_reversed
isTrue
, or maintain the original direction otherwise.Note
This is a \(O(1)\) operation that does not modify the storage of the graph.
Warning
Reversing the graph will invalidate existing vertex and edge descriptors, which will still point to the original graph.
- set_vertex_filter(prop, inverted=False)#
Set the vertex boolean filter property. Only the vertices with value different than
False
are kept in the filtered graph. If theinverted
option is supplied with valueTrue
, only the vertices with valueFalse
are kept. If the supplied property isNone
, the filter is replaced by an uniform filter allowing all vertices.Note
This is a \(O(1)\) operation that does not modify the storage of the graph.
Warning
Setting vertex filters will invalidate existing vertex and edge descriptors, which will still point to the unfiltered graph.
- shrink_to_fit()#
Force the physical capacity of the underlying containers to match the graph’s actual size, potentially freeing memory back to the system.
- vertex(i, use_index=True, add_missing=False)#
Return the vertex with index
i
. Ifuse_index=False
, thei
-th vertex is returned (which can differ from the vertex with indexi
in case of filtered graphs).If
add_missing == True
, and the vertex does not exist in the graph, the necessary number of missing vertices are inserted, and the new vertex is returned.
- vertices()#
Return an
iterator
over the vertices.Note
The order of the vertices traversed by the iterator always corresponds to the vertex index ordering, as given by the
vertex_index
property map.Examples
>>> g = gt.Graph() >>> vlist = list(g.add_vertex(5)) >>> vlist2 = [] >>> for v in g.vertices(): ... vlist2.append(v) ... >>> assert(vlist == vlist2)
- base#
Base graph.
- edge_index#
Edge index map.
It maps for each edge in the graph an unique integer.
Note
Like
vertex_index
, this is a special instance of aEdgePropertyMap
class, which is immutable, and cannot be accessed as an array.Additionally, the indices may not necessarily lie in the range [0,
num_edges()
- 1]. However this will always happen whenever no edges are deleted from the graph.
- edge_index_range#
The size of the range of edge indices.
- edge_properties#
Dictionary of internal edge properties. The keys are the property names.
- ep#
Alias to
edge_properties
.
- gp#
Alias to
graph_properties
.
- graph_properties#
Dictionary of internal graph properties. The keys are the property names.
- properties#
Dictionary of internal properties. Keys must always be a tuple, where the first element if a string from the set
{'v', 'e', 'g'}
, representing a vertex, edge or graph property, respectively, and the second element is the name of the property map.Examples
>>> g = gt.Graph() >>> g.properties[("e", "foo")] = g.new_edge_property("vector<double>") >>> del g.properties[("e", "foo")]
- vertex_index#
Vertex index map.
It maps for each vertex in the graph an unique integer in the range [0,
num_vertices()
- 1].Note
Like
edge_index
, this is a special instance of aVertexPropertyMap
class, which is immutable, and cannot be accessed as an array.
- vertex_properties#
Dictionary of internal vertex properties. The keys are the property names.
- vp#
Alias to
vertex_properties
.