我有一个最小生成树,我正在使用DFS遍历它,其中我的图形的数据结构是一个adjancancy列表。我想从源中获得最大权重的优势,并将其存储在一个数组中,这是我到目前为止所拥有的:
void recDFS(int currVertex, int source, int[] visited, int maxWeight){
visited[currVertex] = 1;
Vector<IntegerPair> neighbours = MSTAdjList.get(currVertex);
for(int j = 0; j < neighbours.size(); j++){
int neighbourWeight = neighbours.get(j)._second // get weight of neighbour
int neighbourIndex = neighbours.get(j)._first; // get index of neighbour
if (neighbourWeight > maxWeight)
maxWeight = neighbourWeight;
bigArrayWithValues[source][neighbourIndex] = maxWeight; // Store each value
recDFS(neighbourIndex, source, visited, maxWeight); // Call next neighbour
}
}
这不起作用,因为当递归中断时,maxWeight的值仍将具有“旧”值,而我希望具有最大权重的边缘来自源的每个节点,即固定..任何帮助将不胜感激。