二分图(BipartiteGraph)的检测 -- C++实现


二分图定义:简单来说,如果图中的点能分成两组,并且没有任何一条边在组内顶点连通,也就是所有的边连通的顶点都是跨组的

实现代码

// GraphUpgrade.h
// 无向图升级版
#ifndef GRAPHUPGRADE_H
#define GRAPHUPGRADE_H

#include "Component.h"

namespace jay{

	template<typename T>
	class GraphUpgrade
	{
	private:

		// 可以用自己实现的散列表,嫌麻烦,直接使用stl提供的hashmap
		hash_map<T,LinkList<T> > m_mapList; // 邻接表 存放了每个对应对应的已连通的顶点
		int m_V; // 顶点的数量
		int m_E; // 边的数量

		int m_iCountCC; //联通分量的个数

		hash_map<T,bool> m_marked; // 是否已遍历
		hash_map<T,T> m_edgeTo;
		T m_DFSVertex;
		hash_map<T,int> m_distTo; //  这里记录当前顶点到起点的距离(边的个数)
		hash_map<T,LinkList<T> > m_iCCid; // 分量ID(以分量的首顶点做ID) 和 其联通的分量顶点
		hash_map<int,int> m_iSizeCC;
		hash_map<T,bool> m_Color;

		T m_CurrentIDCC;

		queue<T> m_cycle;
		void ClearQueue(queue<T>& q) ;
	public:

		void AddEdge(T v,T w );
		void DeleteEdge(T v,T w );
		void DeleteVertex(T v);
		void checkValid(T v);

		queue<T> GenerateConnectedComponent();
		void ShowComponent(T v);

		void DeepFristSearch(T v);
		void DFS(T v);
		bool isConnectTo(T v);
		void ShowPathTo(T v);

		void Show();

		bool hasSelfLoop(); // 是否存在自环
		bool hasParallelEdges(); // 是否存在平行边

		bool isBipartiteGraph();

		void ResetMarkTag(bool mark = false);

		bool m_isBG;

		GraphUpgrade();
		~GraphUpgrade();

	};

	template<typename T>
	GraphUpgrade<T>::GraphUpgrade()
	{
		m_V = 0;
		m_E = 0;
	}

	template<typename T>
	GraphUpgrade<T>::~GraphUpgrade()
	{
	}

	template<typename T>
	void GraphUpgrade<T>::AddEdge(T v,T w )
	{
		m_mapList[v].Add(w);
		m_mapList[w].Add(v);
		m_E++;

		m_marked[v] = false;
		m_marked[w] = false;
		m_Color[v] = false;
		m_Color[w] = false;
	}

	template<typename T>
	bool GraphUpgrade<T>::isBipartiteGraph()
	{
		m_isBG = true;
		ResetMarkTag();
		hash_map<T,LinkList< T> >::iterator it;
		for (it = m_mapList.begin();it!=m_mapList.end();it++)
		{
			if (!m_marked[it->first]) 
			{
				DeepFristSearch(it->first);
			}
		}

		return m_isBG;
	}


	template<typename T>
	void GraphUpgrade<T>::ClearQueue(queue<T>& q) 
	{
		queue<T> empty;
		swap(empty, q);
	}


	template<typename T>
	void GraphUpgrade<T>::ResetMarkTag(bool mark = false)
	{
		hash_map<T,bool>::iterator it;
		for (it = m_marked.begin();it!=m_marked.end();it++)
		{
			it->second = mark;
		}
	}



	template<typename T>
	void GraphUpgrade<T>::DFS(T v)
	{
		DeepFristSearch(v);
	}

	template<typename T>
	void GraphUpgrade<T>::DeepFristSearch(T v)
	{
		// 
		m_marked[v] = true;
		m_iCCid[m_CurrentIDCC].Add(v);
		LinkList< T>::iterator it;
		for (it = m_mapList[v].begin();it!=m_mapList[v].end();it++)
		{
			// 如果当前v的邻接顶点未遍历
			if (!m_marked[it->m_value]) 
			{
				m_Color[it->m_value] = !m_Color[v];
				DeepFristSearch(it->m_value);
			}
			// 如果当前v的邻接顶点已遍历,切颜色和v的颜色一样
			else if (m_Color[it->m_value] == m_Color[v])
			{
				m_isBG = false;
			}
		}
	}

	template<typename T>
	bool GraphUpgrade<T>::isConnectTo(T v)
	{
		return m_marked[v];
	}

	template<typename T>
	void GraphUpgrade<T>::ShowPathTo(T v)
	{
		if (!isConnectTo(v))
		{
			cout << "当前DFS顶点和指定顶点不连通" << endl;
			return;
		}

		T tmpold = v;

		stack<T> myStack;
		for (T tmp = v;v !=m_DFSVertex;v = m_edgeTo[v])
		{
			myStack.push(v);
		}
		myStack.push(m_DFSVertex);

		while (!myStack.empty())
		{
			T tmp = myStack.top();
			cout << tmp;
			if (tmp != tmpold)
			{
				cout << " -> ";
			}
			myStack.pop();
		}

		cout << endl;
	}


	template<typename T>
	void GraphUpgrade<T>::DeleteEdge(T v,T w )
	{
		m_mapList[v].Delete(w);
		m_mapList[w].Delete(v);

		m_E--;
	}

	// 删除顶点
	template<typename T>
	void GraphUpgrade<T>::DeleteVertex(T v)
	{
		// 获取和v已连通的顶点
		// LinkList<T> deleteV = m_mapList[v];

		LinkList<T>::iterator it;
		for(it = m_mapList[v].begin();it != m_mapList[v].end();it++)
		{
			m_mapList[it->m_value].Delete(v); // 从其他顶点删除v
		}

		for(int i=0;i<m_mapList[v].Size();i++ )
		{
			T BUF = m_mapList[v].Get(0);
			m_mapList[v].Delete(BUF); // 从v删除其他顶点
		}

		// 删除顶点v
		m_mapList.erase(v);
	}

	template<typename T>
	void GraphUpgrade<T>::Show()
	{
		hash_map< T,LinkList<T> >::iterator it;
		for(it = m_mapList.begin();it != m_mapList.end();it++)
		{
			cout << "当前顶点 " << it->first << ": ";
			it->second.Show();
		}
	}
};

#endif

测试代码

//main.cpp

#include <time.h>
#include<windows.h>

#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#include "Component.h"
#include "Graph.h"
#include "GraphUpgrade.h"

using namespace jay;



int main(int argc, char* argv[])
{

	GraphUpgrade<int> gra;

	gra.AddEdge(1,2);
	gra.AddEdge(2,3);
	gra.AddEdge(3,4);
	gra.AddEdge(4,5);
	gra.AddEdge(5,6);
	gra.AddEdge(6,7);
	gra.Show();

	cout << "isBipartiteGraph? " << gra.isBipartiteGraph() << endl;

	
	
	
}

 

暂无评论

注册用户登录后才能发表或者回复评论,请先登录 注册。