Question

(5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void breadthFirstSearcb. (3 marks) Consider the pseudocode for breadth-first search in (a.) above. Define the DISTANCE between two vertices and y i

(5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void breadthFirstSearch (vertex w) for all vertices u num (u) 0 null edges i=1; num (w) i++ enqueue (w) while queue is not empty dequeue ( V= for all vertices u adjacent to v if num(u) is 0 num (u) = i++; enqueue (u) attach edge (vu) to edges; output edges; Now consider the following graph. Give the breadth-first traversal of the graph, starting from vertex 1. At any vertex where there is a choice of edge you should choose the next vertex to be visited based on ascending numeric order 3 4 6 10 The minor modifications are i there is a return type for the function; ii there is an argument to supply for the starting point of the search; and iii the code is guaranteed to start the search at the specified starting point. the Drozdek pseudocode on connected graphs This will behave the same as
b. (3 marks) Consider the pseudocode for breadth-first search in (a.) above. Define the DISTANCE between two vertices and y in a graph as being the minimal number of edges between them. The distance from a vertex to itself is 0; if the graph is not connected, and there is no path between the vertices ax and y, the distance is oo In the graph in (a.), the following are some example distances vertex y distance vertex r 1 1 4 10 2 1 6 3 In the graph in (a.), which vertex (vertices) is (are) at the maximum distance from vertex 1? What is that distance? (8 marks) Give a modified version of the pseudocode in (a.) that will return all the vertices visited during the breadth-first search starting from vertex w that are at maximum (finite) distance from w. c. List maxDistVerticesBFS (int w) PRE Vertex w exists // POST: Given the graph is not empty, returns all vertices at maximum finite distance from w (Hint: Introduce a variable to store distance from the source.)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question A)

Starting from vertex 1, the algorithm will push 3rd and 4th vertex. After that, from vertex 3 the algorithm will push 7th vertex. From vertex 4, vertex 2 and then vertex 8 will be pushed. After that from vertex 7, vertex 5 and then vertex 9 will be pushed. From vertex 2, vertex 6 will be pushed. After that, from adjacent nodes of the vertex 8, there is no vertex left which was not pushed, so the algorithm will go on searching adjacent vertexes of vertex 5. We have same case here, so algorithm will continue finding adjacent vertexes of vertex 9. These vertexes are 10 and 11, so they will be added to queue. Now we have all vertexes visited in order 1 => 3 => 4 => 7 => 2 => 8 => 5 => 9 => 6 => 10 => 11.

Question B)

The biggest distance between 1 and other vertexes is 4. vertex 10 and 11 are at the distance of 4 from vertex 1.

Question C)

vector < int > maxDistverticesBFS (int w) for all vertices u num (u) = 0 i 1; i++ num (w dist = 0 1/Second variable is distan


comment down for any queries

please give a thumbs up

vector < int > maxDistverticesBFS (int w) for all vertices u num (u) = 0 i 1; i++ num (w dist = 0 1/Second variable is distance from vertex w to current vertex enqueue (w, 0); while queue is now empty dequeue ); answer (v.first) for all vertices adjacent if num (u) is 0 V = = v. second u to v.first i++ (u) num enqueue (u, v.second 1); return answer;

Add a comment
Know the answer?
Add Answer to:
(5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Show the operation of depth-first search (DFS) on the graph of Figure 1 starting from vertex...

    Show the operation of depth-first search (DFS) on the graph of Figure 1 starting from vertex q. Always process vertices in alphabetical order. Show the discovery and finish times for each vertex, and the classification of each edge. (b) A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first search (BFS) tree can also be used to classify the edges reachable from the source of the search into the same four categories....

  • Consider the following directed graph for each of the problems: 1. Perform a breadth-first search on...

    Consider the following directed graph for each of the problems: 1. Perform a breadth-first search on the graph assuming that the vertices and adjacency lists are listed in alphabetical order. Show the breadth-first search tree that is generated. 2. Perform a depth-first search on the graph assuming that the vertices and adjacency lists are listed in alphabetical order. Classify each edge as tree, back or cross edge. Label each vertex with its start and finish time. 3. Remove all the...

  • from collections import defaultdict    # This class represents a directed graph using # adjacency list...

    from collections import defaultdict    # This class represents a directed graph using # adjacency list representation class Graph:        # Constructor     def __init__(self):            # default dictionary to store graph         self.graph = defaultdict(list)        # function to add an edge to graph     def addEdge(self,u,v):         self.graph[u].append(v)        # Function to print a BFS of graph     def BFS(self, s):            # Mark all the vertices as not visited         visited = [False] * (len(self.graph))            # Create a queue for BFS         queue...

  • You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a...

    You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a graph stored as an adjacency list. The AdjacencyList class inherits from the Graph class shown below. class Graph { private: vector _distances; vector _previous; public: Graph() { } virtual int vertices() const = 0; virtual int edges() const = 0; virtual int distance(int) const = 0; virtual void bfs(int) const = 0; virtual void dfs(int) const = 0; virtual void display() const = 0;...

  • (c) Simulate breadth first search on the graph shown in Fig HW2Q1c. You can assume that...

    (c) Simulate breadth first search on the graph shown in Fig HW2Q1c. You can assume that the starting vertex is 1, and the neighbors of a vertex are examined in increasing numerical order (i.e. if there is a choice between two or more neighbors, we pick the smaller one). You have to show: both the order in which the vertices are visited and the breadth first search tree. No explanations necessary. (d) On the same graph, i.e. the graph in...

  • Question 3 1 pts Select all of the following that are true: Breadth-First Search only adds...

    Question 3 1 pts Select all of the following that are true: Breadth-First Search only adds each vertex to the queue once. Breadth-First Search generally uses more space compared to Depth-First search. Breadth-First Search may not find the shortest path for an unweighted graph if the graph contains a cycle. At any time during Breadth-First Search, the queue holds at most two distinct dist values from all vertices in

  • In Python 3 please Apply Breadth First Search (BFS) to traverse the following graph. Start your...

    In Python 3 please Apply Breadth First Search (BFS) to traverse the following graph. Start your traversal from vertex 0, and write down the order in which vertices will be visited during the traversal. 1 8 6 7 2 9 5 4 3

  • 7.[6] Consider the graph G below: a.[3] Find a Depth-First Search tree T for the above...

    7.[6] Consider the graph G below: a.[3] Find a Depth-First Search tree T for the above graph starting with the vertex 0. Show all the vertices as they are discovered in sequence starting from 1 to the last vertex included in T. b.[3] Find a Breadth-First Search tree T for the above graph starting with the vertex 0. Show all the vertices as they are discovered in sequence starting from 1 to the last vertex included in T.

  • Exam 3 Sample.pdf * ) Q © w E © 112 A n o 99.9% 1....

    Exam 3 Sample.pdf * ) Q © w E © 112 A n o 99.9% 1. Breadth-first Search a) List out the following graph using adjacency list. Assume the adjacency lists are in sorted order, e.g. when exploring vertex F, the algorithm considers the edge F-B before F-C, F-E, F-H or F-I. b) Run breadth-first-search on the graph below, starting at vertex A. List the vertices in the order in which the vertices are enqueued on the FIFO queue. c)...

  • 1- Give an example (by drawing or by describing) of the following undirected graphs (a) A...

    1- Give an example (by drawing or by describing) of the following undirected graphs (a) A graph where the degree in each vertex is even and the total number of edges is odd (b) A graph that does not have an eulerian cycle. An eulerian cycle is a cycle where every edge of the graph is visited exactly once. (c) A graph that does not have any cycles and the maximum degree of a node is 2 (minimum degree can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT