Question: Java Code Below takes a graph file and makes it a graph..modify it such that you have option to create a graph manually. Example you
Java Code Below takes a graph file and makes it a graph..modify it such that you have option to create a graph manually.
Example you can do g= new Graph()
g.addedge(source, destination,cost)
don't have to be this format. you just have to be able to create a graph manually.. can't use new methods not declared/used under.
public class Graph { public static final int MAX_NODES = 26; private Node[] nodes = new Node[MAX_NODES]; private Edge[] edges = new Edge[MAX_NODES * MAX_NODES]; private int numberEdges = 0; private Scanner input;
public Graph(String fileName) {
try { input = new Scanner(new FileInputStream(fileName)); } catch (Exception e) { System.err.println(e); return; }
// Assume valid input file. while ( input.hasNext() ) { char from, to; int cost;
from = input.next().charAt(0); to = input.next().charAt(0); cost = input.nextInt();
int fromIndex = from - 'A'; int toIndex = to - 'A';
// Create nodes if label is new; no checking for now. if ( nodes[fromIndex] == null ) nodes[fromIndex] = new Node(from);
if ( nodes[toIndex] == null ) nodes[toIndex] = new Node(to);
// Create edge, add edge to list of edges for the "from" node. edges[numberEdges] = new Edge( nodes[fromIndex], nodes[toIndex], cost); nodes[fromIndex].addEdge(edges[numberEdges]); numberEdges++; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
