无向图的割切点

求无向图的割切点,删除割切点及其相关的边将导致图的连通分量增加,采用了邻接矩阵表示图。

//============================================================================
// Name        : CutPointProblem.cpp
// Author      : Qichi
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

int N;				//number of vertex
bool *graph;		//matrix of graph
int *low, *mark;	//mark

void dfs(int u, int v, int num);

void explore_cont_block() {

	int i;
	for ( i=0; i<N; i++ ) if ( mark[i] == 0 ) break;

	if ( i < N ) {
		dfs(-1, i,1);
	}

}

void dfs(int u, int v, int num) {

	mark[v] = low[v] = num;

	int count = (u==-1)?0:1;
	bool potential = false;
	for ( int j=0; j<N; j++ ) {
		if ( graph[v*N+j] && j!=u ) {
			// an edge <v,j> is found
			if ( mark[j] == 0 ) {
				dfs(v,j,num+1);
				if ( low[j] < low[v] )
					low[v] = low[j];
				count++;
			} else {
				if ( mark[j] < low[v] )
					low[v] = mark[j];
			}
			// child j with low[j] >= mark[v]
			if ( low[j] >= mark[v] )
				potential = true;
		}
	}

	if ( potential && count>=2 )
		cout << "Cut point : " << v << endl;

}

int main() {

	cout << "Enter number of vertex : ";
	cin >> N;

	graph = (bool*)malloc(N*N*sizeof(bool));
	low = new int[N];
	mark = new int[N];

	memset(graph,0,N*N*sizeof(bool));
	memset(mark,0,N*sizeof(int));
	memset(low,0,N*sizeof(int));

	int i,j;

	cout << "Enter an edge : ";
	while ( cin >> i >> j ) {
		if ( i < N && j < N && i >=0 && j >= 0 ) {
			graph[i*N+j] = graph[j*N+i] = true;
		} else {
			cout << "Invalid!" << endl;
		}
		cout << "Enter an edge : ";
	}

	explore_cont_block();

	free(graph);
	free(low);
	free(mark);

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值