Prim算法

发布一个k8s部署视频:https://edu.youkuaiyun.com/course/detail/26967

课程内容:各种k8s部署方式。包括minikube部署,kubeadm部署,kubeasz部署,rancher部署,k3s部署。包括开发测试环境部署k8s,和生产环境部署k8s。

腾讯课堂连接地址https://ke.qq.com/course/478827?taid=4373109931462251&tuin=ba64518

第二个视频发布  https://edu.youkuaiyun.com/course/detail/27109

腾讯课堂连接地址https://ke.qq.com/course/484107?tuin=ba64518

介绍主要的k8s资源的使用配置和命令。包括configmap,pod,service,replicaset,namespace,deployment,daemonset,ingress,pv,pvc,sc,role,rolebinding,clusterrole,clusterrolebinding,secret,serviceaccount,statefulset,job,cronjob,podDisruptionbudget,podSecurityPolicy,networkPolicy,resourceQuota,limitrange,endpoint,event,conponentstatus,node,apiservice,controllerRevision等。

第三个视频发布:https://edu.youkuaiyun.com/course/detail/27574

详细介绍helm命令,学习helm chart语法,编写helm chart。深入分析各项目源码,学习编写helm插件
————————————————------------------------------------------------------------------------------------------------------------------

 

package com.data.struct;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

public class Prim {
	private Node[] list;
	private Set<Node> restSet = new HashSet<Node>();
	private Set<Node> computedSet = new HashSet<Node>();

	public Prim(int v, int e) {
		System.out.println("v:" + v + " e:" + e);
		list = new Node[v];
		for (int i = 0; i < v; i++) {
			Node node = new Node();
			node.id = i;
			node.key = Integer.MAX_VALUE;
			list[i] = node;
		}
		System.out.print("weight:");
		for (int i = 0; i < e; i++) {
			int v1 = new Random().nextInt(v);
			int v2 = new Random().nextInt(v);
			if (v1 == v2) {
				continue;
			}
			while (true) {
				Node node = list[v1];
				Node x = node;
				boolean already = false;
				while (node.next != null) {
					if (node.next.id == v2) {
						already = true;
						break;
					}
					node = node.next;
				}
				if (already == true) {
					break;
				}
				Node ex = new Node();
				ex.id = v2;
				ex.w = new Random().nextInt(e);
				System.out.print(ex.w + " ");
				node.next = ex;
				break;
			}
		}
		System.out.println();
	}

	public void prim() {
		list[0].key = 0;
		computedSet.add(list[0]);
		for (int i = 1; i < list.length; i++) {
			restSet.add(list[i]);
		}
		
		Node w=list[0].next;
		Node u=list[0];
		while (w != null) {
			if(w.w<list[w.id].key){
				w.parent=u;
				w.key=w.w;
			}
			w = w.next;
		}


		while (restSet.size() > 0) {
			 u = extractMin();
			 if(u==null){
				 break;
			 }
			computedSet.add(u);
			 w=u.next;
			while (w != null) {
				Iterator<Node> it2 = computedSet.iterator();
				boolean has = false;
				while (it2.hasNext()) {
					if (w.id == it2.next().id) {
						has = true;
						break;
					}
				}
				if (!has) {
					if(w.w<list[w.id].key){
						list[w.id].parent=u;
						list[w.id].key=w.w;
					}
				}
				w = w.next;
			}

		}

	}

	public Node extractMin() {
		Iterator<Node> it = computedSet.iterator();
		int id = -1;
		int minW = Integer.MAX_VALUE;
		while (it.hasNext()) {
			Node node = it.next();
			Node w = node.next;
			while (w != null) {
				Iterator<Node> it2 = computedSet.iterator();
				boolean has = false;
				while (it2.hasNext()) {
					if (w.id == it2.next().id) {
						has = true;
						break;
					}
				}
				if (!has) {
					if (minW > w.w) {
						id = w.id;
						minW = w.w;
					}
				}
				w = w.next;
			}
		}
		if(id==-1){
			return null;
		}
		return list[id];
	}

