看数据结构写代码(41) 强连通分量

首先介绍概念问题,在有向图中,若 顶点v1 到 v2 存成路径,并且 v2 到 v1 存成 路径,则称 顶点 v1 和 v2 是强连通的。若 有向图 任意两个节点 都是 强连通的,则 称为强连通图。非强连通图的 极大强连通子图,为 强连通分量。

特别说明,连通的概念 属于 无向图,强连通 属于 有向图。例如:无向图:连通图,连通分量,生成树; 有向图:强连通图,强连通分量。

数据结构书上 简单的 说了 一个 求 强连通 分量的 一个 办法,详细如下:

其实 自己 不太明白 这个算法的原理。 好在 实现 这个算法没什么问题。

除了这个方法 还有其他 三种 方法:Kosaraju,Tarjan和Gabow。以后
有空 都实现一下。

完整源代码:

源代码工程文件网盘地址:点击打开链接

// CrossLinkGraph.cpp : 定义控制台应用程序的入口点。
//有向图的十字链表表示法

#include "stdafx.h"
#include <cstdlib>

#define MAX_VEX_NUM 20
enum E_State
{
	E_State_Error = 0,
	E_State_Ok = 1,
};

struct ArcNode//弧节点
{
	int tailIndex;//弧尾位置
	int headIndex;//弧头位置
	ArcNode * tailNext;//下一个弧尾相同的弧
	ArcNode * headNext;//下一个弧头相同的弧
};

typedef struct VNode
{
	char vexName;//顶点名称
	ArcNode * firstIn;
	ArcNode * firstOut;
}GraphList[MAX_VEX_NUM];//

struct Graph
{
	GraphList list;//顶点数组.
	int vexNum,arcNum;
};
//获取弧 的 头节点
ArcNode * getHeadNode(){
	ArcNode * pNode = (ArcNode *)malloc(sizeof(ArcNode));
	if (pNode){
		pNode->headIndex = pNode->tailIndex = -1;
		pNode->headNext = pNode->tailNext = NULL;
	}
	return pNode;
}

ArcNode * getArcNode(int tailIndex,int headIndex){
	ArcNode * pNode = getHeadNode();
	if (pNode){
		pNode->headIndex = headIndex;
		pNode->tailIndex = tailIndex;
	}
	return pNode;
}

int vexLocation(Graph g,char vex){
	for (int i = 0; i < g.vexNum; i++){
		if (g.list[i].vexName == vex){
			return i;
		}
	}
	return -1;
}

void createGrahp(Graph * g){
	printf("输入图的顶点数 和 边(弧)数\n");
	scanf("%d%d%*c",&g->vexNum,&g->arcNum);
    //构造顶点集
    printf("请输入顶点集\n");
    for (int i = 0; i < g->vexNum; i++){
        char name;
		scanf("%c",&name);
		g->list[i].vexName = name;
		g->list[i].firstIn = g->list[i].firstOut = getHeadNode();//建立 头节点,并让头指针指向头节点
    }
    //构造顶点关系
    fflush(stdin);
    printf("请输入顶点的关系\n");
    for (int i = 0; i < g->arcNum; i++){
        char vex1,vex2;
		scanf("%c%c%*c",&vex1,&vex2);
		int location1 = vexLocation(*g,vex1);
		int location2 = vexLocation(*g,vex2);
		ArcNode * pNode = getArcNode(location1,location2);
		pNode->tailNext = g->list[location1].firstOut->tailNext;
		g->list[location1].firstOut->tailNext = pNode;
		pNode->headNext = g->list[location2].firstIn->headNext;
		g->list[location2].firstIn->headNext = pNode;
    }
}

//只要删除所有顶点的弧尾(或者弧头)节点即可,
//同时删除弧头弧尾 ,内存错误
void destoryGraph(Graph * g){
	for (int i = 0; i < g->vexNum; i++){
		ArcNode * next = g->list[i].firstOut;//删除所有弧尾
		while (next != NULL){
			ArcNode * freeNode = next;
			next = next->tailNext;
			free(freeNode);
		}
		g->list[i].firstIn = g->list[i].firstOut = NULL;
		g->list[i].vexName = ' ';
		g->vexNum = g->arcNum = 0;
	}
}

int firstOutAdj(Graph g,int location){
	ArcNode * next = g.list[location].firstOut->tailNext;
	if (next != NULL)
	{
		return next->headIndex;
	}
	return -1;
}

