图结构定义
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef char VertexType;
typedef int EdgeType;
#define MAXV 20
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;
}
}
}
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
} 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;
}
}
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;
}
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;
}
}
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 >)
{
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;
}
}
void transpose(ALGraph &G,ALGraph >)
{
int 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++)
for(p=G.adjList[i].firstArcNode;p;p=p->nextarc)
{
j=p->adjvex;
ArcNode *q=(ArcNode *)malloc(sizeof(ArcNode));
q->adjvex=i;
q->nextarc=GT.adjList[j].firstArcNode;
GT.adjList[j].firstArcNode=q;
}
}
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(){
TestAM();
return 0;
}