Views

Graph views are wrappers around a graph which adjust its functionality. These views are live, meaning that changes in the original graph are reflected in the view and vice-versa.

jgrapht.views.as_edge_reversed(graph)[source]

Create an edge reversed view of a graph. Any updates in the original graph are reflected in the view.

Parameters

graph – the original graph

Returns

a graph with reversed edges

jgrapht.views.as_graph_union(graph1, graph2, edge_weight_combiner_cb=None)[source]

Create a graph union view of two graphs. Any updates in the original graphs are reflected in the view.

The resulting graph is unmodifiable and may contain multiple-edges even if the input graphs did not contain multiple edges. If both graphs contain an edge with the same identifier, but the edge endpoints are different in the two graphs, then the graph union contains the edge with the endpoints from the first graph. The weight of edge is computed using the combiner provided as parameter.

Note

Graph types must be the same. You cannot union a directed with an undirected graph.

Parameters
  • graph1 – the first graph

  • graph2 – the second graph

  • edge_weight_combiner_cb – function responsible for combining weights in edges which belong to both graphs. If None then the default combiner is addition. The callback must accept two double parameters and return one.

Returns

a graph which is the union of the two graphs

jgrapht.views.as_listenable(graph)[source]

Create a listenable view of a graph. This is a graph view which supports listeners on structural change events.

Parameters

graph – the original graph

Returns

a listenable graph which is an instance of type ListenableGraph.

jgrapht.views.as_masked_subgraph(graph, vertex_mask_cb, edge_mask_cb=None)[source]

Create a masked subgraph view.

This is an unmodifiable subgraph induced by the vertex/edge masking callbacks. The subgraph will keep track of edges being added to its vertex subset as well as deletion of edges and vertices (from the original graph). When iterating over the vertices/edges, it will iterate over the vertices/edges of the base graph and discard vertices/edges that are masked (an edge with a masked endpoint vertex is discarded as well).

Note

Callback functions accept the vertex or edge as a parameter and they must return true or false indicating whether the vertex or edge should be masked.

Parameters
  • graph – the original graph

  • vertex_mask_cb – a vertex mask callback

  • edge_mask_cb – an edge mask callback

Returns

a masked subgraph

jgrapht.views.as_undirected(graph)[source]

Create an undirected view of a graph. Any updates in the original graph are reflected in the view.

Parameters

graph – the original graph

Returns

an undirected graph

jgrapht.views.as_unmodifiable(graph)[source]

Create an unmodifiable view of a graph. Any updates in the original graph are reflected in the view.

Parameters

graph – the original graph

Returns

an unmodifiable graph

jgrapht.views.as_unweighted(graph)[source]

Create an unweighted view of a graph. Any updates in the original graph are reflected in the view.

Parameters

graph – the original graph

Returns

an unweighted graph

jgrapht.views.as_weighted(graph, edge_weight_cb, cache_weights=True, write_weights_through=False)[source]

Create a weighted view of a graph.

This function can be used to make an unweighted graph weighted, to override the weights of a weighted graph, or to provide different weighted views of the same underlying graph.

The weights are calculated using the user provided edge_weight_cb callback function. This function should take as argument the edge identifier and return its weight. If the edge weight callback is None, then a default function which always returns 1.0 is used.

If parameter cache_weights is True, then the edge weight function is only called once to initialize the weight. Other calls will return the cached weight without calling the provided function. Moreover, the returned value can be adjusted. Note that calling set_edge_weight() with caching disabled will raise an error.

If parameter write_weights_through is True, the weight set by calling method set_edge_weight() will be propagated to the backing graph. In this case the backing graph must be weighted, otherwise an error will be raised.

Parameters
  • graph – the original graph

  • edge_weight_cb – edge weight function

  • cache_weights – if true weights are cached once computed by the weight function

  • write_weights_through – if true, any weight adjustment by method set_edge_weight() will be propagated to the backing graph

Returns

a weighted view