<图>邻接矩阵(有向)

图的存储结构
  1.顺序存储
    邻接矩阵adjacency_matrix表示顶点之间的关系
        缺点:迅速判断两个节点之间是否有边,方便计算各个顶点之间的度
        优点:不方便图中顶点的增加个删除,不方便访问各个邻接点
    需要两个数组来储存图。
        一维数组储存顶点信息
        二维数组储存顶点和顶点之间的邻接关系即边 notice:
          a -- b 
          |  / |    4个顶点,5条边
          d -- c
          vex[] = a b c d
                         a b c d
          Edge[i][j] = a 0 1 0 1
                       b 1 0 1 1
                       c 0 1 0 1
                       d 1 1 1 0
            1.对称阵 Edge[i][j] == Edge[j][i]
            2.第i行或第i列所有非0元素的个数 == 顶点的度
        -------------------------------------
            b
          ↗   ↖
         a  →  c  5个顶点,7条边
         ↓  ↙  ↓
         e  ←  d
         Vex[] = a b c d e 
                        a b c d e 
         Egde[i][j] = a 0 1 1 0 1
                      b 0 0 1 0 0
                      c 0 0 0 1 1
                      d 0 0 0 0 1
                      e 0 0 0 0 0 
         1.不一定是对称
         2.第i行非零元素的个数 == 第i个顶点的出度
         3.第i列非零元素的个数 == 第i个顶点的入度

        --------------------------------------
             5
          a  →  b  
        2 ↑     ↓ 4
          d  ←  c
             3
        Vex[] = a b c d
                        a  b  c  d
        Egde[i][j] = a -1  5 -1 -1
                     b -1 -1  4 -1
                     c -1 -1 -1  3
                     d  2 -1 -1 -1 

  2.链式存储
    邻接表
#include "directed_mat.h"

int main()
{
	struct AMG_Graph* ud_graph;

	ud_graph = Create_AMG_Graph();

    Show_AMG_Graph(ud_graph);

    return 0;
}

directed_mat.cpp


#include <iostream>
#include "directed_mat.h"
using namespace std;


struct AMG_Graph* Create_AMG_Graph(void)
{
	int i, j;
    char u, v;
	struct AMG_Graph* graph;

	// 申请内存空间,结构体多大申请多大,强制类型转换
	graph = (struct AMG_Graph *)malloc(sizeof(struct AMG_Graph));

	cout << "请输入顶点的个数: ";
	cin >> graph->vex_num;

	cout << "请输入边的个数: ";
	cin >> graph->edge_num;


		
	cout << "请输入顶点信息: " << endl;
	for(i = 0; i < graph->vex_num; i++)
	{
		cin >> graph->Vex[i];

	}

    for(i = 0; i < graph->vex_num; i++)
    {
        for(j = 0; j < graph->vex_num; j++)
        {
            // 初始化为0
            graph->Edge[i][j] = 0;
        }
    }

    while(graph->edge_num--)
    {
        cout << "请输入通过边连接起来的顶点:" << endl;
        cin >> u;
        cin >> v;
        // 到graph中找到字符u,所对应的索引
        i = search_vex(graph, u);
        j = search_vex(graph, v);
        if(i != -1 && j != -1)
            // 有向图为非对称的二维邻接矩阵
            graph->Edge[i][j]  = 1;
        else
        {
            cout << "你输入的顶点信息是错的,请再次输入" << endl;
            graph->edge_num++;
        }
    }

	return graph;

}

void Show_AMG_Graph(struct AMG_Graph * graph)
{
    int i,j;
    cout << "显示顶点信息:"<< endl;
    for(i = 0; i < graph->vex_num; i++)
        cout << graph->Vex[i] << "\t";
    cout << endl;

    cout << "显示邻接矩阵:" << endl;
    for(i = 0; i < graph->vex_num; i++)
    {
        for(j = 0; j < graph->vex_num; j++)
        {
           cout <<  graph->Edge[i][j] << "\t";
        }
        cout << endl;
    }
}


