     "source": [
      "from collections import deque\n",
      "\n",
      "\n",
      "def bfs(root, visit_func):\n",
      "    if root is None:\n",
      "        return\n",
      "    queue = deque()\n",
      "    queue.append(root)\n",
      "    root.visit_state = State.visited\n",
      "    while queue:\n",
      "        node = queue.popleft()\n",
      "        visit_func(node)\n",
      "        for adjacent_node in node.adjacent:\n",
      "            if adjacent_node.visit_state == State.unvisited:\n",
      "                queue.append(adjacent_node)\n",
      "                adjacent_node.visit_state = State.visited"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
 
</s> remove     "def bfs(root, visit_func):\n",
    "    # TODO: Implement me\n",
    "    pass"
 </s> add     "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        # TODO: Implement me\n",
    "        pass" </s> remove     "        bfs(nodes[0], self.results.add_result)\n",
 </s> add     "        graph.bfs(nodes[0], self.results.add_result)\n", </s> remove     "        bfs(nodes[0], self.results.add_result)\n",
 </s> add     "        graph.bfs(nodes[0], self.results.add_result)\n", </s> remove     "        graph = Graph()\n",
 </s> add     "        graph = GraphBfs()\n", </s> remove     "        graph = Graph()\n",
 </s> add     "        graph = GraphBfs()\n",
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
     "source": [
      "from collections import deque\n",
      "\n",
      "\n",
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
    "class GraphBfs(Graph):\n",
    "\n",
    "    def bfs(self, root, visit_func):\n",
    "        if root is None:\n",
    "            return\n",
    "        queue = deque()\n",
    "        queue.append(root)\n",
    "        root.visit_state = State.visited\n",
    "        while queue:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node)\n",
    "            for adjacent_node in node.adj_nodes.values():\n",
    "                if adjacent_node.visit_state == State.unvisited:\n",
    "                    queue.append(adjacent_node)\n",
    "                    adjacent_node.visit_state = State.visited"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 Move graph bfs to a class 
graphs_trees/graph_bfs/bfs_solution.ipynb