weisfeiler_lehman_hash

weisfeiler_lehman_hash#

graph_tool.structure.weisfeiler_lehman_hash(g, vertex_label=None, edge_label=None, n_iter=3, vertex_hashes=False)[git][source]#

Return the Weisfeiler-Lehman hash of the graph.

Parameters:
gGraph

Graph to be used.

vertex_labelVertexPropertyMap (optional, default: None)

If given, the initial labels of the vertices.

edge_labelEdgePropertyMap (optional, default: None)

If given, the labels of the edges.

n_iterint (optional, default: 3)

Number of refinement iterations.

vertex_hashesbool (optional, default: False)

If True, the hashes of the individual vertices after the last iteration are also returned.

Returns:
hashint

Hash of the graph, as an unsigned 64-bit integer.

vhashVertexPropertyMap

Hashes of the vertices, as signed 64-bit integers. Only returned if vertex_hashes == True.

See also

isomorphism

Exact isomorphism test between two graphs

bliss_automorphism_group

Canonical labeling with the bliss algorithm

nauty_automorphism_group

Canonical labeling with nauty or Traces

Notes

The color refinement of Weisfeiler and Leman [weisfeiler-reduction-1968] [shervashidze-weisfeiler-2011] starts from the labels \(h^{(0)}_v = \mathrm{hash}(x_v)\), where \(x_v\) is the value of vertex_label (the same for all vertices if it is not given), and computes in each iteration

\[h^{(t+1)}_v = \mathrm{hash}\left(h^{(t)}_v, \{\!\{(d_e, y_e, h^{(t)}_{u_e}) : e \ni v\}\!\}\right),\]

where the multiset runs over the edges \(e\) incident to \(v\) (both in- and out-edges, for directed graphs), \(u_e\) is the other endpoint of \(e\), \(y_e\) its value of edge_label (if given), and \(d_e\) indicates whether \(e\) is an out- or an in-edge of \(v\) (for undirected graphs, it is the same for all edges). Every edge appears in the multiset as many times as it contributes to the total degree of \(v\): parallel edges appear once each, and a self-loop twice (once as an out-edge and once as an in-edge, for directed graphs), with \(u_e = v\). The multisets are hashed after sorting. The hash of the graph is \(\mathrm{hash}(n_{\text{iter}}, M^{(0)}, \dots, M^{(n_{\text{iter}})})\), where \(M^{(t)} = \{\!\{h^{(t)}_v\}\!\}\) is the multiset of the labels of iteration \(t\), and the vertex hashes returned are \(h^{(n_{\text{iter}})}_v\).

Isomorphic graphs have the same hash, but graphs with the same hash are not necessarily isomorphic: besides hash collisions, which are unlikely for 64-bit hashes, the refinement cannot distinguish some non-isomorphic graphs, such as regular graphs of the same size and degree. The automorphism group and an exact isomorphism test are obtained with bliss_automorphism_group() or nauty_automorphism_group(). The hash values are not cryptographic, and do not depend on the platform; the values of labels of type python::object are hashed via their Python representation.

The algorithm runs in time \(O(n_{\text{iter}}(V\log V + E\log k_{\text{max}}))\).

Parallel implementation.

If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.

References

[weisfeiler-reduction-1968]

Boris Weisfeiler and Andrei Leman, “The reduction of a graph to canonical form and the algebra which appears therein”, Nauchno-Technicheskaya Informatsia 2(9), 12-16 (1968).

[shervashidze-weisfeiler-2011]

Nino Shervashidze, Pascal Schweitzer, Erik Jan van Leeuwen, Kurt Mehlhorn and Karsten M. Borgwardt, “Weisfeiler-Lehman graph kernels”, Journal of Machine Learning Research 12, 2539-2561 (2011), https://jmlr.org/papers/v12/shervashidze11a.html

Examples

>>> g = gt.collection.data["karate"]
>>> u = gt.Graph(g, vorder=g.new_vp("int", vals=np.random.permutation(g.num_vertices())))
>>> gt.weisfeiler_lehman_hash(g) == gt.weisfeiler_lehman_hash(u)
True
>>> u.remove_edge(next(u.edges()))
>>> gt.weisfeiler_lehman_hash(g) == gt.weisfeiler_lehman_hash(u)
False