62,614
社区成员
发帖
与我相关
我的任务
分享
package service;
import java.util.ArrayList;
import java.util.Scanner;
public class WeightedGraph {
private int [][] edges; // adjacency matrix
private Object [] labels;
public WeightedGraph(int n,int [][] edges,Object [] labels) {
this.edges = new int [n][n];
this.labels = new Object[n];
this.edges = edges;
this.labels = labels;
}
public int size() { return labels.length; }
public void setLabel (int vertex, Object label) { labels[vertex]=label; }
public Object getLabel (int vertex) { return labels[vertex]; }
public void addEdge (int source, int target, int w) { edges[source][target] = w;edges[target][source] = w;}
public boolean isEdge (int source, int target) { return edges[source][target]>0; }
public void removeEdge (int source, int target) { edges[source][target] = 0; }
public int getWeight (int source, int target) { return edges[source][target]; }
public int [] neighbors (int vertex) {
int count = 0;
for (int i=0; i<edges[vertex].length; i++) {
if (edges[vertex][i]>0) count++;
}
final int[]answer= new int[count];
count = 0;
for (int i=0; i<edges[vertex].length; i++) {
if (edges[vertex][i]>0) answer[count++]=i;
}
return answer;
}
public void setEdges(int [][] edges){
this.edges = edges;
}
public void setLabels(Object labels[]) {
this.labels = labels;
}
}
package service;
import java.util.ArrayList;
public class Dijkstra {
// Dijkstra's algorithm to find shortest path from s to all other nodes
public int [] dijkstra (WeightedGraph G, int s) {
final int [] dist = new int [G.size()]; // shortest known distance from "s"
final int [] pred = new int [G.size()]; // preceeding node in path
final boolean [] visited = new boolean [G.size()]; // all false initially
for (int i=0; i<dist.length; i++) {
dist[i] = Integer.MAX_VALUE;
// dist[i] = G.getWeight(s,i);
}
dist[s] = 0;
for (int i=0; i<dist.length; i++) {
final int next = minVertex (dist, visited);
if(next>=0) {
visited[next] = true;
// The shortest path to next is dist[next] and via pred[next].
final int [] n = G.neighbors (next);
for (int j=0; j<n.length; j++) {
final int v = n[j];
final int d = dist[next] + G.getWeight(next,v);
if (dist[v] > d) {
dist[v] = d;
pred[v] = next;
}
}
}
}
return pred; // (ignore pred[s]==0!)
}
private int minVertex (int [] dist, boolean [] v) {
int x = Integer.MAX_VALUE;
int y = -1; // graph not connected, or no unvisited vertices
for (int i=0; i<dist.length; i++) {
if (!v[i] && dist[i]<x) {y=i; x=dist[i];}
}
return y;
}
public ArrayList getPath (WeightedGraph G, int [] pred, int s, int e) {
final ArrayList path = new ArrayList();
int x = e;
while (x!=s) {
path.add (0, G.getLabel(x));
x = pred[x];
}
path.add (0, G.getLabel(s));
return path;
// System.out.println (path);
}
public ArrayList pathPlan(int n,int [][]edges,Object []labels,int s,int t) {
WeightedGraph G = new WeightedGraph(n,edges,labels);
int [] pred = dijkstra (G, s);
ArrayList al = getPath (G, pred, s, t);
return al;
}
}
package test;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import java.util.ArrayList;
public class PathPlanClient {
public static void main(String[] args) throws Exception
{
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/Dijkstra");
options.setTo(targetEPR);
// 指定pathPlan方法的参数值
int [][]edges = {
{0, 2, 0, 0, 0, 9},
{2, 0, 8, 15, 0, 6},
{0, 8, 0, 1, 7, 0},
{0, 15, 1, 0, 3, 0},
{0, 0, 7, 3, 0, 3},
{9, 6, 0, 0, 3, 0}};
Object []labels = {"v0","v1","v2","v3","v4","v5"};
Object[] opAddEntryArgs = new Object[] {labels};
// 指定pathPlan方法返回值的数据类型的Class对象
Class[] classes = new Class[] {ArrayList.class};
// 指定要调用的pathPlan方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://service", "pathPlan");
opAddEntryArgs = new Object[] {6,edges,labels,3,0};
ArrayList al = (ArrayList)serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];
System.out.println(al);
}
}