圖的遍歷c語言
㈠ 基於鄰接矩陣的圖的遍歷(數據結構c語言版)
已經幫你在那個帖子上回答了,望加分!
圖的遍歷是指按某條搜索路徑訪問圖中每個結點,使得每個結點均被訪問一次,而且僅被訪問一次。圖的遍歷有深度遍歷演算法和廣度遍歷演算法,最近阿傑做了關於圖的遍歷的演算法,下面是圖的遍歷深度優先的演算法(C語言程序):
#include<stdio.h>
#include<malloc.h>
#define MaxVertexNum 5
#define m 5
#define TRUE 1
#define NULL 0
typedef struct node
{
int adjvex;
struct node *next;
}JD;
typedef struct EdgeNode
{
int vexdata;
JD *firstarc;
}TD;
typedef struct
{
TD ag[m];
int n;
}ALGRAPH;
void DFS(ALGRAPH *G,int i)
{
JD *p;
int visited[80];
printf("visit vertex:%d->",G->ag[i].vexdata);
visited[i]=1;
p=G->ag[i].firstarc;
while(p)
{
if (!visited[p->adjvex])
DFS(G,p->adjvex);
p=p->next;
}
}
void creat(ALGRAPH *G)
{
int i,m1,j;
JD *p,*p1;
printf("please input the number of graph\n");
scanf("%d",&G->n);
for(i=0;i<G->n;i++)
{
printf("please input the info of node %d",i);
scanf("%d",&G->ag[i].vexdata);
printf("please input the number of arcs which adj to %d",i);
scanf("%d",&m1);
printf("please input the adjvex position of the first arc\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",&p->adjvex);
p->next=NULL;
G->ag[i].firstarc=p;
p1=p;
for(j=2 ;j<=m1;j++)
{
printf("please input the position of the next arc vexdata\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",&p->adjvex);
p->next=NULL;
p1->next=p;
p1=p;
}
}
}
int visited[MaxVertexNum];
void DFSTraverse(ALGRAPH *G)
{
int i;
for(i=0;i<G->n;i++)
visited[i]=0;
for(i=0;i<G->n;i++)
if(!visited[i])
DFS(G,i);
}
int main()
{
ALGRAPH *G;
printf("下面以臨接表存儲一個圖;\n");
creat(G);
printf("下面以深度優先遍歷該圖 \n");
DFSTraverse(G);
getchar();
}
㈢ C語言的遍歷演算法
思路1:
寫出所有24種4個數的排列,存到一個數組里,假如數組是P[24][4];
那麼可以
for
(i
=
0;
i
<
24;
i++)
for
(j
=
0;
j
<
24;
j++)
for
(k
=
0;
k
<
24;
k++)
三層循環,P[i],P[j],P[k]分別是矩陣的三個列
思路2:
利用dfs遞歸枚舉
int
used[3][4];/*這個數組存放三個列中0~3這四個數是否已在這一列中出現過,需要提前清零*/
int
mat[3][4];/*要枚舉的矩陣*/
void
dfs(int
col,
int
row)/*col表示現在已經搜索到哪一列(從0開始編號),row表示這一列已經填了幾行*/
{
int
i;
if
(col
==
2
&&
row
==
4)
{
....../*運行到這里的時候,mat就是枚舉到的一個矩陣*/
return;
}
if
(row
==
4)
{row
=
0;
col++;}
for
(i
=
0;
i
<
4;
i++)
if
(!used[col][i])
{
used[col][i]
=
1;
mat[col][row]
=
i;
dfs(col,
row
+
1);
used[col][i]
=
0;
}
return;
}
調用的時候調用dfs(0,0)
㈣ c語言 圖的遍歷
你的演算法好像是對的!我試驗了一下:
測試數據:5,16 頂點數 邊數
0 1 2 3 4 邊
對應的邊為:
0,3
0,2
0,1
1,4
1,3
1,0
2,4
2,3
2,2
2,0
3,4
3,2
3,1
4,3
4,2
4,1
0-1-2-3
1-0-3-4
2-0-1-3-4
3-1-2-4
4-1-2-3
結果為0 1 3 2 4 正確啊!
樓主把你的測試數據和結果貼出來,我看看!
看看是不是我的測試數據太少不全面!
㈤ C語言 圖的遍歷
思路:
以鄰接表或鄰接矩陣為存儲結構,實現連通無向圖的深度和廣度優先遍歷。以用戶指定的結點為起始點
,分別輸出每種遍歷下的結點訪問序列和相應的生成樹的邊集。
設圖的結點不超過30個,每個結點用一個編號表示。通過輸入圖的全部邊輸入一個圖,每個邊為一個數對
可以對邊的輸入順序作出某種限制。注意,生成樹和生成邊是有向邊,端點順序不能顛倒。
㈥ C語言編寫程序實現圖的遍歷操作
樓主你好,下面是源程序!
/*/////////////////////////////////////////////////////////////*/
/* 圖的深度優先遍歷 */
/*/////////////////////////////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
struct node /* 圖頂點結構定義 */
{
int vertex; /* 頂點數據信息 */
struct node *nextnode; /* 指下一頂點的指標 */
};
typedef struct node *graph; /* 圖形的結構新型態 */
struct node head[9]; /* 圖形頂點數組 */
int visited[9]; /* 遍歷標記數組 */
/********************根據已有的信息建立鄰接表********************/
void creategraph(int node[20][2],int num)/*num指的是圖的邊數*/
{
graph newnode; /*指向新節點的指針定義*/
graph ptr;
int from; /* 邊的起點 */
int to; /* 邊的終點 */
int i;
for ( i = 0; i < num; i++ ) /* 讀取邊線信息,插入鄰接表*/
{
from = node[i][0]; /* 邊線的起點 */
to = node[i][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("vertex[%d]\n",current); /* 輸出遍歷頂點值 */
ptr = head[current].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
if ( visited[ptr->vertex] == 0 ) /* 如過沒遍歷過 */
dfs(ptr->vertex); /* 遞回遍歷呼叫 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
}
/****************************** 主程序******************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 邊線數組 */
{1, 3}, {3, 1},
{1, 4}, {4, 1},
{2, 5}, {5, 2},
{2, 6}, {6, 2},
{3, 7}, {7, 3},
{4, 7}, {4, 4},
{5, 8}, {8, 5},
{6, 7}, {7, 6},
{7, 8}, {8, 7} };
int i;
clrscr();
for ( i = 1; i <= 8; i++ ) /* 頂點數組初始化 */
{
head[i].vertex = i; /* 設定頂點值 */
head[i].nextnode = NULL; /* 指針為空 */
visited[i] = 0; /* 設定遍歷初始標志 */
}
creategraph(node,20); /* 建立鄰接表 */
printf("Content of the gragh's ADlist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf("vertex%d ->",head[i].vertex); /* 頂點值 */
ptr = head[i].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
printf(" %d ",ptr->vertex); /* 印出頂點內容 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
printf("\n"); /* 換行 */
}
printf("\nThe end of the dfs are:\n");
dfs(1); /* 列印輸出遍歷過程 */
printf("\n"); /* 換行 */
puts(" Press any key to quit...");
getch();
}
/*//////////////////////////////////////////*/
/* 圖形的廣度優先搜尋法 */
/* ///////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
#define MAXQUEUE 10 /* 隊列的最大容量 */
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[20][2],int num)
{
graph newnode; /* 頂點指針 */
graph ptr;
int from; /* 邊起點 */
int to; /* 邊終點 */
int i;
for ( i = 0; i < num; i++ ) /* 第i條邊的信息處理 */
{
from = node[i][0]; /* 邊的起點 */
to = node[i][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; /* 插入第i個節點的鏈表尾部 */
}
}
/************************ 數值入隊列************************************/
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; /* 已遍歷過記錄標志置疑1*/
printf(" Vertex[%d]\n",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; /* 置遍歷標記為1 */
printf(" Vertex[%d]\n",ptr->vertex);/* 印出遍歷頂點值 */
}
ptr = ptr->nextnode; /* 下一個頂點 */
}
}
}
/*********************** 主程序 ************************************/
/*********************************************************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 邊信息數組 */
{6, 3}, {3, 6},
{2, 4}, {4, 2},
{1, 5}, {5, 1},
{3, 7}, {7, 3},
{1, 7}, {7, 1},
{4, 8}, {8, 4},
{5, 8}, {8, 5},
{2, 8}, {8, 2},
{7, 8}, {8, 7} };
int i;
clrscr();
puts("This is an example of Width Preferred Traverse of Gragh.\n");
for ( i = 1; i <= 8; i++ ) /*頂點結構數組初始化*/
{
head[i].vertex = i;
head[i].nextnode = NULL;
visited[i] = 0;
}
creategraph(node,20); /* 圖信息轉換,鄰接表的建立 */
printf("The content of the graph's allist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf(" vertex%d =>",head[i].vertex); /* 頂點值 */
ptr = head[i].nextnode; /* 頂點位置 */
while ( ptr != NULL ) /* 遍歷至鏈表尾 */
{
printf(" %d ",ptr->vertex); /* 列印輸出頂點內容 */
ptr = ptr->nextnode; /* 下一個頂點 */
}
printf("\n"); /* 換行 */
}
printf("The contents of BFS are:\n");
bfs(1); /* 列印輸出遍歷過程 */
printf("\n"); /* 換行 */
puts(" Press any key to quit...");
getch();
}
㈦ C語言編程 圖的創建與遍歷
在C語言編程中,圖的創建和遍歷:
#include<stdio.h>
#define N 20
#define TRUE 1
#define FALSE 0
int visited[N];
typedef struct /*隊列的定義*/
{
int data[N];
int front,rear;
}queue;
typedef struct /*圖的鄰接矩陣*/
{
int vexnum,arcnum;
char vexs[N];
int arcs[N][N];
}
graph;
void createGraph(graph *g); /*建立一個無向圖的鄰接矩陣*/
void dfs(int i,graph *g); /*從第i個頂點出發深度優先搜索*/
void tdfs(graph *g); /*深度優先搜索整個圖*/
void bfs(int k,graph *g); /*從第k個頂點廣度優先搜索*/
void tbfs(graph *g); /*廣度優先搜索整個圖*/
void init_visit(); /*初始化訪問標識數組*/
void createGraph(graph *g) /*建立一個無向圖的鄰接矩陣*/
{ int i,j;
char v;
g->vexnum=0;
g->arcnum=0;
i=0;
printf("輸入頂點序列(以#結束):
");
while((v=getchar())!='#')
{
g->vexs[i]=v; /*讀入頂點信息*/
i++;
}
g->vexnum=i; /*頂點數目*/
for(i=0;i<g->vexnum;i++) /*鄰接矩陣初始化*/
for(j=0;j<g->vexnum;j++)
g->arcs[i][j]=0;
printf("輸入邊的信息:
");
scanf("%d,%d",&i,&j); /*讀入邊i,j*/
while(i!=-1) /*讀入i,j為-1時結束*/
{
g->arcs[i][j]=1;
g->arcs[j][i]=1;
scanf("%d,%d",&i,&j);
}
}
void dfs(int i,graph *g) /*從第i個頂點出發深度優先搜索*/
{
int j;
printf("%c",g->vexs[i]);
visited[i]=TRUE;
for(j=0;j<g->vexnum;j++)
if((g->arcs[i][j]==1)&&(!visited[j]))
dfs(j,g);
}
void tdfs(graph *g) /*深度優先搜索整個圖*/
{
int i;
printf("
從頂點%C開始深度優先搜索序列:",g->vexs[0]);
for(i=0;i<g->vexnum;i++)
if(visited[i]!=TRUE)
dfs(i,g);
}
void bfs(int k,graph *g) /*從第k個頂點廣度優先搜索*/
{
int i,j;
queue qlist,*q;
q=&qlist;
q->rear=0;
q->front=0;
printf("%c",g->vexs[k]);
visited[k]=TRUE;
q->data[q->rear]=k;
q->rear=(q->rear+1)%N;
while(q->rear!=q->front)
{
i=q->data[q->front];
q->front=(q->front+1)%N;
for(j=0;j<g->vexnum;j++)
if((g->arcs[i][j]==1)&&(!visited[j]))
{
printf("%c",g->vexs[j]);
visited[j]=TRUE;
q->data[q->rear]=j;
q->rear=(q->rear+1)%N;
}
}
}
void tbfs(graph *g) /*廣度優先搜索整個圖*/
{
int i;
printf("
從頂點%C開始廣度優先搜索序列:",g->vexs[0]);
for(i=0;i<g->vexnum;i++)
if(visited[i]!=TRUE)
bfs(i,g);
printf("
");
}
void init_visit() /*初始化訪問標識數組*/
{
int i;
for(i=0;i<N;i++)
visited[i]=FALSE;
}
int main()
{
graph ga;
int i,j;
createGraph(&ga);
printf("無向圖的鄰接矩陣:
");
for(i=0;i<ga.vexnum;i++)
{
for(j=0;j<ga.vexnum;j++)
printf("%3d",ga.arcs[i][j]);
printf("
");
}
init_visit();
tdfs(&ga);
init_visit();
tbfs(&ga);
return 0;
}
㈧ 圖的深度優先遍歷c語言演算法
#include <stdio.h>
int m,n;
bool w[100][100],visited[100];
void dfs(int i){
visited[i] = true;
printf("%d ",i);
for(int j = 0;j<n;j++)
if(w[i][j] && !visited[j])
dfs(j);
}
int main(){
scanf("%d%d",&m,&n);
int a,b;
for(int i = 0;i<m;i++){
scanf("%d%d,&a,&b);
w[a][b] = w[b][a] = true;
}
for(int i = 0;i<n;i++)
if(!visited[i])
dfs(i);
return 0;
}
㈨ C語言實現圖的廣度優先搜索遍歷演算法
先寫個大題思路,樓主先自己想想,想不出來的話,2天後給代碼。
queue<node> q;
q.push(start);
bool canVisit[][];
node cur;
while(!q.empty()){
cur = q.top();
q.pop();
foreach(node is connected by cur){
if(canVisit[node.x][node.y])
{
printf("訪問結點(%d,%d)",node.x,node.y);
canVisit[node.x][node.y]=false;
q.push(node);
}
}
}
㈩ 圖的遍歷(c語言)完整上機代碼
//圖的遍歷演算法程序
//圖的遍歷是指按某條搜索路徑訪問圖中每個結點,使得每個結點均被訪問一次,而且僅被訪問一次。圖的遍歷有深度遍歷演算法和廣度遍歷演算法,程序如下:
#include <iostream>
//#include <malloc.h>
#define INFINITY 32767
#define MAX_VEX 20 //最大頂點個數
#define QUEUE_SIZE (MAX_VEX+1) //隊列長度
using namespace std;
bool *visited; //訪問標志數組
//圖的鄰接矩陣存儲結構
typedef struct{
char *vexs; //頂點向量
int arcs[MAX_VEX][MAX_VEX]; //鄰接矩陣
int vexnum,arcnum; //圖的當前頂點數和弧數
}Graph;
//隊列類
class Queue{
public:
void InitQueue(){
base=(int *)malloc(QUEUE_SIZE*sizeof(int));
front=rear=0;
}
void EnQueue(int e){
base[rear]=e;
rear=(rear+1)%QUEUE_SIZE;
}
void DeQueue(int &e){
e=base[front];
front=(front+1)%QUEUE_SIZE;
}
public:
int *base;
int front;
int rear;
};
//圖G中查找元素c的位置
int Locate(Graph G,char c){
for(int i=0;i<G.vexnum;i++)
if(G.vexs[i]==c) return i;
return -1;
}
//創建無向網
void CreateUDN(Graph &G){
int i,j,w,s1,s2;
char a,b,temp;
printf("輸入頂點數和弧數:");
scanf("%d%d",&G.vexnum,&G.arcnum);
temp=getchar(); //接收回車
G.vexs=(char *)malloc(G.vexnum*sizeof(char)); //分配頂點數目
printf("輸入%d個頂點.\n",G.vexnum);
for(i=0;i<G.vexnum;i++){ //初始化頂點
printf("輸入頂點%d:",i);
scanf("%c",&G.vexs[i]);
temp=getchar(); //接收回車
}
for(i=0;i<G.vexnum;i++) //初始化鄰接矩陣
for(j=0;j<G.vexnum;j++)
G.arcs[i][j]=INFINITY;
printf("輸入%d條弧.\n",G.arcnum);
for(i=0;i<G.arcnum;i++){ //初始化弧
printf("輸入弧%d:",i);
scanf("%c %c %d",&a,&b,&w); //輸入一條邊依附的頂點和權值
temp=getchar(); //接收回車
s1=Locate(G,a);
s2=Locate(G,b);
G.arcs[s1][s2]=G.arcs[s2][s1]=w;
}
}
//圖G中頂點k的第一個鄰接頂點
int FirstVex(Graph G,int k){
if(k>=0 && k<G.vexnum){ //k合理
for(int i=0;i<G.vexnum;i++)
if(G.arcs[k][i]!=INFINITY) return i;
}
return -1;
}
//圖G中頂點i的第j個鄰接頂點的下一個鄰接頂點
int NextVex(Graph G,int i,int j){
if(i>=0 && i<G.vexnum && j>=0 && j<G.vexnum){ //i,j合理
for(int k=j+1;k<G.vexnum;k++)
if(G.arcs[i][k]!=INFINITY) return k;
}
return -1;
}
//深度優先遍歷
void DFS(Graph G,int k){
int i;
if(k==-1){ //第一次執行DFS時,k為-1
for(i=0;i<G.vexnum;i++)
if(!visited[i]) DFS(G,i); //對尚未訪問的頂點調用DFS
}
else{
visited[k]=true;
printf("%c ",G.vexs[k]); //訪問第k個頂點
for(i=FirstVex(G,k);i>=0;i=NextVex(G,k,i))
if(!visited[i]) DFS(G,i); //對k的尚未訪問的鄰接頂點i遞歸調用DFS
}
}
//廣度優先遍歷
void BFS(Graph G){
int k;
Queue Q; //輔助隊列Q
Q.InitQueue();
for(int i=0;i<G.vexnum;i++)
if(!visited[i]){ //i尚未訪問
visited[i]=true;
printf("%c ",G.vexs[i]);
Q.EnQueue(i); //i入列
while(Q.front!=Q.rear){
Q.DeQueue(k); //隊頭元素出列並置為k
for(int w=FirstVex(G,k);w>=0;w=NextVex(G,k,w))
if(!visited[w]){ //w為k的尚未訪問的鄰接頂點
visited[w]=true;
printf("%c ",G.vexs[w]);
Q.EnQueue(w);
}
}
}
}
//主函數
void main(){
int i;
Graph G;
CreateUDN(G);
visited=(bool *)malloc(G.vexnum*sizeof(bool));
printf("\n廣度優先遍歷: ");
for(i=0;i<G.vexnum;i++)
visited[i]=false;
DFS(G,-1);
printf("\n深度優先遍歷: ");
for(i=0;i<G.vexnum;i++)
visited[i]=false;
BFS(G);
printf("\n程序結束.\n");
}
輸出結果為(紅色為鍵盤輸入的數據,權值都置為1):
輸入頂點數和弧數:8 9
輸入8個頂點.
輸入頂點0:a
輸入頂點1:b
輸入頂點2:c
輸入頂點3:d
輸入頂點4:e
輸入頂點5:f
輸入頂點6:g
輸入頂點7:h
輸入9條弧.
輸入弧0:a b 1
輸入弧1:b d 1
輸入弧2:b e 1
輸入弧3:d h 1
輸入弧4:e h 1
輸入弧5:a c 1
輸入弧6:c f 1
輸入弧7:c g 1
輸入弧8:f g 1
廣度優先遍歷: a b d h e c f g
深度優先遍歷: a b c d e f g h
程序結束.
已經在vc++內運行通過,這個程序已經達到要求了呀~