[英]Complexity of this simple algorithm
I did a algorithm to resolve a problem, but I don't know its complexity. 我做了一个算法来解决问题,但是我不知道它的复杂性。 The algorithm verifies if all of the vertex of a graph are "good".
该算法验证图的所有顶点是否都为“良好”。 A "good" vertex is a vertex that can access all others vertexes of a graph following a path that started himself.
“好”顶点是指可以按照自己开始的路径访问图的所有其他顶点的顶点。
public static boolean verify(Graph graph)
{
for(int i=0; i < graph.getVertex().size(); i++)
{
// List of vertexes visited
ArrayList<Character> accessibleVertex = new ArrayList<Character>();
getChildren(graph.getVertex().get(i), graph.getVertex().get(i).getName(), accessibleVertex);
// If the count of vertex without father equals a count of the list of vertexes visited, his is a "good" vertex
if((graph.getVertex().size()-1) == accessibleVertex.size())
return true;
}
return false;
}
private static void getChildren(Vertex vertex, char fatherName, ArrayList<Character> accessibleVertex)
{
// Ignore the 'father'
if(vertex.getName() != fatherName)
addIfUnique(vertex.getName(), accessibleVertex);
for(int i=0; i < vertex.getEdges().size(); i++)
{
getChildren(vertex.getEdges().get(i).otherVertex(), fatherName, accessibleVertex);
}
}
private static void addIfUnique(char name, ArrayList<Character> accessibleVertex)
{
boolean uniqueVertex = true;
for(int i=0; i < accessibleVertex.size(); i++)
{
if(accessibleVertex.get(i).equals(name))
uniqueVertex = false;
}
if(uniqueVertex)
accessibleVertex.add(name);
}
Thanks and sorry for my english. 谢谢你,我的英语不好意思。
我认为复杂度为O(n ^ 2),因为您可以通过调用以下内容使用嵌套循环:
getChildren(graph.getVertex().get(i), graph.getVertex().get(i).getName(), accessibleVertex);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.