邻接矩阵(C语言)(图)(icoding

邻接矩阵

试在邻接矩阵存储结构上实现图的基本操作 matrix_insert_vertex 和matrix_insert_arc,相关定义如下:

`

```c
typedef int VertexType;

typedef enum{
    DG, UDG
}GraphType;

typedef struct{
    VertexType vertex[MAX_VERTEX_NUM]; //顶点向量
    int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //邻接矩阵
    int vexnum, arcnum;   //图的当前顶点数和弧数
    GraphType type;     //图的种类标志
}MatrixGraph;

int matrix_locate_vertex(MatrixGraph MG, VertexType vex); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1
bool matrix_insert_vertex(MatrixGraph G, VertexType v);
bool matrix_insert_arc(MatrixGraph *G, VertexType v, VertexType w);



//当成功插入顶点或边时,函数返回true,否则(如顶点或边已存在、插入边时顶点v或w不存在)返回false。

答案

#include "graph.h" // 请不要删除,否则检查不通过
#include 

bool matrix_insert_vertex(MatrixGraph* G, VertexType v)
{
    if(G->vexnum==MAX_VERTEX_NUM)
    return false;
    if (matrix_locate_vertex(G, v) == -1) {
        G->vertex[G->vexnum++] = v;
        for (int i = 0; i < G->vexnum; i++)//这不是赋权图所以要清空被随机分配的数据
            G->arcs[i][G->vexnum - 1] = G->arcs[G->vexnum - 1][i] = 0;
        return true;
    } else
        return false;
}
bool matrix_insert_arc(MatrixGraph* G, VertexType v, VertexType w)
{
    int v1 = matrix_locate_vertex(G, v), w1 = matrix_locate_vertex(G, w);
    if (v1 == -1 || w1 == -1 || G->arcs[v1][w1] == 1) //假设有向图里v1是弧尾,w1是弧头
        return false;
    if (G->type == DG) {//加边
        G->arcnum++;
        G->arcs[v1][w1] = 1;
        return true;
    } else {
        G->arcnum++;
        G->arcs[v1][w1] = G->arcs[w1][v1] = 1;
        return true;
    }
}

收获

题目分析
  • matrix_locate_vertex(MatrixGraph MG, VertexType
    vex)用来定位vex在数组中的下表,所以用结构变量MG就可以。
  • 而 matrix_insert_vertex和matrix_insert_arc都需要利用指向结构变量G的指针作为函数的参数从而允许函数修改原变量
    就是“只读”和修改的区别
    所以matrix_insert_vertex中的G应该是指针。
用于调试的完整代码
#include
#include
#include
#define MAX_VERTEX_NUM 100

typedef int VertexType;

typedef enum {
	DG, UDG
}GraphType;


typedef struct {
	VertexType vertex[MAX_VERTEX_NUM]; //顶点向量
	int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //邻接矩阵
	int vexnum, arcnum;   //图的当前顶点数和弧数
	GraphType type;     //图的种类标志
}MatrixGraph;
int matrix_locate_vertex(MatrixGraph MG, VertexType vex) {
	for (int i = 0;i < MG.vexnum;i++) {
			if (MG. vertex[i] == vex)
				return i;
	}
	return -1;
};
bool matrix_insert_vertex(MatrixGraph* G, VertexType v)
{
	if (G->vexnum == MAX_VERTEX_NUM)
		return false;
	if (matrix_locate_vertex(G, v) == -1) {
		G->vertex[G->vexnum++] = v;
		for (int i = 0; i < G->vexnum; i++)
			G->arcs[i][G->vexnum - 1] = G->arcs[G->vexnum - 1][i] = 0;
		return true;
	}
	else
		return false;
}
bool matrix_insert_arc(MatrixGraph* G, VertexType v, VertexType w)
{
	int v1 = matrix_locate_vertex(G, v), w1 = matrix_locate_vertex(G, w);
	if (v1 == -1 || w1 == -1 || G->arcs[v1][w1] == 1) //假设有向图里v1是弧尾,w1是弧头
		return false;
	if (G->type == DG) {
		G->arcnum++;
		G->arcs[v1][w1] = 1;
		return true;
	}
	else {
		G->arcnum++;
		G->arcs[v1][w1] = G->arcs[w1][v1] = 1;
		return true;
	}
}

MatrixGraph A;
int main(void) {
	freopen("in.txt", "r", stdin);
	int type;
	scanf("%d", &type);
	//if (type == 0) A.type = GraphType::DG;
	//else A.type = GraphType::UDG;
	A.type = GraphType(type);
	scanf("%d %d", &A.vexnum, &A.arcnum);
	for (int i = 0;i < A.vexnum;i++) {//输入顶点
		scanf("%d", &A.vertex[i]);
	}//也可以直接调用matrix_insert_vertex函数
	for (int i = 0;i<A.arcnum;i++) {
		int a, b;
		scanf("%d %d",&a,&b);
		int tar_a = matrix_locate_vertex(A, a);
		int tar_b = matrix_locate_vertex(A, b);
		A.arcs[tar_a][tar_b] = 1;
	}//在scanf后也可以直接调用matrix_insert_arc函数
	/*
	matrix_insert_arc(&A,a,b);
	*/
	int v,m,n;
	scanf("%d", &v);
	matrix_insert_vertex(&A, v);
	scanf("%d %d",&m,&n);
	matrix_insert_arc(&A, m, n);
	return 0;
}
要点分析

1.枚举类型的应用

typedef enum {
	DG, UDG
}GraphType;
  • 其为枚举类型,而在系统内部,C语言会把枚举变量和常量作为整数来处理. 默认情况下,DG为0,UDG为1.

  • 读取枚举中的某个枚举常量的方法
    (以将枚举GraphType中的枚举常量DG赋值给A.type为例

  • 枚举中的强制类型转换

 int type;
	scanf("%d", &type);
	/*if (type == 0) A.type =    GraphType::DG;
	else A.type = GraphType::UDG;*/一种正确方法	   
	//A.type = type; 错误 ,不能将"int"型的值分配到GrapfType类型的实体
      A.type = GraphType(type);

2.变量生命期

MatrixGraph A ;

在main函数外面时,A是一个全局变量,默认值为零。(假如定义结构变量的时候没有同时初始化元素)
在main函数内部时,A是一个随机变量,默认值为随机值。
3.关于matrix_locate_vertex函数

int matrix_locate_vertex(MatrixGraph MG, VertexType vex) {
/*VertexType vertex[MAX_VERTEX_NUM]; **错误代码**
int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
for (int i = 0;i < MG.vexnum;i++) {
int flag = 0;
while (!flag) {
if (MG.vertex[i++] == vex)
return i - 1;
}
}
return -1;
};*/

while语句和for循环在此处作用重复,而且如果vex在数组中没有对应的下标,该函数失效。

你可能感兴趣的:(邻接矩阵(C语言)(图)(icoding)