209 views
1 1 vote

An undirected graph has $\mathbf{7}$ vertices numbered from $1$ to $7$.

The adjacency list of the graph (neighbors are listed in increasing order):

 

If Breadth First Search (BFS) is performed starting from vertex $\mathbf{1}$, what is the order of traversal?

  1. $1234567$
     
  2. $1235467$
     
  3. $1326457$
     
  4. $1234657$

1 Answer

0 0 votes

Step 1: Initialize the BFS

 

The Breadth First Search (BFS) starts at vertex 1. A queue is used to keep track of vertices to visit, and a set tracks visited vertices. The neighbors of each vertex are listed in increasing order, which determines the order they are added to the queue.

  • Queue:
  • Visited: {1}
  • Traversal Order:

Step 2: Process vertex 1

 

Dequeue 1. Its neighbors (from the image) are 2 and 3 (in increasing order). Add them to the queue and visited set.

  • Queue:
  • Visited: {1, 2, 3}
  • Traversal Order:

Step 3: Process vertex 2

 

Dequeue 2. Its neighbors are 1, 4, 5. Vertex 1 has already been visited. Add 4 and 5 to the queue and visited set.

  • Queue:
  • Visited: {1, 2, 3, 4, 5}
  • Traversal Order:

Step 4: Process vertex 3

 

Dequeue 3. Its neighbors are 2 and 6. Vertex 2 has already been visited. Add 6 to the queue and visited set.

  • Queue:
  • Visited: {1, 2, 3, 4, 5, 6}
  • Traversal Order:

Step 5: Process vertex 4

 

Dequeue 4. Its only neighbor is 2, which has been visited.

  • Queue:
  • Visited: {1, 2, 3, 4, 5, 6}
  • Traversal Order:

Step 6: Process vertex 5

 

Dequeue 5. Its neighbors are 2 and 7. Vertex 2 has been visited. Add 7 to the queue and visited set.

  • Queue:
  • Visited: {1, 2, 3, 4, 5, 6, 7}
  • Traversal Order:

Step 7: Process vertex 6

 

Dequeue 6. Its only neighbor is 3, which has been visited.

  • Queue:
  • Visited: {1, 2, 3, 4, 5, 6, 7}
  • Traversal Order:

Step 8: Process vertex 7

 

Dequeue 7. Its only neighbor is 5, which has been visited. The queue is now empty.

  • Queue: []
  • Visited: {1, 2, 3, 4, 5, 6, 7}
  • Traversal Order:

Answer: (a) 1234567

 

Answer:
Position:
Show:

Related questions

1 1 vote
1 1 answer
223
223 views
GO Classes asked Jan 15
223 views
A binary tree satisfies the following properties:The tree has exactly one node with only one child All other non-leaf nodes have exactly two children The height of the tr...
1 1 vote
1 1 answer
211
211 views
GO Classes asked Jan 15
211 views
Consider the array:[7,3,5,2,6]The array is sorted using Insertion Sort in ascending order.How many element shifts (not comparisons) are performed during the entire sortin...
3 3 votes
1 1 answer
238
238 views
GO Classes asked Jan 15
238 views
Consider the following Python code:d = {} x = (1,2) y = (1, ) d[x] = 10 d[y] = 20 print(len(d))What is the output?$\verb|1|$ $\verb|2|$ $\verb|TypeError at d[y] = 20|$ $\...
2 2 votes
1 1 answer
212
212 views
GO Classes asked Jan 15
212 views
Consider the following Python code:a = [1,2,3] b = a b.append(4) c = a + [5] a[0] = 10 print(a, b, c)What is the output?$\verb|[10,2,3,4][10,2,3,4][1,2,3,4,5]|$ $\verb|[1...