Bellmanford algorithm in java applet

Topics: algorithm, bug, glee, graph, serialization
Jun 7, 2010 at 1:28 AM
hey, i make a code of bellmanford in java applet but the compiler shows reached end of file while parsing error...but i cant find any error.. i ve close my braces properly... plz anyone help me to identify my error.....plz my code is given bellow... import java.awt.*; import java.applet.*; public class GraphAlgorithm extends Applet { GraphCanvas graphcanvas = new GraphCanvas(this); Options options = new Options(this); Documentation documentation = new Documentation(); public void init() { setLayout(new BorderLayout(10, 10)); add("Center", graphcanvas); add("East", options); } public Insets insets(){ return new Insets(10, 10, 10, 10); } public void lock(){ graphcanvas.lock(); options.lock(); } public void unlock(){ graphcanvas.unlock(); options.unlock(); } } class Options extends Panel{ Button b1 = new Button("clear"); Button b2 = new Button("run"); Button b3 = new Button("step"); Button b4 = new Button("reset"); Button b5 = new Button("exit"); GraphAlgorithm parent; boolean locked=false; Options(GraphAlgorithm myparent){ parent = myparent; setLayout(new GraidLayout(6, 1, 0, 10)); add(b1); add(b2); add(b3); add(b4); add(b5); } public boolean action(Event evt, Object arg){ if(evt.target instanceof Button){ if(((String)arg).equals("step")){ if(!locked){ b3.setLabel("next step"); parent.graphcanvas.stepalg(); } else parent.documentation.doctext.showline("locked"); } if(((String)arg).equals("next step")){ parent.graphcanvas.nextstep(); if(((String)arg).equals("reset")){ parent.graphcanvas.reset(); b3.setLabel("step"); parent.documentation.doctext.showline("all times"); } if(((String)arg).equals("clear")){ parent.graphcanvas.clear(); b3.setLabel("step"); parent.documentation.doctext.showline("all times"); } if(((String)arg).equals("run")){ if(!locked) parent.graphcanvas.runalg(); else parent.documentation.doctext.showline("locked"); } if(((String)arg).equals("exit")){ System.exit(0); } } return true; } lock();{ Locked=true; } unlock();{ Locked=false; b3.setLabel("step"); } } class Documentation extends Panel{ DocOptions docopt = new DocOptions(this); DocText doctext = new DocText(); Documentation(){ setLayout(new BorderLayout(10, 10)); add("West", docopt); add("Center", doctext); } } class DocOptions extends Panel{ Choice doc = new Choice(); Documentation parent; DocOptions(Documentation myparent){ setLayout(new GridLayout(2, 1, 5, 0)); parent = myparent; add(new Label("Documentation:")); doc.addItem("draw nodes"); doc.addItem("remove nodes"); doc.addItem("move nodes"); doc.addItem("startnode"); doc.addItem("draw arrow"); doc.addItem("remove arrow"); doc.addItem("change weights"); doc.addItem("clear/reset"); doc.addItem("run algorithm"); doc.addItem("step through"); doc.addItem("example"); doc.addItem("exit"); doc.addItem("add items"); add(doc); } public boolean action(Event evt, Object arg){ if(evt.target){ String str = new String(doc.getSelectedItem()); parent.doctext.showline(str); } return true; } } class DocText extends TextArea{ final String drawnodes = new String("DRAWING NODES:\n" + "Draw a node by clicking the mouse.\n\n"); final String rmvnodes = new String("REMOVE NODES:\n" + "Select a node except start node,press <ctrl> and click on the node.\n\n"); final String mvnodes = new String("MOVE NODES:\n" + " To move a node press <Shift>, click on the node,and drag it to.\n\n"); final String startnode = new String("START NODE:\n"+"The startnode is blue, other nodes are grey.\n"); final String drawarrows = new String("DRAWING ARROWS:\n"+"To draw an arrow click mouse in a node,"+"and drag it to another node.\n\n"); final String rmvarrows = new String("REMOVE ARROWS:\n"+"To remove an arrow, change its weight to 0.\n\n"); final String weight = new String("CHANGING WEIGHTS:\n"+"To change the weight of an arrow, click on the arrowhead and drag\n"+"it along the arrow.\n\n"); final String clrreset = new String("<CLEAR> BUTTON: "+"Remove the current graph from the screen.\n"+"<RESET> BUTTON: "); final String runalg = new String("<RUN> BUTTON: "+"Run the algorithm on the graph, there will be a time\n" + "To run the algorithm slower, use <STEP>.\n"); final String step = new String("<STEP> BUTTON: " +"An opportunity to step through the algorithm.\n"); final String example = new String("<EXAMPLE> BUTTON: "+"Displays a graph on the screen for you.\n"+"You can then use <STEP> or <RUN>\n"); final String exitbutton = new String("<EXIT> BUTTON: " +"Only works if applet is run with appletviewer.\n"); final String toclose = new String("ERROR: "+"This position is to close to another node/arrow.\n"); final String done = new String("Algorithm has finished, " + "follow orange arrows from startnode to any node " + "press <RESET> to reset the graph, and unlock the screen."); final String some = new String("Algorithm has finished, " + "follow orange arrows from startnode to any node "+ "press <RESET> to reset the graph, and unlock the screen."); final String none = new String("Algorithm has finished, " +"there are no nodes reachable from the start node.\n"+"press <RESET> to reset the graph, and unlock the screen."); final String maxnodes = new String("ERROR: "+"Maximum number of nodes reached!\n\n"); final String info = new String("DOCUMENTATION:\n"+"You can scroll through the documentation or get documentation\n"+" to the scrolling text.\n\n"); final String locked = new String("ERROR: "+"Keyboard/mouse locked for this action.\n"+"Either press <NEXT STEP> or <RESET>.\n"); final String doc = info + drawnodes + rmvnodes + mvnodes + startnode + drawarrows + weight + rmvarrows + clrreset + runalg + step + example + exitbutton; DocText(){ super(5,2); setText(doc); } public void showline(String str) { if (str.equals("draw nodes")) setText(drawnodes); else if (str.equals("remove nodes")) setText(rmvnodes); else if (str.equals("move nodes")) setText(mvnodes); else if (str.equals("the startnode")) setText(startnode); else if (str.equals("draw arrows")) setText(drawarrows); else if (str.equals("change weights")) setText(weight); else if (str.equals("remove arrows")) setText(rmvarrows); else if (str.equals("clear / reset")) setText(clrreset); else if (str.equals("run algorithm")) setText(runalg); else if (str.equals("step through")) setText(step); else if (str.equals("example")) setText(example); else if (str.equals("exit")) setText(exitbutton); else if (str.equals("all items")) setText(doc); else if (str.equals("toclose")) setText(toclose); else if (str.equals("done")) setText(done); else if (str.equals("locked")) setText(locked); else if (str.equals("maxnodes")) setText(maxnodes); else if (str.equals("none")) setText(none); else if (str.equals("some")) setText(some); else setText(str); } } class GraphCanves extends Canvas implements Runnable{ final int MAXNODES = 10; final int MAX = MAXNODES+1; final int NODESIZE = 10; final int NODERADIX = 13; final int BELLMANFORD = -1; Point node[] = new Point[MAX]; int weight[][] = new int[MAX][MAX]; Point arrow[][] = new Point[MAX][MAX]; Point startp[][] = new Point[MAX][MAX]; Point endp[][] = new Point[MAX][MAX]; float dir_x[][] = new float[MAX][MAX]; float dir_y[][] = new float[MAX][MAX]; boolean algedge[][] = new boolean[MAX][MAX]; int dist[] = new int[MAX]; int finaldist[] = new int[MAX]; Color colornode[] = new Color[MAX]; boolean changed[] = new boolean[MAX]; int numchanged =0; int neighbours=0; int step=0; int mindist, minnode, minstart, minend; int numnodes=0; int emptyspots=0; int startgraph=0; int hitnode; int node1, node2; Point thispoint=new Point(0,0); Point oldpoint=new Point(0, 0); boolean newarrow = false; boolean movearrow = false; boolean movestart = false; boolean deletenode = false; boolean movenode = false; boolean performalg = false; boolean clicked = false; Font roman= new Font("TimesRoman", Font.BOLD, 12); Font helvetica= new Font("Helvetica", Font.BOLD, 15); FontMetrics fmetrics = getFontMetrics(roman); int h = (int)fmetrics.getHeight()/3; private Image offScreenImage; private Graphics offScreenGraphics; private Dimension offScreenSize; Thread algrthm; int algorithm; String showstring = new String(""); boolean stepthrough=false; boolean Locked = false; GraphAlgorithm parent; void GraphCanvas(GraphAlgorithm myparent) { parent = myparent; init(); algorithm = BELLMANFORD; setBackground(Color.cyan); } public void lock() { Locked=true; } public void unlock() { Locked=false; } public void start() { if (algrthm != null) algrthm.resume(); } public void init() { for (int i=0;i<MAXNODES;i++) { colornode[i]=Color.gray; for (int j=0; j<MAXNODES;j++) algedge[i][j]=false; } colornode[startgraph]=Color.blue; performalg = false; } public void clear() { startgraph=0; numnodes=0; emptyspots=0; init(); for(int i=0; i<MAXNODES; i++) { node[i]=new Point(0, 0); for (int j=0; j<MAXNODES;j++) weight[i][j]=0; } if (algrthm != null) algrthm.stop(); parent.unlock(); repaint(); } public void reset() { init(); if (algrthm != null) algrthm.stop(); parent.unlock(); repaint(); } public void runalg() { parent.lock(); initalg(); performalg = true; algrthm = new Thread(this); algrthm.start(); } public void stepalg() { parent.lock(); initalg(); performalg = true; nextstep(); } public void initalg() { init(); for(int i=0; i<MAXNODES; i++) { dist[i]=-1; finaldist[i]=-1; for (int j=0; j<MAXNODES;j++) algedge[i][j]=false; } dist[startgraph]=0; finaldist[startgraph]=0; step=0; } public void nextstep() { finaldist[minend]=mindist; algedge[minstart][minend]=true; colornode[minend]=Color.orange; step++; repaint(); } public void stop() { if (algrthm != null) algrthm.suspend(); } public void run() { for(int i=0; i<(numnodes-emptyspots); i++){ nextstep(); try { algrthm.sleep(2000); } catch (InterruptedException e) {} } algrthm = null; } public void showexample() { int w, h; clear(); init(); numnodes=10; emptyspots=0; for(int i=0; i<MAXNODES; i++) { node[i]=new Point(0, 0); for (int j=0; j<MAXNODES;j++) weight[i][j]=0; } w=this.size().width/8; h=this.size().height/8; node[0]=new Point(w, h); node[1]=new Point(3*w, h); node[2]=new Point(5*w, h); node[3]=new Point(w, 4*h); node[4]=new Point(3*w, 4*h); node[5]=new Point(5*w, 4*h); node[6]=new Point(w, 7*h); node[7]=new Point(3*w, 7*h); node[8]=new Point(5*w, 7*h); node[9]=new Point(7*w, 4*h); weight[0][1]=4; weight[0][3]=1; weight[1][0]=74; weight[1][2]=2; weight[1][4]=12; weight[2][5]=74; weight[2][1]=12; weight[2][9]=12; weight[3][4]=32; weight[3][6]=22; weight[4][3]=66; weight[4][5]=76; weight[4][7]=33; weight[5][8]=11; weight[5][9]=21; weight[6][7]=10; weight[6][3]=12; weight[7][6]=2; weight[7][8]=72; weight[8][5]=31; weight[8][9]=7; weight[8][7]=18; weight[9][5]=8; for (int i=0;i<numnodes;i++) for (int j=0;j<numnodes;j++) if (weight[i][j]>0) arrowupdate(i, j, weight[i][j]); repaint(); } public boolean mouseDown(Event evt, int x, int y) { if (Locked) parent.documentation.doctext.showline("locked"); else { clicked = true; if (evt.shiftDown()) { if (nodehit(x, y, NODESIZE)) { oldpoint = node[hitnode]; node1 = hitnode; movenode=true; } } else if (evt.controlDown()) { if (nodehit(x, y, NODESIZE)) { node1 = hitnode; if (startgraph==node1) { movestart=true; thispoint = new Point(x,y); colornode[startgraph]=Color.gray; } else deletenode= true; } } else if (arrowhit(x, y, 5)) { movearrow = true; repaint(); } else if (nodehit(x, y, NODESIZE)) { if (!newarrow) { newarrow = true; thispoint = new Point(x, y); node1 = hitnode; } } else if ( !nodehit(x, y, 50) && !arrowhit(x, y, 50) ) { if (emptyspots==0) { if (numnodes < MAXNODES) node[numnodes++]=new Point(x, y); else parent.documentation.doctext.showline("maxnodes"); } else { int i; for (i=0;i<numnodes;i++) if (node[i].x==-100) break; node[i]=new Point(x, y); emptyspots--; } } else parent.documentation.doctext.showline("toclose"); repaint(); } return true; } public boolean mouseDrag(Event evt, int x, int y) { if ( (!Locked) && clicked ) { if (movenode) { node[node1]=new Point(x, y); for (int i=0;i<numnodes;i++) { if (weight[i][node1]>0) { arrowupdate(i, node1, weight[i][node1]); } if (weight[node1][i]>0) { arrowupdate(node1, i, weight[node1][i]); } } repaint(); } else if (movestart || newarrow) { thispoint = new Point(x, y); repaint(); } else if (movearrow) { changeweight(x, y); repaint(); } } return true; } public boolean mouseUp(Event evt, int x, int y) { if ( (!Locked) && clicked ) { if (movenode) { node[node1]=new Point(0, 0); if ( nodehit(x, y, 50) || (x<0) || (x>this.size().width) || (y<0) || (y>this.size().height) ) { node[node1]=oldpoint; parent.documentation.doctext.showline("toclose"); } else node[node1]=new Point(x, y); for (int i=0;i<numnodes;i++) { if (weight[i][node1]>0) arrowupdate(i, node1, weight[i][node1]); if (weight[node1][i]>0) arrowupdate(node1, i, weight[node1][i]); } movenode=false; } else if (deletenode) { nodedelete(); deletenode=false; } else if (newarrow) { newarrow = false; if (nodehit(x, y, NODESIZE)) { node2=hitnode; if (node1!=node2) { arrowupdate(node1, node2, 50); if (weight[node2][node1]>0) { arrowupdate(node2, node1, weight[node2][node1]); } parent.documentation.doctext.showline("change weights"); } else System.out.println("zelfde"); } } else if (movearrow) { movearrow = false; if (weight[node1][node2]>0) changeweight(x, y); } else if (movestart) { if (nodehit(x, y, NODESIZE)) startgraph=hitnode; colornode[startgraph]=Color.blue; movestart=false; } repaint(); } return true; } public boolean nodehit(int x, int y, int dist) { for (int i=0; i<numnodes; i++) if ( (x-node[i].x)*(x-node[i].x) + (y-node[i].y)*(y-node[i].y) < dist*dist ) { hitnode = i; return true; } return false; } public boolean arrowhit(int x, int y, int dist) { for (int i=0; i<numnodes; i++) for (int j=0; j<numnodes; j++) { if ( ( weight[i][j]>0 ) && (Math.pow(x-arrow[i][j].x, 2) + Math.pow(y-arrow[i][j].y, 2) < Math.pow(dist, 2) ) ) { node1 = i; node2 = j; return true; } } return false; } public void nodedelete() { node[node1]=new Point(-100, -100); for (int j=0;j<numnodes;j++) { weight[node1][j]=0; weight[j][node1]=0; } emptyspots++; } public void changeweight(int x, int y) { int diff_x = (int)(20*dir_x[node1][node2]); int diff_y = (int)(20*dir_y[node1][node2]); boolean follow_x=false; if (Math.abs(endp[node1][node2].x-startp[node1][node2].x) > Math.abs(endp[node1][node2].y-startp[node1][node2].y) ) { follow_x = true; } if (follow_x) { int hbound = Math.max(startp[node1][node2].x,endp[node1][node2].x)-Math.abs(diff_x); int lbound = Math.min(startp[node1][node2].x,endp[node1][node2].x)+Math.abs(diff_x); if (x<lbound) { arrow[node1][node2].x=lbound; } else if (x>hbound) { arrow[node1][node2].x=hbound; } else arrow[node1][node2].x=x; arrow[node1][node2].y=(arrow[node1][node2].x-startp[node1][node2].x)*(endp[node1][node2].y-startp[node1][node2].y)/(endp[node1][node2].x-startp[node1][node2].x)+startp[node1][node2].y; weight[node1][node2]=Math.abs(arrow[node1][node2].x-startp[node1][node2].x-diff_x)*100/(hbound-lbound); } else { int hbound = Math.max(startp[node1][node2].y,endp[node1][node2].y)-Math.abs(diff_y); int lbound = Math.min(startp[node1][node2].y,endp[node1][node2].y)+Math.abs(diff_y); if (y<lbound) { arrow[node1][node2].y=lbound; } else if (y>hbound) { arrow[node1][node2].y=hbound; } else arrow[node1][node2].y=y; arrow[node1][node2].x=(arrow[node1][node2].y-startp[node1][node2].y)*(endp[node1][node2].x-startp[node1][node2].x)/(endp[node1][node2].y-startp[node1][node2].y)+startp[node1][node2].x; weight[node1][node2]=Math.abs(arrow[node1][node2].y-startp[node1][node2].y-diff_y)*100/(hbound-lbound); } } public void arrowupdate(int p1, int p2, int w) { int dx, dy; float l; weight[p1][p2]=w; dx = node[p2].x-node[p1].x; dy = node[p2].y-node[p1].y; l = (float)( Math.sqrt((float)(dx*dx + dy*dy))); dir_x[p1][p2]=dx/l; dir_y[p1][p2]=dy/l; if (weight[p2][p1]>0) { startp[p1][p2] = new Point((int)(node[p1].x-5*dir_y[p1][p2]),(int)(node[p1].y+5*dir_x[p1][p2])); endp[p1][p2] = new Point((int)(node[p2].x-5*dir_y[p1][p2]),(int)(node[p2].y+5*dir_x[p1][p2])); } else { startp[p1][p2] = new Point(node[p1].x, node[p1].y); endp[p1][p2] = new Point(node[p2].x, node[p2].y); } int diff_x = (int)(Math.abs(20*dir_x[p1][p2])); int diff_y = (int)(Math.abs(20*dir_y[p1][p2])); if (startp[p1][p2].x>endp[p1][p2].x) { arrow[p1][p2] = new Point(endp[p1][p2].x + diff_x+(Math.abs(endp[p1][p2].x-startp[p1][p2].x) - 2*diff_x )*(100-w)/100 , 0); } else { arrow[p1][p2] = new Point(startp[p1][p2].x + diff_x + (Math.abs(endp[p1][p2].x-startp[p1][p2].x) - 2*diff_x )* w/100, 0); } if (startp[p1][p2].y>endp[p1][p2].y) { arrow[p1][p2].y=endp[p1][p2].y + diff_y + (Math.abs(endp[p1][p2].y-startp[p1][p2].y) - 2*diff_y )*(100-w)/100; } else { arrow[p1][p2].y=startp[p1][p2].y + diff_y + (Math.abs(endp[p1][p2].y-startp[p1][p2].y) - 2*diff_y )*w/100; } } public String intToString(int i) { char c=(char)((int)'a'+i); return ""+c; } public final synchronized void update(Graphics g) { Dimension d=size(); if ((offScreenImage == null) || (d.width != offScreenSize.width) || (d.height != offScreenSize.height)) { offScreenImage = createImage(d.width, d.height); offScreenSize = d; offScreenGraphics = offScreenImage.getGraphics(); } offScreenGraphics.setColor(Color.white); offScreenGraphics.fillRect(0, 0, d.width, d.height); paint(offScreenGraphics); g.drawImage(offScreenImage, 0, 0, null); } public void drawarrow(Graphics g, int i, int j) { int x1, x2, x3, y1, y2, y3; x1= (int)(arrow[i][j].x - 3*dir_x[i][j] + 6*dir_y[i][j]); x2= (int)(arrow[i][j].x - 3*dir_x[i][j] - 6*dir_y[i][j]); x3= (int)(arrow[i][j].x + 6*dir_x[i][j]); y1= (int)(arrow[i][j].y - 3*dir_y[i][j] - 6*dir_x[i][j]); y2= (int)(arrow[i][j].y - 3*dir_y[i][j] + 6*dir_x[i][j]); y3= (int)(arrow[i][j].y + 6*dir_y[i][j]); int arrowhead_x[] = { x1, x2, x3, x1 }; int arrowhead_y[] = { y1, y2, y3, y1 }; if (algedge[i][j]) g.setColor(Color.orange); g.drawLine(startp[i][j].x, startp[i][j].y, endp[i][j].x, endp[i][j].y); g.fillPolygon(arrowhead_x, arrowhead_y, 4); int dx = (int)(Math.abs(7*dir_y[i][j])); int dy = (int)(Math.abs(7*dir_x[i][j])); String str = new String("" + weight[i][j]); g.setColor(Color.black); if ((startp[i][j].x>endp[i][j].x) && (startp[i][j].y>=endp[i][j].y)) g.drawString( str, arrow[i][j].x + dx, arrow[i][j].y - dy); if ((startp[i][j].x>=endp[i][j].x) && (startp[i][j].y<endp[i][j].y)) g.drawString( str, arrow[i][j].x - fmetrics.stringWidth(str) - dx , arrow[i][j].y - dy); if ((startp[i][j].x<endp[i][j].x) && (startp[i][j].y<=endp[i][j].y)) g.drawString( str, arrow[i][j].x - fmetrics.stringWidth(str),arrow[i][j].y + fmetrics.getHeight()); if ((startp[i][j].x<=endp[i][j].x) && (startp[i][j].y>endp[i][j].y)) g.drawString( str, arrow[i][j].x + dx,arrow[i][j].y + fmetrics.getHeight() ); } public class BellmanFord { private static class Vertex { String label; int pred; double d; public Vertex(String label) { this.label = label; pred = -1; } } private static class Arc { int vertex; double weight; public Arc(int v, double w) { vertex = v; weight = w; } } private static Vertex[] vertices; private static Arc[][] adjList; private static int source; public static void main(String[] args) { readGraph(args); printAdjList("Adjacency list of G", adjList); if (bf(source)) printShortestPaths(source); else System.out.println("G contains a negative weight loop"); } public static boolean bf(int s) { initSingleSource(s); for (int i=1; i<vertices.length; i++) for (int u=0; u<adjList.length; u++) for (int v=0; v<adjList[u].length; v++) relax(u, adjList[u][v].vertex, adjList[u][v].weight); for (int u=0; u<adjList.length; u++) for (int v=0; v<adjList[u].length; v++) if (vertices[adjList[u][v].vertex].d > vertices[u].d + adjList[u][v].weight) return(false); return(true); } public static void initSingleSource(int s) { for (int v=0; v<vertices.length; v++) { vertices[v].d = Double.MAX_VALUE; vertices[v].pred = -1; } vertices[s].d = 0; } public static void relax(int u, int v, double w) { if (vertices[v].d > vertices[u].d + w) { vertices[v].d = vertices[u].d + w; vertices[v].pred = u; } } public static void printShortestPaths(int s) { System.out.println("\nShortest paths from " + vertices[s].label + ":"); for (int v=0; v<vertices.length; v++) { Stack<Integer> stack = new Stack<Integer>(); stack.push(new Integer(v)); for (int u=vertices[v].pred; u >= 0; u=vertices[u].pred) stack.push(new Integer(u)); int i = stack.pop().intValue(); System.out.print(vertices[i].label); int length = 0; if (v == s) System.out.print(" -> " + vertices[i].label); else while (! stack.isEmpty()) { int j = stack.pop().intValue(); System.out.print(" -> " +vertices[j].label); int k = 0; for ( ; adjList[i][k].vertex != j; k++); length += adjList[i][k].weight; i = j; } System.out.println(" length=" + length); } } public static void printAdjList(String s, Arc[][] adj) { System.out.println(s); for (int i=0; i<adj.length; i++) { System.out.print(vertices[i].label + ": "); for (int j=0; j<adj[i].length; j++) System.out.print(vertices[adj[i][j].vertex].label + "/" + adj[i][j].weight + " "); System.out.println(); } } public static void readGraph(String[] args) { if (args.length == 0) { System.out.println("No source node specified"); System.exit(0); } Scanner inFile = new Scanner(System.in); HashMap<String, Integer> nodeMap = new HashMap<String, Integer>(); ArrayList<Vertex> nodeList = new ArrayList<Vertex>(); String input = ""; int index = 0; String s = ""; while (inFile.hasNextLine()) { s = inFile.nextLine(); if (s.length() > 0 && s.charAt(0) != '#') { StringTokenizer sTk = new StringTokenizer(s); String label = sTk.nextToken(); if (! nodeMap.containsKey(label)) { Vertex v = new Vertex(label); nodeMap.put(label, new Integer(index)); nodeList.add(v); index++; input = input + "#" + s; } else { System.out.println("Multiple declaration of vertex: " + label); System.exit(0); } } } if (nodeMap.containsKey(args[0])) source = nodeMap.get(args[0]).intValue(); else { System.out.println("Non-existing source node " + args[0]); System.exit(0); } vertices = (Vertex[]) nodeList.toArray(new Vertex[nodeList.size()]); adjList = new Arc[vertices.length][]; StringTokenizer sTk = new StringTokenizer(input, "#"); for (int i=0; i<vertices.length; i++) { StringTokenizer sTk1 = new StringTokenizer(sTk.nextToken()); adjList[i] = new Arc[(sTk1.countTokens()-1)/2]; s = sTk1.nextToken(); for (int j=0; j<adjList[i].length; j++) { s = sTk1.nextToken(); double weight = 0; try { weight = Double.parseDouble(sTk1.nextToken()); } catch(NumberFormatException nfe) { System.out.println("Wrong label/weight combination in the " + "adjacency list of " + vertices[i].label); System.exit(0); } catch(NoSuchElementException nfe) { System.out.println("Wrong adjacency list for node " + vertices[i].label); System.exit(0); } if (nodeMap.containsKey(s)) { int ind = nodeMap.get(s).intValue(); adjList[i][j] = new Arc(ind, weight); } else { System.out.println("Undeclared vertex: " + s); System.exit(0); } } } } }