图的存储实现方法+邻接表(邻接表矩阵+图的点数和边数)+邻接矩阵(顶点表+邻接矩阵+图的点数和边数)

本文介绍了图的两种基本存储方式:邻接矩阵和邻接表。详细解释了邻接矩阵的构建过程及其代码实现,并展示了如何通过邻接矩阵来表示图的连接关系。此外,还探讨了从邻接矩阵向邻接表的转换方法。

图的实现方法:

1.顶点表+邻接矩阵+图的点数和边数

2.邻接表矩阵+图的点数和边数

邻接矩阵表示:有边值为1(自己到自己没边的为0)

#include<stdio.h>
int main()
{
	int arr[8][8] = {{0, 1, 1, 1, 1, 1, 0, 0},  
					 {0, 0, 1, 0, 1, 0, 0, 0},  
					 {0, 0, 0, 1, 0, 0, 0, 0},  
					 {0, 0, 0, 0, 1, 0, 0, 0},  
					 {0, 0, 0, 0, 0, 1, 0, 0},  
					 {0, 0, 1, 0, 0, 0, 1, 1},  
					 {0, 0, 0, 0, 0, 1, 0, 1}, 
					 {0, 0, 0, 0, 0, 1, 1, 0}} ;
	for(int i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
			printf(" %d",arr[i][j]);

		printf(" \n");
	}   
	return 0;
}

对于邻接矩阵参考书上更加详细的构造:

用邻接矩阵表示法表示图,除了一“个用于存储邻接矩阵的二维数组外",还需要一个一维数组用于存储顶点信息。

//-----图的邻接矩阵存储表示-----
#include <iostream>
using namespace std;

#define MaxInt 32767
//表示极大值,即∞
#define MVNum 100
//最大顶点数
typedef char VerTexType;//假设顶点的数据类型为字符型
typedef int ArcType;//假设边的权值类型为整型
typedef struct{
VerTexType vexs [MVNum];//顶点表
ArcType arcs [MVNum] [MVNum];//邻接矩阵
int vexnum, arcnum;//图的当前点数和边数
} AMGraph;
int CreateUDN (AMGraph &G)
{
	cin>>G.vexnum>>G.arcnum;//输人总顶点数,总边数
	for(i=0;i<G.vexnum;i++)
		cin>>G.vexs[i];//依次输入点的信息(数组一)

	for(i=0;i<G.vexnum;++i)//邻接矩阵初始化
		for(j=0;j<G.vexnum; ++j)
		G.arcs[i] [j] =MaxInt;

	for(k=0;k<G.arcnum;++k)//构造邻接矩阵
	{
		cin>>v1>>v2>>w; //输人一条边依附的顶点及权值
		i=locateVex(G, v1) ;j=LocateVex(G,v2); //确定v1和v2在G中的位置,即顶点数组的下标
		G.arcs[i][j]=w; //边<v1, v2>的权值置为W
		G.arcs[j][i]=G.arcs[i][j];//置<v1, v2>的对称边<v2, v1>的权值为w
	}
	return 1;
}

邻接表

邻接链表
邻接链表

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

typedef struct arcn //边类型 边的定义(或者树中孩子的定义)还是最简单的链表形式
{ int id;/*边另一端的数组索引*/  arcn *next; };//为了简化,边没有权重

typedef struct vn //顶点数组
{ arcn *next;}; 

typedef struct  //图的构成
{ vn adjlist[100]; int vnum,arcnum;} G;
    vector<vector<int>> buildGraph(vector<vector<int>>& prerequisites,int numCourses) { // prerequisites    numCourses 
        vector<vector<int>> graph(numCourses);
        inDegree.resize(numCourses,0);
        for (auto pre: prerequisites) {
            int from = pre[1];
            int to = pre[0];
            graph[from].push_back(to);
            inDegree[to]++;
        }
        return graph;
    }

CG

  • 在线调试
  • 带权重的图又称为网络
  • (邻接矩阵表示无向图可以只存下三角,网络需要引入#define INF 10000 )
/* * 基于邻接边实现的顶点结构 */ package dsa; public class Vertex_List implements Vertex { //变量 protected Object info;//当前顶点中存放的数据元素 protected Position vPosInV;//当前顶点在所属的顶点表V中的位置 protected List outEdges;//关联边:存放以当前顶点为尾的所有边(的位置) protected List inEdges;//关联边:存放以当前顶点为头的所有边(的位置) protected int status;//(在遍历等操作过程中)顶点的状态 protected int dStamp;//时间标签:DFS过程中该顶点被发现时的时刻 protected int fStamp;//时间标签:DFS过程中该顶点被访问结束时的时刻 protected int distance;//到指定起点的距离:BFS、Dijkstra等算法所确定该顶点到起点的距离 protected Vertex bfsParent;//在最短距离树(BFS或BestFS)中的父亲 //构造方法:在G中引入一个属性为x的新顶点 public Vertex_List(Graph G, Object x) { info = x;//数据元素 vPosInV = G.insert(this);//当前顶点在所属的顶点表V中的位置 outEdges = new List_DLNode();//出边 inEdges = new List_DLNode();//入边 status = UNDISCOVERED; dStamp = fStamp = Integer.MAX_VALUE; distance = Integer.MAX_VALUE; bfsParent = null; } //返回当前顶点的信息 public Object getInfo() { return info; } //将当前顶点的信息更新为x,并返回原先的信息 public Object setInfo(Object x) { Object e = info; info = x; return e; } //返回当前顶点的出、入度 public int outDeg() { return outEdges.getSize(); } public int inDeg() { return inEdges.getSize(); } //返回当前顶点所有关联边、关联边位置的迭代器 public Iterator inEdges() { return inEdges.elements(); } public Iterator inEdgePositions() { return inEdges.positions(); } public Iterator outEdges() { return outEdges.elements(); } public Iterator outEdgePositions() { return outEdges.positions(); } //取当前顶点在所属的的顶点集V中的位置 public Position getVPosInV() { return vPosInV; } //读取、设置顶点的状态(DFS + BFS) public int getStatus() { return status; } public int setStatus(int s) { int ss = status; status = s; return ss; } //读取、设置顶点的时间标签(DFS) public int getDStamp() { return dStamp; } public int setDStamp(int s) { int ss = dStamp; dStamp = s; return ss; } public int getFStamp() { return fStamp; } public int setFStamp(int s) { int ss = fStamp; fStamp = s; return ss; } //读取、设置顶点至起点的最短距离(BFS) public int getDistance() { return distance; } public int setDistance(int s) { int ss = distance; distance = s; return ss; } //读取、设置顶点在的DFS、BFS、BestFS或MST树中的父亲 public Vertex getBFSParent() { return bfsParent; } public Vertex setBFSParent(Vertex s) { Vertex ss = bfsParent; bfsParent = s; return ss; } }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值