Graph - basic manipulationΒΆ

This notebook illustrates the basic functionality of pyrigi.graph.Graph. It can be downloaded here.

# The import will work if the package was installed using pip.
from pyrigi import Graph

An easy way to construct a graph is to provide the list of its edges:

G = Graph([(0,1), (1,2), (2,3), (0,3)])
G
Graph with vertices [0, 1, 2, 3] and edges [[0, 1], [0, 3], [1, 2], [2, 3]]

Edges and vertices can be added:

G.add_vertices([0, 2, 5, 7, 'a', 'b'])
G.add_edges([(0,7), (2,5)])
G
Graph with vertices [0, 1, 2, 3, 5, 7, 'a', 'b'] and edges [[0, 1], [0, 3], [0, 7], [1, 2], [2, 3], [2, 5]]

or removed:

G.delete_vertex('a')
G
Graph with vertices [0, 1, 2, 3, 5, 7, 'b'] and edges [[0, 1], [0, 3], [0, 7], [1, 2], [2, 3], [2, 5]]
G.delete_vertices([2, 7])
G
Graph with vertices [0, 1, 3, 5, 'b'] and edges [[0, 1], [0, 3]]
G.delete_edges([(0,1), (0,3)])
G
Graph with vertices [0, 1, 3, 5, 'b'] and edges []

There are also other ways how to construct a graph:

import pyrigi.graphDB as graphs
graphs.Complete(4)
Graph with vertices [0, 1, 2, 3] and edges [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]
Graph.CompleteOnVertices(['a', 1, (1.2)])
Graph with vertices ['a', 1, 1.2] and edges [('a', 1), ('a', 1.2), (1, 1.2)]
from sympy import Matrix
Graph.from_adjacency_matrix(Matrix([[0,1,1], [1,0,0], [1,0,0]]))
Graph with vertices [0, 1, 2] and edges [[0, 1], [0, 2]]
Graph.from_vertices(range(4))
Graph with vertices [0, 1, 2, 3] and edges []
Graph.from_vertices_and_edges(range(6), [[i, (i+2) % 6] for i in range(6)])
Graph with vertices [0, 1, 2, 3, 4, 5] and edges [[0, 2], [0, 4], [1, 3], [1, 5], [2, 4], [3, 5]]

A vertex of a graph can be of any hashable type, but it is recommended to have all of them of the same type, not as above. If all vertices have the same type, the vertex/edge set can be sorted when a list is required; otherwise, the order might differ:

G = Graph([[0, 7], [2, 5], [1, 2], [0, 1], [0, 3], [2, 3]])
print(G.vertex_list())
print(G.edge_list())
print(Graph.from_vertices(['a',1,(1,2)]).vertex_list())
print(Graph.from_vertices([1,'a',(1,2)]).vertex_list())
[0, 1, 2, 3, 5, 7]
[[0, 1], [0, 3], [0, 7], [1, 2], [2, 3], [2, 5]]
['a', 1, (1, 2)]
[1, 'a', (1, 2)]