Question: Weighted edge graph java program help? Please also show you implementing it in a main method! Thank you! My Graph.java class import java.io.BufferedReader; import java.io.IOException;

 Weighted edge graph java program help? Please also show you implementing

Weighted edge graph java program help? Please also show you implementing it in a main method! Thank you!

My Graph.java class

import java.io.BufferedReader;

import java.io.IOException;

import java.util.LinkedList;

import java.util.Stack;

import java.util.StringTokenizer;

import java.util.Iterator;

public class Graph {

public int V;

public int E;

public LinkedList[] adj;

public Graph()

{

V = 0;

E = 0;

}

@SuppressWarnings("unchecked")

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

adj[v] = new LinkedList();

}

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) {

adj[v].add(w);

}

public String tostring()

{

String s = new String();

s = "There are "+V+" vertices and "+E+" edges ";

for(int i=0;i

{

s = s+i+": ";

for(int j = 0; j

{

s = s+adj[i].get(j)+" ";

}

s = s+" ";

}

return s;

}

// A recursive function used by topologicalSort

void topoSort(int v, boolean visited[], Stack stack)

{

// Mark the current node as visited.

visited[v] = true;

Integer i;

// Recur for all the vertices adjMacent to this

// vertex

Iterator ints = adj[v].iterator();

while (ints.hasNext())

{

i = ints.next();

if (!visited[i]) {

topoSort(i, visited, stack);

}

}

// Push current vertex to stack which stores result

stack.push(new Integer(v));

}

void topologicalSort()

{

Stack stack = new Stack();

// Mark all the vertices as not visited

boolean visited[] = new boolean[V];

for (int i = 0; i

visited[i] = false;

}

for (int i = 0; i

if (visited[i] == false) {

topoSort(i, visited, stack);

}

}

// Print contents of stack

while (stack.empty()==false) {

System.out.print(stack.pop() + " , ");

}

}

}

Input File Sample:

8

15

4 5 0.35

5 4 0.35

4 7 0.37

5 7 0.28

7 5 0.28

5 1 0.32

0 4 0.38

0 2 0.26

7 3 0.39

1. 3 0.29

2. 7 0.34

6 2 0.40

3. 6 0.52

6 0 0.58

6 4 0.93

1. 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 edgelD int edgeWeight 2. Change the adjacency list to hold the vertex and edge weight. 3. Write a driver program, which reads input files mediumGraph.txt, LargeGraph.txt and XtraLargeGraph.txt (on Canvas) and display the weighted graphs by printing adjacency list

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!