|
585 | 585 | },
|
586 | 586 | {
|
587 | 587 | "cell_type": "code",
|
588 |
| - "execution_count": 3, |
| 588 | + "execution_count": 2, |
589 | 589 | "id": "23428e44",
|
590 | 590 | "metadata": {},
|
591 | 591 | "outputs": [],
|
|
610 | 610 | },
|
611 | 611 | {
|
612 | 612 | "cell_type": "code",
|
613 |
| - "execution_count": 4, |
| 613 | + "execution_count": 3, |
614 | 614 | "id": "32ada213",
|
615 | 615 | "metadata": {},
|
616 | 616 | "outputs": [],
|
|
619 | 619 | " visited = set()\n",
|
620 | 620 | " queue = deque([start])\n",
|
621 | 621 | "\n",
|
622 |
| - " while queue:\n", |
623 |
| - " current_vertex = queue.popleft()\n", |
| 622 | + " while queue: # While our queue isn't empty, do the following\n", |
| 623 | + " current_vertex = queue.popleft() # Look and remove at the first element in our queue\n", |
624 | 624 | "\n",
|
625 |
| - " if current_vertex not in visited:\n", |
| 625 | + " if current_vertex not in visited: # If it hasn't been visited, then:\n", |
626 | 626 | " # Process the current vertex\n",
|
627 |
| - " print(current_vertex, end=' ')\n", |
628 |
| - " visited.add(current_vertex)\n", |
| 627 | + " print(current_vertex, end=' ') # Print it\n", |
| 628 | + " visited.add(current_vertex) # Add it as visited\n", |
629 | 629 | "\n",
|
630 | 630 | " # Enqueue unvisited neighbors\n",
|
631 |
| - " for neighbor in graph.graph.get(current_vertex, []):\n", |
| 631 | + " for neighbor in graph.graph.get(current_vertex, []): # Add all of the vertex's neighbours to the queue (if not visi))\n", |
632 | 632 | " if neighbor not in visited:\n",
|
633 | 633 | " queue.append(neighbor)"
|
634 | 634 | ]
|
|
643 | 643 | },
|
644 | 644 | {
|
645 | 645 | "cell_type": "code",
|
646 |
| - "execution_count": 5, |
| 646 | + "execution_count": 12, |
647 | 647 | "id": "3b8d278a",
|
648 | 648 | "metadata": {},
|
649 | 649 | "outputs": [
|
650 | 650 | {
|
651 | 651 | "name": "stdout",
|
652 | 652 | "output_type": "stream",
|
653 | 653 | "text": [
|
654 |
| - "1 3 6 10 7 " |
| 654 | + "15 " |
655 | 655 | ]
|
656 | 656 | }
|
657 | 657 | ],
|
|
665 | 665 | "ex_graph.add_edge(7, [10, 6])\n",
|
666 | 666 | "\n",
|
667 | 667 | "# Perform BFS starting from vertex 1\n",
|
668 |
| - "bfs(ex_graph, 1)" |
| 668 | + "bfs(ex_graph, 15)" |
669 | 669 | ]
|
670 | 670 | },
|
671 | 671 | {
|
|
680 | 680 | },
|
681 | 681 | {
|
682 | 682 | "cell_type": "code",
|
683 |
| - "execution_count": 6, |
| 683 | + "execution_count": 9, |
684 | 684 | "id": "d883118d",
|
685 | 685 | "metadata": {},
|
686 | 686 | "outputs": [],
|
|
709 | 709 | },
|
710 | 710 | {
|
711 | 711 | "cell_type": "code",
|
712 |
| - "execution_count": 7, |
| 712 | + "execution_count": 10, |
713 | 713 | "id": "c4b48ff8",
|
714 | 714 | "metadata": {},
|
715 | 715 | "outputs": [
|
716 | 716 | {
|
717 | 717 | "name": "stdout",
|
718 | 718 | "output_type": "stream",
|
719 | 719 | "text": [
|
720 |
| - "1 3 6 10 7 " |
| 720 | + "1 3 10 7 6 " |
721 | 721 | ]
|
722 | 722 | }
|
723 | 723 | ],
|
724 | 724 | "source": [
|
725 |
| - "bfs(ex_graph, 1)" |
| 725 | + "recursive_preorder_traversal(ex_graph, 1)" |
726 | 726 | ]
|
727 | 727 | },
|
728 | 728 | {
|
|
0 commit comments