邻接矩阵和邻接链表表示的有向图的转置

图结构定义

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

typedef char VertexType;
typedef int EdgeType;
#define MAXV 20

// 图的邻接矩阵表示法 adjacency matrix
typedef struct
{
    VertexType vexs[MAXV];
    EdgeType edges[MAXV][MAXV];
    int n, e;
} AMGraph;

// 初始化邻接矩阵
void InitAMGraph(AMGraph &G)
{
    G.n = 0;
    G.e = 0;
    for (int i = 0; i < MAXV; i++)
    {
        for (int j = 0; j < MAXV; j++)
        {
            G.edges[i][j] = 0;
        }
    }
}



// 图的邻接表表示法 adjacency list

// 边表结点
typedef struct ArcNode
{
    int adjvex; // 该弧所指向的顶点的位置
    struct ArcNode *nextarc;    // 指向下一个邻接点的指针
    // InfoType info;
} ArcNode;

// 定义顶点表结点和顶点结点列表
typedef struct VNode
{
    VertexType data;
    ArcNode *firstArcNode;
} VNode, AdjList[MAXV];
// 定义图
typedef struct
{
    AdjList adjList;
    int n, e;
} ALGraph;


// 初始化邻接表
void InitALGraph(ALGraph &G)
{
    G.n = 0;
    G.e = 0;
    for (int i = 0; i < MAXV; i++)
    {
        G.adjList[i].firstArcNode = NULL;
    }
}

// 图的基本操作

