c/c++ 图的创建(二维数组法)
c/c++ 图的创建(二维数组法)
图的概念
- 图由点和线组成
- 知道了图中有多少个点,和哪些点之间有线,就可以把一张图描绘出来
- 点之间的线,分有方向和无方向
创建图
创建图,实际就是创建出节点,和节点之间的线,节点和节点之间的线,可以用二维数组,也就是矩阵来表示。


下面的代码实现了上面的图的创建
graph_mtx.h
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#define Default_vertex_size 10
#define T char//dai biao ding dian de lei xing
typedef struct GraphMtx{
int MaxVertices;//zui da ding dian shu liang]
int NumVertices;//shi ji ding dian shu liang
int NumEdges;//bian de shu lian
T* VerticesList;//ding dian list
int** Edge;//bian de lian jie xin xi, bu shi 0 jiu shi 1
}GraphMtx;
//chu shi hua tu
void init_graph(GraphMtx* gm);
//打印二维数组
void show_graph(GraphMtx* gm);
//插入顶点
void insert_vertex(GraphMtx* gm, T v);
//添加顶点间的线
void insert_edge(GraphMtx* gm, T v1, T v2);
//删除顶点
void remove_vertex(GraphMtx* gm, T v);
//删除顶点间的线
void remove_edge(GraphMtx* gm, T v1, T v2);
//摧毁图
void destroy_graph(GraphMtx* gm);
#endif
graph_mtx.c
#include "graph_mtx.h"
void init_graph(GraphMtx* gm){
gm->MaxVertices = Default_vertex_size;
gm->NumEdges = gm->NumVertices = 0;
//kai pi ding dian de nei cun kong jian
gm->VerticesList = (T*)malloc(sizeof(T) * (gm->MaxVertices));
assert(NULL != gm->VerticesList);
//创建二维数组
//让一个int的二级指针,指向一个有8个int一级指针的数组
//开辟一个能存放gm->MaxVertices个int一级指针的内存空间
gm->Edge = (int**)malloc(sizeof(int*) * (gm->MaxVertices));
assert(NULL != gm->Edge);
//开辟gm->MaxVertices组,能存放gm->MaxVertices个int的内存空间
for(int i = 0; i < gm->MaxVertices; ++i){
gm->Edge[i] = (int*)malloc(sizeof(int) * gm->MaxVertices);
}
//初始化二维数组
//让每个顶点之间的边的关系都为不相连的
for(int i = 0; i < gm->MaxVertices; ++i){
for(int j = 0; j < gm->MaxVertices; ++j){
gm->Edge[i][j] = 0;
}
}
}
//打印二维数组
void show_graph(GraphMtx* gm){
printf(" ");
for(int i = 0; i < gm->NumVertices; ++i){
printf("%c ", gm->VerticesList[i]);
}
printf("\n");
for(int i = 0; i < gm->NumVertices; ++i){
//在行首,打印出顶点的名字
printf("%c:", gm->VerticesList[i]);
for(int j = 0; j < gm->NumVertices; ++j){
printf("%d ", gm->Edge[i][j]);
}
printf("\n");
}
printf("\n");
}
//插入顶点
void insert_vertex(GraphMtx* gm, T v){
//顶点空间已满,不能再插入顶点了
if(gm->NumVertices >= gm->MaxVertices){
return;
}
gm->VerticesList[gm->NumVertices++] = v;
}
//添加顶点间的线
void insert_edge(GraphMtx* gm, T v1, T v2){
if(v1 == v2)return;
int j = -1;
int k = -1;
//查找2个顶点的下标
for(int i = 0; i < gm->NumVertices; ++i){
if(gm->VerticesList[i] == v1){
j = i;
continue;
}
if(gm->VerticesList[i] == v2){
k = i;
continue;
}
}
//说明找到顶点的下标了
if(j != -1 && k != -1){
//因为是无方向,所以更新2个值
gm->Edge[j][k] = gm->Edge[k][j] = 1;
//边数加1
gm->NumEdges++;
}
}
graph_mtxmain.c
#include "graph_mtx.h"
int main(){
GraphMtx gm;
//初始化图
init_graph(&gm);
//插入顶点
insert_vertex(&gm, 'A');
insert_vertex(&gm, 'B');
insert_vertex(&gm, 'C');
insert_vertex(&gm, 'D');
insert_vertex(&gm, 'E');
insert_edge(&gm, 'A', 'B');
insert_edge(&gm, 'A', 'D');
insert_edge(&gm, 'B', 'C');
insert_edge(&gm, 'B', 'E');
insert_edge(&gm, 'C', 'E');
insert_edge(&gm, 'C', 'D');
//顶点和顶点之间的连线关系
show_graph(&gm);
}
c/c++ 图的创建(二维数组法)的更多相关文章
- c/c++ 图相关的函数(二维数组法)
c/c++ 图相关的函数(二维数组法) 遍历图 插入顶点 添加顶点间的线 删除顶点 删除顶点间的线 摧毁图 取得与v顶点有连线的第一个顶点 取得与v1顶点,v1顶点之后的v2顶点的之后的有连线的第一个 ...
- C语言 动态创建二维数组
/*C语言 如何动态创建二维数组 转化为一维数组申请数组,创建和释放都比较简单 */ #include <stdlib.h> #include <stdio.h> #inclu ...
- Python创建二维数组(关于list的一个小坑)
0.目录 1.遇到的问题 2.创建二维数组的办法 3.1 直接创建法 3.2 列表生成式法 3.3 使用模块numpy创建 1.遇到的问题 今天写Python代码的时候遇到了一个大坑,差点就耽误我交作 ...
- C#中创建二维数组,使用[][]和[,]的区别
C#中,我们在创建二维数组的时候,一般使用arr[][]的形式,例如 int[][] aInt = new int[2][]; 但声明二维数组还有一种方法,是使用arr[,]的形式.两者有什么区别呢? ...
- c++ 用new创建二维数组~创建指针数组【转】
#include <iostream> using namespace std; void main() { //用new创建一个二维数组,有两种方法,是等价的 //一: ] = ][]; ...
- c++用vector创建二维数组
1 vector二维数组的创建和初始化 std::vector <int> vec(10,90); //将10个一维动态数组初始为90std::vector<std::vector& ...
- stl vector创建二维数组
vector<vector<); for (auto it = v.begin(); it != v.end(); it++) { ; (*it).reserve();//预留空间为5,但 ...
- c++中创建二维数组的几种方法
一.用new申请内存空间 int **dp=new int*[n];//动态申请二维数组nxm ;i<n;++i){ dp[i]=new int[m]; } 二.用malloc申请内存空间 ; ...
- typescript 创建二维数组
private mouseView: Mouse private mouseArray: Array<Array<any>> = new Array<Array<a ...
随机推荐
- netty源码解解析(4.0)-14 Channel NIO实现:读取数据
本章分析Nio Channel的数据读取功能的实现. Channel读取数据需要Channel和ChannelHandler配合使用,netty设计数据读取功能包括三个要素:Channel, Eve ...
- Jmeter连接Redis,获取Redis数据集
Redis(REmote DIctionary Server)是一个开源的内存数据结构存储,用作数据库,缓存和消息代理. 本博文是分享jmeter怎么连接使用Redis数据库. 安装Redis数据集J ...
- Linux核心命令
Linux核心命令 strace(查看系统调用的一个过程) 例:strace cat /test.txt netstat perf top pidstat mpstat dstat vmstat sl ...
- iOS多线程(上)——GCD详解(上)
GCD(Grand central Dispatch)是Apple开发的一个多核编程的较新的解决方法.它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统.下面我讲讲述关于GCD的点,通篇读完 ...
- 【转载】PhpStudy修改网站根目录
phpStudy是一个PHP调试环境的程序集成包.该程序包集成最新的Apache+PHP+MySQL+phpMyAdmin+ZendOptimizer,一次性安装,无须配置即可使用,是非常方便.好用的 ...
- 关于Facebook和Google+授权登录
实际中遇到需要Facebook和Google+等第三方授权登录自己的Web应用(可能还有Android和IOS的手机应用),本质上都是JS SDK的官方应用.这时候不得不去他们官方查看文档. 注:一下 ...
- LeNet训练MNIST
jupyter notebook: https://github.com/Penn000/NN/blob/master/notebook/LeNet/LeNet.ipynb LeNet训练MNIST ...
- JS使用cookie实现只出现一次的广告代码效果
我们上网经常会遇到第一次需要登录而之后不用再登录的网站的情况,其实是运用了Cookie 存储 web 页面的用户信息,Cookie 以名/值对形式存储,当浏览器从服务器上请求 web 页面时, 属于该 ...
- canvas学习笔记之2d画布基础的实现
一. Canvas是啥 < canvas > 是一个可以使用脚本(通常是js)来绘图的HTML元素 < canvas > 最早由Apple引入WebKit,用于Mac OS X ...
- C#弹出窗体、C#导出Excel、C#数据展示框、C#弹出框
1.new 一个窗体,然后设置窗体属性. 2.添加数据展示控件,显示数据. 3.添加按钮控件,调用导出Excel代码,实现导出Excel功能. using System; using System.C ...