Exemplo n.º 1
0
def shortest_path_cover_logn_apx(g: gt.Graph, weight: gt.EdgePropertyMap):
    started_with_directed = g.is_directed()
    if not g.is_directed():
        reversed_edges = np.fliplr(g.get_edges())
        g.set_directed(True)
        g.add_edge_list(reversed_edges)
        weight.a[-reversed_edges.shape[0]:] = weight.a[:reversed_edges.
                                                       shape[0]]

    if weight.value_type() not in [
            "bool", "int", "int16_t", "int32_t", "int64_t"
    ]:
        #min = np.min(weight.a)
        #min_second = np.min(weight.a[weight.a > min])

        eps = 1  #min_second - min
        scaled_weight = (np.ceil(weight.a / eps) *
                         (g.num_vertices() + 1)).astype(np.int)  # ints >= 1
    else:
        scaled_weight = weight.a * (g.num_vertices() + 1)

    summed_edge_weight = np.sum(scaled_weight)

    adjusted_weight = g.new_edge_property("long", vals=scaled_weight - 1)

    paths = []

    covered_vertices = set()

    while len(covered_vertices) != g.num_vertices():
        curr_paths = shortest_path_visiting_most_nodes(g, adjusted_weight,
                                                       covered_vertices,
                                                       summed_edge_weight)

        for path in curr_paths:
            paths.append(path)

            #if len(path) <= 2 switch to fast mode and just add single edges/vertices until done.
            path_vertices = set(path)
            for v in path_vertices.difference(covered_vertices):
                for w in g.get_in_neighbors(v):
                    adjusted_weight[g.edge(w, v)] += 1  #.a[list()] -= 1
                    if adjusted_weight[g.edge(
                            w, v)] % (g.num_vertices() + 1) != 0:
                        exit(5)

            new_covered = path_vertices.difference(covered_vertices)
            covered_vertices = covered_vertices.union(path_vertices)
            print(len(new_covered), len(path), len(covered_vertices), path)
    if not started_with_directed:
        g.set_directed(False)
        for e in reversed_edges:
            g.remove_edge(g.edge(e[0], e[1]))
    return paths
Exemplo n.º 2
0
def print_graph(g: gt.Graph):
    nodes = g.vertices()

    print('Directed' if g.is_directed() else 'Undirected')

    for node in nodes:
        prop = {}
        for key in g.vp.keys():
            prop[key] = g.vp[key][node]
        print(node, ' : ', prop)
    for e in g.edges():
        prop = {}
        for key in g.ep.keys():
            prop[key] = g.ep[key][e]
        print(e, ' : ', prop)
Exemplo n.º 3
0
def create_q_graph(a_graph: gt.Graph,
                   q_nodes: Union[None, Iterable[gt.Vertex]] = None,
                   q_edges: Union[None, Iterable[gt.Edge]] = None,
                   add_back_reference=True,
                   directed: Union[bool, None] = None) -> gt.Graph:
    _directed = directed if directed is not None else a_graph.is_directed()
    q = gt.Graph(directed=_directed)
    a_q_v = {}
    a_q_e = {}
    q_a_v = {}
    q_a_e = {}

    if q_nodes is None:
        q_nodes = set(a_graph.vertices())

    for v in q_nodes:
        nv = a_q_v[v] = q.add_vertex()
        q_a_v[nv] = v

    for p_type, vp_name in a_graph.vp.properties:
        if p_type != 'v':
            continue
        old_vp = a_graph.vp[vp_name]
        q.vp[vp_name] = q.new_vp(old_vp.value_type())
        new_vp = q.vp[vp_name]
        for v in a_graph.vertices():
            if a_graph.vertex_index[v] in q_nodes:
                new_vp[a_q_v[v]] = deepcopy(old_vp[v])

    if q_edges is None:
        q_edges = set(a_graph.edges())

    for e in q_edges:
        e_start, e_end = e
        if e_start in q_nodes and e_end in q_nodes:
            ne = q.add_edge(a_q_v[e_start], a_q_v[e_end])
            a_q_e[e] = ne
            q_a_e[ne] = e

    for p_type, ep_name in a_graph.ep.properties:
        if p_type != 'e':
            continue
        old_ep = a_graph.ep[ep_name]
        q.ep[ep_name] = q.new_ep(old_ep.value_type())
        new_ep = q.ep[ep_name]
        for e, ne in a_q_e.items():
            new_ep[ne] = deepcopy(old_ep[e])

    if add_back_reference:
        from_a_node = q.new_vp('int')
        q.vp['fromANode'] = from_a_node
        from_a_edge = q.new_ep('object')
        q.ep['fromAEdge'] = from_a_edge

        for v in q.vertices():
            from_a_node[v] = a_graph.vertex_index[q_a_v[v]]

        for e in q.edges():
            e_a = q_a_e[e]
            vs, ve = e_a
            from_a_edge[e] = (a_graph.vertex_index[vs],
                              a_graph.vertex_index[ve])

    return q
