|
| 1 | +package org.nodes.motifs; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.LinkedHashSet; |
| 6 | +import java.util.LinkedList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import org.nodes.Graph; |
| 11 | +import org.nodes.Node; |
| 12 | + |
| 13 | +import nl.peterbloem.kit.Functions; |
| 14 | +import nl.peterbloem.kit.Global; |
| 15 | +import nl.peterbloem.kit.Series; |
| 16 | + |
| 17 | +/** |
| 18 | + * Enumerates all subgraphs using the FANMOD algorithm. |
| 19 | + * @author Peter |
| 20 | + * |
| 21 | + */ |
| 22 | +public class AllSubgraphs implements Iterable<Set<Integer>> |
| 23 | +{ |
| 24 | + private Graph<?> data; |
| 25 | + private int size; |
| 26 | + private List<Double> probs = null; |
| 27 | + |
| 28 | + |
| 29 | + public AllSubgraphs(Graph<?> data, int size) |
| 30 | + { |
| 31 | + this.data = data; |
| 32 | + this.size = size; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param data |
| 37 | + * @param size |
| 38 | + * @param prob The probability at each node in the search tree, that that |
| 39 | + * node will be expanded. |
| 40 | + */ |
| 41 | + public AllSubgraphs(Graph<?> data, int size, double prob) |
| 42 | + { |
| 43 | + this.data = data; |
| 44 | + this.size = size; |
| 45 | + |
| 46 | + probs = new ArrayList<Double>(size); |
| 47 | + for(int i : Series.series(size)) |
| 48 | + probs.add(prob); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns an iterator over lists indices of the original graph. Each |
| 53 | + * returned list represents a (weakly) connected subgraph, and all such |
| 54 | + * subgraphs are returned once. |
| 55 | + * |
| 56 | + * @return |
| 57 | + */ |
| 58 | + public Iterator<Set<Integer>> iterator() |
| 59 | + { |
| 60 | + return new Enumerator(); |
| 61 | + } |
| 62 | + |
| 63 | + private class Enumerator implements Iterator<Set<Integer>> |
| 64 | + { |
| 65 | + private LinkedList<State> buffer = new LinkedList<State>(); |
| 66 | + |
| 67 | + public Enumerator() |
| 68 | + { |
| 69 | + for(int index : Series.series(data.size())) |
| 70 | + buffer.add(new State(index)); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean hasNext() |
| 75 | + { |
| 76 | + expandBuffer(); |
| 77 | + return ! buffer.isEmpty(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Set<Integer> next() |
| 82 | + { |
| 83 | + expandBuffer(); |
| 84 | + return buffer.pop().indices(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Expands the buffer until the first element is a leaf state. If that's |
| 89 | + * not possible, it returns. |
| 90 | + */ |
| 91 | + private void expandBuffer() |
| 92 | + { |
| 93 | + if(buffer.isEmpty()) |
| 94 | + return; |
| 95 | + |
| 96 | + while(! buffer.peek().isLeaf()) |
| 97 | + { |
| 98 | + State next = buffer.pop(); |
| 99 | + |
| 100 | + if(probs == null || Global.random().nextDouble() < probs.get(next.indices().size() - 1)) |
| 101 | + buffer.addAll(0, next.children()); |
| 102 | + |
| 103 | + if(buffer.isEmpty()) |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + assert(buffer.isEmpty() || buffer.peek().isLeaf()); |
| 108 | + } |
| 109 | + |
| 110 | + private class State |
| 111 | + { |
| 112 | + Set<Integer> current = new LinkedHashSet<Integer>(); |
| 113 | + Set<Integer> neighbors = new LinkedHashSet<Integer>(); |
| 114 | + |
| 115 | + private State() |
| 116 | + { |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + public State(int start) |
| 121 | + { |
| 122 | + current.add(start); |
| 123 | + initNeighbors(); |
| 124 | + } |
| 125 | + |
| 126 | + private void initNeighbors() |
| 127 | + { |
| 128 | + if(isLeaf()) |
| 129 | + return; |
| 130 | + |
| 131 | + int max = Functions.max(current); |
| 132 | + |
| 133 | + for(int index : current) |
| 134 | + for(Node<?> neighbor : data.get(index).neighbors()) |
| 135 | + if(neighbor.index() > max && ! current.contains(neighbor.index())) |
| 136 | + neighbors.add(neighbor.index()); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Adds all children of the state to the given list. |
| 141 | + * |
| 142 | + * @param list |
| 143 | + */ |
| 144 | + public List<State> children() |
| 145 | + { |
| 146 | + // * TODO: we can generate this list on the fly |
| 147 | + List<State> children = new ArrayList<State>(neighbors.size()); |
| 148 | + |
| 149 | + for(int neighbor : neighbors) |
| 150 | + { |
| 151 | + State state = new State(); |
| 152 | + |
| 153 | + state.current.addAll(this.current); |
| 154 | + state.current.add(neighbor); |
| 155 | + |
| 156 | + state.initNeighbors(); |
| 157 | + |
| 158 | + children.add(state); |
| 159 | + } |
| 160 | + |
| 161 | + return children; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Whether this state represents a complete subgraph (ie. it has the |
| 166 | + * required number of nodes). |
| 167 | + * @return |
| 168 | + */ |
| 169 | + public boolean isLeaf() |
| 170 | + { |
| 171 | + return current.size() == size; |
| 172 | + } |
| 173 | + |
| 174 | + public Set<Integer> indices() |
| 175 | + { |
| 176 | + return current; |
| 177 | + } |
| 178 | + |
| 179 | + public String toString() |
| 180 | + { |
| 181 | + return current + "_" + neighbors; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments