【java】最小生成树(Prim算法,Kruskal算法)

本文提供了一个Java实现的图算法案例,详细展示了如何使用Prim算法与Kruskal算法寻找加权无向图中的最小生成树。通过定义顶点类和邻接矩阵,实现了图的基本操作,并通过具体示例比较了两种算法的不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



public class Q2 {

	class Vertex        //顶点类
	{
		String vertex_name;
		boolean new_or_old;       //"true" means "new"
		int f;
		Vertex(String vertex_name,boolean new_or_old,int f)
		{
			this.vertex_name=vertex_name;
			this.new_or_old=new_or_old;
			this.f=f;
		}
	}
	
	public int vertex_count=0;
	public Vertex v[] = new Vertex[6];
	public int [][] Adj_Matrix=new int [6][6];
	int search_count=vertex_count;
	
	public void set_vertex()
	{
		v[0]=new Vertex("a",true,0);
		v[1]=new Vertex("b",true,1);
		v[2]=new Vertex("c",true,2);
		v[3]=new Vertex("d",true,3);
		v[4]=new Vertex("e",true,4);
		v[5]=new Vertex("f",true,5);
	}

	public void setAdj_Matrix()
	{
		Adj_Matrix[0][0]=0;
		Adj_Matrix[0][1]=6;
		Adj_Matrix[0][2]=3;
		Adj_Matrix[0][3]=0;
		Adj_Matrix[0][4]=0;
		Adj_Matrix[0][5]=0;
	
		Adj_Matrix[1][0]=6;
		Adj_Matrix[1][1]=0;
		Adj_Matrix[1][2]=0;
		Adj_Matrix[1][3]=1;
		Adj_Matrix[1][4]=0;
		Adj_Matrix[1][5]=5;
	
		Adj_Matrix[2][0]=3;
		Adj_Matrix[2][1]=0;
		Adj_Matrix[2][2]=0;
		Adj_Matrix[2][3]=6;
		Adj_Matrix[2][4]=6;
		Adj_Matrix[2][5]=0;
	
		Adj_Matrix[3][0]=0;
		Adj_Matrix[3][1]=1;
		Adj_Matrix[3][2]=6;
		Adj_Matrix[3][3]=0;
		Adj_Matrix[3][4]=0;
		Adj_Matrix[3][5]=5;
	
		Adj_Matrix[4][0]=0;
		Adj_Matrix[4][1]=0;
		Adj_Matrix[4][2]=6;
		Adj_Matrix[4][3]=0;
		Adj_Matrix[4][4]=0;
		Adj_Matrix[4][5]=2;
	
		Adj_Matrix[5][0]=0;
		Adj_Matrix[5][1]=5;
		Adj_Matrix[5][2]=0;
		Adj_Matrix[5][3]=5;
		Adj_Matrix[5][4]=2;
		Adj_Matrix[5][5]=0;
	}
	
	public void renew_vertex()     //将全部顶点状态设置为new
	{
		for(vertex_count=0;vertex_count<6;vertex_count++)
		{
			v[vertex_count].new_or_old=true;
		}
	}
	
	public int old_vertex;
	public int new_vertex;
	public void Prim()
	{
		renew_vertex();
		v[0].new_or_old=false;
		int min_length=1234;
		int count=1;
		int old_vertex_count=0;
		int new_vertex_count=0;
		while(count<6)
		{
			min_length=1234;
			for(old_vertex_count=0;old_vertex_count<6;old_vertex_count++)
			{
				for(new_vertex_count=0;new_vertex_count<6;new_vertex_count++)
				{
					if(v[old_vertex_count].new_or_old==false&&v[new_vertex_count].new_or_old==true&&Adj_Matrix[old_vertex_count][new_vertex_count]<min_length&&Adj_Matrix[old_vertex_count][new_vertex_count]>0)
					{
						min_length=Adj_Matrix[old_vertex_count][new_vertex_count];
						old_vertex=old_vertex_count;
						new_vertex=new_vertex_count;
						
					}
				}
			}
			count++;
			v[new_vertex].new_or_old=false;
			System.out.println(v[old_vertex].vertex_name+"-"+v[new_vertex].vertex_name);
		}
	}
	
	public void Kruskal()
	{
		renew_vertex();
		int min_length=2333;
		int i = -1;
		int j = -1;
		int count=1;
		while(count<6)
		{
			min_length=2333;
			for(int i_count=0;i_count<6;i_count++)
			{
				for(int j_count=0;j_count<6;j_count++)
				{
					if(v[j_count].f!=v[i_count].f&&Adj_Matrix[i_count][j_count]!=0&&Adj_Matrix[i_count][j_count]<min_length)
					{
						min_length=Adj_Matrix[i_count][j_count];
						i=i_count;
						j=j_count;
					}
				}
			}
			count++;
			System.out.println(v[i].vertex_name+"-"+v[j].vertex_name);
			for(int k=0;k<6;k++)
			{
				if(v[k].f==v[j].f)
				v[k].f=v[i].f;
			}
		}
		
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Q2 q=new Q2();
		q.set_vertex();
		q.setAdj_Matrix();
		//测试Prim算法
		System.out.println("Prim算法:最小生成树为:");
		q.Prim();
		//测试Kruskal算法
		System.out.println("Kruskal算法:最小生成树为:");
		q.Kruskal();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值