Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I get error when trying to run it please help Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 4 at adjmatrix.graph.inputGraph(graph.java:167) at adjmatrix.graph. (graph.java:90) at adjmatrix.AdjMatrix.main(AdjMatrix.java:24) C:UsersJamarAppDataLocalNetBeansCache8.2executor-snippets un.xml:53:

image text in transcribed

I get error when trying to run it please help

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

at adjmatrix.graph.inputGraph(graph.java:167)

at adjmatrix.graph.(graph.java:90)

at adjmatrix.AdjMatrix.main(AdjMatrix.java:24)

C:\Users\Jamar\AppData\Local\NetBeans\Cache\8.2\executor-snippets un.xml:53: Java returned: 1

BUILD FAILED (total time: 0 seconds)

Here is my code

adjmatrix.java

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package adjmatrix;

import java.io.IOException;

/** * */ public class AdjMatrix {

private static graph graph;

/** * @param args the command line arguments * @throws java.io.IOException */ public static void main(String[] args) throws IOException { // TODO code application logic here graph = new graph("file location and file name"); } }

graph.java

package adjmatrix;

//********************************************************************

// graph.java

// Graph class which has data and methods to work with a graph.

// Note: The graph class takes the filename as an input parameter.

// File should exist in same directory as program.

// File format: The first line of the file is an integer n (number of vertices).

// Then there will be n lines each with n integers on each line

// with a space separating each integer (each integer is edge weight).

// Make sure no spaces follow last character on each line

// Make sure no blank lines or extra CR's follow last row

// Edge weights cannot exceed 900000000

//

// to run this program: > Call the class from another program.

// Example: graph a = new graph("graph.txt")

//--------------------------------------------------------------------

/**

*

*

*/

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintStream;

import java.util.StringTokenizer;

public final class graph {

/**

* class definitions

*/

private static final String INF = "&";

private static final int INFINITY = 999999999;

/**

* class variables

*/

private int numVerts;

private int[][] adjMatrix;

private BufferedReader graphStream;

public int current_edge_weight;

private int[] next;

/**

* default constructor

* included for completeness

*/

public graph() {

graph graph = new graph (10);

}

/**

* constructor; initializes new graph to all INFINITY

* @param numVs the number of vertices in the graph

*/

public graph (int numVs) {

numVerts = numVs;

adjMatrix = new int[numVerts][numVerts];

this.resetGraph();

// initializes neighbor array

next = new int[numVerts];

for (int i = 0; i

next[i] = -1;

}

/**

* constructor; inputs graph located in file

* @param filename

* @throws IOException if file is empty or cannot be opened

*/

public graph (String filename) throws IOException {

graphStream = fileIn (filename);

inputGraph();

// initializes neighbor array

next = new int[numVerts];

resetnext();

}

/**

* resets adjacency matrix to all INFINITY

*/

public void resetGraph() {

for (int row = 0; row

for (int col = 0; col

adjMatrix[row][col] = INFINITY;

adjMatrix[row][row] = 0;

}

}

/**

* opens a buffered reader using input filename

* @param file the input filename

* @return a buffered reader

*/

private BufferedReader fileIn (String file) {

BufferedReader br = null;

try

{

br = new BufferedReader (new FileReader (file));

}

catch (FileNotFoundException fnf)

{

System.out.println ("File " + file + " not found. ");

}

return br;

}

/**

* inputs the text graph into an adjacency matrix

*/

private void inputGraph() {

int row, col;

String line, current;

StringTokenizer st;

try

{

// get number of vertices from first line

line = graphStream.readLine();

st = new StringTokenizer(line);

current = st.nextToken();

numVerts = Integer.parseInt (current);

adjMatrix = new int[numVerts][numVerts];

row = 0;

while ((line = graphStream.readLine()) != null) {

st = new StringTokenizer (line);

col = 0;

while (col

current = st.nextToken();

if (current.compareTo (INF) == 0)

adjMatrix[row][col++] = INFINITY;

else

adjMatrix[row][col++] = Integer.parseInt (current);

}

row++;

}

}

catch (IOException io)

{

System.out.println ("File has I/O problems. ");

}

catch (NumberFormatException nfe)

{

System .out.println (" File must contain only integers. ");

}

finally

{

try

{

if (graphStream != null)

graphStream.close();

}

catch (IOException io2)

{

System.out.println ("Unable to close file. ");

}

}

}

//------------------------------------------------------------------------

public void insertVertex(int a, int x, int y) // insert a vertex

{

if(x == y) // if adjMatrix[i][i]

{

if(a != 0) // if value if not zero, display error and exit

{

System.out.println("Cannot initialize graph, adjMatrix[i][i] must be zero! So No Path Found... Exiting...");

System.exit(0);

}// end if

}// end outer if

adjMatrix[x][y] = a; // insert vertex into matrix

}// end method insertVertex()

//------------------------------------------------------------------------

/**

* prints adjacency matrix to screen

* @throws java.io.FileNotFoundException

*/

public void display() throws FileNotFoundException {

for (int row = 0; row

for (int col = 0; col

// prints symbol instead of 999999999

if (adjMatrix[row][col] > INFINITY - 0.01 * INFINITY)

System.out.print ("& ");

else

System.out.print ("" + adjMatrix[row][col] + " ");

System.out.println();

}

System.out.println(" ");

/* //Printing the results to a Sumamry_Output text file

PrintStream console = System.out;

File Summary = new File("Summary_Output.txt");

FileOutputStream fos = new FileOutputStream (Summary);

PrintStream ps = new PrintStream(fos);

System.setOut(ps);

System.out.println(" ");

System.out.println("");

System.out.println(" ");

System.setOut(console);

System.out.println("Thank You and GoodBye! The summary output is provided in a text file");

*/

}

/**

* accessor for class variable numVerts

* @return number of vertices in graph

*/

public int vertices() {

return numVerts;

}

/**

* accessor for edge weight between two vertices

* @param row the vertex from which edge extends

* @param col the vertex to which edge extends

* @return the edge weight between the two vertices

*/

public int edgeLength (int row, int col) {

return adjMatrix[row][col];

}

/**

* accessor to get one row of adjacency matrix

* @param row the row number requested

* @return the row requested from the adjacency matrix

*/

public int[] getRow (int row) {

int[] oneRow = new int[numVerts];

System.arraycopy(adjMatrix[row], 0, oneRow, 0, numVerts);

return oneRow;

}

//----------------------------------------------------

/**

* mutator for edge weight between two vertices

* @param row the vertex from which edge extends

* @param col the vertex to which edge extends

* @param edgeWeight the new weight for the edge

*/

public void setWeight (int row, int col, int edgeWeight) {

adjMatrix[row][col] = edgeWeight;

}

//---------------------------------------------------

public int nextneighbor(int v) {

next[v] = next[v] + 1;

if(next[v]

while(adjMatrix[v][next[v]] == 0 && next[v]

next[v] = next[v] + 1;

if(next[v] == numVerts)

break;

}

}

if(next[v] >= numVerts) {

next[v] = -1;

current_edge_weight = -1;

}

else

current_edge_weight = adjMatrix[v][next[v]];

return next[v];

}

//---------------------------------------------------------------------------

public void resetnext()

{

for (int i=0; i

next[i] = -1;

}// end method resetnext()

//---------------------------------------------------------------------------

}

The diagram below is an example of a directed graph. Each edge has an arrow denoting its direction Node B is considered adjacent to node A if there is a directed edge from A to B. A will only be adjacent to B if there is also an edge from B to A. It is possible to associate a matrix called the adjacency matrix with a graph. Adj[i] 1 if and only if there is an edge from node i to node j in the graph. If Adj[i] = 0 then there is no edge from node i to node j. (You may find it helpful to review the chapter in the text on graphs). We will assume that our adjacency matrices contain only 1's or 0's In a graph, one is usually interested in the possible paths from one node to another. The ones which are the most useful are the ones which do not contain loops (cycles), i.e.. do not visit a node more than once. The node sequence 1 2 4 is an example of a path with no loops. 12 241 3 is an example of a with a cycle and a loop. An allowable exception is for the starting and ending node to be the same. 1211 is OK but not 121241 To Nodes" 0 11 0 Adjacency From 111 1 Matrix Nodes 1 0 0 0 1 1 0 1 Write a program to read in the number of nodes in the graph and the corresponding adjacency matrix, one row at a time. Echo the adjacency matrix to your output file. The program should use recursion to find and list all possible non-looping paths between all possible pairs of nodes. It is ok if the start and end nodes are the same but otherwise the nodes should not repeat within the path. If no path exists for a particular pair of nodes then print 'No Path Found'. Check out all possible paths in each graph. See the attached file for the required input. Make up some other graphs as additional input. You will lose points for having insufficient I/O The recursion is the most important part of this assignment. Use an array to store the adjacency matrix. Consider why it is appropriate. You may not use ArrayLists. Consider an iterative solution as part of your analysis. What impact would that have on your data strucutres. our program does all that it is sup posed to do. If Remember, you are responsible for showing thaty you put in any special features, you need to have an /O set which demonstrates the features adjacency matrix fro m the file an Your program should read an paths, then read an new adjacency matrix, until the file is empty. Make sure to allow for reasonable error situations. d process the graph to find all the

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: 3

blur-text-image

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

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

ISBN: 061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago