Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1436244
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-02-20 20:33:23

广度优先算法(BFS)的要点:
(1)从一个图节点编号开始加入队列(创建一个队列,FIFO)
(2)只要队列不为空,则取出队列中一个图节点编号,将该节点编号所有还未遍历的节点之间的边。(创建一个已经遍历节点的数组)

    深度优先遍历的要点:
从第一个节点开始,遍历一个相邻的,然后把该节点作为起始点遍历未曾遍历的节点。
如何遍历所有边?

点击(此处)折叠或打开

  1. #define MAX 100
  2. typedef enum
  3. {
  4.     NoVisit=0,
  5.     HasVisit=1
  6. }VisitStat;

  7. typedef enum
  8. {
  9.     NoExisit = 0,
  10.     IsExisit = 1
  11. }ExistStat;

  12. typedef enum
  13. {
  14.     NoEmpty = 0,
  15.     IsEmpty = 1
  16. }EmptyStat;

  17. typedef struct ListNode
  18. {
  19.     int pointNo;
  20.     int wight;
  21.     struct ListNode* next;
  22. }ListNode;

  23. typedef struct GNODE
  24. {
  25.     int pointNo;
  26.     ListNode* listHead;
  27. }GNODE,GPoint[MAX];

  28. typedef struct Graph
  29. {
  30.     GPoint GListNode;
  31.     int points;
  32.     int edges;
  33. }GRAPH;

  34. void CreateG(GRAPH* grap)
  35. {
  36.     scanf("%d %d",&grap->points,&grap->edges);

  37.     int i;
  38.     ListNode* tmpHead;

  39.     for (i=0; i<grap->points;i++)
  40.     {
  41.         grap->GListNode[i].pointNo = i;
  42.         tmpHead = (ListNode*)malloc(sizeof(ListNode));
  43.         if (NULL != tmpHead)
  44.         {
  45.             tmpHead->next = NULL;
  46.             grap->GListNode[i].listHead = tmpHead;
  47.         }
  48.     }
  49.     
  50.     int start=0,end=0,weight=0;
  51.     ListNode* InsertNode;
  52.     ListNode* Head;
  53.     ListNode* Next;
  54.     for (i=0;i<grap->edges;i++)
  55.     {
  56.         scanf("%d %d %d",&start,&end,&weight);
  57.         
  58.         InsertNode = (ListNode*)malloc(sizeof(ListNode));
  59.         if (NULL == InsertNode)
  60.             return ;
  61.         InsertNode->pointNo = end;
  62.         InsertNode->wight = weight;
  63.         Head = grap->GListNode[start].listHead;
  64.         Next = Head->next;
  65.         while (Next != NULL)
  66.         {
  67.             Head = Next;
  68.             Next = Head->next;
  69.         }
  70.         InsertNode->next = Next;
  71.         Head->next = InsertNode;

  72.         
  73.         InsertNode = (ListNode*)malloc(sizeof(ListNode));
  74.         if (NULL == InsertNode)
  75.             return ;
  76.         InsertNode->pointNo = start;
  77.         InsertNode->wight = weight;
  78.         Head = grap->GListNode[end].listHead;
  79.         Next = Head->next;
  80.         while (Next != NULL)
  81.         {
  82.             Head = Next;
  83.             Next = Head->next;
  84.         }
  85.         InsertNode->next = Next;
  86.         Head->next = InsertNode;
  87.     }

  88. }
  89. typedef struct QueueNode
  90. {
  91.     int PonintNo;
  92.     struct QueueNode* next;
  93. }QueueNode;

  94. QueueNode* CreateQueue()
  95. {
  96.     QueueNode* head = (QueueNode*)malloc(sizeof(QueueNode));
  97.     if (head == NULL)
  98.     {
  99.         return NULL;
  100.     }
  101.     head->next = NULL;
  102. }

  103. EmptyStat EmptyQueue(QueueNode* queueHead)
  104. {
  105.     if (NULL == queueHead)
  106.         return IsEmpty;
  107.     QueueNode* Head = queueHead;
  108.     QueueNode* Next = Head->next;
  109.     if (NULL == Next)
  110.         return IsEmpty;
  111.     else
  112.         return NoEmpty;
  113. }

  114. void EnterQueue(QueueNode** queue,int pointNo)
  115. {
  116.     if (queue == NULL)
  117.         return;

  118.     QueueNode* Head = *queue;
  119.     QueueNode* Next = Head->next;
  120.     while(Next != NULL)
  121.     {
  122.         if (Next->PonintNo == pointNo)
  123.         {
  124.             return;
  125.         }
  126.         Head = Next;
  127.         Next = Head->next;
  128.     }
  129.     QueueNode* InsertNode = (QueueNode*)malloc(sizeof(QueueNode));
  130.     if (NULL == InsertNode)
  131.     {
  132.         return;
  133.     }
  134.     InsertNode->PonintNo = pointNo;
  135.     InsertNode->next = Next;
  136.     Head->next = InsertNode;
  137.     return;
  138. }
  139. QueueNode* OutQueue(QueueNode** queue)
  140. {
  141.     if (NULL == queue)
  142.         return NULL;
  143.     
  144.     QueueNode* Head = *queue;
  145.     QueueNode* Next = Head->next;
  146.     QueueNode* tmp;
  147.     if (NULL == Next)
  148.         return NULL;
  149.     tmp = Next;
  150.     Head->next = Next->next;
  151.     return tmp;
  152. }

  153. void BFS(GRAPH* grap)
  154. {
  155.     int VisitLog[MAX];
  156.     int i;
  157.     for (i=0; i<grap->points; i++)
  158.     {
  159.         VisitLog[i] = NoVisit;
  160.     }

  161.     QueueNode* OutNode;
  162.     QueueNode* QueueHead = CreateQueue();
  163.     EnterQueue(&QueueHead,grap->GListNode[0].pointNo);
  164.     while(EmptyQueue(QueueHead) == NoEmpty)
  165.     {
  166.         OutNode = OutQueue(&QueueHead);
  167.         if (OutNode == NULL)
  168.         {
  169.             return;
  170.         }

  171.         VisitLog[OutNode->PonintNo] = HasVisit;
  172.         ListNode* ListHead = grap->GListNode[OutNode->PonintNo].listHead;
  173.         ListNode* ListNext = ListHead->next;
  174.         while (ListNext != NULL)
  175.         {
  176.             if (VisitLog[ListNext->pointNo] == NoVisit)
  177.             {
  178.                 printf("(%d,%d) = %d\n",OutNode->PonintNo,ListNext->pointNo,ListNext->wight);
  179.                 EnterQueue(&QueueHead,ListNext->pointNo);
  180.             }
  181.                         
  182.             ListHead = ListNext;
  183.             ListNext = ListHead->next;
  184.         }
  185.         free(OutNode);
  186.         OutNode = NULL;
  187.     }

  188. }

  189. void DFS_Trav(GRAPH* grap,int i,int* Visit)
  190. {
  191.     Visit[i] = HasVisit;
  192.     ListNode* ListNext = grap->GListNode[i].listHead->next;
  193.     while (NULL != ListNext)
  194.     {
  195.         if (Visit[ListNext->pointNo] == NoVisit)
  196.         {
  197.             printf("(%d,%d)=%d\n",i,ListNext->pointNo,ListNext->pointNo);
  198.             DFS_Trav(grap,ListNext->pointNo,Visit);
  199.         }
  200.         ListNext = ListNext->next;
  201.     }
  202. }
  203. void DFS(GRAPH* grap)
  204. {
  205.     int VisitLog[MAX];
  206.     int i;
  207.     for (i=0; i<grap->points; i++)
  208.     {
  209.         VisitLog[i] = NoVisit;
  210.     }

  211.     for (i=0; i<grap->points; i++)
  212.     {
  213.         if (VisitLog[i] == NoVisit)
  214.         {
  215.             DFS_Trav(grap,i,VisitLog);
  216.         }
  217.     }
  218. }


阅读(1338) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~