Exemplo n.º 4
0
    def __init__(self,
                 nodes=0,
                 copy_graph=None,
                 weighted=True,
                 directed=True,
                 **kwargs):
        '''
        @todo: document that
        see :class:`gt.Graph`'s constructor '''
        self._nattr = _GtNProperty(self)
        self._eattr = _GtEProperty(self)

        self._edges_deleted = False

        g = copy_graph.graph if copy_graph is not None else None

        if g is not None:
            from graph_tool import Graph as GtGraph
            from graph_tool.stats import remove_parallel_edges

            num_edges = copy_graph.edge_nb()

            if copy_graph._edges_deleted:
                # set edge filter for non-deleted edges
                eprop = g.new_edge_property("bool",
                                            vals=np.ones(num_edges,
                                                         dtype=bool))

                g.set_edge_filter(eprop)
                g = GtGraph(g, directed=g.is_directed(), prune=True)

            if not directed and g.is_directed():
                g = g.copy()
                g.set_directed(False)
                remove_parallel_edges(g)
            elif directed and not g.is_directed():
                g = g.copy()
                g.set_directed(True)

            self._from_library_graph(g, copy=True)

            # make edge id property map
            if "eid" in g.edge_properties:
                g.edge_properties["eid"].a = list(range(num_edges))
            else:
                eids = self._graph.new_edge_property("int",
                                                     vals=list(
                                                         range(self._max_eid)))

                g.edge_properties["eid"] = eids

            self._max_eid = num_edges
        else:
            self._graph = nngt._config["graph"](directed=directed)

            if nodes:
                self._graph.add_vertex(nodes)

            # make edge id property map
            self._max_eid = 0

            eids = self._graph.new_edge_property("int")

            self._graph.edge_properties["eid"] = eids
