Esempio n. 1
0
 def graph(self, new_graph):
     if isinstance(new_graph, GraphLib):
         self._graph = GraphObject.to_graph_object(new_graph)
     elif isinstance(new_graph, GraphObject):
         self._graph = new_graph
     else:
         raise TypeError("The object passed is not a \
             GraphObject but a {}".format(new_graph.__class__.__name__))
Esempio n. 2
0
    def __init__(self, nodes=0, name="Graph",
                  weighted=True, directed=True, libgraph=None, **kwargs):
        '''
        Initialize Graph instance

        Parameters
        ----------
        nodes : int, optional (default: 0)
            Number of nodes in the graph.
        name : string, optional (default: "Graph")
            The name of this :class:`Graph` instance.
        weighted : bool, optional (default: True)
            Whether the graph edges have weight properties.
        directed : bool, optional (default: True)
            Whether the graph is directed or undirected.
        libgraph : :class:`~nngt.core.GraphObject`, optional
            An optional :class:`~nngt.core.GraphObject` to serve as base.
        
        Returns
        -------
        self : :class:`~nggt.core.Graph`
        '''
        self.__id = self.__class__.__max_id
        self._name = name
        self._directed = directed
        self._edges = []
        # create the graphlib graph
        if libgraph is not None:
            self._graph = GraphObject.to_graph_object(libgraph)
        else:
            self._graph = GraphObject(nodes=nodes, directed=directed)
        # take care of the weights @todo: use those of the libgraph
        if weighted:
            if "weight_prop" in kwargs.keys():
                self._w = kwargs["weight_prop"]
            else:
                self._w = {"distrib": "constant"}
            self.set_weights()
        # update the counters
        self.__class__.__num_graphs += 1
        self.__class__.__max_id += 1