Graph Exercises for Section 10.4; Self-Check #1
ANSWER
Breadth-First Search Tree (BFS):
Breadth-First Search explores all the vertices at the current level before moving to the next level. The BFS tree is constructed in a level-by-level manner, starting from the source vertex.
- Start from the source vertex.
- Enqueue the source vertex into a queue.
- While the queue is not empty: a. Dequeue a vertex from the front of the queue. b. Visit the dequeued vertex. c. Enqueue all unvisited adjacent vertices of the dequeued vertex into the queue. d. Mark the dequeued vertex as visited.
- Repeat steps 3 until the queue is empty.
The BFS tree is formed as you visit each vertex, and it will reflect the shortest path from the source vertex to all other vertices.
Depth-First Search Tree (DFS):
Depth-First Search explores as far as possible along one branch before backtracking. The DFS tree is constructed by exploring as deep as possible along each branch before moving to the next branch.
- Start from the source vertex.
- Push the source vertex onto a stack.
- While the stack is not empty: a. Pop a vertex from the top of the stack. b. Visit the popped vertex. c. Push all unvisited adjacent vertices of the popped vertex onto the stack. d. Mark the popped vertex as visited.
- Repeat steps 3 until the stack is empty.
The DFS tree is formed as you visit each vertex, and it may not necessarily reflect the shortest path.
Now, for your programming request, here’s a basic outline of a DepthFirstSearch class in Python with accessor methods and a constructor:
class DepthFirstSearch:
def __init__(self, graph, start_vertices):
self.graph = graph
self.start_vertices = start_vertices
self.visited = set()
self.stack = []
def depth_first_search(self):
for start_vertex in self.start_vertices:
if start_vertex not in self.visited:
self._dfs(start_vertex)
def _dfs(self, vertex):
self.stack.append(vertex)
while self.stack:
current_vertex = self.stack.pop()
if current_vertex not in self.visited:
self.visited.add(current_vertex)
# Process current_vertex here (e.g., print or store in the DFS tree)
for neighbor in self.graph.get_adjacent_vertices(current_vertex):
if neighbor not in self.visited:
self.stack.append(neighbor)
def get_visited_vertices(self):
return list(self.visited)
def get_dfs_tree(self):
# Implement logic to construct and return the DFS tree if needed
pass
In this outline, the DepthFirstSearch
class uses a stack to implement DFS without recursion. You’ll need to implement the get_adjacent_vertices
method in your graph class to get the adjacent vertices of a given vertex.
Please adapt this outline to your specific graph data structure and requirements.
QUESTION
Description
Show the breadth-first search trees for the following graphs.
Show the depth-first search trees for the graphs in Exercise 1 above.
PROGRAMMING
Provide all accessor methods for class DepthFirstSearch and the constructor that specifies the order of start vertices.
Implement method depthFirstSearch without using recursion. Hint: Use a stack to save the parent of the current vertex when you start to search one of its adjacent vertices.