您的位置:首页 > 其它

gSkeletonClu: Revealing Density-Based Clustering Structure from the Core-Connected Tree of a Network

2013-11-01 20:55 549 查看
完成的主要是将一个文件的格式转换到另一格式,然后用gSkeletonClu的程序运行后,再讲结果转换到这种格式,并填写运行的结果。

所给的格式是:

Source;Target;Type;Id;Label;Weight
所要用的数据是source和target,weight

但是在这个格式里source和target均是字符串型,所以需要转换为整型

且所需的格式里,应该是id从0或1开始,且连续

即,先按照source排序,相同source的,按照target排序;

最后需要的格式是:id,id,weight

其中id需要转换到原source或者target的值

public class ReadgSkeletonClu是主要方法实现;

public class GenerateNeedDataTest 产生所需数据

public class ReBuildCSVTest 从运行结果产生最后的数据

包含类class Subgraph

class Point

class FinalData

class PointToCluster

public class ReadgSkeletonClu

package gSkeletonCluTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
* @author zhaodeng
*
*/
public class ReadgSkeletonClu {

List<String> sourceList = new ArrayList<String>();
List<Subgraph> graphDataList = new ArrayList<Subgraph>();
List<Subgraph> neededGraphDataList = new ArrayList<Subgraph>();
List<Point> pointList = new ArrayList<Point>();
List<FinalData> finalDatas = new ArrayList<FinalData>();

List<PointToCluster> pointToClusters = new ArrayList<PointToCluster>();

/**
* @param fileName
*            读取最开始的csv文件,产生List<Subgraph> graphDataList
*/
public List<Subgraph> readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
// System.out.println(tempString);
if (line != 1) {
String tempSplitString[] = tempString.split(";");
Subgraph subgraph = new Subgraph(tempSplitString[0],
tempSplitString[1], tempSplitString[2],
Integer.valueOf(tempSplitString[3]),
tempSplitString[4],
Integer.valueOf(tempSplitString[5]));
graphDataList.add(subgraph);
}

// System.out.println("line " + line + ": " + tempString);
line++;
}
// System.out.println("line's number: " + line);
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return graphDataList;
}

/*
* 根据读取文件产生的List<Subgraph> graphDataList,进行source和target的转换,
* 重新生成新的List<Subgraph> neededGraphDataList
*/
public List<Subgraph> generateNeededGraph(List<Subgraph> graphDataList) {

int id = 1;
for (Subgraph subgraph : graphDataList) {
Subgraph tmpSubgraph = subgraph;
boolean ifSourceExist = false; // 不存在
boolean ifTargetExist = false;
for (Point point : pointList) {
if (subgraph.getSource().equals(point.getName())) {
ifSourceExist = true;
tmpSubgraph.setSource(String.valueOf(point.getId()));

}
if (subgraph.getTarget().equals(point.getName())) {
ifTargetExist = true;
tmpSubgraph.setTarget(String.valueOf(point.getId()));
}
}
if (!ifSourceExist) {
Point point = new Point(subgraph.getSource(), id++);
pointList.add(point);
tmpSubgraph.setSource(String.valueOf(point.getId()));
}
if (!ifTargetExist) {
Point point = new Point(subgraph.getTarget(), id++);
pointList.add(point);
tmpSubgraph.setTarget(String.valueOf(point.getId()));
}
neededGraphDataList.add(tmpSubgraph);
// System.out.println(tmpSubgraph.getSource() + ";"+
// tmpSubgraph.getTarget());
}// for
return neededGraphDataList;
}

/*
* 根据转换后的List<Subgraph> neededGraphDataList,来提取程序运行所需的格式 int source;int
* target;int weight; 生成List<FinalData> finalDatas
*/
public List<FinalData> generateFinalData(List<Subgraph> neededGraphDataList) {
for (Subgraph subgraph : neededGraphDataList) {
FinalData finalData = null;
if (Integer.valueOf(subgraph.getSource()) < Integer
.valueOf(subgraph.getTarget())) {
finalData = new FinalData(
Integer.valueOf(subgraph.getSource()),
Integer.valueOf(subgraph.getTarget()),
subgraph.getWeight());
finalDatas.add(finalData);
} else if (Integer.valueOf(subgraph.getSource()) > Integer
.valueOf(subgraph.getTarget())) {
finalData = new FinalData(
Integer.valueOf(subgraph.getTarget()),
Integer.valueOf(subgraph.getSource()),
subgraph.getWeight());
finalDatas.add(finalData);
}
}

// 排序
ComparatorFinalData comparator = new ComparatorFinalData();
Collections.sort(finalDatas, comparator);
return finalDatas;
}

