Skip to content

Commit 08a86f5

Browse files
committed
commit
1 parent 25b0cb4 commit 08a86f5

File tree

91 files changed

+6738
-4
lines changed

Some content is hidden

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

91 files changed

+6738
-4
lines changed

.metadata/.log

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,12 @@ Command-line arguments: -os linux -ws gtk -arch x86_64
468468

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

0 commit comments

Comments
 (0)