當前位置:首頁 » 編程語言 » 圖的遍歷c語言上機
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

圖的遍歷c語言上機

發布時間: 2022-10-09 03:04:23

⑴ 求一個c語言編程,圖的遍歷,深度優先和廣度優先搜索的程序。要淺顯易懂的~~~~

給你一個作為參考吧

#include <iostream>

#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");
}

⑵ 用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語言演算法

#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語言編寫程序實現圖的遍歷操作

樓主你好,下面是源程序!

/*/////////////////////////////////////////////////////////////*/
/* 圖的深度優先遍歷 */
/*/////////////////////////////////////////////////////////////*/
#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語言版)

這是樹的遍歷:我以前做的··
#include<stdio.h>#include<malloc.h>
#define NULL 0

typedef struct node{
char data;
int ltag,rtag;
struct node * lch,* rch;
}node;

node * jianli(node * p){
int x;
scanf("%d",&x);
if(x==0)
p=NULL;
else
{
p=(node *)malloc(sizeof(node));
p->data=x;
printf("輸入%d的左孩子\n",p->data);
p->lch= jianli(p->lch);
printf("輸入%d的右孩子\n",p->data);
p->rch =jianli(p->lch);
}
return p;
}

void bianli(node * p){
if(p==NULL);
else
{
printf("%d",p->data );
bianli(p->lch );
bianli(p->rch );
}
}

void main(){
node * T=NULL;

printf("輸入樹根\n");
T=jianli(T);
bianli(T);

}

⑹ c語言圖的遍歷,鄰接表存儲,深度,廣度優先遍歷

(1) 圖的建立,按採用鄰接表作為存儲結構。
(2) 從指定頂點出發進行深度優先搜索遍歷。
(3) 從指定頂點出發進行廣度優先搜索遍歷。

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"

#define MAX_INT 1000
#define MAX_VERTEX_NUM 20
#define MAX_QUEUE_NUMBER 20

typedef struct ArcNode
{
int adjvex;
double adj;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VexNode
{
char szName[40];
ArcNode *firstarc;
}VexNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vexs;
int vexnum,arcnum;
}Net;
//定義隊列
typedef struct{
int *elem;
int front, rear;
}Queue;
void InitQueue(Queue &Q)
{
Q.elem = new int[MAX_QUEUE_NUMBER];
Q.front = Q.rear = 0;
}
int EmptyQueue(Queue Q)
{
if(Q.front==Q.rear)
return 0;
else
return 1;
}
void DestroyQueue(Queue &Q){
delete []Q.elem;
Q.front = Q.rear = 0;
}

void EnterQueue(Queue &Q, int e)
{
if((Q.rear + 1)%MAX_QUEUE_NUMBER != Q.front)
Q.elem[Q.rear ] = e;
else
printf("隊列滿!\n");
Q.rear = (Q.rear + 1)%MAX_QUEUE_NUMBER;
}
void LeaveQueue(Queue &Q, int &e)
{
if(Q.rear != Q.front)
e = Q.elem[Q.front];
else
printf("隊列空!\n");
Q.front = (Q.front+1)%MAX_QUEUE_NUMBER;
}
int LocateVex(Net ga,char *name)
{
int i;
for(i=0;i<ga.vexnum;i++)
if(strcmp(name,ga.vexs[i].szName)==0)
return i;
return -1;

}
void crt_net(Net &ga)
{
ArcNode *p;
char name1[40],name2[40];
int i,j,k;
double w;
printf("請輸入頂點數和弧數:");
scanf("%d%d",&ga.vexnum,&ga.arcnum);
printf("請依次輸入頂點名:\n");
for(i=0;i<ga.vexnum;i++)
{
scanf("%s",ga.vexs[i].szName);
ga.vexs[i].firstarc=NULL;
}
for(k=0;k<ga.arcnum;k++)
{
printf("請輸入相鄰的兩個定點和權值:");
scanf("%s%s%lf",name1,name2,&w);
i=LocateVex(ga,name1);
j=LocateVex(ga,name2);
p=new ArcNode;
p->adjvex=j;
p->adj=w;
p->nextarc=ga.vexs[i].firstarc;
ga.vexs[i].firstarc=p;
}
}

