示例#1
0
bool EdgeColoringTest<DefaultStructs>::test(const Graph &graph, const ColorMap &colors)
{
	typedef typename Graph::PVertex Vert;
	typedef typename Graph::PEdge Edge;
	const EdgeDirection Mask = EdDirIn|EdDirOut|EdUndir;
	int degree = graph.Delta(Mask);
	int LOCALARRAY(kolory, degree+1);
	int lenKolory = 0;
	for(Vert vv = graph.getVert(); vv; vv = graph.getVertNext(vv)) {
		lenKolory = 0;
		for(Edge ee = graph.getEdge(vv, Mask); ee;
			ee = graph.getEdgeNext(vv, ee, Mask))
		{
			if(!colors.hasKey(ee)) return false;
			int col = colors[ee];
			if(col<0) return false;
			kolory[lenKolory++] = col;
		}
		DefaultStructs::sort(kolory, kolory+lenKolory);
		int tmpLen = std::unique(kolory, kolory+lenKolory)-kolory;
		if(tmpLen!=lenKolory)
			return false;
	}
	return true;
}
示例#2
0
SeqEdgeColoringPar<DefaultStructs>::VizingState<Graph,ColorMap>::
VizingState(const Graph &g, ColorMap &c, int maxCol):
	graph(g), delta(g.Delta(EdDirIn|EdDirOut|EdUndir)),
	colors(c), notColored(maxCol), vertToTab( g.getVertNo() ),
	edgeToList( g.getEdgeNo(EdDirIn|EdDirOut|EdUndir) )
{
	typedef typename Graph::PVertex Vert;
	typedef typename Graph::PEdge Edge;
	const EdgeDirection Mask = EdDirIn|EdDirOut|EdUndir;
	tabVert = new TabVert[ g.getVertNo() ];
	colorToList = new int[ maxCol+1 ];
	listEdgeCol = new ListEdgeCol[ g.getEdgeNo(Mask)+1 ];
	maxColor = -1;

	//init all lists
	int ii = 0;
	for(Vert vv = graph.getVert(); vv; vv = graph.getVertNext(vv)) {
		vertToTab[vv] = ii;
		tabVert[ii++].freeColor = 0;
	}
	for(int i=0; i<notColored; i++)
		colorToList[i] = 0;
	colorToList[notColored] = 1;
	ii = 1; //we assume that colors are not set to any edge
	for(Edge ee = graph.getEdge(Mask); ee;
		ee = graph.getEdgeNext(ee, Mask), ii++)
	{
		edgeToList[ee] = ii;
		listEdgeCol[ii].next = ii+1;
		listEdgeCol[ii].prev = ii-1;
		listEdgeCol[ii].edge = ee;
	}
	listEdgeCol[ii-1].next = 0;
	if(ii==1) //if the graph is empty
		colorToList[notColored] = 0;
	if(colors.size()<=0) return;

	for(Edge ee = graph.getEdge(Mask); ee;
		ee = graph.getEdgeNext(ee, Mask))
	{
		if(!colors.hasKey(ee)) continue;
		int tmpCol = colors[ee];
		if(tmpCol<0 || tmpCol>=notColored)
			continue;

		changeColor(ee, notColored, tmpCol);
		if(maxColor<tmpCol) maxColor = tmpCol;
	}
}
示例#3
0
int SeqEdgeColoringPar<DefaultStructs>::vizing(const Graph &graph,
	ColorMap &colors, typename Graph::PEdge edge,
	typename Graph::PVertex vert, int maxCol)
{
	if(colors.hasKey(edge)&&colors[edge]>=0)
		return -1;

	typedef typename Graph::PVertex Vert;
	typedef typename Graph::PEdge Edge;
	const EdgeDirection Mask = EdDirIn|EdDirOut|EdUndir;
	int degree = graph.Delta(Mask);
	int mu = makeSubgraph(graph, std::make_pair(stdChoose(true), !edgeTypeChoose(Loop))).mu();
	VizingState<Graph, ColorMap> vState(graph, colors, degree+mu);
	vState.maxColor = maxCol;

	//edge coloring
	vizing(vState, edge, vert);
	return colors[edge];
}
示例#4
0
int SeqEdgeColoringPar<DefaultStructs>::vizing(const Graph &graph,
	ColorMap &colors)
{
	typedef typename Graph::PVertex Vert;
	typedef typename Graph::PEdge Edge;
	const EdgeDirection Mask = EdDirIn|EdDirOut|EdUndir;
	colors.reserve(graph.getEdgeNo(EdDirIn|EdDirOut|EdUndir));
	int degree = graph.Delta(Mask);
	int mu = makeSubgraph(graph, std::make_pair(stdChoose(true), !edgeTypeChoose(Loop))).mu();
	VizingState<Graph, ColorMap> vState(graph, colors, degree+mu);
	if(degree-1>vState.maxColor)
		vState.maxColor = degree-1;

	//edge coloring
	int idFree;
	while( (idFree=vState.colorToList[vState.notColored])>0 ) {
		Edge ee = vState.listEdgeCol[idFree].edge;
		vizing(vState, ee, ee->getEnd1());
	}
	return vState.maxColor;
}
示例#5
0
int SeqEdgeColoringPar<DefaultStructs>::vizingSimple(const Graph &graph,
	ColorMap &colors)
{
	typedef typename Graph::PVertex Vert;
	typedef typename Graph::PEdge Edge;
	const EdgeDirection Mask = EdDirIn|EdDirOut|EdUndir;
	colors.reserve(graph.getEdgeNo(EdDirIn|EdDirOut|EdUndir));

	int degree = graph.Delta(Mask);
	VizingState<Graph, ColorMap> vState(graph, colors, degree+1);

	int LOCALARRAY(tmpTab, vState.notColored);

	//tmpTab - is used for checking free color availability
	if(colors.size()>0) {
		//for each vertex checks the free color availability (freeColor)
		for(Vert vv = graph.getVert(); vv; vv = graph.getVertNext(vv)) {
			for(int i=0; i<vState.notColored; i++)
				tmpTab[i] = 0;
			for(Edge ee=graph.getEdge(vv, Mask); ee;
				ee = graph.getEdgeNext(vv, ee, Mask))
			{
				if(!colors.hasKey(ee)) continue;
				int tmpCol = colors[ee];
				if(tmpCol<0 || tmpCol>=vState.notColored) continue;
				tmpTab[ tmpCol ] = 1;
			}
			int i=0;
			while(i<vState.notColored && tmpTab[i]==1) i++;
			vState.tabVert[ vState.vertToTab[vv] ].freeColor = i;
		}
	}

	//init edges by degree colors
	int ii;
	for(Edge edge = graph.getEdge(Mask); edge;
		edge = graph.getEdgeNext(edge, Mask))
	{
		if(colors.hasKey(edge)&&colors[edge]>=0) continue;
		Vert vert1 = graph.getEdgeEnd1(edge);
		Vert vert2 = graph.getEdgeEnd2(edge);
		for(int i=0; i<degree; i++)
			tmpTab[i] = 0;
		for(Edge ee = graph.getEdge(vert1, Mask); ee;
			ee = graph.getEdgeNext(vert1, ee, Mask))
		{
			if(!colors.hasKey(ee)) continue;
			int tmpCol = colors[ee];
			if(tmpCol<0 || tmpCol>=vState.notColored) continue;
			tmpTab[ tmpCol ] = 1;
		}
		for(Edge ee=graph.getEdge(vert2, Mask); ee;
			ee=graph.getEdgeNext(vert2, ee, Mask))
		{
			if(!colors.hasKey(ee)) continue;
			int tmpCol = colors[ee];
			if(tmpCol<0 || tmpCol>=vState.notColored) continue;
			tmpTab[ tmpCol ] |= 2;
		}

		for(int i=0; i<degree; i++) {
			if(tmpTab[i]!=0) continue;

			colors[edge] = i;
			vState.changeColor(edge, vState.notColored, i);
			tmpTab[i] = 3; // 3=1|2
			if(i > vState.maxColor)
				vState.maxColor = i;
			break;
		}
		//setting free colors at vertices
		for(ii=0; ii<degree; ii++)
			if((tmpTab[ii]&1)==0) break;
		int id = vState.vertToTab[vert1];
		vState.tabVert[id].freeColor = ii;
		for(ii=0; ii<degree; ii++)
			if((tmpTab[ii]&2)==0) break;
		id = vState.vertToTab[vert2];
		vState.tabVert[id].freeColor = ii;
	}
	//edge coloring
	int idFree;
	while( (idFree=vState.colorToList[vState.notColored])>0 ) {
		Edge ee = vState.listEdgeCol[idFree].edge;
		vizingSimple(vState, ee, ee->getEnd1());
	}
	return vState.maxColor;
}