Kruskal算法Java实现

import java.util.Scanner;


public class Main {

	/**
	 * @param args
	 */
	public static int parent[];//并查集
	public static int find(int x){
		while(parent[x]!=x) x=parent[x];
		return x;
	}
		
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Kruskal算法
		parent=new int[100];
		Graph G=new Graph();
		Scanner sc1=new Scanner(System.in);
		int num1=sc1.nextInt();
		int num2=sc1.nextInt();
		G.initEdgeNum(num1);//边的数量
		G.initVertexNum(num2);//点的数量
		G.initEdge();//初始化
		for(int i=0;i<G.vertexNum;i++){
			parent[i]=i;//初始化并查集
		}
		int num,i;
		for(num=0,i=0;i<G.edgeNum;i++){
			int vex1=find(G.edge[i].form);
			int vex2=find(G.edge[i].to);
			if(vex1!=vex2){
				System.out.print("("+G.edge[i].form+","+G.edge[i].to+")");
				System.out.println();
				parent[vex2]=vex1;
				num++;
				if(num==num2-1)
					return ;
			}
		}
		
		
	}
	
	
	

	
}
	





class Graph{
		EdgeType edge[];//存放所有带权边
		int vertexNum,edgeNum;//点和边的数目
		//初始化点和边的数目
		public void initVertexNum(int num){
			vertexNum=num;
		}
		
		public void initEdgeNum(int num){
			edgeNum=num;
		}
		
		public void initEdge(){//初始化所有边
			Scanner sc=new Scanner(System.in);
			int n=sc.nextInt();
			edge=new EdgeType[n];
			
			for(int i=0;i<n;i++){
				edge[i]=new EdgeType();
				edge[i].form=sc.nextInt();
				edge[i].to=sc.nextInt();
				edge[i].weight=sc.nextInt();
			}
			
		}
	}	
	
	

class EdgeType{//存储所有边的属性
		int form=0;//起始顶点
		int to=0;//结束顶点
		int weight=0;//权值
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kruskal算法是一种最小生成树的算法,它以边为基础来构建最小生成树。下面是Java语言实现Kruskal算法的代码示例: ``` import java.util.*; class Edge implements Comparable<Edge> { int src, dest, weight; public int compareTo(Edge compareEdge) { return this.weight - compareEdge.weight; } } class Graph { int V, E; Edge edge[]; Graph(int v, int e) { V = v; E = e; edge = new Edge[E]; for (int i = 0; i < e; ++i) edge[i] = new Edge(); } int find(int parent[], int i) { if (parent[i] == -1) return i; return find(parent, parent[i]); } void Union(int parent[], int x, int y) { int xset = find(parent, x); int yset = find(parent, y); parent[xset] = yset; } void KruskalMST() { Edge result[] = new Edge[V]; int e = 0; int i = 0; for (i = 0; i < V; ++i) result[i] = new Edge(); Arrays.sort(edge); int parent[] = new int[V]; Arrays.fill(parent, -1); i = 0; while (e < V - 1) { Edge next_edge = new Edge(); next_edge = edge[i++]; int x = find(parent, next_edge.src); int y = find(parent, next_edge.dest); if (x != y) { result[e++] = next_edge; Union(parent, x, y); } } System.out.println("Following are the edges in " + "the constructed MST"); int minimumCost = 0; for (i = 0; i < e; ++i) { System.out.println(result[i].src + " -- " + result[i].dest + " == " + result[i].weight); minimumCost += result[i].weight; } System.out.println("Minimum Cost Spanning Tree " + minimumCost); } } public class KruskalAlgorithm { public static void main(String[] args) { int V = 4; int E = 5; Graph graph = new Graph(V, E); graph.edge[0].src = 0; graph.edge[0].dest = 1; graph.edge[0].weight = 10; graph.edge[1].src = 0; graph.edge[1].dest = 2; graph.edge[1].weight = 6; graph.edge[2].src = 0; graph.edge[2].dest = 3; graph.edge[2].weight = 5; graph.edge[3].src = 1; graph.edge[3].dest = 3; graph.edge[3].weight = 15; graph.edge[4].src = 2; graph.edge[4].dest = 3; graph.edge[4].weight = 4; graph.KruskalMST(); } } ``` 以上代码实现Kruskal算法,并输出了最小生成树的边和权重。注意,这里的实现是使用了并查集来维护已经添加到生成树中的节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值