ConvertGraph

ConvertGraph(GraphType, RenumberNodes=False)

A graph method that converts a graph to a graph of type GraphType with an optional node renumbering. The resulting graph will have type GraphType. Any node and edge data is not copied, but it is shared by input and output graphs.

Parameters:

  • GraphType: graph class

    Class of output graph – one of TNGraph, TUNGraph or TNEANet.

  • (optional) RenumberNodes: bool

    Determines whether the node ids are preserved or not. If False, then nodes in the resulting graph have the same node ids as nodes in the original graph. If True, then nodes in the resulting graph are renumbered sequentially from 0 to N-1, where N is the number of nodes. By default, the nodes are not renumbered.

Return value:

  • graph

    A graph of type GraphType converted from the original graph.

The following example shows how to convert between TNGraph, TUNGraph, and TNEANet types of graphs:

import snap

GIn = snap.GenRndGnm(snap.TNGraph, 100, 1000)
# convert directed graph to undirected
GOut = GIn.ConvertGraph(snap.TUNGraph)

GIn = snap.GenRndGnm(snap.TUNGraph, 100, 1000)
# convert undirected graph to directed
GOut = GIn.ConvertGraph(snap.TNGraph)

GIn = snap.GenRndGnm(snap.TNGraph, 100, 1000)
# convert directed graph to a network
GOut = GIn.ConvertGraph(snap.TNEANet)