我有一个包含数百万个顶点和大约数十亿个边的文件,其格式为edgelist如下:
7 5
1 2
2 4
2 5
3 6
3 7
此图表是无向的,但是我需要将图形更改为有向和邻接列表并加权(每条边具有权重1)形式如下:
7 5
2 1
1 1 4 1 5 1
6 1 7 1
2 1
2 1
3 1
3 1
它有一个包含顶点和边数的标题。 我使用hashmap实现了这样的代码。但是,在非常大的图形中,当边缘超过数十亿时,由于使用了hashmap,我的内存超出了错误。有没有人想知道这个代码适用于任何大小的图形? 这是我的代码:
public class Mygraphgenerator {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String fileName = "test.edgelist";
String line = null;
String [] lineSepartor;
int currentvertice=1;
int edgecounter=0;
int vertice;
int adjacents;
PrintWriter pw = new PrintWriter(new File("test.adjlist"));
HashMap<Integer, ArrayList<String>> directedMaker = new HashMap<Integer, ArrayList<String>>();
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String firstline;
String adjLists="";
String sentinel=" ";
boolean f = true;
while((line = bufferedReader.readLine()) != null)
{
//changeto networkxformat
//lineSepartor=line.split("\t");
lineSepartor=line.split(" ");
vertice=Integer.parseInt(lineSepartor[0]) + 1;
adjacents=Integer.parseInt(lineSepartor[1]) + 1;
//if switched from current vertices to the next one (0 to 1)
if (currentvertice==vertice){
if(!f)
pw.write(" " + adjacents + " " + "1");
else
{
pw.write(adjacents + " " + "1");
f=false;
}
ArrayList<String> edges;
if (directedMaker.containsKey(adjacents)){
edges=directedMaker.get(adjacents);
edges.add(vertice + "");
}else {
edges=new ArrayList<String>();
edges.add(vertice + "");
}
directedMaker.put(adjacents,edges);
//System.out.println(currentvertice);
//System.out.println(adjLists);
}else {
sentinel = "\n";
//System.out.println("Insde first while" + currentvertice + " adjLists " + adjLists);
pw.write(sentinel);
if (vertice -currentvertice>1)
for (int i = currentvertice + 1; i < vertice; i++){
if (directedMaker.containsKey(i)){
f=false;
List<String> edges = directedMaker.get(i);
for (int j = 0; j< edges.size(); j++){
if(j!=0)
pw.write(" " + edges.get(j) + " " + "1");
else
pw.write(edges.get(j) + " " + "1");
}
directedMaker.remove(i);
}
pw.write("\n");
}
if (directedMaker.containsKey(vertice)){
f=false;
List<String> edges = directedMaker.get(vertice);
for (int i =0; i< edges.size(); i++){
if(i!=0)
pw.write(" " + edges.get(i) + " " + "1");
else
pw.write(edges.get(i) + " " + "1");
}
directedMaker.remove(vertice);
pw.write(" " + adjacents + " " + "1");
}else
pw.write(adjacents + " " + "1");
ArrayList<String> edges;
if (directedMaker.containsKey(adjacents)){
edges=directedMaker.get(adjacents);
edges.add(vertice+"");
}else {
edges=new ArrayList<String>();
edges.add(vertice+"");
}
directedMaker.put(adjacents,edges);
}
currentvertice=vertice;
edgecounter++;
}
//System.out.println("In middle " + currentvertice + " adjLists " + adjLists);
pw.write(adjLists);
while (directedMaker.size()!=0){
pw.write("\n");
currentvertice++;
//System.out.println("Outside first while " + currentvertice);
if (directedMaker.containsKey(currentvertice)){
List<String> edges = directedMaker.get(currentvertice);
for (int i =0; i< edges.size(); i++){
if(i!=0)
pw.write(" " + edges.get(i) + " " + "1");
else
pw.write(edges.get(i) + " " + "1");
}
directedMaker.remove(currentvertice);
}
}
pw.close();
System.out.println("done!");
System.out.println("edges of this graph : " + edgecounter);
} catch(FileNotFoundException ex)
{
System.out.println(
"Unable to open file '" +
fileName + "'");
}
}
}