117.info
人生若只如初见

C语言拓扑排序代码怎么写

下面是一个用C语言实现拓扑排序的示例代码:

#include 
#include 

#define MAXVERTICES 100

struct Graph {
    int vertices;
    int edges;
    int adjacencyMatrix[MAXVERTICES][MAXVERTICES];
};

void initializeGraph(struct Graph* graph, int vertices) {
    graph->vertices = vertices;
    graph->edges = 0;

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

void addEdge(struct Graph* graph, int startVertex, int endVertex) {
    graph->adjacencyMatrix[startVertex][endVertex] = 1;
    graph->edges++;
}

void topologicalSortUtil(struct Graph* graph, int vertex, bool visited[], int stack[], int* stackIndex) {
    visited[vertex] = true;

    for (int i = 0; i < graph->vertices; i++) {
        if (graph->adjacencyMatrix[vertex][i] == 1 && !visited[i]) {
            topologicalSortUtil(graph, i, visited, stack, stackIndex);
        }
    }

    stack[(*stackIndex)++] = vertex;
}

void topologicalSort(struct Graph* graph) {
    bool visited[MAXVERTICES] = {false};
    int stack[MAXVERTICES];
    int stackIndex = 0;

    for (int i = 0; i < graph->vertices; i++) {
        if (!visited[i]) {
            topologicalSortUtil(graph, i, visited, stack, &stackIndex);
        }
    }

    printf("Topological Sort: ");

    while (stackIndex > 0) {
        printf("%d ", stack[--stackIndex]);
    }

    printf("\n");
}

int main() {
    struct Graph graph;
    int vertices, edges, startVertex, endVertex;

    printf("Enter the number of vertices in the graph: ");
    scanf("%d", &vertices);

    initializeGraph(&graph, vertices);

    printf("Enter the number of edges in the graph: ");
    scanf("%d", &edges);

    for (int i = 0; i < edges; i++) {
        printf("Enter the start and end vertex of edge %d: ", i + 1);
        scanf("%d %d", &startVertex, &endVertex);
        addEdge(&graph, startVertex, endVertex);
    }

    topologicalSort(&graph);

    return 0;
}

这个代码中,首先定义了一个结构体Graph来表示图,其中vertices表示顶点的个数,edges表示边的个数,adjacencyMatrix表示邻接矩阵。

然后定义了几个函数来操作图,包括initializeGraph用于初始化图,addEdge用于添加边,topologicalSortUtil用于递归实现拓扑排序,topologicalSort用于调用topologicalSortUtil进行拓扑排序。

main函数中,首先获取用户输入的顶点个数和边的个数,然后通过addEdge函数添加边,最后调用topologicalSort函数进行拓扑排序。

注意:该示例代码中使用邻接矩阵来表示图,适用于边数较少的情况。如果边数较多,推荐使用邻接表来表示图。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe652AzsLAwFTB1I.html

推荐文章

  • c语言goto用法是什么

    在C语言中,goto语句用于从一个位置跳转到另一个位置。它可以用于跳出循环、条件语句或者函数。goto语句的语法如下:
    goto label;
    ...
    label: //...

  • goto在c语言中的运用方法是什么

    goto 语句在 C 语言中允许程序从一个位置跳转到另一个位置。然而,在实际编程中,过度使用 goto 语句通常被认为是一种不好的编程实践,因为它可能导致代码结构混...

  • c语言堆和栈的区别是什么

    C语言中的堆和栈是两种不同的内存分配方式。 栈(Stack):栈是由编译器自动分配和释放的内存区域,用于存储局部变量和函数调用的上下文信息。栈的特点是先进后出...

  • C语言结构体数组怎么定义和使用

    在C语言中,可以通过定义结构体数组来存储多个具有相同数据类型的结构体对象。下面是定义和使用结构体数组的示例代码:
    #include // 定义一个结构体类型

  • maven怎么配置使用本地仓库

    要配置Maven使用本地仓库,您需要按照以下步骤进行操作: 确保您已经安装了Maven,并且已经设置好了Maven的环境变量。
    打开settings.xml文件,该文件位于Ma...

  • maven导入jar包的方法是什么

    在Maven项目中导入jar包有以下几种方法: 通过Maven Central Repository导入:在pom.xml文件中的dependencies标签中添加对应的jar包坐标,Maven会自动从Maven Ce...

  • linux怎么调整swap大小

    要调整Linux上的swap大小,可以按照以下步骤进行操作: 首先,检查当前系统上的swap分区或文件的大小和使用情况。可以使用以下命令查看swap的情况:
    swapon...

  • mac怎么重新分配windows空间

    在Mac上重新分配Windows空间的步骤如下: 打开Mac的“应用程序”文件夹,然后进入“实用工具”文件夹。
    打开“磁盘工具”应用程序。
    在左侧的侧边栏中...