import java.util.*; class Graph extends Data implements IModel { private Node[] all; private Graph() { all = filter(append (B.stops, append (C.stops, append (D.stops, append (E.stops, append (blue.stops, append (orange.stops, redAshmont.stops))))))); } public static Graph theGraph = new Graph(); public String[] all_names() { String[] s = new String[all.length]; for(int i = 0; i< all.length; i++) s[i] = all[i].name; return s; } public String[] search(String f, String t) { Node[] robject = searchNode(f,t); String[] sobject = new String[robject.length]; for(int i = 0; i < robject.length; i++) sobject[i] = robject[i].name + " " + robject[i].descTLines(); return sobject; } public Node[] searchNode(String f, String t) { Node from = String2Node(f); Node to = String2Node(t); // search for routes with a specific number of transfers, // iterating from 0 up to the number of lines there are. for (int allowed_transfers = 0; allowed_transfers < Graph.theGraph.all_tlines.length; allowed_transfers++) { // reset the flags on the nodes on each attempt for(int i = 0; i < all.length; i++) all[i].resetVisited(); // start from the inital station, once with each line at this station for (int j = 0; j < from.tlines.size(); j++) { TLine tline = (TLine)from.tlines.elementAt(j); Vector result = from.search(to,tline,allowed_transfers); if (result != null) { result.add(to); Object[] robject = result.toArray(); Node[] sobject = new Node[robject.length]; for(int i = 0; i < robject.length; i++) { Node n = (Node)robject[i]; sobject[i]=n; } return sobject; } } } return null; } private static Node[] filter(Node[] x) { int duplicates = 0; Node[] y = new Node[x.length]; for(int i = 0; i < x.length; i++) y[i] = x[i]; for(int i = 0; i < y.length; i++) if (null != y[i]) for(int j = i+1; j < y.length; j++) if (null != y[j] && y[i].equals(y[j])) { duplicates += 1; y[j] = null; }; Node[] result = new Node[y.length-duplicates]; int i = 0; for(int j = 0; j < y.length; j++) if (null != y[j]) { result[i] = y[j]; i++; } return result; } private static Node[] append(Node[] x, Node[] y) { Node[] result = new Node[x.length+y.length]; for(int i = 0; i < x.length; i++) result[i] = x[i]; for(int i = 0; i < y.length; i++) result[i+x.length] = y[i]; return result; } private Node String2Node(String f) { for(int i = 0; i < all.length; i++) if (all[i].name.equals(f)) return all[i]; return null; } }