void DFS(Net ga,char *name,int *visited)
{
int v,w;
ArcNode *p;
v=LocateVex(ga,name);
visited[v]=1;
printf("%s ",ga.vexs[v].szName);
p=ga.vexs[v].firstarc;
while(p!=NULL)
{
w=p->adjvex;
if(visited[w]==0)
DFS(ga,ga.vexs[w].szName,visited);
p=p->nextarc;
}

}
void DFSTravel(Net ga,char *name)
{
int v,k=0;
int visited[20];
for(v=0;v<ga.vexnum;v++)
visited[v]=0;
for(v=LocateVex(ga,name);k!=2;v=(v+1)%(ga.vexnum-1))
{
if(v+1==LocateVex(ga,name))
k++;
if(visited[v]==0)
DFS(ga,ga.vexs[v].szName,visited);

}
}

void BFSTravel(Net ga,char *name)
{
ArcNode *p;
int v,w,u,k=0;
Queue Q;
int visited[20];
for(v=0;v<ga.vexnum;v++)
visited[v]=0;
InitQueue(Q);
for(v=LocateVex(ga,name);k!=2;v=(v+1)%(ga.vexnum-1))
{
if(v+1==LocateVex(ga,name))
k++;
if(visited[v]==0)
{
visited[v]=1;
printf("%s ",ga.vexs[v].szName);
EnterQueue(Q,v);
while(EmptyQueue(Q)!=0)
{
LeaveQueue(Q,u);
p=ga.vexs[u].firstarc;
while(p!=NULL)
{
w=p->adjvex;
if(visited[w]==0)
{
printf("%s ",ga.vexs[w].szName);
visited[w]=1;
EnterQueue(Q,w);
}
p=p->nextarc;
}
}
}

}
}

void main()
{
char name[40];
Net ga;
crt_net(ga);
printf("請輸入深度優先遍歷開始點的名:");
scanf("%s",name);
printf("深度優先遍歷:");
DFSTravel(ga,name);
printf("\n");
printf("請輸入廣度優先遍歷開始點的名:");
scanf("%s",name);
printf("廣度優先遍歷:");
BFSTravel(ga,name);
printf("\n");

}

⑺ C語言 圖的遍歷

思路:
以鄰接表或鄰接矩陣為存儲結構,實現連通無向圖的深度和廣度優先遍歷。以用戶指定的結點為起始點
,分別輸出每種遍歷下的結點訪問序列和相應的生成樹的邊集。
設圖的結點不超過30個,每個結點用一個編號表示。通過輸入圖的全部邊輸入一個圖,每個邊為一個數對
可以對邊的輸入順序作出某種限制。注意,生成樹和生成邊是有向邊,端點順序不能顛倒。

⑻ 用c語言編一段圖的創建與遍歷的代碼

//#include <stdafx.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 20
typedef char VertexType;//頂點數據類型
bool visited[MAX_SIZE];
typedef struct ArcNode{
int AdjVex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_SIZE];
typedef struct{
int vexnum,arcnum;
AdjList Vertices;
}ALGraph;
int LocateVex(ALGraph G,char v)
{
int i;
for(i=0;i<G.vexnum;i++)
{
if(G.Vertices[i].data==v) return i;
}
return -1;
}
void CreatG(ALGraph &G)
{
int i,a,b;
char m,n;
ArcNode *p,*q;
scanf("%d %d",&G.vexnum,&G.arcnum);
printf("請依次輸入頂點:\n");
for(i=0;i<G.vexnum;i++)
{
flushall();
scanf("%c",&G.Vertices[i].data);
G.Vertices[i].firstarc=NULL;
}
printf("請依次輸入邊:\n");
for(i=0;i<G.arcnum;i++)
{
flushall();
scanf("%c %c",&m,&n);
a=LocateVex(G,m);
b=LocateVex(G,n);
p=(ArcNode *)malloc(sizeof(ArcNode));
p->AdjVex=b;
p->nextarc=G.Vertices[a].firstarc;
G.Vertices[a].firstarc=p;
q=(ArcNode *)malloc(sizeof(ArcNode));
q->AdjVex=a;
q->nextarc=G.Vertices[b].firstarc;
G.Vertices[b].firstarc=q;
}
}
int FirstAdjVex(ALGraph G,int v)
{
ArcNode *p;
p=G.Vertices[v].firstarc;
return p->AdjVex;
}
int NextAdjVex(ALGraph G,int v,int w)
{
ArcNode *p;
p=G.Vertices[v].firstarc;
while(p->nextarc)
{
return p->nextarc->AdjVex;
}
}
void DFS(ALGraph G,int v)
{
int w;
visited[v]=true;
printf("%c ",G.Vertices[v].data);
for(w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))
if(!visited[w])
DFS(G,w);
}
void DFSTraverse(ALGraph G){
int v;
for(v=0;v<G.vexnum;v++)
visited[v]=false;
for(v=0;v<G.vexnum;v++)
if(!visited[v])
DFS(G,v);
}
void main()
{
ALGraph G;
printf("請輸入圖的頂點數和邊數:\n");
CreatG(G);
printf("深度優先遍歷結果:\n");
DFSTraverse(G);
}

