Ejemplo n.º 1
0
def bfs(head):
    visited_ids, visited_vals = set(), List()

    queue = Queue()
    queue.enqueue(head)

    visited_ids.add(head.idx)
    visited_vals.append(head.val)

    def step():
        if not queue:
            return

        node = queue.dequeue()

        for next_node in node.nexts:
            if next_node.idx not in visited_ids:
                queue.enqueue(next_node)
                visited_ids.add(next_node.idx)
                visited_vals.append(next_node.val)

        step()
        return visited_vals

    step()
    return visited_vals
Ejemplo n.º 2
0
 def items(self):
     pairs = List()
     for kv_list in self.arr:
         if kv_list is not None:
             for kv in kv_list:
                 pairs.append((kv.key, kv.val))
     return pairs
Ejemplo n.º 3
0
def dfs(head):
    visited_ids, visited_vals = set(), List()

    def step(node):
        if node.idx not in visited_ids:
            visited_ids.add(node.idx)
            visited_vals.append(node.val)
            for node in node.nexts:
                step(node)

    step(head)
    return visited_vals
Ejemplo n.º 4
0
    def __setitem__(self, key, val):
        idx = self.hash_func(key)
        kv_list = self.arr[idx]

        if kv_list is None:
            self.arr[idx] = List()
            self.arr[idx].append(KeyVal(key=key, val=val))
            self.len += 1
        else:
            kv_idx = HashMap._find_key_in_list(kv_list, key)
            if kv_idx is None:
                self.arr[idx].append(KeyVal(key=key, val=val))
                self.len += 1
            else:
                self.arr[idx][kv_idx].val = val
Ejemplo n.º 5
0
def dfs(adj_list, ii_start):
    visited, ii_order = set(), List()

    def step(idx):
        if idx in visited:
            return

        else:
            visited.add(idx)
            ii_order.append(idx)

            for j in adj_list[idx]:
                step(j)

    for node_idx in ii_start:
        step(node_idx)

    return ii_order
Ejemplo n.º 6
0
def bfs(adj_list, i_start=0):
    visited, ii_order = set(), List()
    queue = Queue()

    queue.enqueue(i_start)
    visited.add(i_start)
    ii_order.append(i_start)

    def step():
        if not queue:
            return
        else:
            i = queue.dequeue()
            for j in adj_list[i]:
                if j not in visited:
                    visited.add(j)
                    ii_order.append(j)
                    queue.enqueue(j)
            step()

    step()
    return ii_order
Ejemplo n.º 7
0
 def list(self, id: str = None, use_log: bool = False) -> List:
     return List(app_key=self.app_key,
                 token=self.app_token,
                 id=id,
                 use_log=self.swith_loggingging(use_log))
Ejemplo n.º 8
0
 def __init__(self):
     self.max_size = 100
     self._list = List()
Ejemplo n.º 9
0
def adj_mat_to_adj_list(adj_mat):
    adj_list = List()
    for inds in adj_mat:
        adj_list.append(np.nonzero(inds)[0].tolist())

    return adj_list