Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
1367356 committed Oct 3, 2017
1 parent 115d6a7 commit d435be9
Show file tree
Hide file tree
Showing 166 changed files with 17,724 additions and 9 deletions.
85 changes: 85 additions & 0 deletions .metadata/.log
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,88 @@ Command-line arguments: -os linux -ws gtk -arch x86_64

!ENTRY org.eclipse.core.resources 2 10035 2017-10-03 09:41:52.109
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.

!ENTRY org.eclipse.ui 4 0 2017-10-03 14:48:58.507
!MESSAGE Unhandled event loop exception
!STACK 0
java.lang.IllegalArgumentException: Argument cannot be null
at org.eclipse.swt.SWT.error(SWT.java:4342)
at org.eclipse.swt.SWT.error(SWT.java:4276)
at org.eclipse.swt.SWT.error(SWT.java:4247)
at org.eclipse.swt.widgets.Widget.error(Widget.java:480)
at org.eclipse.swt.widgets.Control.removeMouseMoveListener(Control.java:1989)
at org.eclipse.jface.text.AbstractInformationControl$5.mouseUp(AbstractInformationControl.java:403)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:220)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3562)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3186)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)

!ENTRY org.eclipse.ui 4 0 2017-10-03 16:12:08.646
!MESSAGE Unhandled event loop exception
!STACK 0
org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(SWT.java:4361)
at org.eclipse.swt.SWT.error(SWT.java:4276)
at org.eclipse.swt.SWT.error(SWT.java:4247)
at org.eclipse.swt.widgets.Widget.error(Widget.java:480)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:417)
at org.eclipse.swt.widgets.Control.redraw(Control.java:3443)
at org.eclipse.swt.widgets.Link.gtk_event_after(Link.java:353)
at org.eclipse.swt.widgets.Widget.windowProc(Widget.java:1768)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:5116)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4377)
at org.eclipse.swt.internal.gtk.OS._gtk_main_do_event(Native Method)
at org.eclipse.swt.internal.gtk.OS.gtk_main_do_event(OS.java:8317)
at org.eclipse.swt.widgets.Control.dragDetect(Control.java:2272)
at org.eclipse.swt.widgets.Control.dragDetect(Control.java:2217)
at org.eclipse.swt.widgets.Control.dragDetect(Control.java:2170)
at org.eclipse.swt.custom.StyledText.checkDragDetect(StyledText.java:1966)
at org.eclipse.swt.custom.StyledText.handleMouseDown(StyledText.java:5986)
at org.eclipse.swt.custom.StyledText$7.handleEvent(StyledText.java:5632)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3562)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3186)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package minimal_spanning_tree;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;



/**
* 最小生成树的prim算法,从一个节点出发,每次找到与所有加入的节点相邻的节点,权重最小的节点加入,不能形成环。
*
* @author liyafei
*
*/
public class MSTPrim {

List list=new ArrayList<>();
int count=1;
double[][] weights;

class Node{
Node link;
Node pre;
int weight;
int key;
int start;
int end;
}

/**
* prim 算法求最小生成树,给定根节点,开始找相邻节点的最小值,算法每一步在连接集合A和A之外的节点的所有边中
* 选择一条轻量级边加入到A中
* 用一个列表将遍历过的轻量级边的节点存储起来,然后看新的轻量级边的两点是否在列表中,如果在,就不加入。
* @param r 根节点
* 注意:图的矩阵表示形式是对称的,可以利用
*/
public void mstprim(int r){
List list2=new ArrayList<>();
for (int i = 0; i < weights.length; i++) {
Node node=(Node) list.get(i);
node.key=Integer.MAX_VALUE;
node.pre=null;
}
// Node node=(Node) list.get(r);
// node.key=0;

// double[] arr=weights[r-1]; //取出给定顶点相邻节点的权重值,然后找出几行几列。排序
// double min = Double.MAX_VALUE;
// int index=0; //角标值
// for (int i = 0; i < arr.length; i++) {
// if(arr[i]>0 && arr[i]<min){
// min=arr[i];
// index=i;
// }
// }
//(r,index)这条线被包括进来了
// list2.add(r);
// if(!list2.contains(index)){
// list2.add(index);
// }
Map map=new HashMap();
list2.add(r);
while(list2.size()<2){
double minest=Double.MAX_VALUE;
int index2=0;
for (int i = 0; i < list2.size(); i++) {
double[] arr2=weights[(int) list2.get(i)];
int key=(int) list2.get(i);
for (int j = 0; j < arr2.length; j++) {
if(arr2[j]>0 && arr2[j]<minest){
System.out.println(arr2[j]);
if(!list2.contains(j)){
minest=arr2[j];
index2=j;
map.put(key, index2);
// System.out.println(index2);
}
}
}
}
list2.add(index2);
}


// double[] weis=weights[index];
// double[][] afteriter=new double[2][weights.length];
// afteriter[0]=arr;
// afteriter[1]=weis;
for (int i = 0; i < list2.size(); i++) {
System.out.println("key "+list2.get(i));
// System.out.println("value "+map.get(list2.get(i)));
}
// System.out.println(list2.size());
// System.out.println(min);
// System.out.println(index);
//
}

/**
* 得到创建的带有权重的图,读出相邻节点之间的距离,然后存储到二维数组weights中。
* 权重图的大小比节点多1,但是角标为0的位置都没用,为了处理存储的位置与节点的编号相一致
*/
public double[][] getWeightArray(){
weights=new double[list.size()][list.size()];
for (int i = 0; i < list.size(); i++) {
Node node=(Node) list.get(i);
while(node!=null){
int row=node.start-1;
int col=node.end-1;
double weight=node.weight;
weights[row][col]=weight;
node=node.link;
}

}
return weights;
}

/**
* 打印权重图
*/
public void printWeightGraph(){
double[][] weightsArray=getWeightArray();
for (int i = 0; i < weightsArray.length; i++) {

double[] wa=weightsArray[i];
for (int j = 0; j < wa.length; j++) {
System.out.print(wa[j]+" ");
}
System.out.println();
}
System.out.println();
}

/**
* 先构建带有权重的无向图。
*/
public List createGraph(){
Class clazz=this.getClass();
InputStream ins=clazz.getResourceAsStream("/data.txt");
Scanner scanner=new Scanner(ins);

while(scanner.hasNextLine()){
String s1=scanner.nextLine();
Scanner oneLine=new Scanner(s1);
Node newNode;
Node first=null;
Node last=null;
while(oneLine.hasNext()){
String s2=oneLine.next();
int end=Integer.parseInt(s2);
if(end==999){
break;
}
newNode=new Node();
if(first!=null && oneLine.hasNext()){
String s3=oneLine.next();
int weight=Integer.parseInt(s3);
newNode.weight=weight;
}
newNode.start=count;
newNode.end=end;
newNode.link=null;
if(first==null){
first=newNode;
last=newNode;
}else{
last.link=newNode;
last=newNode;
}
}
list.add(first);
count++;
}
return list;
}

}
Loading

0 comments on commit d435be9

Please sign in to comment.