Skip to content

Commit 6590a22

Browse files
committed
commit
1 parent 08a86f5 commit 6590a22

File tree

38 files changed

+2749
-9
lines changed

38 files changed

+2749
-9
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
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+
* 构造函数
32+
* @param maxSize
33+
* @param gSize
34+
*/
35+
public DijkstraAlgorithm(int maxSize, int gSize) {
36+
this.maxSize = maxSize;
37+
this.gSize = gSize;
38+
graph = new Node[maxSize]; //存放节点的数组初始化,可以用List代替。
39+
}
40+
41+
/**
42+
* 图中的节点
43+
*
44+
* @author liyafei
45+
*
46+
* @param <> 节点中的泛型 有三个属性,下一节点,关键字,两个节点之间的权重, 权重应该以矩阵的方式存储(也就是一个二维数组)
47+
* 可以使用一个开始值(start)和结束值(end)来代表权重(weight)是哪两个节点的。
48+
* 例如:start=2,end=4,weight=8,那么表示2号节点和4号节点之间的权重值为8;
49+
* 可以在data.txt里面每个相邻节点后面跟上权重值。 如果求最短距离时,可以用sumWeight记录到该节点总距离的最短距离
50+
* 在执行广度优先搜索或者深度优先搜索时,可以用color标记每个节点的颜色,代表每个节点是否已经被搜索过。
51+
*/
52+
public class Node {
53+
double weight;
54+
Node link;
55+
int key;
56+
int start;
57+
int end;
58+
double sumWeight=0;
59+
String color="WHITE";
60+
}
61+
62+
/**
63+
* 得到创建的带有权重的图,读出相邻节点之间的距离,然后存储到二维数组weights中。
64+
* 权重图的大小比节点多1,但是角标为0的位置都没用,为了处理存储的位置与节点的编号相一致
65+
*/
66+
public double[][] getWeightArray(){
67+
weights=new double[list.size()][list.size()];
68+
for (int i = 0; i < list.size(); i++) {
69+
Node node=(Node) list.get(i);
70+
while(node!=null){
71+
int row=node.start-1;
72+
int col=node.end-1;
73+
double weight=node.weight;
74+
weights[row][col]=weight;
75+
node=node.link;
76+
}
77+
}
78+
return weights;
79+
}
80+
81+
/**
82+
* 根据权重数组,求最短路径。找出给定节点到所有节点的最短路径
83+
* 单源节点到其它所有节点的最短距离。
84+
*/
85+
public void shortestPathOfBFS(int vertex,int end){
86+
// int v=0; //定义一个常量,用于记录最小点数
87+
double minWeight;//定义一个常量,记录最小权重
88+
double[][] weis=getWeightArray();
89+
90+
91+
92+
int vertexNum=weis.length;
93+
int k=weis[vertex].length;
94+
smallWeight=new double[k];
95+
for (int i = 0; i < smallWeight.length; i++) {
96+
smallWeight[i]=weights[vertex][i];//将与vertex相邻节点的距离复制出来
97+
}
98+
boolean[] weightFound=new boolean[vertexNum];
99+
for (int i = 0; i < weightFound.length; i++) {
100+
weightFound[i]=false;
101+
List li=new ArrayList<>();
102+
li.add(vertex);
103+
list2.add(li);
104+
}
105+
weightFound[vertex]=true;
106+
smallWeight[vertex]=0; //源节点到源节点的距离设为0
107+
108+
for (int i = 0; i < weightFound.length; i++) {
109+
minWeight=Double.MAX_VALUE;
110+
for (int j = 0; j < weightFound.length; j++) {
111+
if(!weightFound[j]){
112+
if(smallWeight[j]<minWeight && smallWeight[j]>0){
113+
v=j;
114+
minWeight=smallWeight[v]; //与vertex相邻的节点最小距离,每次找到一个最小值。
115+
}
116+
}
117+
}
118+
list2.get(v).add(v);
119+
weightFound[v]=true; //最小的距离标记
120+
if(v==end){
121+
System.out.println(v);
122+
break;
123+
}
124+
125+
for (int j = 0; j < weightFound.length; j++) {
126+
if(!weightFound[j]){
127+
if(minWeight+weis[v][j]<smallWeight[j]){ //如果从源点到v点加上从v点到j点的值,与从源点到j点值比较大小。
128+
smallWeight[j]=minWeight+weis[v][j];
129+
130+
//(v,j)的值记录下来,这是从源节点到j节点经过的路径为v。
131+
list2.get(j).add(v);
132+
}
133+
}
134+
}
135+
}
136+
}
137+
138+
public void printShortestPathOfBFS(int vertex,int end){
139+
shortestPathOfBFS(vertex,end);
140+
DecimalFormat twoDigits=new DecimalFormat("0.00");
141+
System.out.println("source vertex"+vertex);
142+
System.out.println("shortest distance from the source to each vertex");
143+
// for (int i = 0; i < list.size(); i++) {
144+
// System.out.println(" "+(i)+"\t\t"+twoDigits.format(smallWeight[i]));
145+
// System.out.println(" ");
146+
// }
147+
System.out.println(" 从"+vertex+"+”到“+end"+"的最短距离 "+"\t\t"+twoDigits.format(smallWeight[v]));
148+
System.out.println("经过的路径");
149+
for (int i = 0; i < list2.get(end).size(); i++) {
150+
System.out.println(list2.get(end).get(i));//依次打印从源节点到终点的路径。
151+
}
152+
153+
154+
double d=minValueofPath(list2.get(end));//将这条路径上的最小值找出,用于求最大流最小割问题。
155+
System.out.println("路径上的容量最小值"+d);
156+
}
157+
158+
/**
159+
* 找出源节点到终止节点路径上的最小容量。
160+
* @param list3 存放源节点到终止节点路径上节点的列表
161+
* @return 源节点到终止节点路径上最小容量
162+
*/
163+
private double minValueofPath(List list3) {
164+
// TODO Auto-generated method stub
165+
double d =Double.MAX_VALUE;
166+
double weight;
167+
for (int i = 0; i < list3.size()-1; i++) {
168+
weight=weights[(int) list3.get(i)][(int) list3.get(i+1)];
169+
if(weight<d){
170+
d=weight;
171+
}
172+
}
173+
return d;
174+
}
175+
176+
/**
177+
* 得到链表的长度
178+
* @param node
179+
* @return
180+
*/
181+
public int getLength(Node node){
182+
int length=0;
183+
while(node.link!=null){
184+
node=node.link;
185+
length++;
186+
}
187+
return length;
188+
}
189+
190+
/**
191+
* 创建图,以链表的方式创建图
192+
*
193+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
194+
*/
195+
// public Node[] createGraph(){
196+
public List createGraph() {
197+
Class clazz = this.getClass();
198+
InputStream ins = clazz.getResourceAsStream("/data.txt"); // 通过外部数据创建链表,使用/加载src目录下的文件
199+
// 不使用/是加载类路径下的文件
200+
Scanner scanner = new Scanner(ins); // 流输入。
201+
while (scanner.hasNextLine()) {
202+
String s = scanner.nextLine();
203+
Scanner oneLine = new Scanner(s);
204+
Node first = null;
205+
Node newNode = null, last = null;
206+
while (oneLine.hasNext()) {
207+
String s1 = oneLine.next();
208+
209+
int num = Integer.parseInt(s1);
210+
if (num == 999)
211+
break;
212+
newNode = new Node();
213+
214+
if (first != null && oneLine.hasNext()) { // 创建first之后,读取下一节点时再读取权重
215+
String s2 = oneLine.next();// 读取权重
216+
double weight = Double.parseDouble(s2);
217+
newNode.weight = weight;
218+
newNode.end = num;
219+
}
220+
221+
// newNode.key=num; // 被 newNode.end=num;代替了
222+
223+
newNode.start = count;
224+
225+
newNode.link = null;
226+
if (first == null) {
227+
newNode.weight = 0;
228+
newNode.end = count;
229+
first = newNode;
230+
last = newNode;
231+
} else {
232+
last.link = newNode;
233+
last = newNode;
234+
}
235+
}
236+
graph[count] = first;
237+
list.add(first);
238+
count++;
239+
}
240+
return list;
241+
}
242+
243+
/**
244+
* 打印构建的图,起始节点,终止节点,起始节点到终止节点的权重
245+
*/
246+
public void printGraph(){
247+
for (int i = 0; i < list.size(); i++) {
248+
Node node=(Node) list.get(i);
249+
// System.out.println("以第"+(i+1)+"个节点为头节点的链表");
250+
//System.out.println(node.key);
251+
while(node!=null){
252+
// System.out.print("起始节点"+node.start+" ");
253+
// System.out.print("终止节点"+node.end+" ");
254+
// System.out.println("起始节点到终止节点的权重"+node.weight);
255+
node=node.link;
256+
}
257+
}
258+
}
259+
/**
260+
* 打印权重图
261+
*/
262+
public void printWeightGraph(){
263+
double[][] weightsArray=getWeightArray();
264+
for (int i = 0; i < weightsArray.length; i++) {
265+
System.out.println();
266+
double[] wa=weightsArray[i];
267+
for (int j = 0; j < wa.length; j++) {
268+
System.out.print(wa[j]+" ");
269+
}
270+
271+
}
272+
System.out.println();
273+
}
274+
}

0 commit comments

Comments
 (0)