C语言以其高效、灵活和接近硬件的特性,在系统编程和算法开发中占据着重要地位。在图算法领域,C语言同样展现出了其独特的优势。本文将深入探讨C语言在图算法中的应用,通过代码示例揭示如何用C语言编织复杂网络关系。

图的基本概念

在C语言中,图通常通过邻接矩阵或邻接表来表示。邻接矩阵是一种二维数组,用于表示图中任意两个顶点之间是否存在边。而邻接表则是一种更灵活的数据结构,它使用链表来存储每个顶点的邻接顶点。

邻接矩阵

#define MAX_VERTICES 100
#define INF INT_MAX

int graph[MAX_VERTICES][MAX_VERTICES];

void initializeGraph() {
    for (int i = 0; i < MAX_VERTICES; i++) {
        for (int j = 0; j < MAX_VERTICES; j++) {
            if (i == j) {
                graph[i][j] = 0;
            } else {
                graph[i][j] = INF;
            }
        }
    }
}

邻接表

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int vertex;
    struct Node* next;
} Node;

typedef struct Graph {
    int numVertices;
    Node** adjLists;
} Graph;

Graph* createGraph(int vertices) {
    Graph* graph = (Graph*)malloc(sizeof(Graph));
    graph->numVertices = vertices;

    graph->adjLists = (Node**)malloc(vertices * sizeof(Node*));

    for (int i = 0; i < vertices; i++) {
        graph->adjLists[i] = NULL;
    }

    return graph;
}

void addEdge(Graph* graph, int src, int dest) {
    // Add edge from src to dest
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->vertex = dest;
    newNode->next = graph->adjLists[src];
    graph->adjLists[src] = newNode;

    // Add edge from dest to src for undirected graph
    newNode = (Node*)malloc(sizeof(Node));
    newNode->vertex = src;
    newNode->next = graph->adjLists[dest];
    graph->adjLists[dest] = newNode;
}

图的遍历算法

图的遍历算法包括深度优先搜索(DFS)和广度优先搜索(BFS)。这两种算法在C语言中都有多种实现方式。

深度优先搜索(DFS)

#include <stdbool.h>

void DFS(Graph* graph, int vertex, bool visited[]) {
    visited[vertex] = true;
    printf("%d ", vertex);

    Node* adjList = graph->adjLists[vertex];
    Node* temp = adjList;

    while (temp != NULL) {
        int connectedVertex = temp->vertex;

        if (!visited[connectedVertex]) {
            DFS(graph, connectedVertex, visited);
        }
        temp = temp->next;
    }
}

广度优先搜索(BFS)

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_QUEUE_SIZE 100

typedef struct Queue {
    int front;
    int rear;
    int items[MAX_QUEUE_SIZE];
} Queue;

void enqueue(Queue* queue, int value) {
    if (queue->rear == MAX_QUEUE_SIZE - 1) {
        return;
    }

    queue->rear++;
    queue->items[queue->rear] = value;
}

int dequeue(Queue* queue) {
    if (queue->front == queue->rear) {
        return -1;
    }

    int item = queue->items[queue->front];
    queue->front++;

    return item;
}

bool isEmpty(Queue* queue) {
    return (queue->front > queue->rear);
}

void BFS(Graph* graph, int startVertex) {
    bool visited[MAX_VERTICES];
    for (int i = 0; i < graph->numVertices; i++) {
        visited[i] = false;
    }

    Queue queue;
    queue.front = queue.rear = -1;
    enqueue(&queue, startVertex);

    while (!isEmpty(&queue)) {
        int currentVertex = dequeue(&queue);
        printf("%d ", currentVertex);

        Node* adjList = graph->adjLists[currentVertex];
        Node* temp = adjList;

        while (temp) {
            int adjVertex = temp->vertex;

            if (!visited[adjVertex]) {
                visited[adjVertex] = true;
                enqueue(&queue, adjVertex);
            }
            temp = temp->next;
        }
    }
}

总结

C语言在图算法领域具有广泛的应用,它能够高效地处理图数据,并实现各种复杂的图算法。通过上述示例,我们可以看到如何使用C语言构建图数据结构,并实现基本的图遍历算法。掌握这些基本概念和技巧,将有助于你在图算法领域深入探索和实践。