Exemplo n.º 5
0
def compute_hull(g: gt.Graph,
                 S,
                 weight=None,
                 dist_map=None,
                 comps=None,
                 hist=None,
                 compute_closure=True,
                 already_closed=None):
    """

    :param g:
    :param S:
    :param weight: if = None, unit distance is used, which is faster.
    :param dist_map: n*n array with the pairwise shortest distances. if = None, the function will compute it itself
    :param comps:
    :param hist:
    :param compute_closure: #hull=closure or geodetic set, which is faster
    :return:
    """
    n = g.num_vertices()

    I_S = np.zeros(g.num_vertices(), dtype=np.bool)

    I_S[S] = True

    q = queue.Queue()

    for v in S:
        if already_closed is None or v not in already_closed:
            q.put(v)

    if dist_map is None:
        dist_map = gt.topology.shortest_distance(g,
                                                 weights=weight).get_2d_array(
                                                     range(n)).T
        #dist_map = shortest_distance(g, weights=weight).get_2d_array(range(n))
        #is possible but is super slow and memory heavy for some reason. not possible on my 16gb machine with graphs |V| roughly 15k.

    while not q.empty():

        v = q.get()
        if compute_closure:
            starting_nodes = np.arange(g.num_vertices())[I_S]
        else:
            starting_nodes = np.arange(g.num_vertices())[S]
        starting_nodes = starting_nodes  #[starting_nodes > v] #assume undirected

        if comps is not None and not g.is_directed():
            vs_comp = comps.a[v]
            vs_comp = np.where(comps.a == vs_comp)[0]

            if np.all(I_S[vs_comp]):
                continue

        #all vertices x s.t. d(v,x)+d(x,s)=d(v,s) for some s \in S. These are exactly the ones on any shortest v-s-paths.
        #visited_nodes = np.any(dist_map[v,:]+dist_map[:,starting_nodes].T==dist_map[v,starting_nodes][:,np.newaxis],axis=0)

        visited_nodes = np.zeros(n, dtype=np.bool)

        #careful this is not linear runtime. but constructing the "predecessor dag" is very slow with the Visitor classes.
        if not g.is_directed():
            #debug= set()
            '''for s in starting_nodes:
                #if s <= v:
                #    continue
                #if already_closed is not None:
                #    if already_closed[v] and already_closed[s]:
                #        #print("yay")
                #        continue
                debug = debug.union(np.where(dist_map[v]+dist_map[s]==dist_map[v,s])[0])
                #visited_nodes[np.where(dist_map[v].a+dist_map[s].a==dist_map[v].a[s])[0]] = True'''

            visited_nodes[np.any(dist_map[v, :] + dist_map[:, starting_nodes].T
                                 == dist_map[v, starting_nodes][:, np.newaxis],
                                 axis=0)] = True

            #first_mins = starting_nodes[np.argmin(dist_map[:, starting_nodes], axis=1)]
            #second_mins = starting_nodes[np.argpartition(dist_map[:, starting_nodes], 1, axis=1)[:, 1].astype(np.int)]

            #visited_nodes[dist_map[first_mins, range(n)]+ dist_map[range(n),second_mins] == dist_map[first_mins,second_mins]] = True
        else:
            '''if np.issubclass_(dist_map[v].a.dtype, numbers.Integral):
                max_value = np.iinfo(dist_map[v].a.dtype).max
            else:
                max_value = np.finfo(dist_map[v].a.dtype).max
            visited_nodes[
                np.any(dist_map[v, :] + dist_map[:, starting_nodes].T == dist_map[v, starting_nodes][:, np.newaxis],
                       axis=0)] = True'''
            #reachable_starting_nodes = starting_nodes[dist_map[v].a[starting_nodes] < max_value]
            '''for i in range(n):
                if I_S[i]:
                    continue
                if np.any(dist_map[v].a[i] + dist_map[i].a[[reachable_starting_nodes]] == dist_map[v].a[reachable_starting_nodes]):
                    visited_nodes[i] = True'''

            visited_nodes[np.any(dist_map[v, :] + dist_map[:, starting_nodes].T
                                 == dist_map[v, starting_nodes][:, np.newaxis],
                                 axis=0)] = True

        if compute_closure:
            for i in range(n):
                if not I_S[i] and visited_nodes[i]:
                    q.put(i)

        I_S[visited_nodes] = True

        #early stopping if already covered all the connected components of S
        if comps is not None and not g.is_directed():
            if np.sum(I_S) == np.sum(hist[np.unique(comps.get_array()[I_S])]):
                break
        elif np.sum(I_S) == n:
            break

        #print (np.sum(I_S), n)

    return I_S
