Skip to content

Commit f114633

Browse files
committed
commit
1 parent 1eb342b commit f114633

File tree

91 files changed

+6795
-15
lines changed

Some content is hidden

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

91 files changed

+6795
-15
lines changed

.metadata/.log

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,37 @@ java.lang.NullPointerException
285285
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
286286
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
287287
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
288+
289+
!ENTRY org.eclipse.jdt.ui 4 10001 2017-09-30 15:50:29.460
290+
!MESSAGE Internal Error
291+
!STACK 1
292+
Java Model Exception: Java Model Status [Timed out while retrieving the attached javadoc for List [in List.class [in java.awt [in /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar]]] ]
293+
at org.eclipse.jdt.internal.core.JavaElement.getURLContents(JavaElement.java:808)
294+
at org.eclipse.jdt.internal.core.BinaryType.getJavadocContents(BinaryType.java:1037)
295+
at org.eclipse.jdt.internal.core.BinaryMethod.getAttachedJavadoc(BinaryMethod.java:639)
296+
at org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2.getHTMLContent(JavadocContentAccess2.java:463)
297+
at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.extractJavadoc(ProposalInfo.java:95)
298+
at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.computeInfo(ProposalInfo.java:75)
299+
at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.getInfo(ProposalInfo.java:58)
300+
at org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal.getAdditionalProposalInfo(AbstractJavaCompletionProposal.java:555)
301+
at org.eclipse.jface.text.contentassist.AdditionalInfoController$3.run(AdditionalInfoController.java:106)
302+
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
303+
!SUBENTRY 1 org.eclipse.jdt.core 4 1012 2017-09-30 15:50:29.461
304+
!MESSAGE Timed out while retrieving the attached javadoc for List [in List.class [in java.awt [in /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar]]]
305+
306+
!ENTRY org.eclipse.jdt.ui 4 10001 2017-09-30 15:51:36.799
307+
!MESSAGE Internal Error
308+
!STACK 1
309+
Java Model Exception: Java Model Status [Timed out while retrieving the attached javadoc for ArrayList [in ArrayList.class [in java.util [in /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar]]] ]
310+
at org.eclipse.jdt.internal.core.JavaElement.getURLContents(JavaElement.java:808)
311+
at org.eclipse.jdt.internal.core.BinaryType.getJavadocContents(BinaryType.java:1037)
312+
at org.eclipse.jdt.internal.core.BinaryMethod.getAttachedJavadoc(BinaryMethod.java:639)
313+
at org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2.getHTMLContent(JavadocContentAccess2.java:463)
314+
at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.extractJavadoc(ProposalInfo.java:95)
315+
at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.computeInfo(ProposalInfo.java:75)
316+
at org.eclipse.jdt.internal.ui.text.java.ProposalInfo.getInfo(ProposalInfo.java:58)
317+
at org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal.getAdditionalProposalInfo(AbstractJavaCompletionProposal.java:555)
318+
at org.eclipse.jface.text.contentassist.AdditionalInfoController$3.run(AdditionalInfoController.java:106)
319+
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
320+
!SUBENTRY 1 org.eclipse.jdt.core 4 1012 2017-09-30 15:51:36.800
321+
!MESSAGE Timed out while retrieving the attached javadoc for ArrayList [in ArrayList.class [in java.util [in /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/rt.jar]]]
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+
weightFound[j]=true;
102+
}
103+
}
104+
for (int j = 0; j < weightFound.length; j++) {
105+
if(!weightFound[j]){
106+
if(minWeight+weis[v][j]<smallWeight[j]){
107+
smallWeight[j]=minWeight+weis[v][j];
108+
}
109+
}
110+
}
111+
}
112+
113+
}
114+
115+
public void printShortestPathOfBFS(int vertex){
116+
DecimalFormat twoDigits=new DecimalFormat("0.00");
117+
System.out.println("source vertex"+vertex);
118+
System.out.println("shortest distance from the source to each vertex");
119+
for (int i = 0; i < list.size(); i++) {
120+
System.out.println(" "+(i)+"\t\t"+twoDigits.format(smallWeight[i]));
121+
System.out.println(" ");
122+
}
123+
}
124+
125+
/**
126+
* 得到链表的长度
127+
* @param node
128+
* @return
129+
*/
130+
public int getLength(Node node){
131+
int length=0;
132+
while(node.link!=null){
133+
node=node.link;
134+
length++;
135+
}
136+
return length;
137+
}
138+
139+
/**
140+
* 创建图,以链表的方式创建图
141+
*
142+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
143+
*/
144+
// public Node[] createGraph(){
145+
public List createGraph() {
146+
Class clazz = this.getClass();
147+
InputStream ins = clazz.getResourceAsStream("/data.txt"); // 通过外部数据创建链表,使用/加载src目录下的文件
148+
// 不使用/是加载类路径下的文件
149+
Scanner scanner = new Scanner(ins); // 流输入。
150+
while (scanner.hasNextLine()) {
151+
String s = scanner.nextLine();
152+
Scanner oneLine = new Scanner(s);
153+
Node first = null;
154+
Node newNode = null, last = null;
155+
while (oneLine.hasNext()) {
156+
String s1 = oneLine.next();
157+
158+
int num = Integer.parseInt(s1);
159+
if (num == 999)
160+
break;
161+
newNode = new Node();
162+
163+
if (first != null && oneLine.hasNext()) { // 创建first之后,读取下一节点时再读取权重
164+
String s2 = oneLine.next();// 读取权重
165+
double weight = Double.parseDouble(s2);
166+
newNode.weight = weight;
167+
newNode.end = num;
168+
}
169+
170+
// newNode.key=num; // 被 newNode.end=num;代替了
171+
172+
newNode.start = count;
173+
174+
newNode.link = null;
175+
if (first == null) {
176+
newNode.weight = 0;
177+
newNode.end = count;
178+
first = newNode;
179+
last = newNode;
180+
} else {
181+
last.link = newNode;
182+
last = newNode;
183+
}
184+
}
185+
graph[count] = first;
186+
list.add(first);
187+
count++;
188+
}
189+
return list;
190+
}
191+
192+
/**
193+
* 打印构建的图,起始节点,终止节点,起始节点到终止节点的权重
194+
*/
195+
public void printGraph(){
196+
for (int i = 0; i < list.size(); i++) {
197+
Node node=(Node) list.get(i);
198+
// System.out.println("以第"+(i+1)+"个节点为头节点的链表");
199+
//System.out.println(node.key);
200+
while(node!=null){
201+
// System.out.print("起始节点"+node.start+" ");
202+
// System.out.print("终止节点"+node.end+" ");
203+
// System.out.println("起始节点到终止节点的权重"+node.weight);
204+
node=node.link;
205+
}
206+
}
207+
}
208+
/**
209+
* 打印权重图
210+
*/
211+
public void printWeightGraph(){
212+
double[][] weightsArray=getWeightArray();
213+
for (int i = 0; i < weightsArray.length; i++) {
214+
System.out.println();
215+
double[] wa=weightsArray[i];
216+
for (int j = 0; j < wa.length; j++) {
217+
System.out.print(wa[j]+" ");
218+
}
219+
220+
}
221+
System.out.println();
222+
}
223+
224+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package class02;
2+
3+
public class BFS {
4+
5+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package class02;
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+
/**
11+
* 广度优先搜索:第22章第二节
12+
* @author liyafei
13+
*
14+
*/
15+
public class BFS {
16+
17+
public void breadthFirstSearch(int vertex){
18+
for (int i = 0; i < list.size(); i++) {
19+
20+
}
21+
}
22+
List list=new ArrayList();
23+
/**
24+
* 图节点
25+
* @author liyafei
26+
*
27+
*/
28+
public class Node{
29+
Node link;
30+
int key;
31+
String color="WHITE";
32+
}
33+
34+
/**
35+
* 创建图,以链表的方式创建图
36+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
37+
*/
38+
// public Node[] createGraph(){
39+
public List createGraph(){
40+
Class clazz=this.getClass();
41+
InputStream ins=clazz.getResourceAsStream("/data1.txt"); //通过外部数据创建链表,使用/加载src目录下的文件
42+
//不使用/是加载类路径下的文件
43+
Scanner scanner=new Scanner(ins); //流输入。
44+
while(scanner.hasNextLine()){
45+
String s=scanner.nextLine();
46+
Scanner oneLine=new Scanner(s);
47+
Node first=null;
48+
Node newNode,last=null;
49+
while(oneLine.hasNext()){
50+
String s1=oneLine.next();
51+
int num=Integer.parseInt(s1);
52+
if(num==999)
53+
break;
54+
newNode=new Node();
55+
newNode.key=num;
56+
newNode.link=null;
57+
if(first ==null){
58+
first=newNode;
59+
last=newNode;
60+
}else{
61+
last.link=newNode;
62+
last=newNode;
63+
}
64+
}
65+
list.add(first);
66+
}
67+
return list;
68+
}
69+
70+
/**
71+
* 打印图
72+
*/
73+
public void printGraph(){
74+
for (int i = 0; i < list.size(); i++) {
75+
76+
Node first=(Node) list.get(i);
77+
if(first==null){
78+
break;
79+
}
80+
System.out.println("打印了第"+(i+1)+"个节点的数据");
81+
while(first!=null){
82+
System.out.print(first.key+" ");
83+
first=first.link;
84+
}
85+
System.out.println("");
86+
}
87+
}
88+
89+
}

0 commit comments

Comments
 (0)