首页 > 其他分享 >实验九 图的创建与遍历

实验九 图的创建与遍历

时间:2023-11-15 13:46:22浏览次数:39  
标签:Node 遍历 int 创建 vertices dest 实验 graph array

实验时间: 第11周

实验目的: 掌握图的邻接矩阵、邻接表两种存储结构,能够实现在任意一种存储结构上的创建和遍历两种基本操作

实验要求:

1、认真阅读和掌握教材上和本实验相关内容和算法(见P161~170)。

2、上机将图的任意一种存储表示的创建和遍历(DFS和BFS至少实现一种)算法实现。

3、实现下面实验内容要求的功能,并能够进行简单的输入输出验证。

实验内容:

1、 图的创建部分

编程实现图的任意一种存储表示的创建算法,要求能够进行简单的输入输出验证。

2、 图的遍历操作部分

编程实现图的遍历操作,至少实现图的深度优先搜索和广度优先搜索两种遍历算法中的一种,要求能够进行简单的输入输出验证。

图的创建与遍历(使用邻接矩阵)

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

#define MAX_VERTICES 100

// Representation of Graph using Adjacency Matrix
typedef struct {
    int V; // Number of vertices
    int **matrix; // Adjacency matrix
} GraphAdjMatrix;

GraphAdjMatrix* createGraphAdjMatrix(int vertices) {
    GraphAdjMatrix* graph = (GraphAdjMatrix*)malloc(sizeof(GraphAdjMatrix));
    graph->V = vertices;

    // Allocate memory for adjacency matrix
    graph->matrix = (int**)malloc(vertices * sizeof(int*));
    for (int i = 0; i < vertices; ++i) {
        graph->matrix[i] = (int*)calloc(vertices, sizeof(int));
    }

    return graph;
}

void addEdgeAdjMatrix(GraphAdjMatrix* graph, int src, int dest) {
    // Assuming it's an undirected graph, assigning 1 for both directions
    graph->matrix[src][dest] = 1;
    graph->matrix[dest][src] = 1;
}

void printGraphAdjMatrix(GraphAdjMatrix* graph) {
    printf("Adjacency Matrix:\n");
    for (int i = 0; i < graph->V; ++i) {
        for (int j = 0; j < graph->V; ++j) {
            printf("%d ", graph->matrix[i][j]);
        }
        printf("\n");
    }
}

void freeGraphAdjMatrix(GraphAdjMatrix* graph) {
    for (int i = 0; i < graph->V; ++i) {
        free(graph->matrix[i]);
    }
    free(graph->matrix);
    free(graph);
}

int main() {
    int V = 4; // Number of vertices
    GraphAdjMatrix* g = createGraphAdjMatrix(V);

    addEdgeAdjMatrix(g, 0, 1);
    addEdgeAdjMatrix(g, 0, 2);
    addEdgeAdjMatrix(g, 1, 2);
    addEdgeAdjMatrix(g, 2, 3);

    printGraphAdjMatrix(g);

    freeGraphAdjMatrix(g);

    return 0;
}

结果演示:

图的创建与遍历(使用邻接表)

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

// Node to store adjacent vertices in Adjacency List
typedef struct Node {
    int dest;
    struct Node* next;
} Node;

// Adjacency List representation of Graph
typedef struct {
    int V; // Number of vertices
    Node** array; // Array of adjacency lists
} GraphAdjList;

Node* createNode(int dest) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->dest = dest;
    newNode->next = NULL;
    return newNode;
}

GraphAdjList* createGraphAdjList(int vertices) {
    GraphAdjList* graph = (GraphAdjList*)malloc(sizeof(GraphAdjList));
    graph->V = vertices;

    // Create an array of adjacency lists
    graph->array = (Node**)malloc(vertices * sizeof(Node*));
    for (int i = 0; i < vertices; ++i) {
        graph->array[i] = NULL;
    }

    return graph;
}

void addEdgeAdjList(GraphAdjList* graph, int src, int dest) {
    // Add edge from src to dest
    Node* newNode = createNode(dest);
    newNode->next = graph->array[src];
    graph->array[src] = newNode;

    // For undirected graph, uncomment the lines below
    /*
    newNode = createNode(src);
    newNode->next = graph->array[dest];
    graph->array[dest] = newNode;
    */
}

void printGraphAdjList(GraphAdjList* graph) {
    printf("Adjacency List:\n");
    for (int i = 0; i < graph->V; ++i) {
        Node* temp = graph->array[i];
        printf("Adjacency list of vertex %d: ", i);
        while (temp != NULL) {
            printf("%d -> ", temp->dest);
            temp = temp->next;
        }
        printf("NULL\n");
    }
}

