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

In java, implement a weighted graph class from the Graph.java. Graph.java uses i

ID: 3702838 • Letter: I

Question

In java, implement a weighted graph class from the Graph.java. 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, LargeG.txt and XtraLargeG.txt and display the weighted graphs by printing adjacency list.

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

Explanation / Answer

public class Edge {

private int edgeID;
private int edgeWeight;
private Vertex vertex1;
private Vertex vertex2;
  
public Edge() {
super();
}
  
public Edge(int edgeID, int edgeWeight, Vertex vertex1, Vertex vertex2) {
super();
this.edgeID = edgeID;
this.edgeWeight = edgeWeight;
this.vertex1 = vertex1;
this.vertex2 = vertex2;
}

public int getEdgeID() {
return edgeID;
}
  
public void setEdgeID(int edgeID) {
this.edgeID = edgeID;
}
  
public int getEdgeWeight() {
return edgeWeight;
}
  
public void setEdgeWeight(int edgeWeight) {
this.edgeWeight = edgeWeight;
}
  
public Vertex getVertex1() {
return vertex1;
}
  
public void setVertex1(Vertex vertex1) {
this.vertex1 = vertex1;
}
  
public Vertex getVertex2() {
return vertex2;
}
  
public void setVertex2(Vertex vertex2) {
this.vertex2 = vertex2;
}
  
@Override
public String toString() {
return "Edge [edgeID=" + edgeID + ", edgeWeight=" + edgeWeight + ", vertex1=" + vertex1 + ", vertex2=" + vertex2
+ "]";
}
}

class Vertex {
  
private int vertexID;
  
public Vertex(int vertexID) {
this.vertexID = vertexID;
}
  
public int getVertexID() {
return this.vertexID;
}
  
public void setVertexID(int vertexID) {
this.vertexID = vertexID;
}
}

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

class Graph {
  
/* Adjacency List
*
* Edge will contain both vertices
* */
Map<Vertex, List<Edge>> adjacencyList = new HashMap<>();


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote