設計並驗證如下算法:帶權圖採用鄰接表表示,實現無向圖的廣度優先搜索與有向圖的深度優先搜索。
#define MAX_VERTEX_NUM 20 //圖的鄰接表存儲表示
typedef struct ArcNode{
int adjvex; //該弧所指向的頂點的位置
struct ArcNode *nextarc; //指向下一條弧的指針
InfoType *info; //該弧相關信息的指針
}ArcNode;
typedef struct VNode {
VertexType data; //頂點信息
ArcNode *firstarc; //指向第一條依附該頂點弧的指針
}VNode,AdjList[MAX_VERTEX_NUM]
Typedef struct {
AdjList vertices;
int vexnum,arcnum; //圖的當前頂點數和弧數
int kind; //圖的種類標誌
}ALGraph;
以上是題目以及題目給的提示。
#include<stdio.h>
#include<stdlib.h>
#define MAX_VERTEX_NUM 20 //圖的鄰接表存儲表示
#define InfoType int
#define VertexType char
typedef struct ArcNode{
int adjvex; //該弧所指向的頂點的位置
struct ArcNode *nextarc; //指向下一條弧的指針
InfoType *info; //該弧相關信息的指針
}ArcNode;
typedef struct VNode {
VertexType data; //頂點信息
ArcNode *firstarc; //指向第一條依附該頂點弧的指針
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices;
int vexnum,arcnum; //圖的當前頂點數和弧數
int kind; //圖的種類標誌
}ALGraph;
int vs[20]={0,};
int LocateVex(ALGraph G,char v){//定位函數
for(int i=0;i<G.vexnum;i++){
if(v==G.vertices[i].data)
return i;
}
printf("input error ! input again:\n");
scanf("%c",&v);
getchar();
LocateVex(G,v);
return -1;
}
void PrintUDG(ALGraph G){//輸出鄰接表
int i,j;
for(i=0;i<G.vexnum;i++){
printf("%d=%c:",i,G.vertices[i].data);
ArcNode *p;
p=G.vertices[i].firstarc;
while(p!=NULL){
printf("->%2d",p->adjvex);
p=p->nextarc;
}
printf("\n");
}
}
void create(ALGraph &p)
{
printf("input the graph's kind (1 Undirected 2 Directed):\n");
scanf("%d",&p.kind);
printf("input the graph's vex and arc . \n");
scanf("%d%d",&p.vexnum,&p.arcnum);
getchar();
for(int i=0;i<p.vexnum;i++){
printf("vertex(%d): \n" ,i);
scanf("%c",&p.vertices[i].data);
getchar();
p.vertices[i].firstarc=NULL;
}
char v1 ,v2;
int k ,j;
for(int i=0;i<p.arcnum;i++){
printf("input two vertex :\n");
v1 = getchar();
getchar();
v2 = getchar();
getchar();
j = LocateVex(p, v1);//定位
k = LocateVex(p, v2);
ArcNode *q = (ArcNode *)malloc(sizeof(ArcNode));//申請一個結點
q->adjvex=k;//連接結點
q->nextarc=p.vertices[j].firstarc;//連接結點
p.vertices[j].firstarc=q;//連接結點
if(p.kind==1){//如果是無向圖
ArcNode *g = (ArcNode *)malloc(sizeof(ArcNode));//申請一個結點
g->adjvex=j;//連接結點
g->nextarc=p.vertices[k].firstarc;//連接結點
p.vertices[k].firstarc=g;//連接結點
}
}
}
int visit[20]={0};
void DFS(AdjList G,int v){
ArcNode *p;
visit[v]=1;
printf("%d ",v);
p=G[v].firstarc;
while(p!=NULL){
if(visit[p->adjvex]==0){//若w=p->adjvex 頂點未訪問,遞歸訪問它
DFS(G,p->adjvex);
vs[p->adjvex]=2;
}
p=p->nextarc;//p指向頂點v的下一個鄰接點
}
}
void BFS(AdjList G,int v)
{
ArcNode *p;
int Qu[20],front,rear;//定義循環隊列
int visited[20]={0};//使用數組模擬隊列
int w;
front=rear=0;//初始化隊列
printf("%d ",v);
visited[v]=1;
rear=(rear+1);
Qu[rear]=v;//v進隊
while(front!=rear){
front=(front+1);
w=Qu[front];//出隊並賦給w
p=G[w].firstarc;//找與頂點w鄰接的第一個頂點
while(p){
if(visited[p->adjvex]==0){//若當前頂點未被訪問
printf("%d ",p->adjvex);//訪問鄰接頂點
visited[p->adjvex]=1;
vs[p->adjvex]=2;
rear=(rear+1);//該頂點進隊
Qu[rear]=p->adjvex;
}
p=p->nextarc;
}
}
}
程序運行說明:
1.先輸入1或者2構造的圖的類型。
2.然後輸入頂點個數與弧的個數。
3.再依次輸入頂點的名稱例如:A B C
4.再輸入兩個頂點的名稱,構造圖。
5.最後輸入一個起始的點的序號。
最後一個比如你想輸入起始點爲A點,那你就輸入1.
有向圖與無向圖一樣輸入
有什麼問題歡迎留言,看到就會回答。。