数据结构实验7-有向图的邻接表与Dijkstra

实验要求

编写一个程序,实现图的相关运算,并在此基础上设计一个主程序,完成如下功能:

  1. 设计并实现有向图的邻接表表示
  2. 实现Dijkstra算法。
  3. 输出Dijkstra中从目标起点到各顶点的最短路径

程序代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
#define VRtype int
#define Status int
#define VertexType int
typedef enum{DG,DN,UDG,UDN} GraphKind;
typedef struct ArcCell {
    VRtype adj;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct {
    VertexType vex[MAX_VERTEX_NUM];
    AdjMatrix arcs;
    int vexnum, arcnum;
    int kind;
}MGraph;
bool visited[MAX_VERTEX_NUM + 5];
int P[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
void SHOW_PATH(int sta,MGraph G,int D[],int Path[])
{
    printf("各个点到a的最短距离与路径为:\n");
    for(int i = 1; i<=G.vexnum;++i){
        if(i == sta) continue;
        printf("%c %d:\t",i+'a'-1,D[i]);
        printf("%c",sta-1+'a');
        int temp = i;
        while(Path[temp]>0){
            printf("->%c",Path[temp]-1+'a');
            temp = Path[temp];
        }
        printf("->%c\n",i-1+'a');
    }
}
void Shortest_path_DIJ(MGraph G, int vstart, int P[][MAX_VERTEX_NUM])
{
    int finalx[MAX_VERTEX_NUM];
    int D[MAX_VERTEX_NUM];
    int Path[MAX_VERTEX_NUM]= {-1,-1}; memset(Path,0,sizeof(Path));
    for(int i = 1; i<=G.vexnum;++i){
        finalx[i] = FALSE;
        D[i] = G.arcs[vstart][i].adj;
    }
    printf("\n");
    D[vstart] = 0; finalx[vstart] = TRUE;
    int v;
    for(int i = 2; i<=G.vexnum;++i){
        int minx = INFINITY;
        for(int j = 1; j<=G.vexnum;++j){
            if(!finalx[j])
                if(D[j] < minx) {
                    v = j;
                    minx = D[j];
                }
        }
        finalx[v] = TRUE;
        for(int j =1; j <=G.vexnum; ++j){
            if(!finalx[j]&& (minx + G.arcs[v][j].adj < D[j])){
               if(G.arcs[v][j].adj!=INFINITY){
                    D[j] = minx + G.arcs[v][j].adj;
                    Path[j] = v;
               }
            }
        }
    }
    SHOW_PATH(vstart,G,D,Path);
}
Status CreateDN(MGraph &G)
{
    printf("请输入图的顶点个数和边的个数\n");
    scanf("%d %d",&G.vexnum,&G.arcnum);
    printf("%d %d\n",G.vexnum,G.arcnum);
    for(int i = 1; i<=G.vexnum;++i) G.vex[i] = i;
    for(int i = 1; i<=G.vexnum;++i){
        for(int j = 1; j<=G.vexnum;++j){
            G.arcs[i][j].adj = INFINITY;
        }
    }
    printf("请输入%d条边的信息,格式为起点,终点,权值\n",G.arcnum);
    for(int i = 0; i<G.arcnum ;++i){
        int v1,v2,w;
        char t1,t2;
        scanf(" %c %c %d",&t1,&t2,&w);
        v1 = t1 -'a' +1;
        v2 = t2 -'a' +1;
        G.arcs[v1][v2].adj = w;
    }
    return OK;
}
Status CreatGraph( MGraph &G)
{
    printf("请输入图的种类 1代表有向网 3代表无向网\n");
    scanf("%d",&G.kind);
    printf("%d\n",G.kind);
    switch(G.kind){
        //case DG: return CreateDG(G);
        case DN: return CreateDN(G);
       // case UGD: return CreatUDG(G);
    //case UDN: return CreatUDN(G);
        default : return ERROR;
    }
}
void output(MGraph G)
{
    printf("顶点表为\n");
    for(int i =1; i<=G.vexnum;++i){
        printf("%c %d\n",i+'a'-1,i);
    }
    printf("邻接矩阵为\n");
    for(int i = 1; i<=G.vexnum;++i){
        for(int j = 1; j<=G.vexnum;++j){
            if(G.arcs[i][j].adj != INFINITY){
                printf("%2d ",G.arcs[i][j].adj);
            }else{
                printf(" # ");
            }
        }
        printf("\n");
    }
}
int main()
{
    freopen("in.txt","r",stdin);
    MGraph G;
    CreatGraph(G);
    Shortest_path_DIJ(G,1,P);
    return 0;
}

运行结果

这里写图片描述
图形状
这里写图片描述

数据

1
7 11
a b 15
a c 2
a d 12
b e 6
c e 8
f d 5
c f 4
d g 3
e g 9
f g 10
g b 4
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值