/*
* 以最后产生的List<FinalData> finalDatas, 写入fileName.txt文件,去运行结果
*/
public void generatefinalDatasFile(List<FinalData> finalDatas,
String fileName) throws IOException {
File myFilePath = new File(fileName.toString());
if (myFilePath.exists()) { // 如果该文件存在,则删除
myFilePath.delete();
} else {
myFilePath.createNewFile();
}

for (FinalData finalData : finalDatas) {
String addFileContent = finalData.getSource() + " "
+ finalData.getTarget() + " " + finalData.getWeight();
addToFile(fileName, addFileContent);
}
}

/*
* 以运行结果1.txt文件来重新生成
*/
public void generateNewBiggestSubGraphFile(List<Subgraph> subgraphs,
String fileName) {
File myFilePath = new File(fileName.toString());
if (myFilePath.exists()) { // 如果该文件存在,则删除
myFilePath.delete();
}
for (Subgraph subgraph : subgraphs) {
String subgraphString = subgraph.getSource() + ";"
+ subgraph.getTarget() + ";" + subgraph.getType() + ";"
+ String.valueOf(subgraph.getId()) + ";"
+ subgraph.getLable() + ";"
+ String.valueOf(subgraph.getWeight());
addToFile(fileName, subgraphString);
}
}

public static void addToFile(String filePathAndName, String fileContent) {
try {
File myFilePath = new File(filePathAndName.toString());
// if (!myFilePath.exists()) { // 如果该文件不存在,则创建
// myFilePath.createNewFile();
// }
// FileWriter(myFilePath, true); //实现不覆盖追加到文件里

// FileWriter(myFilePath); 覆盖掉原来的内容
FileWriter resultFile = new FileWriter(myFilePath, true);
PrintWriter myFile = new PrintWriter(resultFile);
// 给文件里面写内容,原来的会覆盖掉
myFile.println(fileContent);
resultFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public List<PointToCluster> ReadFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// int line = 1;
while ((tempString = reader.readLine()) != null) {
// System.out.println("line " + line + ": " + tempString);
// line++;
String tmpSplitString[] = tempString.split(" ", 2);
PointToCluster pointToCluster = new PointToCluster(
Integer.valueOf(tmpSplitString[0].trim()),
Integer.valueOf(tmpSplitString[1].trim()));
pointToClusters.add(pointToCluster);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return pointToClusters;
}

/*
* 根据最后运行的1.txt文件产生的List<PointToCluster>,来产生最后所需的graph文件 包含了lable,并且原点不变
*/
public List<Subgraph> writeToSubgraph(List<PointToCluster> pointToClusters) {
for (Subgraph subgraph : neededGraphDataList) {
String lable = "";
for (PointToCluster pointToCluster : pointToClusters) {
if (String.valueOf(pointToCluster.getId()).equals(
subgraph.getSource())) {
lable = String.valueOf(pointToCluster.getClusterId());
lable += "(p1)";
}
}
for (PointToCluster pointToCluster : pointToClusters) {
if (String.valueOf(pointToCluster.getId()).equals(
subgraph.getTarget())) {
lable += String.valueOf(pointToCluster.getClusterId());
lable += "(p2)";
}
}
subgraph.setLable(lable);
}
for (Subgraph subgraph : neededGraphDataList) {
for (Point point : pointList) {
if (subgraph.getSource().equals(String.valueOf(point.getId()))) {
subgraph.setSource(point.getName());
}
if (subgraph.getTarget().equals(String.valueOf(point.getId()))) {
subgraph.setTarget(point.getName());
}
}
}
return neededGraphDataList;
}

// 比较排序,因为程序运行时要求是:点连续,是指第一列的点连续,第二列的点从小到大
class ComparatorFinalData implements Comparator {
public int compare(Object arg0, Object arg1) {
FinalData data1 = (FinalData) arg0;
FinalData data2 = (FinalData) arg1;

int flag = Integer.valueOf(data1.getSource()).compareTo(
Integer.valueOf(data2.getSource()));
if (flag == 0) {
return Integer.valueOf(data1.getTarget()).compareTo(
Integer.valueOf(data2.getTarget()));
} else {
return flag;
}
}
}

// 产生最后的文件:id,点(原来的),clutster
public void newGeneratePointData(List<PointToCluster> pointToClusters,
String resultFileName, String read2fileName) {
File myFilePath = new File(resultFileName.toString());
if (myFilePath.exists()) { // 如果该文件存在,则删除
myFilePath.delete();
}
// maxcluster
int maxcluster = 0;
for (PointToCluster pointToCluster : pointToClusters) {
if (pointToCluster.getClusterId() > maxcluster) {
maxcluster = pointToCluster.getClusterId();
}
}

// 读取hubs和outliers
File file = new File(read2fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
if (tempString.contains("hubs:")) {
maxcluster++;
String hubsString = tempString.substring(6);
String tempSplitString[] = hubsString.split("  ");
for (int i = 0; i < tempSplitString.length; i++) {
PointToCluster hub = new PointToCluster(
Integer.valueOf(tempSplitString[i].trim()),
maxcluster);
pointToClusters.add(hub);
}
}
if (tempString.contains("outliers:")) {
maxcluster++;
String outliersString = tempString.substring(10);
String tempSplitString[] = outliersString.split("  ");
for (int i = 0; i < tempSplitString.length; i++) {
PointToCluster outlier = new PointToCluster(
Integer.valueOf(tempSplitString[i].trim()),
maxcluster);
pointToClusters.add(outlier);
}
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}

// 从cluster写入最后的格式
String title = "Id;Lable;Modularity Class";
addToFile(resultFileName, title);
for (PointToCluster pointToCluster : pointToClusters) {
for (Point point : pointList) {
if (point.getId() == pointToCluster.getId()) {
String addIdPointToCluster = point.getName() + ";"
+ point.getName() + ";"
+ pointToCluster.getClusterId();
addToFile(resultFileName, addIdPointToCluster);
}
}
}

}

}


public class GenerateNeedDataTest

package gSkeletonCluTest;

import gSkeletonCluTest.ReadgSkeletonClu.ComparatorFinalData;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
* @author zhaodeng
*
*/
public class GenerateNeedDataTest {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

ReadgSkeletonClu readgSkeletonClu = new ReadgSkeletonClu();
String fileName = "E:/gSkeletonClu_project/dataFile/Biggest_SubGraph_e.csv";
String finalFileName = "E:/gSkeletonClu_project/dataFile/karate.pairs";

List<Subgraph> graphDataList = readgSkeletonClu
.readFileByLines(fileName);
List<Subgraph> neededGraphDataList = readgSkeletonClu
.generateNeededGraph(graphDataList);
// 生成所需数据格式
List<FinalData> finalDatas = readgSkeletonClu
.generateFinalData(neededGraphDataList);

// 生成所需数据karate.pairs
System.out.println("building the karate.pairs ... please wait ...");
readgSkeletonClu.generatefinalDatasFile(finalDatas, finalFileName);
System.out.println("build the karate.pairs successful! ");
}
}


public class ReBuildCSVTest

package gSkeletonCluTest;

import java.io.File;
import java.util.List;

/**
* @author zhaodeng
*
*/
public class ReBuildCSVTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

ReadgSkeletonClu readgSkeletonClu = new ReadgSkeletonClu();
String BiggestSubGraphPath = "E:/gSkeletonClu_project/dataFile/Biggest_SubGraph_e.csv";
String txt1 = "E:/gSkeletonClu_project/dataFile/1_new.txt";
String txt2 = "E:/gSkeletonClu_project/dataFile/2_new.txt";
String newBiggestSubGraphPath = "E:/gSkeletonClu_project/dataFile/Biggest_SubGraph_e_final.csv";

List<Subgraph> graphDataList = readgSkeletonClu
.readFileByLines(BiggestSubGraphPath);
List<Subgraph> neededGraphDataList = readgSkeletonClu
.generateNeededGraph(graphDataList);

List<PointToCluster> pointToClusters = readgSkeletonClu
.ReadFileByLines(txt1);

//		List<Subgraph> subgraphs = readgSkeletonClu
//				.writeToSubgraph(pointToClusters);

System.out
.println("building the Biggest_SubGraph_e_new.csv ... please wait ...");
readgSkeletonClu.newGeneratePointData(pointToClusters,
newBiggestSubGraphPath,txt2);
System.out.println("build the Biggest_SubGraph_e_new.csv successful!");
}

}


