
/*
**  This file forms part of the GOBLIN C++ Class Library.
**
**  Written by Christian Fremuth-Paeger, February 1999.
**  
**  Copying, compiling, distribution and modification
**  of this source code is permitted only in accordance
**  with the GOBLIN general licence information.
*/             


#if !defined(_GRA2BAL_H_)

#define _GRA2BAL_H_

#include <absbal.h>
#include <absgra.h>


class gra2bal : public abstractBalancedFNW
{                                        
friend class iGra2bal;
protected:

    abstractGraph & G;        // The original graph

    TNode           n0;       // Number of nodes in the original network
    TArc            m0;       // Number of arcs in the original network

    TNode           s1;       // Sorce node, s1 == Source()
    TNode           t1;       // Sink node, t1 == Target()
    TNode           s2;       // Additional source node
    TNode           t2;       // Additional target node
    TArc            ret1;     // Return arc with end nodes t1 and s1
    TArc            ret2;     // Return arc with end nodes t2 and s2
    TArc            art1;     // Artifical arc with end nodes s1 and t2
    TArc            art2;     // Artifical arc with end nodes s2 and t1

    TFloat*         flow;     // Array of flow values
    TFloat*         deg;      // Array of node degrees in the original graph
    TCap            ccap;     // Node capacity constant
    TCap*           cap;      // Array of (upper-lower) node capacities
    TCap*           lower;    // Array of lower node capacities
    
    TCap            sumLower; // sum of lower degree bounds
    TCap            sumUpper; // sum of upper degree bounds

public:             

    gra2bal(abstractGraph &GC,TCap* pLower,TCap *pCap = NULL) throw();
    gra2bal(abstractGraph &GC,TCap cCap) throw();
    gra2bal(abstractGraph &GC) throw();
    ~gra2bal() throw();

    void            Init() throw();
    unsigned long   Size() throw();
    unsigned long   Allocated() throw();

    bool            Perfect() throw();

    void            Symmetrize() throw();
    void            Relax() throw();

    TNode           Source() throw() {return s1;};
    TNode           Target() throw() {return t1;};     

    TNode           StartNode(TArc a) throw(ERRange);
    TNode           EndNode(TArc a) throw(ERRange);
   
    TArc            First(TNode v) throw(ERRange);
    TArc            Right(TArc a,TNode u) throw(ERRange,ERRejected);

    TCap            Demand(TNode v) throw(ERRange);
    TCap            LCap(TArc a) throw(ERRange);
    bool            CLCap() throw() {return 1;};
    TCap            UCap(TArc a) throw(ERRange);
    bool            CUCap() throw() {return 0;};
    TFloat          Length(TArc a) throw(ERRange);
    bool            CLength() throw()
                        {return G.CLength() & (G.MaxLength()==0);};
    bool            Blocking(TArc a) throw() {return a&1;};

    TFloat          C(TNode v,char i) throw(ERRange);
    TFloat          CMax(char i) throw(ERRange);
    char            Dim() throw() {return 2;};

    bool            HiddenNode(TNode v) throw(ERRange);
    bool            HiddenArc(TArc a) throw(ERRange);

    TFloat          Flow(TArc a) throw(ERRange);
    TFloat          BalFlow(TArc a) throw(ERRange);
    void            Push(TArc a,TFloat Lambda) throw(ERRange,ERRejected);
    void            BalPush(TArc a,TFloat Lambda) throw(ERRange,ERRejected);
    
    TFloat          Sub(TArc a) throw(ERRange) {return Flow(a);};
    void            AddArc(TArc a,TFloat Lambda) throw() {};
    void            OmitArc(TArc a,TFloat Lambda) throw() {};
    
    void            CancelOdd() throw();

    void            Write(char* fileName) throw(ERFile)
                        {WriteBalancedFNW(fileName,WRITE_INCIDENCES);};

};                             


#endif
