diff --git a/src/algoritmos/DominatingSeed.java b/src/algoritmos/DominatingSeed.java index bc4d4810ffec78ca1dd519d770a37042882b4243..f62ac8b089049f200f4adcfd3b84ce3ea3bced31 100644 --- a/src/algoritmos/DominatingSeed.java +++ b/src/algoritmos/DominatingSeed.java @@ -31,6 +31,7 @@ public class DominatingSeed implements SeedChooser<Actor> { MinDominatingSet ds = new MinDominatingSet(); minSet = ds.fastGreedy(grafo); + System.out.println("|DS| = " + minSet.size()); if (minSet.size() < k) { System.out.println("Erro: o cojunto domintante é menor que K"); @@ -66,7 +67,50 @@ public class DominatingSeed implements SeedChooser<Actor> { return semente; } - + + public HashSet<Actor> escolher2(int k) { + HashSet<Actor> semente = new HashSet<Actor>(); + HashSet<Actor> minSet = new HashSet<Actor>(); + + MinDominatingSet ds = new MinDominatingSet(); + minSet = ds.fastGreedy2(grafo); + + System.out.println("|DS| = " + minSet.size()); + if (minSet.size() < k) { + System.out.println("Erro: o cojunto domintante é menor que K"); + return null; + } + + // create priority queue of all nodes, with marginal gain delta +inf + PriorityQueue<MarginalGain> fila = priorityQueueOfGains(minSet); + + double MaxSpread = 0; + + while (semente.size() < k) { + // set all gains invalid + for (MarginalGain mg : fila) { + mg.setValid(false); + } + + while (true) { + MarginalGain max = fila.poll(); + if (max.isValid() == true) { + semente.add(max.getVertice()); + MaxSpread = MaxSpread + max.getGain(); + break; + } else { + double sigma = cascata(max.getVertice(), semente); + double delta = sigma - MaxSpread; + max.setGain(delta); + max.setValid(true); + fila.add(max); + } + } + } + + return semente; + } + public HashSet<Actor> escolherGreedy(int k) { HashSet<Actor> semente = new HashSet<Actor>(); HashSet<Actor> minSet = new HashSet<Actor>(); @@ -120,7 +164,7 @@ public class DominatingSeed implements SeedChooser<Actor> { vertices[i++] = a; Random rand = new Random(); - while (rSet.size() < vSet.size()/4) { + while (rSet.size() < vSet.size() / 3) { rSet.add(vertices[rand.nextInt(vertices.length)]); } @@ -183,31 +227,36 @@ public class DominatingSeed implements SeedChooser<Actor> { } public static void main(String[] args) { - DirectedSocialNetwork g = new SocialNetworkGenerate() - .gerarGrafo(800, 2.5); - HashSet<Actor> seed = new DominatingSeed(g).escolherGreedy(15); - - HashSet<Actor> ativos = g.indepCascadeDiffusion(seed); + DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(2000, + 2.5); + long startTime = 0; + startTime = System.nanoTime(); + HashSet<Actor> seed = new DominatingSeed(g).escolher2(15); + System.out.println("Tempo: " + (System.nanoTime() - startTime) / 1000); + + HashSet<Actor> ativos = g.indepCascadeDiffusion(seed); System.out.println("|V(G)| = " + g.vertexSet().size()); - System.out.println("|S| = " + seed.size()); - System.out.println("|A| = "+ativos.size()); - g.deactivate(ativos); -// g.activate(seed); -// g.visualize(); - - HashSet<Actor> seed2 = new DominatingSeed(g).escolher(15); - - HashSet<Actor> ativos2 = g.indepCascadeDiffusion(seed2); - System.out.println("\n|S| = " + seed2.size()); - System.out.println("|A| = "+ativos2.size()); - g.deactivate(ativos2); - - HashSet<Actor> seed3 = new DominatingSeed(g).escolherRandom(15); - - HashSet<Actor> ativos3 = g.indepCascadeDiffusion(seed3); - System.out.println("\n|S| = " + seed3.size()); - System.out.println("|A| = "+ativos3.size()); - + // System.out.println("|S| = " + seed.size()); + System.out.println("|A| = " + ativos.size()); + g.deactivate(ativos); + // g.activate(seed); + // g.visualize(); + + startTime = System.nanoTime(); + HashSet<Actor> seed2 = new DominatingSeed(g).escolher(15); + System.out.println("Tempo: " + (System.nanoTime() - startTime) / 1000); + + HashSet<Actor> ativos2 = g.indepCascadeDiffusion(seed2); +// System.out.println("\n|S| = " + seed2.size()); + System.out.println("|A| = " + ativos2.size()); + g.deactivate(ativos2); + +// HashSet<Actor> seed3 = new DominatingSeed(g).escolherRandom(15); +// +// HashSet<Actor> ativos3 = g.indepCascadeDiffusion(seed3); +//// System.out.println("\n|S| = " + seed3.size()); +// System.out.println("|A| = "+ativos3.size()); + } } diff --git a/src/algoritmos/MinDominatingSet.java b/src/algoritmos/MinDominatingSet.java index 42d942df1b9aebc1157af2d27372f1739adb2610..b217840828b6e0f9a4c8a5284c859d5d031b6b3c 100644 --- a/src/algoritmos/MinDominatingSet.java +++ b/src/algoritmos/MinDominatingSet.java @@ -108,16 +108,45 @@ public class MinDominatingSet { } Set<Actor> dominados = new HashSet<>(); - while (dominados.size() < n) { + Actor v = heapMinMax.removeLast(); - dominados.add(v); Set<Actor> vizinhos = grafo.outNeighborsOf(v); if (dominados.addAll(vizinhos)) { dominante.add(v); } + dominados.add(v); + } + return dominante; + } + + public HashSet<Actor> fastGreedy2(DirectedSocialNetwork grafo) { + int n = grafo.vertexSet().size(); + + // DS <-- {} + HashSet<Actor> dominante = new HashSet<>(); + + // compare os vertices pelo grau + ComparaPorGrau comp = new ComparaPorGrau(grafo); + + MinMaxPriorityQueue<Actor> fila = null; + fila = MinMaxPriorityQueue.orderedBy(comp).maximumSize(n) + .create(); + for (Actor v : grafo.vertexSet()) { + fila.add(v); + } + + Set<Actor> dominados = new HashSet<>(); + while (!fila.isEmpty()) { + Actor v = fila.removeLast(); + Set<Actor> vizinhos = grafo.outNeighborsOf(v); + + if (dominados.addAll(vizinhos)) { + dominante.add(v); + }else + dominados.add(v); } return dominante; } @@ -125,7 +154,7 @@ public class MinDominatingSet { public static void main(String[] args) { DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo( - 100, 2.5); + 5000, 2.5); System.out.println("|V(G)| = " + g.vertexSet().size()); System.out.println("|E(G)| = " + g.edgeSet().size()); @@ -139,10 +168,10 @@ public class MinDominatingSet { startTime = System.nanoTime(); HashSet<Actor> ds = (HashSet<Actor>) minDom.greedy(g); System.out.println("Tempo: "+(System.nanoTime() - startTime)/1000); - System.out.println("|DS|- GreedyPlus: " + ds.size()); + System.out.println("|DS|- vRegularGreedy: " + ds.size()); System.out.println("\nspread: "+g.espectedSpread(ds, true)); - g.activate(ds); - g.visualize(); +// g.activate(ds); +// g.visualize(); startTime = System.nanoTime(); HashSet<Actor> ds2 = (HashSet<Actor>) minDom.regularGreedy(g); @@ -159,13 +188,12 @@ public class MinDominatingSet { HashSet<Actor> ds3 = minDom.fastGreedy(g); // System.out.println("Tempo: "+(System.nanoTime() - startTime)/1000); System.out.println("\n\n|DS|- FastGreedy: " + ds3.size()); - System.out.println("Vertices dominantes:"); // for (Actor v : ds3) { // System.out.print(v.toString()+" "); // } System.out.println("\nspread: "+g.espectedSpread(ds3, true)); - g.activate(ds3); - g.visualize(); +// g.activate(ds3); +// g.visualize(); // HashSet<Actor> seed = new LazyGreedy(g).escolher(ds3.size()); // System.out.println("\nVertices Influentes: "+seed.size()); diff --git a/src/geradores/GeradorGrafoPowerLaw.java b/src/geradores/GeradorGrafoPowerLaw.java index 969d7704e9b4a78561f09aa0ae4bea769f86c815..c7668d1cd14a4b9a4f37454ecf12e0a207b141a8 100644 --- a/src/geradores/GeradorGrafoPowerLaw.java +++ b/src/geradores/GeradorGrafoPowerLaw.java @@ -43,7 +43,7 @@ public class GeradorGrafoPowerLaw { boolean direcionado) { //Devido a futura remoção dos vertices isolados if (direcionado) { - n = (int) (n + n * 0.17); + n = (int) (n + n * 0.7); }else n = (int) (n + n * 0.5); @@ -135,7 +135,7 @@ public class GeradorGrafoPowerLaw { int n, double beta, boolean direcionado) { if (direcionado) { - n = (int) (n + n * 0.17); + n = (int) (n + n * 0.65); }else n = (int) (n + n * 0.5); diff --git a/src/geradores/GeradorModeloGRG.java b/src/geradores/GeradorModeloGRG.java index 2c847ee138c32bcb62350eb7ea3046c095e83fce..6d2f57629c9a3593c1c871d684e02dd570919773 100644 --- a/src/geradores/GeradorModeloGRG.java +++ b/src/geradores/GeradorModeloGRG.java @@ -122,7 +122,7 @@ public class GeradorModeloGRG { if (Math.random() < p){ DefaultWeightedEdge e; e = g.addEdge(vertices.get(i), vertices.get(j)); - g.setEdgeWeight(e, Math.random()); + g.setEdgeWeight(e, peso()); } } } @@ -135,7 +135,7 @@ public class GeradorModeloGRG { if (Math.random() < p){ DefaultWeightedEdge e; e = g.addEdge(vertices.get(i), vertices.get(j)); - g.setEdgeWeight(e, Math.random()); + g.setEdgeWeight(e, peso()); } } } @@ -143,4 +143,16 @@ public class GeradorModeloGRG { } return g; } + + public double peso() { + + // int p = (int) (Math.random() * 3); + // double[] choice = { 0.2, 0.04, 0.008 }; + // return choice[p]; + + // uniform IC model +// return (double)10/100; +// return 0.025; + return (Math.random())/4; + } } diff --git a/src/geradores/SocialNetworkGenerate.java b/src/geradores/SocialNetworkGenerate.java index effd8c09331162ca494104627f4404287ba1d842..0f0d715dd6d78a078043e38253f5c94501e0baa8 100644 --- a/src/geradores/SocialNetworkGenerate.java +++ b/src/geradores/SocialNetworkGenerate.java @@ -20,6 +20,7 @@ import org.jgrapht.WeightedGraph; import org.jgrapht.graph.DefaultEdge; import org.jgrapht.graph.DefaultWeightedEdge; +import plot.Histograma; import util.ComponentesConexos; public class SocialNetworkGenerate { @@ -65,6 +66,18 @@ public class SocialNetworkGenerate { return g; } + + public DirectedSocialNetwork gerarGrafoInteiro(int n, double beta, + boolean direcionado, boolean comPeso) { + + WeightedGraph<Actor, DefaultWeightedEdge> grafo; + grafo = new GeradorGrafoPowerLaw().gerarComPesos(n, beta, + direcionado); + + g = asDirectedSocialNetwork(grafo); + + return g; + } // Por padrão a rede resultante é direcionada e com pesos public DirectedSocialNetwork gerarModeloIC(int n, double beta) { @@ -114,5 +127,17 @@ public class SocialNetworkGenerate { return sn; } - + + public static void main(String[] args) { +// DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafo(50000, 2.5); + DirectedSocialNetwork g = new SocialNetworkGenerate().gerarGrafoInteiro(30000, 2.5, true, true); + System.out.println("|V(G)|"+g.vertexSet().size()); + System.out.println("|E(G)|"+g.edgeSet().size()); + Histograma histograma = new Histograma(); + try { + histograma.plotarGraficos(histograma.gerarHistograma(g)); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/readgraph/GraphReader.java b/src/readgraph/GraphReader.java index a53d2ef484fe48d6cca05be6eb7489bcea15ee97..94f5531302fc91e50b62951957a44ddbc5f74fb3 100644 --- a/src/readgraph/GraphReader.java +++ b/src/readgraph/GraphReader.java @@ -63,7 +63,7 @@ public class GraphReader { DefaultWeightedEdge e; e = g.addEdge(vertices[v1], vertices[v2]); if (e != null) { - g.setEdgeWeight(e, trivalencyModel()); + g.setEdgeWeight(e, peso()); } } } @@ -79,18 +79,17 @@ public class GraphReader { /** * Sorteia um valor aleatório - * - * @return um número entre {0.2, 0.04, 0.008} */ - public double trivalencyModel() { + public double peso() { // int p = (int) (Math.random() * 3); // double[] choice = { 0.2, 0.04, 0.008 }; // return choice[p]; // uniform IC model - return (double)10/100; -// return Math.random(); +// return (double)10/100; +// return 0.025; + return Math.random()/4; } public DirectedSocialNetwork readEpinions() { diff --git a/src/simulacao/Simulacao.java b/src/simulacao/Simulacao.java index ffed98f1fd8ae526286274aad68b0c9f18fccf37..f21f4db1912f124037618fcd61ac92a98986dbd1 100644 --- a/src/simulacao/Simulacao.java +++ b/src/simulacao/Simulacao.java @@ -4,8 +4,11 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; + import javax.media.j3d.PhysicalBody; +import org.jgrapht.graph.DefaultWeightedEdge; + import plot.MeuPlot; import readgraph.GraphReader; import algoritmos.ArticulationSeed; @@ -75,16 +78,102 @@ public class Simulacao { outDir); } - private static void simularArtificial() { - DirectedSocialNetwork g; - g = new SocialNetworkGenerate().gerarGrafo(30000, 2.8); - System.out.println("|V(G)| = " + g.vertexSet().size()); - System.out.println("|E(G)| = " + g.edgeSet().size()); - - String outDir = "'plots/tex/"; - - new Simulacao().simularIC(g, outDir); + public static void simularRandomGraphIC(int n, String outDir) { + double[] tamSemente = { 10, 20, 30, 40, 50 }; + double[] sigma1 = new double[5], tempo1 = new double[5]; + double[] sigma2 = new double[5], tempo2 = new double[5]; + double[] sigma3 = new double[5], tempo3 = new double[5]; + double[] sigma4 = new double[5], tempo4 = new double[5]; + int k, repeticoes = 10; + + double[] ssigma1 = new double[5], stempo1 = new double[5]; + double[] ssigma2 = new double[5], stempo2 = new double[5]; + double[] ssigma3 = new double[5], stempo3 = new double[5]; + double[] ssigma4 = new double[5], stempo4 = new double[5]; + + for (int j = 0; j < repeticoes; j++) { + System.out.println("Grafo "+j); + DirectedSocialNetwork g; + g = new SocialNetworkGenerate().gerarGrafo(n * 1000, 2.5); + System.out.println("|V(G)| = " + g.vertexSet().size()); + System.out.println("|E(G)| = " + g.edgeSet().size()); + System.out.println(); + + long excutionTime = 0; + for (int i = 0; i < tamSemente.length; i++) { + k = (int) tamSemente[i]; + System.out.println("\nTestando para k = " + k); + + excutionTime = System.currentTimeMillis() * -1; + HashSet<Actor> seed1 = new DominatingSeed(g).escolher(k); + excutionTime += System.currentTimeMillis(); + tempo1[i] = (excutionTime / 1000.0f); + sigma1[i] = g.espectedSpread(seed1, true); + System.out.println("DominatingSeed: sigma = " + sigma1[i] + + ", tempo = " + tempo1[i] + " segundos"); + ssigma1[i] += sigma1[i]; + stempo1[i] += tempo1[i]; + + /*excutionTime = System.currentTimeMillis() * -1; + HashSet<Actor> seed2 = new LazyGreedy(g).escolher(k); + excutionTime += System.currentTimeMillis(); + tempo2[i] = (excutionTime / 1000.0f); + sigma2[i] = g.espectedSpread(seed2, true); + System.out.println("LazyGreedy: sigma = " + sigma2[i] + + ", tempo = " + tempo2[i] + " segundos"); + ssigma2[i] += sigma2[i]; + stempo2[i] += tempo2[i];*/ + + excutionTime = System.currentTimeMillis() * -1; + HashSet<Actor> seed2 = new DominatingSeed(g).escolher2(k); + excutionTime += System.currentTimeMillis(); + tempo2[i] = (excutionTime / 1000.0f); + sigma2[i] = g.espectedSpread(seed2, true); + System.out.println("preSelect: sigma = " + sigma2[i] + + ", tempo = " + tempo2[i] + " segundos"); + ssigma2[i] += sigma2[i]; + stempo2[i] += tempo2[i]; + + excutionTime = System.currentTimeMillis() * -1; + HashSet<Actor> seed3 = new HightDegree(g).escolher(k); + excutionTime += System.currentTimeMillis(); + tempo3[i] = (excutionTime / 1000.0f); + sigma3[i] = g.espectedSpread(seed3, true); + System.out.println("HighDegree: sigma = " + sigma3[i] + + ", tempo = " + tempo3[i] + " segundos"); + ssigma3[i] += sigma3[i]; + stempo3[i] += tempo3[i]; + + excutionTime = System.currentTimeMillis() * -1; + HashSet<Actor> seed4 = new RandomSeed(g).escolher(k); + excutionTime += System.currentTimeMillis(); + tempo4[i] = (excutionTime / 1000.0f); + sigma4[i] = g.espectedSpread(seed4, true); + System.out.println("RandomSeed: sigma = " + sigma4[i] + + ", tempo = " + tempo4[i] + " segundos"); + ssigma4[i] += sigma4[i]; + stempo4[i] += tempo4[i]; + + } + } + // TODO tirar a média das 10 repetiçoes e plotar a media + for (int i = 0; i < tamSemente.length; i++) { + sigma1[i] = ssigma1[i] / repeticoes; + sigma2[i] = ssigma2[i] / repeticoes; + sigma3[i] = ssigma3[i] / repeticoes; + sigma4[i] = ssigma4[i] / repeticoes; + + tempo1[i] = stempo1[i] / repeticoes; + tempo2[i] = stempo2[i] / repeticoes; + tempo3[i] = stempo3[i] / repeticoes; + tempo4[i] = stempo4[i] / repeticoes; + } + MeuPlot meuplot = new MeuPlot(); + meuplot.plotPropagacao(tamSemente, sigma1, sigma2, sigma3, sigma4, + outDir); + meuplot.plotTempoExecucao(tamSemente, tempo1, tempo2, tempo3, tempo4, + outDir); } private static void simularPhy() { @@ -124,17 +213,39 @@ public class Simulacao { new Simulacao().simularIC(g, outDir); } + private static void simularDblp() { + System.out.println("dblp.txt"); + DirectedSocialNetwork g; + g = new GraphReader().readDblp(); + System.out.println("|V(G)| = " + g.vertexSet().size()); + System.out.println("|E(G)| = " + g.edgeSet().size()); + + String outDir = "'plots/dblp/"; + + new Simulacao().simularIC(g, outDir); + + } + + private static void simularArtificial() { + int i = 2; + + while (i <= 4) { + String outDir = "'plots/rand/"+i+"/"; + simularRandomGraphIC(i, outDir); + System.out.println("\t----------"); + i = i * 2; + } + } + public static void main(String[] args) { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date data = new Date(); System.out.println("Data de início: " + dateFormat.format(data)); -// simularArtificial(); -// System.out.println("Data de término: " + dateFormat.format(data)); - simularPhy(); - System.out.println("Data de término: " + dateFormat.format(data)); - simularEpinions(); - System.out.println("Data de término: " + dateFormat.format(data)); - simularHep(); - System.out.println("Data de término: " + dateFormat.format(data)); + simularArtificial(); + simularPhy(); +// simularEpinions(); + simularHep(); +// simularDblp(); } + }