鱼C论坛

 找回密码
 立即注册
查看: 1308|回复: 0

[学习笔记] BFS_DFS_Adjacency Matrix

[复制链接]
发表于 2019-1-13 19:14:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 pwnmelife 于 2019-1-13 20:00 编辑
  1. // BFS_DFS.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //


  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>

  6. #define MaxVertexNum 100
  7. typedef char VertexType;
  8. typedef int ElemType;
  9. typedef int EdgeType;
  10. typedef struct QNode{
  11.         ElemType data;
  12.         struct QNode* next;
  13. }QNode, *QNodePtr;
  14. typedef struct {
  15.         QNode *front, *rear;
  16. }Queue, *QueuePtr;

  17. typedef struct {
  18.         VertexType Vex[MaxVertexNum];
  19.         EdgeType Edge[MaxVertexNum][MaxVertexNum];
  20.         int vexnum, arnum;
  21. }MGraph;

  22. bool visited[MaxVertexNum] = {false};
  23. int number;


  24. void InitQueue(QueuePtr *q); //初始化
  25. void InsertQueue(QueuePtr *q, ElemType c); //入队
  26. void DeleteQueue(QueuePtr *q, ElemType *c);//出队
  27. bool IsEmpty(QueuePtr *q); //判空

  28. void InitGraph(MGraph* m, int number);
  29. void DFS(MGraph* m, int start);
  30. void BFS(MGraph* m);
  31. void visit(int);

  32. int main()
  33. {
  34.         int start;
  35.         MGraph m;
  36.         printf("Please the vertex number: ");
  37.         scanf("%d", &number);
  38.         InitGraph(&m, number);
  39.         printf("Please input a starting vertex:");
  40.         scanf("%d", &start);
  41.         //DFS(&m, start);
  42.         BFS(&m);
  43.         printf("\n");
  44.         return 0;
  45. }

  46. void InitGraph(MGraph *m, int number) {
  47.         printf("please input the whole graph, 1(yes), 0(no)\n");
  48.         int isExist;
  49.         for (int i = 0; i < number; i++) {
  50.                 for (int j = 0; j < number; j++) {
  51.                         scanf("%d", &isExist);
  52.                         m->Edge[i][j] = isExist;
  53.                 }
  54.         }
  55. }

  56. void DFS(MGraph *m, int start) {
  57.         visit(start);
  58.         visited[start] = true;
  59.         for (int i = 0; i < number; i++) {
  60.                 if (!visited[i] && m->Edge[start][i]) {
  61.                         DFS(m, i);
  62.                 }
  63.         }
  64. }
  65. void BFS(MGraph* m) {
  66.         int vertex;
  67.         QueuePtr *q = (QueuePtr *)malloc(sizeof(QueuePtr));
  68.         InitQueue(q);
  69.         for (int i = 0; i < number; i++) {
  70.                 if (!visited[i]) {
  71.                         visit(i);
  72.                         visited[i] = true;
  73.                         InsertQueue(q, i);
  74.                         while (!IsEmpty(q)) {
  75.                                 DeleteQueue(q, &vertex);
  76.                                 for (int j = 0; j < number;j++) {
  77.                                         if (!visited[j] && m->Edge[vertex][j]) {
  78.                                                 visit(j);
  79.                                                 visited[j] = true;
  80.                                                 InsertQueue(q, j);
  81.                                         }
  82.                                 }
  83.                         }
  84.                 }
  85.         }
  86. }
  87. void visit(int vertex) {
  88.         printf("%d ", vertex);
  89. }
  90. void InitQueue(QueuePtr *q) {
  91.         (*q) = (QueuePtr)malloc(sizeof(Queue));
  92.         (*q)->front = (*q)->rear = (QNodePtr)malloc(sizeof(QNode)); //头结点
  93.         (*q)->front->next = NULL;
  94. }
  95. void InsertQueue(QueuePtr *q, ElemType c) {
  96.         QNodePtr qNode = (QNodePtr)malloc(sizeof(QNode));
  97.         qNode->data = c;
  98.         qNode->next = NULL;
  99.         (*q)->rear->next = qNode;
  100.         (*q)->rear = qNode;
  101. }
  102. void DeleteQueue(QueuePtr *q, ElemType *c) {
  103.         if ((*q)->front == (*q)->rear) {
  104.                 printf("The Queue is empty\n");
  105.                 return;
  106.         }
  107.         QNodePtr headNode = (*q)->front;
  108.         QNodePtr firstNode = headNode->next;
  109.         *c = firstNode->data;
  110.         headNode->next = firstNode->next;
  111.         if ((*q)->rear == firstNode) {
  112.             (*q)->rear = (*q)->front;
  113.         }
  114.         free(firstNode);
  115. }
  116. bool IsEmpty(QueuePtr *q) {
  117.         if ((*q)->front == (*q)->rear) {
  118.                 return true;
  119.         } else {
  120.                 return false;
  121.         }
  122. }
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 23:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表