图03--[AOV网&&最小生成树&&Prim算法&&Kruskal算法]
1.AVO网
1.1基本概念
1.2拓扑排序
1.3拓扑排序思路
1.4拓扑排序思路
卡恩算法因为删掉原数据 所以需要对卡恩算法进行一定的优化
1.5作业
2.生成树
2.1基本概念
2.2最小生成树
2.3切分定理
2.4Prim算法--执行过程
2.5prim算法实现
private Set<EdgeInfo<V, E>> prim() { Iterator<Vertex<V, E>> it = vertices.values().iterator(); if (!it.hasNext()) return null; Vertex<V, E> vertex = it.next(); Set<EdgeInfo<V, E>> edgeInfos = new HashSet<>(); Set<Vertex<V, E>> addedVertices = new HashSet<>(); addedVertices.add(vertex); MinHeap<Edge<V, E>> heap = new MinHeap<>(vertex.outEdges, edgeComparator); int verticesSize = vertices.size(); while (!heap.isEmpty() && addedVertices.size() < verticesSize) { Edge<V, E> edge = heap.remove(); if (addedVertices.contains(edge.to)) continue; edgeInfos.add(edge.info()); addedVertices.add(edge.to); heap.addAll(edge.to.outEdges); } return edgeInfos; }
2.6Kruskal算法--执行过程
2.7Kruskal算法--实现
private Set<EdgeInfo<V, E>> kruskal() { int edgeSize = vertices.size() - 1; if (edgeSize == -1) return null; Set<EdgeInfo<V, E>> edgeInfos = new HashSet<>(); MinHeap<Edge<V, E>> heap = new MinHeap<>(edges, edgeComparator); UnionFind<Vertex<V, E>> uf = new UnionFind<>(); vertices.forEach((V v, Vertex<V, E> vertex) -> { uf.makeSet(vertex); }); while (!heap.isEmpty() && edgeInfos.size() < edgeSize) { Edge<V, E> edge = heap.remove(); if (uf.isSame(edge.from, edge.to)) continue; edgeInfos.add(edge.info()); uf.union(edge.from, edge.to); } return edgeInfos; }