sgtl.graph.from_edgelist#
- sgtl.graph.from_edgelist(filename, directed=False, num_vertices=None, **kwargs) Graph #
Construct an
sgtl.Graph
object from an edgelist file.The edgelist file represents a graph by specifying each edge in the graph on a separate line in the file. Each line looks like:
id1 id2 <weight>
where id1 and id2 are the vertex indices of the given edge, and an optional weight is given as a floating point number. If any edge has a specified weight, then you must specify the weight for every edge. That is, every line in the edgelist must contain the same number of elements (either 2 or 3). The file may also contain comment lines.
By default, the elements on a given line are assumed to be separated by spaces, and comments should begin with a ‘#’ character. These defaults can be changed by passing additional key-word arguments which will be passed to pandas.read_csv.
- Parameters:
filename – The edgelist filename.
directed – Whether the graph is directerd.
num_vertices – The number of vertices in the graph, if this is known in advance. Specifying this value will speed up the method.
kwargs – Key-word arguments to pass to the pandas.read_csv method which will be used to read in the file. This can be used to set a delimiter other than the space character, or to change the comment character.
- Returns:
the constructed sgtl.Graph object
- Raises:
TypeError – If the edgelist file cannot be parsed due to incorrect types.
- Example:
An example edgelist file is as follows:
# This is a comment line 0 1 0.5 1 2 1 2 0 0.5
The above file gives a weighted triangle graph.