Question: So i know i have to wirte a search method but i need some hint please Graph: import java.util.ArrayList; /* * A class representing a

So i know i have to wirte a search method but i need some hint please

So i know i have to wirte a search method but i

Graph:

import java.util.ArrayList;

/* * A class representing a Graph as an adjacency matrix, * and providing search functions for the Graph. */

/* * Student 1 Name: * Student 1 Number: * * Student 2 Name: * Student 2 Number: * */

public class Graph {

boolean[][] adjMat;

// In the adjacency matrix representation of a Graph, // if adjMat[i][j] == true, then you can move from // node i to node j. // i.e. j is a neighbour of i. // The constructor builds the adjacency matrix, // with initially all values false. public Graph(int size) { adjMat = new boolean[size][size]; for (int i=0; i

// Add a transition to the adjacency matrix, // i.e. a neighbour relation between two nodes. public void add(int from, int to) { adjMat[from][to] = true; }

// Carry out uninformed search of the Graph, // from the start node to goal node. public boolean search(int start, int goal, boolean dp) { // The frontier is an ArrayList of Paths. ArrayList frontier = new ArrayList(); // Initially the frontier contains just the Path // containing only the start node. Path firstPath = new Path(start); frontier.add(firstPath); // Search until the goal is found, // or the frontier is empty. while (! frontier.isEmpty()) { // *** TO-DO *** // CARRY OUT DEPTH-FIRST OR BREADTH-FIRST SEARCH } return false; }

public static void main(String[] args) { // Create a Graph containing 7 nodes Graph g = new Graph(7); // Add edges to the Graph g.add(0, 1); g.add(0, 2); g.add(1,5); g.add(1,6); g.add(2, 3); g.add(3, 4); // select a search type boolean depthFirst = false; // start searching g.search(0,4, depthFirst); } }

path:

import java.util.ArrayList;

/* * DO NOT EDIT THIS CLASS * * A very simple class representing a Path (sequence of nodes). * * This is really just a wrapper for an ArrayList, with a few * helpful methods. */

public class Path {

// The only instance field is an ArrayList of Integers. // i.e. a Path consists of a sequence of nodes, and the // nodes are identified by their Integer id's. private ArrayList p; // First constructor: creates a Path consisting just of the // start node. public Path(Integer start) { p = new ArrayList(); p.add(start); } // Second constructor: creates a new Path by taking // an existing Path and extending it by one node. public Path(Path p2, Integer newNode) { p = new ArrayList(p2.returnList()); p.add(newNode); } // get the last node in the Path public int getLastNode() { return p.get(p.size()-1); } // get the entire ArrayList (sequence of nodes) public ArrayList returnList() { return p; } // a String representation of the Path public String toString() { return p.toString(); } }

2. To-Do You must complete the implementation of the search() method in the Graph class. That is the only part of the code you should change. The search() method must do the following: Display (using a print statement) every node that it inspects. If it finds the goal, display the path to a goal. Return true if the goal is found. Return false otherwise. Use either depth-first or breadth-first search, depending on the third parameter of the method. Note: in the lectures we talked about depth-first treating the frontier like a stack, and breadth-first treating it like a queue. In this code, we use an ArrayList regardless of the search method. You can just select Paths from one end of the ArrayList or the other, depending on the search method

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!