图的创建和遍历

本文介绍了图的两种常见存储方式——邻接矩阵和邻接表,并提供了C++实现。邻接矩阵适用于稠密图,占用空间大但操作简单;邻接表适合稀疏图,空间利用率高但操作相对复杂。同时,文章还展示了广度优先遍历(BFS)和深度优先遍历(DFS)的算法,用于图的遍历和搜索。
摘要由CSDN通过智能技术生成

一、图的创建

存储方法 G(v,e)
1. 邻接矩阵法(二维数组)   
	①实现容易  
	②操作容易   
	③浪费空间(使用静态数组)  
	④占据空间	O(V^2)  	⭐    记住这个其他反推
 	⑤搜索效率  O(v)+O(e)     ⭐
 	⑥适用于稠密图     e>vlogv
 	⑦可适用压缩矩阵方法存储(无向图)      
2. 邻接表法
	①实现困难
	②不适合于删除点、删除边,效率很低且操作复杂  ⭐
	③适合稀疏图
	④邻接表的顶点邻接顺序不是固定的,因此搜索的结果可能不同  ⭐
	⑤邻接表不适合处理带权图,需要多一个结点域存储,大大增加空间
	⑥占据空间   O(V)+O(E) (有向图)  无向图为O(v)+2O(E)  ⭐
	⑦搜索效率   O(V^2)   ⭐

邻接图

请添加图片描述

邻接矩阵(无向图)
#pragma once
#define MaxSize 10
//!!!!
#include<iostream>
#include"Queue.h"
using namespace std;
//邻接矩阵_无向
class MGraph_ND {
public:
	int Vertex[MaxSize];
	int Edge[MaxSize][MaxSize];
	int Vertex_num = 0;
	int Edge_num = 0;
	MGraph_ND(int mode) {
		if (mode == 1) {

			std::cout << "请输入顶点,输入-1结束" << endl;
			int a = 0;
			cin >> a;
			if (a == -1) return;
			while (a != -1) {
				Vertex[this->Vertex_num++] = a;
				cin >> a;
			}
			cout << endl;
			std::cout << "请输入边之间的关系,0表示边不存在,1表示边存在,大于1表示权值" << endl;
			for (int i = 0; i < Vertex_num; i++) {
				cout << "结点:" << i << " ";
				for (int j = 0; j < Vertex_num; j++) {
					cin >> a;
					if (a > 0 && Edge[j][i] < 1) { this->Edge_num++; }				//无向图处理
					this->Edge[i][j] = a;
				}
			}
				
		}
		else
		{
			int a[] = { 0,1,2,3,4 };
			int b[][5] = { {0,1,0,1,0},{1,0,1,0,1},{0,1,0,1,1},{1,0,1,0,0},{0,1,1,0,0} };
			this->Vertex_num = 5;
			this->Edge_num = 6;
			for (int i = 0; i < 5; i++) {
				this->Vertex[i] = a[i];
			}
			for (int i = 0; i < 5; i++) {
				for (int j = 0; j < 5; j++) {
					this->Edge[i][j] = b[i][j];
				}
			}
		}


	}

	//点x,y是否有边<x,y>
	bool Adjacent(int x, int y) {
		if (x < 0 || y < 0 || x >= this->Vertex_num || y >= this->Vertex_num)return false;
		if (x == y)return false;//无环
		if (Edge[Vertex[x]][Vertex[y]] >= 1)return true;
		return false;

	};

	//返回点x的所有相邻结点
	Queue<int>* Neighbors(int x) {
		Queue<int>* que = new Queue<int>();
		if (x < 0 || x >= Vertex_num)return que;
		for (int i = 0; i < this->Vertex_num; i++) {
			if (Edge[x][i] > 0) {
				que->EnQueue(i);
			}
		}
		return que;

	};

	//往顺序结点后新增一个结点
	int InsertVertex() {
		this->Vertex[this->Vertex_num] = this->Vertex_num;
		this->Vertex_num++;
		return this->Vertex_num - 1;
	};

