def closest_vertex(self, frm, target_list):
        """ closest_vertex(frm, target_list) -> id Uses bfs-like
        algorithm to find closest vertex to frm in target_list

        """
        if frm in target_list:
            return frm
        target_list = set(target_list)
        visited = set([frm])
        parent = {}
        q = Queue()
        q.push(frm)
        while 1:
            try:
                current = q.pop()
            except q.EmptyQueue:
                raise GraphException("no vertices reachable: %s %s" % (frm, list(target_list)))
            efrom = self.edges_from(current)
            for (to, eid) in efrom:
                if to in target_list:
                    return to
                if to not in visited:
                    parent[to] = current
                    q.push(to)
                    visited.add(to)
Example #2
0
    def closest_vertex(self, frm, target_list):
        """ closest_vertex(frm, target_list) -> id Uses bfs-like
        algorithm to find closest vertex to frm in target_list

        """
        if frm in target_list:
            return frm
        target_list = set(target_list)
        visited = set([frm])
        parent = {}
        q = Queue()
        q.push(frm)
        while 1:
            try:
                current = q.pop()
            except q.EmptyQueue:
                raise GraphException("no vertices reachable: %s %s" %
                                     (frm, list(target_list)))
            efrom = self.edges_from(current)
            for (to, eid) in efrom:
                if to in target_list:
                    return to
                if to not in visited:
                    parent[to] = current
                    q.push(to)
                    visited.add(to)
    def bfs(self, frm):
        """ bfs(frm:id type) -> dict(id type)
        Perform Breadth-First-Search and return a dict of parent id

        Keyword arguments:
        frm -- 'immutable' vertex id

        """
        visited = set([frm])
        parent = {}
        q = Queue()
        q.push(frm)
        while 1:
            try:
                current = q.pop()
            except q.EmptyQueue:
                break
            efrom = self.edges_from(current)
            for (to, eid) in efrom:
                if to not in visited:
                    parent[to] = current
                    q.push(to)
                    visited.add(to)
        return parent
Example #4
0
    def bfs(self, frm):
        """ bfs(frm:id type) -> dict(id type)
        Perform Breadth-First-Search and return a dict of parent id

        Keyword arguments:
        frm -- 'immutable' vertex id

        """
        visited = set([frm])
        parent = {}
        q = Queue()
        q.push(frm)
        while 1:
            try:
                current = q.pop()
            except q.EmptyQueue:
                break
            efrom = self.edges_from(current)
            for (to, eid) in efrom:
                if to not in visited:
                    parent[to] = current
                    q.push(to)
                    visited.add(to)
        return parent