Skip to content

Commit 2082eb1

Browse files
committed
commit
1 parent f114633 commit 2082eb1

File tree

48 files changed

+2156
-18
lines changed

Some content is hidden

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

48 files changed

+2156
-18
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package breadth_first_search;
2+
3+
import java.util.List;
4+
5+
import breadth_first_search.BreadthFirstSearch.Node;
6+
7+
public class TestBreathFirstSearch {
8+
public static void main(String[] args) {
9+
BreadthFirstSearch bfs=new BreadthFirstSearch(100,0);
10+
bfs.createGraph();
11+
bfs.printGraph();
12+
bfs.printWeightGraph();
13+
bfs.shortestPathOfBFS(3);
14+
bfs.printShortestPathOfBFS(3);
15+
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1 2 10 4 5 3 4
2+
2 3 1 4 2 5 9
3+
3 5 4 1 3 2 5 4 7
4+
4 2 3 3 9 5 2
5+
5 1 7 3 6 999
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1 2 10 4 5
2+
2 3 1 4 2
3+
3 5 4
4+
4 2 3 3 9 5 2
5+
5 1 7 3 6 999
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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];//将与vertex相邻节点的距离复制出来
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+
for (int m= 0; m < weightFound.length; m++) {
91+
weightFound[m]=false;
92+
}
93+
minWeight=Double.MAX_VALUE;
94+
95+
for (int j = 0; j < weightFound.length; j++) {
96+
if(!weightFound[j]){
97+
if(smallWeight[j]<minWeight && smallWeight[j]>0){
98+
v=j;
99+
minWeight=smallWeight[v];
100+
}
101+
}
102+
weightFound[v]=true;
103+
104+
}
105+
for (int j = 0; j < weightFound.length; j++) {
106+
if(!weightFound[j]){
107+
if(minWeight+weis[v][j]<smallWeight[j]){
108+
smallWeight[j]=minWeight+weis[v][j];
109+
}
110+
}
111+
}
112+
}
113+
114+
}
115+
116+
public void printShortestPathOfBFS(int vertex){
117+
DecimalFormat twoDigits=new DecimalFormat("0.00");
118+
System.out.println("source vertex"+vertex);
119+
System.out.println("shortest distance from the source to each vertex");
120+
for (int i = 0; i < list.size(); i++) {
121+
System.out.println(" "+(i)+"\t\t"+twoDigits.format(smallWeight[i]));
122+
System.out.println(" ");
123+
}
124+
}
125+
126+
/**
127+
* 得到链表的长度
128+
* @param node
129+
* @return
130+
*/
131+
public int getLength(Node node){
132+
int length=0;
133+
while(node.link!=null){
134+
node=node.link;
135+
length++;
136+
}
137+
return length;
138+
}
139+
140+
/**
141+
* 创建图,以链表的方式创建图
142+
*
143+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
144+
*/
145+
// public Node[] createGraph(){
146+
public List createGraph() {
147+
Class clazz = this.getClass();
148+
InputStream ins = clazz.getResourceAsStream("/data.txt"); // 通过外部数据创建链表,使用/加载src目录下的文件
149+
// 不使用/是加载类路径下的文件
150+
Scanner scanner = new Scanner(ins); // 流输入。
151+
while (scanner.hasNextLine()) {
152+
String s = scanner.nextLine();
153+
Scanner oneLine = new Scanner(s);
154+
Node first = null;
155+
Node newNode = null, last = null;
156+
while (oneLine.hasNext()) {
157+
String s1 = oneLine.next();
158+
159+
int num = Integer.parseInt(s1);
160+
if (num == 999)
161+
break;
162+
newNode = new Node();
163+
164+
if (first != null && oneLine.hasNext()) { // 创建first之后,读取下一节点时再读取权重
165+
String s2 = oneLine.next();// 读取权重
166+
double weight = Double.parseDouble(s2);
167+
newNode.weight = weight;
168+
newNode.end = num;
169+
}
170+
171+
// newNode.key=num; // 被 newNode.end=num;代替了
172+
173+
newNode.start = count;
174+
175+
newNode.link = null;
176+
if (first == null) {
177+
newNode.weight = 0;
178+
newNode.end = count;
179+
first = newNode;
180+
last = newNode;
181+
} else {
182+
last.link = newNode;
183+
last = newNode;
184+
}
185+
}
186+
graph[count] = first;
187+
list.add(first);
188+
count++;
189+
}
190+
return list;
191+
}
192+
193+
/**
194+
* 打印构建的图,起始节点,终止节点,起始节点到终止节点的权重
195+
*/
196+
public void printGraph(){
197+
for (int i = 0; i < list.size(); i++) {
198+
Node node=(Node) list.get(i);
199+
// System.out.println("以第"+(i+1)+"个节点为头节点的链表");
200+
//System.out.println(node.key);
201+
while(node!=null){
202+
// System.out.print("起始节点"+node.start+" ");
203+
// System.out.print("终止节点"+node.end+" ");
204+
// System.out.println("起始节点到终止节点的权重"+node.weight);
205+
node=node.link;
206+
}
207+
}
208+
}
209+
/**
210+
* 打印权重图
211+
*/
212+
public void printWeightGraph(){
213+
double[][] weightsArray=getWeightArray();
214+
for (int i = 0; i < weightsArray.length; i++) {
215+
System.out.println();
216+
double[] wa=weightsArray[i];
217+
for (int j = 0; j < wa.length; j++) {
218+
System.out.print(wa[j]+" ");
219+
}
220+
221+
}
222+
System.out.println();
223+
}
224+
225+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package breadth_first_search;
2+
3+
import java.util.List;
4+
5+
import breadth_first_search.BreadthFirstSearch.Node;
6+
7+
public class TestBreathFirstSearch {
8+
public static void main(String[] args) {
9+
BreadthFirstSearch bfs=new BreadthFirstSearch(100,0);
10+
bfs.createGraph();
11+
bfs.printGraph();
12+
bfs.printWeightGraph();
13+
bfs.shortestPathOfBFS(3);
14+
bfs.printShortestPathOfBFS(3);
15+
16+
}
17+
}

0 commit comments

Comments
 (0)