图的存储--邻接矩阵和邻接表(链表实现和用vector实现)

17 篇文章 0 订阅

邻接矩阵:

#include<iostream>
#include<vector>
#include<iomanip>
#include<string>

using namespace std;

const int maxVertices = 30;						//图中顶点数目的最大值
const int maxEdges = 900;						// 最大边数
const int maxWeight = 99999;						//最大权值
const char impossibleValue = '#';
const int impossibleWeight = -1;
struct MGraph
{
	int numVertices, numEdges;						//图中实际顶点个数和边的条数
	char VerticesList[maxVertices];					//顶点表
	int Edge[maxVertices][maxVertices];				//邻接矩阵
};

int getVertexPos(MGraph &G, char x)
{//从顶点的数据值中找出该顶点的顶点号,如果查找失败,函数返回-1;
	for (int i = 0; i < G.numVertices; i++)
	{
		if (G.VerticesList[i] == x)
			return i;
	}
	return -1;
}
int firstNeighbor(MGraph &G, int v)
{//返回顶点v的第一个邻居,无返回-1 
	if (v != -1)
	{
		for(int i = 0; i < G.numVertices; i++)
		{
			if(G.Edge[v][i] > 0 && G.Edge[v][i] < maxWeight)
			{
				return i;
			}
		} 
	}
	return -1;
}
int nextNeighbor(MGraph &G, int v, int w)
{//返回顶点v邻居中排在w后的邻居 ,无返回-1  
	if(v != -1 && w != -1)
	{
		for(int i = w+1; i < G.numVertices; i++)
		{
			if(G.Edge[v][i] > 0 && G.Edge[v][i] < maxWeight)
			{
				return i;
			}
		}
	}
	return -1;
}
int numberOfVertices(MGraph &G)
{//返回顶点个数
	return G.numVertices; 
} 
char getValue(MGraph &G,int v)
{
	if(v != -1) return G.VerticesList[v];
	else return impossibleValue;
}
int getWeight(MGraph &G,int v, int  w)
{
	if(v != -1 && w != -1)  return G.Edge[v][w];
	else return impossibleWeight;
}
void createMGraph(MGraph &G, string v,vector<string>edge,vector<int>weight, int d) 
{
	G.numVertices = v.length(); 
	G.numEdges = edge.size();
	for(int i = 0; i < G.numVertices; i++)
	{
		G.VerticesList[i] = v[i];
		for(int j = 0; j < G.numVertices; j++)
		{
			G.Edge[i][j] = (i == j) ? 0 : maxWeight; 
		} 
	} 
	for(int i = 0; i < G.numEdges; i ++)
	{
		int r = getVertexPos(G, edge[i][0]);
		int c = getVertexPos(G, edge[i][1]);
		G.Edge[r][c] = weight[i];
		if(d == 0)
		{
			G.Edge[c][r] = weight[i];
		}
	}
} 
void printMGraph(MGraph &G,int d)
{//d = 1按有向图输出;若d = 0按无向图输出
	cout<<"这是"<<((d == 1)? "有向图":"无向图")<<endl; 
	cout<<"顶点数: "<<G.numVertices<<"边数: "<<G.numEdges<<endl; 
	cout<<"顶点数据为:"<<endl;
	for(int i = 0; i < G.numVertices; i ++)
	{
		cout<<setw(4)<<G.VerticesList[i]; 
	} 
	cout<<endl; 
	cout<<"边数据是:"<<endl; 
	for(int i = 0; i < G.numVertices; i ++)
	{
		int s = (d == 0) ? i: 0; 					//无向图输出上三角 
		cout<<G.VerticesList[i]<<": "; 
		for(int j = s; j < G.numVertices; j++)
		{
			int w = G.Edge[i][j];
			if(w > 0 && w < maxWeight)
			{
				cout<<"("<<i<<","<<j<<"), "<<w<<";  "; 
			}
		} 
		cout<<endl; 
	} 
	cout<<"邻接矩阵打印 : "<<endl; 
	for(int i = 0; i < G.numVertices; i++)
	{
		for(int j = 0; j < G.numVertices; j++)
		{
			if(G.Edge[i][j] == -1 || G.Edge[i][j] == maxWeight)
			{
				cout<<setw(4)<<"-"; 
			} 
			else
			{
				cout<<setw(4)<<G.Edge[i][j];
			}
		}
		cout<<endl;
	}
}
int main()
{
	string vertices = "ABCDE";
	vector<string>edges = {"AB","AC","BC","BE","CB","CD","ED"};
	vector<int>weights = {24, 46, 15, 67, 37, 53, 31};
	MGraph G;
	createMGraph(G,vertices,edges,weights,1);
	printMGraph(G,1); 
	return 0;
}

在这里插入图片描述
2、邻接表:

#include<iostream>
#include<iomanip>
#include<string>
#include<vector>

using namespace std;

