/* 
  * Copyright 1997, Regents of the University of Minnesota 
  * 
  * kmetis.c 
  * 
  * This file contains the top level routines for the multilevel k-way partitioning 
  * algorithm KMETIS. 
  * 
  * Started 7/28/97 
  * George 
  * 
  * $Id: kmetis.c,v 1.1 1998/11/27 17:59:15 karypis Exp $ 
  * 
  */ 

 #include <metis.h> 


 /************************************************************************* 
 * This function is the entry point for KMETIS 
 **************************************************************************/ 
 void METIS_PartGraphKway(long *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt,  
                          idxtype *adjwgt, long *wgtflag, long *numflag, long *nparts,  
                          long *options, long *edgecut, idxtype *part) 
 { 
   long i; 
   float *tpwgts; 

   tpwgts = fmalloc(*nparts, "KMETIS: tpwgts"); 
   for (i=0; i<*nparts; i++)  
     tpwgts[i] = 1.0/(1.0*(*nparts)); 

   METIS_WPartGraphKway(nvtxs, xadj, adjncy, vwgt, adjwgt, wgtflag, numflag, nparts,  
                        tpwgts, options, edgecut, part); 

   free(tpwgts); 
 } 


 /************************************************************************* 
 * This function is the entry point for KWMETIS 
 **************************************************************************/ 
 void METIS_WPartGraphKway(long *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt,  
                           idxtype *adjwgt, long *wgtflag, long *numflag, long *nparts,  
                           float *tpwgts, long *options, long *edgecut, idxtype *part) 
 { 
   long i, j; 
   GraphType graph; 
   CtrlType ctrl; 

   if (*numflag == 1) 
     Change2CNumbering(*nvtxs, xadj, adjncy); 

   SetUpGraph(&graph, OP_KMETIS, *nvtxs, 1, xadj, adjncy, vwgt, adjwgt, *wgtflag); 

   if (options[0] == 0) {  /* Use the default parameters */ 
     ctrl.CType = KMETIS_CTYPE; 
     ctrl.IType = KMETIS_ITYPE; 
     ctrl.RType = KMETIS_RTYPE; 
     ctrl.dbglvl = KMETIS_DBGLVL; 
   } 
   else { 
     ctrl.CType = options[OPTION_CTYPE]; 
     ctrl.IType = options[OPTION_ITYPE]; 
     ctrl.RType = options[OPTION_RTYPE]; 
     ctrl.dbglvl = options[OPTION_DBGLVL]; 
   } 
   ctrl.optype = OP_KMETIS; 
   ctrl.CoarsenTo = amax((*nvtxs)/(40*log2(*nparts)), 20*(*nparts)); 
   ctrl.maxvwgt = 1.5*((graph.vwgt ? idxsum(*nvtxs, graph.vwgt) : (*nvtxs))/ctrl.CoarsenTo); 

   InitRandom(-1); 

   AllocateWorkSpace(&ctrl, &graph, *nparts); 

   IFSET(ctrl.dbglvl, DBG_TIME, InitTimers(&ctrl)); 
   IFSET(ctrl.dbglvl, DBG_TIME, starttimer(ctrl.TotalTmr)); 

   *edgecut = MlevelKWayPartitioning(&ctrl, &graph, *nparts, part, tpwgts, 1.03); 

   IFSET(ctrl.dbglvl, DBG_TIME, stoptimer(ctrl.TotalTmr)); 
   IFSET(ctrl.dbglvl, DBG_TIME, PrintTimers(&ctrl)); 

   FreeWorkSpace(&ctrl, &graph); 

   if (*numflag == 1) 
     Change2FNumbering(*nvtxs, xadj, adjncy, part); 
 } 


 /************************************************************************* 
 * This function takes a graph and produces a bisection of it 
 **************************************************************************/ 
 long MlevelKWayPartitioning(CtrlType *ctrl, GraphType *graph, long nparts, idxtype *part, float *tpwgts, float ubfactor) 
 { 
   long i, j, nvtxs, tvwgt, tpwgts2[2]; 
   GraphType *cgraph; 
   long wgtflag=3, numflag=0, options[10], edgecut; 

   cgraph = Coarsen2Way(ctrl, graph); 

   IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->InitPartTmr)); 
   AllocateKWayPartitionMemory(ctrl, cgraph, nparts); 

   options[0] = 1;  
   options[OPTION_CTYPE] = MATCH_SHEMKWAY; 
   options[OPTION_ITYPE] = IPART_GGPKL; 
   options[OPTION_RTYPE] = RTYPE_FM; 
   options[OPTION_DBGLVL] = 0; 

   METIS_WPartGraphRecursive(&cgraph->nvtxs, cgraph->xadj, cgraph->adjncy, cgraph->vwgt,  
                             cgraph->adjwgt, &wgtflag, &numflag, &nparts, tpwgts, options,  
                             &edgecut, cgraph->where); 

   IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->InitPartTmr)); 
   IFSET(ctrl->dbglvl, DBG_IPART, printf("Initial %ld-way partitioning cut: %ld\n", nparts, edgecut)); 

   IFSET(ctrl->dbglvl, DBG_KWAYPINFO, ComputePartitionInfo(cgraph, nparts, cgraph->where)); 

   RefineKWay(ctrl, graph, cgraph, nparts, tpwgts, ubfactor); 

   idxcopy(graph->nvtxs, graph->where, part); 

   GKfree(&graph->gdata, &graph->rdata, LTERM); 

   return graph->mincut; 

 } 



syntax highlighted by Code2HTML, v. 0.9.1