有向图,无向图的邻接矩阵和邻接表模板

简介: 有向图,无向图的邻接矩阵和邻接表模板

图的定义


图 G GG 由顶点集 V VV 和边集 E EE 组成,记为 G = ( V , E ) G=(V,E)G=(V,E),其中 V ( G ) V(G)V(G) 表示图 G GG 中顶点的有限非空集;E ( G ) E(G)E(G) 表示图 G GG 中顶点之间的关系(边)集合。


有向图

概念

若 E是有向边(也称弧)的有限集合时,则图 G 为有向图。弧是顶点的有序对,记为 < v , w > ,其中 v , w是顶点, v  称为弧尾,w 称为弧头,< v , w > 称为从 v 到 w 的弧,也称 v 邻接到 w。



模板

邻接矩阵

/* 
有向图邻接矩阵
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MaxVertexNum 100
typedef int weightType;  // 权重数据类型
typedef char vertexType; // 顶点数据类型
struct Graph
{
    int vertexnum;
    int edgenum;
    vertexType vertexList[MaxVertexNum];
    weightType edgeList[MaxVertexNum][MaxVertexNum];
};
void BuildGraph(Graph *G)
{
    int start, end;
    cout << "Please enter the number of vertices and edges" << endl;
    cin >> G->vertexnum >> G->edgenum;
    // 图的权重初始化
    for (int i = 0; i < G->vertexnum; i++)
    {
        for (int j = 0; j < G->vertexnum; j++)
        {
            G->edgeList[i][j] = 0;
        }
    }
    // 图的顶点数据
    for (int i = 0; i < G->vertexnum; i++)
    {
        cout << "Please enter the data of vertex" << i + 1 << endl;
        cin >> G->vertexList[i];
    }
    // 输入权重信息
    for (int i = 0; i < G->edgenum; i++)
    {
        cout << "Please enter the Start number, end number, weight" << endl;
        cin >> start >> end;
        cin >> G->edgeList[start - 1][end - 1];
    }
    cout << endl;
}
void Print_Adjacency_Matrix(Graph G)
{
    for (int i = 0; i < G.vertexnum; i++)
    {
        for (int j = 0; j < G.vertexnum; j++)
        {
            cout << G.edgeList[i][j] << '\t';
        }
        cout << endl;
    }
}
int main()
{
    Graph G;
    BuildGraph(&G);
    Print_Adjacency_Matrix(G);
    cout << endl;
    system("pause");
    return 0;
}


邻接表

/* 
有向图邻接表
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MaxVertexNum 100
typedef int weightType;  // 权重数据类型
typedef char vertexType; // 顶点数据类型
struct EdgeNode
{
    int adjvex;
    weightType weight;
    EdgeNode *next;
};
struct VertexNode
{
    vertexType data;
    EdgeNode *firstedge;
};
struct Graph
{
    int vertexnum;
    int edgenum;
    VertexNode vertexList[MaxVertexNum];
    VertexNode inverseVertexList[MaxVertexNum];
};
void BuildGraph(Graph *G)
{
    int start, end, weight;
    EdgeNode *newnode;
    cout << "Please enter the number of vertices and edges" << endl;
    cin >> G->vertexnum >> G->edgenum;
    // 图的顶点数据
    for (int i = 0; i < G->vertexnum; i++)
    {
        cout << "Please enter the data of vertex" << i << endl;
        cin >> G->vertexList[i].data;
        G->vertexList[i].firstedge = NULL;
        G->inverseVertexList[i].data = G->vertexList[i].data;
        G->inverseVertexList[i].firstedge = NULL;
    }
    // 输入权重信息
    for (int i = 0; i < G->edgenum; i++)
    {
        cout << "Please enter the Start number, end number, weight" << endl;
        cin >> start >> end >> weight;
        //start-->end
        newnode = new EdgeNode;
        newnode->adjvex = end;
        newnode->weight = weight;
        newnode->next = G->vertexList[start].firstedge;
        G->vertexList[start].firstedge = newnode;
        // 逆邻接表
        newnode = new EdgeNode;
        newnode->adjvex = start;
        newnode->weight = weight;
        newnode->next = G->inverseVertexList[end].firstedge;
        G->inverseVertexList[end].firstedge = newnode;
    }
}
void Print_Adjacency_Matrix(Graph G)
{
    for (int i = 0; i < G.vertexnum; i++)
    {
        cout << G.vertexList[i].data << '\t';
        EdgeNode *p = G.vertexList[i].firstedge;
        while (p)
        {
            printf("adjvex:%d weight:%d  ", p->adjvex, p->weight);
            p = p->next;
        }
        cout << endl;
    }
    cout << endl;
    cout << "inverse adjacency list" << endl;
    for (int i = 0; i < G.vertexnum; i++)
    {
        cout << G.inverseVertexList[i].data << '\t';
        EdgeNode *p = G.inverseVertexList[i].firstedge;
        while (p)
        {
            printf("adjvex:%d weight:%d  ", p->adjvex, p->weight);
            p = p->next;
        }
        cout << endl;
    }
}
int main()
{
    Graph G;
    BuildGraph(&G);
    Print_Adjacency_Matrix(G);
    cout << endl;
    system("pause");
    return 0;
}


无向图

概念

若 E是无向边的有限集合时,则图 G 为有向图。边是顶点的无序对,记为 ( v , w )或 ( w , v )。可以说 w 和 v  互 为邻接点。边 ( V , W )依附于 w 和 v ,或称边 ( v , w ) 和 v , w相关联。



模板

邻接矩阵

/* 
无向图邻接矩阵
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MaxVertexNum 100
typedef int weightType;  // 权重数据类型
typedef char vertexType; // 顶点数据类型
struct Graph
{
    int vertexnum;
    int edgenum;
    vertexType vertexList[MaxVertexNum];
    weightType edgeList[MaxVertexNum][MaxVertexNum];
};
void BuildGraph(Graph *G)
{
    int start, end;
    cout << "Please enter the number of vertices and edges" << endl;
    cin >> G->vertexnum >> G->edgenum;
    // 图的权重初始化
    for (int i = 0; i < G->vertexnum; i++)
    {
        for (int j = 0; j < G->vertexnum; j++)
        {
            G->edgeList[i][j] = 0;
        }
    }
    // 图的顶点数据
    for (int i = 0; i < G->vertexnum; i++)
    {
        cout << "Please enter the data of vertex" << i + 1 << endl;
        cin >> G->vertexList[i];
    }
    // 输入权重信息
    for (int i = 0; i < G->edgenum; i++)
    {
        cout << "Please enter the Start number, end number, weight" << endl;
        cin >> start >> end;
        cin >> G->edgeList[start - 1][end - 1];
        G->edgeList[end - 1][start - 1] = G->edgeList[start - 1][end - 1];
    }
    cout << endl;
}
void Print_Adjacency_Matrix(Graph G)
{
    cout << '\t';
    for (int i = 0; i < G.vertexnum; i++)
    {
        cout << G.vertexList[i] << '\t';
    }
    cout << endl;
    for (int i = 0; i < G.vertexnum; i++)
    {
        cout << G.vertexList[i] << '\t';
        for (int j = 0; j < G.vertexnum; j++)
        {
            cout << G.edgeList[i][j] << '\t';
        }
        cout << endl;
    }
}
int main()
{
    Graph G;
    BuildGraph(&G);
    Print_Adjacency_Matrix(G);
    cout << endl;
    system("pause");
    return 0;
}


邻接表


/* 
无向图邻接表
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MaxVertexNum 100
typedef int weightType;  // 权重数据类型
typedef char vertexType; // 顶点数据类型
struct EdgeNode
{
    int adjvex;
    weightType weight;
    EdgeNode *next;
};
struct VertexNode
{
    vertexType data;
    EdgeNode *firstedge;
};
struct Graph
{
    int vertexnum;
    int edgenum;
    VertexNode vertexList[MaxVertexNum];
};
void BuildGraph(Graph *G)
{
    int start, end, weight;
    EdgeNode *newnode;
    cout << "Please enter the number of vertices and edges" << endl;
    cin >> G->vertexnum >> G->edgenum;
    // 图的顶点数据
    for (int i = 0; i < G->vertexnum; i++)
    {
        cout << "Please enter the data of vertex" << i << endl;
        cin >> G->vertexList[i].data;
        G->vertexList[i].firstedge = NULL;
    }
    // 输入权重信息
    for (int i = 0; i < G->edgenum; i++)
    {
        cout << "Please enter the Start number, end number, weight" << endl;
        cin >> start >> end >> weight;
        //start-->end
        newnode = new EdgeNode;
        newnode->adjvex = end;
        newnode->weight = weight;
        newnode->next = G->vertexList[start].firstedge;
        G->vertexList[start].firstedge = newnode;
        // end-->start
        newnode = new EdgeNode;
        newnode->adjvex = start;
        newnode->weight = weight;
        newnode->next = G->vertexList[end].firstedge;
        G->vertexList[end].firstedge = newnode;
    }
}
void Print_Adjacency_Matrix(Graph G)
{
    for (int i = 0; i < G.vertexnum; i++)
    {
        cout << G.vertexList[i].data << '\t';
        EdgeNode *p = G.vertexList[i].firstedge;
        while (p)
        {
            printf("adjvex:%d weight:%d\n", p->adjvex, p->weight);
            p = p->next;
        }
        cout << endl;
    }
}
int main()
{
    Graph G;
    BuildGraph(&G);
    Print_Adjacency_Matrix(G);
    cout << endl;
    system("pause");
    return 0;
}


简单图

1.不存在重复边

2.不存在顶点到自身的边


完全图

对于无向图

∣E∣ 的取值范围 0到 n ( n − 1 ) / 2,有 n ( n − 1 ) / 2条边的无向图称为完全图


对于有向图

∣E∣ 的取值范围 0 到 n ( n − 1 ) ,有 n ( n − 1 )条弧的有向图称为有向完全图

目录
相关文章
|
数据安全/隐私保护 iOS开发
Apple Music中的DRM保护
苹果音乐(Apple Music)是一种流媒体音乐服务,为用户提供了广泛的音乐内容。然而,为了保护音乐版权,Apple Music使用数字版权管理(DRM)技术对其音乐进行保护。DRM保护是一种加密技术,旨在防止用户未经授权地复制、传播或修改受版权保护的音乐。
1776 1
|
SQL 监控 安全
sql数据库文件数据修复
当SQL数据库文件(如MDF、LDF等)损坏时,可能需要进行数据修复。以下是一些建议的步骤和策略,帮助你尝试修复SQL数据库文件中的数据: 1. **备份文件**: 在进行任何修复操作之前,请
1545 0
|
监控 安全 开发工具
git fatal: detected dubious ownership in repository at ‘xxx‘ 彻底解决方法
调整文件所有权和权限后,你应该能够无误地进行Git操作。持续的维护与监控文件系统的安全性能降低将来遇到类似问题的风险,并保证团队能够高效协作。如果你是在团队环境中工作,建议建立明确的协作规则和文件管理实践,以避免此类问题。
1780 3
奇偶校验,CRC循环冗余校验,海明码校验
奇偶校验,CRC循环冗余校验,海明码校验
675 0
|
算法 测试技术 开发者
软件质量保证与测试知识点总结
【2月更文挑战第21天】软件质量保证与测试知识点总结
463 0
|
测试技术
针对三角形问题,使用边界值分析法设计测试用例
一、测试问题描述 输入三个整数a、b、c,分别作为三角形的三条边,通过程序判断这三条边是否能构成三角形?如果能构成三角形,则判断三角形的类型(等边三角形、等腰三角形、一般三角形)。要求输入三个整数a、b、c,必须满足以下条件:1≤a≤200;1≤b≤200
1112 0
|
存储 缓存 算法
【计算机网络基础 三】数据链路层
【计算机网络基础 三】数据链路层
1472 0
|
存储 安全 算法
计算机操作系统(慕课版)第一章课后题答案
计算机操作系统(慕课版)第一章课后题答案
1250 0
|
缓存 NoSQL MongoDB
MongoDB占用内存过大频繁宕机
MongoDB占用内存过大频繁宕机
680 0