Bron-Kerbosch C实施

时间:2013-01-01 22:47:07

标签: c implementation clique-problem

我在实现Bron-Kerbosch算法的C版本时遇到了一些问题:

1-我完全不了解算法是如何工作的。我试图在互联网上找到引用,但所有这些都是糟糕的algol示例实现,并且没有任何解释伪代码上出现的一些任意命名的函数(例如P(N))

2-我在互联网上发现了一些C ++实现,但作者未能放入代码中使用的类,所以我留下了非常命名的变量,我甚至不知道它们是什么类型(例如compsub,点头,minnod,fixp,newne,newce)。

我能够翻译的一个代码是SegFaulting。你能帮我理解算法/告诉我在我的代码中我做错了什么吗?

一些有用的信息:

graph->矩阵是连接矩阵。

List_clear返回列表的大小。

int serial (Graph* graph) { 
  int i; 
  int* all = (int *) malloc (graph->size * sizeof (int) ); 
  List compsub;

  List_init (&compsub);

  for (i = 0; i < graph->size; i++)  
all[i] = i; 
  bkv2 (graph, &compsub, all, 0, graph->size);
  free (all); 
  return List_clear (&compsub);
} 

// recursive function version 2 of Bron-Kerbosch  algorithm 
void bkv2 (Graph* graph, List* compsub, int* oldSet, int ne, int ce ) {  
  int* newSet = (int *) malloc (ce * sizeof (int) ); 
  int nod, fixp; 
  int newne, newce, i, j, count, pos, p, s, sel, minnod; 

  minnod = ce; 
  nod = 0; 
  // Determine each counter value and look for minimum 
  for ( i = 0 ; i < ce && minnod != 0; i++) { 
p = oldSet[i]; 
count = 0; 

// Count disconnections 
for (j = ne; j < ce && count < minnod; j++) 
  if ( !(graph->matrix[p][oldSet[j]]) ) { 
    count++; 
    // Save position of potential candidate 
    pos = j; 
  } 
// Test new minimum 
if (count < minnod) {
  fixp = p; 
  minnod = count; 
  if (i < ne)
    s = pos; 
  else { 
    s = i; 
    // pre-increment 
    nod = 1; 
  } 
} 
  } 
  // If fixed point initially chosen from candidates then 
  // number of diconnections will be preincreased by one 
  // Backtrackcycle 
  for (nod = minnod + nod; nod >= 1; nod--) { 
// Interchange 
p = oldSet[s]; 
oldSet[s] = oldSet[ne];   sel = oldSet[ne] = p; 
// Fill new set "not" 
newne = 0; 
for ( i = 0 ; i < ne ; i++) 
  if (graph->matrix[sel][oldSet[i]] ) 
    newSet[newne++] = oldSet[i]; 

// Fill new set "cand" 
newce = newne; 
for (i = ne+1; i<ce; i++) 
  if (graph->matrix[sel][oldSet[i]] ) 
    newSet[newce++] = oldSet[i]; 

// Add to compsub 
List_add (compsub, sel); 
if (newce == 0) { 
  // found a max clique 
  List_print(compsub); 

} else if (newne < newce) 
  bkv2 ( graph, compsub, newSet, newne, newce ); 

// Remove from compsub 
List_remove(compsub); 

// Add to "not" 
ne++; 
if (nod > 1) 
  // Select a candidate disconnected to the fixed point 
  for ( s = ne ; graph->matrix[fixp][oldSet[s]] ; s++) 
    ; 
// nothing but finding s  

  } /* Backtrackcycle */ 
  free (newSet); 
} 

1 个答案:

答案 0 :(得分:0)

对于这个实现,矩阵的对角元素必须是真的,比如,graph-&gt; matrix [i] [i]对于所有我应该是真的。这有点不寻常,我猜你的输入不是,在这种情况下,只是暂时将它们分配给true,它应该有效。

相关问题