void TGreedyAlg::runGreedyAlgorithm() {
    outputGraph = TKColourNet::New();
    
    for (THash<TInt, TNodeInfo>::TIter NI = nodeNmH.BegI(); NI < nodeNmH.EndI(); NI++) {
        outputGraph->AddNode(NI.GetKey(), TKColourNode());
        printf("Added node %d to output graph\n", (int) NI.GetKey());
    }
    
    // for each node i
    for (THash<TInt, TNodeInfo>::TIter NI = nodeNmH.BegI(); NI < nodeNmH.EndI(); NI++) {
        int nodeI = NI.GetKey();
        printf("*****     Considering node i: %d     *****\n", nodeI);
        
        // initialise unaccounted cascades U
        TVec<TCascade> cascades = cascadeV;
        
        // initialise parental neighbourhood
        TIntV parentalNeighbourhood;
        
        bool uselessUnaccountedCasacdesLeft = false;
        while ((cascades.Len() != 0) && (!uselessUnaccountedCasacdesLeft)) {
//            printf("%d Casacdes left...\n", cascades.Len());
            
            // find node j that could possible have infected node i that has been observed for largest number of cascades
            int argmax = -1;
            int maxNoCascades = 0;
            
            bool areUnaccountedCascadesUseless = true;
            for (THash<TInt, TNodeInfo>::TIter LNI = nodeNmH.BegI(); LNI < nodeNmH.EndI(); LNI++) {
                int nodeJ = LNI.GetKey();
//                printf("nodeJ: %d\n", nodeJ);
                int countPotentialNoCascades = 0;
                if (nodeI != nodeJ) {
                    for (int c = 0; c < cascades.Len(); c++) {
                        TCascade cascade = cascades[c];
                        if (cascade.IsNode(nodeJ) && cascade.IsNode(nodeI)) {
//                            if (cascade.GetTm(nodeI) > cascade.GetTm(nodeJ)) {
                            if (cascade.GetTm(nodeJ) == (cascade.GetTm(nodeI) - 1)) {
                                countPotentialNoCascades++;
                            }
                        }
                    }
                    if (countPotentialNoCascades > maxNoCascades) {
                        maxNoCascades = countPotentialNoCascades;
                        argmax = nodeJ;
                        areUnaccountedCascadesUseless = false;
                    }
                    else {
                        areUnaccountedCascadesUseless = areUnaccountedCascadesUseless && true;
                    }
                }
            }
            
            if (areUnaccountedCascadesUseless) {
                uselessUnaccountedCasacdesLeft = true;
            }
            
            if (argmax != -1) {
//                printf("argmax (k) = %d, noCasacdesAppearedIn = %d\n", argmax, maxNoCascades);
                
                // add arg max (k) to the set of parental neighbours
                parentalNeighbourhood.Add(argmax);
            }
            
            // remove the cascades which k belongs to
            TIntV cascadesToRemove;
            for (int c = 0; c < cascades.Len(); c++) {
                TCascade cascade = cascades[c];
                if (cascade.IsNode(argmax) && cascade.IsNode(nodeI)) {
                    if (cascade.GetTm(nodeI) > cascade.GetTm(argmax)) {
                        cascadesToRemove.Add(c);
                    }
                }
            }
            cascadesToRemove.Sort();
            
//            printf("cascadesToRemove: ");
            for (int i = 0; i < cascadesToRemove.Len(); i++) {
//                printf("%d, ", (int) cascadesToRemove[i]);
                cascades.Del(cascadesToRemove[i]-i);
            }
//            printf("\n");
        }
        
        // add edges from node i to each of parent
        for (int i = 0; i < parentalNeighbourhood.Len(); i++) {
            int srcNodeId = parentalNeighbourhood[i];
            int dstNodeId = nodeI;
            outputGraph->AddEdge(srcNodeId, dstNodeId);
//            printf("#####     Added Edge: %d -> %d     #####\n", srcNodeId, dstNodeId);
        }
    }
}
Beispiel #2
0
void TNetInfBs::GenNoisyCascade(TCascade& C, const int& TModel, const double &window, TIntPrIntH& EdgesUsed,
					 	 	    const double& std_waiting_time, const double& std_beta,
					 	 	    const double& PercRndNodes, const double& PercRndRemoval) {
	TIntPrIntH EdgesUsedC; // list of used edges for a single cascade
	GenCascade(C, TModel, window, EdgesUsedC, delta, std_waiting_time, std_beta);

	// store keys
	TIntV KeyV;
	C.NIdHitH.GetKeyV(KeyV);

	// store first and last time
	double tbeg = TFlt::Mx, tend = TFlt::Mn;
	for (int i=0; i < KeyV.Len(); i++) {
		if (tbeg > C.NIdHitH.GetDat(KeyV[i]).Tm) tbeg = C.NIdHitH.GetDat(KeyV[i]).Tm;
		if (tend < C.NIdHitH.GetDat(KeyV[i]).Tm) tend = C.NIdHitH.GetDat(KeyV[i]).Tm;
	}

	// remove PercRndRemoval% of the nodes of the cascades
	if (PercRndRemoval > 0) {
		for (int i=KeyV.Len()-1; i >= 0; i--) {
			if (TFlt::Rnd.GetUniDev() < PercRndRemoval) {
				// remove from the EdgesUsedC the ones affected by the removal
				TIntPrV EdgesToRemove;
				for (TIntPrIntH::TIter EI = EdgesUsedC.BegI(); EI < EdgesUsedC.EndI(); EI++) {
					if ( (KeyV[i]==EI.GetKey().Val1 && C.IsNode(EI.GetKey().Val2) && C.GetTm(KeyV[i]) < C.GetTm(EI.GetKey().Val2)) ||
							(KeyV[i]==EI.GetKey().Val2 && C.IsNode(EI.GetKey().Val1) && C.GetTm(KeyV[i]) > C.GetTm(EI.GetKey().Val1)) ) {
						EI.GetDat() = EI.GetDat()-1;

						if (EI.GetDat()==0)
							EdgesToRemove.Add(EI.GetKey());
					}
				}

				for (int er=0; er<EdgesToRemove.Len(); er++)
					EdgesUsedC.DelKey(EdgesToRemove[er]);

				C.Del(KeyV[i]);
			}
		}

		// defrag the hash table, otherwise other functions can crash
		C.NIdHitH.Defrag();
	}

	// Substitute PercRndNodes% of the nodes for a random node at a random time
	if (PercRndNodes > 0) {
		for (int i=KeyV.Len()-1; i >= 0; i--) {
			if (TFlt::Rnd.GetUniDev() < PercRndNodes) {
				// remove from the EdgesUsedC the ones affected by the change
				TIntPrV EdgesToRemove;
				for (TIntPrIntH::TIter EI = EdgesUsedC.BegI(); EI < EdgesUsedC.EndI(); EI++) {
					if ( (KeyV[i]==EI.GetKey().Val1 && C.IsNode(EI.GetKey().Val2) && C.GetTm(KeyV[i]) < C.GetTm(EI.GetKey().Val2)) ||
							(KeyV[i]==EI.GetKey().Val2 && C.IsNode(EI.GetKey().Val1) && C.GetTm(KeyV[i]) > C.GetTm(EI.GetKey().Val1)) ) {
						EI.GetDat() = EI.GetDat()-1;

						if (EI.GetDat()==0)
							EdgesToRemove.Add(EI.GetKey());
					}
				}

				for (int er=0; er<EdgesToRemove.Len(); er++)
					EdgesUsedC.DelKey(EdgesToRemove[er]);

				printf("Old node n:%d t:%f --", KeyV[i].Val, C.GetTm(KeyV[i]));
				C.Del(KeyV[i]);

				// not repeating a label
				double tnew = 0;
				int keynew = -1;
				do {
					tnew = tbeg + TFlt::Rnd.GetUniDev()*(tend-tbeg);
					keynew = Graph->GetRndNId();
				} while (KeyV.IsIn(keynew));

				printf("New node n:%d t:%f\n", keynew, tnew);

				C.Add(keynew, tnew);
				KeyV.Add(keynew);
			}
		}
	}

	// add to the aggregate list (EdgesUsed)
	EdgesUsedC.Defrag();

	for (int i=0; i<EdgesUsedC.Len(); i++) {
		if (!EdgesUsed.IsKey(EdgesUsedC.GetKey(i))) EdgesUsed.AddDat(EdgesUsedC.GetKey(i)) = 0;

		EdgesUsed.GetDat(EdgesUsedC.GetKey(i)) += 1;
	}
}