int nextOutAdj(Graph g,int location1,int location2){
	ArcNode * next = g.list[location1].firstOut->tailNext;
	while (next != NULL){//查找到 vex2
		if (next->headIndex == location2){
			next = next->tailNext;
			break;
		}
		next = next->tailNext;
	}
	if (next != NULL){
		return next->headIndex;
	}
	return -1;
}

int firstInAdj(Graph g,int location){
	ArcNode * next = g.list[location].firstIn->headNext;
	if (next != NULL)
	{
		return next->tailIndex;
	}
	return -1;
}

int nextInAdj(Graph g,int location1,int location2){
	ArcNode * next = g.list[location1].firstIn->headNext;
	while (next != NULL){//查找到 vex2
		if (next->tailIndex == location2){
			next = next->headNext;
			break;
		}
		next = next->headNext;
	}
	if (next != NULL){
		return next->tailIndex;
	}
	return -1;
}

void outDfs(Graph g,int i,bool * isVisited,int * finished,int * count){
	isVisited[i] = true;
	for (int next = firstOutAdj(g,i);next != -1; next = nextOutAdj(g,i,next)){
		if (isVisited[next] == false){
			outDfs(g,next,isVisited,finished,count);
		}
	}
	finished[(*count)++] = i;//将完成所有邻接点搜索的节点加入到数组中去.
}

void outDfsTraver(Graph g,int * finished){//以弧尾为导向的深度优先遍历
	bool isVisited[MAX_VEX_NUM] = {false};//标记数组.
	int count = 0;
	for (int i = 0; i < g.vexNum; i++){
		if (isVisited[i] == false){
			outDfs(g,i,isVisited,finished,&count);
		}
	}
}

void inDfs(Graph g,int i,bool * isVisited){
	printf("%c",g.list[i].vexName);
	isVisited[i] = true;
	for (int next = firstInAdj(g,i);next != -1; next = nextInAdj(g,i,next)){
		if (isVisited[next] == false){
			inDfs(g,next,isVisited);
		}
	}
}
//强连通图
void scc(Graph g){//
	bool isVisited[MAX_VEX_NUM] = {false};//标记数组.
	int finished[MAX_VEX_NUM] = {-1};//完成搜索 数组.
	outDfsTraver(g,finished);//按照搜索完成的先后关系,存入finished数组.
	int times = 0;//记录强连通分量的个数.
	for (int i = g.vexNum-1; i >= 0; i--){
		int next = finished[i];
		if (isVisited[next] == false){
			printf("第%d个强连通分量为:",times);
			inDfs(g,next,isVisited);
			printf("\n");
			times++;
		}
	}
}

