您的位置:首页 > 编程语言 > C语言/C++

C语言实现图的邻接表的创建以及深度搜索和广度搜索

2017-11-28 21:44 696 查看
#include <stdio.h>
#include <stdlib.h>

#define MAX_VALUE 10

typedef struct EdgeNode{//边顶点
int index;//该顶点下标
struct EdgeNode *next;//存储下一个边顶点
}EdgeNode;

typedef struct HeadNode{//表顶点
char data;
EdgeNode *edgeNode;
}HeadNode,AdjacencyList[MAX_VALUE];

typedef struct Graph{//图
AdjacencyList lists;
int vexNum;//当前顶点数
int edgeNum;//当前边数
}Graph;

//找到该元素所在的下标
int locate(Graph *graph,char ch){
int i ;
for(i = 0;i<graph->vexNum;i++){
if(graph->lists[i].data==ch)
return i;
}
return -1;
}
//构造邻接表图
Graph *createGraph(){
Graph *graph;
char ch;
graph = (Graph*)malloc(sizeof(Graph));
graph->edgeNum = 0;
graph->vexNum = 0;
printf("输入顶点回车退出输入\n");
while( (ch=getchar() ) != '\n'){
//构造顶点表
graph->lists[graph->vexNum].data = ch;
graph->lists[graph->vexNum].edgeNode = NULL;
graph->vexNum++;
printf("输入顶点回车退出输入\n");
fflush(stdin);
}
int i;
printf("当前输入的顶点数如下:\n");
for(i = 0;i<graph->vexNum;i++){
printf("%c  ",graph->lists[i].data);
}
//构造边
printf ("\n");
printf("输入边数\n");
scanf("%d",&graph->edgeNum);
EdgeNode *node;
for(i = 0;i<graph->edgeNum;i++){
printf("输入两个要连接的顶点的值\n");
fflush(stdin);
char valueA, valueB;
scanf("%c %c",&valueA,&valueB);   		//读入<vA,vB>的弧
int indexA = locate(graph,valueA);  	//查找该顶点所在的下标
int indexB = locate(graph,valueB);
//始终在头节点后插入新元素,即链表的头插法
//有向图
node = (EdgeNode*)malloc(sizeof(EdgeNode));  //申请空间
node->index = indexB;
node->next = graph->lists[indexA].edgeNode;  //将新表结点node插入到顶点 vA
graph->lists[indexA].edgeNode = node;
//        若为无向图,则另外一个表顶点也要插入这条路径的边顶点
//        node = (EdgeNode*)malloc(sizeof(EdgeNode));
//        node->index = indexA;
//        node->next = graph->lists[indexB].edgeNode;
//        graph->lists[indexB].edgeNode = node;
}
return graph;
}
//输出矩阵
void outputGraph(Graph *graph){
//依次输入每个表顶点的链表
int i ;
EdgeNode *temp;
for(i = 0;i<graph->vexNum;i++){
printf("第%d个表顶点:",i);
printf("%c  ",graph->lists[i].data);//表头顶点的值
temp = graph->lists[i].edgeNode;
while(temp){
//对该链表进行遍历,输出与 vA 相关的顶点所在的下标
printf ("%d   ", temp->index);
temp = temp->next;
}
printf("\n");
}
}

//图元素操作函数
void work(char ch){
printf("%c  ",ch);
}

//广度优先搜索
void BFSGraph(Graph *graph){
int i ;
int visited[graph->vexNum];
//    对访问标志数组的初始化
for(i = 0;i<graph->vexNum;i++){
visited[i] = 0;
}
EdgeNode *temp;
for(i = 0;i<graph->vexNum;i++){
if(visited[i]==0){
work(graph->lists[i].data);//访问表头节点
visited[i]=1;//设置访问标签
}
//         对该链表进行遍历访问
temp = graph->lists[i].edgeNode;
while(temp){
if(visited[temp->index]==0){//如果该顶点未访问过
work(graph->lists[temp->index].data);
visited[temp->index] = 1;//设置访问标签
}
temp = temp->next;
}
}
}

//深度搜索
void DFS(Graph *graph,int i,int *visited){
EdgeNode *temp;
work(graph-
9d3a
>lists[i].data);//访问表头节点
visited[i]=1;//设置访问标签
temp = graph->lists[i].edgeNode;
while(temp){
if(visited[temp->index]==0){//如果该节点之前未访问过
DFS(graph,temp->index,visited);//深入下一层
}
temp = temp->next;
}
}

//深度优先搜索
void DFSGraph(Graph *graph){
int i ;
int visited[graph->vexNum];
for(i = 0;i<graph->vexNum;i++){
visited[i] = 0;
}
for(i = 0;i<graph->vexNum;i++){
if(visited[i]==0){
DFS(graph,i,visited);
}
}
}

int main()
{
Graph * p = createGraph();
outputGraph(p);
printf("广度优先搜索遍历输出:\n");
BFSGraph(p);
printf("\n深度优先搜索遍历输出:\n");
DFSGraph(p);
return 0;
}

/*
输入:
1
2
3
4

4
1 2
1 3
3 4
4 1
*/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: