zoukankan      html  css  js  c++  java
  • 有向图的深度/广度优先遍历算法

    // 邻接表存储与广度和深度优先算法
    #include <iostream> 
    using namespace std;

    #define MAX_VERTEX_NUM 100

    typedef enum {
     DG,DN,UDG,UDN
    }GraphKind;

    typedef struct EdgeNode {
     int adjvex; // 存储邻接点在顶点中的位置
     struct EdgeNode *nextedge;
     int weight;
    }EdgeNode;

    typedef struct VexNode {
     char vex;
     EdgeNode *firstedge;
    }VexNode;

    typedef struct {
     VexNode vexs[MAX_VERTEX_NUM];
     int vexnum, edgenum;
     GraphKind kind;
    }LGraph;

    // 构造有向图的邻接表,顶点数n,边数 e
    void CreateDG(LGraph &G, int n, int e) {
     char ch;
     int i, j;
     G.vexnum = n;
     G.edgenum = e;
     // 顶点信息初始化
     for (i = 0; i < n; i++) {
      cin >> ch;
      G.vexs[i].vex = ch;
      G.vexs[i].firstedge = NULL;
     }
     // 边的信息初始化
     for (int k = 0; k < e; k++) {
      cin >> i >> j;
      EdgeNode *p = new EdgeNode;
      p->adjvex = j;
      p->nextedge = G.vexs[i].firstedge;
      G.vexs[i].firstedge = p; // 采用头插法来构建
     }
    }

    // 有向图的深度优先算法
    int visited[MAX_VERTEX_NUM];

    void DFS(LGraph &G, int i) {
     visited[i] = 1;
     cout << G.vexs[i].vex << " ";
     int j; // 当前访问的节点信息
     EdgeNode *p;
     p = G.vexs[i].firstedge;
     while (p) {
      j = p->adjvex;
      if (!visited[j]) {
       DFS(G, j);
      }
      p = p->nextedge;
     }
    }

    void DFS_Traverse(LGraph &G) {

     for (int i = 0; i < G.vexnum; i++) {
      visited[i] = 0;
     }

     for (int i = 0; i < G.vexnum; i++) {
      if (!visited[i]) {
       DFS(G, i);
      }
     }
    }

    //  有向图的广度优先遍历
    const int Queue_Size = 100;

    typedef struct circlQueue {
     int *elem;
     int front, rear;
     int queueSize;
    }circlQueue;

    // 循环队列初始化
    void init_circleQueue(circlQueue &Q) {
     Q.elem = new int[Queue_Size];
     Q.front = Q.rear = 0;
     Q.queueSize = Queue_Size;
    }

    // 入队列
    void enterQueue(circlQueue &Q, int x) {
     // 判满
     if ((Q.rear + 1)%Q.queueSize == Q.front) {
      cout << "Queue OverFlow!" << endl;
     }
     Q.elem[Q.rear] = x;
     Q.rear = (Q.rear + 1) % Q.queueSize;
    }

    // 出队列
    void outQueue(circlQueue &Q, int &e) {
     // 判空
     if (Q.front == Q.rear) {
      cout << "Queue Empty!" << endl;
     }
     e = Q.elem[Q.front];
     Q.front = (Q.front + 1) % Q.queueSize;
    }

    // 广度优先
    void BFS_Traverse(LGraph &G) {
     for (int i = 0; i < G.vexnum; i++)
      visited[i] = 0;

     circlQueue Q;
     init_circleQueue(Q);
     int v1,v2;
     for (int i = 0; i < G.vexnum; i++) {
      if (!visited[i]) {
       visited[i] = 1;
       cout << G.vexs[i].vex << " ";
       enterQueue(Q, i);
       // 队列不空
       while (Q.rear != Q.front) {
        outQueue(Q, v1);
        EdgeNode *p;
        p = G.vexs[v1].firstedge;
        while(p){
         v2 = p->adjvex;
         if (!visited[v2]) {
          cout << G.vexs[v2].vex << " ";
          visited[v2] = 1;
          enterQueue(Q, v2);
         }
         p = p->nextedge;
        }
       }
      }
     }
    }

    int main() {
     LGraph G;
     int n, e;
     cout << "请输入顶点数目:" << endl;
     cin >> n;
     cout << "请输入边的数目:" << endl;
     cin >> e;
     CreateDG(G, n, e);
     cout << "深度优先搜索结果:" << endl;
     DFS_Traverse(G);
     cout << endl;
     cout << "广度优先搜索结果:" << endl;
     BFS_Traverse(G);

     system("pause");
     return 0;

    }

  • 相关阅读:
    python使用smtplib库和smtp.qq.com邮件服务器发送邮件
    使用CreateRemoteThread把代码远程注入指定exe执行
    python带cookie提交表单自动登录
    linux+win7双系统重装win7修复grub的办法
    最后总结
    Alpha项目测试--个人第五次作业
    第四次结对编程作业
    第三次作业--原型设计
    熟悉使用工具---第二次作业
    虫虫吃第一颗豆子---第一次作业
  • 原文地址:https://www.cnblogs.com/codingtao/p/6430429.html
Copyright © 2011-2022 走看看