Skip to content

Commit

Permalink
ocdid done
Browse files Browse the repository at this point in the history
  • Loading branch information
MarenHanke committed Oct 14, 2023
1 parent 377c4c3 commit eaab312
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
import org.la4j.matrix.Matrix;
import org.la4j.matrix.dense.Basic2DMatrix;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* Implements the algorithm to the OCDID (Overlapping Community Detection based on Information Dynamics) method, by Z. Sun, B. Wang, J. Sheng,Z. Yu, J. Shao:
Expand All @@ -24,11 +18,11 @@ public class OCDIDAlgorithm implements OcdAlgorithm {
/**
* The threshold value used for spreading the information in the network.
*/
private double thresholdOCDID = 0.001;
private double thresholdOCDID = 0.00001;
/**
* The threshold value used in the community detection phase of the algorithm.
*/
private double thresholdCD = 0.001;
private double thresholdCD = 0.002;
/**
* The threshold value used in the overlapping community detection phase of the algorithm.
*/
Expand Down Expand Up @@ -118,7 +112,7 @@ public Cover detectOverlappingCommunities(CustomGraph graph) throws InterruptedE

//Community detection
Matrix communities = cd(graph, I_v);
Matrix overlapping_communities = ocd_fair_and_good(graph, communities, I_uv);
Matrix overlapping_communities = ocd(graph, communities, I_uv);

Matrix membershipMatrix = toMembershipMatrix(overlapping_communities);
return new Cover(graph, membershipMatrix);
Expand Down Expand Up @@ -414,78 +408,7 @@ protected Matrix cd(CustomGraph graph, double[] informationList) throws Interrup
* @throws InterruptedException if the thread was interrupted
*/
protected Matrix ocd(CustomGraph graph, Matrix communities, double[][] I_uv) throws InterruptedException{
List<Node> BN = boundaryNodes(graph, communities);

for (Node node : BN) {
int nodeID = node.getIndex();

Set<Node> neighbours = graph.getNeighbours(node);
Set<Integer> NC = computeNC(communities, nodeID, neighbours);

double hightesB = 0.0; //extension so that all nodes are assigned
int communityOfHighestB = 0;

for (int community : NC) {
List<Node> communityMembers = getCommunityMembers(graph, communities, community);
double B = belongingDegree(node, neighbours, communityMembers, I_uv);

if (B > thresholdOCD) {
communities.set(nodeID, community, 1);
}

if(B > hightesB){ //extension so that all nodes are assigned
communityOfHighestB = community;
}
}
if (getMemberships(communities, nodeID).isEmpty()){ //extension so that all nodes are assigned
if (hightesB > 0.0) {
communities.set(nodeID, communityOfHighestB, 1);
}else{ //there was no information flow from or to the node then it becomes an own community
communities.set(nodeID, nodeID, 1);
}
}
}
return communities;
}

protected Matrix ocd_fair(CustomGraph graph, Matrix communities, double[][] I_uv) throws InterruptedException{
List<Node> BN = boundaryNodes(graph, communities);
Matrix oc = communities.copy();

for (Node node : BN) {
int nodeID = node.getIndex();

Set<Node> neighbours = graph.getNeighbours(node);
Set<Integer> NC = computeNC(oc, nodeID, neighbours);

double hightesB = 0.0; //extension so that all nodes are assigned
int communityOfHighestB = 0;

for (int community : NC) {
List<Node> communityMembers = getCommunityMembers(graph, communities, community);
double B = belongingDegree(node, neighbours, communityMembers, I_uv);

if (B > thresholdOCD) {
oc.set(nodeID, community, 1);
}

if(B > hightesB){ //extension so that all nodes are assigned
communityOfHighestB = community;
}
}
if (getMemberships(communities, nodeID).isEmpty()){ //extension so that all nodes are assigned
if (hightesB > 0.0) {
oc.set(nodeID, communityOfHighestB, 1);
}else{ //there was no information flow from or to the node then it becomes an own community
oc.set(nodeID, nodeID, 1);
}
}
}
return oc;
}

protected Matrix ocd_fair_and_good(CustomGraph graph, Matrix communities, double[][] I_uv) throws InterruptedException{
List<Node> BN = boundaryNodes(graph, communities);
Set<Node> BN = boundaryNodes(graph, communities);
boolean changes = true;
while(changes){
changes=false;
Expand All @@ -499,7 +422,7 @@ protected Matrix ocd_fair_and_good(CustomGraph graph, Matrix communities, double
int communityOfHighestB = 0;

for (int community : NC) {
List<Node> communityMembers = getCommunityMembers(graph, communities, community);
Set<Node> communityMembers = getCommunityMembers(graph, communities, community);
double B = belongingDegree(node, neighbours, communityMembers, I_uv);

if (B > thresholdOCD) {
Expand Down Expand Up @@ -539,8 +462,8 @@ protected Matrix ocd_fair_and_good(CustomGraph graph, Matrix communities, double
* @return A list of consisting of boundary nodes and nodes with no community
* @throws InterruptedException if the thread was interrupted
*/
protected List<Node> boundaryNodes(CustomGraph graph, Matrix communities) throws InterruptedException {
List<Node> BN = new ArrayList<>();
protected Set<Node> boundaryNodes(CustomGraph graph, Matrix communities) throws InterruptedException {
Set<Node> BN = new HashSet<>();

Iterator<Node> nodesIt = graph.nodes().iterator();
Node node;
Expand Down Expand Up @@ -594,8 +517,8 @@ protected Set<Integer> computeNC(Matrix communities, int nodeID, Set<Node> neigh
* @return A List of nodes containing the members of the input community
* @throws InterruptedException if the thread was interrupted
*/
protected List<Node> getCommunityMembers(CustomGraph graph, Matrix communities, int community) throws InterruptedException {
List<Node> communityMembers = new ArrayList<>();
protected Set<Node> getCommunityMembers(CustomGraph graph, Matrix communities, int community) throws InterruptedException {
Set<Node> communityMembers = new HashSet<>();

Iterator<Node> nodesIt = graph.nodes().iterator();
while (nodesIt.hasNext()) {
Expand Down Expand Up @@ -638,9 +561,9 @@ protected List<Integer> getMemberships(Matrix communities, Integer nodeId) throw
* @return The belonging degree of the input node to the input community
* @throws InterruptedException if the thread was interrupted
*/
protected double belongingDegree(Node node, Set<Node> neighbours, List<Node> communityMembers, double[][] I_uv) throws InterruptedException{
protected double belongingDegree(Node node, Set<Node> neighbours, Set<Node> communityMembers, double[][] I_uv) throws InterruptedException{
//compute BT
List<Node> intersection = new ArrayList<>();
Set<Node> intersection = new HashSet<>();

for (Node neighbour : neighbours) {
if (communityMembers.contains(neighbour)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public void testCd() throws InterruptedException {
Matrix actualMatrix = ocdid.cd(graph, informationList);
assertEquals(expectedMatrix, actualMatrix);
}
//@Ignore
@Test
public void testOcd() throws InterruptedException {
OCDIDAlgorithm ocdid = new OCDIDAlgorithm();
Expand Down Expand Up @@ -193,8 +194,8 @@ public void testBoundaryNodes() throws InterruptedException {
communities.set(2, 1, 1);
communities.set(3, 1, 1);

List<Node> actualBoundaryNodes = ocdid.boundaryNodes(graph, communities);
List<Node> expectedBoundaryNodes = new ArrayList<Node>();
Set<Node> actualBoundaryNodes = ocdid.boundaryNodes(graph, communities);
Set<Node> expectedBoundaryNodes = new HashSet<>();
expectedBoundaryNodes.add(graph.getNode(4));
assertEquals(expectedBoundaryNodes, actualBoundaryNodes);

Expand All @@ -210,8 +211,8 @@ public void testBoundaryNodes() throws InterruptedException {
communities2.set(5, 2, 1);
communities2.set(6, 2, 1);

List<Node> actualBoundaryNodes2 = ocdid.boundaryNodes(graph2, communities2);
List<Node> expectedBoundaryNodes2 = new ArrayList<Node>();
Set<Node> actualBoundaryNodes2 = ocdid.boundaryNodes(graph2, communities2);
Set<Node> expectedBoundaryNodes2 = new HashSet<>();
expectedBoundaryNodes2.add(graph2.getNode(2));
expectedBoundaryNodes2.add(graph2.getNode(3));
assertEquals(expectedBoundaryNodes2, actualBoundaryNodes2);
Expand Down Expand Up @@ -252,15 +253,15 @@ public void testGetCommunityMembers() throws InterruptedException {
inputMatrix.set(3, 1, 1);

//community with two members
List<Node> actualMembers = ocdid.getCommunityMembers(graph, inputMatrix, 1);
List<Node> expectedMembers = new ArrayList<>();
Set<Node> actualMembers = ocdid.getCommunityMembers(graph, inputMatrix, 1);
Set<Node> expectedMembers = new HashSet<>();
expectedMembers.add(graph.getNode(1));
expectedMembers.add(graph.getNode(3));
assertEquals(expectedMembers, actualMembers);

//community with zero members
List<Node> actualMembers2 = ocdid.getCommunityMembers(graph, inputMatrix, 2);
List<Node> expectedMembers2 = new ArrayList<>();
Set<Node> actualMembers2 = ocdid.getCommunityMembers(graph, inputMatrix, 2);
Set<Node> expectedMembers2 = new HashSet<>();
assertEquals(expectedMembers2, actualMembers2);
}
@Test
Expand All @@ -273,11 +274,11 @@ public void testGetMemberships() throws InterruptedException {
inputMatrix.set(3, 0, 1);
inputMatrix.set(3, 1, 1);

List<Integer> expectedMemberships1 = new ArrayList<Integer>();
List<Integer> expectedMemberships1 = new ArrayList<>();
List<Integer> actualMemberships1 = ocdid.getMemberships(inputMatrix, 0);
assertEquals(expectedMemberships1, actualMemberships1);

List<Integer> expectedMemberships2 = new ArrayList<Integer>();
List<Integer> expectedMemberships2 = new ArrayList<>();
expectedMemberships2.add(0);
expectedMemberships2.add(1);
List<Integer> actualMemberships2 = ocdid.getMemberships(inputMatrix, 3);
Expand All @@ -294,7 +295,7 @@ public void testBelongingDegree() throws InterruptedException {
neighbours.add(graph.getNode(1));
neighbours.add(graph.getNode(3));

List<Node> communityMembers = new ArrayList<Node>();
Set<Node> communityMembers = new HashSet<>();
communityMembers.add(graph.getNode(3));
communityMembers.add(graph.getNode(4));
communityMembers.add(graph.getNode(5));
Expand Down

0 comments on commit eaab312

Please sign in to comment.