c++ - 查找图中由一个顶点分隔的顶点

标签 c++ algorithm graph vertex

你知道一些算法(比蛮力更好)可以找到图中被一个顶点分隔并且彼此之间没有连接的顶点。示例:

graph example

在此图中找到的路径为:

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

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

最佳答案

一种方法是基于广度优先搜索,对于图中的每个顶点 i,我们扫描与 i 相邻的顶点(即两级邻接!)。

mark = array[0..n-1] of 0
flag = 1

for i = nodes in graph do

// mark pattern of nodes adjacent to i 
    mark[i] = flag
    for j = nodes adjacent to i do
        mark[j] = flag
    endfor

// scan nodes adjacent to those adjacent to i
// (separated by one vertex!)
    for j = nodes adjacent to i do
    for k = nodes adjacent to j do
        if mark[k] != flag  and k > i then
        // i,k are separated by another vertex
        // and there is no edge i,k

        // prevent duplicates
            mark[k] = flag
        endif
    endfor
    endfor

// implicit unmarking of current pattern
    flag += 1

endfor

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

关于c++ - 查找图中由一个顶点分隔的顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13321932/

相关文章:

c++ - C++ 中的 if(x^1!=1) 和 if(int(x^1)!=1) 有什么区别?

c++ - 我可以用尽堆栈吗?

sql - 什么公式用于在基于标签的系统中构建相关项目列表?

python - 如何找到列表中不一定相邻的最大连续数字集?

string - 使用字典选择不重叠的最大子串总和

graph - pacman 寻路的一些问题

c++ - 我怎样才能让 Doxygen 知道 CUDA 内核调用?

c++ - 为什么在缺少非 const 虚方法时不能实例化类的 const 实例?

php - PHP 中的 Edmonds 最大匹配算法

algorithm - 查找无向图中两个顶点之间所有简单路径上的所有*顶点*