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 a graph

ID: 3771176 • Letter: D

Question

Develop an interactive Java program to implement following operations on a graph:

Create a graph From a file

Display Graph information (adjacency Lists)

Text (Display adjacency lists)

Traverse the graph (BFS)   (prompt for starting vertex)

Traverse the graph (DFS)   (prompt for starting vertex)

Find all shortest paths        (prompt for starting vertex)

GUI

Program should create a new graph using adjacency list representation of graph. Allow any graph with up to 50 nodes created (define an array of 50 elements for header nodes). 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

Explanation / Answer

I have implemented weighted bfs for you

package chegg_graph;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Graph {

   static Node[] nodeArr;
  
   public static void main(String[] args) {
       try {
           BufferedReader br = new BufferedReader(new FileReader("file.txt"));
           String line = null;
           line = br.readLine();
           int nodes = Integer.parseInt(line);
          
           if (nodes > 50) {
               System.out.println("Max 50 nodes are allowed. Setting nodes to 50");
               nodes = 50;
           }
          
           nodeArr = new Node[nodes];
           for (int i = 0; i < nodes; i++)
           {
               nodeArr[i] = new Node(i);
           }
           while ((line = br.readLine()) != null)
           {
               String[] arr = line.split(" ");
               int i = Integer.parseInt(arr[0]);
               int j = Integer.parseInt(arr[1]);
               int weight = Integer.parseInt(arr[2]);
              
               addEdge(nodeArr[i], nodeArr[j], weight);
           }
           BFS(nodes);
       } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
  
   public static void addEdge(Node i, Node j, int weight)
   {
       i.adjacent(j, weight);
   }
  
   public static void BFS(int nodes)
   {
       for (int i=0; i<nodes; i++){
           nodeArr[i].printAdjacent();
       }
   }
}

class Node {
   int value;
   Set<Node> adjacent = new HashSet<Node>();
   Map<Node, Integer> map = new HashMap<>();
  
   public Node(int value) {
       // TODO Auto-generated constructor stub
       this.value = value;
   }
  
   public void adjacent(Node node, int weight){
       this.adjacent.add(node);
       map.put(node, weight);
   }
  
   public void printAdjacent() {
       System.out.print(this.value);
       for (Node adj : adjacent) {
           int weight = map.get(adj);
           for (int i=0; i<weight; i++)
               System.out.print("-");
           System.out.print(adj.value);
       }
       System.out.println();
   }
}