-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCycleDetector.java
88 lines (81 loc) · 2.1 KB
/
CycleDetector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Detects 4-cycles in H by calculating H*H'
* The diagonal elements in H*H' can be ignored, but the off-diagonal
* elements should not be greater than 1 for a cycle-free graph.
* If row r, col c in H*H' is 2, then there is a 4-cycle involving the
* check nodes r and c in H.
* H*H' will be symmetric, so it is enough to inspect the triangle above the diagonal.
*/
import java.util.*;
/**
* Detects 4 cycles and returns as an array
* The array contains contiguous blocks of 4 integers; each block is one 4-cycle
* @author Rajaraman
*/
public class CycleDetector
{
public int[] detectCycles (BitSet[] H)
{
FlexArray arr = new FlexArray();
int rows = H.length;
BitSet tmp;
for (int i=0; i<rows; i++)
{
for (int j=i+1; j<rows; j++)
{
tmp = (BitSet)H[i].clone();
tmp.and(H[j]);
if (tmp.cardinality() <= 1)
continue;
//printBitSet(tmp);
for (int b=tmp.nextSetBit(0); b>=0; b=tmp.nextSetBit(b+1))
{
for (int c=tmp.nextSetBit(b+1); c>=0; c=tmp.nextSetBit(c+1))
{
arr.add(b);arr.add(i);arr.add(c);arr.add(j); //arr.add(b);
//G.traceln (b +"-" +i +"-" +c +"-" +j +"-" +b);
}
}
}
}
return arr.toArray();
}
public void printCycles (BitSet[] H)
{
G.trace("\nCycles :");
int[] cycleArray = detectCycles (H);
G.traceln (cycleArray.length==0 ? " none" : " ");
for (int i=0; i<cycleArray.length; i++)
{
G.trace (cycleArray[i] +"-");
if ((i+1)%4==0 && i!=0)
G.traceln(cycleArray[i-3] +"");
}
}
/*****************
// temporary helper method for debugging
public void printBitSet (BitSet bs)
{
int cols = 50; // cludge !
for (int i=0; i<cols; i++)
{
if (bs.get(i))
G.trace("1 ");
else
G.trace(". ");
}
G.traceln();
}
*******************/
public static void main (String[] args)
{
BitSet[] bs = new BitSet[10];
for (int i=0; i<10; i++)
bs[i] = new BitSet();
bs[2].set (3); bs[2].set (9); bs[2].set (11); bs[2].set (15);
bs[7].set (3); bs[7].set (9); bs[7].set (11);
bs[8].set (9); bs[8].set (11); bs[8].set (15);
CycleDetector cd = new CycleDetector();
cd.printCycles (bs);
}
}