zoukankan      html  css  js  c++  java
  • 邻接表DFS&&BFS

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #define MaxVertexNodeNumSize 1000
      6 #define MaxVertexNodeNameSize 100
      7 
      8 struct VertexBodyNode
      9 {
     10     char VertexName[MaxVertexNodeNameSize];
     11     int ArcWeight;
     12     int VertexIndex;
     13     struct VertexBodyNode *Next;
     14 };
     15 
     16 struct VertexHeadNode
     17 {
     18     char VertexName[MaxVertexNodeNameSize];
     19     int VertexWeight;
     20     struct VertexBodyNode *Next;
     21 };
     22 
     23 struct _Graph
     24 {
     25     struct VertexHeadNode VertexHeadNodeList[MaxVertexNodeNumSize];
     26     int ArcNum,VertexNum;
     27 };
     28 
     29 //Real capacity is CircularQueueMaxSize -1
     30 #define CircularQueueMaxSize 1000
     31 
     32 typedef int ElementType;
     33 
     34 struct CircularQueue
     35 {
     36     ElementType QueueData[CircularQueueMaxSize];
     37     int Front;
     38     int Rear;
     39 };
     40 
     41 int CircularQueueIsEmpty(struct CircularQueue *Queue)
     42 {
     43     return (Queue -> Front == Queue -> Rear);
     44 }
     45 
     46 int CircularQueueIsFull(struct CircularQueue *Queue)
     47 {
     48     return ((Queue -> Rear + 1) % CircularQueueMaxSize == Queue -> Front);
     49 }
     50 
     51 struct CircularQueue *CircularQueueInit()
     52 {
     53     struct CircularQueue *Queue;
     54     Queue = malloc(sizeof(struct CircularQueue));
     55     
     56     Queue -> Front = Queue -> Rear = 0;
     57     
     58     return Queue;
     59 }
     60 
     61 //if Queue is full,return 1 
     62 int CircularQueueEnqueue(struct CircularQueue *Queue,ElementType ToBeEnqueue)
     63 {
     64     if(CircularQueueIsFull(Queue))
     65     {
     66         return 1;
     67     }
     68     else
     69     {
     70         Queue -> Rear = (Queue -> Rear + 1) % CircularQueueMaxSize;
     71         Queue -> QueueData[Queue -> Rear] = ToBeEnqueue;
     72     }
     73     return 0;
     74 }
     75 
     76 //if Queue is empty,return 1 
     77 ElementType CircularQueueTop(struct CircularQueue *Queue)
     78 {
     79     if(CircularQueueIsEmpty(Queue))
     80     {
     81         return ;
     82     }
     83     else
     84     {
     85         return Queue -> QueueData[(Queue -> Front + 1) % CircularQueueMaxSize];
     86     }
     87     return ;
     88 }
     89 
     90 //if Queue is empty,return 1 
     91 int CircularQueueDequeue(struct CircularQueue *Queue)
     92 {
     93     if(CircularQueueIsEmpty(Queue))
     94     {
     95         return 1;
     96     }
     97     else
     98     {
     99         Queue -> Front = (Queue -> Front + 1) % CircularQueueMaxSize;
    100         return 0;
    101     }
    102 }
    103 
    104 int MakeCircularQueueEmpty(struct CircularQueue *Queue)
    105 {
    106     Queue -> Front = Queue -> Rear = 0;
    107     
    108     return 0;
    109 }
    110 
    111 int CircularQueueDelete(struct CircularQueue *Queue)
    112 {
    113     free(Queue);
    114     Queue = NULL;
    115     return 0;
    116 }
    117 
    118 int VertexName2Index(struct _Graph *UnsignedGraph,char *VName)
    119 {
    120     int i;
    121     for(i = 0; i < UnsignedGraph -> VertexNum; i ++)
    122     {
    123         if(strcmp(UnsignedGraph -> VertexHeadNodeList[i].VertexName,VName)==0)
    124         {
    125             return i;
    126         }
    127     }
    128     return -1;
    129 }
    130 
    131 void AddOneArc(struct _Graph *UnsignedGraph,int ArcIndex_1,int ArcIndex_2,int AWeight)
    132 {
    133     struct VertexBodyNode *BNode_1 = malloc(sizeof(struct VertexBodyNode));
    134     struct VertexBodyNode *BNode_2 = malloc(sizeof(struct VertexBodyNode));
    135 
    136     strcpy(BNode_1 -> VertexName,UnsignedGraph -> VertexHeadNodeList[ArcIndex_1].VertexName);
    137     strcpy(BNode_2 -> VertexName,UnsignedGraph -> VertexHeadNodeList[ArcIndex_2].VertexName);
    138     BNode_1 -> ArcWeight = AWeight;
    139     BNode_2 -> ArcWeight = AWeight;
    140     BNode_1 -> VertexIndex = ArcIndex_1;
    141     BNode_2 -> VertexIndex = ArcIndex_2;
    142     BNode_1 -> Next = BNode_2 -> Next = NULL;
    143 
    144     struct VertexBodyNode *TmpPointer;
    145     TmpPointer = UnsignedGraph -> VertexHeadNodeList[ArcIndex_1].Next;
    146     while(TmpPointer != NULL && TmpPointer -> Next != NULL)
    147     {
    148         TmpPointer = TmpPointer -> Next;
    149     }
    150     if(TmpPointer==NULL)
    151     {
    152         UnsignedGraph -> VertexHeadNodeList[ArcIndex_1].Next = BNode_2;
    153     }
    154     else
    155     {
    156         TmpPointer -> Next = BNode_2;
    157     }
    158 
    159     TmpPointer = UnsignedGraph -> VertexHeadNodeList[ArcIndex_2].Next;
    160     while(TmpPointer != NULL && TmpPointer -> Next != NULL)
    161     {
    162         TmpPointer = TmpPointer -> Next;
    163     }
    164     if(TmpPointer==NULL)
    165     {
    166         UnsignedGraph -> VertexHeadNodeList[ArcIndex_2].Next = BNode_1;
    167     }
    168     else
    169     {
    170         TmpPointer -> Next = BNode_1;
    171     }
    172 }
    173 
    174 struct _Graph *UGCreat(int ArcSum,int VertexSum)
    175 {
    176     int i,j;
    177     struct _Graph *UnsignedGraph = malloc(sizeof(struct _Graph));
    178     UnsignedGraph -> ArcNum = ArcSum;
    179     UnsignedGraph -> VertexNum = VertexSum;
    180 
    181     for(i = 0; i < VertexSum; i ++)
    182     {
    183         scanf("%s %d",UnsignedGraph -> VertexHeadNodeList[i].VertexName,&UnsignedGraph -> VertexHeadNodeList[i].VertexWeight);
    184     }
    185 
    186     for(i = 0; i < VertexSum; i ++)
    187     {
    188         UnsignedGraph -> VertexHeadNodeList[i].Next = NULL;
    189     }
    190 
    191     for(i = 0; i < ArcSum; i ++)
    192     {
    193         char Arc_1[MaxVertexNodeNameSize];
    194         char Arc_2[MaxVertexNodeNameSize];
    195         int ArcIndex_1;
    196         int ArcIndex_2;
    197         int ArcWeight;
    198 
    199         scanf("%s %s %d",Arc_1,Arc_2,&ArcWeight);
    200 
    201         ArcIndex_1 = VertexName2Index(UnsignedGraph,Arc_1);
    202         ArcIndex_2 = VertexName2Index(UnsignedGraph,Arc_2);
    203 
    204         AddOneArc(UnsignedGraph,ArcIndex_1,ArcIndex_2,ArcWeight);
    205     }
    206     return UnsignedGraph;
    207 }
    208 
    209 void Travel(struct _Graph *UnsignedGraph)
    210 {
    211     char StartingPoint[MaxVertexNodeNameSize];
    212     char OverPoint[MaxVertexNodeNameSize];
    213 
    214     printf("Input start and over
    ");
    215     scanf("%s %s",StartingPoint,OverPoint);
    216 
    217     int StartIndex = VertexName2Index(UnsignedGraph,StartingPoint);
    218     int OverIndex = VertexName2Index(UnsignedGraph,OverPoint);
    219 
    220     struct VertexBodyNode *TmpPointer;
    221     TmpPointer = UnsignedGraph -> VertexHeadNodeList[StartIndex].Next;
    222     while(TmpPointer != NULL && TmpPointer -> Next != NULL)
    223     {
    224         if(OverIndex==TmpPointer -> VertexIndex)
    225         {
    226             printf("Distance:%d GetVertexPointSum:%d",TmpPointer->ArcWeight
    227                    ,UnsignedGraph -> VertexHeadNodeList[StartIndex].VertexWeight+UnsignedGraph -> VertexHeadNodeList[OverIndex].VertexWeight);
    228             break;
    229         }
    230         else
    231         {
    232             TmpPointer = TmpPointer -> Next;
    233         }
    234     }
    235 }
    236 
    237 void _UGDFS(struct _Graph *UnsignedGraph,int *Visit,int Index)
    238 {
    239     Visit[Index] = 1;
    240 //    printf("%s %d    ",UnsignedGraph -> VertexHeadNodeList[Index].VertexName,UnsignedGraph -> VertexHeadNodeList[Index].VertexWeight);
    241     
    242     struct VertexBodyNode *TmpPointer = UnsignedGraph -> VertexHeadNodeList[Index].Next;
    243     while(TmpPointer)
    244     {
    245         if(!Visit[TmpPointer->VertexIndex])
    246         {
    247             printf("%s->%s
    ",UnsignedGraph -> VertexHeadNodeList[Index].VertexName,UnsignedGraph -> VertexHeadNodeList[TmpPointer->VertexIndex].VertexName);
    248             _UGDFS(UnsignedGraph,Visit,TmpPointer->VertexIndex);
    249         }
    250         TmpPointer = TmpPointer -> Next;
    251     }
    252 }
    253 
    254 void UnsignedGraphDFS(struct _Graph *UnsignedGraph)
    255 {
    256     int Visit[UnsignedGraph->VertexNum];
    257     memset(Visit,0,sizeof(Visit));
    258 
    259     int i;
    260     for(i = 0;i < UnsignedGraph->VertexNum;i ++)
    261     {
    262         if(!Visit[i])
    263         {
    264             _UGDFS(UnsignedGraph,Visit,i);
    265         }
    266     }
    267 }
    268 
    269 void UnsignedGraphBFS(struct _Graph *UnsignedGraph)
    270 {
    271     int Visit[UnsignedGraph->VertexNum];
    272     memset(Visit,0,sizeof(Visit));
    273     
    274     struct CircularQueue *Queue;
    275     Queue = CircularQueueInit();
    276     
    277     int i;
    278     for(i = 0;i < UnsignedGraph->VertexNum;i ++)
    279     {
    280         if(!Visit[i])
    281         {
    282             Visit[i] = 1;
    283             //printf("%s %d    ",UnsignedGraph -> VertexHeadNodeList[i].VertexName,UnsignedGraph -> VertexHeadNodeList[i].VertexWeight);
    284             CircularQueueEnqueue(Queue,i);
    285             
    286             while(!CircularQueueIsEmpty(Queue))
    287             {
    288                 int TmpIndex = CircularQueueTop(Queue);
    289                 CircularQueueDequeue(Queue);
    290                 struct VertexBodyNode *TmpPointer = UnsignedGraph -> VertexHeadNodeList[TmpIndex].Next;
    291                 
    292                 while(TmpPointer)
    293                 {
    294                     if(!Visit[TmpPointer->VertexIndex])
    295                     {
    296                         Visit[TmpPointer->VertexIndex] = 1;
    297                     //    printf("%s %d    ",UnsignedGraph -> VertexHeadNodeList[TmpPointer->VertexIndex].VertexName,UnsignedGraph -> VertexHeadNodeList[TmpPointer->VertexIndex].VertexWeight);
    298                         printf("%s->%s
    ",UnsignedGraph -> VertexHeadNodeList[TmpIndex].VertexName,UnsignedGraph -> VertexHeadNodeList[TmpPointer->VertexIndex].VertexName);
    299                         CircularQueueEnqueue(Queue,TmpPointer->VertexIndex);
    300                     }
    301                     TmpPointer = TmpPointer -> Next;
    302                 }
    303             } 
    304         }
    305     }
    306 }
    307 
    308 int main()
    309 {
    310     struct _Graph *G = UGCreat(8,5);
    311 
    312     //Travel(G);
    313     printf("
    DFS:
    ");
    314     UnsignedGraphDFS(G);
    315     printf("
    BFS:
    ");
    316     UnsignedGraphBFS(G);
    317     return 0;
    318 }
    319 
    320 /*
    321         beijing 18
    322         zhengzhou 10
    323         hefei 9
    324         nanjing 12
    325         guangzhou 14
    326         beijing zhengzhou 7
    327         beijing hefei 9
    328         beijing nanjing 8
    329         zhengzhou hefei 5
    330         hefei nanjing 3
    331         zhengzhou guangzhou 7
    332         hefei guangzhou 8
    333         nanjing guangzhou 6
    334 */
  • 相关阅读:
    【解决方案】RTSP/Onvif安防可视化视频图像智能分析系统EasyNVR助力智慧公租房建设
    【解决方案】RTSP/Onvif网页无插件直播如何通过视频边缘计算网关+APP打造低成本场景视频监控
    文明旅游深入人心,如何通过视频监控系统进一步加强旅游景区管制?
    RTSP协议视频安防综合管理平台EasyNVR与海康萤石云平台运行机制差异对比说明
    视频结构化管理平台EasyNVR视频边缘计算网关和EasyNVR软件版本的异同对比
    视频安防智能监控系统管理平台EasyNVS为什么无法播放某套EasyNVR内通道的HLS视频流?
    RTSP视频安防智能监控系统EasyNVR配置完成后登录web页面不显示数据是什么问题?
    【解决方案】5G+视频智能分析打造工地智慧分析系统,提高建筑工地效率及安全
    视频智能安防监控系统EasyNVR是如何做到在平台内屏蔽部分菜单或者logo标识的?
    网页无插件智能安防直播平台EasyNVR中web页面以blob:http加载视频的探索
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9427517.html
Copyright © 2011-2022 走看看