	//删除对应结点,并抛弃对应空间
	bool DeleteVertex(int x) {
		if (x<0 || x>=Vertex_num)return false;
		else {
			for (int i = 0; i < this->Vertex_num; i++) {
				if (this->Edge[x][i] != 0)Edge_num--;
				this->Edge[x][i] = 0;
			}

			for (int i = 0; i < this->Vertex_num; i++) {
				this->Edge[i][x] = 0;
			}
			Vertex[x] = -1;//删了以后这个空间就再也不能用了
			Vertex_num--;
			return true;
		}
	};

	bool AddEdge(int x, int y,int weight) {
		if (x < 0 || y < 0 || x >= this->Vertex_num || y >= this->Vertex_num)return false;
		if (x == y)return false;//无环
		if (weight < 1)return false;
		if (Edge[x][y] > 0)return false;//已有边
		this->Edge[x][y] = weight;
		this->Edge[y][x] = weight;
		Edge_num++;
		return true;
	};

	bool RemoveEdge(int x, int y) {
		if (x < 0 || y < 0 || x >= this->Vertex_num || y >= this->Vertex_num)return false;
		if (x == y)return false;//无环
		if (Edge[x][y] == 0)return false;//不存在边
		this->Edge[x][y] = 0;
		this->Edge[y][x] = 0;
		Edge_num--;
		return true;
	};

	int FirstNeighbor(int x)
	{
		if (x < 0 || x >= this->Vertex_num)return -1;
		Queue<int>* que = Neighbors(x);
		if (que->Empty())return -1;
		return que->GetHead()->data;
	};
	//除y以外x的邻接点
	int NextNeighbor(int x, int y)
	{
		if (x < 0 || y < 0 || x >= this->Vertex_num || y >= this->Vertex_num)return -1;
		Queue<int>* que = Neighbors(x);
		if (que->Empty())return -1;
		int a;
		que->DeQueue(a);
		while (a != y && !que->Empty()) {
			que->DeQueue(a);
		}
		if (que->Empty())return -1;//没找到邻接点y或y是最后一个邻接点
		que->DeQueue(a);
		return a;

		
	};

	int Get_edge_value(int x, int y) {
		if (x < 0 || y < 0 || x >= this->Vertex_num || y >= this->Vertex_num)return -1;
		return Edge[x][y];// 0 或 1 或 大于1
	};
};
邻接表(无向图)
#pragma once
#define MaxSize 100
#include<iostream>
#include"Queue.h"
using namespace std;
//边表结点
class ArcNode {
public:
	int Vertex_Pos;
	ArcNode* next;
	ArcNode() {
		Vertex_Pos = 0;
		next = nullptr;
	}
	ArcNode(int vertex_pos) {
		this->Vertex_Pos = vertex_pos;
	}
};
//顶点表结点
class VNode {
public:
	int V_num;//当前点的邻接结点总数
	ArcNode* Head;//Head的data域可有可无
	VNode() {
		V_num = 0;
		Head = new ArcNode();
	}
};

//邻接表_无向  无环
class ALGraph_ND {
public:
	VNode* Vnode[MaxSize];
	int Vertex_num = 0;
	int Edge_num = 0;
	ALGraph_ND(int mode) {
		if (mode == 1) {
			std::cout << "请输入顶点,输入-1结束" << endl;
			int a = 0;
			cin >> a;
			if (a == -1) return;
			while (a != -1) {
				Vnode[Vertex_num] = new VNode();
				Vnode[Vertex_num++]->Head->Vertex_Pos = a;
				cin >> a;
			}
			cout << endl;
			std::cout << "请输入关联结点 输入-1结束当前结点创建 注意无向图的对称" << endl;
			for (int i = 0; i < Vertex_num; i++) {
				cout << "结点:" << Vnode[i]->Head->Vertex_Pos<<"      ";
				cin >> a;
				ArcNode* p = Vnode[i]->Head;
				while (a != -1) {
					Vnode[i]->V_num++;
					Edge_num++;
					p->next = new ArcNode();
					p->next->Vertex_Pos = a;
					p = p->next;
					cin >> a;
				}

			}
			Edge_num = Edge_num / 2;//无向图边重复
			
		}
		else
		{
			int a[] = { 0,1,2,3,4 };
			int b[][5] = { {0,1,0,0,1},{1,0,1,1,1},{0,1,0,1,0},{0,1,1,0,1},{1,1,0,1,0} };
			Vertex_num = 5;
			Edge_num = 7;
			for (int i = 0; i < 5; i++) {
				Vnode[i] = new VNode();
				ArcNode* p = Vnode[i]->Head;
				p->Vertex_Pos = i;
				for (int j = 0; j < 5; j++) {
					if (b[i][j] > 0) {
						Vnode[i]->V_num++;
						p->next = new ArcNode();
						p->next->Vertex_Pos =j;
						p = p->next;
						
					}
				}
			}
		}


	}

