图的遍历算法

  • 邻接矩阵的DFS和BFS

  • #include<iostream>
    #include<vector>
    #include<queue>
    #define MAXVEX 100
    using namespace std;
    struct MGraph {
    	int numV;
    	int numE;
    	char vexs[MAXVEX];
    	int arc[MAXVEX][MAXVEX];
    };
    
    void DFS(MGraph* G, int index, vector<bool>&flag)
    {
    	int j;
    	flag[index] = true;
    	cout << G->vexs[index] << " ";
    	for (j = 0; j < G->numV; j++)
    	{
    		if (G->arc[index][j] == 1 && !flag[j])
    		{
    			DFS(G, j, flag);
    		}
    	}
    }
    
    void BFS(MGraph* G)
    {
    	int i, j;
    	queue<int>q;
    	vector<bool>flag(G->numV);
    	for (i = 0; i < G->numV; i++)
    	{
    		if (!flag[i])
    		{
    			flag[i] = true;
    			q.push(i);
    			while (!q.empty())
    			{
    				int index = q.front();
    				cout << G->vexs[index] << " ";
    				for (j = 0; j < G->numV; j++)
    				{
    					if (G->arc[index][j]==1 && !flag[j])
    					{
    						q.push(j);
    						flag[j] = true;
    					}
    				}
    				q.pop();
    			}
    		}
    	}
    }
    
    int main()
    {
    	MGraph* G = new MGraph;
    	cout << "请输入图中的点数:" << endl;
    	cin >> G->numV;
    	cout << "请输入图中的边数:" << endl;
    	cin >> G->numE;
    	cout << "请输入顶点信息:" << endl;
    	for (int i = 0; i < G->numV; i++)
    	{
    		cin >> G->vexs[i];
    	}
    	cout << "请输入边的信息:" << endl;
    	for (int i = 0; i < G->numE; i++)
    	{
    		int e1, e2, weight;
    		//无向图
    		cin >> e1 >> e2;
    		G->arc[e1 - 1][e2 - 1] = 1;
    		G->arc[e2 - 1][e1 - 1] = 1;
    	}
    	//DFS
    	cout << "DFS遍历结果:" << endl;
    	vector<bool>flag(G->numV);
    	for (int i = 0; i < G->numV; i++)
    	{
    		if (!flag[i])
    			DFS(G, i, flag);
    	}
    	cout << endl;
    
    	//BFS
    	cout << "BFS遍历结果:" << endl;
    	BFS(G);
    	cout << endl;
    
    	return 0;
    }

  • 邻接表的DFS和BFS

    #include<iostream>
    #include<vector>
    #include<queue>
    using namespace std;
    struct EdgeNode
    {
    	int adjvex;
    	struct EdgeNode* next;
    };
    
    struct VexNode
    {
    	char data;
    	EdgeNode* firstedge;
    };
    
    struct GraphAdjList
    {
    	int numVex;
    	int numEdge;
    	VexNode adjList[100];
    };
    void DFS(GraphAdjList* G, vector<bool>&flag,int index)
    {
    	flag[index] = true;
    	cout << G->adjList[index].data << " ";
    	EdgeNode* p = G->adjList[index].firstedge;
    	while (p)
    	{
    		if (!flag[p->adjvex])
    		{
    			DFS(G, flag, p->adjvex);
    		}
    		p = p->next;
    	}
    }
    void BFS(GraphAdjList* G)
    {
    	queue<int>q;
    	vector<bool>flag(G->numVex);
    	for (int i = 0; i < G->numVex; i++)
    	{
    		if (!flag[i])
    		{
    			q.push(i);
    			flag[i] = true;
    			while (!q.empty())
    			{
    				int index = q.front();
    				q.pop();
    				cout << G->adjList[index].data << " ";
    				EdgeNode* p = G->adjList[index].firstedge;
    				while (p)
    				{
    					if (!flag[p->adjvex])
    					{
    						q.push(p->adjvex);
    						flag[p->adjvex] = true;
    					}
    					p = p->next;
    				}
    			}
    		}
    	}
    }
    int main()
    {
    	GraphAdjList* G = new GraphAdjList();
    	EdgeNode* e = nullptr;
    	cout << "请输入邻接表中的点数和边数:" << endl;
    	cin >> G->numVex >> G->numEdge;
    	cout << "请输入邻接表中点的信息:" << endl;
    	for (int i = 0; i < G->numVex; i++)
    		cin >> G->adjList[i].data;
    	cout << "请输入邻接表中边的信息:" << endl;
    	for (int i = 0; i < G->numEdge; i++)
    	{
    		//从0开始
    		int e1, e2;
    		cin >> e1 >> e2;
    		e = new EdgeNode();
    		e->adjvex = e2;
    		e->next = G->adjList[e1].firstedge;
    		G->adjList[e1].firstedge = e;
    
    		e = new EdgeNode();
    		e->adjvex = e1;
    		e->next = G->adjList[e2].firstedge;
    		G->adjList[e2].firstedge = e;
    	}
    
    	vector<bool>flag(G->numVex);
    	//DFS遍历图
    	cout << "DFS遍历结果:";
    	for (int i = 0; i < G->numVex; i++)
    	{
    		if (!flag[i])
    		{
    			DFS(G, flag,i);
    		}
    	}
    	cout << endl;
    	//BFS遍历图
    	cout << "BFS遍历结果:";
    	BFS(G);
    	cout << endl;
    	return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值