我用我在“Pål GD”答案的第三条评论中提到的方法解决了这个问题。这是它的java代码。希望对您有所帮助!
// BFS to find the depth of every node (from source node)
// graph is the adjacency matrix.
// elements of row zero and column zero are all useless. this program
// works with indices >=1
private int[][] BFS (int[][] graph, int source, boolean SPedges){
int[][] temp = null;
// nodes is number of graph nodes. (nodes == graph.length - 1)
if (SPedges){
temp = new int[nodes + 1][nodes + 1];
}
else{
depth[source] = 0;
}
LinkedList<Integer> Q = new LinkedList<Integer>();
Q.clear();
visited[source] = true;
Q.addFirst(source);
while (!Q.isEmpty()){
int u = Q.removeLast();
for (int k = 1; k <= nodes; k++){
if (!SPedges){
// checking if there's a edge between node u and other nodes
if (graph[u][k] == 1 && visited[k] == false){
visited[k] = true;
depth[k] = depth[u] + 1;
Q.addFirst(k);
}
}
else{
if (graph[u][k] == 1 && depth[k] == depth[u] - 1){
Q.addFirst(k);
temp[k][u] = 1;
}
}
}
}
return temp;
}
// fills the edges of shortest path graph in flow
private ArrayList<Edge> maxFlow(int[][] spg, int source, int sink){
int u = source;
ArrayList<Integer> path = new ArrayList<Integer> (depth[sink]);
path.add(source);
Arrays.fill(visited, false);
visited[source] = true;
for (int i = 1; i <= nodes + 1; i++){
if (i == nodes + 1){
if (u == source)
break;
u = path.get(path.size() - 2);
i = path.remove(path.size() - 1);
}
else if(spg[u][i] == 1 && visited[i] == false){
visited[i] = true;
path.add(i);
if (i == sink){
for(int k = 0; k < path.size() - 1; k++){
spg[path.get(k)][path.get(k+1)] = 0;
spg[path.get(k+1)][path.get(k)] = 1;
}
i = 0;
u = source;
path.clear();
path.add(u);
Arrays.fill(visited, false);
}
else{
u = i;
i = 0;
}
}
}
LinkedList<Integer> Q = new LinkedList<Integer>();
Q.clear();
Arrays.fill(visited, false);
visited[source] = true;
Q.addFirst(source);
while (!Q.isEmpty()){
u = Q.removeLast();
for (int k = 1; k <= nodes; k++){
if (spg[u][k] == 1 && visited[k] == false){
visited[k] = true;
Q.addFirst(k);
}
}
}
ArrayList<Edge> edges = new ArrayList<Edge>();
for (int i = 1; i <= nodes; i++){
for (int j = 1; j <= nodes; j++){
if ((spg[i][j] == 1) && (visited[i] ^ visited[j])){
edges.add(new Edge(i, j));
}
}
}
return edges;
}
public void Solv(){
// adjacency matrix as g. represents the graph.
// first we find depth of each node corresponding to source node by a BFS from source
BFS(g, s, false);
// shortest path length from source to sink (node t)
SPL = depth[t];
// shortest path graph
// it's a subgraph of main graph consisting only edges that are in a shortest path
// between s and t
spg = BFS(g, t, true);
// lastly we find edges of a min cut in shortest paths graph
// and store them in "edges"
edges = maxFlow(spg, s, t);
}
class Edge{
private int begin, end;
public Edge(int begin, int end){
this.begin = begin;
this.end = end;
}
@Override
public String toString() {
return new String(String.valueOf(begin) + " " + String.valueOf(end));
}
}