希望對你有用

⑼ 數據結構代碼(用C語言) 圖的遍歷操作

#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等*/
#include<limits.h> /* INT_MAX 等*/
#include<stdio.h> /* EOF(=^Z 或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<io.h> /* eof() */
#include<math.h> /* floor(),ceil(),abs() */
#include<process.h> /* exit() */
/* 函數結果狀態代碼*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
/* #define OVERFLOW -2 因為在math.h 中已定義OVERFLOW 的值為3,故去掉此行*/
typedef int Status; /* Status 是函數的類型,其值是函數結果狀態代碼,如OK 等*/
typedef int Boolean; Boolean 是布爾類型,其值是TRUE 或FALSE */

/* .........................*/
#define MAX_VERTEX_NUM 20
typedef enumGraphKind; /* */
typedef struct ArcNode
{
int adjvex; /* 該弧所指向的頂點的位置*/
struct ArcNode *nextarc; /* 指向下一條弧的指針*/
InfoType *info; /* 網的權值指針) */
}ArcNode; /* 表結點*/
typedef struct
{
VertexType data; /* 頂點信息*/
ArcNode *firstarc; /* 第一個表結點的地址,指向第一條依附該頂點的弧的指針*/
}VNode,AdjList[MAX_VERTEX_NUM]; /* 頭結點*/
typedef struct
{
AdjList vertices;
int vexnum,arcnum; /* 圖的當前頂點數和弧數*/
int kind; /* 圖的種類標志*/
}ALGraph;
/* .........................*/

