邻接矩阵转邻接表

邻接矩阵转邻接表

Description

给一个带权有向图的邻接矩阵表示,将之转换为邻接表的表示,并输出对应的邻接表

Input

第一行:两个整数m(图的节点数),n(图的边数)(0 < n,m < 100,)
余下n行:n*n矩阵,代表矩阵表示下的图(其中以非零表示有链接,数字间以空格隔开)

Output

输出有m行,对应m个节点的邻接表示下的节点连接情况

Sample Input

6 10
0 5 0 7 0 0
0 0 4 0 0 0
8 0 0 0 0 0
0 0 5 0 0 6
0 0 5 0 0 0
3 0 0 0 1 0

Sample Output

0:1 3
1:2
2:0
3:2 5
4:2
5:0 4

Hint

这是非联通图。


#include <stdio.h>
#include <malloc.h>
//#include "graph.h" 
#include<iostream>
using namespace std;
typedef int InfoType;
#define	MAXV 100				//最大顶点个数
#define INF 32767				//INF表示∞
//以下定义邻接矩阵类型
typedef struct 
{  	int no;						//顶点编号
	InfoType info;				//顶点其他信息
} VertexType;					//顶点类型
typedef struct  				//图的定义
{  	int edges[MAXV][MAXV]; 		//邻接矩阵
   	int n,e;   					//顶点数,边数
	VertexType vexs[MAXV];		//存放顶点信息
} MGraph;						//图的邻接矩阵类型
//以下定义邻接表类型
typedef struct ANode           	//边的节点结构类型
{	int adjvex;              	//该边的终点位置
   	struct ANode *nextarc; 		//指向下一条边的指针
   	InfoType info;           	//该边的相关信息,这里用于存放权值
} ArcNode;
typedef int Vertex;
typedef struct Vnode      		//邻接表头节点的类型
{	Vertex data;            	//顶点信息
    ArcNode *firstarc;     		//指向第一条边
} VNode;
typedef VNode AdjList[MAXV];	//AdjList是邻接表类型
typedef struct 
{	AdjList adjlist;         	//邻接表
    int n,e;                 	//图中顶点数n和边数e
} ALGraph;                   	//图的邻接表类型

void MatToList(MGraph g,ALGraph *&G)
//将邻接矩阵g转换成邻接表G
{	int i,j;
	ArcNode *p;
	G=(ALGraph *)malloc(sizeof(ALGraph));
	for (i=0;i<g.n;i++)					//给邻接表中所有头节点的指针域置初值
		G->adjlist[i].firstarc=NULL;
	for (i=0;i<g.n;i++)					//检查邻接矩阵中每个元素
		for (j=g.n-1;j>=0;j--)
			if (g.edges[i][j]!=0)		//存在一条边
			{	p=(ArcNode *)malloc(sizeof(ArcNode));	//创建一个节点*p
				p->adjvex=j;
				p->nextarc=G->adjlist[i].firstarc;		//采用头插法插入*p
				G->adjlist[i].firstarc=p;
			}
	G->n=g.n;G->e=g.e;
}
void ListToMat(ALGraph *G,MGraph &g)
//将邻接表G转换成邻接矩阵g
{	int i;
	ArcNode *p;
	for (i=0;i<G->n;i++)
	{	p=G->adjlist[i].firstarc;
		while (p!=NULL)
		{	g.edges[i][p->adjvex]=1;
			p=p->nextarc;
		}
	}
	g.n=G->n;g.e=G->e;
}
void DispMat(MGraph g)
//输出邻接矩阵g
{
	int i,j;
	for (i=0;i<g.n;i++)
	{
		for (j=0;j<g.n;j++)
			printf("%3d",g.edges[i][j]);
		printf("\n");
	}
}
void DispAdj(ALGraph *G)
//输出邻接表G
{
	int i;
	ArcNode *p;
	for (i=0;i<G->n;i++)
	{
		p=G->adjlist[i].firstarc;
		//printf("%3d: ",i);
		cout<<i<<":";
		while (p!=NULL)
		{
			//printf("%3d",p->adjvex);
			cout<<p->adjvex<<" ";
			p=p->nextarc;
		}
		printf("\n");
	}
}
//以下主函数用作调试
int main()
{
	int i,j;
	MGraph g,g1;
	ALGraph *G;
	int m,n;
	cin>>n>>m;
	int A[n][n];
	for(int i=0;i<n;i++)
	{
            for(int j=0;j<n;j++)
            {
                    cin>>A[i][j];
            }
    }
	
	g.n=n;g.e=m;
	for (i=0;i<g.n;i++)
		for (j=0;j<g.n;j++)
			g.edges[i][j]=A[i][j];
	//printf("\n");
	///printf(" 有向图G的邻接矩阵:\n");
	//DispMat(g);
	G=(ALGraph *)malloc(sizeof(ALGraph));
	//printf(" 图G的邻接矩阵转换成邻接表:\n");
	MatToList(g,G);
	DispAdj(G);
/*	printf(" 图G的邻接表转换成邻接邻阵:\n");
	for (i=0;i<g.n;i++)
	   	for (j=0;j<g.n;j++)
			g1.edges[i][j]=0;
	ListToMat(G,g1);
	DispMat(g1);
	printf("\n");*/
	//system("pause");
}



  • 21
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CatTalk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值