克鲁斯卡尔算法(Kruskal)的java实现

作者在 2007-06-20 21:15:00 发布以下内容

下面是我对软件工程教程里面的克鲁斯卡尔算法的实现,发现在网是很少有网友贴出,为了方便大家查询,所以贴出来了,还希望大家能多指点.

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<g.length;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<g.length;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);
 }
}

java算法设计 | 阅读 4702 次
文章评论,共0条
游客请输入验证码