Jas*_*ker 10 algorithm graph-theory clique-problem
我的算法类中的一个任务是设计一个穷举搜索算法来解决集团问题.也就是说,给定大小为n的图,该算法应该确定是否存在大小为k的完整子图.我想我已经得到了答案,但我不禁想到它可以改进.这就是我所拥有的:
输入:由数组A [0,... n -1] 表示的图形,要查找的子图形的大小k.
output:如果子图存在则为True,否则为False
算法(类似python的伪代码):
def clique(A, k):
P = A x A x A //Cartesian product
for tuple in P:
if connected(tuple):
return true
return false
def connected(tuple):
unconnected = tuple
for vertex in tuple:
for test_vertex in unconnected:
if vertex is linked to test_vertex:
remove test_vertex from unconnected
if unconnected is empty:
return true
else:
return false
Run Code Online (Sandbox Code Playgroud)
input:大小为n乘n的邻接矩阵,k是要查找的子图的大小
输出:A中大小为k的所有完整子图.
算法(这次是在函数/ Python伪代码中):
//Base case: return all vertices in a list since each
//one is a 1-clique
def clique(A, 1):
S = new list
for i in range(0 to n-1):
add i to S
return S
//Get a tuple representing all the cliques where
//k = k - 1, then find any cliques for k
def clique(A,k):
C = clique(A, k-1)
S = new list
for tuple in C:
for i in range(0 to n-1):
//make sure the ith vertex is linked to each
//vertex in tuple
for j in tuple:
if A[i,j] != 1:
break
//This means that vertex i makes a clique
if j is the last element:
newtuple = (i | tuple) //make a new tuple with i added
add newtuple to S
//Return the list of k-cliques
return S
Run Code Online (Sandbox Code Playgroud)
有没有人有任何想法,意见或建议?这包括我可能错过的错误以及使其更具可读性的方法(我不习惯使用很多伪代码).
幸运的是,我在提交任务之前与我的教授交谈过.当我发现他的伪代码,我写了,他笑着对我说,我做了这样的工作太多了.首先,我没有提交伪代码; 我只需证明我理解了这个问题.其二,他是想蛮力解决方案.所以我上交的内容看起来像这样:
输入:图G =(V,E),找到k的集团的大小
output:如果clique确实存在则为True,否则为false
算法:
更新:添加第二个版本.虽然我没有添加任何花哨的动态编程(我知道),但我认为这会越来越好.
更新2:添加了一些注释和文档,使版本2更具可读性.这可能是我今天转的版本.谢谢大家的帮助!我希望我能接受不止一个答案,但我接受了最能帮助我的人的答案.我会让你们知道我的教授的想法.
一些评论:
connected(tuple)
看起来不对劲.你不需要unconnected
在循环内重置吗?