Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop an interactive Java program to implement following operations on an adja

ID: 3771384 • Letter: D

Question

Develop an interactive Java program to implement following operations on an adjacency list:

Create a graph From a file

Display Graph information (adjacency Lists) using GUI

Graph nodes are numbered from 1-N, where N is the number of nodes in the graph. The information about a graph should be read from a text file with the following layout:

N                              Number of nodes in the graph

i j w                       an edge exists between nodes i and j and weight is w

..

The rest of the file contains more edge information.

7

1 2 7

1 3 4

1 4 2

2 3 1

3 4 1

3 5 2

3 6 2

3 7 5

5 7 3

6 7 1

Once graph is created report graph information and allow the user select the other options.  

Explanation / Answer

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

class main{
   public static void main(String[] args){
       Scanner s = null;   // Scanner Class to read the file;
       try{
           s = new Scanner(new BufferedReader(new FileReader(filename))); // Open the file_name
           int n = Integer.parseInt(s.nextLine());
           int[][] graph = new int[n][n];   // Graph List
           while (s.hasNext() == true){
               int a = sc.next();
               int b = sc.next();
               int w = sc.next();
               // Edge Between a and b;
               graph[a-1][b-1] = w;
               // Edge between b and a as Edge is undirected;
               graph[b-1][a-1] = w;
           }
       }
       catch(FileNotFoundException e){
           e.printStackTrace();
       }
       finally{
           if (s != null)    s.close();
       }
   }
}