Java DFS不是';没有被正确地执行
我想解决这个问题,但我被问题的一小部分卡住了。简而言之,给定一棵树,该树的每个顶点都有一定的权重。我们将树的和定义为树中包含的所有节点的所有权重之和 我有N个节点,我想计算这N个节点中每一个的子树的总和。我想将此总和存储在数组Java DFS不是';没有被正确地执行,java,algorithm,depth-first-search,tree-traversal,Java,Algorithm,Depth First Search,Tree Traversal,我想解决这个问题,但我被问题的一小部分卡住了。简而言之,给定一棵树,该树的每个顶点都有一定的权重。我们将树的和定义为树中包含的所有节点的所有权重之和 我有N个节点,我想计算这N个节点中每一个的子树的总和。我想将此总和存储在数组res[]中。为此,我必须执行DFS并正确地汇总节点的权重。然而,我的DFS不是这样工作的,我不知道如何纠正它 编辑:我已经调试了代码,但不知道如何更正。它无法计算树叶的res[]值(对于树叶,它不返回任何内容)。此外,它不会为内部节点计算正确的值。我想我必须在dfs方法中
res[]
中。为此,我必须执行DFS并正确地汇总节点的权重。然而,我的DFS不是这样工作的,我不知道如何纠正它
编辑:我已经调试了代码,但不知道如何更正。它无法计算树叶的res[]
值(对于树叶,它不返回任何内容)。此外,它不会为内部节点计算正确的值。我想我必须在dfs
方法中定义新变量int tempRes
,然后返回这个变量,但在某个时候我必须将它归零,我不知道在哪里
package searching;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class CutTheTree {
static List<ArrayList<Integer>> adj; //list which stores adjacency nodes, i.e. at position i I have list of the neighbours of node i
static int N; //number of nodes
static int[] res; //store the sum of weights of tree rooted at node i
static boolean[] visited; //array which indicates if I have visited node or not
static int[] weights; //array of the given weights of each node
static int W; //this variable is not relevant to my problem
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line1 = br.readLine();
N = Integer.parseInt(line1);
String[] line2 = br.readLine().split(" ");
weights = new int[N]; //weights of each vertex
visited = new boolean[N];
res = new int[N];
adj = new ArrayList<ArrayList<Integer>>(N);
for (int i = 0; i < N; i++) {
adj.add(i, new ArrayList<Integer>());
}
for (int i = 0; i < line2.length; i++) {
weights[i] = Integer.parseInt(line2[i]);
}
W = 0; //total sum
for (int i = 0; i < weights.length; i++) {
W+= weights[i]; //total sum of all weights
}
for (int i = 0; i < N-1; i++) { //follow N-1 lines of the edges given as pairs, i.e. (1, 3) means edge from vertex 1 to vertex 3
String[] line3 = br.readLine().split(" ");
int start = Integer.parseInt(line3[0]);
int end = Integer.parseInt(line3[1]);
adj.get(start-1).add(end); //store adjacent nodes in a linked list; substract 1 from the vtx id, since the indexing starts from 0
adj.get(end-1).add(start); //example: vtx 1 is a neighbor of vtx 3 and vtx 3 is neigbor of vtx 1
}
dfs(1); //take vtx one as a root
for (int i = 0; i < N; i++) {
System.out.print(res[i] + " ");
}
}
// The problematic function!!!
private static int dfs(int root) {
int temp;
Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> neigh = new ArrayList<Integer>(); //list of unvisited neighoring vetrices of the current node
st.push(root);
visited[root-1] = true; //mark current node as visited
while(!st.isEmpty()){
int curr = st.pop();
if(isLeaf(curr)){
res[curr-1]= weights[curr-1];
return weights[curr-1];
}
else{
neigh = neighbours(curr);
if(neigh.size() == 0){
temp = weights[curr-1]; //if there is no unvisited nodes, return the weight function of the given node; however this does not work for the leaf nodes!
}
else{ //else I have to visit unvisited neighbors
res[curr-1] = weights[curr-1]; //the current res increases by the weight of the given node
for (int i = 0; i < neigh.size(); i++) {
int child = neigh.get(i);
visited[child-1] = true;
st.push(child);
res[curr-1]+= dfs(child); // for each unvisited neighbor I perform dfs and add the result to the corresponding index of res array
}
}
}
}
return 0;
//returns ArrayList of unvisited nodes of the current node
private static ArrayList<Integer> neighbours(int node){
ArrayList<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < adj.get(node-1).size(); i++) {
int child = adj.get(node-1).get(i);
if(!visited[child-1]){
res.add(child);
}
}
return res;
}
}
包搜索;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Stack;
公共级切花树{
静态列表adj;//存储邻接节点的列表,即在位置i处有节点i的邻居列表
静态int N;//节点数
static int[]res;//存储在节点i上生根的树的权重之和
静态布尔值[]已访问;//表示是否已访问节点的数组
静态int[]权重;//每个节点的给定权重数组
static int W;//此变量与我的问题无关
公共静态void main(字符串[]args)引发IOException{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
字符串line1=br.readLine();
N=整数.parseInt(第1行);
字符串[]line2=br.readLine().split(“”);
权重=新整数[N];//每个顶点的权重
已访问=新布尔值[N];
res=新整数[N];
adj=新阵列列表(N);
对于(int i=0;i
您的dfs
方法返回0,叶节点除外
此外,您似乎混合了递归和迭代方法。如果您使用自己的未访问节点堆栈,则不需要依赖递归提供的调用堆栈
基本上,您需要访问每个节点。在每次访问中,您将节点的权重添加到单个和中,然后将其子节点添加到堆栈中
int result = 0;
while(!st.isEmpty()){
int curr = st.pop();
neigh = neighbours(curr);
result += weights[curr-1];
if(neigh.size() != 0){
for (int i = 0; i < neigh.size(); i++) {
int child = neigh.get(i);
visited[child-1] = true;
st.push(child);
}
}
}
return result;
int结果=0;
而(!st.isEmpty()){
int curr=st.pop();
neigh=邻居(curr);
结果+=权重[curr-1];
如果(neigh.size()!=0){
对于(int i=0;i
到目前为止,您试图解决这个问题的方法是什么?您是否调试了代码以确定失败的位置和原因?我添加了它,因为我必须在最后返回一些int;但是它从未返回