图结构及其应用

这篇博客介绍了图结构的多种表示方法,包括邻接矩阵、链接表、十字链表和邻接多重表,并提供了对应的代码实现。此外,还讲解了深度优先遍历(DFS)、广度优先遍历(BFS)以及最小生成树的普里姆算法和Dijkstra单源最短路径算法。最后,探讨了有向图的强连通分支和拓扑排序算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <fstream>
#include <cstdlib>
#include <queue>
#include <string.h>
using namespace std;
#define MAX_VERTEX_NUM   20
#define N 100
bool visit[N];  //访问标记数组是全局变量
int dfn[N];  //顶点的先深编号
int count = 1;
int FLAG = 99999;
int in_order[N];
//无向图
struct MTGragh
{
    int vertex [N]; //顶点表
    int edge[N][N];//邻接矩阵—边表, 可视为边之间的关系
    int n, e; //图的顶点数与边数
};
//正向链接表
typedef struct node  //边表结点
{
    int adjvex; //邻接点域(下标)
    int  cost; //边上的权值
    struct node *next; //下一边链接指针
} EdgeNode;
typedef struct  //顶点表结点
{
    int  vertex; //顶点数据域
    EdgeNode * firstedge;//边链表头指针
} VertexNode;
typedef struct  //图的邻接表
{
    VertexNode vexlist [N];
    int n,e;  //顶点个数与边数
} AdjGraph;
//十字链表
typedef struct ArcBox
{
    int  tailvex, headvex; //该弧的尾和头顶点的位置
    int  cost; //该弧的信息
    struct ArcBox * hlink, * tlink;  //分别为弧头相同和弧尾相同的弧的链域
} ArcBox;
typedef struct VexNode
{
    int data; //顶点值
    ArcBox * firstin, * firstout;  //分别指向该顶点第一条入弧和出弧
} VexNode;
typedef struct
{
    VexNode  xlist[MAX_VERTEX_NUM]; //表头向量
    int n, e;       //有向图的当前顶点数和弧数
} OLGraph;
//无向图的邻接多重表
typedef enum {unvisited, visited} VisitIf;
typedef struct EBox
{
    VisitIf   mark; //访问标记
    int ivex, jvex; //该边依附的两个顶点的位置
    struct EBox   * ilink, * jlink; //分别指向依附这两个顶点的下一条边
    int cost; //该边信息
} EBox;
typedef struct VexBox
{
    int   data;
    EBox * firstedge; //分别指向该顶点第一条入弧和出弧
} VexBox;
struct AMLGraph
{
    VexBox adjmulist[MAX_VERTEX_NUM];
    int n,e; //无向图的当前顶点数和边数
};
void  DFS(OLGraph *G, int v);
void  Rev_DFS(OLGraph *G, int v);
//建立图的邻接矩阵
int CreateMGragh(MTGragh *G,int c[N],int flag,int sum)
{
    int i, j;
    G->n=c[1];
    for (i=0; i<G->n; i++) //2.读入顶点信息,建立顶点表
        G->vertex[i]=i;
    for (i=0; i<G->n; i++)
        for (j=0; j<G->n; j++)
            G->edge[i][j]=0; //3.邻接矩阵初始化
    int a,b;
    int temp=0;
    i=2;
    if(flag==0)
    {
        while(i<sum)
        {
            a=c[i];
            i++;
            b=c[i];
            i++;
            G->edge[a][b] = c[i];
            G->edge[b][a] = c[i];
            temp++;
            i++;
        }
    }
    else
    {
        while(i<sum)
        {
            a=c[i];
            i++;
            b=c[i];
            i++;
            G->edge[a][b] = c[i];
            //G->edge[b][a] = c[i];
            temp++;
            i++;
        }
    }
    G->e=temp;
    return G->e;
}
//输出无向图的邻接矩阵
void putMgraph(MTGragh G)
{
    cout<<"无向图的邻接矩阵:"<<endl;
    for(int i=0; i<G.n; i++)
    {
        cout<<i<<": ";
        for(int j=0; j<G.n; j++)
        {
            cout<<G.edge[i][j]<<" ";
        }
        cout<<endl;
    }
}
//建立图的链接表
AdjGraph CreateGraph(AdjGraph G,int c[N],int k)
{
    G.n=c[1]; G.e=k;int j=2;
    for(int i = 0; i < G.n; i++)  //2.建立顶点表
    {
        G.vexlist[i].vertex=i;  //2.1输入顶点信息
        G.vexlist[i].firstedge = NULL;//2.2边表置为空表
    }
    for ( int i = 0; i < k; i++)//逐条边建立边表
    {
        int tail = c[j];
        j++;
        int head=c[j];
        j++;
        int w
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值