Skip to content

Instantly share code, notes, and snippets.

@nzw0301
Last active August 29, 2015 14:27
Show Gist options
  • Save nzw0301/88ea08930e1705a917d4 to your computer and use it in GitHub Desktop.
Save nzw0301/88ea08930e1705a917d4 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import com.google.common.primitives.Ints;
/**
* Created by nzw on 2015/08/12.
*/
public class conductance {
private Set<int[]> edges = new HashSet<>();
public conductance(String linkPath, String comPath) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(linkPath));
br.lines().forEach(l -> {
if (!l.equals("")) {
edges.add(Arrays.stream(l.split(" "))
.mapToInt(Integer::parseInt)
.toArray()
);
}
});
br.close();
br = new BufferedReader(new FileReader(comPath));
br.lines().forEach(l -> {
System.out.println(
calCondactance(Arrays.stream(l.split(" "))
.mapToInt(Integer::parseInt)
.toArray()
)
);
});
br.close();
}
public double calCondactance(int[] com){
List<Integer> comList = Ints.asList(com);
int ms = 0;
int cs = 0;
for(int[] edge : edges){
int inNode = 0;
for(int nodeID : edge){
if(comList.contains(nodeID)){
inNode++;
}
}
if(inNode==1){
cs++;
}else if (inNode==2){
ms++;
}
}
return (double)cs/(2*ms+cs);
}
public static void main(String[] args) throws IOException {
String linkPath = "src/resources/sample.tsv";
String comPath = "src/resources/community.txt";
conductance c = new conductance(linkPath,comPath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment