Skip to main content

package to manipulate graphs

Project description


This is a package for the manipulation of graphs made for a class of graphs in the Brazilian university 'Universidade Tecnologica Federal do Paraná'


pip install graphpython

Complete Documentation


to use this class, the main element is Graph class. With this class, you will be able to fill the graph with vertex and edges and do all the necessary operations

  • Graph([directed]): create a graph
    • directed: Defaults to False. tells if the graph is directed or not

Code example

# Create a new graph
from graphpy.graph import Graph

gp = Graph()
# add a new vertex

# to get the create vertex, you can use the [] operator
vertex1 = gp['vertex1']

Vertex operations

The base of all graph is the vertex, to create a new vertex you got to use the follow functions

  • gp.add_vertex(name, [value]): create a new vertex and insert to the graph

    • name: Unique identification to the vertex inside the graph
    • value: optional value to the vertex
  • gp.get_vertex(name) or gp[name]: return the vertex from the graph

    • name: Unique identification to the vertex inside the graph
  • gp.get_all_vertex(): get a list with all vertex from the graph

  • gp.adjacents_vertex(vtx): get all adjacent vertex from one vertex

    • vtx: vertex you want to know the adjacent
  • gp.remove_vertex(vertex_to_remove): Remove a vertex and all the connections he have

    • vertex_to_remove: vertex you want to remove

Code example

from graphpy.graph import Graph

gp = Graph()

Search in the graph

The main class has a search method and to use, you need to pass a by params an strategy to make the search.

Implement a new search strategy

In the class has two class strategies already implemented:

  • BFSstrategy
  • DFSstrategy
from graphpy.graph import Graph
from graphpy.BFSstrategy import BFSstrategy

graph = Graph()

To extend all the search types you can create a new strategy extending the SearchStrategy class from search_strategy.

from graphpy.search_strategy import SearchStrategy

class DFSstrategy(SearchStrategy):
    def __init__(self):

        self.__predecessors = {}
        self.__firstSee = {}
        self.__close = {}
        self.__time = 0

    def __dfs_visit(self, vertex):
        self.__time = self.__time + 1
        self.__firstSee[vertex] = self.__time

        for adjacent in self.get_adjacent_list()[vertex]:
            if adjacent.get_color() == 0:
                self.__predecessors[adjacent] = vertex
        self.__time += 1
        self.__close[vertex] = self.__time

    def search(self):
        # colors:
        #   white: not visited
        #   grey: in the queue
        #   black: nothing more to do
        for key in self.get_adjacent_list():
            # set color for all vertices to white
            self.__predecessors[key] = None
        self.__time = 0
        for key in self.get_adjacent_list():
            if key.get_color() == 0:

        return self.__firstSee, self.__close, self.__predecessors

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for graphpython, version 0.83
Filename, size File type Python version Upload date Hashes
Filename, size graphpython-0.83-py3-none-any.whl (8.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size graphpython-0.83.tar.gz (6.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page