const int maxVertices = 30;					//图中顶点数目的最大值			
const int maxEdge = 450;            		// 最大边数
const char impossibleValue = '#';   		//最大权值
const int impossibleWeight = -1;
struct ENode							//边结点的定义 
{
	int dest;							//边的另一结点 
	int weight;							//边上的权重 
	ENode* link;						//下一条边链接指针 
	ENode(int _dest,int _weight)
	{
		dest = _dest;
		weight = _weight;
		link = NULL;
	}
	~ENode()
	{
		delete link;
	}
};
struct VNode				//顶点结构定义 
{
	char data;				//顶点的值	 
	ENode *adj;				//出边表的头指针 
};
struct ALGraph									//图结构的定义 
{	
	VNode verticesList[maxVertices];            //顶点表 
	int numVertices, numEdges;					//顶点数和边数 
	ALGraph()
	{
		numVertices = 0;	
		numEdges = 0;
	}	
};
int getVertexPos(ALGraph &G, char v)						 
{//返回顶在顶点列表的下标,无返回-1 	
	int i; 
	for(i = 0; i < G.numVertices && G.verticesList[i].data != v; i++)
				;
	
	if(i < G.numVertices)   return i;
	else return -1;
} 
int numberOfVertices(ALGraph &G)
{//返回顶点的个数 
	return G.numVertices; 
}
int numberOfEdge(ALGraph &G)
{//返回边数 
	return G.numEdges; 
} 
int firstNeighbor(ALGraph &G, int v)
{//返回下标为v的第一个相邻结点的下标,无返回-1  
	if(v != -1)
	{
		ENode *p = G.verticesList[v].adj;
		if(p != NULL) return p->dest; 
	}
	return -1;
}
int nextNeighbor(ALGraph &G, int v, int w)
{//返回与下标为v的顶点相邻并且排在w后的顶点的下标,无返回-1
	if(v != -1)
	{
		ENode *p = G.verticesList[v].adj;
		while ( p != NULL && p->dest != w)
		{
			p = p->link;
		} 
		if(p != NULL && p->link != NULL)
		{
			return p->link->dest;
		}
	}
	return -1;
} 
char getValue(ALGraph &G,int v)
{//返回下标为v的顶点的顶点值 
	if(v != -1)
	{
		return G.verticesList[v].data;
	}
	else return impossibleWeight;
}
int getWeight(ALGraph &G,int v,int w)
{//返回边(v,w)的权重 
	ENode *p = G.verticesList[v].adj;
	while(p != NULL && p->dest != v)  
	{
		p = p->link;
	} 
	
	if( p != NULL)
	{
		return p->weight;
	}
	else 
	{
		return impossibleWeight;
	}
}
void createALGraph(ALGraph &G, string v,vector<string>edge,vector<int>weights, int d) 
{
	G.numVertices = v.length(); 
	G.numEdges = edge.size();
	for(int i = 0; i < G.numVertices; i++)
	{
		G.verticesList[i].data = v[i];
		G.verticesList[i].adj = NULL;
	} 
	for(int i = 0; i < G.numEdges; i ++)
	{
		int j = getVertexPos(G, edge[i][0]);
		int k = getVertexPos(G, edge[i][1]);
		ENode *p = G.verticesList[j].adj;
		while( p != NULL && p->dest != k)
		{
			p = p->link;
		} 
		if(p == NULL)
		{
			ENode *q = new ENode(k,weights[i]);
			q->link = G.verticesList[j].adj;
			G.verticesList[j].adj = q;
			if( d = 0)
			{
				q = new ENode(j,weights[i]);
				q->link = G.verticesList[k].adj;
				G.verticesList[k].adj = q;
			}
			
		}
		else
		{
			p->weight = weights[i];
			if(d == 0)
			{
				p = G.verticesList[k].adj;
				while( p != NULL && p->dest != k)
				{
					p = p->link;
				}
				p->weight = weights[i];
			}
		}
	}
	 
}
void printALGraph(ALGraph &G,int d)
{//输出图G,d = 0,输出无向图,d = 1输出有向图
	cout<<"这是"<<((d == 1)? "有向图":"无向图")<<endl; 
	cout<<"图G的顶点数是: "<<G.numVertices<<endl;
	cout<<"这些顶点是:" <<endl;
	for(int i = 0; i < G.numVertices; i++)
	{
		cout<<setw(3)<<G.verticesList[i].data; 
	}
	
	cout<<endl<<"图的边数是: "<<G.numEdges<<endl;
	for(int i = 0; i < G.numVertices; i++)
	{
		cout<<G.verticesList[i].data<<":  ";
		for(ENode *p = G.verticesList[i].adj; p != NULL; p = p->link)
		{
			if(d = 0 && p->dest < i) continue;
			cout<<"("<<i<<","<<p->dest<<") "<<p->weight<<";   "; 
		}
		cout<<endl;
	} 
} 
int main()
{
	string vertices = "ABCDE";
	vector<string>edges = {"AB","AC","BC","BE","CB","CD","ED"};
	vector<int>weights = {24, 46, 15, 67, 37, 53, 31};
	ALGraph G;
	createALGraph(G,vertices,edges,weights,1);
	printALGraph(G,1); 
	return 0;
}

在这里插入图片描述

3、不得不说上面的方法还是麻烦了,用STL里的vector就好多了
比如:

#include<iostream>
#include<vector>
#include<string>

using namespace std;

void addEdge(vector<int>adj[],int u, int v)
{
	adj[u].push_back(v);
	adj[v].push_back(u);
};
void printGraph(vector<int>adj[],int V)
{
	for(int i = 0; i < V; i++)
	{
		cout<<"顶点"<<i<<"的邻接表"<<endl;
		cout<<i;
		for(int j = 0; j < adj[i].size(); j++)
		{
			cout<<"->"<<adj[i][j]; 
		}
		cout<<endl; 
	}
} 
int main() 
{ 
    int V = 5; 
    vector<int> adj[V]; 
    addEdge(adj, 0, 1); 
    addEdge(adj, 0, 4); 
    addEdge(adj, 1, 2); 
    addEdge(adj, 1, 3); 
    addEdge(adj, 1, 4); 
    addEdge(adj, 2, 3); 
    addEdge(adj, 3, 4); 
    printGraph(adj, V); 
    return 0; 
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值