📄 dijkstra.java
字号:
package org.osu.ogsa.stream.info;import java.util.*;import java.io.*;import java.net.URL;import java.net.URI;public class Dijkstra extends GraphAlgorithm{ void updateDist(){ /* replace labels */ int i,j; j = v[u].delta_plus; while (j>=0) { i = e[j].rndd_minus; if ((v[i].succ>=-2)&&((v[i].dist<0)|| (v[i].dist>v[u].dist+e[j].len))) { if (v[i].dist<0) append_pre_s(i); v[i].dist = v[u].dist + e[j].len; v[i].prev = u; /* label */ } j = e[j].delta_plus; } if (!isdigraph) { j = v[u].delta_minus; while (j>=0) { i = e[j].rndd_plus; if ((v[i].succ>=-2)&&((v[i].dist<0)|| (v[i].dist>v[u].dist+e[j].len))) { if (v[i].dist<0) append_pre_s(i); v[i].dist = v[u].dist + e[j].len; v[i].prev = u; /* label */ } j = e[j].delta_minus; } } v[u].succ = COMPLETION_LABEL; } public static void main(String args[] ) { if(args.length < 1) { System.out.println("Usage: Dijkstra <config file> [starting vertex]"); System.exit(-1); } String configFile = args[0]; Dijkstra dj = new Dijkstra(); if(args.length == 2) dj.init(configFile, Integer.parseInt(args[1])); else dj.init(configFile, 0); }/* double weight(Node n, double x, double y) { double w,z,xx,yy; w = 0; for (int j = n.delta_plus; j>=0; j=e[j].delta_plus) { xx = (double)(v[e[j].rndd_minus].x - n.x); yy = (double)(v[e[j].rndd_minus].y - n.y); z = (x*xx+y*yy)/Math.sqrt((x*x+y*y)*(xx*xx+yy*yy))+1.0; w += z*z*z*z; } for (int j = n.delta_minus; j>=0; j=e[j].delta_minus) { xx = (double)(v[e[j].rndd_plus].x - n.x); yy = (double)(v[e[j].rndd_plus].y - n.y); z = (x*xx+y*yy)/Math.sqrt((x*x+y*y)*(xx*xx+yy*yy))+1.0; w += z*z*z*z; } return w; } void init_sub() { int x[] = {1, 0, -1, 1, 0, -1}; int y[] = {1, 1, 1, -1, -1, -1}; int i,j,k; double w,z; for (i=0; i<n; i++) { k=0; w=weight(v[i],(double)x[0],(double)y[0]); for (j=1; j<6; j++) { z=weight(v[i],(double)x[j],(double)y[j]); if (z<w) { w = z; k = j; } } v[i].dx = x[k]; v[i].dy = y[k]; } } public void paintNode(Graphics g, Node n, FontMetrics fm) { String s; int x = n.x; int y = n.y; int w = fm.stringWidth(n.name) + 10; int h = fm.getHeight() + 4; n.w = w; n.h = h; if (n.succ<-2) g.setColor(Color.blue); else if (n.succ==-2) g.setColor(Color.gray); else g.setColor(Color.red); g.drawRect(x-w/2,y-h/2,w,h); if (n.succ==-4) g.setColor(Color.cyan); else if (n.succ==-3) g.setColor(Color.pink); else if (n.succ>-2) g.setColor(Color.yellow); else g.setColor(getBackground()); g.fillRect(x-w/2+1,y-h/2+1,w-1,h-1); g.setColor(Color.black); g.drawString(n.name,x-(w-10)/2,(y-(h-4)/2)+fm.getAscent()); if (n.dist<0) s = ""; else s = ""+n.dist; w = fm.stringWidth(s) + 10; x += (h+1)*n.dx; y += (h+1)*n.dy; g.setColor(getBackground()); g.fillRect(x-n.pw/2,y-h/2,n.pw,h); n.pw = w; if (n.succ<-2) g.setColor(Color.blue); else g.setColor(Color.red); g.drawString(s,x-(w-10)/2,y-(h-4)/2+fm.getAscent()); } int [] xy(int a, int b, int w, int h) { int x[] = new int[2]; if (Math.abs(w*b)>=Math.abs(h*a)) { x[0] = ((b>=0)?1:-1)*a*h/b/2; x[1] = ((b>=0)?1:-1)*h/2; } else { x[0] = ((a>=0)?1:-1)*w/2; x[1] = ((a>=0)?1:-1)*b*w/a/2; } return x; } void drawArrow(Graphics g,int x1,int y1,int x2,int y2) { int a = x1-x2; int b = y1-y2; if (isdigraph) { double aa = Math.sqrt(a*a+b*b)/16.0; double bb = b/aa; aa = a/aa; g.drawLine(x2,y2,x2+(int)((aa*12+bb*5)/13),y2+(int)((-aa*5+bb*12)/13)); g.drawLine(x2,y2,x2+(int)((aa*12-bb*5)/13),y2+(int)((aa*5+bb*12)/13)); } g.drawLine(x1,y1,x2,y2); } public void paintEdge(Graphics g, Edge e, FontMetrics fm) { Node v1 = v[e.rndd_plus]; Node v2 = v[e.rndd_minus]; int a = v1.x-v2.x; int b = v1.y-v2.y; int x1[] = xy(-a,-b,v1.w,v1.h); int x2[] = xy(a,b,v2.w,v2.h); if (v2.prev == e.rndd_plus) { if ((v1.succ<-2)&&(v2.succ>=-2)) g.setColor(Color.red); else g.setColor(Color.blue); } else { g.setColor(Color.lightGray); } if ((!isdigraph)&&(v1.prev == e.rndd_minus)) { if ((v2.succ<-2)&&(v1.succ>=-2)) g.setColor(Color.red); else g.setColor(Color.blue); } drawArrow(g,v1.x+x1[0],v1.y+x1[1],v2.x+x2[0],v2.y+x2[1]); int w = fm.stringWidth("" + e.len); int h = fm.getHeight(); g.setColor(getBackground()); g.fillRect((v1.x+v2.x-w)/2,(v1.y+v2.y-h)/2,w,h); if ((v2.prev == e.rndd_plus)|| ((!isdigraph)&&(v1.prev == e.rndd_minus))) g.setColor(Color.black); else g.setColor(Color.lightGray); g.drawString("" + e.len,(v1.x+v2.x-w)/2,(v1.y+v2.y-h)/2+fm.getAscent()); } public void paint(Graphics g) { FontMetrics fm = g.getFontMetrics(); for (int i=0; i<n; i++) paintNode(g,v[i],fm); for (int i=0; i<m; i++) paintEdge(g,e[i],fm); } public void update(Graphics g) { paint(g); } public void mousePressed(MouseEvent ev) { if (iteration==0) { init_step1(); iteration++; step = 2; } else if (iteration>=n) { endLoop(); iteration = 0; } else { if (step == 2) { updateDist(); step = 3; } else { chooseMinNode(); iteration++; step = 2; } } repaint(); } public void mouseClicked(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} */ public void getShortestPaths() { init_step1(); int i; for(i = 1; i <= n; i++) { updateDist(); chooseMinNode(); } endLoop(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -