C++ 图的深度搜索广度搜索和最小生成树代码实现

本文介绍了一个图论算法的实现过程,包括图的创建、深度优先搜索(DFS)、广度优先搜索(BFS)以及最小生成树算法。通过具体的C++代码示例,详细展示了如何从创建节点到构建图,再到实现不同类型的遍历方法,最后计算出图的最小生成树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件

Node.h

#pragma once
#include<iostream>
using namespace std;
class  Node
{
public:
	int value;
	int weight;
	Node *next;
	Node();
};


Node_list.h

#pragma once
#include"Node.h"
class Node_list
{
public:
	int score;
	int vertex;
	Node *first;
	Node_list();
};

Graph.h

#pragma once
#include"Node_list.h"
#include<queue>
class Graph
{
public:
	int *visited;
	int NodeNum;
	void Creat();
	void DFS(int);
	void BFS(int);
	void Creatvisit();
	int Find(int);
	Node_list *nodelist;
};

MGraph.h

#pragma once
#include<iostream>
#include"Graph.h"
using namespace std;
class MGraph
{
public:
	int **mGraphArry;
	int *adjvex;
	int *lowcost;
	int *vertex;
	int Pos;
	void GetMGraphArry(Graph);
	void ShowMGraphArry();
	void GetMinTree();
	~MGraph();
};

源文件

Node.cpp

#include"Node.h"
Node::Node()
{
	value = -1;
	weight = 0;
	next = NULL;
}

Node_list.cpp

#include"Node_list.h"
Node_list::Node_list()
{
	vertex = 0;
	score = 0;
	first = new Node();
}

MGraph.cpp

#include"MGraph.h"
void MGraph::GetMGraphArry(Graph graph)
{	
	Pos = graph.NodeNum;
	mGraphArry = new int*[graph.NodeNum];
	vertex = new int[graph.NodeNum];
	for (int i = 0; i < graph.NodeNum; i++)
	{
		vertex[i] = graph.nodelist[i].vertex;
	}
	for (int i = 0; i < graph.NodeNum; i++)
	{
		mGraphArry[i] = new int[graph.NodeNum];
	}
	for (int i = 0; i < graph.NodeNum; i++)
	{ 
		for (int j = 0; j < graph.NodeNum; j++)
		{
			
			if (i == j)
			{
				mGraphArry[i][j] = 0;
			}
			else
				mGraphArry[i][j] = 1000000;
		}
	}
	for (int i = 0; i < graph.NodeNum; i++)
	{
		Node *Node; 
		Node = graph.nodelist[i].first->next;
		while (Node)
		{
			mGraphArry[i][Node->value]=Node->weight;
			Node = Node->next;
		}
	}
}

void MGraph::ShowMGraphArry()
{
	for (int i = 0; i < Pos; i++)
	{
		for (int j = 0; j < Pos; j++)
		{
			cout << mGraphArry[i][j] << " ";
		}
		cout << endl;
	}
}
void MGraph::GetMinTree()
{
	adjvex = new int[Pos];
	lowcost = new int[Pos];
	for (int i = 0; i < Pos; i++)
	{
		adjvex[i] = vertex[0];
		lowcost[i] = mGraphArry[0][i];
	}
	
	for (int i = 1; i < Pos; i++)
	{
		int min = 10000000;
		int pos=0;
		for (int j = 0; j < Pos; j++)
		{
			if (lowcost[j] && lowcost[j] < min)
			{
				min = lowcost[j];
				pos = j;
			}
		}
		cout << "(" << adjvex[pos] << "," << vertex[pos] << ")" << endl;
		lowcost[pos] = 0;
		for (int t = 0; t < Pos; t++)
		{
			if (mGraphArry[pos][t]&&mGraphArry[pos][t] < lowcost[t])
			{
				adjvex[t] = vertex[pos];
				lowcost[t] = mGraphArry[pos][t];
			}
		}
	}
}

MGraph::~MGraph()
{
	for (int i = 0; i <Pos; i++)
	{
		delete mGraphArry[i];
	}
	delete vertex;
}


Graph.cpp

#include"Graph.h"
void Graph::Creat()
{
	cout << "输入节点个数:";
	cin >> NodeNum;
	nodelist = new Node_list[NodeNum];
	cout << "输入各个点的值:" << endl;
	for (int i = 0; i < NodeNum; i++)
	{
		cin >> nodelist[i].vertex;
		//nodelist[i].score=i;
	}
	cout << "请以此输入出节点 入节点 权值" << endl;
	int NUM1, NUM2,wight;
	while (cin>>NUM1>>NUM2>>wight&&(NUM1+NUM2))
	{
		Node *p;
		int Pos1=0,Pos2=0;
		for (int i = 0; i < NodeNum; i++)
		{
			if (nodelist[i].vertex == NUM1)
			{
				Pos1 = i;
				break;
			}
		}
		for (int i = 0; i < NodeNum; i++)
		{
			if (nodelist[i].vertex == NUM2)
			{
				Pos2 = i;
				break;
			}
		}
		p = nodelist[Pos1].first;
		while (p->next != NULL)
		{
			p = p->next;
		}
		Node *s = new Node();
		s->value = Pos2;
		s->weight = wight;
		p->next = s;
	}

}

void Graph::Creatvisit()
{
	visited = new int[NodeNum];
	for (int i = 0; i < NodeNum; i++)
	{
		visited[i] = 0;
	}
}

void Graph::DFS(int n)
{
	cout << nodelist[n].vertex << " ";
	visited[n] = 1;
	Node *w = nodelist[n].first->next;
	while (w != NULL)
	{
		if (visited[w->value] == 0)
		{
			DFS(w->value);
		}
		w = w->next;
	}
}

void Graph::BFS(int n)
{
	queue<Node*> queue1;
	cout << nodelist[n].vertex << " ";
	visited[n] = 1;
	queue1.push(nodelist[n].first);
	while (!queue1.empty())
	{
		Node *v = queue1.front();
		queue1.pop();
		Node *w = v->next;
		while (w != NULL)
		{
			if (visited[w->value] == 0)
			{
				cout << nodelist[w->value].vertex << " ";
				visited[w->value] = 1;
				queue1.push(nodelist[w->value].first);
			}
			w = w->next;
		}
	}
}

int Graph::Find(int NUM)
{
	int Pos1 = 0;
	for (int i = 0; i < NodeNum; i++)
	{
		if (nodelist[i].vertex == NUM)
		{
			Pos1 = i;
			return Pos1;
		}
	}
}

Main,cpp

#include"Graph.h"
#include"MGraph.h"
int main()
{
	Graph graph;
	MGraph mGraph;
	graph.Creat();
	graph.Creatvisit();
	int Num;
	cout << "DFS输入开始节点:";
	cin >> Num;
	graph.DFS(graph.Find(Num));
	cout << endl;
	graph.Creatvisit();
	cout << "BFS输入开始节点:";
	cin >> Num;
	graph.BFS(graph.Find(Num));
	cout << endl;
	cout << "链接矩阵" << endl;
	mGraph.GetMGraphArry(graph);
	mGraph.ShowMGraphArry();
	cout << endl;
	cout << "最小生成树" << endl;
	mGraph.GetMinTree();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值