Question: Rewrite the depthFirstPrint method from Figure 14.4 so that it carries out a breadth-first search instead. FIGURE 14.4 Depth-First Search Implementations public static void depthFirstRecurse

Rewrite the depthFirstPrint method from Figure 14.4 so that it carries out a breadth-first search instead.

FIGURE 14.4 Depth-First Search Implementations public static  void depthFirstRecurse (Graph g, int v, boolean[ ] marked) { int[ ] connections = g.neighbors(v); int i; marked [v] = true; System.out.println(g.getLabel(v)); // Traverse all the neighbors, looking for unmarked vertices: for (int nextNeighbor : connections) { if (!marked [nextNeighbor]) depthFirstRecurse(g, nextNeighbor,

FIGURE 14.4 Depth-First Search Implementations public static void depthFirstRecurse (Graph g, int v, boolean[ ] marked) { int[ ] connections = g.neighbors(v); int i; marked [v] = true; System.out.println(g.getLabel(v)); // Traverse all the neighbors, looking for unmarked vertices: for (int nextNeighbor : connections) { if (!marked [nextNeighbor]) depthFirstRecurse(g, nextNeighbor, marked); } public static void depthFirstPrint(Graph g, int start) { boolean[ ] marked = new boolean [g.size( )]; depthFirstRecurse (g, start, marked); }

Step by Step Solution

3.41 Rating (170 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Breadth First Search uses Queue and Depth first search uses Stack ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Data Structures and Other Objects Using Java Questions!