图形有20个顶点和6个边。此图中的组件数是否始终为14? 如果没有,图表中组件的最大和最小值是多少?
答案 0 :(得分:1)
如果图表有任何周期,没有。您可能拥有超过14个组件。
例如,上图有20个顶点,6个边和16个组件。
对于非循环图,组件数等于顶点数减去边数。从递归公式可以看出这一点:
function num_components(vertices, edges){
//base case: we have nothing but unconnected vertices.
if (edges == 0){
//Each one is its own component.
return vertices;
}
else{
//Adding a single edge to an acyclic graph
//reduces the number of components by one.
return num_components(vertices, edges - 1) - 1;
}
}