数据结构——图的数组实现(邻接矩阵表示法)
来源:互联网 发布:java 多张图片 合成 编辑:程序博客网 时间:2024/03/29 13:56
Graph
基本知识这里不做介绍,可以去看<DSAA>或者<algorithm>.这里主要给出图的C语言实现。
首先我们要对图进行“抽象”,具体的找出关键能够描述图的关键信息,这里我简单的选取了vertex和edge,就是节点和节点所连同的路径。
下面是基于数组描述节点间关系的实现
graph.h
/************************************************************code file: graph.hcode writer: EOFcode date: 2014.11.22e-mail: jasonleaster@gmail.comcode description:This file is a header file for out test program.We abstract the data structure -- Graph here. And we alsodeclare some useful API to construct out naive graph :)************************************************************/#ifndef _GRAPH_H_#define _GRAPH_H_#include <stdio.h>#include <stdlib.h>#define CONNECTED 1#define DISCONNECTED 0#define SUCCESS 0#define FAILED -1struct graph{int num_vertex;int num_edge;char adjacent[0];};struct graph* init_graph(int vertex,int edge);void release_graph(struct graph* p_graph);int add_edge(struct graph* p_graph,char from_v,char to_v);int print_graph(struct graph* p_graph);#endif
init_graph.c 通过该函数实现graph的初始化
#include "graph.h"struct graph* init_graph(int vertex,int edge){if(vertex <=0 || edge <= 0){return NULL;}int temp = 0;struct graph* p_graph = NULL; p_graph = (struct graph*)malloc(sizeof(struct graph) + vertex*vertex);p_graph->num_vertex = vertex;p_graph->num_edge = edge;//initialize the adjacent relationshipfor(temp = 0;temp < vertex*vertex;temp++){p_graph->adjacent[temp] = 0;}return p_graph;}
add_edge.c 该函数为from_v 和 to_v 两个节点建立连通关系。
/************************************************************code file: add_edge.ccode writer: EOFcode date: 2014.11.22e-mail: jasonleaster@gmail.comcode description:This function will help us to add a new connectionbetween different vertex which is in the graph.*************************************************************/#include "graph.h"int add_edge(struct graph* p_graph,char from_v,char to_v){if(!p_graph || from_v < 0 || to_v < 0){return -1;}*((char*)(p_graph->adjacent) + from_v*(p_graph->num_vertex) + to_v) = 1;*((char*)(p_graph->adjacent) + to_v*(p_graph->num_vertex) + from_v) = 1;return 0;}
release_graph.c 这里最后释放图
/************************************************************code file: release_graph.ccode writer: EOFcode date: 2014.11.22e-mail: jasonleaster@gmail.comcode description:It's easy and convenient for us to call this API onceand free all the graph.*************************************************************/#include "graph.h"#include "graph.h"void release_graph(struct graph* p_graph){if(!p_graph){return ;}free(p_graph);}
最后是用于测试的主程序。
#include <stdio.h>#include "graph.h"int main(){struct graph* p_graph = NULL;FILE* fp = fopen("./text.txt","r+");if(!fp){printf("fopen() failed!\n");return 0;}int ret = 0;int vertex = 0;int edge = 0;int from_v = 0;int to_v = 0;fscanf(fp,"%d",&vertex);fscanf(fp,"%d",&edge);p_graph = init_graph(vertex,edge);int temp = 0;for(temp;temp < edge;temp++){/***I think it's necessary to check the returned value** of scanf() family.*/ret = fscanf(fp,"%d %d",&from_v,&to_v);if(ret != 2){break;}add_edge(p_graph,from_v,to_v);}print_graph(p_graph);release_graph(p_graph);fclose(fp);return 0;}
测试材料:文本文件text.txt
13130 54 30 19 126 45 40 211 129 100 67 89 115 3
我们的测试结果:
0 0
- 数据结构——图的数组实现(邻接矩阵表示法)
- 数据结构——图的邻接矩阵表示法
- 数据结构之---C语言实现图的数组(邻接矩阵)存储表示
- 数据结构(C实现)------- 图的邻接矩阵表示
- 数据结构(C实现)------- 图的邻接矩阵表示
- 数据结构 图的邻接矩阵表示法
- 数据结构——邻接矩阵表示的图的Floyd算法
- 数据结构——邻接矩阵表示的图的Dijkstra算法
- 数据结构——求邻接矩阵表示的图的关节点
- 数据结构,图的邻接矩阵表示
- 数据结构-图的邻接矩阵表示
- 图的数组表示(邻接矩阵)
- 详解数据结构——图之邻接矩阵表示法
- 数据结构与C语言实现(八)——图(上):邻接矩阵表示图
- 图--邻接矩阵(数组表示法)
- 数据结构的C实现_图_邻接矩阵表示
- 图算法-----图的邻接矩阵表示法(数组表示法)
- 图的存储表示--邻接矩阵法实现
- 定时器的开关
- 杂碎_chrome浏览器(google浏览器)无法登陆的问题!
- android sdk manager install package
- java基础一(介绍)
- 搭建和QQ技术乐园一模一样的教程网站
- 数据结构——图的数组实现(邻接矩阵表示法)
- Neatpad Overview
- 结构体数组做映射(写了个风格还算靠谱的程序, 欢迎大家找茬拍砖, 共同进步)
- json和jsonp区别
- JAVA Instrumentation 简介
- Loading a text file
- Scrollbars & Scrolling
- linux系统2440开发板域名解析问题
- 每日一道算法题:字符串反转