	public void printG() {
		for (int i = 0; i < list.length; i++) {
			Node node = list[i];
			System.out.print(node.id + "=>");
			while (node.next != null) {
				System.out.print(node.next.id + "(" + node.next.w + ")=>");
				node = node.next;
			}
			System.out.println();
		}

	}
	
	public void printTree(){
		for(int i=0;i<list.length;i++){
			Node node=list[i];
			while(node.parent!=null){
			  System.out.print(node.id +" ");;
			  node=node.parent;
			}
			System.out.print(node.id+" ");
			System.out.println();
		}
	}

	public Node findSet(Node x) {
		if (x.parent != x) {
			x.parent = findSet(x.parent);
		}
		return x.parent;
	}

	public static class Node {
		private int id;
		private Node next;
		private int w;
		private int key;
		private Node parent;
	}

	public static void main(String[] args) {
		Prim p = new Prim(5, 20);
		p.printG();
		p.prim();
		p.printTree();

	}

}

 

### Prim算法与Kruskal算法的比较 Prim算法和Kruskal算法都是用于求解加权连通图中最小生成树的经典算法。尽管它们的目标相同,但在实现方式、时间复杂度、空间复杂度以及适用场景等方面存在显著差异。 #### 时间复杂度 - **Prim算法**:在最基础的形式下,Prim算法的时间复杂度为 $O(n^2)$,其中 $n$ 表示顶点的数量。通过使用更高效的数据结构如二叉堆优化后,时间复杂度可以降低至 $O(E\log V)$,这里 $E$ 是边的数量,$V$ 是顶点的数量[^1]。 - **Kruskal算法**:Kruskal算法的时间复杂度主要受到排序所有边的影响,通常为 $O(E\log E)$ 或者等价于 $O(E\log V)$,因为边的数量最多可达 $V(V-1)/2$(对于完全图)[^1]。 #### 空间复杂度 - **Prim算法**:其空间复杂度主要取决于顶点数量,大约为 $O(V)$,因为它需要维护一个包含所有顶点的信息的数据结构来跟踪哪些顶点已经被加入到最小生成树中。 - **Kruskal算法**:其空间复杂度则与边数相关,约为 $O(E)$,主要用于存储所有的边及其权重信息。 #### 实现难度 - **Prim算法**:通常认为Prim算法比Kruskal算法更容易实现,尤其是当使用邻接矩阵作为数据结构时。它依赖于优先队列来选择下一个最近的顶点加入到已有的树中。 - **Kruskal算法**:相比之下,Kruskal算法的实现稍微复杂一些,因为它不仅需要对所有边按权重进行排序,还需要一种机制(如并查集)来检测和避免形成环路。 #### 适用场景 - **Prim算法**:更适合处理边稠密的图,即边的数量接近于顶点数量平方的情况。在这种情况下,Prim算法的性能优势更为明显[^3]。 - **Kruskal算法**:对于稀疏图而言,即边的数量远小于顶点数量平方的情况下,Kruskal算法的表现更加出色。由于只需要对所有边进行一次排序,因此在处理大规模稀疏图时效率更高。 综上所述,虽然两种算法都能有效地找到最小生成树,但根据具体的应用场景选择合适的算法是非常重要的。如果图是稠密的,那么Prim算法可能是更好的选择;而对于稀疏图,则推荐使用Kruskal算法。 ```python # 示例代码 - Kruskal算法的基本框架 class UnionFind: def __init__(self, size): self.parent = list(range(size)) def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): rootX = self.find(x) rootY = self.find(y) if rootX == rootY: return False self.parent[rootY] = rootX return True def kruskal(n, edges): uf = UnionFind(n) res = [] for u, v, weight in sorted(edges, key=lambda x: x[2]): if uf.union(u, v): res.append((u, v, weight)) if len(res) == n - 1: break return res ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hxpjava1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值