Skip to content

Commit 93b34e8

Browse files
committed
commit
1 parent 7dbd64e commit 93b34e8

File tree

40 files changed

+881
-1
lines changed

40 files changed

+881
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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=0;
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+
*/
32+
public class Node{
33+
double weight;
34+
Node link;
35+
int key;
36+
int start;
37+
int end;
38+
}
39+
40+
/**
41+
* 创建图,以链表的方式创建图
42+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
43+
*/
44+
// public Node[] createGraph(){
45+
public List createGraph(){
46+
Class clazz=this.getClass();
47+
InputStream ins=clazz.getResourceAsStream("/data.txt"); //通过外部数据创建链表,使用/加载src目录下的文件
48+
//不使用/是加载类路径下的文件
49+
Scanner scanner=new Scanner(ins); //流输入。
50+
while(scanner.hasNextLine()){
51+
String s=scanner.nextLine();
52+
Scanner oneLine=new Scanner(s);
53+
Node first=null;
54+
Node newNode,last=null;
55+
while(oneLine.hasNext()){
56+
String s1=oneLine.next();
57+
58+
int num=Integer.parseInt(s1);
59+
if(first!=null){
60+
String s2=oneLine.next();//读取权重
61+
double weight=Double.parseDouble(s2);
62+
newNode.weight=weight;
63+
}
64+
if(num==999)
65+
break;
66+
newNode=new Node();
67+
// newNode.key=num; 被 newNode.end=num;代替了
68+
69+
newNode.start=count;
70+
71+
newNode.link=null;
72+
if(first ==null){
73+
first=newNode;
74+
last=newNode;
75+
}else{
76+
newNode.end=num;
77+
last.link=newNode;
78+
last=newNode;
79+
}
80+
}
81+
graph[count]=first;
82+
list.add(first);
83+
count++;
84+
}
85+
return list;
86+
}
87+
88+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package breadth_first_search;
2+
3+
public class BreadthFirstSearch {
4+
5+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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=0;
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+
*/
32+
public class Node{
33+
double weight;
34+
Node link;
35+
int key;
36+
int start;
37+
int end;
38+
}
39+
40+
/**
41+
* 创建图,以链表的方式创建图
42+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
43+
*/
44+
// public Node[] createGraph(){
45+
public List createGraph(){
46+
Class clazz=this.getClass();
47+
InputStream ins=clazz.getResourceAsStream("/data.txt"); //通过外部数据创建链表,使用/加载src目录下的文件
48+
//不使用/是加载类路径下的文件
49+
Scanner scanner=new Scanner(ins); //流输入。
50+
while(scanner.hasNextLine()){
51+
String s=scanner.nextLine();
52+
Scanner oneLine=new Scanner(s);
53+
Node first=null;
54+
Node newNode,last=null;
55+
while(oneLine.hasNext()){
56+
String s1=oneLine.next();
57+
int num=Integer.parseInt(s1);
58+
59+
if(num==999)
60+
break;
61+
newNode=new Node();
62+
newNode.key=num;
63+
newNode.link=null;
64+
if(first ==null){
65+
first=newNode;
66+
last=newNode;
67+
}else{
68+
last.link=newNode;
69+
last=newNode;
70+
}
71+
}
72+
graph[count]=first;
73+
list.add(first);
74+
count++;
75+
}
76+
return list;
77+
}
78+
79+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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=0;
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+
*/
32+
public class Node{
33+
double weight;
34+
Node link;
35+
int key;
36+
int start;
37+
int end;
38+
}
39+
40+
/**
41+
* 创建图,以链表的方式创建图
42+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
43+
*/
44+
// public Node[] createGraph(){
45+
public List createGraph(){
46+
Class clazz=this.getClass();
47+
InputStream ins=clazz.getResourceAsStream("/data.txt"); //通过外部数据创建链表,使用/加载src目录下的文件
48+
//不使用/是加载类路径下的文件
49+
Scanner scanner=new Scanner(ins); //流输入。
50+
while(scanner.hasNextLine()){
51+
String s=scanner.nextLine();
52+
Scanner oneLine=new Scanner(s);
53+
Node first=null;
54+
Node newNode = null,last=null;
55+
while(oneLine.hasNext()){
56+
String s1=oneLine.next();
57+
58+
int num=Integer.parseInt(s1);
59+
if(first!=null){
60+
String s2=oneLine.next();//读取权重
61+
double weight=Double.parseDouble(s2);
62+
newNode.weight=weight;
63+
}
64+
if(num==999)
65+
break;
66+
newNode=new Node();
67+
// newNode.key=num; 被 newNode.end=num;代替了
68+
69+
newNode.start=count;
70+
71+
newNode.link=null;
72+
if(first ==null){
73+
first=newNode;
74+
last=newNode;
75+
}else{
76+
newNode.end=num;
77+
last.link=newNode;
78+
last=newNode;
79+
}
80+
}
81+
graph[count]=first;
82+
list.add(first);
83+
count++;
84+
}
85+
return list;
86+
}
87+
88+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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=0;
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+
*/
32+
public class Node{
33+
double weight;
34+
Node link;
35+
int key;
36+
int start;
37+
int end;
38+
}
39+
40+
/**
41+
* 创建图,以链表的方式创建图
42+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
43+
*/
44+
// public Node[] createGraph(){
45+
public List createGraph(){
46+
Class clazz=this.getClass();
47+
InputStream ins=clazz.getResourceAsStream("/data.txt"); //通过外部数据创建链表,使用/加载src目录下的文件
48+
//不使用/是加载类路径下的文件
49+
Scanner scanner=new Scanner(ins); //流输入。
50+
while(scanner.hasNextLine()){
51+
String s=scanner.nextLine();
52+
Scanner oneLine=new Scanner(s);
53+
Node first=null;
54+
Node newNode = null,last=null;
55+
while(oneLine.hasNext()){
56+
String s1=oneLine.next();
57+
58+
int num=Integer.parseInt(s1);
59+
60+
if(first!=null && oneLine.hasNext()){ //创建first之后,读取下一节点时再读取权重
61+
String s2=oneLine.next();//读取权重
62+
double weight=Double.parseDouble(s2);
63+
newNode.weight=weight;
64+
}
65+
66+
if(num==999)
67+
break;
68+
newNode=new Node();
69+
// newNode.key=num; 被 newNode.end=num;代替了
70+
71+
newNode.start=count;
72+
73+
newNode.link=null;
74+
if(first ==null){
75+
first=newNode;
76+
last=newNode;
77+
}else{
78+
newNode.end=num;
79+
last.link=newNode;
80+
last=newNode;
81+
}
82+
}
83+
graph[count]=first;
84+
list.add(first);
85+
count++;
86+
}
87+
return list;
88+
}
89+
90+
}

0 commit comments

Comments
 (0)