	//点x,y是否有边<x,y>
	bool Adjacent(int x, int y) {
		if (x < 0 || y < 0 || x >= this->Vertex_num || y >=this->Vertex_num)return false;
		if (x == y)return false;//无环
		if (Vnode[x]->Head->next == nullptr)return false;//无边相连
		ArcNode* p = Vnode[x]->Head;
		while (p->next!= nullptr)
		{
			p = p->next;
			if (p->Vertex_Pos == y)return true;		
		}
		return false;
	};

	//返回点x的所有相邻结点
	Queue<int>* Neighbors(int x) {
		Queue<int>* que = new Queue<int>();
		if (x < 0 || x >=Vertex_num)return que;
		ArcNode* p = Vnode[x]->Head->next;
		while (p->next != nullptr)
		{
			p = p->next;
			que->EnQueue(p->Vertex_Pos);	
		}
		return que;

	};

	//往顺序结点后新增一个结点
	int InsertVertex() {
		Vnode[Vertex_num] = new VNode();
		Vnode[Vertex_num]->Head->Vertex_Pos = Vertex_num;
		Vertex_num++;
		return Vertex_num;
	};

	//删除对应结点,并抛弃对应空间  较难,时间复杂度搞
	bool DeleteVertex(int x) {
		if (x < 0 || x >= Vertex_num)return false;
		else {
			for (int i = 0; i <Vertex_num;i++) {
				if (i == x)continue;//被删结点不需处理
				if (Vnode[i]->V_num == 0)continue;//无相连结点
				if (Vnode[i]->Head->next->Vertex_Pos == x)
				{
					ArcNode* p = Vnode[i]->Head->next;
					Vnode[i]->Head->next = p->next;
					delete p;
					Edge_num--;
					Vnode[i]->V_num--;
					continue;
				}
				ArcNode* before;
				ArcNode* current;
				ArcNode* after;
				before = Vnode[i]->Head;
				current = Vnode[i]->Head->next;//删除的是current
				after = Vnode[i]->Head->next->next;
				while (current!= nullptr)
				{
					if (current->Vertex_Pos == x) {
						delete current;
						before->next = after;
						Edge_num--;
						Vnode[i]->V_num--;
						break;
					}
					before = before->next;
					current = current->next;
					if (after == nullptr)break;//current在链尾,after为nullptr
					after = after->next;
				}
			
			}
			Vertex_num--;
			delete Vnode[x];
			return true;
		}
	};

	bool AddEdge(int x, int y) {
		if (x < 0 || y < 0 || x >= this->Vertex_num || y >this->Vertex_num)return false;
		if (x == y)return false;//无环
		ArcNode* p = Vnode[x]->Head;
		ArcNode* q = Vnode[y]->Head;

		while (p->next != nullptr)
		{
			p = p->next;
			if (p->Vertex_Pos == y) {
				return false;//重复 }

			}
		}
			p->next = new ArcNode();
			p->next->Vertex_Pos = y;
			Vnode[x]->V_num++;
			Edge_num++;
	

		while (q->next != nullptr)
		{
			q = q->next;
		}
		q->next = new ArcNode();
		q->next->Vertex_Pos = x;
		Vnode[y]->V_num++;
		return true;

	};

