Today I am here to upload and present the code I previously designed to sift out the loops demanded of standing out of the group, the strong branch loops.
This set of code runs two dfs detours to get work done.
Scenario: Define each item in a graph as "stand", each stand has in and out directive arrows to form a graph.
The basic rule streaming: First dfs half "forward" detour to number all the stands--First dfs half "back" detour to name another ids for all the stands from zero to the numbers of stands--Reverse the direction of arrows for all the stands--Pick the largest ID stand as the starting point--Launch a second dfs search to group strong branches out into individual strong loops
I have tested different random sequential inputs for this set of code and it works fine, but optimization using dynamic regulation is very likely to enhance the performance when it comes to massive inputs in PB grade, another something is the detailed data structure selection in storing temporary data.
这是运行后的generated output,再下面是源码。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.PriorityQueue;
public class StrongBranchSearch {
private static StrongBranchSearch mBranchSearch;
private LinkedList<Vertex> q = new LinkedList<Vertex>();
private int counter = 0;
private LinkedList<Vertex> vList;
private int[][] edgeGroup;
private Graph mGraph;
private ArrayList<ArrayList<Integer>> strongBranchList;
private int reverseCount = 0;
private Vertex currentTop;
private ArrayList<Integer> strongBranch;
public void findArt(Vertex v) {
v.visited = true;
HashSet<Integer> childList = mGraph.adjacencyList.get(v.index);
if (childList != null) {
for (int i : childList) {
Vertex w = mGraph.vertexList.get(i);
if (!w.visited) {
w.parent = v;
findArt(w);
} else {
if (w.parent != v) {
}
}
}
}
v.num = counter++;
q.add(v);
System.out.print(v.num + ",");
}
public void reverseAllEdges() {
mGraph.adjacencyList.clear();
mGraph.currentKey = -1;
mGraph.currentSet.clear();
for (int k = 0; k < edgeGroup.length; k++) {
mGraph.addEdge(edgeGroup[k][1], edgeGroup[k][0]);
// System.out.println("\n" + edgeGroup[k][1] + " " +
// edgeGroup[k][0]);
}
}
public void getVertexToSearch(int pos) {
ArrayList<Vertex> vList = mGraph.vertexList;
if (vList != null) {
int len = vList.size();
System.out.println("vertext list size " + len);
if (pos <= len - 1) {
boolean isFirst = true;
for (int k = pos; k < len; k++) {
if (!isFirst && k == pos)
break;
Vertex first = vList.get(k);
if (!first.visited) {
findArt(first);
}
if (k == len - 1) {
k = 0;
isFirst = false;
}
}
}
reverseAllEdges();
clearVisited();
Iterator<Vertex> orderList = q.descendingIterator();
while (orderList.hasNext()) {
currentTop = orderList.next();
if (!currentTop.known) {
reverseCount = currentTop.num;
System.out.print("\n" + "reverse num " + reverseCount);
ArrayList<Integer> tmp = new ArrayList<Integer>();
tmp.add(currentTop.index);
strongBranchSearch(currentTop, tmp);
}
}
System.out.print("\n" + "final group size "
+ strongBranchList.size());
for (ArrayList<Integer> tmp : strongBranchList) {
System.out.println("");
for (int k : tmp) {
System.out.print(k + " ");
}
}
}
}
public void clearVisited() {
for (Vertex v : mGraph.vertexList) {
v.visited = false;
}
}
public void strongBranchSearch(Vertex v, ArrayList<Integer> list) {
if (strongBranchList == null) {
strongBranchList = new ArrayList<ArrayList<Integer>>();
}
v.visited = true;
HashSet<Integer> childList = mGraph.adjacencyList.get(v.index);
if (childList != null) {
// System.out.print("\n" + "currentTop " + currentTop.index);
for (int k : childList) {
Vertex w = mGraph.vertexList.get(k);
if (!w.visited) {
strongBranchSearch(w, list);
if (w.isLoop) {
if (!v.equals(currentTop) && !list.contains(v.index)) {
list.add(v.index);
}
v.known = true;
v.isLoop = true;
}
} else {
if (w.equals(currentTop)) {
list.add(v.index);
v.isLoop = true;
v.known = true;
}
}
}
if (v.equals(currentTop)) {
strongBranchList.add(list);
}
} else {
ArrayList<Integer> tmpList = new ArrayList<Integer>();
tmpList.add(v.index);
strongBranchList.add(tmpList);
v.known = true;
}
}
public static void main(String[] args) {
mBranchSearch = new StrongBranchSearch();
mBranchSearch.mGraph = new Graph(10);
mBranchSearch.edgeGroup = new int[][] { { 0, 1 }, { 0, 3 }, { 1, 2 },
{ 1, 5 }, { 2, 0 }, { 2, 3 }, { 2, 4 }, { 3, 4 }, { 5, 2 },
{ 6, 5 }, { 6, 7 }, { 7, 5 }, { 7, 9 }, { 8, 7 }, { 9, 8 } };
for (int k = 0; k < mBranchSearch.edgeGroup.length; k++) {
mBranchSearch.mGraph.addEdge(mBranchSearch.edgeGroup[k][0],
mBranchSearch.edgeGroup[k][1]);
}
mBranchSearch.getVertexToSearch(1);
}
}

浙公网安备 33010602011771号