图的遍历(深度优先搜索法和广度优先搜索法)
生活随笔
收集整理的這篇文章主要介紹了
图的遍历(深度优先搜索法和广度优先搜索法)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
?
?
?
深度搜索? // ----------------------------------------------------------------// 圖的深度優先搜索法
// ----------------------------------------------------------------
#include " iostream "
#include " stdlib.h "
using namespace std;
struct node // 圖頂點結構聲明
{
int vertex; // 頂點數據
struct node * nextnode; // 指下一頂點的指針
};
typedef struct node * graph; // 圖的結構新類型
struct node head[ 9 ]; // 圖的頂點結構數組
int visited[ 9 ]; // 頂點記錄數組
// -----------------------------------------------------------------
// 創建圖
// -----------------------------------------------------------------
void creategraph( int * node, int num)
{
graph newnode; // 新頂點指針
graph ptr;
int from; // 邊的起點
int to; // 邊的終點
int i;
for (i = 0 ;i < num;i ++ ) // 讀取邊的循環
{
from = node[i * 2 ]; // 邊的起點
to = node[i * 2 + 1 ]; // 邊的終點
// -----------------創建新頂點內存------------------------------
newnode = (graph) malloc( sizeof ( struct node));
newnode -> vertex = to; // 創建頂點內容
newnode -> nextnode = NULL; // 設置指針初值
ptr =& (head[from]); // 頂點位置
while (ptr -> nextnode != NULL) // 遍歷至鏈表尾
ptr = ptr -> nextnode; // 下一個頂點
ptr -> nextnode = newnode; // 插入結尾
}
}
// ------------------------------------------------------------------------
// 圖的深度優先搜索
// ------------------------------------------------------------------------
void dfs( int current)
{
graph ptr;
visited[current] = 1 ; // 記錄已遍歷過
printf( " 頂點[%d] " ,current); // 輸出遍歷頂點值
ptr = head[current].nextnode; // 頂點位置
while (ptr != NULL) // 遍歷至鏈表尾
{
if (visited[ptr -> vertex] == 0 ) // 如果沒遍歷過
dfs(ptr -> vertex); // 遞歸遍歷調用
ptr = ptr -> nextnode; // 下一個頂點
}
}
// ----------------將遍歷內容輸出------------------------
int main()
{
graph ptr;
int node[ 20 ][ 2 ] = { // 邊數組
{ 1 , 2 },{ 2 , 1 },
{ 1 , 3 },{ 3 , 1 },
{ 2 , 4 },{ 4 , 2 },
{ 2 , 5 },{ 5 , 2 },
{ 3 , 6 },{ 6 , 3 },
{ 3 , 7 },{ 7 , 3 },
{ 4 , 8 },{ 8 , 4 },
{ 5 , 8 },{ 8 , 5 },
{ 6 , 8 },{ 8 , 6 },
{ 7 , 8 },{ 8 , 7 } };
int i;
for (i = 1 ;i <= 8 ;i ++ )
{
head[i].vertex = i; // 設置頂點值
head[i].nextnode = NULL; // 清除圖指針
visited[i] = 0 ; // 設置遍歷初值
}
creategraph( * node, 20 ); // 創建圖
printf( " 圖的鄰接表內容:\n " );
for (i = 1 ;i <= 8 ;i ++ )
{
printf( " 頂點%d=> " ,head[i].vertex); // 頂點值
ptr = head[i].nextnode; // 頂點位置
while (ptr != NULL) // 遍歷至鏈表尾
{
printf( " %d " ,ptr -> vertex); // 輸出頂點內容
ptr = ptr -> nextnode; // 下一個頂點
}
printf( " \n " );
}
printf( " 圖的深度優先遍歷內容: \n " );
dfs( 1 );
printf( " \n " );
}
?
廣度搜索? // ----------------------圖的廣度優先搜索------------------------#include " iostream "
#include " stdlib.h "
#define MAXQUEUE 10 // 隊列的最大容量
using namespace std;
struct node // 圖頂點結構聲明
{
int vertex; // 頂點數據
struct node * nextnode; // 指向下一頂點的指針
};
typedef struct node * graph; // 圖的結構新類型
struct node head[ 9 ]; // 圖的頂點結構數組
int visited[ 9 ]; // 遍歷記錄數組
int queue[MAXQUEUE]; // 隊列數組聲明
int front =- 1 ; // 隊列的對頭
int rear =- 1 ; // 隊列的隊尾
// ---------------------創建圖-----------------------
void creategraph( int * node, int num)
{
graph newnode; // 新頂點指針
graph ptr;
int from; // 邊的起點
int to; // 邊的終點
int i;
for (i = 0 ;i < num;i ++ ) // 讀取邊的循環
{
from = node[i * 2 ]; // 邊的起點
to = node[i * 2 + 1 ]; // 邊的終點
// -----------創建新頂點內存---------------------
newnode = ( graph ) malloc( sizeof ( struct node));
newnode -> vertex = to; // 創建頂點內容
newnode -> nextnode = NULL; // 設置指針初值
ptr =& (head[from]); // 頂點位置
while (ptr -> nextnode != NULL) // 遍歷至鏈表尾
ptr = ptr -> nextnode; // 下一個頂點
ptr -> nextnode = newnode; // 插入結尾
}
}
// -----------------------隊列的數據存入-------------------------
int enqueue( int value)
{
if (rear >= MAXQUEUE) // 檢查隊列是否全滿
return - 1 ; // 無法存入
rear ++ ; // 隊尾指針往前移
queue[rear] = value; // 存入隊列
}
// -----------------------隊列數據的取出-----------------------
int dequeue()
{
if (front == rear) // 檢查隊列是否為空
return - 1 ; // 無法取出
front ++ ; // 對頭指針往前移
return queue[front]; // 隊列取出
}
// -------------------------圖的廣度優先搜索法--------------------------------
void bfs( int current)
{
graph ptr;
// 處理第一個頂點
enqueue(current); // 將頂點存入隊列
visited[current] = 1 ; // 記錄已遍歷過
printf( " 頂點[%d] " ,current); // 輸出遍歷頂點值
while (front != rear ) // 隊列是否為空
{
current = dequeue(); // 將頂點從隊列中取出
ptr = head[current].nextnode; // 頂點位置
while (ptr != NULL) // 遍歷至鏈表尾
{
if (visited[ptr -> vertex] == 0 ) // 如果沒有遍歷過
{
enqueue(ptr -> vertex); // 遞歸遍歷調用
visited[ptr -> vertex] = 1 ; // 記錄已遍歷過
printf( " 頂點[%d] " ,ptr -> vertex);
}
ptr = ptr -> nextnode; // 下一個頂點
}
}
}
// -----------------將遍歷內容輸出-----------------------
int main()
{
graph ptr;
int node[ 20 ][ 2 ] = { // 邊數組
{ 1 , 2 },{ 2 , 1 },
{ 1 , 3 },{ 3 , 1 },
{ 2 , 4 },{ 4 , 2 },
{ 2 , 5 },{ 5 , 2 },
{ 3 , 6 },{ 6 , 3 },
{ 3 , 7 },{ 7 , 3 },
{ 4 , 8 },{ 8 , 4 },
{ 5 , 8 },{ 8 , 5 },
{ 6 , 8 },{ 8 , 6 },
{ 7 , 8 },{ 8 , 7 } };
int i;
for (i = 1 ;i <= 8 ;i ++ )
{
head[i].vertex = i; // 設置頂點值
head[i].nextnode = NULL; // 清除圖指針
visited[i] = 0 ; // 設置遍歷初值
}
creategraph( * node, 20 ); // 創建圖
printf( " 圖的鄰接表內容:\n " );
for (i = 1 ;i <= 8 ;i ++ )
{
printf( " 頂點%d => " ,head[i].vertex); // 頂點值
ptr = head[i].nextnode; // 頂點位置
while (ptr != NULL) // 遍歷至鏈表尾
{
printf( " %d " ,ptr -> vertex); // 輸出頂點內容
ptr = ptr -> nextnode; // 下一個頂點
}
printf( " \n " );
}
printf( " 圖的廣度優先遍歷內容: \n " );
bfs( 1 );
printf( " \n " );
}
轉載于:https://my.oschina.net/garyun/blog/602961
總結
以上是生活随笔為你收集整理的图的遍历(深度优先搜索法和广度优先搜索法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis的学习总结:调用存储过程【
- 下一篇: 在Ubuntu 16.04.3 LTS