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

Graph.java import java.io.BufferedReader; import java.io.File; import java.io.Fi

ID: 3705003 • Letter: G

Question

Graph.java


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.StringTokenizer;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

public class Graph {
public int V;
public int E;
  
public LinkedList<Integer>[] adj;
  
public Graph()
{
V = 0;
E = 0;
}
  
public Graph(BufferedReader reader) throws IOException
{
String line;
line = reader.readLine();
V = Integer.parseInt(line);
line = reader.readLine();
E = Integer.parseInt(line);
adj = new LinkedList[V];
for (int v = 0; v < V; v++) {
adj[v] = new LinkedList<Integer>();
}
while ((line = reader.readLine()) != null) {
int tempV1, tempV2;
StringTokenizer st = new StringTokenizer(line, " ");
tempV1 = Integer.parseInt(st.nextToken());
tempV2 = Integer.parseInt(st.nextToken());
addEdge(tempV1, tempV2);
}
}
  
  
public void addEdge(int v, int w) {

}
  
public String tostring()
{
String s = new String();
s = "There are "+V+" vertices and "+E+" edges ";
for(int i=0;i<V;i++)
{
s = s+i+": ";
for(int j = 0; j<adj[i].size();j++)
{
s = s+adj[i].get(j)+" ";
}
s = s+" ";
  
}
return s;
}
  
}

mediumG.txt

250
1273
244 246
239 240
238 245
235 238
233 240
232 248
231 248
229 249
228 241
226 231
223 242
223 249
222 225
220 247
219 221
218 224
218 227
217 232
216 232
214 219
214 221
213 235
213 238
212 214
212 219
212 221
212 244
211 222

largeG.txt

1000000
7586063
999812 999997
999592 999782
999499 999881
999213 999297
999082 999896
999067 999159
999063 999354
999037 999626
998991 999808
998856 999431
998602 998726
998601 998719
998494 999657
998475 999232
998456 999256
998375 999945
998353 999790
998347 999348
998299 998335
998292 998433
998261 999240
998244 999924
998013 999598
998008 999192
997936 998069
997905 999016
997877 999937
997847 998290

xtraLargeG.txt

1000000
7586063
999812 999997
999592 999782
999499 999881
999213 999297
999082 999896
999067 999159
999063 999354
999037 999626
998991 999808
998856 999431
998602 998726
998601 998719
998494 999657
998475 999232
998456 999256
998375 999945
998353 999790
998347 999348
998299 998335
998292 998433
998261 999240
998244 999924
998013 999598
998008 999192
997936 998069
997905 999016
997877 999937
997847 998290

Implement a weighted graph class from the Graph.java used previously. Graph.java uses integer value for storing an edge. Instead of using integer value for storing edges, create an “Edge" class that holds information of edge. Edge class has following attributes (not limited to): Vertex v1, v2 int edgeID int edgeWeight Change the adjacency list to hold the vertex and edge weight. Write a driver program, which reads input files mediumGraph.txt, LargeGraph.txt and Xtra LargeGraph.txt (on Canvas) and display the weighted graphs by printing adjacency list.

Explanation / Answer

Code:

The following classes are given as shown below

Code:

Edge.java:

// implementing a edge class

class Edge{

// define required variable

Vertex v1,v2;

int edgeId;

int edgeWeight;

}

// define a class

class Vertex{

// declare variables and linkelist object

int Vid;

LinkedList Edge;

// define a method

public Vertex(){

    // assign

Vid = 0;

// make new linkedlist

Edge = new LinkedList();

}

}

Graph.java:

LinkedList.java: