【数据结构】怎么把图的邻接表表示转化为图的邻接矩阵表示?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了【数据结构】怎么把图的邻接表表示转化为图的邻接矩阵表示?相关的知识,希望对你有一定的参考价值。

帮忙把这个程序改写成图的邻接矩阵表示
谢谢啊
#define MAX_VERTEX_NUM 20
#define MAXQSIZE 100
typedef struct ArcNode
int adjvex;
struct ArcNode *nextarc;
int *info;ArcNode;

typedef struct VNode
int data;
ArcNode *firstarc; VNode, AdjList[MAX_VERTEX_NUM];

typedef struct
AdjList vertices;
int vexnum, arcnum;
int kind; ALGraph;

int LocateVex(ALGraph *g,int v)

int i=0;
while(g->vertices[i].data!=v)
i++;
return i;


ALGraph Create(ALGraph *g)
int i,k,j,v1,v2;ArcNode *p;
p=(ArcNode*)malloc(sizeof(ArcNode));
printf("Please input vexnum and arcnum:");

scanf("%d%d",&g->vexnum, &g->arcnum);
printf("Please input %d vertexs:",g->vexnum);
for(i=0; i<g->vexnum; i++)
scanf("%d",&g->vertices[i].data);
g->vertices[i].firstarc =0;
printf("Please input %d edges:",g->arcnum);
for(k=0; k<g->arcnum; k++)
scanf("%d-%d",&v1,&v2);
i = LocateVex(g, v1); j = LocateVex(g, v2);
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j; p->info =0;
p->nextarc = g->vertices[i].firstarc;
g->vertices[i].firstarc = p;
return *g;


int visited[MAX_VERTEX_NUM];
void (*VisitFunc)(int* v);
void dfs(ALGraph *g, int j)

ArcNode *p;
visited[j]=1;
printf("%d ",g->vertices[j].data);
p=g->vertices[j].firstarc;
while(p)

if(visited[p->adjvex]==0)
dfs(g,p->adjvex);
p=p->nextarc;



void dfstraverse(ALGraph *g)

int i;
for(i=0;i<g->vexnum;i++)
visited[i]=0;
for(i=0;i<g->vexnum;i++)

if(visited[i]==0)
dfs(g,i);

/*深度优先遍历*/

void bfstraverse(ALGraph g)
int i,v,w,s=0,n,head,tail;int queue[30];
ArcNode *t;
for(i=0;i<n;i++)
visited[i]=0;
head=tail=0;
printf("%d ",s+1);
visited[s]=1;
queue[tail++]=s;
while(head<tail)
v=queue[head++];
for(t=g.vertices[v].firstarc;t!=0;t=t->nextarc)
w=t->adjvex;
if(visited[w]==0)
printf("%d ",w+1);
visited[w]=1;
queue[tail++]=w;/*广度优先遍历*/

void print(ALGraph *g)
int i;
ArcNode *p;
for(i=0;i<g->vexnum;i++)
printf("%d ",i);
printf("%d-->",g->vertices[i].data);
p=g->vertices[i].firstarc;
while(p!=0)
printf("%d-->",p->adjvex);
p=p->nextarc;
printf("^\n");

/*打印邻接表*/

ALGraph *g;
void main()
g=(ALGraph *)malloc(sizeof(ALGraph));*g=Create(g);
printf("\nThe graph is:\n");
print(g);
printf("The Depth_First Search order is:\n");
dfstraverse(g);
getch();
printf("\nThe Breadth_First Search order is:\n");
bfstraverse(*g);
getch();

我要的是c语言编的...

邻接表(Adjacency List):是图的一种链式存储结构。

在邻接表中,对图中每个顶点建立一个单链表,第i个单链表中的结点表示依附于顶点vi的邻接表由两部分构成:表头结头、表结点组成的单链表。 

邻接表的表示意义为:对于图G=(V,E),若(i,j)∈E,则第i个表头结点的单链表上有一个adjvex为j的表结头。 

参考技术A 你是要把邻接表转换成邻接矩阵
还是要一个邻接矩阵表示的代码啊?
好像不能直接转吧

#pragma once
#define MAX_VERTEX_NUM 20
typedef enumDG,DN,UDG,UDNGraphKind;
#define INFINTY 1000
#include <iostream>
using namespace std;

class ArcCell

friend class MGraph;
public:
int adj;
int *info;
;

class Closedge

friend class MGraph;
public:
int adjvex;
int lowcost;
;

class MGraph

public:
char vexs[MAX_VERTEX_NUM];
ArcCell arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum,arcnum;
GraphKind kind;
bool Visited[MAX_VERTEX_NUM];

//bool Visted[MAX_VERTEX_NUM]; //遍历的访问数组

Closedge closedge[MAX_VERTEX_NUM];

public:
MGraph(void);
~MGraph(void);

int LocateVex(char ch)

for(int i=0;i<this->vexnum;i++)

if(this->vexs[i]==ch)
return i;

return 0;



void CreateUND()

int vexn,arcn,i,j;
cout<<"请输入节点个数:";
cin>>vexn;
cout<<"请输入弧数:";
cin>>arcn;
this->arcnum=arcn;
this->vexnum=vexn;
for(i=0;i<vexnum;i++)

cout<<"请输入节点名:";
cin>>this->vexs[i];

for(i=0;i<vexnum;i++)
for(j=0;j<vexnum;j++)