int search_vex(struct AMG_Graph* graph, char c)
{
    int i;
    // o(n)
    for(i = 0; i < graph->vex_num; i++)
    {
        if(c == graph->Vex[i])
            return i;
    }
    return -1;
}

directed_mat.h

#ifndef __directed_mat_h__
#define __directed_mat_h__

#define MAX 100

// 图结构,代表一整张图
struct AMG_Graph
{
	// 顶点的个数,边的个数
	int vex_num, edge_num;
	// 储存顶点的信息的一维数组
	char Vex[MAX];
	// 储存顶点和顶点之间的邻接关系,二维矩阵
	int Edge[MAX][MAX];

};



struct AMG_Graph* Create_AMG_Graph(void);
void Show_AMG_Graph(struct AMG_Graph * graph);
int search_vex(struct AMG_Graph* graph, char c);


#endif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来一步一步实现: 首先,我们需要定义一个结构体来表示中每个节点的信息,包括节点编号和节点的邻居。代码如下: ```c++ struct Node { int id; // 节点编号 vector<int> neighbors; // 邻居 }; ``` 接下来,我们定义一个函数来创建邻接矩阵。该函数接收一个节点数量和一个表示边的二维数组,返回一个邻接矩阵。代码如下: ```c++ vector<vector<int>> createGraph(int n, vector<vector<int>>& edges) { vector<vector<int>> graph(n, vector<int>(n, 0)); // 初始化邻接矩阵 for (auto& edge : edges) { int u = edge[0]; int v = edge[1]; graph[u][v] = 1; graph[v][u] = 1; } return graph; } ``` 该函数使用一个二维数组来表示边的信息,每个元素为一条边的起点和终点。我们遍历这个二维数组,将每条边的信息添加到邻接矩阵中。 最后,我们使用<queue>库函数来完成邻接矩阵的广度遍历。代码如下: ```c++ void bfs(vector<vector<int>>& graph, int start) { int n = graph.size(); // 节点数量 vector<bool> visited(n, false); // 记录节点是否被访问过 queue<int> q; // 使用队列存储遍历的节点 // 将起始节点加入队列并标记为已访问 q.push(start); visited[start] = true; while (!q.empty()) { int curr = q.front(); q.pop(); cout << curr << " "; // 输出遍历的节点 // 遍历当前节点的邻居 for (int i = 0; i < n; i++) { if (graph[curr][i] == 1 && !visited[i]) { q.push(i); visited[i] = true; } } } } ``` 在该函数中,我们使用一个bool类型的数组来记录节点是否被访问过,使用一个队列来存储遍历的节点。我们将起始节点加入队列并标记为已访问,然后在while循环中,每次从队列中取出一个节点,输出该节点并遍历该节点的邻居。若邻居节点未被访问,则将其加入队列并标记为已访问。 完整代码如下: ```c++ #include <iostream> #include <vector> #include <queue> using namespace std; struct Node { int id; // 节点编号 vector<int> neighbors; // 邻居 }; vector<vector<int>> createGraph(int n, vector<vector<int>>& edges) { vector<vector<int>> graph(n, vector<int>(n, 0)); // 初始化邻接矩阵 for (auto& edge : edges) { int u = edge[0]; int v = edge[1]; graph[u][v] = 1; graph[v][u] = 1; } return graph; } void bfs(vector<vector<int>>& graph, int start) { int n = graph.size(); // 节点数量 vector<bool> visited(n, false); // 记录节点是否被访问过 queue<int> q; // 使用队列存储遍历的节点 // 将起始节点加入队列并标记为已访问 q.push(start); visited[start] = true; while (!q.empty()) { int curr = q.front(); q.pop(); cout << curr << " "; // 输出遍历的节点 // 遍历当前节点的邻居 for (int i = 0; i < n; i++) { if (graph[curr][i] == 1 && !visited[i]) { q.push(i); visited[i] = true; } } } } int main() { int n = 6; vector<vector<int>> edges = {{0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 4}, {3, 5}, {4, 5}}; vector<vector<int>> graph = createGraph(n, edges); bfs(graph, 0); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值