Skip to content

Commit 1eb342b

Browse files
committed
commit
1 parent 4b7acc4 commit 1eb342b

37 files changed

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

0 commit comments

Comments
 (0)