Skip to content

Commit 4b7acc4

Browse files
committed
commit
1 parent 8a75558 commit 4b7acc4

File tree

67 files changed

+5364
-14
lines changed

Some content is hidden

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

67 files changed

+5364
-14
lines changed

.metadata/.log

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,73 @@ Command-line arguments: -os linux -ws gtk -arch x86_64
215215

216216
!ENTRY org.eclipse.core.resources 2 10035 2017-09-30 08:44:47.990
217217
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.
218+
219+
!ENTRY org.eclipse.ui 4 0 2017-09-30 11:11:50.430
220+
!MESSAGE Unhandled event loop exception
221+
!STACK 0
222+
java.lang.NullPointerException
223+
at org.eclipse.jdt.internal.compiler.util.ObjectVector.contains(ObjectVector.java:75)
224+
at org.eclipse.jdt.internal.codeassist.InternalExtendedCompletionContext.computeVisibleElementBindings(InternalExtendedCompletionContext.java:217)
225+
at org.eclipse.jdt.internal.codeassist.InternalExtendedCompletionContext.getVisibleElements(InternalExtendedCompletionContext.java:368)
226+
at org.eclipse.jdt.internal.codeassist.InternalCompletionContext.getVisibleElements(InternalCompletionContext.java:324)
227+
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.getAssignableElements(ParameterGuessingProposal.java:113)
228+
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.guessParameters(ParameterGuessingProposal.java:311)
229+
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.computeGuessingCompletion(ParameterGuessingProposal.java:246)
230+
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.computeReplacementString(ParameterGuessingProposal.java:212)
231+
at org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal.getReplacementString(LazyJavaCompletionProposal.java:330)
232+
at org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal.apply(AbstractJavaCompletionProposal.java:364)
233+
at org.eclipse.jdt.internal.ui.text.java.JavaMethodCompletionProposal.apply(JavaMethodCompletionProposal.java:57)
234+
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.apply(ParameterGuessingProposal.java:124)
235+
at org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal.apply(AbstractJavaCompletionProposal.java:477)
236+
at org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal.apply(LazyJavaCompletionProposal.java:488)
237+
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.insertProposal(CompletionProposalPopup.java:940)
238+
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.insertSelectedProposalWithMask(CompletionProposalPopup.java:891)
239+
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.verifyKey(CompletionProposalPopup.java:1323)
240+
at org.eclipse.jface.text.contentassist.ContentAssistant$InternalListener.verifyKey(ContentAssistant.java:808)
241+
at org.eclipse.jface.text.TextViewer$VerifyKeyListenersManager.verifyKey(TextViewer.java:491)
242+
at org.eclipse.swt.custom.StyledTextListener.handleEvent(StyledTextListener.java:65)
243+
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
244+
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
245+
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1300)
246+
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1285)
247+
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1079)
248+
at org.eclipse.swt.custom.StyledText.handleKeyDown(StyledText.java:5932)
249+
at org.eclipse.swt.custom.StyledText$7.handleEvent(StyledText.java:5629)
250+
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
251+
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
252+
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1300)
253+
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1285)
254+
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1312)
255+
at org.eclipse.swt.widgets.Widget.gtk_key_press_event(Widget.java:748)
256+
at org.eclipse.swt.widgets.Control.gtk_key_press_event(Control.java:3050)
257+
at org.eclipse.swt.widgets.Composite.gtk_key_press_event(Composite.java:741)
258+
at org.eclipse.swt.widgets.Widget.windowProc(Widget.java:1773)
259+
at org.eclipse.swt.widgets.Control.windowProc(Control.java:5116)
260+
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4377)
261+
at org.eclipse.swt.internal.gtk.OS._gtk_main_do_event(Native Method)
262+
at org.eclipse.swt.internal.gtk.OS.gtk_main_do_event(OS.java:8317)
263+
at org.eclipse.swt.widgets.Display.eventProc(Display.java:1193)
264+
at org.eclipse.swt.internal.gtk.OS._g_main_context_iteration(Native Method)
265+
at org.eclipse.swt.internal.gtk.OS.g_main_context_iteration(OS.java:2342)
266+
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3184)
267+
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
268+
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
269+
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
270+
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
271+
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
272+
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
273+
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
274+
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
275+
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
276+
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
277+
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
278+
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
279+
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
280+
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
281+
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
282+
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
283+
at java.lang.reflect.Method.invoke(Method.java:606)
284+
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
285+
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
286+
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
287+
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
14+
}
15+
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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()+1][list.size()+1];
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;
60+
int col=node.end;
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=list.size();
77+
int k=getLength(list.get(vertex));
78+
smallWeight=new double[k];
79+
for (int i = 0; i < smallWeight.length; i++) {
80+
smallWeight[i]=weights[vertex][i+1];
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;
88+
for (int i = 0; i < weightFound.length; i++) {
89+
minWeight=Double.MAX_VALUE;
90+
for (int j = 0; j < weightFound.length; j++) {
91+
if(!weightFound[j]){
92+
if(smallWeight[j]<minWeight){
93+
v=j;
94+
minWeight=smallWeight[v];
95+
}
96+
weightFound[j]=true;
97+
}
98+
}
99+
for (int j = 0; j < weightFound.length; j++) {
100+
if(!weightFound[j]){
101+
if(minWeight+weis[v][j]<smallWeight[j]){
102+
smallWeight[j]=minWeight+weis[v][j];
103+
}
104+
}
105+
}
106+
}
107+
108+
}
109+
110+
public void printShortestPathOfBFS(int vertex){
111+
DecimalFormat twoDigits=new DecimalFormat("0.00");
112+
for (int i = 0; i < list.size(); i++) {
113+
System.out.println(" "+i+"\t\t"+twoDigits.format(smallWeight[i]));
114+
}
115+
}
116+
117+
/**
118+
* 得到链表的长度
119+
* @param node
120+
* @return
121+
*/
122+
public int getLength(Node node){
123+
int length=0;
124+
while(node.link!=null){
125+
node=node.link;
126+
length++;
127+
}
128+
return length;
129+
}
130+
131+
/**
132+
* 创建图,以链表的方式创建图
133+
*
134+
* @return 返回图的链表形式,其中数组中每个位置是一个顶点的链表
135+
*/
136+
// public Node[] createGraph(){
137+
public List createGraph() {
138+
Class clazz = this.getClass();
139+
InputStream ins = clazz.getResourceAsStream("/data.txt"); // 通过外部数据创建链表,使用/加载src目录下的文件
140+
// 不使用/是加载类路径下的文件
141+
Scanner scanner = new Scanner(ins); // 流输入。
142+
while (scanner.hasNextLine()) {
143+
String s = scanner.nextLine();
144+
Scanner oneLine = new Scanner(s);
145+
Node first = null;
146+
Node newNode = null, last = null;
147+
while (oneLine.hasNext()) {
148+
String s1 = oneLine.next();
149+
150+
int num = Integer.parseInt(s1);
151+
if (num == 999)
152+
break;
153+
newNode = new Node();
154+
155+
if (first != null && oneLine.hasNext()) { // 创建first之后,读取下一节点时再读取权重
156+
String s2 = oneLine.next();// 读取权重
157+
double weight = Double.parseDouble(s2);
158+
newNode.weight = weight;
159+
newNode.end = num;
160+
}
161+
162+
// newNode.key=num; // 被 newNode.end=num;代替了
163+
164+
newNode.start = count;
165+
166+
newNode.link = null;
167+
if (first == null) {
168+
newNode.weight = 0;
169+
newNode.end = count;
170+
first = newNode;
171+
last = newNode;
172+
} else {
173+
last.link = newNode;
174+
last = newNode;
175+
}
176+
}
177+
graph[count] = first;
178+
list.add(first);
179+
count++;
180+
}
181+
return list;
182+
}
183+
184+
/**
185+
* 打印构建的图,起始节点,终止节点,起始节点到终止节点的权重
186+
*/
187+
public void printGraph(){
188+
for (int i = 0; i < list.size(); i++) {
189+
Node node=(Node) list.get(i);
190+
System.out.println("以第"+(i+1)+"个节点为头节点的链表");
191+
//System.out.println(node.key);
192+
while(node!=null){
193+
System.out.print("起始节点"+node.start+" ");
194+
System.out.print("终止节点"+node.end+" ");
195+
System.out.println("起始节点到终止节点的权重"+node.weight);
196+
node=node.link;
197+
}
198+
}
199+
}
200+
/**
201+
* 打印权重图
202+
*/
203+
public void printWeightGraph(){
204+
double[][] weightsArray=getWeightArray();
205+
for (int i = 0; i < weightsArray.length; i++) {
206+
System.out.println();
207+
double[] wa=weightsArray[i];
208+
for (int j = 0; j < wa.length; j++) {
209+
System.out.print(wa[j]+" ");
210+
}
211+
212+
}
213+
}
214+
215+
}

0 commit comments

Comments
 (0)