arcs[i][j].adj=INFINTY;
this->arcs[i][j].info=NULL;

cout<<"请输入弧的节点及其权值:"<<endl;
for(i=0;i<arcnum;i++)

char a,b;
int x,y,w;
cout<<"请输入节点1:";
cin>>a;
cout<<"请输入节点2:";
cin>>b;
cout<<"请输入权值大小:";
cin>>w;
x=this->LocateVex(a);
y=this->LocateVex(b);
this->arcs[x][y].adj=w;
this->arcs[y][x].adj=this->arcs[x][y].adj;



/*
void GraphPrint()

for(int i=0;i<vexnum;i++)

for(int j=0;j<vexnum;j++)

cout<<arcs[i][j].adj<<"\t";

cout<<endl;



void MiniSpanTree_PRIM(int ch)

int k=this->LocateVex(ch);
for(int j=0;j<this->vexnum;++j)

if(j!=k)

this->closedge[j].adjvex=ch;
this->closedge[j].lowcost=this->arcs[k][j].adj;


this->closedge[k].lowcost=0;
for(int i=1;i<this->vexnum;++i)

//k=this->minimum();
int min=1000;

for(int j=0;j<vexnum;j++)

if(closedge[j].lowcost>0 && closedge[j].lowcost<min)

k=j;
min=closedge[j].lowcost;




cout<<"\t新增节点:\t"<<closedge[k].adjvex<<"\t权值:\t"<<this->vexs[k]<<endl;
closedge[k].lowcost=0;

for(int j=0;j<this->vexnum;j++)

if(this->arcs[k][j].adj<closedge[j].lowcost)

closedge[j].adjvex=this->vexs[k];
closedge[j].lowcost=this->arcs[k][j].adj;





int minimum()

Closedge temp;
temp.lowcost=1000;
for(int i=0;i<this->vexnum;++i)

if(temp.lowcost>closedge[i].lowcost)

temp.lowcost=closedge[i].lowcost;


return temp.lowcost;


*/

void BFSTraverse()

int v,r=0,f=0;
int w;
int cq[20];
for(v=0;v<this->vexnum;v++)

this->Visited[v]=0;

for(v=0;v<this->vexnum;v++)

cq[v]=-1;

for(v=0;v<this->vexnum;v++)

if(!this->Visited[v])

this->Visited[v]=1;
cout<<this->vexs[v]<<"\t";
cq[r]=v;
while(cq[f]!=-1)

v=cq[f];
f++;
// int i;
for(w=this->FirstAdjVex(v);w!=-1;w=this->NextAdjVex(v,w))

/*for(i=0;i<this->vexnum;i++)
if(this->vexs[i]==w)
break;*/
if(!this->Visited[w])


/*for(i=0;i<this->vexnum;i++)
if(this->vexs[i]==w)
break;*/
this->Visited[w]=1;
cout<<this->vexs[w]<<"\t";
r++;
cq[r]=w;







int FirstAdjVex(int u)

int i,j;
for(i=0;i<this->vexnum;i++)

if(this->vexs[u]==this->vexs[i])
break;

//for(;i<this->vexnum;i++)
for(j=0;j<this->vexnum;j++)

if(this->arcs[i][j].adj!=INFINTY)
return j;//!!!!!!!

return -1;

int NextAdjVex(int u,int w)

int i,j;
for(i=0;i<this->vexnum;i++)

if(this->vexs[u]==this->vexs[i])
break;

//for(;i<this->vexnum;i++)
/*for(j=0;j<this->vexnum;j++)
if(j==w)
break*/;
for(j=w;j<this->vexnum;j++)

if(this->arcs[i][j].adj!=INFINTY&&j!=w)
return j;

return -1;


void DFS(int v)

this->Visited[v]=1;
cout<<this->vexs[v]<<"\t";
int w;
for(w=this->FirstAdjVex(v);w!=-1;w=this->NextAdjVex(v,w))

/*int i;
for(i=0;i<this->vexnum;i++)
if(this->vexs[i]==w)
break;*/
if(!this->Visited[w])
this->DFS(w);




void DFSTraverse()

int v;
for(v=0;v<this->vexnum;v++)
this->Visited[v]=0;
for(v=0;v<this->vexnum;v++)

if(!this->Visited[v])
this->DFS(v);


;

这个是我用c++写的一个邻接矩阵
没粘main函数 不知是否符合你的要求
参考技术B 如果有对gml格式转换成邻接表或邻接矩阵有问题的请看博文
http://blog.sina.com.cn/s/blog_64914f1b0102vjzt.html
或者
http://weibo.com/p/1001603831291614203314

以上是关于【数据结构】怎么把图的邻接表表示转化为图的邻接矩阵表示?的主要内容,如果未能解决你的问题,请参考以下文章

用c语言编程 1创建图的邻接矩阵和邻接表 2验证图的深度优先、广度优先遍历算法 3验证最短路径

数据结构学习笔记——图的存储结构(邻接矩阵和邻接表)

数据结构学习笔记——图的存储结构(邻接矩阵和邻接表)

数据结构学习笔记——图的存储结构(邻接矩阵和邻接表)

图的存储表示及示例

数据结构与算法图 ( 图的存储形式 | 图的基本概念 | 图的表示方式 | 邻接矩阵 | 邻接表 | 图的创建 | 代码示例 )