Exemplo n.º 1
0
    def add_edge(self, head_id, tail_id, edge_data=1, create_nodes=True):
        """
        Adds a directed edge going from head_id to tail_id.
        Arbitrary data can be attached to the edge via edge_data.
        It may create the nodes if adding edges between nonexisting ones.

        :param head_id: head node
        :param tail_id: tail node
        :param edge_data: (optional) data attached to the edge
        :param create_nodes: (optional) creates the head_id or tail_id node in case they did not exist
        """
        # shorcut
        edge = self.next_edge

        # add nodes if on automatic node creation
        if create_nodes:
            self.add_node(head_id)
            self.add_node(tail_id)

        # update the corresponding incoming and outgoing lists in the nodes
        # index 0 -> incoming edges
        # index 1 -> outgoing edges

        try:
            self.nodes[tail_id][0].append(edge)
            self.nodes[head_id][1].append(edge)
        except KeyError:
            raise GraphError('Invalid nodes %s -> %s' % (head_id, tail_id))

        # store edge information
        self.edges[edge] = (head_id, tail_id, edge_data)

        self.next_edge += 1
Exemplo n.º 2
0
    def out_edges(self, node):
        """
        Returns a list of the outgoing edges
        """
        try:
            return list(self.nodes[node][1])
        except KeyError:
            raise GraphError('Invalid node %s' % node)

        return None
Exemplo n.º 3
0
    def inc_edges(self, node):
        """
        Returns a list of the incoming edges
        """
        try:
            return list(self.nodes[node][0])
        except KeyError:
            raise GraphError('Invalid node %s' % node)

        return None
Exemplo n.º 4
0
    def edge_by_id(self, edge):
        """
        Returns the edge that connects the head_id and tail_id nodes
        """
        try:
            head, tail, data = self.edges[edge]
        except KeyError:
            head, tail = None, None
            raise GraphError('Invalid edge %s' % edge)

        return (head, tail)
Exemplo n.º 5
0
 def restore_edge(self, edge):
     """
     Restores a previously hidden edge back into the graph.
     """
     try:
         head_id, tail_id, data = self.hidden_edges[edge]
         self.nodes[tail_id][0].append(edge)
         self.nodes[head_id][1].append(edge)
         self.edges[edge] = head_id, tail_id, data
         del self.hidden_edges[edge]
     except KeyError:
         raise GraphError('Invalid edge %s' % edge)
Exemplo n.º 6
0
 def restore_node(self, node):
     """
     Restores a previously hidden node back into the graph and restores
     all of its incoming and outgoing edges.
     """
     try:
         self.nodes[node], all_edges = self.hidden_nodes[node]
         for edge in all_edges:
             self.restore_edge(edge)
         del self.hidden_nodes[node]
     except KeyError:
         raise GraphError('Invalid node %s' % node)
Exemplo n.º 7
0
 def hide_node(self, node):
     """
     Hides a node from the graph.  The incoming and outgoing edges of the
     node will also be hidden.  The node may be unhidden at some later time.
     """
     try:
         all_edges = self.all_edges(node)
         self.hidden_nodes[node] = (self.nodes[node], all_edges)
         for edge in all_edges:
             self.hide_edge(edge)
         del self.nodes[node]
     except KeyError:
         raise GraphError('Invalid node %s' % node)
Exemplo n.º 8
0
 def hide_edge(self, edge):
     """
     Hides an edge from the graph. The edge may be unhidden at some later
     time.
     """
     try:
         head_id, tail_id, edge_data = self.hidden_edges[edge] = self.edges[
             edge]
         self.nodes[tail_id][0].remove(edge)
         self.nodes[head_id][1].remove(edge)
         del self.edges[edge]
     except KeyError:
         raise GraphError('Invalid edge %s' % edge)
Exemplo n.º 9
0
    def __init__(self, edges=None):
        """
        Initialization
        """

        self.next_edge = 0
        self.nodes, self.edges = {}, {}
        self.hidden_edges, self.hidden_nodes = {}, {}

        if edges is not None:
            for item in edges:
                if len(item) == 2:
                    head, tail = item
                    self.add_edge(head, tail)
                elif len(item) == 3:
                    head, tail, data = item
                    self.add_edge(head, tail, data)
                else:
                    raise GraphError("Cannot create edge from %s" % (item, ))
Exemplo n.º 10
0
def generate_random_graph(node_num, edge_num, self_loops=False, multi_edges=False):
    '''
    Generates and returns a :py:class:`~altgraph.Graph.Graph` instance with *node_num* nodes
    randomly connected by *edge_num* edges.
    '''
    g = Graph.Graph()

    if not multi_edges:
        if self_loops:
            max_edges = node_num * node_num
        else:
            max_edges = node_num * (node_num-1)

        if edge_num > max_edges:
            raise GraphError("inconsistent arguments to 'generate_random_graph'")

    nodes = range(node_num)

    for node in nodes:
        g.add_node(node)

    while 1:
        head = random.choice(nodes)
        tail = random.choice(nodes)

        # loop defense
        if head == tail and not self_loops:
            continue

        # multiple edge defense
        if g.edge_by_node(head,tail) is not None and not multi_edges:
            continue

        # add the edge
        g.add_edge(head, tail)
        if g.number_of_edges() >= edge_num:
            break

    return g