class Subgraph

package gSkeletonCluTest;
/**
* @author zhaodeng
*
*/
public class Subgraph {

String source;
String target;
String type;
int id;
String lable;
int weight;

public Subgraph() {
super();
}

public Subgraph(String source, String target, String type, int id,
String lable, int weight) {
super();
this.source = source;
this.target = target;
this.type = type;
this.id = id;
this.lable = lable;
this.weight = weight;
}

public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}

}


class Point

package gSkeletonCluTest;

/**
* @author zhaodeng
*
*/
public class Point {

String name;
int id;

public Point() {
super();
}

public Point(String name, int id) {
super();
this.name = name;
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

}


class FinalData

package gSkeletonCluTest;

/**
* @author zhaodeng
*
*/
public class FinalData {

int source;
int target;
int weight;

public FinalData() {
super();
}

public FinalData(int source, int target, int weight) {
super();
this.source = source;
this.target = target;
this.weight = weight;
}

public int getSource() {
return source;
}

public void setSource(int source) {
this.source = source;
}

public int getTarget() {
return target;
}

public void setTarget(int target) {
this.target = target;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

}


class PointToCluster

package gSkeletonCluTest;

/**
* @author zhaodeng
*
*/
public class PointToCluster {

int id;
int clusterId;

public PointToCluster() {
super();
}

public PointToCluster(int id, int clusterId) {
super();
this.id = id;
this.clusterId = clusterId;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getClusterId() {
return clusterId;
}

public void setClusterId(int clusterId) {
this.clusterId = clusterId;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: