kruskal算法java_克鲁斯卡尔算法(Kruskal)的java实现
下面是我對軟件工程教程里面的克魯斯卡爾算法的實現,發現在網是很少有網友貼出,為了方便大家查詢,所以貼出來了,還希望大家能多指點.
package Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Kruskal
{
private static int vex=0,arc=0;//vex,arc分別表示頂點數和邊數
private static char [] Vex;
private static int [] Arc;
private Graphal [] g;
Kruskal()
{
try
{
System.out.println("請輸入無向圖的頂點個數:");
InputStreamReader oI0=new InputStreamReader(System.in);
BufferedReader oIn0=new BufferedReader(oI0);
String o0=oIn0.readLine();
vex=Integer.parseInt(o0);
System.out.println("請輸入無向圖的邊數:");
InputStreamReader oI1=new InputStreamReader(System.in);
BufferedReader oIn1=new BufferedReader(oI1);
String o1=oIn1.readLine();
arc=Integer.parseInt(o1);
}
catch(IOException e1)
{
e1.printStackTrace();
}
g=new Graphal[arc];
for(int i=0;i
{
String oo1=null,oo2=null;
int ooa=0;
System.out.print("請輸入頂點 ");
try
{
InputStreamReader oI=new InputStreamReader(System.in);
BufferedReader oIn=new BufferedReader(oI);
oo1=oIn.readLine();
}
catch(IOException e2)
{
e2.printStackTrace();
}
System.out.println("請輸入與頂點 "+oo1+"有關系的頂點");
try
{
InputStreamReader oI=new InputStreamReader(System.in);
BufferedReader oIn=new BufferedReader(oI);
oo2=oIn.readLine();
}
catch(IOException e3)
{
e3.printStackTrace();
}
System.out.println("請輸入頂點"+oo1+"->"+oo2+"的權值:");
try
{
InputStreamReader oI=new InputStreamReader(System.in);
BufferedReader oIn=new BufferedReader(oI);
String oa=oIn.readLine();
ooa=Integer.parseInt(oa);
}
catch(IOException e3)
{
e3.printStackTrace();
}
g=new Graphal(oo1,oo2,ooa);? //把輸入的數據填入數組g中
}
System.out.println("頂點1? ->? 頂點1\t權值");
for(int i=0;i
{
System.out.println(g);
}
}
public static void main(String [] args)
{
new Kruskal();
}
}
class Graphal// implements Comparable
{
//此類用于存儲無向圖的各頂點關系,及權值
private String v1;
private String v2;
private int a;
public Graphal(String oo1, String oo2, int ooa)
{
this.v1=oo1;
this.v2=oo2;
this.a=ooa;
}
public String? toString()
{
return(v1+"? ->? "+v2+"\t"+a);
}
}
總結
以上是生活随笔為你收集整理的kruskal算法java_克鲁斯卡尔算法(Kruskal)的java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 超时集合_确定性监视器脉冲/等
- 下一篇: java调用go接口_go语言调用API