1.无向图 邻接矩阵
#include <stdio.h>
#include <stdbool.h>
#define MaxVertexNum 100
typedef struct {
int Vex[MaxVertexNum];
int Edge[MaxVertexNum][MaxVertexNum];
int vexnum, arcnum;
} MGraph;
void initGraph(MGraph* graph, int numVertices) {
graph->vexnum = numVertices;
graph->arcnum = 0;
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
graph->Edge[i][j] = 0;
}
}
}
void insertEdge(MGraph* graph, int start, int end) {
if (start >= 0 && start < graph->vexnum && end >= 0 && end < graph->vexnum) {
if (graph->Edge[start][end] == 0) {
graph->Edge[start][end] = 1;
graph->Edge[end][start] = 1;
graph->arcnum++;
}
}
}
void removeEdge(MGraph* graph, int start, int end) {
if (start >= 0 && start < graph->vexnum && end >= 0 && end < graph->vexnum) {
if (graph->Edge[start][end] == 1) {
graph->Edge[start][end] = 0;
graph->Edge[end][start] = 0;
graph->arcnum--;
}
}
}
bool hasEdge(MGraph* graph, int start, int end) {
if (start >= 0 && start < graph->vexnum && end >= 0 && end < graph->vexnum) {
return graph->Edge[start][end] == 1;
}
return false;
}
int firstNeighbor(MGraph* graph, int x) {
if (x >= 0 && x < graph->vexnum) {
for (int i = 0; i < graph->vexnum; i++) {
if (graph->Edge[x][i] == 1) {
return i;
}
}
}
return -1;
}
void neighborsExceptY(MGraph* graph, int x, int y) {
if (x >= 0 && x < graph->vexnum && y >= 0 && y < graph->vexnum) {
for (int i = 0; i < graph->vexnum; i++) {
if (graph->Edge[x][i] == 1 && i != y) {
printf("%d ", i);
}
}
printf("\n");
}
}
int main() {
MGraph graph;
int numVertices = 5;
initGraph(&graph, numVertices);
insertEdge(&graph, 0, 1);
insertEdge(&graph, 0, 2);
insertEdge(&graph, 1, 3);
printf("Number of vertices: %d\n", graph.vexnum);
printf("Number of edges: %d\n", graph.arcnum);
if (hasEdge(&graph, 0, 1)) {
printf("There is an edge between 0 and 1.\n");
} else {
printf("There is no edge between 0 and 1.\n");
}
printf("First neighbor of vertex 0: %d\n", firstNeighbor(&graph, 0));
printf("Neighbors of vertex 0 except vertex 1: ");
neighborsExceptY(&graph, 0, 1);
removeEdge(&graph, 0, 1);
return 0;
}
2.无向图 邻接表
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_VERTICES 100
struct Node {
int vertex;
struct Node* next;
};
struct Graph {
struct Node* adjList[MAX_VERTICES];
};
int numVertices = 0;
struct Graph* createGraph() {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
for (int i = 0; i < MAX_VERTICES; i++) {
graph->adjList[i] = NULL;
}
return graph;
}
void insertVertex(struct Graph* graph, int vertex) {
if (numVertices < MAX_VERTICES) {
numVertices++;
}
}
void deleteVertex(struct Graph* graph, int vertex) {
if (vertex < numVertices) {
for (int i = 0; i < numVertices; i++) {
struct Node* current = graph->adjList[i];
struct Node* prev = NULL;
while (current != NULL) {
if (current->vertex == vertex) {
if (prev == NULL) {
graph->adjList[i] = current->next;
} else {
prev->next = current->next;
}
free(current);
break;
}
prev = current;
current = current->next;
}
}
for (int i = vertex; i < numVertices - 1; i++) {
graph->adjList[i] = graph->adjList[i + 1];
}
numVertices--;
}
}
void addEdge(struct Graph* graph, int vertex1, int vertex2) {
if (vertex1 < numVertices && vertex2 < numVertices) {
struct Node* newNode1 = (struct Node*)malloc(sizeof(struct Node));
newNode1->vertex = vertex2;
newNode1->next = graph->adjList[vertex1];
graph->adjList[vertex1] = newNode1;
struct Node* newNode2 = (struct Node*)malloc(sizeof(struct Node));
newNode2->vertex = vertex1;
newNode2->next = graph->adjList[vertex2];
graph->adjList[vertex2] = newNode2;
}
}
void removeEdge(struct Graph* graph, int vertex1, int vertex2) {
if (vertex1 < numVertices && vertex2 < numVertices) {
struct Node* current = graph->adjList[vertex1];
struct Node* prev = NULL;
while (current != NULL) {
if (current->vertex == vertex2) {
if (prev == NULL) {
graph->adjList[vertex1] = current->next;
} else {
prev->next = current->next;
}
free(current);
break;
}
prev = current;
current = current->next;
}
current = graph->adjList[vertex2];
prev = NULL;
while (current != NULL) {
if (current->vertex == vertex1) {
if (prev == NULL) {
graph->adjList[vertex2] = current->next;
} else {
prev->next = current->next;
}
free(current);
break;
}
prev = current;
current = current->next;
}
}
}
bool hasEdge(struct Graph* graph, int vertex1, int vertex2) {
if (vertex1 < numVertices && vertex2 < numVertices) {
struct Node* current = graph->adjList[vertex1];
while (current != NULL) {
if (current->vertex == vertex2) {
return true;
}
current = current->next;
}
}
return false;
}
int firstNeighbor(struct Graph* graph, int vertex) {
if (vertex < numVertices) {
struct Node* first = graph->adjList[vertex];
if (first != NULL) {
return first->vertex;
}
}
return -1;
}
int nextNeighbor(struct Graph* graph, int vertex, int neighbor) {
if (vertex < numVertices) {
struct Node* current = graph->adjList[vertex];
while (current != NULL) {
if (current->vertex != neighbor) {
return current->vertex;
}
current = current->next;
}
}
return -1;
}
int main() {
struct Graph* graph = createGraph();
insertVertex(graph, 0);
insertVertex(graph, 1);
addEdge(graph, 0, 1);
if (hasEdge(graph, 0, 1)) {
printf("There is an edge between 0 and 1.\n");
} else {
printf("There is no edge between 0 and 1.\n");
}
deleteVertex(graph, 1);
return 0;
}
3.有向图 邻接矩阵
#include <stdio.h>
#include <stdbool.h>
#define MAX_VERTICES 100
int graph[MAX_VERTICES][MAX_VERTICES] = {0};
int numVertices = 0;
void insertVertex(int vertex) {
if (numVertices < MAX_VERTICES) {
numVertices++;
}
}
void deleteVertex(int vertex) {
if (vertex < numVertices) {
for (int i = vertex; i < numVertices - 1; i++) {
for (int j = 0; j < numVertices; j++) {
graph[i][j] = graph[i + 1][j];
}
}
for (int i = 0; i < numVertices; i++) {
for (int j = vertex; j < numVertices - 1; j++) {
graph[i][j] = graph[i][j + 1];
}
}
numVertices--;
}
}
void addEdge(int start, int end) {
if (start < numVertices && end < numVertices) {
graph[start][end] = 1;
}
}
void removeEdge(int start, int end) {
if (start < numVertices && end < numVertices) {
graph[start][end] = 0;
}
}
bool hasEdge(int start, int end) {
if (start < numVertices && end < numVertices) {
return graph[start][end] != 0;
}
return false;
}
int firstNeighbor(int vertex) {
if (vertex < numVertices) {
for (int i = 0; i < numVertices; i++) {
if (graph[vertex][i] != 0) {
return i;
}
}
}
return -1;
}
int nextNeighbor(int vertex, int neighbor) {
if (vertex < numVertices && neighbor < numVertices) {
for (int i = neighbor + 1; i < numVertices; i++) {
if (graph[vertex][i] != 0) {
return i;
}
}
}
return -1;
}
int main() {
insertVertex(0);
insertVertex(1);
addEdge(0, 1);
if (hasEdge(0, 1)) {
printf("There is an edge between 0 and 1.\n");
} else {
printf("There is no edge between 0 and 1.\n");
}
deleteVertex(1);
return 0;
}
4.有向图 邻接表
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxVertexNum 100
typedef struct ArcNode {
int adjvex;
struct ArcNode* next;
} ArcNode;
typedef struct VNode {
int data;
ArcNode* first;
} VNode, AdjList[MaxVertexNum];
typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
void initGraph(ALGraph* graph, int numVertices) {
graph->vexnum = numVertices;
graph->arcnum = 0;
for (int i = 0; i < numVertices; i++) {
graph->vertices[i].data = i;
graph->vertices[i].first = NULL;
}
}
void addEdge(ALGraph* graph, int start, int end) {
if (start >= 0 && start < graph->vexnum && end >= 0 && end < graph->vexnum) {
ArcNode* newNode = (ArcNode*)malloc(sizeof(ArcNode));
newNode->adjvex = end;
newNode->next = graph->vertices[start].first;
graph->vertices[start].first = newNode;
graph->arcnum++;
}
}
void removeEdge(ALGraph* graph, int start, int end) {
if (start >= 0 && start < graph->vexnum) {
ArcNode* current = graph->vertices[start].first;
ArcNode* prev = NULL;
while (current != NULL) {
if (current->adjvex == end) {
if (prev == NULL) {
graph->vertices[start].first = current->next;
} else {
prev->next = current->next;
}
free(current);
graph->arcnum--;
break;
}
prev = current;
current = current->next;
}
}
}
bool hasEdge(ALGraph* graph, int start, int end) {
if (start >= 0 && start < graph->vexnum) {
ArcNode* current = graph->vertices[start].first;
while (current != NULL) {
if (current->adjvex == end) {
return true;
}
current = current->next;
}
}
return false;
}
int main() {
ALGraph graph;
int numVertices = 5;
initGraph(&graph, numVertices);
addEdge(&graph, 0, 1);
addEdge(&graph, 0, 2);
addEdge(&graph, 1, 3);
printf("Number of vertices: %d\n", graph.vexnum);
printf("Number of edges: %d\n", graph.arcnum);
if (hasEdge(&graph, 0, 1)) {
printf("There is an edge from 0 to 1.\n");
} else {
printf("There is no edge from 0 to 1.\n");
}
removeEdge(&graph, 0, 1);
return 0;
}
5.无向图 邻接表 广度优先搜索
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MaxVertexNum 100
#define QUEUE_SIZE MaxVertexNum
typedef struct {
int Vex[MaxVertexNum];
int Edge[MaxVertexNum][MaxVertexNum];
int vexnum, arcnum;
} MGraph;
void initGraph(MGraph* graph, int numVertices) {
graph->vexnum = numVertices;
graph->arcnum = 0;
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
graph->Edge[i][j] = 0;
}
}
}
void addEdge(MGraph* graph, int vertex1, int vertex2) {
if (vertex1 >= 0 && vertex1 < graph->vexnum && vertex2 >= 0 && vertex2 < graph->vexnum) {
graph->Edge[vertex1][vertex2] = 1;
graph->Edge[vertex2][vertex1] = 1;
graph->arcnum++;
}
}
typedef struct {
int data[QUEUE_SIZE];
int front, rear;
} Queue;
void initQueue(Queue* q) {
q->front = q->rear = -1;
}
void enqueue(Queue* q, int value) {
if (q->rear == QUEUE_SIZE - 1) {
printf("Queue is full.\n");
return;
}
if (q->front == -1) {
q->front = 0;
}
q->rear++;
q->data[q->rear] = value;
}
int dequeue(Queue* q) {
if (q->front == -1) {
printf("Queue is empty.\n");
return -1;
}
int value = q->data[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
}
return value;
}
bool visited[MaxVertexNum] = { false };
Queue queue;
void BFS(MGraph* graph, int start) {
visited[start] = true;
printf("%d ", start);
enqueue(&queue, start);
while (queue.front != -1) {
int current = dequeue(&queue);
for (int i = 0; i < graph->vexnum; i++) {
if (graph->Edge[current][i] == 1 && !visited[i]) {
visited[i] = true;
printf("%d ", i);
enqueue(&queue, i);
}
}
}
}
void BFSTraverse(MGraph* graph) {
for (int i = 0; i < graph->vexnum; i++) {
if (!visited[i]) {
printf("从节点%d开始:",i);
BFS(graph, i);
printf("\n");
}
}
}
int main() {
MGraph graph;
int numVertices = 6;
initGraph(&graph, numVertices);
addEdge(&graph, 0, 1);
addEdge(&graph, 0, 2);
addEdge(&graph, 1, 3);
addEdge(&graph, 4, 5);
initQueue(&queue);
BFSTraverse(&graph);
return 0;
}
6.有向图 邻接矩阵 深度优先搜索
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MaxVertexNum 100
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
} ArcNode;
typedef struct VNode {
ArcNode* firstarc;
} VNode;
typedef struct {
VNode adjlist[MaxVertexNum];
int vexnum, arcnum;
} ALGraph;
void InitGraph(ALGraph* graph, int numVertices) {
graph->vexnum = numVertices;
graph->arcnum = 0;
for (int i = 0; i < numVertices; i++) {
graph->adjlist[i].firstarc = NULL;
}
}
void AddEdge(ALGraph* graph, int start, int end) {
if (start >= 0 && start < graph->vexnum && end >= 0 && end < graph->vexnum) {
ArcNode* newArc = (ArcNode*)malloc(sizeof(ArcNode));
newArc->adjvex = end;
newArc->nextarc = graph->adjlist[start].firstarc;
graph->adjlist[start].firstarc = newArc;
graph->arcnum++;
}
}
bool visited[MaxVertexNum] = { false };
void DFS(ALGraph* graph, int v) {
visited[v] = true;
printf("%d ", v);
ArcNode* currentArc = graph->adjlist[v].firstarc;
while (currentArc != NULL) {
int adjvex = currentArc->adjvex;
if (!visited[adjvex]) {
DFS(graph, adjvex);
}
currentArc = currentArc->nextarc;
}
}
void DFSTraverse(ALGraph* graph) {
for (int i = 0; i < graph->vexnum; i++) {
visited[i] = false;
}
for (int i = 0; i < graph->vexnum; i++) {
if (!visited[i]) {
printf("从节点 %d 开始: ", i);
DFS(graph, i);
printf("\n");
}
}
}
int main() {
ALGraph graph;
int numVertices = 6;
InitGraph(&graph, numVertices);
AddEdge(&graph, 0, 1);
AddEdge(&graph, 1, 2);
AddEdge(&graph, 2, 0);
AddEdge(&graph, 3, 4);
AddEdge(&graph, 4, 5);
DFSTraverse(&graph);
return 0;
}