无向图的深度优先生成森
来源:互联网 发布:asp.net php学那个好 编辑:程序博客网 时间:2024/05/18 00:28
对于无向图,从任意一个顶点出发,根据深度优先遍历,可以得到一个连通分量。对于非连通图,每一次从一个未访问的顶点出发,可以得到多个连通分量。将这些通过深度优先遍历求得连通分量构成一棵棵生成树,并以兄弟孩子链表,将这些生成树以森林的形式存储。
将这个森林(二叉树)进行先序遍历,得到的结果和深度优先遍历的结果一致!
参考:数据结构c语言版
以下面非连通无向图为例:
原图:
生成深度优先森林(孩子兄弟结构)
输入图的信息0 (表示无向图)10 (节点数目)10(弧数目)1 2 0 (弧的两个端点,权重)1 3 02 4 02 5 03 6 03 7 04 8 05 8 06 7 09 10 0(邻接链表表示)V1--> < 3 , 0 > --> < 2 , 0 > V2--> < 5 , 0 > --> < 4 , 0 > --> < 1 , 0 > V3--> < 7 , 0 > --> < 6 , 0 > --> < 1 , 0 > V4--> < 8 , 0 > --> < 2 , 0 > V5--> < 8 , 0 > --> < 2 , 0 > V6--> < 7 , 0 > --> < 3 , 0 > V7--> < 6 , 0 > --> < 3 , 0 > V8--> < 5 , 0 > --> < 4 , 0 > V9--> < 10 , 0 > V10--> < 9 , 0 >
C++ 代码实现:
森林结构
class CSNode{public: CSNode(); ~CSNode(); CSNode(int d) ; void setDate(int d) ; void setFirstChild(CSNode *firstC); void setNextBrother(CSNode *nextB) { nextBrother = nextB; } int getDate() ; CSNode *getFirstChild() ; CSNode *getNextBrother() ;private: int data; CSNode *firstChild; CSNode *nextBrother;};class CSTree{public: CSTree() { root = NULL; } CSTree(CSNode * r) { root = r; } CSNode *getRoot() { return root; } void setRoot(CSNode * r) { root = r; } void printCSTree(); // 深度优先森林树的先序遍历,即深度优先搜索结果private: CSNode * root;};
生成生成树核心代码:
void ALGraph::ALGraphDFS(CSNode*root) // 指定某个节点广度优先搜索{ ArcNode* arcnode; CSNode*ch = NULL; VNode *vnode; int vIndex; int start = root->getDate(); vnode = &(vetices[start]); vetices[start].setVColor('g'); arcnode = vnode->getFirstArc(); int first = 1; // 标识为第一个邻接点 for (; arcnode != NULL; arcnode = arcnode->getNextarc()) { vIndex = arcnode->getAdjvex() - 1; if (vetices[vIndex].getVColor() == 'w') { vetices[vIndex].setVColor('g'); ch = new CSNode(vIndex); if (first) { root->setFirstChild(ch); first = 0; } else { root->getFirstChild()->setNextBrother(ch); } ALGraphDFS(ch); } }}void ALGraph::getCSTree(CSTree* T){ CSNode *root = NULL, *eldBrother = NULL; // 单棵树的根节点 vector<VNode*> vistNodeVec; for (int vIndex = 0; vIndex < this->getVexNum(); vIndex++) { this->vetices[vIndex].setVColor('w'); } for (int vIndex = 0; vIndex < this->getVexNum(); vIndex++) { if (this->vetices[vIndex].getVColor() == 'w') { vetices[vIndex].setVColor('g'); if (!T->getRoot()) // 森林的第一个根节点 { root = new CSNode(vIndex); T->setRoot(root); } else { root = new CSNode(vIndex); eldBrother->setNextBrother(root); } eldBrother = root; ALGraphDFS(root); // 建立以 root 为根的生成树 } }}
主函数:
#include<iostream>#include "ALGraph.h"using namespace std;void main(){ ALGraph *alGraph = new ALGraph(); alGraph->createALGraph(); alGraph->printALGraph(); alGraph->ALGraphBFS(); alGraph->ALGraphDFS(); CSTree *T = new CSTree(); alGraph->getCSTree(T); T->printCSTree();}
完整代码下载地址:
http://download.csdn.net/detail/lmx2014001/9622467
0 0
- 无向图的深度优先生成森
- 无向图的深度优先生成树
- 无向图的深度优先遍历
- 无向图的深度优先搜索
- 无向图的深度优先搜索
- 无向图的深度优先搜索
- 无向图的深度优先搜索
- 深度优先生成树(无向图,邻接矩阵,DFS)
- 无向图中的深度优先生成森林
- 看数据结构写代码(40) 无向图的深度优先生成树与广度优先生成树
- 有向图无向图领接表深度优先广度优先最小生成树
- Java实现图:邻接矩阵表示、深度优先搜索、广度优先搜索、无向图的最小生成树
- 对无向图的深度优先遍历的理解
- 无向图邻接表的深度优先遍历(DFS)
- 无向图的邻接矩阵 -- DFS - 深度优先遍历
- 数据结构无向图的深度优先遍历
- 无向图的深度优先搜索(最终版)
- 无向图邻接矩阵的储存和深度优先遍历
- unity - 二进制文件操作-存储与读取
- java工程如何打jar包和war包
- [CSU 1804 有向无环图] DP+拓扑排序
- Android databinding笔记
- 红黑树和AVL树的效率对比
- 无向图的深度优先生成森
- Request对象的主要方法:
- 1036. Boys vs Girls (25)
- mysql千万级数据表结构修改
- synchronized 详解
- eclipse上传代码到gitbub
- 1. JMeter 3.0 环境搭建
- 从机器学习谈起
- 实现Socket通信