def __init__(self, nx_graph=nx.Graph()):
        self.bipartite_graph = nx.Graph()
        self.node = self.bipartite_graph.node
        self.next_edge_index = 0
        self.next_hedge_index = 0

        self.nodes_count = 0
        self.edges_count = 0
        self.hedges_count = 0

        # ready sets
        self.reset_nodes_with_more_labels()
        self.reset_self_loops()
        self.reset_parallel_hedges_groups()

        # add nodes
        for node in nx_graph.nodes_iter():
            self.add_node(node, attr_dict=copy.deepcopy(nx_graph.node[node]))

        # add edges of order 2
        if nx_graph.is_directed():
            adj_nodes = nxext.get_all_adjacent_nodes(nx_graph)
            for pair in adj_nodes:
                edges = nxext.get_edge_labels_and_dirs(nx_graph, pair[0],
                                                       pair[1])
                u = Hypergraph.format_node_id(pair[0])
                v = Hypergraph.format_node_id(pair[1])
                for edge in edges:
                    dir_code = edge[1]
                    if dir_code == 0:
                        direction = None
                    elif dir_code > 0:
                        direction = set([(u, v)])
                    else:
                        direction = set([(v, u)])
                    self.add_edge(set([u, v]),
                                  direction=direction,
                                  label=copy.deepcopy(edge[0]))
        else:
            for edge_endpoints in nx_graph.edges_iter():
                u = u"n_{0}".format(edge_endpoints[0])
                v = u"n_{0}".format(edge_endpoints[1])
                if nx_graph.is_multigraph():
                    edges = nx_graph[edge_endpoints[0]][edge_endpoints[1]]
                    for i in range(len(edges)):
                        self.add_edge(set([u, v]), label=edges[i]["label"])
                else:
                    edge_label = nx_graph.edge[edge_endpoints[0]][
                        edge_endpoints[1]]["label"]
                    self.add_edge(set([u, v]), label=copy.deepcopy(edge_label))

        # Initialize ready sets
        self.init_parallel_edges_groups()
        self.init_nodes_with_n_neighbors()
Beispiel #2
0
 def __init__(self, nx_graph = nx.Graph()):
     self.bipartite_graph = nx.Graph()
     self.node = self.bipartite_graph.node
     self.next_edge_index = 0
     self.next_hedge_index = 0
     
     self.nodes_count = 0
     self.edges_count = 0
     self.hedges_count = 0
     
     # ready sets
     self.reset_nodes_with_more_labels()
     self.reset_self_loops()
     self.reset_parallel_hedges_groups()
     
     # add nodes
     for node in nx_graph.nodes_iter():
         self.add_node(node, attr_dict=copy.deepcopy(nx_graph.node[node]))
     
     # add edges of order 2
     if nx_graph.is_directed():
         adj_nodes = nxext.get_all_adjacent_nodes(nx_graph)
         for pair in adj_nodes:
             edges = nxext.get_edge_labels_and_dirs(nx_graph, pair[0], pair[1])
             u = Hypergraph.format_node_id(pair[0])
             v = Hypergraph.format_node_id(pair[1])
             for edge in edges:
                 dir_code = edge[1]
                 if dir_code == 0:
                     direction = None
                 elif dir_code > 0:
                     direction = set([(u, v)])
                 else:
                     direction = set([(v, u)])
                 self.add_edge(set([u, v]), direction=direction, label=copy.deepcopy(edge[0]))
     else:
         for edge_endpoints in nx_graph.edges_iter():
             u = u"n_{0}".format(edge_endpoints[0])
             v = u"n_{0}".format(edge_endpoints[1])
             if nx_graph.is_multigraph():
                 edges = nx_graph[edge_endpoints[0]][edge_endpoints[1]]
                 for i in range(len(edges)):
                     self.add_edge(set([u, v]), label=edges[i]["label"])
             else:
                 edge_label = nx_graph.edge[edge_endpoints[0]][edge_endpoints[1]]["label"]
                 self.add_edge(set([u, v]), label=copy.deepcopy(edge_label))
     
     # Initialize ready sets
     self.init_parallel_edges_groups()
     self.init_nodes_with_n_neighbors()
 def process_parallel_edgess(all_adj_nodes, i, new_feature):
     '''Process parallel edges between nodes u and v recursively
     through all adjacent pairs of nodes.
     '''
     u, v = all_adj_nodes[i]
     edges = nxext.get_edge_labels_and_dirs(feature, u, v)
     edges_count = len(edges)
     for j, edge in enumerate(edges):
         if j < edges_count - 1:
             _new_feature = new_feature.copy()
         else:
             # no need to copy when the graph will not be used for other iterations
             _new_feature = new_feature
         
         if edge[1] <= 0:
             _new_feature.add_edge(v, u, label=edge[0])
         if edge[1] >= 0:
             _new_feature.add_edge(u, v, label=edge[0])
         
         if i < len(all_adj_nodes) - 1:
             for processed_feature in process_parallel_edgess(all_adj_nodes, i + 1, _new_feature): 
                 yield processed_feature
         else:
             yield _new_feature