// 判断图中是否存在边<x,y>
// 邻接矩阵
bool Adjacent(AMGraph G, int x, int y)
{
    if (x < 0 || x > G.n - 1 || y < 0 || y > G.n - 1)
    {
        return false;
    }
    if (G.edges[x][y] == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
// 邻接表
bool Adjacent(ALGraph G, int x, int y)
{
    if (x < 0 || x > G.n - 1 || y < 0 || y > G.n - 1)
    {
        return false;
    }
    ArcNode *p = G.adjList[x].firstArcNode;
    while (p)
    {
        if (p->adjvex == y)
        {
            return true;
        }
        p = p->nextarc;
    }
    return false;
}

// 求图中顶点x的第一个邻接顶点
// 邻接矩阵
int FirstNeighbor(AMGraph G, int x)
{
    if (x < 0 || x > G.n - 1)
    {
        return -1;
    }
    for (int i = 0; i < G.n; i++)
    {
        if (G.edges[x][i] == 1)
        {
            return i;
        }
    }
    return -1;
}
// 邻接表
int FirstNeighbor(ALGraph G, int x)
{
    if (x < 0 || x > G.n - 1)
    {
        return -1;
    }
    ArcNode *p = G.adjList[x].firstArcNode;
    if (p)
    {
        return p->adjvex;
    }
    else
    {
        return -1;
    }
}

// 假如图中顶点x的一个邻接顶点是y,返回除y外x的下一个邻接顶点
// 邻接矩阵
int NextNeighbor(AMGraph G, int x, int y)
{
    if (x < 0 || x > G.n - 1 || y < 0 || y > G.n - 1)
    {
        return -1;
    }
    for (int i = y + 1; i < G.n; i++)
    {
        if (G.edges[x][i] == 1)
        {
            return i;
        }
    }
    return -1;
}
// 邻接表
int NextNeighbor(ALGraph G, int x, int y)
{
    if (x < 0 || x > G.n - 1 || y < 0 || y > G.n - 1)
    {
        return -1;
    }
    ArcNode *p = G.adjList[x].firstArcNode;
    while (p)
    {
        if (p->adjvex == y)
        {
            p = p->nextarc;
            if (p)
            {
                return p->adjvex;
            }
            else
            {
                return -1;
            }
        }
        p = p->nextarc;
    }
    return -1;
}

有向图转置算法

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#include "Graph.cpp"


//有向图的转置

//邻接矩阵
void transpose(AMGraph &G,AMGraph &GT)
{
    int i,j;
    GT.n=G.n;
    GT.e=G.e;
    for(i=0;i<G.n;i++)
        for(j=0;j<G.n;j++){
            VertexType temp=G.edges[i][j];
            GT.edges[i][j]=G.edges[j][i];
            GT.edges[j][i]=temp;
        }
            
}//时间复杂度O(n^2)

//邻接链表
void transpose(ALGraph &G,ALGraph &GT)
{
    int i,j;    //i为顶点序号,j为邻接点序号
    ArcNode *p;
    GT.n=G.n;
    GT.e=G.e;
    for(i=0;i<G.n;i++)
        GT.adjList[i].data=G.adjList[i].data;
    for(i=0;i<G.n;i++)
        GT.adjList[i].firstArcNode=NULL;
    for(i=0;i<G.n;i++)
        //对于每个顶点i,遍历其所有邻接点,将其邻接点的邻接表中加入i
        for(p=G.adjList[i].firstArcNode;p;p=p->nextarc)
        {
            j=p->adjvex;    //j为i的邻接点
            ArcNode *q=(ArcNode *)malloc(sizeof(ArcNode));
            q->adjvex=i;    //q为j的邻接点
            q->nextarc=GT.adjList[j].firstArcNode;
            GT.adjList[j].firstArcNode=q;
        }
}//时间复杂度O(n+e)

void CreateALGraph(ALGraph &G)
{
    int i,j,k;
    ArcNode *p;
    cout<<"请输入顶点数和边数:";
    cin>>G.n>>G.e;
    cout<<"请输入顶点信息:";
    for(i=0;i<G.n;i++)
        cin>>G.adjList[i].data;
    for(i=0;i<G.n;i++)
        G.adjList[i].firstArcNode=NULL;
    cout<<"请输入边的信息:"<<endl;
    for(k=0;k<G.e;k++)
    {
        cout<<"请输入边(vi,vj)上的顶点序号:";
        cin>>i>>j;
        p=(ArcNode *)malloc(sizeof(ArcNode));
        p->adjvex=j;
        p->nextarc=G.adjList[i].firstArcNode;
        G.adjList[i].firstArcNode=p;
    }
}

void CreateAMGraph(AMGraph &G){
    int i,j,k;
    cout<<"请输入顶点数和边数:";
    cin>>G.n>>G.e;
    cout<<"请输入顶点信息:";
    for(i=0;i<G.n;i++)
        cin>>G.vexs[i];
    for(i=0;i<G.n;i++)
        for(j=0;j<G.n;j++)
            G.edges[i][j]=0;
    cout<<"请输入边的信息:"<<endl;
    for(k=0;k<G.e;k++)
    {
        cout<<"请输入边(vi,vj)上的顶点序号:";
        cin>>i>>j;
        G.edges[i][j]=1;
    }

}

void TestAL(){
    //验证一个邻接链表的转置
    ALGraph G,GT;
    InitALGraph(G);
    InitALGraph(GT);
    CreateALGraph(G);
    transpose(G,GT);
    cout<<"转置后的邻接表为:"<<endl;
    for(int i=0;i<GT.n;i++)
    {
        cout<<GT.adjList[i].data<<" ";
        ArcNode *p=GT.adjList[i].firstArcNode;
        while(p)
        {
            cout<<p->adjvex<<" ";
            p=p->nextarc;
        }
        cout<<endl;
    }
}

void TestAM(){
    //验证一个邻接矩阵的转置
    AMGraph G,GT;
    InitAMGraph(G);
    InitAMGraph(GT);
    CreateAMGraph(G);
    transpose(G,GT);
    cout<<"转置后的邻接矩阵为:"<<endl;
    for(int i=0;i<GT.n;i++)
    {
        for(int j=0;j<GT.n;j++)
            cout<<GT.edges[i][j]<<" ";
        cout<<endl;
    }
}

int main(){
    
    //TestAL(G);

    TestAM();

    return 0;
}
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值