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
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
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
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
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
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
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))
def __init__(self): self.max_size = 100 self._list = List()
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