Exemplo n.º 6
0
class BaseGraph(object):
    """
    Class representing a graph. We do not use pure graph_tool.Graph for we want
    to be able to easily change this library. Neither we use inheritance
    as graph_tool has inconvenient licence.
    """
    def __init__(self):
        self._g = None
        self._node_dict = {}
        self._syn_to_vertex_map = None
        self._lemma_to_nodes_dict = None
        self._lu_on_vertex_dict = None

    def use_graph_tool(self):
        """
        Returns underlying graph_tool.Graph. It should be avoided at all costs.
        """
        return self._g

    def get_node_for_synset_id(self, syn_id):
        """
        Lazy function to makes the map of synset identifiers to nodes into
        the graph. The building of map is made only on the first funcion call.
        The first and the next calls of this function will return the built map.
        """
        if not self._syn_to_vertex_map:
            self._syn_to_vertex_map = {}
            for node in self.all_nodes():
                if node.synset:
                    synset_id = node.synset.synset_id
                    self._syn_to_vertex_map[synset_id] = node
        return self._syn_to_vertex_map.get(syn_id, None)

    def pickle(self, filename):
        self._g.save(filename)

    def unpickle(self, filename):
        self._g = load_graph(filename)

    def init_graph(self, drctd=False):
        self._g = Graph(directed=drctd)

    def copy_graph_from(self, g):
        self._g = g._g.copy()

    def set_directed(self, drctd):
        self._g.set_directed(drctd)

    def is_directed(self):
        return self._g.is_directed()

    def merge_graphs(self, g1, g2):
        self._g = graph_union(g1._g, g2._g, internal_props=True)

    # Node operations:
    def all_nodes(self):
        for node in self._g.vertices():
            yield BaseNode(self._g, node)

    def create_node_attribute(self, name, kind, value=None):
        if not self.has_node_attribute(name):
            node_attr = self._g.new_vertex_property(kind, value)
            self._g.vertex_properties[name] = node_attr

    def create_node_attributes(self, node_attributes_list):
        for attr in node_attributes_list:
            if not self.has_node_attribute(attr[0]):
                node_attr = self._g.new_vertex_property(attr[1])
                self._g.vertex_properties[attr[0]] = node_attr

    def has_node_attribute(self, name):
        """ Checks if a node attribute already exists """
        return name in self._g.vertex_properties

    def delete_node_attribute(self, name):
        """ Delete node attribute """
        del self._g.vertex_properties[name]

    def add_node(self, name, node_attributes_list=None):
        if node_attributes_list is None:
            node_attributes_list = []

        if name not in self._node_dict:
            new_node = self._g.add_vertex()
            self._node_dict[name] = BaseNode(self._g, new_node)
            for attr in node_attributes_list:
                self._g.vertex_properties[attr[0]][new_node] = attr[1]
        return self._node_dict[name]

    def get_node(self, name):
        return self._node_dict[name]

    def remove_node(self, name):
        self._g.remove_vertex(self._node_dict[name]._node)
        del self._node_dict[name]

    def nodes_filter(self,
                     nodes_to_filter_set,
                     inverted=False,
                     replace=False,
                     soft=False):
        """
        Filters out nodes from set

        Args:
          nodes_to_filter_set (Iterable): Nodes which fill be filtered out.
          inverted (bool): If True, nodes NOT in set will be filtered out.
            Defaults to False.
          replace (bool): Replace current filter instead of combining the two.
            Defaults to False.
          soft (bool): Hide nodes without removing them so they can be restored
            with reset_nodes_filter. Defaults to False.
        """
        predicate = lambda node: node not in nodes_to_filter_set
        self.nodes_filter_conditional(predicate, inverted, replace, soft)

    def nodes_filter_conditional(self,
                                 predicate,
                                 inverted=False,
                                 replace=False,
                                 soft=False):
        """
        Filters node based on a predicate

        Args:
          predicate (Callable): Predicate returning False for nodes that should be
            filtered out.
          inverted (bool): Invert condition. Defaults to False.
          replace (bool): Replace current filter instead of combining the two.
            Defaults to False.
          soft (bool): Hide nodes without removing them so they can be restored
            with reset_nodes_filter. Defaults to False.
        """

        (old_filter, old_inverted) = self._g.get_vertex_filter()
        new_filter = self._g.new_vertex_property("bool")

        for node in self.all_nodes():
            kept = predicate(node) != inverted
            if not replace and old_filter:
                old_kept = bool(old_filter[node._node]) != old_inverted
                kept = kept and old_kept
            new_filter[node._node] = kept

        self._g.set_vertex_filter(new_filter, False)
        if not soft:
            self.apply_nodes_filter()

    def apply_nodes_filter(self):
        """ Removes nodes that are currently filtered out """
        self._g.purge_vertices()

    def reset_nodes_filter(self):
        """ Clears node filter """
        self._g.set_vertex_filter(None)

    # Edge operations:
    def num_edges(self):
        return self._g.num_edges()

    def all_edges(self):
        for e in self._g.edges():
            yield BaseEdge(self._g, e)

    def get_edges_between(self, source, target):
        """
        Return all edges between source and target. Source and target can be either
        BaseNode or integer.
        """
        if isinstance(source, BaseNode):
            source = source._node
        if isinstance(target, BaseNode):
            target = target._node
        for e in self._g.edge(source, target, all_edges=True):
            yield BaseEdge(self._g, e)

    def get_edge(self, source, target, add_missing=False):
        """
        Return some edge between source and target. Source and target can be either
        BaseNode or integer.
        """
        if isinstance(source, BaseNode):
            source = source._node
        if isinstance(target, BaseNode):
            target = target._node
        e = self._g.edge(source, target, add_missing)
        if e is not None:
            return BaseEdge(self._g, e)
        else:
            return None

    def create_edge_attribute(self, name, kind, value=None):
        if not self.has_edge_attribute(name):
            edge_attr = self._g.new_edge_property(kind, value)
            self._g.edge_properties[name] = edge_attr

    def alias_edge_attribute(self, name, alias):
        self._g.edge_properties[alias] = self._g.edge_properties[name]

    def create_edge_attributes(self, edge_attributes_list):
        for attr in edge_attributes_list:
            if not self.has_edge_attribute(attr[0]):
                edge_attr = self._g.new_edge_property(attr[1])
                self._g.edge_properties[attr[0]] = edge_attr

    def has_edge_attribute(self, name):
        """ Checks if an edge attribute already existst """
        return name in self._g.edge_properties

    def delete_edge_attribute(self, name):
        """ Delete edge attribute """
        del self._g.edge_properties[name]

    def add_edge(self, parent, child, edge_attributes_list=None):
        if edge_attributes_list is None:
            edge_attributes_list = []

        new_edge = self._g.add_edge(parent._node, child._node)
        for attr in edge_attributes_list:
            self._g.edge_properties[attr[0]][new_edge] = attr[1]

        return BaseEdge(self._g, new_edge)

    def edges_filter(self, edges_to_filter_set):
        edge_filter = self._g.new_edge_property("bool")

        for e in self.all_edges():
            if e in edges_to_filter_set:
                edge_filter[e._edge] = False
            else:
                edge_filter[e._edge] = True

        self._g.set_edge_filter(edge_filter)
        self._g.purge_edges()

    def ungraph_tool(self, thingy, lemma_on_only_synset_node_dict):
        """
        Converts given data structure so that it no longer have any graph_tool dependencies.
        """
        logger = logging.getLogger(__name__)

        if type(thingy) == dict:
            return {
                self.ungraph_tool(k, lemma_on_only_synset_node_dict):
                self.ungraph_tool(thingy[k], lemma_on_only_synset_node_dict)
                for k in thingy
            }

        nodes_to_translate = set()
        for vset in lemma_on_only_synset_node_dict.values():
            for v in vset:
                nodes_to_translate.add(v)

        if type(thingy) == gt.PropertyMap:
            dct = {}
            if thingy.key_type() == 'v':
                for node in nodes_to_translate:
                    dct[node] = thingy[node.use_graph_tool()]
            elif thingy.key_type() == 'e':
                for edge in self.all_edges():
                    dct[edge] = thingy[edge.use_graph_tool()]
            else:
                logger.error('Unknown property type %s', thingy.key_type())
                raise NotImplemented
            return dct

    def generate_lemma_to_nodes_dict_synsets(self):
        """
        This method generates a utility dictionary, which maps lemmas to
        corresponding node objects. It is expensive in menas of time
        needed to generate the dictionary. It should therefore be executed
        at the beginning of the runtime and later its results should be reused
        as many times as needed without re-executing the function.
        """
        lemma_to_nodes_dict = defaultdict(set)
        for node in self.all_nodes():
            try:
                lu_set = node.synset.lu_set
            except KeyError:
                continue

            for lu in lu_set:
                lemma = lu.lemma.lower()
                lemma_to_nodes_dict[lemma].add(node)

        self._lemma_to_nodes_dict = lemma_to_nodes_dict

    def generate_lemma_to_nodes_dict_lexical_units(self):
        """
        This method generates a utility dictionary, which maps lemmas to
        corresponding node objects. It is expensive in menas of time
        needed to generate the dictionary. It should therefore be executed
        at the beginning of the runtime and later its results should be reused
        as many times as needed without re-executing the function.
        """
        lemma_to_nodes_dict = defaultdict(set)

        for node in self.all_nodes():
            try:
                lemma = node.lu.lemma.lower()
                lemma_to_nodes_dict[lemma].add(node)
            except:
                continue

        self._lemma_to_nodes_dict = lemma_to_nodes_dict

    @property
    def lemma_to_nodes_dict(self):
        return self._lemma_to_nodes_dict

    def _make_lu_on_v_dict(self):
        """
        Makes dictionary lu on vertex
        """
        lu_on_vertex_dict = defaultdict(set)
        for node in self.all_nodes():
            try:
                nl = node.lu
            except Exception:
                continue

            if nl:
                lu_on_vertex_dict[node.lu.lu_id] = node

        self._lu_on_vertex_dict = lu_on_vertex_dict