From b2703a63380116d083c91a18a4018a393afff36f Mon Sep 17 00:00:00 2001 From: Renato Melo <renatinho.smelo@gmail.com> Date: Mon, 21 Mar 2016 09:19:32 -0300 Subject: [PATCH] PrevalentSeed --- src/algoritmos/MinDominatingSet.java | 2 +- ...DominatingSeed.java => PrevalentSeed.java} | 56 +++++++++++++++---- src/grafos/DirectedSocialNetwork.java | 46 ++++++++------- 3 files changed, 70 insertions(+), 34 deletions(-) rename src/algoritmos/{DominatingSeed.java => PrevalentSeed.java} (82%) diff --git a/src/algoritmos/MinDominatingSet.java b/src/algoritmos/MinDominatingSet.java index b217840..c16e9a7 100644 --- a/src/algoritmos/MinDominatingSet.java +++ b/src/algoritmos/MinDominatingSet.java @@ -88,7 +88,7 @@ public class MinDominatingSet { } /** - * Está errado, tem que olhar melhor a questão da direção das arestas + * Com grafo direcionado não retorna um conjunto dominante, mas é um bom cojunto de vértices influentes **/ public HashSet<Actor> fastGreedy(DirectedSocialNetwork grafo) { diff --git a/src/algoritmos/DominatingSeed.java b/src/algoritmos/PrevalentSeed.java similarity index 82% rename from src/algoritmos/DominatingSeed.java rename to src/algoritmos/PrevalentSeed.java index f62ac8b..e333ef9 100644 --- a/src/algoritmos/DominatingSeed.java +++ b/src/algoritmos/PrevalentSeed.java @@ -5,6 +5,10 @@ import java.util.PriorityQueue; import java.util.Random; import java.util.Set; +import util.ComparaPorGrau; + +import com.google.common.collect.MinMaxPriorityQueue; + import algoritmos.MarginalGain; import geradores.SocialNetworkGenerate; import grafos.Actor; @@ -13,33 +17,61 @@ import interfaces.SeedChooser; /*De acordo com os experimentos, utilizando o CELF como subrotina * dentro do conjunto dominante obtem-se um resultado semelhante - * ao CELF original, o que leva a crer sãio fortes indicativos de + * ao CELF original, o que leva a crer são fortes indicativos de * que os vértices "bons" estão dentro do conjunto dominante minimo * num grafo direcionado */ -public class DominatingSeed implements SeedChooser<Actor> { +public class PrevalentSeed implements SeedChooser<Actor> { private DirectedSocialNetwork grafo; - public DominatingSeed(DirectedSocialNetwork g) { + public PrevalentSeed(DirectedSocialNetwork g) { this.grafo = g; } + + public HashSet<Actor> preSelecao(DirectedSocialNetwork grafo) { + int n = grafo.vertexSet().size(); + + // DS <-- {} + HashSet<Actor> candidatos = new HashSet<>(); + + // compare os vertices pelo grau + ComparaPorGrau comp = new ComparaPorGrau(grafo); + + MinMaxPriorityQueue<Actor> heapMinMax = null; + heapMinMax = MinMaxPriorityQueue.orderedBy(comp).maximumSize(n) + .create(); + for (Actor v : grafo.vertexSet()) { + heapMinMax.add(v); + } + + Set<Actor> cobertos = new HashSet<>(); + while (cobertos.size() < n) { + + Actor v = heapMinMax.removeLast(); + Set<Actor> vizinhos = grafo.outNeighborsOf(v); + + if (cobertos.addAll(vizinhos)) { + candidatos.add(v); + } + cobertos.add(v); + } + return candidatos; + } @Override public HashSet<Actor> escolher(int k) { HashSet<Actor> semente = new HashSet<Actor>(); - HashSet<Actor> minSet = new HashSet<Actor>(); + HashSet<Actor> candidatos = new HashSet<Actor>(); - MinDominatingSet ds = new MinDominatingSet(); - minSet = ds.fastGreedy(grafo); + candidatos = preSelecao(grafo); - System.out.println("|DS| = " + minSet.size()); - if (minSet.size() < k) { - System.out.println("Erro: o cojunto domintante é menor que K"); + if (candidatos.size() < k) { + System.out.println("Erro: o cojunto de candidatos é menor que K"); return null; } // create priority queue of all nodes, with marginal gain delta +inf - PriorityQueue<MarginalGain> fila = priorityQueueOfGains(minSet); + PriorityQueue<MarginalGain> fila = priorityQueueOfGains(candidatos); double MaxSpread = 0; @@ -231,7 +263,7 @@ public class DominatingSeed implements SeedChooser<Actor> { 2.5); long startTime = 0; startTime = System.nanoTime(); - HashSet<Actor> seed = new DominatingSeed(g).escolher2(15); + HashSet<Actor> seed = new PrevalentSeed(g).escolher2(15); System.out.println("Tempo: " + (System.nanoTime() - startTime) / 1000); HashSet<Actor> ativos = g.indepCascadeDiffusion(seed); @@ -243,7 +275,7 @@ public class DominatingSeed implements SeedChooser<Actor> { // g.visualize(); startTime = System.nanoTime(); - HashSet<Actor> seed2 = new DominatingSeed(g).escolher(15); + HashSet<Actor> seed2 = new PrevalentSeed(g).escolher(15); System.out.println("Tempo: " + (System.nanoTime() - startTime) / 1000); HashSet<Actor> ativos2 = g.indepCascadeDiffusion(seed2); diff --git a/src/grafos/DirectedSocialNetwork.java b/src/grafos/DirectedSocialNetwork.java index f9d2f3a..fb86913 100644 --- a/src/grafos/DirectedSocialNetwork.java +++ b/src/grafos/DirectedSocialNetwork.java @@ -186,7 +186,7 @@ public class DirectedSocialNetwork extends public double espectedSpread(HashSet<Actor> seed, boolean ic) { double media = 0; int soma = 0; - int repeticoes = 10000; + int repeticoes = 5000; if (!ic) { HashSet<Actor> ativados = linearThresholdDiffusion(seed); @@ -243,6 +243,7 @@ public class DirectedSocialNetwork extends while (escolhidos.size() < k) { Actor maior = heapMinMax.removeLast(); + System.out.print(maior+"; "); escolhidos.add(maior); } @@ -327,29 +328,32 @@ public class DirectedSocialNetwork extends DirectedSocialNetwork g = new DirectedSocialNetwork( DefaultWeightedEdge.class); - g = new SocialNetworkGenerate().gerarGrafo(40, 2); - - HashSet<Actor> seed = new HashSet<>(); - - Set<Actor> vertices = g.vertexSet(); - for (Actor actor : vertices) { - if (seed.size() < 5) { - seed.add(actor); - } - - } - + g = new SocialNetworkGenerate().gerarGrafo(30, 2); + +// HashSet<Actor> seed = new HashSet<>(); +// +// Set<Actor> vertices = g.vertexSet(); +// for (Actor actor : vertices) { +// if (seed.size() < 5) { +// seed.add(actor); +// } +// +// } +// System.out.println("|V(G)| = " + g.vertexSet().size()); System.out.println("|E(G)| = " + g.edgeSet().size()); - System.out.println("Seed:"); - for (Actor a : seed) { - System.out.println(a.toString()); - } - - System.out.println("Difusão média no modelo IC = "+g.espectedSpread(seed, true)); - System.out.println("Difusão média no modelo LT = "+g.espectedSpread(seed, false)); + System.out.println("Q: "); + Set<Actor> Q = g.verticesGrauMaior(g.vertexSet(), g.vertexSet().size()); + System.out.println(); +// System.out.println("Seed:"); +// for (Actor a : seed) { +// System.out.println(a.toString()); +// } +// +// System.out.println("Difusão média no modelo IC = "+g.espectedSpread(seed, true)); +// System.out.println("Difusão média no modelo LT = "+g.espectedSpread(seed, false)); // g.activate(seed); -// g.visualize(); + g.visualize(); } } -- GitLab