zoukankan      html  css  js  c++  java
  • DFS算法的实现

    #graph.h头文件

     1 #ifndef GRAPH_H
     2 #define GRAPH_H
     3 
     4 struct adjNode{
     5     int node;
     6     struct adjNode *next;
     7 };
     8 
     9 
    10 /*图的矩阵表示向邻接表表示的转换*/
    11 void matrixToAdjlist(int *matrix, adjNode *adjList, int n){
    12     int i, j;
    13     adjNode *tempNode;
    14     for(i=0; i<n; ++i){
    15         adjList[i].node=i;
    16         adjList[i].next=NULL;
    17 
    18         for(j=n-1; j>=0; j--){
    19             if(*(matrix+i*n+j)== 1){
    20                 tempNode=(adjNode *)malloc(sizeof(adjNode));
    21                 tempNode->next=adjList[i].next;
    22                 tempNode->node=j;
    23                 adjList[i].next=tempNode;
    24             }
    25         }
    26     }
    27 }
    28 
    29 /*释放邻接表中分配的空间*/
    30 void freeAdjList(adjNode *adjList, int n){
    31     int i;
    32     adjNode *tempNode;
    33 
    34     for(i=0; i<n; ++i){
    35         tempNode=adjList[i].next;
    36         while(tempNode != NULL){
    37             adjList[i].next=tempNode->next;
    38             free(tempNode);
    39             tempNode=adjList[i].next;
    40         }
    41     }
    42 
    43     free(adjList);
    44 }
    45 
    46 #endif // GRAPH_H

    main.h

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <vector>
      4 #include "graph.h"
      5 using namespace std;
      6 
      7 const int invalid_p=-1;
      8 int gtm=0;
      9 enum Color{w, g, b};
     10 
     11 struct DFS_struct{
     12     Color color;
     13     int parent;
     14     int dtime, ftime;  //节点的发现时间和邻接表扫描完成时间
     15 };
     16 
     17 void DFS_visit(adjNode *adjList, DFS_struct *dfsArray, int u){
     18     int v;
     19     adjNode *tempNode;
     20 
     21     dfsArray[u].color=g;
     22     gtm += 1;
     23     dfsArray[u].dtime=gtm;
     24 
     25     tempNode=adjList[u].next;
     26     while(tempNode != NULL){
     27         v=tempNode->node;
     28         if(dfsArray[v].color == w){
     29            dfsArray[v].parent=u;
     30            DFS_visit(adjList, dfsArray, v);
     31         }
     32 
     33         tempNode=tempNode->next;
     34     }
     35 
     36     dfsArray[u].color=b;
     37     gtm += 1;
     38     dfsArray[u].ftime=gtm;
     39 }
     40 
     41 void DFS(adjNode *adjList, DFS_struct *dfsArray, int n, vector<int> &forestRoots){
     42     int i;
     43     for(i=0; i<n; ++i){
     44         dfsArray[i].color=w;
     45         dfsArray[i].parent=invalid_p;
     46         dfsArray[i].dtime=0;
     47         dfsArray[i].ftime=0;
     48     }
     49 
     50     gtm=0;
     51     for(i=0; i<n; ++i)
     52         if(dfsArray[i].color == w){
     53             DFS_visit(adjList, dfsArray, i);    //每次调用都会生成一棵深度优先搜索树,最终生成深度优先搜索森林
     54             forestRoots.push_back(i);
     55     }
     56 }
     57 
     58 int main(){
     59     int *matrix;
     60     adjNode *adjList, *tempNode;
     61     int nodeNum=0, i, j;
     62     DFS_struct *dfsArray;
     63     vector<int> forestRoots;        //forestRoots中保存每棵深度优先搜索树的树根节点编号
     64 
     65     printf("Input node number: ");
     66     scanf("%d", &nodeNum);
     67 
     68     matrix=(int *)malloc(sizeof(int)*nodeNum*nodeNum);
     69     adjList=(adjNode *)malloc(sizeof(adjNode)*nodeNum);
     70 
     71     for(i=0; i<nodeNum; ++i)
     72         for(j=0; j<nodeNum; ++j)
     73             scanf("%d", matrix+i*nodeNum+j);
     74 
     75     /*以矩阵形式输出图*/
     76     printf("matrix: \n");
     77     for(i=0; i<nodeNum; ++i){
     78         for(j=0; j<nodeNum; ++j)
     79             printf("%d ", *(matrix+i*nodeNum+j));
     80         printf("\n");
     81     }
     82 
     83     matrixToAdjlist(matrix, adjList, nodeNum);
     84     /*以邻接表形式输出图*/
     85     printf("adjacency list: \n");
     86     for(i=0; i<nodeNum; ++i){
     87         printf("node %d:", adjList[i].node);
     88         tempNode=adjList[i].next;
     89         while(tempNode != NULL){
     90             printf("->%d", tempNode->node);
     91             tempNode=tempNode->next;
     92         }
     93         printf("\n");
     94     }
     95 
     96     dfsArray=(DFS_struct *)malloc(sizeof(DFS_struct)*nodeNum);
     97     DFS(adjList, dfsArray, nodeNum, forestRoots);
     98 
     99     /*在这里深度优先搜索森林已经建立,可以进行别的操作*/
    100     printf("DFS learning completed\n");
    101     printf("forest roots:");
    102     for(i=0; i<forestRoots.size(); ++i)
    103         printf("%d ", forestRoots[i]);
    104     printf("\n");
    105 
    106 
    107     free(matrix);
    108     free(dfsArray);
    109     freeAdjList(adjList, nodeNum);
    110     return 0;
    111 }
  • 相关阅读:
    NOIP2008双栈排序[二分图染色|栈|DP]
    洛谷P1108 低价购买[DP | LIS方案数]
    洛谷P1330封锁阳光大学[二分图染色]
    NOIP模板整理计划
    期中考试
    UVA 10564 Paths through the Hourglass[DP 打印]
    UVA 11404 Palindromic Subsequence[DP LCS 打印]
    POJ2479 Maximum sum[DP|最大子段和]
    POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
    UVA11324 The Largest Clique[强连通分量 缩点 DP]
  • 原文地址:https://www.cnblogs.com/wanghetao/p/2497147.html
Copyright © 2011-2022 走看看