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 */
相关文章推荐
- 基于C++ STL图的邻接表表示及深度、广度搜索实现
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- 图的邻接表(数组实现)建立,广度和深度搜索。。
- 图-深度优先广度优先,以及邻接表的创建
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- [置顶] 图:图的邻接表创建、深度优先遍历和广度优先遍历代码实现
- 用邻接表实现深度和广度搜索
- C语言以邻接表为存储结构的图的构造以及广度优先,深度优先遍历
- 图的深度搜索完整实现(邻接表,DFS)
- lucene全文搜索之四:创建索引搜索器、6种文档搜索器实现以及搜索结果分析(结合IKAnalyzer分词器的搜索器)基于lucene5.5.3
- 邻接表--创建图、删除图、深度优先搜索、广度优先搜索---代码
- 图的邻接表表示法及深度搜索与广度搜索
- C语言数据结构与算法之深度、广度优先搜索
- 22.基于 邻接表 表示的 深度优先搜索dfs 和 广度优先搜索bfs
- 图---邻接表的深度搜索与广度搜索测试一
- 图的邻接表存储、创建、深度优先遍历、广度优先遍历
- 图基本算法 图搜索基于邻接表的(广度优先、深度优先)
- 基于邻接表的无向图的深度广度遍历实现
- C语言实现图的遍历之深度优先搜索实例
- C++ 无向图 邻接表创建 广度遍历 深度遍历