图(无向)的邻接矩阵、邻接表存储结构

邻接矩阵存储结构(+深度遍历)

//邻接矩阵及深度遍历
#include<iostream>
#include<stdbool.h>
#define MaxInt 999//表示最大值 正无穷
#define MVNum 100//最大顶点数
#define OK 1

typedef char VerTexType;
typedef int ArcType;
using namespace std;

typedef struct
{
    VerTexType vexs[MVNum];//顶点表
    ArcType arcs[MVNum][MVNum];//邻接矩阵
    int vexnum, arcnum;//图的当前点数和边数
}AMGraph;

int CreateUDN(AMGraph &G)
{
    int i, j, k,w;
    cout << "输入总顶点数,总边数:" << endl;
    cin >> G.vexnum >> G.arcnum;//输入总顶点数,总边数
    for (i = 0; i < G.vexnum; ++i)
    {
        cout << "请输入各顶点的值:" << endl;
        cin >> G.vexs[i];//输入各顶点信息
    }
    for (i = 0; i < G.vexnum; ++i)
    {
        for (j = 0; j < G.vexnum; ++j)
        {
            G.arcs[i][j] = MaxInt;//都置为极大值MaxInt
        }
    }
    for (k = 0; k < G.vexnum; ++k)//构造邻接矩阵
    {
        cout << "请输入<vi,vj>的下标i,j和权值" << endl;
        cin >> i >> j >> w;//输入一条边依附的顶点和权值
        G.arcs[i][j] = w;//<v1,v2>边的权值置为w
        G.arcs[j][i] = G.arcs[i][j];//置<v1,v2>的对称边<v2,v1>的权值为w
    }
    return OK;
}

bool visited[MVNum];
//邻接矩阵 深度优先遍历
void DFS_AM(AMGraph G, int v)
{
    int w;
    cout << v;
    visited[v] = true;
    for (w = 0; w < G.vexnum; w++)
    {
        if ((G.arcs[v][w] != 0) && (!visited[w]))
        {
            DFS_AM(G, w);
        }
    }
}

int main()
{
    AMGraph G;
    CreateUDN(G);
    for (int i = 0; i < G.vexnum; i++)
    {
        for (int j = 0; j < G.vexnum; j++)
        {
            cout<< G.arcs[i][j]<<' ';
        }
        cout << endl;
    }

    cout << "深度优先遍历:" << endl;
    DFS_AM(G, 1);
    cout << endl;

    return 0;
}

邻接表存储结构


//邻接表
#include<iostream>
#define MVNum 100//最大顶点数
#define OK 1
using namespace std;

typedef struct ArcNode//边结点
{
    int adjvex;//该边所指向的顶点的位置
    struct ArcNode* nextarc;//指向下一条边的指针
    char info;//和边相关的信息
}ArcNode;

typedef struct VNode  //顶点信息
{
    char data;
    ArcNode* firstarc;//指向第一条依附该顶点的边的指针
}VNode,AdjList[MVNum]; //AdjList表示邻接表类型

typedef struct  //邻接表
{
    AdjList vertices;
    int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;

int CreateUDG(ALGraph& G)
{
    int i, j, k;
    cout << "输入总顶点数 总边数:" << endl;
    cin >> G.vexnum >> G.arcnum;//输入总顶点数 总边数
    cout << "输入各顶点值:" << endl;
    for (i = 0; i < G.vexnum; ++i)//输入各点,构造表头结点
    {
        cin >> G.vertices[i].data;//输入顶点值
        G.vertices[i].firstarc = NULL;//初始化表头结点的指针域为NULL
    }
    for (k = 0; k < G.vexnum; ++k)
    {
        cout << "请输入<vi,vj>的下标i,j" << endl;
        cin >> i >> j ;//输入一条边依附的顶点
        ArcNode* p1 = new ArcNode;
        p1->adjvex = j;
        p1->nextarc = G.vertices[i].firstarc;
        G.vertices[i].firstarc = p1;

        ArcNode* p2 = new ArcNode;
        p2->adjvex = i;
        p2->nextarc = G.vertices[j].firstarc;
        G.vertices[j].firstarc = p2;
    }
    return OK;
}

int main()
{
    ALGraph G;
    ArcNode* p;
    CreateUDG(G);

    cout << "依次输出各顶点邻接表: " << endl;

    ArcNode* temp = NULL;
    for (int i = 0; i <G.vexnum ; ++i)
    {
        temp = G.vertices[i].firstarc;
        while (temp)
        {
            cout << temp->adjvex << " ";
            temp = temp->nextarc;
        }
        cout << endl;

    }
    return 0;
}

邻接矩阵存储结构运行截图:
在这里插入图片描述

邻接表存储结构运行截图:
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以的,以下是用C语言代码输出无向带权邻接矩阵邻接表的程序: ``` #include <stdio.h> #define MAX_VERTICES 100 typedef struct { int edges[MAX_VERTICES][MAX_VERTICES]; // 邻接矩阵 int degree[MAX_VERTICES]; // 存储每个节点的度 int nvertices; // 节点数 } Graph; void init_graph(Graph* graph, int n) { graph->nvertices = n; for (int i = 0; i < n; i++) { graph->degree[i] = 0; for (int j = 0; j < n; j++) { graph->edges[i][j] = 0; // 初始化所有边的权重为0 } } } void add_edge(Graph* graph, int u, int v, int w) { graph->edges[u][v] = w; // 设置边的权重 graph->edges[v][u] = w; // 无向图,两个方向都要设置 graph->degree[u]++; // 节点u的度+1 graph->degree[v]++; // 节点v的度+1 } void print_matrix(Graph* graph) { printf("邻接矩阵:\n"); for (int i = 0; i < graph->nvertices; i++) { for (int j = 0; j < graph->nvertices; j++) { printf("%2d ", graph->edges[i][j]); } printf("\n"); } } void print_list(Graph* graph) { printf("邻接表:\n"); for (int i = 0; i < graph->nvertices; i++) { printf("%d -> ", i); for (int j = 0; j < graph->nvertices; j++) { if (graph->edges[i][j] > 0) { printf("%d(%d) ", j, graph->edges[i][j]); } } printf("\n"); } } int main() { Graph graph; int n = 6; init_graph(&graph, n); add_edge(&graph, 0, 1, 2); add_edge(&graph, 0, 2, 3); add_edge(&graph, 1, 3, 1); add_edge(&graph, 1, 4, 4); add_edge(&graph, 2, 4, 5); add_edge(&graph, 3, 5, 6); add_edge(&graph, 4, 5, 7); print_matrix(&graph); print_list(&graph); return 0; } ``` 在上面的程序,我们定义了一个 Graph 结构体,用于存储无向带权邻接矩阵邻接表。在初始化时,我们将节点数 n 作为参数传入 init_graph 函数,然后将每个边的权重都初始化为0,节点的度初始化为0。在添加边时,我们需要传入两个节点的编号和边的权重,然后将对应的两个节点的度+1,同时将对应的两个节点之间的边的权重设置为我们传入的值。最后,我们可以分别使用 print_matrix 和 print_list 函数输出邻接矩阵邻接表。 如果你有任何问题,欢迎继续向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值