Question

Creating Graphs First, write Java code in GraphTest.java to read a data file (graph.in) and create a graph using the input da

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you like the solution please give it a thumbs up!

Solution :-

Java Code :-

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

class GraphTest
{
   int numVertex;   // number of vertices
   char graph;       // 'U' -> if undirected, 'D' -> if directed graph
   int[][] adj;   //2-D matrix to represent graph adj[i][j] -> weight of edge (i,j)
   public void read() throws Exception
   {

       // reading data from file using scanner
       Scanner sc = new Scanner(new File("graph.in"));

       numVertex = sc.nextInt();

       adj = new int[numVertex][numVertex];

       graph = sc.next().charAt(0);

       while(sc.hasNext())
       {
           int u,v,w;

           u = sc.nextInt();
           v = sc.nextInt();
           w = sc.nextInt();

           adj[u][v] = w;

           if(graph == 'U')
               adj[v][u] = w;

       }
   }

   public void print()
   {
       System.out.println("printing graph in form adjacency matrix :- ");

       for(int i=0;i<numVertex;i++)
       {
           for(int j=0;j<numVertex;j++)
               System.out.print(adj[i][j] + " ");

           System.out.print("\n");
       }
   }
   public static void main(String args[]) throws Exception
   {
       GraphTest obj = new GraphTest();

       obj.read();

       obj.print();
   }
}

Sample output :-

printing graph in form adjacency matrix :-
0 3 0 5 1
3 0 4 0 0
0 4 0 0 8
5 0 0 0 0
1 0 8 0 0

Add a comment
Know the answer?
Add Answer to:
Creating Graphs First, write Java code in GraphTest.java to read a data file (graph.in) and create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Design a format for storing graphs in files. This will store your graph into a file with t...

    in C++ Design a format for storing graphs in files. This will store your graph into a file with the following requirements: The first line will contain the number of Vertices. The second line will have a ‘U’ or a ‘D’ to tell the system if it is Undirected or Directed. The rest of the lines will be here for each of the edges. Each edge will contain three pieces of information: An integer containing the first Vertex An integer...

  • Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, ...

    Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...

  • In java, how can I convert this text file format: n=6 m=7 1 2 5 4...

    In java, how can I convert this text file format: n=6 m=7 1 2 5 4 9 5 6 2 3 2 3 4 3 5 6 2 6 4 2 // end of file into this text file format: 1,2,5 1,4,9 1,5,3 2,3,2 3,4,3 5,6,2 6,4,2 This is how to read the first text file: The first line of each file below contains the number of vertices and the number of edges in the graph (in the format "n=XXXX...

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • Consider the java Graph class below which represents an undirected graph in an adjacency list. How...

    Consider the java Graph class below which represents an undirected graph in an adjacency list. How would you add a method to delete an edge from the graph? // Exercise 4.1.3 (Solution published at http://algs4.cs.princeton.edu/) package algs41; import stdlib.*; import algs13.Bag; /** * The <code>Graph</code> class represents an undirected graph of vertices * named 0 through V-1. * It supports the following operations: add an edge to the graph, * iterate over all of the neighbors adjacent to a vertex....

  • Java Programming 1) zipped project folder 2) executable jar file 3) UML and running results screen...

    Java Programming 1) zipped project folder 2) executable jar file 3) UML and running results screen shot in a word file with names of your team 5 points will be taken for each missing file Problem Description A graph consists of vertices and edges that connect vertices Write a program that reads a graph from a file and displays it on a panel. The first line in the file contains a number that indicates the number of vertices (n) The...

  • Help with Java Program Please Create a simple graph class. The graph class should have the...

    Help with Java Program Please Create a simple graph class. The graph class should have the following items: an adjacency list or matrix to hold the graph data variables to hold the current size and max size of the graph default constructor create empty adjacency list/matrix max size = 10 overloaded constructor create empty adjacency list/matrix max size = int parameter isEmpty check if current size is zero createGraph read a formatted file and fill adjacency list/matrix The first line...

  • You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a...

    You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a graph stored as an adjacency list. The AdjacencyList class inherits from the Graph class shown below. class Graph { private: vector _distances; vector _previous; public: Graph() { } virtual int vertices() const = 0; virtual int edges() const = 0; virtual int distance(int) const = 0; virtual void bfs(int) const = 0; virtual void dfs(int) const = 0; virtual void display() const = 0;...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT