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

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

图的定义


图 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 )条弧的有向图称为有向完全图

目录
相关文章
|
3月前
|
程序员 Go API
Ontology 为什么越建越乱?用 Protégé 讲透 Class 层级、Domain/Range、Disjoint 与建模边界
本文深入剖析Protégé本体建模常见误区:从“Class vs Individual”混淆、组合爆炸、Property命名混乱,到Domain/Range误用、Disjoint滥用等。强调Ontology核心是语义建模——用稳定概念(Class)、精准关系(ObjectProperty)和合理推理(Equivalent Class、Reasoner)表达真实世界,而非复制数据库结构。倡导以Competency Questions驱动设计,追求可维护、可推理、业务可读的高质量本体。(239字)
258 1
|
2月前
|
人工智能 运维 监控
企业 AI 落地不再是买工具:以千问办公 FDE 服务,把 AI 嵌入真实业务的实践经验分享
聚焦企业AI落地难痛点,首创“场景诊断—智能体搭建—系统集成—陪跑迭代”模式,助力制造、医药、电商等行业将AI嵌入业务流程,打造可落地、可度量、可持续的AI生产力。
496 0
|
2月前
|
人工智能 缓存
阿里云AI模型的Token如何计费?输入、输出及免费100 Tokens费用说明
2026年阿里云通义千问(Qwen)系列模型Token收费标准公布:按输入/输出分别计价,覆盖Qwen3-Max、Qwen3.5-Plus、Qwen-Long、Qwen3-Omni-Flash(多模态)、通义点金及全妙轻应用等。支持Batch调用5折、上下文缓存优惠及90天内免费额度(各100万Token起),精准报价以阿里云百炼官网为准。(239字)
1211 0
|
9月前
|
人工智能 搜索推荐 数据库
Rokid AI 赛道铜奖 [奖金10000元]:烹饪实时辅助智能体开发总结分享
本项目基于Rokid眼镜与灵珠智能体平台,打造专为厨房场景优化的AI烹饪助手。聚焦高频痛点,采用单Agent架构实现灵活交互、上下文记忆与技能扩展,集成拍照识材、实时指导、多菜统筹等8大功能,并针对小屏显示精简提示、优化进度跟踪,兼顾实用性与创新性。(239字)
443 1
|
3月前
|
人工智能 关系型数据库 MySQL
让 Agent 真正"记住"项目:从会话记忆到长期记忆
本文探讨AI Agent记忆技术的演进:从受限的上下文窗口、无状态的RAG,到具备自动记录与推理能力的Agent Memory,最终走向系统化管理的上下文数据库(ContextDB),旨在让Agent真正成为“记住项目”的智能队友。
439 0
|
3月前
|
人工智能 运维 安全
2026中小企业轻量化AI数字化落地实践:零自研、低成本架构方案
本文提出面向中小微企业的轻量化AI数字化方案,涵盖私有知识库、AI智能体、AI智能官网与全站SSL安全四大模块,无需自研、低成本、快落地,助力企业构建可控数据资产、提升AI曝光与智能接待能力,实现合规、高效、可持续的数字化升级。(239字)
|
9月前
|
SQL 人工智能 安全
别人都在“养龙虾”,我靠这个AI工具箱3小时搞定“祖传代码”
2026年,“OpenClaw龙虾”AI智能体席卷中国,但代码质量成最大瓶颈。飞算JavaAI工具箱应运而生:10秒重构面条代码、一键修复依赖冲突、自动堵住SQL注入漏洞、生成3万字文档、秒建高覆盖单元测试——专为祖传Java系统“拆弹”,让AI真正敢用、能用、好用。(239字)
|
小程序 前端开发 关系型数据库
告别“月月光”:Uni+Php校园系统小程序,给大学生的低成本创业方案,学业赚钱两不误
uni+Php,寓意“大学加技能”,融合技术与校园生活。轻量小程序整合跑腿、团购、打印等服务,助力学生技能变现。PHP低成本架构,快速落地,覆盖代取快递、宿舍团购、失物招领等高频需求,打造校园一站式服务平台,实现多渠道盈利,月入过万可期。
告别“月月光”:Uni+Php校园系统小程序,给大学生的低成本创业方案,学业赚钱两不误
|
12月前
|
人工智能 自然语言处理 Cloud Native
概述篇
AI时代重塑软件开发,本课程填补DeepSeek+Cursor+Devbox融合教学空白。零基础也能通过自然语言实现“需求→代码→部署”全链路开发。掌握AI辅助设计数据库、生成代码、联调测试到云原生部署,3小时实战交付完整项目,助力开发者高效转型,抢占智能开发先机。(238字)
|
存储 自然语言处理 Java
全球语言无障碍:Unicode标准解读与技术演进史
Unicode是全球字符统一编码标准,旨在为世界上所有文字、符号及emoji分配唯一码点,解决多语言乱码问题。它兼容ASCII,支持超14万字符,覆盖现代与古文字,通过UTF-8、UTF-16等编码方案实现跨平台信息交换,是互联网、操作系统和多语言通信的基础。
2236 2