for (Integer integer : adj[s]) { n = integer; if (!visitedVertices[n]) DFSUtil(n, visitedVertices); } }
/** * 逆转图 * @return 方向调转之后的图 */ public KosarajuGraph transpose(){ KosarajuGraph g = new KosarajuGraph(V); for (int s = 0; s < V; s++) { for (Integer integer : adj[s]) g.adj[integer].add(s); } return g; }
publicvoidfillOrder(int s, boolean[] visitedVertices, Deque<Integer> stack){ visitedVertices[s] = true; for (int n : adj[s]) { if (!visitedVertices[n]) fillOrder(n, visitedVertices, stack); } stack.push(s); }
publicvoidprintSCC(){ Deque<Integer> stack = new ArrayDeque<>(); boolean[] visitedVertices = newboolean[V]; // 1. 初始化访问列表 for (int i = 0; i < V; i++) visitedVertices[i] = false;
for (int i = 0; i < V; i++) { if (!visitedVertices[i]) fillOrder(i, visitedVertices, stack); }
KosarajuGraph graph = transpose();
for (int i = 0; i < V; i++) visitedVertices[i] = false;
while (!stack.isEmpty()) { int s = stack.pop(); if (!visitedVertices[s]) { graph.DFSUtil(s, visitedVertices); System.out.println(); } } }
/** * @AUTHOR: raymond * @DATETIME: 2020/8/12 09:08 * DESCRIPTION: Kruskal 算法生成最小生成树 O(e*lge) * 1. Sort all the edges from low weight to high * 2. Take the edge with the lowest weight and add it to the spanning tree. * If adding the edge created a cycle, then reject this edge. * 3. Keep adding edges until we reach all vertices. **/ publicclassKruskalGraph{
privateint vertices, edges;
private Edge[] edge;
publicKruskalGraph(int v, int e){ this.vertices = v; this.edges = e; this.edge = new Edge[edges]; for (int i = 0; i < e; i++) edge[i] = new Edge(); }
publicintfind(Subset[] subsets, int i){ if (subsets[i].parent != i) subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; }
publicvoidunion(Subset[] subsets, int x, int y){ int xRoot = find(subsets, x); int yRoot = find(subsets, x);
/** * Kruskal 具体实现 */ publicvoidkruskal(){ Edge[] res = new Edge[vertices]; int e = 0, i = 0; for (i = 0; i < vertices; ++i) res[i] = new Edge();
// 1. 排序 Arrays.sort(edge); Subset[] subsets = new Subset[vertices]; for (i = 0; i < vertices; ++i) subsets[i] = new Subset();
for (int v = 0; v < vertices; ++v) { subsets[v].parent = v; subsets[v].rank = 0; } i = 0; while (e < vertices - 1) { Edge nextEdge = new Edge(); nextEdge = edge[i++]; int x = find(subsets, nextEdge.src); int y = find(subsets, nextEdge.dest); if (x != y) { res[e++] = nextEdge; union(subsets, x, y); } }
for (i = 0; i < e; ++i) System.out.println("src-" + res[i].src + " - " + "dest-" + res[i].dest + " - " + "weight-" + res[i].weight); }
publicstaticvoidmain(String[] args){ int vertices = 6; // Number of vertices int edges = 8; // Number of edges KruskalGraph G = new KruskalGraph(vertices, edges);
/** * @AUTHOR: raymond * @DATETIME: 2020/8/12 09:33 * DESCRIPTION: Prim 算法生成最小生成树 O(ElgV) * 1. Initialize the minimum spanning tree with a vertex chosen at random. * 2. Find all the edges that connect the tree to new vertices, find the minimum and add it to the tree. * 3. Keep repeating step 2 until we get a minimum spanning tree. **/ publicclassPrimGraph{