/* This function reads in a discription of a graph and loads it into
 * a graph discription structure.  The file format is asci were the
 * first token is the number of vericies followed by verticy labels
 * and guess x, y coordinates.  This is followed by the number of edges
 * and the indexes of the vericies that they connect.
 */
GraphDiscription readGraphDiscription(const char* filename){
    FILE* file;
    int i;
    GraphDiscription gd = ALLOCATE(graph_discription,1);
    DEBUG_CHECK(gd,"Error allocating memory");

    file = fopen(filename,"r");
    DEBUG_CHECK_1ARG(file,"Error opening file: %s", filename);

    /* Read the number of vericies in the file */
    DEBUG_CHECK_1ARG(fscanf(file,"%d",&(gd->numVert)) == 1, "Error: could not determin the number of verticies in file %s", filename);

    /* allocate space for vericies and load discriptions */
    gd->verts = ALLOCATE(Vert,gd->numVert);
    gd->vertLabels = ALLOCATE(char*,gd->numVert);
    gd->bunch = ALLOCATE(JetBunch,gd->numVert);
    for(i = 0; i < gd->numVert; i++){
        gd->vertLabels[i] = ALLOCATE(char,LABELMAX);
        DEBUG_CHECK_2ARG(fscanf(file, "%s %lf %lf",  gd->vertLabels[i], &(gd->verts[i].x), &(gd->verts[i].y)) == 3,
                         "Error Parsing vertex %d in file: %s", i, filename);
        gd->bunch[i] = makeJetBunch();

    }

    /* Read the number of edges in the file */
    DEBUG_CHECK_1ARG(fscanf(file,"%d",&(gd->numEdge)) == 1, "Error: could not determin the number of edges in file %s", filename);

    /* allocate space for vericies and load discriptions */
    gd->edges = ALLOCATE(Edge,gd->numEdge);
    for(i = 0; i < gd->numEdge; i++){
        DEBUG_CHECK_2ARG(fscanf(file, "%d %d",&(gd->edges[i].vert1), &(gd->edges[i].vert2)) == 2,
                         "Error Parsing vertex %d in file: %s", i, filename);
    }
    fclose(file);

    return gd;
}
示例#2
0
GraphDiscription buildBunchGraph(GraphDiscription modelJets, JetSimilarityMeasure jetSim, int bunchsize){
    int vert;

    assert(bunchsize > 1);
    for(vert = 0; vert < modelJets->numVert; vert++){
        Matrix dist;
        int size, i, j;
        FTYPE maxsim, minsim, smallest;
        int minjet;
        JetBunch finalBunch = makeJetBunch();

        MESSAGE1ARG("Selecting jets for point: %s",modelJets->vertLabels[vert]);

        size = modelJets->bunch[vert]->size;
        assert(bunchsize <= size);
        assert(size > 0);

        dist = makeZeroMatrix(size, size);

        smallest = maxsim = jetSim(modelJets->bunch[vert]->jets[0],modelJets->bunch[vert]->jets[0]);
        for(i = 0; i < size; i++){
            for(j = 0; j < size; j++){
                ME(dist,i,j) = jetSim(modelJets->bunch[vert]->jets[i],modelJets->bunch[vert]->jets[j]);
                maxsim = MAX( maxsim, ME(dist,i,j) );
                smallest = MIN(smallest, ME(dist,i,j));
            }
        }

        /* find the minimum similarity */
        minsim = 0.0;
        minjet = -1;
        for(i = 0; i < size; i++){
            for(j = 0; j < size; j++){
                if(minjet < 0 || minsim > ME(dist,i,j) ){
                    minsim = ME(dist,i,j);
                    minjet = i;
                }
            }
        }

        while(1){
            /* MESSAGE2ARG("    Adding jet %03d to final bunch. sim=%+f",minjet,minsim); */
            /* add that jet to the final bunch */
            addJetToBunch(finalBunch, modelJets->bunch[vert]->jets[minjet]);
            /* eleminate the newly added jet from the old bunch and the distance matrix */
            modelJets->bunch[vert]->jets[minjet] = NULL;
            for( i = 0; i < size; i++){
                /* Make sure new value is greater that the maximum similarity */
                ME(dist,i,minjet) = 2*ABS(maxsim);
            }

            if(finalBunch->size >= bunchsize) break;

            minsim = 0.0;
            minjet = -1;
            for(j = 0; j < size; j++){
                FTYPE locmax = smallest;
                for(i = 0; i < size; i++){
                    /* compute the best similarity to the jets in the data set */
                    if(modelJets->bunch[vert]->jets[i] == NULL){
                        locmax = MAX(locmax,ME(dist,i,j));
                    }
                }
                if(minjet < 0 || minsim > locmax ){
                    minsim = locmax;
                    minjet = j;
                }
            }
        }

        freeJetBunch(modelJets->bunch[vert]);
        modelJets->bunch[vert] = finalBunch;
        freeMatrix(dist);
    }
    return modelJets;
}