Clique problem algorithm design

♀尐吖头ヾ 提交于 2019-12-02 23:53:07

Some comments:

  • You only need to consider n-choose-k combinations of vertices, not all k-tuples (n^k of them).
  • connected(tuple) doesn't look right. Don't you need to reset unconnected inside the loop?
  • As the others have suggested, there are better ways of brute-forcing this. Consider the following recursive relation: A (k+1)-subgraph is a clique if the first k vertices form a clique and vertex (k+1) is adjacent to each of the first k vertices. You can apply this in two directions:
    • Start with a 1-clique and gradually expand the clique until you get the desired size. For example, if m is the largest vertex in the current clique, try to add vertex {m+1, m+2, ..., n-1} to get a clique that is one vertex larger. (This is similar to a depth-first tree traversal, where the children of a tree node are the vertices larger than the largest vertex in the current clique.)
    • Start with a subgraph of the desired size and check if it is a clique, using the recursive relation. Set up a memoization table to store results along the way.
  • (implementation suggestion) Use an adjacency matrix (0-1) to represent edges in the graph.
  • (initial pruning) Throw away all vertices with degree less than k.

I once implemented an algorithm to find all maximal cliques in a graph, which is a similar problem to yours. The way I did it was based on this paper: http://portal.acm.org/citation.cfm?doid=362342.362367 - it described a backtracking solution which I found very useful as a guide, although I changed quite a lot from that paper. You'd need a subscription to get at that though, but I presume your University would have one available.

One thing about that paper though is I really think they should have named the "not set" the "already considered set" because it's just too confusing otherwise.

The algorithm "for each k-tuple of vertices, if it is a clique, then return true" works for sure. However, it's brute force, which is probably not what an algorithms course is searching for. Instead, consider the following:

  1. Every vertex is a 1-clique.
  2. For every 1-clique, every vertex that connects to the vertex in the 1-clique contributes to a 2-clique.
  3. For every 2-clique, every vertex that connects to each vertex in the 2-clique contributes to a 3-clique.
  4. ...
  5. For every (k-1)-clique, every vertex that connects to each vertex in the (k-1) clique contributes to a k-clique.

This idea might lead to a better approach.

It's amazing what typing things down as a question will show you about what you've just written. This line:

P = A x A x A  //Cartesian product

should be this:

P = A k //Cartesian product

What do you mean by A^k? Are you taking a matrix product? If so, is A the adjacency matrix (you said it was an array of n+1 elements)?

In setbuilder notation, it would look something like this:

P = {(x0, x1, ... xk) | x0 ∈ A and x1 ∈ A ... and xk ∈ A}

It's basically just a Cartesian product of A taken k times. On paper, I wrote it down as k being a superscript of A (I just now figured out how to do that using markdown).

Plus, A is just an array of each individual vertex without regard for adjacency.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!