A. 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");
}
B. c語言關於圖的廣度優先遍歷
深度優先是沿著一條路走到底,走不通了或到頭了,再回溯,再搜索。而廣搜是先搜離得最近的,再慢慢搜索遠的,隊列就是按順序存,所以開頭存的近的,末尾存遠的,說白了隊列就是從近到遠保存數據的,說的不好,希望對你會點幫助。
C. 求一個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");
}
D. 如何寫 圖的深度優先和廣度優先遍歷的C程序。
深度優先遍歷可以用遞歸寫,訪問跟節點,然後遞歸遍歷根節點的各個子樹,注意是遍歷子樹不是訪問孩子節點
廣度優先遍歷可以用隊列,訪問根節點,然後把根節點的各個孩子節點放入隊列,每次訪問一個節點之後,訪問隊頭節點,把那個節點的兒子節點繼續放入隊列
E. 圖的廣度優先遍歷的C語言程序(有頭文件的)
// bo7-2.cpp 圖的鄰接表存儲(存儲結構由c7-2.h定義)的基本操作(15個)
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;
}
Status PutVex(ALGraph &G,VertexType v,VertexType value)
{ // 初始條件: 圖G存在,v是G中某個頂點
// 操作結果: 對v賦新值value
int i;
i=LocateVex(G,v);
if(i>-1) // v是G的頂點
{
strcpy(G.vertices[i].data,value);
return OK;
}
return ERROR;
}
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的)下一個鄰接頂點的序號
}
void InsertVex(ALGraph &G,VertexType v)
{ // 初始條件: 圖G存在,v和圖中頂點有相同特徵
// 操作結果: 在圖G中增添新頂點v(不增添與頂點相關的弧,留待InsertArc()去做)
strcpy(G.vertices[G.vexnum].data,v); // 構造新頂點向量
G.vertices[G.vexnum].firstarc=NULL;
G.vexnum++; // 圖G的頂點數加1
}
Status DeleteVex(ALGraph &G,VertexType v)
{ // 初始條件: 圖G存在,v是G中某個頂點
// 操作結果: 刪除G中頂點v及其相關的弧
int i,j;
ArcNode *p,*q;
j=LocateVex(G,v); // j是頂點v的序號
if(j<0) // v不是圖G的頂點
return ERROR;
p=G.vertices[j].firstarc; // 刪除以v為出度的弧或邊
while(p)
{
q=p;
p=p->nextarc;
if(G.kind%2) // 網
free(q->info);
free(q);
G.arcnum--; // 弧或邊數減1
}
G.vexnum--; // 頂點數減1
for(i=j;i<G.vexnum;i++) // 頂點v後面的頂點前移
G.vertices[i]=G.vertices[i+1];
for(i=0;i<G.vexnum;i++) // 刪除以v為入度的弧或邊且必要時修改表結點的頂點位置值
{
p=G.vertices[i].firstarc; // 指向第1條弧或邊
while(p) // 有弧
{
if(p->adjvex==j)
{
if(p==G.vertices[i].firstarc) // 待刪結點是第1個結點
{
G.vertices[i].firstarc=p->nextarc;
if(G.kind%2) // 網
free(p->info);
free(p);
p=G.vertices[i].firstarc;
if(G.kind<2) // 有向
G.arcnum--; // 弧或邊數減1
}
else
{
q->nextarc=p->nextarc;
if(G.kind%2) // 網
free(p->info);
free(p);
p=q->nextarc;
if(G.kind<2) // 有向
G.arcnum--; // 弧或邊數減1
}
}
else
{
if(p->adjvex>j)
p->adjvex--; // 修改表結點的頂點位置值(序號)
q=p;
p=p->nextarc;
}
}
}
return OK;
}
Status InsertArc(ALGraph &G,VertexType v,VertexType w)
{ // 初始條件: 圖G存在,v和w是G中兩個頂點
// 操作結果: 在G中增添弧<v,w>,若G是無向的,則還增添對稱弧<w,v>
ArcNode *p;
int w1,i,j;
i=LocateVex(G,v); // 弧尾或邊的序號
j=LocateVex(G,w); // 弧頭或邊的序號
if(i<0||j<0)
return ERROR;
G.arcnum++; // 圖G的弧或邊的數目加1
if(G.kind%2) // 網
{
printf("請輸入弧(邊)%s→%s的權值: ",v,w);
scanf("%d",&w1);
}
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
if(G.kind%2) // 網
{
p->info=(int*)malloc(sizeof(int));
*(p->info)=w1;
}
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)=w1;
}
else
p->info=NULL;
p->nextarc=G.vertices[j].firstarc; // 插在表頭
G.vertices[j].firstarc=p;
}
return OK;
}
Status DeleteArc(ALGraph &G,VertexType v,VertexType w)
{ // 初始條件: 圖G存在,v和w是G中兩個頂點
// 操作結果: 在G中刪除弧<v,w>,若G是無向的,則還刪除對稱弧<w,v>
ArcNode *p,*q;
int i,j;
i=LocateVex(G,v); // i是頂點v(弧尾)的序號
j=LocateVex(G,w); // j是頂點w(弧頭)的序號
if(i<0||j<0||i==j)
return ERROR;
p=G.vertices[i].firstarc; // p指向頂點v的第一條出弧
while(p&&p->adjvex!=j) // p不空且所指之弧不是待刪除弧<v,w>
{ // p指向下一條弧
q=p;
p=p->nextarc;
}
if(p&&p->adjvex==j) // 找到弧<v,w>
{
if(p==G.vertices[i].firstarc) // p所指是第1條弧
G.vertices[i].firstarc=p->nextarc; // 指向下一條弧
else
q->nextarc=p->nextarc; // 指向下一條弧
if(G.kind%2) // 網
free(p->info);
free(p); // 釋放此結點
G.arcnum--; // 弧或邊數減1
}
if(G.kind>=2) // 無向,刪除對稱弧<w,v>
{
p=G.vertices[j].firstarc; // p指向頂點w的第一條出弧
while(p&&p->adjvex!=i) // p不空且所指之弧不是待刪除弧<w,v>
{ // p指向下一條弧
q=p;
p=p->nextarc;
}
if(p&&p->adjvex==i) // 找到弧<w,v>
{
if(p==G.vertices[j].firstarc) // p所指是第1條弧
G.vertices[j].firstarc=p->nextarc; // 指向下一條弧
else
q->nextarc=p->nextarc; // 指向下一條弧
if(G.kind==3) // 無向網
free(p->info);
free(p); // 釋放此結點
}
}
return OK;
}
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"c3-2.h"
#include"bo3-2.cpp"
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");
}
}
// c7-2.h 圖的鄰接表存儲表示
#define MAX_VERTEX_NUM 20
enum GraphKind{DG,DN,AG,AN}; // {有向圖,有向網,無向圖,無向網}
struct ArcNode
{
int adjvex; // 該弧所指向的頂點的位置
ArcNode *nextarc; // 指向下一條弧的指針
InfoType *info; // 網的權值指針
}; // 表結點
typedef struct
{
VertexType data; // 頂點信息
ArcNode *firstarc; // 第一個表結點的地址,指向第一條依附該頂點的弧的指針
}VNode,AdjList[MAX_VERTEX_NUM]; // 頭結點
struct ALGraph
{
AdjList vertices;
int vexnum,arcnum; // 圖的當前頂點數和弧數
int kind; // 圖的種類標志
};
// c3-2.h 單鏈隊列--隊列的鏈式存儲結構
typedef struct QNode
{
QElemType data;
QNode *next;
}*QueuePtr;
struct LinkQueue
{
QueuePtr front,rear; // 隊頭、隊尾指針
};
// bo3-2.cpp 鏈隊列(存儲結構由c3-2.h定義)的基本操作(9個)
Status InitQueue(LinkQueue &Q)
{ // 構造一個空隊列Q
if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
Status DestroyQueue(LinkQueue &Q)
{ // 銷毀隊列Q(無論空否均可)
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
Status ClearQueue(LinkQueue &Q)
{ // 將Q清為空隊列
QueuePtr p,q;
Q.rear=Q.front;
p=Q.front->next;
Q.front->next=NULL;
while(p)
{
q=p;
p=p->next;
free(q);
}
return OK;
}
Status QueueEmpty(LinkQueue Q)
{ // 若Q為空隊列,則返回TRUE,否則返回FALSE
if(Q.front==Q.rear)
return TRUE;
else
return FALSE;
}
int QueueLength(LinkQueue Q)
{ // 求隊列的長度
int i=0;
QueuePtr p;
p=Q.front;
while(Q.rear!=p)
{
i++;
p=p->next;
}
return i;
}
Status GetHead(LinkQueue Q,QElemType &e)
{ // 若隊列不空,則用e返回Q的隊頭元素,並返回OK,否則返回ERROR
QueuePtr p;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;
e=p->data;
return OK;
}
Status EnQueue(LinkQueue &Q,QElemType e)
{ // 插入元素e為Q的新的隊尾元素
QueuePtr p;
if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 存儲分配失敗
exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e)
{ // 若隊列不空,刪除Q的隊頭元素,用e返回其值,並返回OK,否則返回ERROR
QueuePtr p;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return OK;
}
Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType))
{ // 從隊頭到隊尾依次對隊列Q中每個元素調用函數vi()。一旦vi失敗,則操作失敗
QueuePtr p;
p=Q.front->next;
while(p)
{
vi(p->data);
p=p->next;
}
printf("\n");
return OK;
}
F. 圖的深度/廣度優先遍歷C語言程序
#define INFINITY 10000 //無窮大
#define MAX_VERTEX_NUM 40
#define MAX 40
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
typedef struct ArCell{
int adj;
}ArCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
char name[20];
}infotype;
typedef struct
{
infotype vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
}MGraph;
int LocateVex(MGraph *G,char* v)
{ int c=-1,i;
for(i=0;i<G->vexnum;i++)
if(strcmp(v,G->vexs[i].name)==0)
{c=i;break;}
return c;
}
MGraph * CreatUDN(MGraph *G)//初始化圖,接受用戶輸入
{
int i,j,k,w;
char v1[20],v2[20];
printf("請輸入圖的頂點數和弧數:");
scanf("%d,%d",&G->vexnum,&G->arcnum);
printf("請依次輸入圖的頂點\n");
for(i=0;i<G->vexnum;i++){
printf("G.vexs[%d] : ",i);
scanf("%s",G->vexs[i].name);
getchar();
} // 構造頂點向量
for(i=0;i<G->vexnum;i++) //初始化鄰接矩陣
for(j=0;j<G->vexnum;j++)
G->arcs[i][j].adj=INFINITY;
printf("請輸入一條邊依附的兩個頂點和權值:\n");
for(k=0;k<G->arcnum;k++) //構造鄰接矩陣
{
printf("第%d條邊:\n",k+1);
printf("起始結點:");
scanf("%s",v1);
printf("結束結點:");
scanf("%s",v2);
printf("邊的權值:");
scanf("%d",&w);
i=LocateVex(G,v1);
j=LocateVex(G,v2); // 確定v1和v2在G中位置
if(i>=0&&j>=0){
G->arcs[i][j].adj=w;
G->arcs[j][i]=G->arcs[i][j]; //置<v1,v2>的對稱弧<v2,v1>
}
}
return G;
}
int FirstAdjVex(MGraph *G,int v)
{
int i;
if(v>=0 &&v<G->vexnum)
{ //v合理
for(i=0;i<G->vexnum;i++)
if(G->arcs[v][i].adj!=INFINITY)
{return i;break;}
}
return -1;
}
void VisitFunc(MGraph *G,int v)
{
printf("%s ",G->vexs[v].name);
}
int NextAdjVex(MGraph *G,int v,int w)
{
int k;
if(v>=0 && v<G->vexnum && w>=0 && w<G->vexnum) //v,w合理
{
for( k=w+1;k<G->vexnum;k++)
if(G->arcs[v][k].adj!=INFINITY)
{return k;break;}
}
return -1;
}
int visited[MAX];
void DFS(MGraph *G,int v)//從第v個頂點出發遞歸地深度優先遍歷圖G
{
int w;
visited[v]=1;
VisitFunc(G,v);//訪問第v個結點
for(w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))
if(!visited[w]){
DFS(G,w);
}
}
void DFSTraverse(MGraph *G,char *s)//深度優先遍歷
{int v,k;
for(v=0;v<G->vexnum;v++)
visited[v]=0;
k=LocateVex(G,s);
if(k>=0&&k<G->vexnum){
for(v=k;v>=0;v--){
if(!visited[v])
DFS(G,v);}
for(v=k+1;v<G->vexnum;v++)
if(!visited[v])
DFS(G,v);
}
}
typedef struct QNode {
int data;
struct QNode *next;
}QNode,*Queueptr;
typedef struct {
Queueptr front;
Queueptr rear;
}LinkQueue;
int InitQueue (LinkQueue &Q){
Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
if(!Q.front)exit(-1);
Q.front->next=NULL;
return 1;
}
int EnQueue(LinkQueue &Q,int e){
Queueptr p;
p=(Queueptr)malloc(sizeof(QNode));
if(!p)exit(-1);
p->data=e; p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return 1;
}
int DeQueue (LinkQueue &Q,int &e){
Queueptr p;
if(Q.front==Q.rear)return(-1);
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return 1;
}
int QueueEmpty(LinkQueue Q)
{
if(Q.rear==Q.front)
return 1;
return 0;
}
int Visited[MAX];
void BFSTraverse(MGraph *G,char *str) { //廣度優先遍歷
int w,u,v,k;
LinkQueue Q;
for(v=0;v<G->vexnum;v++) Visited[v]=0;
InitQueue(Q);
k=LocateVex(G,str);
for(v=k;v>=0;v--)
//for(v=0;v<G->vexnum;++v)
{
if(!Visited[v])
{
Visited[v]=1;
VisitFunc(G,v);
EnQueue(Q,v); //v入隊
while(!QueueEmpty(Q))
{
DeQueue(Q,u); //出隊
for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
if(!Visited[w])
{
Visited[w]=1;
VisitFunc(G,w);
EnQueue(Q,w);
}
} //while
}
}
for(v=k+1;v<G->vexnum;v++)
if(!Visited[v])
{
Visited[v]=1;
VisitFunc(G,v);
EnQueue(Q,v);//v入隊
while(!QueueEmpty(Q))
{
DeQueue(Q,u);//出隊
for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
if(!Visited[w])
{
Visited[w]=1;
VisitFunc(G,w);
EnQueue(Q,w);
}
}
}
}
void main()
{
MGraph *G,b;
char v[10];
G=CreatUDN(&b);
printf("請輸入開始遍歷的起始結點名稱:");
scanf("%s",v);
printf("\n深度優先遍歷(輸出結點序列):\n");
DFSTraverse(G,v);
printf("\n廣度優先遍歷(輸出結點序列):\n");
BFSTraverse(G,v);
}
這個程序我剛寫好,可以運行出結果的,你先運行一下試試.
G. 用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();
}
H. 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();
}
I. 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);
}
}
}
J. 求圖的廣度遍歷(C/C++ 程序)
#include<stdio.h>
#include<stdlib.h>
#define Max 10
#define FALSE 0
#define TRUE 1
#define Error printf
#define QueueSize 30
typedef struct
{
char vexs[Max];
int edges[Max][Max];
int n,e;
}MGraph;
int visited[Max];
typedef struct
{
int front;
int rear;
int count;
int data[QueueSize];
}CirQueue;
//初始化隊列
void InitQueue(CirQueue *Q)
{
Q->front=Q->rear=0;
Q->count=0;
}
//隊列空
int QueueEmpty(CirQueue *Q)
{
return Q->count=QueueSize;
}
//隊列滿
int QueueFull(CirQueue *Q)
{
return Q->count==QueueSize;
}
//進隊
void EnQueue(CirQueue *Q,int x)
{
if(QueueFull(Q))
{
Error("Queue overflow");
}
else
{
Q->count++;
Q->data[Q->rear]=x;
Q->rear=(Q->rear+1)%QueueSize;
}
}
//出隊
int DeQueue(CirQueue *Q)
{
int temp;
if(QueueEmpty(Q))
{
Error("Queue underflow");
}
else
{
temp=Q->data[Q->front];
Q->count--;
Q->front=(Q->front+1)%QueueSize;
return temp;
}
}
//建立圖矩陣
void CreateMGraph(MGraph *G)
{
int i,j,k;
char ch1,ch2;
printf("\n\t\t請輸入頂點數,邊數並按回車鍵(格式:3,4):");
scanf("%d,%d",&(G->n),&(G->e));
for(i=0;i<G->n;i++)
{
getchar();
printf("\n\t\t請輸入第%d個頂點序號並按回車鍵",i+1);
scanf("%c",&(G->vexs[i]));
}
for(i=0;i<G->n;i++)
{
for(j=0;j<G->n;j++)
{
G->edges[i][j]=0;
}
}
for(k=0;k<G->e;k++)
{
getchar();
printf("\n\t\t請輸入第%d條邊的頂點序號並按回車鍵(格式:i,j):",k+1);
scanf("%c,%c",&ch1,&ch2);
for(i=0;ch1!=G->vexs[i];i++);
for(j=0;ch2!=G->vexs[j];j++);
G->edges[i][j]=1;
}
}
//深度優先遍歷遞歸
void DFSM(MGraph *G,int i)
{
int j;
printf("\n\t\t深度遍歷序列:%c\n",G->vexs[i]);
visited[i]=TRUE;
for(j=0;j<G->n;j++)
{
if(G->edges[i][j]==1&&!visited[j])
{
DFSM(G,j);
}
}
}
//廣度優先遍歷遞歸
void BFSM(MGraph *G,int k)
{
int i,j;
CirQueue Q;
InitQueue(&Q);
printf("\n\t\t廣度優先遍歷序列:%c\n",G->vexs[k]);
visited[k]=TRUE;
EnQueue(&Q,k);
while(!QueueEmpty(&Q))
{
i=DeQueue(&Q);
for(j=0;j<G->n;j++)
{
if(G->edges[i][j]==1 &&! visited[j])
{
visited[j]=TRUE;
EnQueue(&Q,j);
}
}
}
}
//深度優先遍歷
void DFSTraverseM(MGraph *G)
{
int i;
for(i=0;i<G->n;i++)
{
visited[i]=FALSE;
}
for(i=0;i<G->n;i++)
{
if(!visited[i])
{
DFSM(G,i);
}
}
}
//廣度優先遍歷
void BFSTraverseM(MGraph *G)
{
int i;
for(i=0;i<G->n;i++)
{
visited[i]=FALSE;
}
for(i=0;i<G->n;i++)
{
if(!visited[i])
{
BFSM(G,i);
}
}
}
main()
{
MGraph *G,a;
char ch1;
int i,j,ch2;
G=&a;
printf("\n\t\t建立一個圖矩陣\n");
CreateMGraph(G);
printf("\n\t\t已建立圖的矩陣\n");
for(i=0;i<G->n;i++)
{
printf("\n\t\t ");
for(j=0;j<G->n;j++)
{
printf("%5d",G->edges[i][j]);
}
}
getchar();
ch1='y';
while(ch1=='y'||ch1=='Y')
{
printf("\n");
printf("\n\t\t 圖子系統 \n");
printf("\n\t\t***************************************\n");
printf("\n\t\t* 1--------更新矩陣 *\n");
printf("\n\t\t* 2--------深度優先遍歷 *\n");
printf("\n\t\t* 3--------廣度優先遍歷 *\n");
printf("\n\t\t* 0--------退出 *\n");
printf("\n\t\t***************************************\n");
printf("\n\t\t請選擇菜單號(0~3):");
scanf("%d",&ch2);
getchar();
switch(ch2)
{
case 1:
CreateMGraph(G);
printf("\n\t\t圖建立完畢!\n");
break;
case 2:
DFSTraverseM(G);
break;
case 3:
BFSTraverseM(G);
break;
case 0:
ch1='n';
break;
default:
system("cls");
printf("\n\t\t輸入有誤!\n");
break;
}
if(ch2==1||ch2==2||ch2==3)
{
printf("\n\n\t\t ");
system("pause");
system("cls");
}
}
}