Java 如何修改Ford Fulkerson最大流算法以打印访问的节点?
我需要修改最大流算法,以便查看与最大流相关的路径。不需要打印与图相关的所有节点,我希望打印具有相应容量的图的最大流的路径 它应该是这样的 ** ** 因此,通过增加容量可以验证最大流量,因为两个值相同 请帮我做这个,提前谢谢 公共级MaxFlow_Ford_Fulkerson{Java 如何修改Ford Fulkerson最大流算法以打印访问的节点?,java,algorithm,ford-fulkerson,Java,Algorithm,Ford Fulkerson,我需要修改最大流算法,以便查看与最大流相关的路径。不需要打印与图相关的所有节点,我希望打印具有相应容量的图的最大流的路径 它应该是这样的 ** ** 因此,通过增加容量可以验证最大流量,因为两个值相同 请帮我做这个,提前谢谢 公共级MaxFlow_Ford_Fulkerson{ static class Graph { int vertices; int graph[][]; public Graph(int vertex, int[][] graph) { this.vertices
static class Graph {
int vertices;
int graph[][];
public Graph(int vertex, int[][] graph) {
this.vertices = vertex;
this.graph = graph;
}
public int findMaxFlow(int source, int sink) {
int[][] residualGraph = new int[vertices][vertices];
for (int i = 0; i <vertices ; i++) {
for (int j = 0; j <vertices ; j++) {
residualGraph[i][j] = graph[i][j];
}
}
int [] parent = new int[vertices];
int max_flow = 0;
while(isPathExist_BFS(residualGraph, source, sink, parent)){
int flow_capacity = Integer.MAX_VALUE;
int t = sink;
while(t!=source){
int s = parent[t];
flow_capacity = Math.min(flow_capacity, residualGraph[s][t]);
t = s;
}
t = sink;
while(t!=source){
int s = parent[t];
residualGraph[s][t]-=flow_capacity;
residualGraph[t][s]+=flow_capacity;
t = s;
}
max_flow+=flow_capacity;
}
return max_flow;
}
public boolean isPathExist_BFS(int [][] residualGraph, int src, int dest, int [] parent){
boolean pathFound = false;
boolean [] visited = new boolean[vertices];
Queue<Integer> queue = new LinkedList<>();
queue.add(src);
parent[src] = -1;
visited[src] = true;
while(queue.isEmpty()==false){
int u = queue.poll();
for (int v = 0; v <vertices ; v++) {
if(visited[v]==false && residualGraph[u][v]>0) {
queue.add(v);
parent[v] = u;
visited[v] = true;
}
}
}
pathFound = visited[dest];
return pathFound;
}}*
*
**After visited[v]=true,System.out.print(b)它打印我想要的与最大流相关的路径的所有节点After visited[v]=true,System.out.print(b)它打印我想要的与最大流相关的路径的所有节点
static class Graph {
int vertices;
int graph[][];
public Graph(int vertex, int[][] graph) {
this.vertices = vertex;
this.graph = graph;
}
public int findMaxFlow(int source, int sink) {
int[][] residualGraph = new int[vertices][vertices];
for (int i = 0; i <vertices ; i++) {
for (int j = 0; j <vertices ; j++) {
residualGraph[i][j] = graph[i][j];
}
}
int [] parent = new int[vertices];
int max_flow = 0;
while(isPathExist_BFS(residualGraph, source, sink, parent)){
int flow_capacity = Integer.MAX_VALUE;
int t = sink;
while(t!=source){
int s = parent[t];
flow_capacity = Math.min(flow_capacity, residualGraph[s][t]);
t = s;
}
t = sink;
while(t!=source){
int s = parent[t];
residualGraph[s][t]-=flow_capacity;
residualGraph[t][s]+=flow_capacity;
t = s;
}
max_flow+=flow_capacity;
}
return max_flow;
}
public boolean isPathExist_BFS(int [][] residualGraph, int src, int dest, int [] parent){
boolean pathFound = false;
boolean [] visited = new boolean[vertices];
Queue<Integer> queue = new LinkedList<>();
queue.add(src);
parent[src] = -1;
visited[src] = true;
while(queue.isEmpty()==false){
int u = queue.poll();
for (int v = 0; v <vertices ; v++) {
if(visited[v]==false && residualGraph[u][v]>0) {
queue.add(v);
parent[v] = u;
visited[v] = true;
}
}
}
pathFound = visited[dest];
return pathFound;
}}*
public static void main(String[] args) {
int vertices = 6;
int graph[][] = { {0, 10, 8, 0, 0, 0},
{0, 0, 5, 5, 0, 0},
{0, 4, 0, 0, 10, 0},
{0, 0, 9, 0, 10, 3},
{0, 0, 0, 6, 0, 14},
{0, 0, 0, 0, 0, 0}
};
Graph g = new Graph(vertices, graph);
int source = 0;
int destination = 5;
int max_flow = g.findMaxFlow(source,destination);
System.out.println("Maximum flow : " max_flow); }}