template
GraphGenerato.java:
package assign8_template;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* The class to create a weighted graph by building the adjacency list as a HashMap,
* (where the key is a vertex, the value is the list of outgoing edges from this vertex),
* and show the graph by displaying the adjacency list in a tabular format.
* @author Cindy
*/
public class GraphGenerator {
//the delimiter comma in the data file: graphData.csv
private final String COMMA_DELIMITER = ",";
//----Assign 8------------//
//source, edge list, each edge: source,destination,weight
//define the data field: adjacencyListMap
// declare it as a Map in Java API
// key: a vertex
// value: the list of outgoing edges from this vertex
// For the required part of this assignment, each edge is stored as a String in the format:
// source vertex,destination vertex,weight
//NOTE – this adjacency list map is DIFFERENT from the map used in the MapGraph class in the textbook (Page 507 in 4th edition).
//constructor
public GraphGenerator(){
//need to use HashMap in completing the assignment
//create the object for the empty adjacencyListMap: a HashMap
}
//----Assign 8------------//
//It takes in a BufferedReader object that is already associated with the file graphData.csv,
// read the graph data line by line,
// build the graph adjacency list by populating the HashMap adjacencyListMap.
//• You must use the BufferedReader br to read from the given data file and populate the adjacency List map.
public void createGraph(BufferedReader br) {
//build the graph represented by a HashMap of AdjacencyList
List<String> edgeData = new ArrayList<>();
try {
String line;
br.readLine(); //read and no process first line
while ((line = br.readLine()) != null) {
String[] fields = line.split(COMMA_DELIMITER);
edgeData = Arrays.asList(fields);
//System.out.println(edgeData);
String source = edgeData.get(0);
String destination = edgeData.get(1);
double weight = Double.parseDouble(edgeData.get(2));
//----Assign 8------------//
//add your own code
//populate the HashMap in the data field: adjacencyListMap
// if source vertex is new in the map
// create an empty list (array list or linked list) as the edge list;
// else
// get the existing edge list for this source vertex from the hashmap
// add the destination vertex to the edge list.
// assemble the edge information.
// add the map entry for this source vertex to the hashmap: adjacencyListMap
//
//----Assign 8------------//
}
} catch (IOException ioe) {
System.out.println("file input error.");
ioe.printStackTrace();
System.exit(1);
}
}
//----Assign 8------------//
//• print the content of the adjacencyListMap in a tabular format like the one in the sample output.
// Must be a tabular format where data is aligned.
// The number of blanks does not have to be exactly the same as in the sample output.
// The alignment can be either left-justified or right-justified.
//• Each edge is required to be displayed in the following format:
// {(source vertex destination vertex), edge weight}
// Example: {(Larry Page-->Bill Gates), 28.0}
public void showGraph(){
//add your own code
//print the header
//for each entry in the set of entrys in adjacencyListMap
// get the source vertex in the entry,
// get the edge list in the entry
// print the source vertex
// print the [
// for each edge in the edge list
// get the destination vertex, weight from the edge
// (see how the line in graphData.csv is parsed in createGraph(...).
// print the edge in the required format
// print the ]
}
//----Assign 8 End ------------//
}
GraphGeneratorTest.java: package assign8_template;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
//This class tests the implementation of GraphGenerator class.
// It creates a BufferedReader to read from the data file graphData.csv,
// creates an object of GraphGenerator,
// uses this object to call createGraph and pass the BufferedReader object br to this method,
// uses this object to call showGraph, which displays the graph adjacency list.
public class GraphGeneratorTest {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("graphData.csv"));
//----Assign 8------------//
//add your own code
//creates an object of GraphGenerator,
//uses this object to call createGraph and pass the BufferedReader object br to this method,
//uses this object to call showGraph.
//----Assign 8 End------------//
} catch (IOException ioe) {
System.out.println("file input error.");
ioe.printStackTrace();
System.exit(1);
}
}
} csv.file: graphData Source,Destination,Weight Alan Turing,Donald Knuth,10.0 Donald Knuth,Grace Hopper,25.0 Alan Turing,Grace Hopper,20.0 Grace Hopper,Elon Mask,35.0 Alan Turing,Elon Mask,30.0 Larry Page,Bill Gates,28.0 Grace Hopper,Bill Gates,15.0 Larry Page,Elon Mask,18.0


0 comments