0
votes

My Code won't run after doing a few tweaks. I was trying to create a java program which stores information about a graph (di-graph) then performs several operations on the graph. The program will read the data from an input file named graph.txt. The text file will have the following format

The first line contains a number which represents how many vertices will the graph contain. The second line will be the start of the label for the vertices. After the labels for the vertices are the edges. (vertices which edge connects to) After reading the contents of the input file the program will perform the following:

  1. Perform a Breadth-First Traversal and display the order of visited vertices.

  2. Perform a Depth-First Traversal and display the order of visited vertices. Below is my code I am currently using intelij as my IDE
    Any help would be appriciated Thankyou

    import java.io.*; import java.util.Scanner;

    public class Lee { public static void main(String[] args) throws IOException { File file = new File("graph.txt").getAbsoluteFile(); InputStream in = null; BufferedReader reader = null; String[] nodes = null; int[][] matrix = null;

         try {
             in = new FileInputStream(file);
             reader = new BufferedReader(new InputStreamReader(in));
    
             int i, ii, size = Integer.parseInt(reader.readLine());
             nodes = new String[size];
             matrix = new int[size][size];
             for (i = 0; i < size; i++) {
                 String node = reader.readLine();
                 if (node == null) throw new IllegalStateException("Insufficient nodes");
                 nodes[i] = node;
    
                 matrix[i][i] = -1;
                 for (ii = 0; ii < size; ii++) {
                     matrix[i][ii] = -1;
                     matrix[ii][i] = -1;
                 }
             }
             int fi, ti;
             String from, to;
             while ((from = reader.readLine()) != null && (to = reader.readLine()) != null) {
                 fi = indexOfOrThrow(nodes, from);
                 ti = indexOfOrThrow(nodes, to);
    
                 matrix[fi][ti] = 1;
                 // TODO: If not bidirectional then remove next line
                 matrix[ti][fi] = 1;
             }
         } catch (Throwable tr) {
             tr.printStackTrace();
             return;
         } finally {
             try {
                 if (reader != null) reader.close();
                 if (in != null) in.close();
             } catch (Throwable ignored) {
             }
         }
     }
     private static int indexOfOrThrow(String[] nodes, String node) {
         int i = indexOf(nodes, node);
         if (i < 0) throw new IllegalArgumentException("No entry found for: " + node);
         return i;
     }
    
     private static int indexOf(String[] nodes, String node) {
         if (node == null) {
             for (int i = nodes.length - 1; i >= 0; i--) {
                 if (nodes[i] == null) return i;
             }
         } else {
             for (int i = nodes.length - 1; i >= 0; i--) {
                 if (node.equals(nodes[i])) return i;
             }
         }
         return -1;
     }
    

    }

1

1 Answers

0
votes

10

A

B C

D

E

F

G

H

I

J

A E

A H

B C

D A

D H

E F

F B

F C

F G

F I

H E

H I

I J

J G This was the Graph.txt