首页 > 代码库 > 寻找通用汇点

寻找通用汇点

// Graph2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<queue>
using namespace std;
typedef int Vertex;
#define NotAVertex 0
#define INF 65536
//定义链表节点////////////////////////////////////
typedef struct TreeNode *Position;
struct TreeNode {
	int vertex;
	int weight;
	Position Next;
};

//定义邻接表结构/////////////////////////////////////
typedef struct adjaceency_list *adjaceency;
struct adjaceency_list {
	int numVertex;      //大小
	Position* table;   //表地址
};

//邻接表初始化函数////////////////////////////////////
adjaceency initAdjaceency_list(int numVertex)
{
	//申请一个邻接表地址,给邻接表赋初值
	adjaceency adja = (adjaceency)malloc(sizeof(adjaceency_list));
	adja->numVertex = numVertex;
	if (adja == NULL)
		cout << "Error";

	//申请一个table地址
	adja->table = (Position*)malloc(sizeof(Position)*(adja->numVertex + 1));
	if (adja->table == NULL)
		cout << "Error";

	//给邻接表每一个表项添加一个链表表头
	for (int i = 1; i <= adja->numVertex; i++) {
		adja->table[i] = (Position)malloc(sizeof(TreeNode));
		if (adja->table[i] == NULL)
			cout << "Error";
		else {
			adja->table[i]->vertex = i;
			adja->table[i]->weight = 0;       //给每个邻接表项的链表头的权重设为0
			adja->table[i]->Next = NULL;
		}
	}
	return adja;
}

//邻接表的插入函数,制定一个顶点per_ver,把邻接的顶点aft_ver插入其后//////////////////////////////////
void Insert(adjaceency adja, Vertex per_ver, Vertex aft_ver, int weight)
{
	//申请一个链表节点地址
	Position inser = (Position)malloc(sizeof(TreeNode));
	if (inser == NULL)
		cout << "Error";

	//从头插入,修改指针
	inser->vertex = aft_ver;
	inser->weight = weight;                   //从per_ver指向aft_ver的权重
	inser->Next = adja->table[per_ver]->Next;
	adja->table[per_ver]->Next = inser;
}

//打印邻接表//////////////////////////////////////////
void print(adjaceency adja)
{
	cout << "Vertex" << endl;
	for (int i = 1; i <= adja->numVertex; i++)
	{
		Position p = adja->table[i];
		while (p != NULL) {
			cout << p->vertex << ‘\t‘;
			p = p->Next;
		}
		cout << endl;
	}
	cout << endl;
}

//寻找通用汇点,通用汇点是指入度为V-1,出度为0的点
Vertex  findUniversalSink(adjaceency adja)
{
	Vertex UniversalSink;
	Position p;
	bool flag = false;

	//先寻找有没有出度为0的点,若没有直接返回一个空点NotAVertex;
	for (Vertex ver = 1; ver <= adja->numVertex; ver++)
		if (adja->table[ver]->Next == NULL)
		{
			UniversalSink = ver;
			flag = true;      
			//这个标志位是为了下面的循环服务的,若下面的循环没有修改这个标志位,说明所有点的邻接点都有UniversalSink;
		}
	if (!flag) return NotAVertex;

	//能运行到这里说明有出度为0的点,有且只有一个,检查是不是所有邻接链表都有它
	for (Vertex ver = 1; ver <= adja->numVertex; ver++)
	{
		if (ver == UniversalSink)  //本身不检查
			continue;
		else
		{
			//每一个链表查找
			p = adja->table[ver]->Next;
			while (p)
			{
				if (p->vertex == UniversalSink)
					break;
				p = p->Next;
			}

			//如果P==NULL说明上面的循环没有提前退出,可见UniversalSink不在该链表里,所以可以直接退出大循环,并设标志位为假
			if (p == NULL)
			{
				break;
				flag = false;
			}
		}
	}
	return flag ? UniversalSink : NotAVertex;
}

int main()
{
	//初始化邻接表////////////////////////////////////////
	adjaceency adja = initAdjaceency_list(7);
	Insert(adja, 1, 3, 4); Insert(adja, 1, 4, 1); Insert(adja, 1, 2, 2);
	Insert(adja, 2, 5, 10); Insert(adja, 2, 4, 3);
	Insert(adja, 3, 6, 5);
	Insert(adja, 4, 3, 2); Insert(adja, 4, 7, 4); Insert(adja, 4, 6, 8);
	Insert(adja, 5, 7, 6); Insert(adja, 5, 4, 2);
	Insert(adja, 6, 4, 1);
	Insert(adja, 7, 6, 1);
	print(adja);

	//寻找通用汇点
	Vertex UniversalSink = findUniversalSink(adja);
	if (findUniversalSink(adja))
		cout << "本图的通用汇点为V" << UniversalSink << endl;
	else
		cout << "本图没有通用汇点" << endl;
	while (1);

    return 0;
}

  

寻找通用汇点