Lecture
Coding Quiz
In this coding quiz, you are tasked with writing a program that uses the Breadth-First Search (BFS) algorithm to begin at a specific node of a graph and visit all the nodes.
The user will provide an adjacency list representing the nodes in the graph and their connections. Write a program to perform BFS starting from a specific node and print the order in which all nodes are visited.
Write Code
def solution(graph): # Write your code here return
Constraints
-
The graph is an undirected graph.
-
All nodes are represented by numbers, starting from 1.
-
The graph has at least one node and at most 100 nodes.
-
There is at most one edge between any two nodes.
-
The starting node is always node 1.
Input and Output Example
Input
-
Adjacency List:
{1: [2, 3], 2: [4], 3: [4], 4: []} -
Starting Node:
1
Output
- Order of Visits:
[1, 2, 3, 4]
Previous lessonBreadth-First Search (BFS) Implementation GuideNext lessonWhat are Greedy Algorithms?
Lessons in this chapter · Graph Data Structures and Greedy Algorithms
- 1. What is a Graph?
- 2. Graph Implementation Techniques
- 3. Multiple-choice quiz
- 4. What is Depth First Search (DFS)?
- 5. Depth First Search (DFS) Implementation
- 6. Coding Quiz - Visit Order (DFS)
- 7. What is Breadth-First Search (BFS)?
- 8. Breadth-First Search (BFS) Implementation Guide
- 9. Coding Quiz - Order of Visits (BFS)
- 10. What are Greedy Algorithms?
- 11. Greedy Algorithm Implementation Guide
- 12. Multiple-choice quiz
- 13. Coding Quiz - ATM Problem
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help