1. 程式人生 > >C/C++:各種基本演算法實現小結(四)—— 圖及其遍歷

C/C++:各種基本演算法實現小結(四)—— 圖及其遍歷

各種基本演算法實現小結(四)—— 圖及其遍歷

(均已測試通過)

====================================================================

圖——深度優先和廣度優先演算法

無向圖用二維鄰接矩陣表示

測試環境:VC 6.0 (C)

[cpp] view plain copy  print?
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #define INFINITY 32767
  5. #define MAX_VEX 20
  6. #define QUEUE_SIZE (MAX_VERTEX+1)
  7. #define DataType char  /* vertext's info  */
  8. int *visited; /* Node: visited flag with dynamic array, good idea ! */
  9. /* init queue for bfs */
  10. struct _node  
  11. {  
  12.     int v_num;  
  13.     struct _node *next;  
  14. };  
  15. typedefstruct _node node, *pnode;  
  16. struct _queue  
  17. {  
  18.     pnode front;  
  19.     pnode rear;  
  20. };  
  21. typedefstruct _queue queue, *pqueue;  
  22. struct _graph  
  23. {  
  24.     DataType *vexs;  
  25.     int arcs[MAX_VEX][MAX_VEX];  
  26.     int vexnum, arcnum;  
  27. };  
  28. typedefstruct _graph graph, *pgraph;  
  29. /* operation of queue */
  30. queue init_queue()  
  31. {  
  32.     queue qu;  
  33.     qu.front=qu.rear=(pnode)malloc(sizeof(node));  
  34.     if(qu.front == NULL)  
  35.         exit(1);  
  36.     qu.rear->next=NULL;  
  37.     return qu;  
  38. }  
  39. void en_queue(pqueue pqu, int v_num)  
  40. {  
  41.     pnode pn;  
  42.     pn=(pnode)malloc(sizeof(node));  
  43.     if(pqu->front == NULL)  
  44.         exit(1);  
  45.     pn->v_num=v_num;  
  46.     pn->next=NULL;  
  47.     pqu->rear->next=pn;  
  48.     pqu->rear=pqu->rear->next;  
  49. }  
  50. int isempty_queue(pqueue pqu)  
  51. {  
  52.     if(pqu->front == pqu->rear)  
  53.         return 1;  
  54.     else
  55.         return 0;  
  56. }  
  57. int de_queue(pqueue pqu)  
  58. {  
  59.     pnode pn;  
  60.     int d;  
  61.     if(isempty_queue(pqu))  
  62.         return -1;  
  63.     pn=pqu->front;  
  64.     d=pn->v_num;  
  65.     pqu->front=pn->next;  
  66.     free(pn);  
  67.     return d;  
  68. }  
  69. int locate(graph g, DataType data)  
  70. {  
  71.     int i;  
  72.     for(i=0;i<g.vexnum;i++)  
  73.         if(g.vexs[i] == data)  
  74.             return i;  
  75.      return -1;  
  76. }  
  77. graph create_graph()  
  78. {  
  79.     int i,j,w, s1,s2;  
  80.     DataType ch1,ch2,tmp;  
  81.     graph g;  
  82.     printf("g sizeof: %d/n"sizeof(g));  
  83.     printf("Enter vexnum arcnum:");  
  84.     scanf("%d %d", &g.vexnum, &g.arcnum);  
  85.     tmp=getchar();  
  86.     g.vexs=(DataType *)malloc(sizeof(DataType));  
  87.     if(g.vexs == NULL)  
  88.         exit(1);  
  89.     printf("Enter %d vertext,please.../n", g.vexnum);  
  90.     for(i=0;i<g.vexnum;i++)  
  91.     {  
  92.         printf("vex %d: ", i);  
  93.         scanf("%c", &g.vexs[i]);  
  94.         tmp=getchar();  
  95.         //visited[i]=0;
  96.     }  
  97.     for(i=0;i<g.vexnum;i++)  
  98.         for(j=0;j<g.vexnum;j++)  
  99.             g.arcs[i][j]=INFINITY;  
  100.      printf("Enter %d arcs:/n", g.arcnum);  
  101.      for(i=0;i<g.arcnum;i++)  
  102.      {  
  103.         printf("arc %d: ", i);  
  104.         scanf("%c %c %d", &ch1, &ch2, &w);  
  105.         tmp=getchar();  
  106.         s1=locate(g, ch1);  
  107.         s2=locate(g, ch2);  
  108.         g.arcs[s1][s2]=g.arcs[s2][s1]=w; /* NOTE: weight */
  109.      }  
  110.      return g;  
  111. }  
  112. int firstvex_graph(graph g, int k)  
  113. {  
  114.     int i;  
  115.     if(k>=0 && k<g.vexnum)  
  116.         for(i=0;i<g.vexnum;i++)  
  117. 相關推薦

    C/C++各種基本演算法實現小結—— 及其

    各種基本演算法實現小結(四)—— 圖及其遍歷 (均已測試通過) ==================================================================== 圖——深度優先和廣度優先演算法 無向圖

    各種基本算法實現小結—— 及其

    malloc end type details 幽默 can false 希望 頂點 分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!http://www.captainbed.net 各種基本算法實現

    C/C++各種基本演算法實現小結—— 查詢演算法

    各種基本演算法實現小結(六)—— 查詢演算法 (均已測試通過) =================================================================== 1、簡單查詢 在一組無序數列中,查詢特定某個數值,並返回其位置

    C/C++各種基本演算法實現小結—— 常用演算法

    各種基本演算法實現小結(七)—— 常用演算法 (均已測試通過) ====================================================================== 1、判斷素數 測試環境:VC 6.0

    C/C++各種基本演算法實現小結—— 堆 棧

    各種基本演算法實現小結(二)—— 堆 棧 (均已測試通過) ============================================================== 棧——陣列實現 測試環境:Win - TC

    C/C++各種基本演算法實現小結—— 樹與二叉樹

    各種基本演算法實現小結(三)—— 樹與二叉樹 (均已測試通過) =================================================================== 二叉樹——先序 測試環境:VC 6.0 (C

    C/C++:各種基本演算法實現小結—— 單鏈表

    各種基本演算法實現小結(一)—— 單鏈表 (均已測試通過) ============================================================ 單鏈表(測試通過) 測試環境: Win-TC

    各種基本演算法實現小結—— 排序演算法

    分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

    ——基本演算法

    圖——基本的圖演算法(二)圖的遍歷 1. 基本概念 圖的遍歷指的是從圖中的某個頂點出發訪問圖中其餘的頂點,且每個頂點只被訪問一次的這個過程。通常來說,圖的遍歷次序有兩種:深度優先遍歷(Depth first Search, DFS)和廣度優先遍歷(Breadth First Se

    C++ 靜態連結串列基本演算法實現

    C++ 靜態連結串列基本演算法實現 #ifndef StaticLinkList_h #define StaticLinkList_h const int MAXSIZE = 100; template <class T> struct StaticNode{ T data;

    C++優先順序隊列表基本演算法實現

    C++優先順序隊列表基本演算法實現 主要採用鏈式結構,進行資料儲存,然後定義一個最後結點指標陣列,將所有優先順序最後一個元素的地址儲存到這個指標陣列中。 #ifndef PriorityQueue_h #define PriorityQueue_h #include <iostream>

    各種基本演算法實現總結

    各種基本演算法實現小結(七)—— 常用演算法 (均已測試通過) ====================================================================== 1、判斷素數 測試環境:VC 6.0 (C)

    C++繼承公有,私有,保護

    情況 pre mes 派生類 它的 保持 ++ col ble 公有繼承(public)、私有繼承(private)、保護繼承(protected)是常用的三種繼承方式。 1. 公有繼承(public) 公有繼承的特點是基類的公有成員和保護成員作為派生類的成員時,它們都保持

    C#中委託與事件的學習小結

    最近又學習了一些C#的小知識點,在此釋出部落格記錄一下。 一、委託 C#中的委託的關鍵字是delegate,我們可以使用委託型別來將已有的方法例項化出來,也可以將我們自己定義的方法作為引數來傳遞。 例如: private delegate string GetAStri

    C語言_位運算子的實現舉例

    1. 返回引數二進位制中 1 的個數。 比如: 15 0000 1111 4 個 1 程式碼如下: //返回引數二進位制中1的個數 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include

    用PHP實現一個關於德州撲克演算法的程式程式碼

    程式主入口 <?php include "Table.class.php"; //顯示函式 function pre($str){ echo '<pre>'; print_r($str); echo '</pre

    Spring技術內幕Spring AOP的實現原理

    dede ide configure ida mini == src min dem 生成SingleTon代理對象在getSingleTonInstance方法中完畢,這種方法時ProxyFactoryBean生成AopProxy對象的入口。代理對象會

    OpenCV下車牌定位演算法實現程式碼

                 分類:            影象處理

    演算法工程師修仙之路吳恩達機器學習

    吳恩達機器學習筆記及作業程式碼實現中文版 第四章 Logistic迴歸 分類 在分類問題中,要預測的變數y是離散的值,邏輯迴歸 (Logistic Regression) 演算法是目前最流行使用最廣泛的一種學習演算法。 在分類問題中,我們嘗試預測的是結果

    演算法的實戰LeetCode -- Palindrome Number

    一 題目描述 判斷一個整數是否是迴文數。迴文數是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數。 示例 1: 輸入: 121 輸出: true 示例 2: 輸入: -121 輸出: false 解釋: 從左向右讀, 為 -121 。 從右向左讀, 為 121