深度优先搜索树遍历C ++中的实现

时间:2016-02-16 08:32:35

标签: c++ algorithm recursion depth-first-search

问题陈述:说我有n个项目,每个项目的重量都列在某处。我必须选择项目的每个组合,并找到每个组合的总重量(不在示例代码中计算)。我想使用DFS遍历树的每个顶点(每个顶点都有一个唯一的组合)。

例如,对于3个项目,树将如下所示:

------------------------------------
        [000]
------------------------------------
        [000]
        /   
    [100]
------------------------------------
        [000]
        /   
    [100]
      /
  [110]
------------------------------------ 
        [000]
        /   
    [100]
      /
  [110]
   /
[111]   
------------------------------------
        [000]
        /   
    [100]
      /  \
  [110]  [101]
   /
[111]
------------------------------------
        [000]
        /  \ 
    [100]  [010]
      /  \
  [110]  [101]
   /       
[111]
------------------------------------
        [000]
        /  \ 
    [100]  [010]
      /  \     \
  [110]  [101] [011]
   /       
[111]
------------------------------------
        [000]----
        /  \     \
    [100]  [010]  [001]
      /  \     \
  [110]  [101] [011]
   /       
[111]

因此将有7种选择组合, nlist = [item1 item2 item3] 即如果item1和item3是choosem,则nlist为[1 0 1]。

我已经给出了以下代码。最重要的变量是

next  --- the next item#
list  --- current selection
nlist --- next selection

我期待以下输出:

#1 ==> next: 1 | list: [000] | nlist: [100]
#2 ==> next: 2 | list: [100] | nlist: [110]
#3 ==> next: 3 | list: [110] | nlist: [111]
#4 ==> next: 3 | list: [100] | nlist: [101]
#5 ==> next: 2 | list: [000] | nlist: [010]
#6 ==> next: 3 | list: [010] | nlist: [011]
#7 ==> next: 3 | list: [000] | nlist: [001]

然而,我得到了这个:

#1 ==> next: 1 | list: [000] | nlist: [100]
#2 ==> next: 2 | list: [100] | nlist: [110]
#3 ==> next: 3 | list: [110] | nlist: [111]
#4 ==> next: 3 | list: [111] | nlist: [111] <-- unexpected output begins in this line
#5 ==> next: 2 | list: [111] | nlist: [111]
#6 ==> next: 3 | list: [111] | nlist: [111]
#7 ==> next: 3 | list: [111] | nlist: [111]
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>


using namespace std;

// Declaration of global variables
const int num_IP = 3;
const int num_func = 4;
      int L[num_IP][num_func];
      int Pwr[num_IP];
      int perf[num_func];
      int constraint_matrix[num_func];
      int * p_nlist;
      int iteration;     

int * init_list( ){
  static int list[num_IP];
  for ( int index = 0; index < num_IP; index++ )
    list[index]=0; 
  return list; 
}

int * init_nlist( ){
  static int nlist[num_IP];
  for ( int index = 0; index < num_IP; index++ )
    nlist[index]=0; 
  return nlist;
}

// branch function
int * branch( int * p_list, int next )
{
  cout << '#' << iteration << " ==> " << "next: " << next+1 << " | ";
  cout << "list: [";
  for ( int index = 0; index < num_IP; index++ )
    cout << *(p_list+index);
  cout << "] | ";

  * (p_nlist + next) = 1;

  cout << "nlist: [";
  for ( int index = 0; index < num_IP; index++ )
    cout << *(p_nlist+index);
  cout << "]" << '\n';
  iteration++;

  for ( int i = next+1; i < num_IP; i++ ){
    p_nlist = branch ( p_nlist , i );
  }

  return p_list;
}

////////////////////////////////
// Main Function
////////////////////////////////

int main(){
  // variable declaration and initialization
  int index;   // index variable used for most iterations
  iteration = 1;
  int list[num_IP];
  int nlist[num_IP];
  int * p_list;

  p_list = init_list( );
  p_nlist = init_nlist( );

  // branch implementation  
  for ( int next = 0; next < num_IP; next++ ){
      p_nlist = branch ( p_nlist , next );      
  }

  return 0;
}

请让我知道我做错了什么。我有一个类似的Matlab实现,并且工作正常。

Matlab中的主要功能,

% main.m
global num_IP
list = [];
for next=1:num_IP
    [nlist] = branch(list,next);
end

和分支函数,

% branch.m
function [list] = branch(list,next)

global num_IP

disp('----------------');
next
list
nlist = [list,next]

for i = (next+1):num_IP
    [nlist] = branch(nlist,i);
end

1 个答案:

答案 0 :(得分:0)

我不会评论你的程序,只是指出错误......

您正使用1将数组元素设置为*(p_nlist + next) = 1。您永远不会将它们设置回0,所以很明显,一旦设置为0,它们将永远不再是1

在递归调用之后,您只需要添加1行来将修改后的元素更改回0:

...

for (int i = next + 1; i < num_IP; i++){
    p_nlist = branch(p_nlist, i);
}

*(p_nlist + next) = 0; // ADDED

return p_list;

...