Exemplo n.º 1
0
int main () {
	struct digraph *G = new_digraph (5);
	struct digraph *tc_G;

	/* make a funny digraph */

	insert_edge (G, 0, 1);
	insert_edge (G, 1, 2);
	//insert_edge (G, 2, 3);
	insert_edge (G, 3, 4);
	insert_edge (G, 4, 0);

	/* is the digraph connected? */

	if (is_connected (G)) printf ("connected!\n"); else printf ("not connected!\n");

	/* print the adjacency matrix */

	printf ("adjacency matrix for G:\n");
	print_adj_matrix (G);

	/* find the transitive closure */

	tc_G = Warshall (G);

	/* print the adjacency matrix for the transitive closure */

	printf ("adjacency matrix for transitive closure of G:\n");
	print_adj_matrix (tc_G);
	//return 0;

	/* make a funny DAG */

	struct digraph *H = new_digraph (7);
	insert_edge (H, 3, 4);
	insert_edge (H, 4, 2);
	insert_edge (H, 3, 5);
	insert_edge (H, 5, 2);
	insert_edge (H, 2, 6);
	insert_edge (H, 5, 3);
	insert_edge (H, 6, 1);
	insert_edge (H, 6, 0);
	printf ("adjacency matrix for H:\n");
	print_adj_matrix (H);
	if (is_connected (H)) printf ("connected!\n"); else printf ("not connected!\n");
	topological_sort (H, 3);
	//return 0;
	struct digraph *tc_H = Warshall (H);
	printf ("transitive closure of H is:\n");
	print_adj_matrix (tc_H);
	return 0;
}
Exemplo n.º 2
0
void main()
{
    int i,j,num;
    FILE*p;
    p=fopen("2.txt","r");
    if(p==NULL)
    {
        printf("cannot open 2.txt");
        exit(-1);
    }
    fscanf(p,"%d",&num);
    int **r=(int**)malloc(sizeof(int*)*(num+1));
    for(i=0;i<num+1;i++)
        r[i]=(int*)malloc(sizeof(int)*(num+1));
    for(i=1;i<num+1;i++)
        for(j=1;j<num+1;j++)
            fscanf(p,"%d",&r[i][j]);
    printf("顶点个数为:%d\n",num);
    printf("邻接矩阵为:\n");
    for(i=1;i<num+1;i++)
    {
        for(j=1;j<num+1;j++)
            printf(" %d  ",r[i][j]);
        printf("\n");
    }
    Warshall(num,r);
    printf("最终的传递闭包为\n");
    for(i=1;i<num+1;i++)
    {
        for(j=1;j<num+1;j++)
            printf(" %d  ",r[i][j]);
        printf("\n");
    }

}
void main()
{
	int i, j, cp;
	MTGraph G;
	IniMGraph_directed(&G);
	VertexData v[]={'a', 'b', 'c', 'd', 'e','f'};//顶点集
	EdgeData e[NumVertices][NumVertices]={
		{0,3,MaxValue,4,MaxValue,5},
		{MaxValue,0,1, MaxValue, MaxValue,1},
		{MaxValue, MaxValue,0,2,MaxValue,MaxValue},
		{MaxValue,3,MaxValue,0,MaxValue,MaxValue},
		{MaxValue, MaxValue, MaxValue,3,0,2},
		{MaxValue,MaxValue,MaxValue,2,MaxValue,0}
	};//边集,邻接矩阵
	CreateMGraph_directed(&G, v, e, 6);

	EdgeData A[NumVertices][NumVertices]={0};
	int A1[NumVertices][NumVertices]={0};
	int P[NumVertices][NumVertices];
	
	Floyd(A, G, P, G.n);
	cout<<"每一对顶点之间的最短路径:"<<endl;
	for(i=0; i<G.n; i++)
	{
		for(int j=0; j<G.n; j++)
			cout<<A[i][j]<<'\t';
		cout<<endl;
	}
	cout<<"相通节点之间的路径长以及中间节点: "<<endl;
	for(i=0; i<G.n; i++)
		for(j=0; j<G.n; j++)
		{
			if(A[i][j]<MaxValue)
			{
				cout<<G.vexlist[i]<<"->"<<G.vexlist[j]<<", 最短路径长度: "<<A[i][j]<<", 中间结点"<<':'<<endl;;		
				Path(P, i, j);
			}
		}
//求传递闭包
	Warshall(A1, G, G.n);
	cout<<"\n传递闭包为:"<<endl;
	for(i=0; i<G.n; i++)
	{
		for(int j=0; j<G.n; j++)
			cout<<A1[i][j]<<'\t';
		cout<<endl;
	}
//求中心节点
	CenterPoint(A, G.n, cp);
	cout<<"\n\n中心点为: "<<G.vexlist[cp+1]<<endl;

}