void printGrahp(Graph g){
	for (int i = 0; i < g.vexNum; i++){
		printf("以%c为弧尾的 顶点有:",g.list[i].vexName);
		ArcNode * next = g.list[i].firstOut->tailNext;//删除所有弧尾
		while (next != NULL){
			printf("%c",g.list[next->headIndex].vexName);
			next = next->tailNext;
		}
		printf("\n以%c为弧头的 顶点有:",g.list[i].vexName);
		next = g.list[i].firstIn->headNext;//删除所有弧尾
		while (next != NULL){
			printf("%c",g.list[next->tailIndex].vexName);
			next = next->headNext;
		}
		printf("\n");
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	Graph g;
	createGrahp(&g);
	printGrahp(g);
	printf("图的顶点数:%d,边(弧)树为:%d\n",g.vexNum,g.arcNum);
	scc(g);//找出有向图的强连通分量。。
	destoryGraph(&g);
	return 0;
}

运行截图:

时间: 2024-08-03 18:39:15

看数据结构写代码(41) 强连通分量的相关文章

看数据结构写代码(32) 赫夫曼树编码以及译码

杂谈:最近有点慵懒,不好不好.好几天都没写代码,原本准备上星期完结 树 这一章节的.现在 又耽误了.哎.要抓紧时间啊. 下面直接上代码: 可以到我的网盘下载源代码,或者 直接拷贝下面的源代码 运行 网盘地址:点击打开链接 // HuffmanTree.cpp : 定义控制台应用程序的入口点. //哈弗曼编码,译码 #include "stdafx.h" #include <stdlib.h> #include <cstring> enum E_State { E

看数据结构写代码(44) 判断无向图是否有环路

在 看 严蔚敏的 数据结构 一书 7.5小节时,书上 说" 判断有向图是否存在环要不无向图复杂.对于无向图来说,深度优先遍历过程中遇到回边(即指向已访问过的顶点的边),则必定存在环路". 看的不明白,所以 网上 百度了一下. 有了思路:故写下算法 和思路,以便以后 温故. 思路: 1.一个n个顶点,e条边的 无向图,若 e>= n,必有环路. 2.若 e < n ,需要 深度 遍历,并把 父节点传入 参数中,如果 遇到 一个 节点 被访问过 并且 不是 父节点,那么 就有环

看数据结构写代码(43) 关节点

首先 说明一下 概念问题: 关节点 :如果删除无向 图中的一个顶点,以及与顶点相关的边,把 图的 一个连通 分量 变成 两个 以上的 连通 分量.这样的顶点叫做关节点. 没有 关节点的 无向图,叫做 重连通图.重连通图中 任意 两个顶点 至少 存在 两条以上的 通路. 如果 删除 连通图上的 k个 节点,才能 破坏 他的连通性,那么 这个连通图的 连通度 为k. 下面的算法 是 求 连通图的 关节点,并没有 考虑 求图的 关节点,不过 要 改成 图的 关节点 也不难,只要 加 一个 for i

看数据结构写代码(67) 置换 _ 选择排序(完结篇)

杂谈: 严蔚敏版<数据结构(C语言版)> 一书 终于看完了.这是 一个完结,也是 一个新的开端.<算法导论> 已到手. 置换选择排序的思想 是 将 归并段 尽量 变的 更大,而不是根据 内存 大小 限制在 固定的 大小. 这样 可以 利用赫夫曼树 来 进行 最优归并树,从而 使 外存 读写次数 最少. 下面给出 具体 代码:欢迎指出代码不足. // Replace_Selcetion.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h&q

看数据结构写代码(50)伙伴系统

伙伴系统 是一种 只 可以 分配 2的 幂次方 个 空间的 ,回收 内存 时 只 合并 "伙伴空间" 的一种 动态内存管理方式. 例如 一个 空间 大小 为 64 的 内存,伙伴 系统 为 这 64 的内存  建立 一组 双向循环 链表,分别 管理着  2的 0 次方,2的1 次方幂,2的 2 次方幂...2的6次方幂的 可用空间. 即使 我们 只想分配 一个 大小 为3的 空间,系统 却 只能 返回 一个 内存 大小 为 4(2的2次方)的 一个空间. 系统 在 初始化的 时候 ,并

看数据结构写代码(15)链式队列的实现

队列 和 栈 是 一种 受限制的 线性表.所以 他们的 实现方式 都 相差 无几.之前有过  链栈 和 链式线性表 的 实现经验,自然 写 链队 ,也毫无问题. 下面详细讲解每一段代码 的技术要点 下面是队列节点的数据结构 struct QueueNode { ElementType data; QueueNode * next; }; //生成一个节点 QueueNode * queueNodeMake(ElementType data){ QueueNode * pNode = (Queue

看数据结构写代码(35) 图的邻接矩阵表示法

杂谈:最近清明小长假,好好的放松了一下.节前 和 节后 都有点 松懈.不好,不好.贵在坚持.加油. 图的邻接矩阵表示法是用 两个数组 来表示 图的数据结构.一个是顶点数组,另一个是邻接矩阵数组.邻接矩阵 里存放着 顶点的关系. 用邻接矩阵表示图,在 看 顶点之间 是否有边,或者 求顶点的度等操作时比较简单.但空间浪费巨大,在插入,删除 顶点 和边 操作时 需要 移动大量数据,造成不便.所以在插入删除比较多,节点数比较多的时候 不宜 使用这种结构. 下面上代码: 源代码网盘地址:点击打开链接 //

看数据结构写代码(57) AVL树的删除

上一节 已经说了 AVL树的插入 操作,可是 只有 插入,没有删除,怎么能叫 动态 查找表呢. 呵呵,博主 赶紧 去 研究了一番.下面 是成果: AVL树的删除 大致 分为 两大块: 1. 查找节点 并 删除 2. 保持 删除 后 平衡因子的 影响 1. 首先 找到 这个 节点,如果 节点 不存在,直接 退出 函数 if (*tree == NULL){//没找到 return false; } 2.如果 存在,分为 四种情况:(根 二叉 排序树的 删除 类似) 1.节点 为 叶子 节点,直接

看数据结构写代码(63) 堆排序

// HeapSort.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <cstdlib> #define LIST_MAX_SIZE 100 //顺序表 struct sqList{ int base[LIST_MAX_SIZE]; int len; }; typedef sqList Heap;//顺序表作为堆排序的基本类型 //初始化顺序表 void initHeap(Heap * list,int * arr