void freeGraphAdjList(GraphAdjList* graph) {
    for (int i = 0; i < graph->V; ++i) {
        Node* current = graph->array[i];
        while (current != NULL) {
            Node* next = current->next;
            free(current);
            current = next;
        }
    }
    free(graph->array);
    free(graph);
}

int main() {
    int V = 4; // Number of vertices
    GraphAdjList* g = createGraphAdjList(V);

    addEdgeAdjList(g, 0, 1);
    addEdgeAdjList(g, 0, 2);
    addEdgeAdjList(g, 1, 2);
    addEdgeAdjList(g, 2, 3);

    printGraphAdjList(g);

    freeGraphAdjList(g);

    return 0;
}

结果演示:

标签:Node,遍历,int,创建,vertices,dest,实验,graph,array
From: https://www.cnblogs.com/NorthPoet/p/17833633.html

相关文章

  • Vue实验记录
    webpack安装首先下载node.jshttps://nodejs.org/en下载完成后进行安装,直接下一步就可以安装完成后,在cmd中查看是否安装成功然后安装webpack安装脚手架创建项目选择第二个创建完成后的效果进入项目并运行在学习通中下载源码,把源码按照项目格式放到创建好的项目......
  • 【随手记】mybatis动态sql foreach遍历List<Map>问题
    使用mybatis时经常需要在xml里写动态sql,发现foreach标签使用的问题foreach标签使用当Mapper传参是List<Map<String,Object>集合的形式时,不能直接使用参数名,会找不到对应的参数。list类型的参数会特殊处理封装在map中,map的key就叫list所以collection属性值只能是"list"//m......
  • Grafana新手教程-实现仪表盘创建和告警推送
    前言最近在使用Grafana的时候,发现Grafana功能比想象中要强大,除了配合Prometheus使用之外,他自身都可以做很多事情,可视化和监控平台,还可以直接根据用户自定义的告警规则完成告警和进行各种通知。于是在深入学习了一段时间之后,整理成此博文。温馨提示,本文约1.3w字,几十张示例图片并......
  • 实验八. urllib模块、requests模块+BeautifulSoup模块使用、Feapder框架
    一、实验目标:熟悉模块的的用法,练习编写爬虫二、实验要求:编写代码,完成功能三、实验内容:(1)使用urllib模块或request模块读取网页内容,并利用BeautifulSoup模块进行内容解析,编写爬虫从http://www.cae.cn/cae/html/main/col48/column_48_1.html爬取中国工程院院士信息模......
  • 【转载】按照文件名创建文件夹,并把文件移动到对应文件夹中
    @echooff&cd/d"%~dp0"&modeconlines=5000rem按照文件名创建文件夹,并把文件移动到对应文件夹中set#=Anyquestion&set_=WX&set$=Q&set/az=0x53b7e0b4title%#%+%$%%$%/%_%%z%for/f"delims="%%ain('dir/a-d-h/b')do(if......
  • Map遍历删除元素的几种方法
    2哥 :3妹,今天是周末,又不用上班,干嘛看着不开心的样子啊?3妹:你没有看昨天的新闻吗,昨天国家痛失了两位重要人物。2哥:哎,看到了,生老病死,也是没有办法。唯愿逝者安息,生者坚强!我们能做的,就是更加坚强,好好学习,建设祖国!3妹:好吧。2哥:还记得我们之前学习的:list遍历时删除元素的方法 吗,那如......
  • 设计模式实验12实验13
    外观模式 packagetest12;publicclassMemory{publicvoidcheck(){System.out.println("内存自检");}}packagetest12;publicclassHardDisk{publicvoidread(){System.out.println("硬盘读取");}}packa......
  • f通过new关键词进行函数调用,之后无论如何都会返回一个与F关联的普通对象(因为不是通过
    varF=function(){};Object.prototype.a=function(){};Function.prototype.b=function(){};varf=newF();关于这段代码的描述,正确的是:Af能取到a,但取不到bBf能取到a,bCF能取到b,不能取到aDF能取到a,不能取到b正确答案:A网上有一道美团外卖的面试题是这样的:Function......
  • 实验4
    任务1.1#include<stdio.h>#defineN4voidtest1(){ inta[N]={1,9,8,4}; inti; printf("sizeof(a)=%d\n",sizeof(a)); for(i=0;i<N;++i) printf("%p:%d\n",&a[i],a[i]); printf("a=%p\n",a);}......
  • 创建自己的https证书(转)
    使用mkcert工具创建证书1、下载mkcert工具,下载地址如下:​​ ​mkcert工具下载​​百度下载:链接:​ ​https://pan.baidu.com/s/10ym5W91g612LDk3t9isFGQ ​​提取码:12342、解压后运行https本地证书生成工具.bat(文件上点鼠标右键,以管理员身份运行) 生成的证书在C:\Users\cx......