Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Complete the following code (java): import java.util.ArrayList; /* * A class representing a Graph as an adjacency matrix, * and providing search functions for the

Complete the following code (java):

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 < adjMat.length; i++) { for (int j=0; j < adjMat.length; j++) { adjMat[i][j] = false; } } }

// 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); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students explore these related Databases questions