Skip to content

Commit 320f8f5

Browse files
committed
commit
1 parent f980225 commit 320f8f5

File tree

57 files changed

+1477
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1477
-15
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package singlesourceshortestpath;
2+
3+
4+
public class TestDijkstraAlgorithm {
5+
public static void main(String[] args) {
6+
DijkstraAlgorithm bfs=new DijkstraAlgorithm(100,0);
7+
bfs.createGraph();
8+
bfs.printGraph();
9+
bfs.printWeightGraph();
10+
bfs.shortestPathOfBFS(2);
11+
bfs.printShortestPathOfBFS(2);
12+
13+
}
14+
}
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
package singlesourceshortestpath;
2+
3+
import java.io.InputStream;
4+
import java.text.DecimalFormat;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Scanner;
8+
9+
10+
11+
public class DijkstraAlgorithm {
12+
protected Node[] graph; // 以数组的方式存储图,需要初始化指定数组的长度
13+
protected List<Node> list = new ArrayList<Node>(); // 以数组列表的形式存放图,可以不用初始化,直接添加
14+
Graph graphObj=new Graph(100,0);
15+
protected int maxSize;
16+
protected int gSize;
17+
public int count = 1;
18+
public double[][] weights;
19+
double[] smallWeight;
20+
21+
public BreadthFirstSearch(int maxSize, int gSize) {
22+
this.maxSize = maxSize;
23+
this.gSize = gSize;
24+
graph = new Node[maxSize];
25+
}
26+
27+
/**
28+
* 图中的节点
29+
*
30+
* @author liyafei
31+
*
32+
* @param <> 节点中的泛型 有三个属性,下一节点,关键字,两个节点之间的权重, 权重应该以矩阵的方式存储(也就是一个二维数组)
33+
* 可以使用一个开始值(start)和结束值(end)来代表权重(weight)是哪两个节点的。
34+
* 例如:start=2,end=4,weight=8,那么表示2号节点和4号节点之间的权重值为8;
35+
* 可以在data.txt里面每个相邻节点后面跟上权重值。 如果求最短距离时,可以用sumWeight记录到该节点总距离的最短距离
36+
* 在执行广度优先搜索或者深度优先搜索时,可以用color标记每个节点的颜色,代表每个节点是否已经被搜索过。
37+
*/
38+
public class Node {
39+
double weight;
40+
Node link;
41+
int key;
42+
int start;
43+
int end;
44+
double sumWeight=0;
45+
String color="WHITE";
46+
}
47+
48+
/**
49+
* 得到创建的带有权重的图,读出相邻节点之间的距离,然后存储到二维数组weights中。
50+
* 权重图的大小比节点多1,但是角标为0的位置都没用,为了处理存储的位置与节点的编号相一致
51+
*/
52+
public double[][] getWeightArray(){
53+
weights=new double[list.size()][list.size()];
54+
for (int i = 0; i < list.size(); i++) {
55+
Node node=(Node) list.get(i);
56+
while(node!=null){
57+
int row=node.start-1;
58+
int col=node.end-1;
59+
double weight=node.weight;
60+
weights[row][col]=weight;
61+
node=node.link;
62+
}
63+
}
64+
return weights;
65+
}
66+
67+
/**
68+
* 根据权重数组,求最短路径。找出给定节点到所有节点的最短路径
69+
* 单源节点到其它所有节点的最短距离。
70+
*/
71+
public void shortestPathOfBFS(int vertex){
72+
int v=0; //定义一个常量,用于记录最小点数
73+
double minWeight;//定义一个常量,记录最小权重
74+
double[][] weis=getWeightArray();
75+
int vertexNum=weis.length;
76+
int k=weis[vertex].length;
77+
smallWeight=new double[k];
78+
for (int i = 0; i < smallWeight.length; i++) {
79+
smallWeight[i]=weights[vertex][i];//将与vertex相邻节点的距离复制出来
80+
}
81+
boolean[] weightFound=new boolean[vertexNum];
82+
for (int i = 0; i < weightFound.length; i++) {
83+
weightFound[i]=false;
84+
}
85+
weightFound[vertex]=true;
86+
smallWeight[vertex]=0; //源节点到源节点的距离设为0
87+
88+
for (int i = 0; i < weightFound.length; i++) {
89+
// for (int m= 0; m < weightFound.length; m++) {
90+
// weightFound[m]=false;
91+
// }
92+
minWeight=Double.MAX_VALUE;
93+
94+
for (int j = 0; j < weightFound.length; j++) {
95+
if(!weightFound[j]){
96+
if(smallWeight[j]<minWeight && smallWeight[j]>0){
97+
v=j;
98+
minWeight=smallWeight[v]; //与vertex相邻的节点最小距离
99+
}
100+
}
101+
}
102+
weightFound[v]=true; //最小的距离标记
103+
for (int j = 0; j < weightFound.length; j++) {
104+
if(!weightFound[j]){
105+
if(minWeight+weis[v][j]<smallWeight[j]){ //如果从源点到v点加上从v点到j点的值,与从源点到j点值比较大小。
106+
smallWeight[j]=minWeight+weis[v][j];
107+
}
108+
}
109+
}
110+
}
111+
}
112+
113+
public void printShortestPathOfBFS(int vertex){
114+
DecimalFormat twoDigits=new DecimalFormat("0.00");
115+
System.out.println("source vertex"+vertex);
116+
System.out.println("shortest distance from the source to each vertex");
117+
for (int i = 0; i < list.size(); i++) {
118+
System.out.println(" "+(i)+"\t\t"+twoDigits.format(smallWeight[i]));
119+
System.out.println(" ");
120+
}
121+
}
122+
123+
/**
124+
* 得到链表的长度
125+
* @param node
126+
* @return
127+
*/
128+
public int getLength(Node node){
129+
int length=0;
130+
while(node.link!=null){
131+
node=node.link;
132+
length++;
133+
}
134+
return length;
135+
}
136+
137+
/**
138+
* 创建图,以链表的方式创建图
139+
*
140+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
141+
*/
142+
// public Node[] createGraph(){
143+
public List createGraph() {
144+
Class clazz = this.getClass();
145+
InputStream ins = clazz.getResourceAsStream("/data.txt"); // 通过外部数据创建链表,使用/加载src目录下的文件
146+
// 不使用/是加载类路径下的文件
147+
Scanner scanner = new Scanner(ins); // 流输入。
148+
while (scanner.hasNextLine()) {
149+
String s = scanner.nextLine();
150+
Scanner oneLine = new Scanner(s);
151+
Node first = null;
152+
Node newNode = null, last = null;
153+
while (oneLine.hasNext()) {
154+
String s1 = oneLine.next();
155+
156+
int num = Integer.parseInt(s1);
157+
if (num == 999)
158+
break;
159+
newNode = new Node();
160+
161+
if (first != null && oneLine.hasNext()) { // 创建first之后,读取下一节点时再读取权重
162+
String s2 = oneLine.next();// 读取权重
163+
double weight = Double.parseDouble(s2);
164+
newNode.weight = weight;
165+
newNode.end = num;
166+
}
167+
168+
// newNode.key=num; // 被 newNode.end=num;代替了
169+
170+
newNode.start = count;
171+
172+
newNode.link = null;
173+
if (first == null) {
174+
newNode.weight = 0;
175+
newNode.end = count;
176+
first = newNode;
177+
last = newNode;
178+
} else {
179+
last.link = newNode;
180+
last = newNode;
181+
}
182+
}
183+
graph[count] = first;
184+
list.add(first);
185+
count++;
186+
}
187+
return list;
188+
}
189+
190+
/**
191+
* 打印构建的图,起始节点,终止节点,起始节点到终止节点的权重
192+
*/
193+
public void printGraph(){
194+
for (int i = 0; i < list.size(); i++) {
195+
Node node=(Node) list.get(i);
196+
// System.out.println("以第"+(i+1)+"个节点为头节点的链表");
197+
//System.out.println(node.key);
198+
while(node!=null){
199+
// System.out.print("起始节点"+node.start+" ");
200+
// System.out.print("终止节点"+node.end+" ");
201+
// System.out.println("起始节点到终止节点的权重"+node.weight);
202+
node=node.link;
203+
}
204+
}
205+
}
206+
/**
207+
* 打印权重图
208+
*/
209+
public void printWeightGraph(){
210+
double[][] weightsArray=getWeightArray();
211+
for (int i = 0; i < weightsArray.length; i++) {
212+
System.out.println();
213+
double[] wa=weightsArray[i];
214+
for (int j = 0; j < wa.length; j++) {
215+
System.out.print(wa[j]+" ");
216+
}
217+
218+
}
219+
System.out.println();
220+
}
221+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package singlesourceshortestpath;
2+
3+
public class DijkstraAlgorithm {
4+
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.compliance=1.7
5+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.source=1.7

.metadata/.plugins/org.eclipse.core.resources/.history/4f/80a9c78916a8001715918e2c1fc31ded

Whitespace-only changes.

.metadata/.plugins/org.eclipse.core.resources/.history/59/80090a2916a8001715918e2c1fc31ded

Whitespace-only changes.

0 commit comments

Comments
 (0)