Skip to content

Commit

Permalink
store in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
Adoby7 committed Apr 14, 2019
1 parent a33f623 commit e0d9f25
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
*.iml
out
data
efiles
8 changes: 7 additions & 1 deletion src/Certificate/Edge.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package Certificate;

import java.io.Serializable;
import java.util.Objects;

public class Edge implements Comparable<Edge> {
public class Edge implements Comparable<Edge>, Serializable {

public int src;
public int des;
Expand Down Expand Up @@ -30,4 +31,9 @@ public int compareTo(Edge o) {
}
return this.src - o.src;
}

@Override
public String toString() {
return String.format("%d %d", src, des);
}
}
5 changes: 4 additions & 1 deletion src/Certificate/Estimate.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ private static void estimateGraph(String fileName) throws Exception {
}
adjacentList.add(nbrs);
}
in.close();
System.out.println("finish loading");

nEdges /= 2;
HashMap<Edge, Integer> edges = new HashMap<>();
for (int i = 2; i <= nEdges; i *= 2) {
if (edges.size() >= nEdges) break;
Set<Edge> cert = new Certificate(adjacentList).generateCert(i);
Set<Edge> cert = new NewCertificate(adjacentList).generateCert(i);
for (Edge ct: cert) {
if (!edges.containsKey(ct)) {
edges.put(ct, i / 2);
Expand Down
200 changes: 200 additions & 0 deletions src/Certificate/NewCertificate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package Certificate;

import java.util.*;
import java.io.*;

import util.Const;

public class NewCertificate {

// private ArrayList<ArrayList<Edge>> E = new ArrayList<>();
private HashMap<Integer, ArrayList<Edge>> E2 = new HashMap<>();
private ArrayList<HashSet<Integer>> adjacentList = new ArrayList<>();
private Set<Edge> edgeList = new TreeSet<Edge>();
private UFDS uf;
private int maxCount = 0;

public NewCertificate(String dataFileName) throws FileNotFoundException {
Scanner in = new Scanner(new BufferedReader(new FileReader(Const.OUTPUT_DIR + dataFileName)));
int N = in.nextInt();
uf = new UFDS(N);
for (int i = 0; i < N; i++) {
int M = in.nextInt();
adjacentList.add(new HashSet<>());
for(int j = 0; j < M; j++) {
int nbr = in.nextInt();
adjacentList.get(i).add(nbr);
if (i < nbr) {
edgeList.add(new Edge(i, nbr));
}
}
}
}

public NewCertificate(ArrayList<HashSet<Integer>> nbrList) {
uf = new UFDS(nbrList.size());
adjacentList = nbrList;
for (int i = 0; i < nbrList.size(); i++) {
for (int nbr: nbrList.get(i)) {
if (i < nbr) {
edgeList.add(new Edge(i, nbr));
}
}
}
}

public NewCertificate() {}

public Set<Edge> generateCert(int k) throws Exception {
while (uf.size() > 1 && edgeList.size() > k * (uf.size() - 1)) {
System.out.println("number of node: "+uf.size());
clear();
solve(this.adjacentList);
// System.out.println("=====Edge List========");
// edgeList.forEach(ct -> {
// System.out.printf("%d, %d\n", ct.src, ct.des);
// });
// System.out.println("======Certificate=======");
Set<Edge> cert = query(k);
// cert.forEach(ct -> {
// System.out.printf("%d, %d\n", ct.src, ct.des);
// });
// System.out.println("======UFDS=======");

edgeList.removeAll(cert);

// edgeList.forEach(ct -> {
// System.out.printf("%d, %d\n", ct.src, ct.des);
// });

for (Edge e : edgeList) {
uf.union(e.src, e.des);
}

// for(int i = 0; i < 7; i++) {
// System.out.print(uf.find(i));
// }
// System.out.println();
// System.out.println("=============");

edgeList = cert;
}
clear();
return edgeList;
}

/**
* For undirected graph, need (src, des) and (des, src) both in adjList.
*/
public void solve(ArrayList<HashSet<Integer>> adjList) throws Exception {
int M = edgeList.size();
System.out.println("Number of edge:" + M);
int[] r = new int[adjList.size()];
HashMap<Edge, Integer> visited = new HashMap<>();

TreeSet<Node> pq = new TreeSet<Node>();
for (int i = 0; i < adjList.size(); i++) {
pq.add(new Node(i));
}
while (!pq.isEmpty()) {
Node nodex = pq.pollLast();
int x = nodex.node;
Set<Integer> nbrs = adjList.get(x);
for (int y: nbrs) {
int rootx = uf.find(x);
int rooty = uf.find(y);
if (rootx == rooty || visited.containsKey(new Edge(x, y))) {
continue;
}
int cur = r[rooty] + 1;
maxCount = cur > maxCount ? cur : maxCount;
if (!E2.containsKey(cur)) {
E2.put(cur, new ArrayList<>());
}
E2.get(cur).add(new Edge(x, y));
if (E2.get(cur).size() >= uf.size() - 1) {
writeToFile(cur, E2.get(cur));
E2.remove(cur);
}

// if (r[rooty] + 1 >= E.size()) {
// for (int a = E.size(); a <= r[rooty] + 1; a++) {
// E.add(new ArrayList<>());
// }
// }
// E.get(r[rooty] + 1).add(new Edge(x, y));

if (r[x] == r[rooty]) r[x]++;
// pq.remove(new Node(rooty, r[rooty]));
for (int i = 0; i < adjList.size(); i++) {
if (uf.find(i) == rooty) {
pq.remove(new Node(i, r[i]));
r[i]++;
pq.add(new Node(i, r[i]));
}
}
// pq.add(new Node(rooty, r[rooty]));
visited.put(new Edge(x, y), 1);
// visited.put(new Edge(y, x).hashCode(), 1);
}
}
}

private void writeToFile(int id, ArrayList<Edge> edge) throws Exception {
String fileName = Const.TEMPORARY_CERT_DIR + Const.TEMPORARY_CERT_FILE + id;
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(edge);
oos.close();
fos.close();
}

public Set<Edge> query(int k) throws Exception {
// int mink = Math.min(k, E.size() - 1);
// TreeSet<Edge> ans = new TreeSet<Edge>();
// for (int i = 1; i <= mink; i++) {
// ans.addAll(E.get(i));
// }
// return ans;
int mink = Math.min(k, maxCount);
TreeSet<Edge> ans = new TreeSet<Edge>();
for (int i = 1; i <= mink; i++) {
if (E2.containsKey(i)) {
ans.addAll(E2.get(i));
} else {
ans.addAll(readEdges(i));
}
}
return ans;
}

public ArrayList<Edge> readEdges(int id) throws Exception {
String fileName = Const.TEMPORARY_CERT_DIR + Const.TEMPORARY_CERT_FILE + id;
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);

ArrayList<Edge> edges = (ArrayList) ois.readObject();

ois.close();
fis.close();
return edges;
}

public void clear() {
// System.out.println("Start to clear");
maxCount = 0;
deleteAll();
E2 = new HashMap<>();
}

private void deleteAll() {
File folder = new File(Const.TEMPORARY_CERT_DIR);
for (File file: folder.listFiles()) {
if (!file.delete()) {
System.out.println("ERROR\n");
}
}
}
}


8 changes: 0 additions & 8 deletions src/Certificate/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ public static void testActualGraph() {
adjList.get(i).add(a[i][j]);
}
}

Certificate c = new Certificate();
//c.solve(adjList);
Set<Edge> cert = c.query(3);
System.out.println(cert.size());
cert.forEach(ct -> {
System.out.printf("%d, %d\n", ct.src, ct.des);
});
}

public static void testUnionEdge() {
Expand Down
3 changes: 3 additions & 0 deletions src/util/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

public final class Const {
public static final String OUTPUT_DIR = "./data/";

public static final String TEMPORARY_CERT_DIR = "./efiles/";
public static final String TEMPORARY_CERT_FILE = "efile";
}

0 comments on commit e0d9f25

Please sign in to comment.