Skip to content

Commit 8a75558

Browse files
committed
commit
1 parent 93b34e8 commit 8a75558

File tree

94 files changed

+4560
-72
lines changed

Some content is hidden

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

94 files changed

+4560
-72
lines changed

.metadata/.log

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

207207
!ENTRY org.eclipse.core.resources 2 10035 2017-09-29 21:21:02.730
208208
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.
209+
!SESSION 2017-09-30 08:44:41.629 -----------------------------------------------
210+
eclipse.buildId=debbuild
211+
java.version=1.7.0_131
212+
java.vendor=Oracle Corporation
213+
BootLoader constants: OS=linux, ARCH=x86_64, WS=gtk, NL=zh_CN
214+
Command-line arguments: -os linux -ws gtk -arch x86_64
215+
216+
!ENTRY org.eclipse.core.resources 2 10035 2017-09-30 08:44:47.990
217+
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package breadth_first_search;
2+
3+
import java.io.InputStream;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Scanner;
7+
8+
import RepresentationGraph.Graph.Node;
9+
10+
public class BreadthFirstSearch {
11+
protected Node[] graph; // 以数组的方式存储图,需要初始化指定数组的长度
12+
protected List<Node> list = new ArrayList<Node>(); // 以数组列表的形式存放图,可以不用初始化,直接添加
13+
protected int maxSize;
14+
protected int gSize;
15+
public int count = 1;
16+
public double[][] weights;
17+
18+
public BreadthFirstSearch(int maxSize, int gSize) {
19+
this.maxSize = maxSize;
20+
this.gSize = gSize;
21+
graph = new Node[maxSize];
22+
}
23+
24+
/**
25+
* 图中的节点
26+
*
27+
* @author liyafei
28+
*
29+
* @param <> 节点中的泛型 有三个属性,下一节点,关键字,两个节点之间的权重, 权重应该以矩阵的方式存储(也就是一个二维数组)
30+
* 可以使用一个开始值(start)和结束值(end)来代表权重(weight)是哪两个节点的。
31+
* 例如:start=2,end=4,weight=8,那么表示2号节点和4号节点之间的权重值为8;
32+
* 可以在data.txt里面每个相邻节点后面跟上权重值。 如果求最短距离时,可以用sumWeight记录到该节点总距离的最短距离
33+
*/
34+
public class Node {
35+
double weight;
36+
Node link;
37+
int key;
38+
int start;
39+
int end;
40+
double sumWeight=0;
41+
}
42+
43+
/**
44+
* 得到创建的带有权重的图,读出相邻节点之间的距离,然后存储到二维数组weights中。
45+
* 权重图的大小比节点多1,但是角标为0的位置都没用,为了处理存储的位置与节点的编号相一致
46+
*/
47+
public double[][] getWeightArray(){
48+
System.out.println(list.size());
49+
weights=new double[list.size()+1][list.size()+1];
50+
for (int i = 0; i < list.size(); i++) {
51+
Node node=(Node) list.get(i);
52+
int row=node.start;
53+
int col=node.end;
54+
double weight=node.weight;
55+
weights[row][col]=weight;
56+
}
57+
return weights;
58+
}
59+
60+
/**
61+
* 打印权重图
62+
*/
63+
public void printWeightGraph(){
64+
double[][] weightsArray=getWeightArray();
65+
for (int i = 0; i < weightsArray.length; i++) {
66+
System.out.println();
67+
double[] wa=weightsArray[i];
68+
for (int j = 0; j < wa.length; j++) {
69+
System.out.print(wa[j]+" ");
70+
}
71+
72+
}
73+
}
74+
75+
/**
76+
* 创建图,以链表的方式创建图
77+
*
78+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
79+
*/
80+
// public Node[] createGraph(){
81+
public List createGraph() {
82+
Class clazz = this.getClass();
83+
InputStream ins = clazz.getResourceAsStream("/data.txt"); // 通过外部数据创建链表,使用/加载src目录下的文件
84+
// 不使用/是加载类路径下的文件
85+
Scanner scanner = new Scanner(ins); // 流输入。
86+
while (scanner.hasNextLine()) {
87+
String s = scanner.nextLine();
88+
Scanner oneLine = new Scanner(s);
89+
Node first = null;
90+
Node newNode = null, last = null;
91+
while (oneLine.hasNext()) {
92+
String s1 = oneLine.next();
93+
94+
int num = Integer.parseInt(s1);
95+
if (num == 999)
96+
break;
97+
newNode = new Node();
98+
99+
if (first != null && oneLine.hasNext()) { // 创建first之后,读取下一节点时再读取权重
100+
String s2 = oneLine.next();// 读取权重
101+
double weight = Double.parseDouble(s2);
102+
newNode.weight = weight;
103+
newNode.end = num;
104+
}
105+
106+
// newNode.key=num; // 被 newNode.end=num;代替了
107+
108+
newNode.start = count;
109+
110+
newNode.link = null;
111+
if (first == null) {
112+
newNode.weight = 0;
113+
newNode.end = count;
114+
first = newNode;
115+
last = newNode;
116+
} else {
117+
last.link = newNode;
118+
last = newNode;
119+
}
120+
}
121+
graph[count] = first;
122+
list.add(first);
123+
count++;
124+
}
125+
return list;
126+
}
127+
128+
/**
129+
* 打印构建的图,起始节点,终止节点,起始节点到终止节点的权重
130+
*/
131+
public void printGraph(){
132+
for (int i = 0; i < list.size(); i++) {
133+
Node node=(Node) list.get(i);
134+
System.out.println("以第"+(i+1)+"个节点为头节点的链表");
135+
//System.out.println(node.key);
136+
while(node!=null){
137+
System.out.print("起始节点"+node.start+" ");
138+
System.out.print("终止节点"+node.end+" ");
139+
System.out.println("起始节点到终止节点的权重"+node.weight);
140+
node=node.link;
141+
}
142+
}
143+
}
144+
145+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package breadth_first_search;
2+
3+
import java.io.InputStream;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Scanner;
7+
8+
import RepresentationGraph.Graph.Node;
9+
10+
public class BreadthFirstSearch {
11+
protected Node[] graph; // 以数组的方式存储图,需要初始化指定数组的长度
12+
protected List<Node> list = new ArrayList<Node>(); // 以数组列表的形式存放图,可以不用初始化,直接添加
13+
protected int maxSize;
14+
protected int gSize;
15+
public int count = 1;
16+
public double[][] weights;
17+
18+
public BreadthFirstSearch(int maxSize, int gSize) {
19+
this.maxSize = maxSize;
20+
this.gSize = gSize;
21+
graph = new Node[maxSize];
22+
}
23+
24+
/**
25+
* 图中的节点
26+
*
27+
* @author liyafei
28+
*
29+
* @param <> 节点中的泛型 有三个属性,下一节点,关键字,两个节点之间的权重, 权重应该以矩阵的方式存储(也就是一个二维数组)
30+
* 可以使用一个开始值(start)和结束值(end)来代表权重(weight)是哪两个节点的。
31+
* 例如:start=2,end=4,weight=8,那么表示2号节点和4号节点之间的权重值为8;
32+
* 可以在data.txt里面每个相邻节点后面跟上权重值。 如果求最短距离时,可以用sumWeight记录到该节点总距离的最短距离
33+
*/
34+
public class Node {
35+
double weight;
36+
Node link;
37+
int key;
38+
int start;
39+
int end;
40+
double sumWeight=0;
41+
}
42+
43+
/**
44+
* 得到创建的带有权重的图,读出相邻节点之间的距离,然后存储到二维数组weights中。
45+
*/
46+
public double[][] getWeightArray(){
47+
return weights;
48+
}
49+
50+
/**
51+
* 创建图,以链表的方式创建图
52+
*
53+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
54+
*/
55+
// public Node[] createGraph(){
56+
public List createGraph() {
57+
Class clazz = this.getClass();
58+
InputStream ins = clazz.getResourceAsStream("/data.txt"); // 通过外部数据创建链表,使用/加载src目录下的文件
59+
// 不使用/是加载类路径下的文件
60+
Scanner scanner = new Scanner(ins); // 流输入。
61+
while (scanner.hasNextLine()) {
62+
String s = scanner.nextLine();
63+
Scanner oneLine = new Scanner(s);
64+
Node first = null;
65+
Node newNode = null, last = null;
66+
while (oneLine.hasNext()) {
67+
String s1 = oneLine.next();
68+
69+
int num = Integer.parseInt(s1);
70+
if (num == 999)
71+
break;
72+
newNode = new Node();
73+
74+
if (first != null && oneLine.hasNext()) { // 创建first之后,读取下一节点时再读取权重
75+
String s2 = oneLine.next();// 读取权重
76+
double weight = Double.parseDouble(s2);
77+
newNode.weight = weight;
78+
newNode.end = num;
79+
}
80+
81+
// newNode.key=num; // 被 newNode.end=num;代替了
82+
83+
newNode.start = count;
84+
85+
newNode.link = null;
86+
if (first == null) {
87+
newNode.weight = 0;
88+
newNode.end = count;
89+
first = newNode;
90+
last = newNode;
91+
} else {
92+
last.link = newNode;
93+
last = newNode;
94+
}
95+
}
96+
graph[count] = first;
97+
list.add(first);
98+
count++;
99+
}
100+
return list;
101+
}
102+
103+
/**
104+
* 打印构建的图,起始节点,终止节点,起始节点到终止节点的权重
105+
*/
106+
public void printGraph(){
107+
for (int i = 0; i < list.size(); i++) {
108+
Node node=(Node) list.get(i);
109+
System.out.println("以第"+(i+1)+"个节点为头节点的链表");
110+
//System.out.println(node.key);
111+
while(node!=null){
112+
System.out.print("起始节点"+node.start+" ");
113+
System.out.print("终止节点"+node.end+" ");
114+
System.out.println("起始节点到终止节点的权重"+node.weight);
115+
node=node.link;
116+
}
117+
}
118+
}
119+
120+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package breadth_first_search;
2+
3+
import java.io.InputStream;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Scanner;
7+
8+
import RepresentationGraph.Graph.Node;
9+
10+
public class BreadthFirstSearch {
11+
protected Node[] graph; //以数组的方式存储图,需要初始化指定数组的长度
12+
protected List<Node> list=new ArrayList<Node>(); //以数组列表的形式存放图,可以不用初始化,直接添加
13+
protected int maxSize;
14+
protected int gSize;
15+
public int count=1;
16+
public BreadthFirstSearch(int maxSize,int gSize){
17+
this.maxSize=maxSize;
18+
this.gSize=gSize;
19+
graph=new Node[maxSize];
20+
}
21+
/**
22+
*图中的节点
23+
* @author liyafei
24+
*
25+
* @param <> 节点中的泛型
26+
* 有三个属性,下一节点,关键字,两个节点之间的权重,
27+
* 权重应该以矩阵的方式存储(也就是一个二维数组)
28+
* 可以使用一个开始值(start)和结束值(end)来代表权重(weight)是哪两个节点的。
29+
* 例如:start=2,end=4,weight=8,那么表示2号节点和4号节点之间的权重值为8;
30+
* 可以在data.txt里面每个相邻节点后面跟上权重值。
31+
* 如果求最短距离时,可以用sumWeight记录到该节点总距离的最短距离
32+
*/
33+
public class Node{
34+
double weight;
35+
Node link;
36+
int key;
37+
int start;
38+
int end;
39+
double sumWeight;
40+
}
41+
42+
/**
43+
* 创建图,以链表的方式创建图
44+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
45+
*/
46+
// public Node[] createGraph(){
47+
public List createGraph(){
48+
Class clazz=this.getClass();
49+
InputStream ins=clazz.getResourceAsStream("/data.txt"); //通过外部数据创建链表,使用/加载src目录下的文件
50+
//不使用/是加载类路径下的文件
51+
Scanner scanner=new Scanner(ins); //流输入。
52+
while(scanner.hasNextLine()){
53+
String s=scanner.nextLine();
54+
Scanner oneLine=new Scanner(s);
55+
Node first=null;
56+
Node newNode = null,last=null;
57+
int trailNum=1;
58+
while(oneLine.hasNext()){
59+
String s1=oneLine.next();
60+
61+
int num=Integer.parseInt(s1);
62+
63+
if(first!=null && oneLine.hasNext()){ //创建first之后,读取下一节点时再读取权重
64+
String s2=oneLine.next();//读取权重
65+
double weight=Double.parseDouble(s2);
66+
newNode.weight=weight;
67+
newNode.end=num;
68+
}
69+
70+
if(num==999)
71+
break;
72+
newNode=new Node();
73+
//newNode.key=num; // 被 newNode.end=num;代替了
74+
75+
newNode.start=count;
76+
77+
newNode.link=null;
78+
if(first ==null){
79+
newNode.weight=0;
80+
newNode.end=trailNum++;
81+
first=newNode;
82+
last=newNode;
83+
}else{
84+
last.link=newNode;
85+
last=newNode;
86+
}
87+
}
88+
graph[count]=first;
89+
list.add(first);
90+
count++;
91+
}
92+
return list;
93+
}
94+
95+
}

0 commit comments

Comments
 (0)