/* .........................*/
/*ALGraphAlgo.cpp 圖的鄰接表存儲(存儲結構由ALGraphDef.h 定義)的基本操作*/
int LocateVex(ALGraph G,VertexType u)
{ /* 初始條件: 圖G 存在,u 和G 中頂點有相同特徵*/
/* 操作結果: 若G 中存在頂點u,則返回該頂點在圖中位置;否則返回-1 */
int i;
for(i=0;i<G.vexnum;++i)
if(strcmp(u,G.vertices[i].data)==0)
return i;
return -1;
}
Status CreateGraph(ALGraph &G)
{ /* 採用鄰接表存儲結構,構造沒有相關信息的圖G(用一個函數構造4 種圖) */
int i,j,k;
int w; /* 權值*/
VertexType va,vb;
ArcNode *p;
printf("請輸入圖的類型(有向圖:0,有向網:1,無向圖:2,無向網:3): ");
scanf("%d",&(G.kind));
printf("請輸入圖的頂點數,邊數: ");
scanf("%d,%d",&(G.vexnum),&(G.arcnum));
printf("請輸入%d 個頂點的值(<%d 個字元):\n",G.vexnum,MAX_NAME);
for(i=0;i<G.vexnum;++i) /* 構造頂點向量*/
{
scanf("%s",G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
if(G.kind==1||G.kind==3) /* 網*/
printf("請順序輸入每條弧(邊)的權值、弧尾和弧頭(以空格作為間隔):\n");
else /* 圖*/
printf("請順序輸入每條弧(邊)的弧尾和弧頭(以空格作為間隔):\n");
for(k=0;k<G.arcnum;++k) /* 構造表結點鏈表*/
{
if(G.kind==1||G.kind==3) /* 網*/
scanf("%d%s%s",&w,va,vb);
else /* 圖*/
scanf("%s%s",va,vb);
i=LocateVex(G,va); /* 弧尾*/
j=LocateVex(G,vb); /* 弧頭*/
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
if(G.kind==1||G.kind==3) /* 網*/
{
p->info=(int *)malloc(sizeof(int));
*(p->info)=w;
}
else
p->info=NULL; /* 圖*/
p->nextarc=G.vertices[i].firstarc; /* 插在表頭*/
G.vertices[i].firstarc=p;
if(G.kind>=2) /* 無向圖或網,產生第二個表結點*/
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=i;
if(G.kind==3) /* 無向網*/
{
p->info=(int*)malloc(sizeof(int));
*(p->info)=w;
}
else
p->info=NULL; /* 無向圖*/
p->nextarc=G.vertices[j].firstarc; /* 插在表頭*/
G.vertices[j].firstarc=p;
}
}
return OK;
}
void DestroyGraph(ALGraph &G)
{ /* 初始條件: 圖G 存在。操作結果: 銷毀圖G */
int i;
ArcNode *p,*q;
G.vexnum=0;
G.arcnum=0;
for(i=0;i<G.vexnum;++i)
{
p=G.vertices[i].firstarc;
while(p)
{
q=p->nextarc;
if(G.kind%2) /* 網*/
free(p->info);
free(p);
p=q;
}
}
}
VertexType* GetVex(ALGraph G,int v)
{ /* 初始條件: 圖G 存在,v 是G 中某個頂點的序號。操作結果: 返回v 的值*/
if(v>=G.vexnum||v<0)
exit(ERROR);
return &G.vertices[v].data;
}
int FirstAdjVex(ALGraph G,VertexType v)
{ /* 初始條件: 圖G 存在,v 是G 中某個頂點*/
/* 操作結果: 返回v 的第一個鄰接頂點的序號。若頂點在G 中沒有鄰接頂點,則返回-1 */
ArcNode *p;
int v1;
v1=LocateVex(G,v); /* v1 為頂點v 在圖G 中的序號*/
p=G.vertices[v1].firstarc;
if(p)
return p->adjvex;
else
return -1;
}
int NextAdjVex(ALGraph G,VertexType v,VertexType w)
{ /* 初始條件: 圖G 存在,v 是G 中某個頂點,w 是v 的鄰接頂點*/
/* 操作結果: 返回v 的(相對於w 的)下一個鄰接頂點的序號。*/
/* 若w 是v 的最後一個鄰接點,則返回-1 */
ArcNode *p;
int v1,w1;
v1=LocateVex(G,v); /* v1 為頂點v 在圖G 中的序號*/
w1=LocateVex(G,w); /* w1 為頂點w 在圖G 中的序號*/
p=G.vertices[v1].firstarc;
while(p&&p->adjvex!=w1) /* 指針p 不空且所指表結點不是w */
p=p->nextarc;
if(!p||!p->nextarc) /* 沒找到w 或w 是最後一個鄰接點*/
return -1;
else /* p->adjvex==w */
return p->nextarc->adjvex; /* 返回v 的(相對於w 的)下一個鄰接頂點的序號*/
}
Boolean visited[MAX_VERTEX_NUM]; /* 訪問標志數組(全局量) */
void(*VisitFunc)(char* v); /* 函數變數(全局量) */
void DFS(ALGraph G,int v)
{ /* 從第v 個頂點出發遞歸地深度優先遍歷圖G。演算法7.5 */
int w;
VertexType v1,w1;
strcpy(v1,*GetVex(G,v));
visited[v]=TRUE; /* 設置訪問標志為TRUE(已訪問) */
VisitFunc(G.vertices[v].data); /* 訪問第v 個頂點*/
for(w=FirstAdjVex(G,v1);w>=0;w=NextAdjVex(G,v1,strcpy(w1,*GetVex(G,w))))
if(!visited[w])
DFS(G,w); /* 對v 的尚未訪問的鄰接點w 遞歸調用DFS */
}
void DFSTraverse(ALGraph G,void(*Visit)(char*))
{ /* 對圖G 作深度優先遍歷。演算法7.4 */
int v;
VisitFunc=Visit; /* 使用全局變數VisitFunc,使DFS 不必設函數指針參數*/
for(v=0;v<G.vexnum;v++)
visited[v]=FALSE; /* 訪問標志數組初始化*/
for(v=0;v<G.vexnum;v++)
if(!visited[v])
DFS(G,v); /* 對尚未訪問的頂點調用DFS */
printf("\n");
}
typedef int QElemType; /* 隊列類型*/
#include"LinkQueueDef.h"
#include"LinkQueueAlgo.h"
void BFSTraverse(ALGraph G,void(*Visit)(char*))
{/*按廣度優先非遞歸遍歷圖G。使用輔助隊列Q 和訪問標志數組visited。演算法7.6 */
int v,u,w;
VertexType u1,w1;
LinkQueue Q;
for(v=0;v<G.vexnum;++v)
visited[v]=FALSE; /* 置初值*/
InitQueue(Q); /* 置空的輔助隊列Q */
for(v=0;v<G.vexnum;v++) /* 如果是連通圖,只v=0 就遍歷全圖*/
if(!visited[v]) /* v 尚未訪問*/
{
visited[v]=TRUE;
Visit(G.vertices[v].data);
EnQueue(Q,v); /* v 入隊列*/
while(!QueueEmpty(Q)) /* 隊列不空*/
{
DeQueue(Q,u); /* 隊頭元素出隊並置為u */
strcpy(u1,*GetVex(G,u));
for(w=FirstAdjVex(G,u1);w>=0;w=NextAdjVex(G,u1,strcpy(w1,*GetVex(G,w))))
if(!visited[w]) /* w 為u 的尚未訪問的鄰接頂點*/
{
visited[w]=TRUE;
Visit(G.vertices[w].data);
EnQueue(Q,w); /* w 入隊*/
}
}
}
printf("\n");
}
void Display(ALGraph G)
{ /* 輸出圖的鄰接矩陣G */
int i;
ArcNode *p;
switch(G.kind)
{ case DG: printf("有向圖\n"); break;
case DN: printf("有向網\n"); break;
case AG: printf("無向圖\n"); break;
case AN: printf("無向網\n");
}
printf("%d 個頂點:\n",G.vexnum);
for(i=0;i<G.vexnum;++i)
printf("%s ",G.vertices[i].data);
printf("\n%d 條弧(邊):\n",G.arcnum);
for(i=0;i<G.vexnum;i++)
{
p=G.vertices[i].firstarc;
while(p)
{
if(G.kind<=1) /* 有向*/
{
printf("%s→%s ",G.vertices[i].data,G.vertices[p->adjvex].data);
if(G.kind==DN) /* 網*/
printf(":%d ",*(p->info));
}
else /* 無向(避免輸出兩次) */
{
if(i<p->adjvex)
{
printf("%s-%s ",G.vertices[i].data,G.vertices[p->adjvex].data);
if(G.kind==AN) /* 網*/
printf(":%d ",*(p->info));
}
}
p=p->nextarc;
}
printf("\n");
}
}

/* .........................*/

/* .........................*/
#include "pubuse.h"
#define MAX_NAME 3 /* 頂點字元串的最大長度+1 */
typedef int InfoType; /* 存放網的權值*/
typedef char VertexType[MAX_NAME]; /* 字元串類型*/
#include"ALGraphDef.h"
#include"ALGraphAlgo.h"
void print(char *i)
{
printf("%s ",i);
}

void main()
{
int i,j,k,n;
ALGraph g;
VertexType v1,v2;
printf("請選擇有向圖\n");
CreateGraph(g);
Display(g);
printf("深度優先搜索的結果:\n");
DFSTraverse(g,print);
printf("廣度優先搜索的結果:\n");
BFSTraverse(g,print);
DestroyGraph(g); /* 銷毀圖*/
}