C/C++:各種基本演算法實現小結(四)—— 圖及其遍歷
各種基本演算法實現小結(四)—— 圖及其遍歷
(均已測試通過)
====================================================================
圖——深度優先和廣度優先演算法
無向圖用二維鄰接矩陣表示
測試環境:VC 6.0 (C)
[cpp] view plain copy print?- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
-
#define INFINITY 32767
- #define MAX_VEX 20
- #define QUEUE_SIZE (MAX_VERTEX+1)
- #define DataType char /* vertext's info */
- int *visited; /* Node: visited flag with dynamic array, good idea ! */
- /* init queue for bfs */
- struct _node
- {
- int v_num;
- struct _node *next;
- };
-
typedefstruct _node node, *pnode;
- struct _queue
- {
- pnode front;
- pnode rear;
- };
- typedefstruct _queue queue, *pqueue;
- struct _graph
- {
- DataType *vexs;
- int arcs[MAX_VEX][MAX_VEX];
- int vexnum, arcnum;
- };
- typedefstruct _graph graph, *pgraph;
- /* operation of queue */
-
queue init_queue()
- {
- queue qu;
- qu.front=qu.rear=(pnode)malloc(sizeof(node));
- if(qu.front == NULL)
- exit(1);
- qu.rear->next=NULL;
- return qu;
- }
- void en_queue(pqueue pqu, int v_num)
- {
- pnode pn;
- pn=(pnode)malloc(sizeof(node));
- if(pqu->front == NULL)
- exit(1);
- pn->v_num=v_num;
- pn->next=NULL;
- pqu->rear->next=pn;
- pqu->rear=pqu->rear->next;
- }
- int isempty_queue(pqueue pqu)
- {
- if(pqu->front == pqu->rear)
- return 1;
- else
- return 0;
- }
- int de_queue(pqueue pqu)
- {
- pnode pn;
- int d;
- if(isempty_queue(pqu))
- return -1;
- pn=pqu->front;
- d=pn->v_num;
- pqu->front=pn->next;
- free(pn);
- return d;
- }
- int locate(graph g, DataType data)
- {
- int i;
- for(i=0;i<g.vexnum;i++)
- if(g.vexs[i] == data)
- return i;
- return -1;
- }
- graph create_graph()
- {
- int i,j,w, s1,s2;
- DataType ch1,ch2,tmp;
- graph g;
- printf("g sizeof: %d/n", sizeof(g));
- printf("Enter vexnum arcnum:");
- scanf("%d %d", &g.vexnum, &g.arcnum);
- tmp=getchar();
- g.vexs=(DataType *)malloc(sizeof(DataType));
- if(g.vexs == NULL)
- exit(1);
- printf("Enter %d vertext,please.../n", g.vexnum);
- for(i=0;i<g.vexnum;i++)
- {
- printf("vex %d: ", i);
- scanf("%c", &g.vexs[i]);
- tmp=getchar();
- //visited[i]=0;
- }
- for(i=0;i<g.vexnum;i++)
- for(j=0;j<g.vexnum;j++)
- g.arcs[i][j]=INFINITY;
- printf("Enter %d arcs:/n", g.arcnum);
- for(i=0;i<g.arcnum;i++)
- {
- printf("arc %d: ", i);
- scanf("%c %c %d", &ch1, &ch2, &w);
- tmp=getchar();
- s1=locate(g, ch1);
- s2=locate(g, ch2);
- g.arcs[s1][s2]=g.arcs[s2][s1]=w; /* NOTE: weight */
- }
- return g;
- }
- int firstvex_graph(graph g, int k)
- {
- int i;
- if(k>=0 && k<g.vexnum)
- for(i=0;i<g.vexnum;i++)
-
相關推薦
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