基于邻接矩阵存储结构实现有向网络的典型操作(初始化、销毁、增加顶点、删除顶点、增加弧、删除弧、查 找一个顶点、修改一个顶点、查找一个弧、修改一个弧、判空、判满、图中顶点个数、图中的弧个数、深度优先遍历、 广度优先遍历),测试和调试程序。
时间: 2023-09-16 14:07:49 浏览: 47
基于邻接矩阵的有向图存储结构可以使用二维数组来表示,其中数组的行和列分别表示顶点和边。
下面是典型操作的实现代码:
```cpp
#include <iostream>
#include <vector>
class DirectedGraph {
private:
int maxVertices; // 最大顶点数
std::vector<std::vector<int>> adjacencyMatrix; // 邻接矩阵
public:
DirectedGraph(int maxVertices) {
this->maxVertices = max
相关问题
用C语言基于邻接矩阵存储结构实现有向网络的典型操作(初始化、销毁、增加顶点、删除顶点、增加弧、删除弧、查 找一个顶点、修改一个顶点、查找一个弧、修改一个弧、判空、判满、图中顶点个数、图中的弧个数、深度优先遍历、 广度优先遍历),测试和调试程序。
以下是基于邻接矩阵存储结构实现有向图的典型操作的C语言代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define MAX_VERTICES 100
typedef struct {
int vertices[MAX_VERTICES]; // 存储顶点
int arcs[MAX_VERTICES][MAX_VERTICES]; // 存储边
int vertexCount; // 顶点个数
int arcCount; // 边个数
} DirectedGraph;
// 初始化有向图
void initGraph(DirectedGraph* graph) {
graph->vertexCount = 0;
graph->arcCount = 0;
int i, j;
for (i = 0; i < MAX_VERTICES; i++) {
graph->vertices[i] = 0;
for (j = 0; j < MAX_VERTICES; j++) {
graph->arcs[i][j] = 0;
}
}
}
// 销毁有向图
void destroyGraph(DirectedGraph* graph) {
graph->vertexCount = 0;
graph->arcCount = 0;
}
// 增加顶点
bool addVertex(DirectedGraph* graph, int vertex) {
if (graph->vertexCount >= MAX_VERTICES) {
return false;
}
graph->vertices[graph->vertexCount++] = vertex;
return true;
}
// 删除顶点
bool removeVertex(DirectedGraph* graph, int vertex) {
int i, j;
for (i = 0; i < graph->vertexCount; i++) {
if (graph->vertices[i] == vertex) {
for (j = i; j < graph->vertexCount - 1; j++) {
graph->vertices[j] = graph->vertices[j + 1];
}
graph->vertexCount--;
for (j = 0; j < graph->vertexCount; j++) {
graph->arcs[i][j] = graph->arcs[i + 1][j];
}
for (j = 0; j < graph->vertexCount; j++) {
graph->arcs[j][i] = graph->arcs[j][i + 1];
}
return true;
}
}
return false;
}
// 增加弧
bool addArc(DirectedGraph* graph, int startVertex, int endVertex) {
int i, startIndex = -1, endIndex = -1;
for (i = 0; i < graph->vertexCount; i++) {
if (graph->vertices[i] == startVertex) {
startIndex = i;
}
if (graph->vertices[i] == endVertex) {
endIndex = i;
}
}
if (startIndex != -1 && endIndex != -1) {
if (graph->arcs[startIndex][endIndex] == 0) {
graph->arcs[startIndex][endIndex] = 1;
graph->arcCount++;
return true;
}
}
return false;
}
// 删除弧
bool removeArc(DirectedGraph* graph, int startVertex, int endVertex) {
int i, startIndex = -1, endIndex = -1;
for (i = 0; i < graph->vertexCount; i++) {
if (graph->vertices[i] == startVertex) {
startIndex = i;
}
if (graph->vertices[i] == endVertex) {
endIndex = i;
}
}
if (startIndex != -1 && endIndex != -1) {
if (graph->arcs[startIndex][endIndex] == 1) {
graph->arcs[startIndex][endIndex] = 0;
graph->arcCount--;
return true;
}
}
return false;
}
// 查找顶点
int findVertex(DirectedGraph* graph, int vertex) {
int i;
for (i = 0; i < graph->vertexCount; i++) {
if (graph->vertices[i] == vertex) {
return i;
}
}
return -1;
}
// 修改顶点
bool modifyVertex(DirectedGraph* graph, int oldVertex, int newVertex) {
int index = findVertex(graph, oldVertex);
if (index != -1) {
graph->vertices[index] = newVertex;
return true;
}
return false;
}
// 查找弧
bool findArc(DirectedGraph* graph, int startVertex, int endVertex) {
int startIndex = findVertex(graph, startVertex);
int endIndex = findVertex(graph, endVertex);
if (startIndex != -1 && endIndex != -1) {
if (graph->arcs[startIndex][endIndex] == 1) {
return true;
}
}
return false;
}
// 修改弧
bool modifyArc(DirectedGraph* graph, int startVertex, int endVertex, int newArc) {
int startIndex = findVertex(graph, startVertex);
int endIndex = findVertex(graph, endVertex);
if (startIndex != -1 && endIndex != -1) {
graph->arcs[startIndex][endIndex] = newArc;
return true;
}
return false;
}
// 判断图是否为空
bool isEmpty(DirectedGraph* graph) {
return graph->vertexCount == 0;
}
// 判断图是否已满
bool isFull(DirectedGraph* graph) {
return graph->vertexCount == MAX_VERTICES;
}
// 获取图中顶点个数
int getVertexCount(DirectedGraph* graph) {
return graph->vertexCount;
}
基于邻接矩阵存储结构实现有向网络的典型操作(初始化、销毁、增加顶点、删除顶点、增加弧、删除弧、查 找一个顶点、修改一个顶点、查找一个弧、修改一个弧、判空、判满、图中顶点个数、图中的弧个数、图的输出、深 度优先遍历、广度优先遍历),用c语言实现
以下是使用C语言实现基于邻接矩阵存储结构的有向网络的典型操作的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 100
typedef struct {
int matrix[MAX_VERTICES][MAX_VERTICES];
int numVertices;
} DirectedGraph;
void initializeGraph(DirectedGraph* graph, int numVertices) {
graph->numVertices = numVertices;
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
graph->matrix[i][j] = 0;
}
}
}
void destroyGraph(DirectedGraph* graph) {
graph->numVertices = 0;
}
void addVertex(DirectedGraph* graph) {
if (graph->numVertices < MAX_VERTICES) {
graph->numVertices++;
} else {
printf("Graph is full.\n");
}
}
void deleteVertex(DirectedGraph* graph, int vertex) {
if (vertex >= 0 && vertex < graph->numVertices) {
for (int i = 0; i < graph->numVertices; i++) {
graph->matrix[i][vertex] = 0;
graph->matrix[vertex][i] = 0;
}
for (int i = vertex; i < graph->numVertices - 1; i++) {
for (int j = 0; j < graph->numVertices; j++) {
graph->matrix[j][i] = graph->matrix[j][i + 1];
}
}
for (int i = vertex; i < graph->numVertices - 1; i++) {
for (int j = 0; j < graph->numVertices; j++) {
graph->matrix[i][j] = graph->matrix[i + 1][j];
}
}
graph->numVertices--;
} else {
printf("Invalid vertex.\n");
}
}
void addArc(DirectedGraph* graph, int src, int dest) {
if (src >= 0 && src < graph->numVertices && dest >= 0 && dest < graph->numVertices) {
graph->matrix[src][dest] = 1;
} else {
printf("Invalid vertices.\n");
}
}
void deleteArc(DirectedGraph* graph, int src, int dest) {
if (src >= 0 && src < graph->numVertices && dest >= 0 && dest < graph->numVertices) {
graph->matrix[src][dest] = 0;
} else {
printf("Invalid vertices.\n");
}
}
int findVertex(DirectedGraph* graph, int vertex) {
if (vertex >= 0 && vertex < graph->numVertices) {
return 1;
} else {
return 0;
}
}
void modifyVertex(DirectedGraph* graph, int vertex, int newValue) {
if (vertex >= 0 && vertex < graph->numVertices) {
// Modify the vertex value here
} else {
printf("Invalid vertex.\n");
}
}
int findArc(DirectedGraph* graph, int src, int dest) {
if (src >= 0 && src < graph->numVertices && dest >= 0 && dest < graph->numVertices) {
if (graph->matrix[src][dest] == 1) {
return 1;
} else {
return 0;
}
} else {
printf("Invalid vertices.\n");
return 0;
}
}
void modifyArc(DirectedGraph* graph, int src, int dest, int newValue) {
if (src >= 0 && src < graph->numVertices && dest >= 0 && dest < graph->numVertices) {
// Modify the arc value here
} else {
printf("Invalid vertices.\n");
}
}
int isEmpty(DirectedGraph* graph) {
if (graph->numVertices == 0) {
return 1;
} else {
return 0;
}
}
int isFull(DirectedGraph* graph) {
if (graph->numVertices == MAX_VERTICES) {
return 1;
} else {
return 0;
}
}
int getNumVertices(Di