	//类似删除结点
	bool RemoveEdge(int x, int y) {
		if (x < 0 || y < 0 || x >= this->Vertex_num || y >= this->Vertex_num)return false;
		if (x == y)return false;
		if (Vnode[x]->V_num == 0)return false;//无相连结点
		ArcNode* p = Vnode[x]->Head;
		ArcNode* q = Vnode[y]->Head;
		//三指针法删除结点
		if (Vnode[x]->Head->next->Vertex_Pos == y)
		{
			ArcNode* p = Vnode[x]->Head->next;
			Vnode[x]->Head->next = p->next;
			delete p;
			Edge_num--;
			Vnode[x]->V_num--;
		}
		else {
			ArcNode* before;
			ArcNode* current;
			ArcNode* after;
			before = Vnode[x]->Head;
			current = Vnode[x]->Head->next;//删除的是current
			after = Vnode[x]->Head->next->next;
			while (current != nullptr)
			{
				if (current->Vertex_Pos == y) {
					delete current;
					before->next = after;
					Edge_num--;//上下相同 只减一份
					Vnode[x]->V_num--;
					break;
				}
				before = before->next;
				current = current->next;
				if (after == nullptr)break;//current在链尾,after为nullptr
				after = after->next;
			}
		}
		if (Vnode[y]->Head->next->Vertex_Pos == x)
		{
			ArcNode* p = Vnode[y]->Head->next;
			Vnode[y]->Head->next = p->next;
			delete p;
			Vnode[y]->V_num--;
		}
		else {
			ArcNode* before;
			ArcNode* current;
			ArcNode* after;
			before = Vnode[y]->Head;
			current = Vnode[y]->Head->next;//删除的是current
			after = Vnode[y]->Head->next->next;
			while (current != nullptr)
			{
				if (current->Vertex_Pos == x) {
					delete current;
					before->next = after;
					Vnode[y]->V_num--;
					break;
				}
				before = before->next;
				current = current->next;
				if (after == nullptr)break;//current在链尾,after为nullptr
				after = after->next;
			}

		}
			
		return true;

	};

	int FirstNeighbor(int x)
	{
		if (Vnode[x]->V_num == 0)return -1;
		return Vnode[x]->Head->next->Vertex_Pos;
	};
	int NextNeighbor(int x, int y)
	{
		int flag = 0;
		if (Vnode[x]->V_num <2)return -1;
		ArcNode* p = Vnode[x]->Head;
		while (p->next!=nullptr)
		{
			p = p->next;
			if (p->Vertex_Pos == y)
			{
				flag = 1;
				break;
			}

		}
		if (flag == 1 && p->next != nullptr) {
			return p->next->Vertex_Pos;
		}
		return -1;

	};
};

二 遍历

 **广度优先遍历**
//对图进行广度搜索
	void BFSTraverse() {
		bool visited[MaxSize];
		Queue<int>* que = new Queue<int>();		//在此处初始化队列,同时处理多个联通分支
		int count_banch = 0;
		for (int i = 0; i < this->Vertex_num; i++) {
			visited[i] = false;
		}
		for (int i = 0; i < this->Vertex_num; i++) {
			//如果是连通图,这里只执行一次循环
			//如果是非联通图,则会调用多次BFS
			if (visited[i] == false) {
				cout << "连通分支:" << (++count_banch) << "         ";
				BFS(i, que, visited);
				cout << endl;
			}
		}


	}
	//广搜
	void BFS(int i, Queue<int>*& que, bool* visited) {
		que->EnQueue(i);
		Visit(i);
		visited[i] = true;
		int a = 0;
		while (!que->Empty()) {
			que->DeQueue(a);
			for (int x = FirstNeighbor(a); x != -1; x = NextNeighbor(a, x))
			{
				if (visited[x] == false) {
					Visit(x);
					visited[x] = true;//如果放在外层访问会出现重复
					que->EnQueue(x);
				}

			}

		}
	}
**深度优先遍历** (递归)
//对图进行深度搜索,使用工作栈
	void DFSTraverse() {
		bool visited[MaxSize];
		int count_banch = 0;
		for (int i = 0; i < this->Vertex_num; i++) {
			visited[i] = false;
		}
		for (int i = 0; i < this->Vertex_num; i++) {
			//如果是连通图,这里只执行一次循环
			//如果是非联通图,则会调用多次BFS
			if (visited[i] == false) {
				cout << "连通分支:" << (++count_banch) << "         ";
				DFS(i, visited);
				cout << endl;
			}
		}

	}
	//深搜
	void DFS(int i, bool* visited) {
		Visit(i);
		visited[i] = true;
		for (int a = FirstNeighbor(i); a != -1; a = NextNeighbor(i, a))
		{
			if (visited[a] == false) {
				DFS(a, visited);
			}
		}

	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值