Exemple #1
0
        //Given seed probability P, find the best k nodes that can maximize influence spread
        public Tuple <List <int>, double> Greedy(int k, List <double> P)
        {
            HashSet <int> seeds   = new HashSet <int> ();
            List <int>    seedSet = new List <int> ();
            List <double> edgeW   = new List <double> ();

            for (int h = 0; h < numS; ++h)
            {
                edgeW.Add(1.0);
            }


            //CELF Algorithm
            PriorityQueue <VNode> pq     = new PriorityQueue <VNode> (numV + 1, new VNodeComparer());
            List <bool>           update = new List <bool> ();

            for (int u = 0; u < numV; ++u)
            {
                VNode node = new VNode(u, numS);
                pq.Push(node);
                update.Add(false);
            }

            double total_gain = 0.0;

            for (int i = 0; i < k; ++i)
            {
                for (int u = 0; u < numV; ++u)
                {
                    update [u] = false;
                }
                int    next = 0;
                double gain = 0;
                while (true)
                {
                    VNode node  = pq.Pop();
                    int   max_u = node.id;

                    if (update [max_u])
                    {
                        next = max_u;
                        gain = node.val;
                        break;
                    }
                    else
                    {
                        double sum = 0;
                        if (i == 0)
                        {
                            sum = V2S[max_u].Count * P[max_u];
                        }
                        else
                        {
                            foreach (int sid in V2S[max_u])
                            {
                                sum += edgeW[sid] * P[max_u];
                            }
                        }
                        VNode n1 = new VNode(max_u, sum);
                        pq.Push(n1);
                        update [max_u] = true;
                    }
                }
                total_gain += gain;
                foreach (int sid in V2S[next])
                {
                    edgeW [sid] = edgeW [sid] * (1 - P [next]);
                }
                seeds.Add(next);
                seedSet.Add(next);
            }

            return(new Tuple <List <int>, double> (seedSet, total_gain * numV / numS));
        }