Example #1
0
Graph *ReadGraph(char *filename, LabelList *labelList, BOOLEAN directed)
{
   Graph *graph;
   FILE *graphFile;
   ULONG lineNo;             // Line number counter for graph file
   char token[TOKEN_LEN];
   ULONG vertexListSize = 0; // Size of currently-allocated vertex array
   ULONG edgeListSize = 0;   // Size of currently-allocated edge array
   ULONG vertexOffset = 0;   // Dummy argument to ReadVertex and ReadEdge

   // Allocate graph
   graph = AllocateGraph(0,0);

   // Open graph file
   graphFile = fopen(filename,"r");
   if (graphFile == NULL) 
   {
      fprintf(stderr, "Unable to open graph file %s.\n", filename);
      exit(1);
   }

   // Parse graph file
   lineNo = 1;
   while (ReadToken(token, graphFile, &lineNo) != 0) 
   {
      if (strcmp(token, "v") == 0)         // read vertex
         ReadVertex(graph, graphFile, labelList, &vertexListSize, &lineNo,
                    vertexOffset);

      else if (strcmp(token, "e") == 0)    // read 'e' edge
         ReadEdge(graph, graphFile, labelList, &edgeListSize, &lineNo, directed,
                  vertexOffset);

      else if (strcmp(token, "u") == 0)    // read undirected edge
         ReadEdge(graph, graphFile, labelList, &edgeListSize, &lineNo, FALSE,
                  vertexOffset);

      else if (strcmp(token, "d") == 0)    // read directed edge
         ReadEdge(graph, graphFile, labelList, &edgeListSize, &lineNo, TRUE,
                  vertexOffset);

      else 
      {
         fclose(graphFile);
         FreeGraph(graph);
         fprintf(stderr, "Unknown token %s in line %lu of graph file %s.\n",
                 token, lineNo, filename);
         exit(1);
      }
   }
   fclose(graphFile);

   //***** trim vertex, edge and label lists

   return graph;
}
Example #2
0
void Test(char *subsFileName, char *graphFileName, Parameters *parameters,
          ULONG *TPp, ULONG *TNp, ULONG *FPp, ULONG *FNp)
{
   FILE *graphFile;
   LabelList *labelList;
   BOOLEAN directed;
   Graph **subGraphs;
   ULONG numSubGraphs;
   Graph *graph;
   BOOLEAN positive1;
   BOOLEAN positive2;
   ULONG vertexOffset = 0;
   ULONG lineNo = 1;
   char token[TOKEN_LEN];
   ULONG FP = 0;
   ULONG FN = 0;
   ULONG TP = 0;
   ULONG TN = 0;
   ULONG i;

   labelList = parameters->labelList;
   directed = parameters->directed;

   // read substructures
   subGraphs = ReadSubGraphsFromFile(subsFileName, SUB_TOKEN, &numSubGraphs,
                                     parameters);
   fprintf(stdout, "Read %lu substructures from file %s.\n",
           numSubGraphs, subsFileName);

   // open example graphs file and compute stats
   graphFile = fopen(graphFileName, "r");
   if (graphFile == NULL) 
   {
      fprintf(stderr, "Unable to open graph file %s.\n", graphFileName);
      exit(1);
   }

   graph = NULL;
   positive1 = TRUE;
   while (ReadToken(token, graphFile, &lineNo) != 0) 
   {
      if (strcmp(token, POS_EG_TOKEN) == 0) 
      { // reading positive eg
         if (graph != NULL) 
         {
            // test last graph
            positive2 = PositiveExample(graph, subGraphs, numSubGraphs,
                                        parameters);
            // increment appropriate counter
            if (positive1 && positive2) TP++;
            if (positive1 && (! positive2)) FN++;
            if ((! positive1) && positive2) FP++;
            if ((! positive1) && (! positive2)) TN++;
            FreeGraph(graph);
         }
         graph = AllocateGraph(0,0);
         positive1 = TRUE;
      }
      else if (strcmp(token, NEG_EG_TOKEN) == 0) 
      { // reading negative eg
         if (graph != NULL) 
         {
            // test last graph
            positive2 = PositiveExample(graph, subGraphs, numSubGraphs,
                                        parameters);
            // increment appropriate counter
            if (positive1 && positive2) TP++;
            if (positive1 && (! positive2)) FN++;
            if ((! positive1) && positive2) FP++;
            if ((! positive1) && (! positive2)) TN++;
            FreeGraph(graph);
         }
         graph = AllocateGraph(0,0);
         positive1 = FALSE;
      }
      else if (strcmp(token, "v") == 0) 
      {  // read vertex
         if (positive1 && (graph == NULL)) 
         {
            // first graph starts without positive token, so assumed positive
            graph = AllocateGraph(0,0);
         }
         ReadVertex(graph, graphFile, labelList, &lineNo, vertexOffset);
      }
      else if (strcmp(token, "e") == 0)    // read 'e' edge
         ReadEdge(graph, graphFile, labelList, &lineNo, directed, vertexOffset);

      else if (strcmp(token, "u") == 0)    // read undirected edge
         ReadEdge(graph, graphFile, labelList, &lineNo, FALSE, vertexOffset);

      else if (strcmp(token, "d") == 0)    // read directed edge
         ReadEdge(graph, graphFile, labelList, &lineNo, TRUE, vertexOffset);

      else 
      {
         fclose(graphFile);
         fprintf(stderr, "Unknown token %s in line %lu of input file %s.\n",
                 token, lineNo, graphFileName);
         exit(1);
      }
   }
   // test last graph
   if (graph != NULL) 
   {
      positive2 = PositiveExample(graph, subGraphs, numSubGraphs,
                                  parameters);
      // increment appropriate counter
      if (positive1 && positive2) TP++;
      if (positive1 && (! positive2)) FN++;
      if ((! positive1) && positive2) FP++;
      if ((! positive1) && (! positive2)) TN++;
      FreeGraph(graph);
   }

   fclose(graphFile);

   // free substructure graphs
   for (i = 0; i < numSubGraphs; i++)
      FreeGraph(subGraphs[i]);
   free(subGraphs);

   *TPp = TP;
   *TNp = TN;
   *FPp = FP;
   *FNp = FN;
}
Example #3
0
void ReadInputFile(Parameters *parameters)
{
   FILE *inputFile = NULL;
   Graph *graph = NULL;
   Graph *posGraph= NULL;
   Graph *negGraph = NULL;
   ULONG posGraphVertexListSize = 0;
   ULONG posGraphEdgeListSize = 0;
   ULONG negGraphVertexListSize = 0;
   ULONG negGraphEdgeListSize = 0;
   ULONG *vertexListSizePtr = NULL;
   ULONG *edgeListSizePtr = NULL;
   LabelList *labelList = NULL;
   ULONG numPosEgs = 0;
   ULONG numNegEgs = 0;
   ULONG *posEgsVertexIndices = NULL;
   ULONG *negEgsVertexIndices = NULL;
   BOOLEAN readingPositive = TRUE;
   ULONG vertexOffset = 0;
   BOOLEAN directed = TRUE;
   ULONG lineNo = 1;
   char token[TOKEN_LEN];

   labelList = parameters->labelList;
   directed = parameters->directed;

   // Open input file
   inputFile = fopen(parameters->inputFileName,"r");
   if (inputFile == NULL) 
   {
      fprintf(stderr, "Unable to open input file %s.\n",
              parameters->inputFileName);
      exit(1);
   }

   // Parse input file
   while (ReadToken(token, inputFile, &lineNo) != 0) 
   {
      if (strcmp(token, POS_EG_TOKEN) == 0) 
      { // reading positive eg
         if (posGraph == NULL)
            posGraph = AllocateGraph(0,0);
         numPosEgs++;
         vertexOffset = posGraph->numVertices;
         posEgsVertexIndices = AddVertexIndex(posEgsVertexIndices,
                                              numPosEgs, vertexOffset);
         graph = posGraph;
         vertexListSizePtr = & posGraphVertexListSize;
         edgeListSizePtr = & posGraphEdgeListSize;
         readingPositive = TRUE;
      }
      else if (strcmp(token, NEG_EG_TOKEN) == 0) 
      { // reading negative eg
         if (negGraph == NULL)
            negGraph = AllocateGraph(0,0);
         numNegEgs++;
         vertexOffset = negGraph->numVertices;
         negEgsVertexIndices = AddVertexIndex(negEgsVertexIndices,
                                              numNegEgs, vertexOffset);
         graph = negGraph;
         vertexListSizePtr = & negGraphVertexListSize;
         edgeListSizePtr = & negGraphEdgeListSize;
         readingPositive = FALSE;
      }
      else if (strcmp(token, "v") == 0) 
      {  // read vertex
         if (readingPositive && (posGraph == NULL)) 
         {
            // first graph starts without positive token, so assumed positive
            posGraph = AllocateGraph(0,0);
            numPosEgs++;
            vertexOffset = 0;
            posEgsVertexIndices = AddVertexIndex(posEgsVertexIndices,
                                                numPosEgs, vertexOffset);
            graph = posGraph;
            vertexListSizePtr = & posGraphVertexListSize;
            edgeListSizePtr = & posGraphEdgeListSize;
         }
         ReadVertex(graph, inputFile, labelList, vertexListSizePtr, &lineNo,
                    vertexOffset);
      }
      else if (strcmp(token, "e") == 0)    // read 'e' edge
         ReadEdge(graph, inputFile, labelList, edgeListSizePtr, &lineNo,
                  directed, vertexOffset);

      else if (strcmp(token, "u") == 0)    // read undirected edge
         ReadEdge(graph, inputFile, labelList, edgeListSizePtr, &lineNo,
                  FALSE, vertexOffset);

      else if (strcmp(token, "d") == 0)    // read directed edge
         ReadEdge(graph, inputFile, labelList, edgeListSizePtr, &lineNo,
                  TRUE, vertexOffset);

      else 
      {
         fclose(inputFile);
         fprintf(stderr, "Unknown token %s in line %lu of input file %s.\n",
                 token, lineNo, parameters->inputFileName);
         exit(1);
      }
   }
   fclose(inputFile);

   //***** trim vertex, edge and label lists

   parameters->posGraph = posGraph;
   parameters->negGraph = negGraph;
   parameters->labelList = labelList;
   parameters->numPosEgs = numPosEgs;
   parameters->numNegEgs = numNegEgs;
   parameters->posEgsVertexIndices = posEgsVertexIndices;
   parameters->negEgsVertexIndices = negEgsVertexIndices;
}