/* ========================================================================
   MULTICOMMODITY (CONCURRENT) FLOW PROGRAM

   Uses the following files:
       MULTICOM3.C	main program
       MULTICOMIO.C	input / output routines
       ROUTE.C		routine to find initial routes
       DECONGEST3.C	iterated procedure
       MINCOST.C	minimum-cost flow routine (Bertsekas & Tseng)
       COMMON.H		include files; macro, type, struct definitions
       MCF.H		global variable and array declarations
       EXPINTEGERS	integral powers of e
       EXPHUNDRETHS	decimal powers of e (to the 2nd decimal place)
       LIBF2C.A		library functions from Fortran to C conversion

   Written:
       August 8, 1991 by Tishya A. Leong

   Usage:
       mcf3 inputfile outputfile
   ======================================================================== */


/* ----------------------------------------------------------------------
   File: ROUTE.C
   Purpose: Find an initial route for a commodity
   Contains functions called from: MULTICOM3.C
   Calls functions in: MINCOST.C
   ---------------------------------------------------------------------- */


/* ------------------------------------------------------------------------
   Include libraries
   Macro definition, type definition, structure definition
   Declaration of global variables and arrays
   ------------------------------------------------------------------------ */
#include "common.h"
#include "mcf.h"


/* ------------------------------------------------------------------------
   Function declarations
   ------------------------------------------------------------------------ */
void findInitialRoute();	/* find a flow for a commodity	*/
extern void min_cost_flow();	/* in MINCOST.C			*/


/* ------------------------------------------------------------------------
   findInitialRoute:  route commodity along min-cost flow
   called by function:  main in MULTICOM3.C
   calls functions:  min_cost_flow in MINCOST.C
   ------------------------------------------------------------------------ */
void findInitialRoute(commodity)
    int commodity;
{
    static int i;
    static int demand;
    static int *base;

    /* Locate beginning of commodity group and extract demand for group */
    base = commodityGroups[commodity - 1].demands;
    demand = -1 * base[commodityGroups[commodity - 1].source - 1];

    /* Give edges with lower capacity higher cost (length),
       Set capacities of edges equal to demand so that demand can be met */
    for (i = 1; i <= n_edges; i++) {
        comLengths[i - 1] = (int)(1 / capacities[i - 1]);
        comLengths[n_edges + i - 1] = comLengths[i - 1];
        comCapacities[i - 1] = demand;
	comCapacities[n_edges + i - 1] = 0; /* DIGRAPH CHANGE */
    }

    /* Get demand for each node */
    for (i = 1; i <= n_nodes; i++)
        comDemands[i - 1] = base[i - 1];

    /* Find a flow using min-cost flow routine */
    min_cost_flow(n_nodes, 2 * n_edges, heads, tails, comLengths,
		  comCapacities, comDemands, intFlows);
    for (i = 1; i <= n_edges; i++)
        individual_flows[n_edges * (commodity - 1) + i - 1] =
	    (Ftype)intFlows[i - 1] - (Ftype)intFlows[n_edges + i - 1];

    return;
}

