dag的深度优先搜索

 1 int edge[V][V], pre[V], post[V], tag;
 2 void dfstag(int cur, int n) { // vertex: 0 ~ n-1
 3     pre[cur] = ++tag;
 4     for (int i = 0; i < n; ++i)
 5         if (edge[cur][i]) {
 6             if (0 == pre[i]) {
 7                 cout<<"cur: "<<cur<<"i: "<<i;
 8                 printf("Tree Edge!\n");//代表深度优先搜索树中的边
 9                 dfstag(i, n);
10             } else {
11                 if (0 == post[i]){
12                     cout<<"cur: "<<cur<<"i: "<<i;
13                     printf("Back Edge!\n");//回边:在生成的深度优先搜索树中,在探测<v,w>时,发现w是v的祖先
14                 }else if (pre[i] > pre[cur]){
15                     cout<<"cur: "<<cur<<"i: "<<i;
16                     printf("Down Edge!\n");//前向边:在生成的深度优先搜索树中,在探测<v,w>时,发现w是v的孩子
17                 }
18                 else{
19                     cout<<"cur: "<<cur<<"i: "<<i;
20                     printf("Cross Edge!\n");//横跨边:其余的所有边
21 
22                 }
23             }
24         }
25     post[cur] = ++tag;
26 }

posted on 2012-07-09 00:05  kakamilan  阅读(479)  评论(0编辑  收藏  举报

导航