Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在图中查找由一个顶点分隔的顶点_C++_Algorithm_Graph_Vertex - Fatal编程技术网

C++ 在图中查找由一个顶点分隔的顶点

C++ 在图中查找由一个顶点分隔的顶点,c++,algorithm,graph,vertex,C++,Algorithm,Graph,Vertex,你知道一些算法(比蛮力更好)可以在图中找到由一个顶点分隔且彼此不相连的顶点。例如: 在此图中,找到的路径为: 1-4 2-4 3-5 最好的是C++代码,它使用STL列表数组作为图形表示,但任何其他过程语言或伪代码都是可以的。 < P>一种方法是基于广度优先风格搜索,在图中每个顶点 i ,我们扫描毗邻于“代码> > 的顶点。(即两级邻接!) 如果图的每个顶点有m边,这将是一个O(n*m^2)算法,需要O(n)额外的空间。这个问题的一个简单而直观的解决方案在于邻接矩阵邻接矩阵的n次幂的第n个

你知道一些算法(比蛮力更好)可以在图中找到由一个顶点分隔且彼此不相连的顶点。例如:

在此图中,找到的路径为:

  • 1-4
  • 2-4
  • 3-5

最好的是C++代码,它使用STL列表数组作为图形表示,但任何其他过程语言或伪代码都是可以的。

< P>一种方法是基于广度优先风格搜索,在图中每个顶点<代码> i <代码>,我们扫描毗邻于“代码> > 的顶点。(即两级邻接!)


如果图的每个顶点有
m
边,这将是一个
O(n*m^2)
算法,需要
O(n)
额外的空间。

这个问题的一个简单而直观的解决方案在于邻接矩阵邻接矩阵的n次幂的第n个元素列出了长度正好在i和j之间的所有路径。
所以我只是读入A,邻接矩阵,然后计算A^2。最后,我列出了它们之间有一条长度为2的路径的所有对

//sg
#include<stdio.h>
#define MAX_NODE 10
int main()
{
    int a[MAX_NODE][MAX_NODE],c[MAX_NODE][MAX_NODE];
    int i,j,k,n;
    printf("Enter the number of nodes : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    for(j=0;j<=i;j++)
    {
        printf("Edge from %d to %d (1 yes/0 no) ? : ",i+1,j+1);
        scanf("%d",&a[i][j]);
        a[j][i]=a[i][j]; //undirected graph
    }
    //dump the graph
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
        c[i][j]=0;
        printf("%d",a[i][j]);
    }
    printf("\n");
    }
    printf("\n");

    //multiply
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    for(k=0;k<n;k++)
    {
        c[i][j]+=a[i][k]*a[k][j];
    }
    //result of the multiplication
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
        printf("%d",c[i][j]);
    }
    printf("\n");
    }
    for(i=0;i<n;i++)
    for(j=0;j<=i;j++)
    {
        if(c[i][j]==1&&(!a[i][j])&&(i!=j)) //list the paths
        {
            printf("\n%d - %d",i+1, j+1 );

        }
    }
    return 0;
}
分析
对于n个顶点:

  • 时间:O(n^3)。可以减少到,这是非常好的

  • 空格:O(n^2)


您可以使用的修改版本来执行此操作。以下代码示例中的算法使用图形的邻接矩阵,如果存在,则打印
i j
是从
i
k
的边和从
k
j
的边,但没有从
i
j
的直接方式

#include <iostream>

int main() {
    // Adjacency Matrix of your graph
    const int n = 5;
    bool d[n][n] = {
       { 0, 1, 1, 0, 0 },
       { 0, 0, 1, 0, 0 }, 
       { 0, 0, 0, 1, 0 },
       { 0, 0, 0, 0, 1 },
       { 0, 0, 0, 0, 0 },
    };

    // Modified Warshall Algorithm
    for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
            if (d[i][k])
                for (int j = 0; j < n; j++)
                    if (d[k][j] && !d[i][j])
                        std::cout << i + 1 << " " j + 1 << std::endl;
}
#包括
int main(){
//图的邻接矩阵
常数int n=5;
bool d[n][n]={
{ 0, 1, 1, 0, 0 },
{ 0, 0, 1, 0, 0 }, 
{ 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0 },
};
//改进的Warshall算法
对于(int k=0;k空间要求不是n²吗?我只能看到矩阵a和c,两者都有n²元素。
[aman@aman c]$ ./Adjacency2 
Enter the number of nodes : 5
Edge from 1 to 1 (1 yes/0 no) ? : 0
Edge from 2 to 1 (1 yes/0 no) ? : 1
Edge from 2 to 2 (1 yes/0 no) ? : 0
Edge from 3 to 1 (1 yes/0 no) ? : 1
Edge from 3 to 2 (1 yes/0 no) ? : 1
Edge from 3 to 3 (1 yes/0 no) ? : 0
Edge from 4 to 1 (1 yes/0 no) ? : 0
Edge from 4 to 2 (1 yes/0 no) ? : 0
Edge from 4 to 3 (1 yes/0 no) ? : 1
Edge from 4 to 4 (1 yes/0 no) ? : 0
Edge from 5 to 1 (1 yes/0 no) ? : 0
Edge from 5 to 2 (1 yes/0 no) ? : 0
Edge from 5 to 3 (1 yes/0 no) ? : 0
Edge from 5 to 4 (1 yes/0 no) ? : 1
Edge from 5 to 5 (1 yes/0 no) ? : 0
01100
10100
11010
00101
00010

21110
12110
11301
11020
00101

4 - 1
4 - 2
5 - 3
#include <iostream>

int main() {
    // Adjacency Matrix of your graph
    const int n = 5;
    bool d[n][n] = {
       { 0, 1, 1, 0, 0 },
       { 0, 0, 1, 0, 0 }, 
       { 0, 0, 0, 1, 0 },
       { 0, 0, 0, 0, 1 },
       { 0, 0, 0, 0, 0 },
    };

    // Modified Warshall Algorithm
    for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
            if (d[i][k])
                for (int j = 0; j < n; j++)
                    if (d[k][j] && !d[i][j])
                        std::cout << i + 1 << " " j + 1 << std::endl;
}