鱼C论坛

 找回密码
 立即注册
查看: 416|回复: 2

算法设计题

[复制链接]
发表于 2023-11-12 00:30:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
算法设计题:假设图G采用邻接表存储,设计一个算法,输出图G中从顶点u到v的所有简单路径。(用C语言解决)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 00:30:25 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define MAX_VERTICES 100

  4. typedef struct Node {
  5.     int vertex;
  6.     struct Node* next;
  7. } Node;

  8. typedef struct {
  9.     Node* head;
  10. } AdjList[MAX_VERTICES];

  11. void addEdge(AdjList adjList, int src, int dest) {
  12.     Node* newNode = (Node*)malloc(sizeof(Node));
  13.     newNode->vertex = dest;
  14.     newNode->next = adjList[src].head;
  15.     adjList[src].head = newNode;
  16. }

  17. void printAllPathsUtil(AdjList adjList, int u, int v, int visited[], int path[], int pathIndex) {
  18.     visited[u] = 1;
  19.     path[pathIndex] = u;
  20.     pathIndex++;

  21.     if (u == v) {
  22.         for (int i = 0; i < pathIndex - 1; i++) {
  23.             printf("%d -> ", path[ i]);
  24.         }
  25.         printf("%d\n", path[pathIndex - 1]);
  26.     } else {
  27.         Node* current = adjList[u].head;
  28.         while (current != NULL) {
  29.             if (!visited[current->vertex]) {
  30.                 printAllPathsUtil(adjList, current->vertex, v, visited, path, pathIndex);
  31.             }
  32.             current = current->next;
  33.         }
  34.     }

  35.     visited[u] = 0;
  36.     pathIndex--;
  37. }

  38. void printAllPaths(AdjList adjList, int u, int v, int numVertices) {
  39.     int visited[MAX_VERTICES] = {0};
  40.     int path[MAX_VERTICES];
  41.     int pathIndex = 0;

  42.     printAllPathsUtil(adjList, u, v, visited, path, pathIndex);
  43. }

  44. int main() {
  45.     int numVertices = 4;
  46.     AdjList adjList;
  47.     for (int i = 0; i < numVertices; i++) {
  48.         adjList[ i].head = NULL;
  49.     }

  50.     addEdge(adjList, 0, 1);
  51.     addEdge(adjList, 0, 2);
  52.     addEdge(adjList, 1, 2);
  53.     addEdge(adjList, 2, 0);
  54.     addEdge(adjList, 2, 3);
  55.     addEdge(adjList, 3, 3);

  56.     int u = 2, v = 3;
  57.     printf("从顶点%d到顶点%d的所有简单路径为:\n", u, v);
  58.     printAllPaths(adjList, u, v, numVertices);

  59.     return 0;
  60. }
复制代码


这段代码实现了一个从顶点u到v的所有简单路径的算法。首先定义了一个邻接表结构,然后使用addEdge函数向邻接表中添加边。接着使用printAllPathsUtil函数递归地找到从顶点u到顶点v的所有路径,并将其打印出来。最后在main函数中构建了一个图,并调用printAllPaths函数进行测试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-11-12 00:40:22 | 显示全部楼层
本帖最后由 sususususu 于 2023-11-12 00:43 编辑
isdkz 发表于 2023-11-12 00:30
这段代码实现了一个从顶点u到v的所有简单路径的算法。首先定义了一个邻接表结构,然后使用addEdge函数 ...


为什么报错呢?
anonymous type with no linkage used to declare function 'void printAllPaths(<anonymous struct>*, int, int, int)' with linkage [-fpermissive]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 00:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表