Esempio n. 1
0
int GetMaxFlowIntPR (PNEANet &Net, const int& SrcNId, const int& SnkNId) {
  IAssert(Net->IsNode(SrcNId));
  IAssert(Net->IsNode(SnkNId));
  if (SrcNId == SnkNId) { return 0; }

  TPRManager PRM(Net);
  int MaxLabel = PRM.GetMaxLabel();

  TNEANet::TNodeI SrcNI = Net->GetNI(SrcNId);
  for (int EdgeN = 0; EdgeN < SrcNI.GetOutDeg(); EdgeN++) {
    int EId = SrcNI.GetOutEId(EdgeN);
    int OutNId = SrcNI.GetOutNId(EdgeN);
    if (OutNId != SrcNId) {
      int Capacity = PRM.Capacity(EId);
      PRM.Flow(EId) = Capacity;
      PRM.Excess(OutNId) = Capacity;
    }
  }
  GlobalRelabel(Net, PRM, SrcNId, SnkNId);
  PRM.SetLabel(SrcNId, MaxLabel);
  int RelabelCount = 1;
  int GRRate = Net->GetNodes();
  while (PRM.HasActive()) {
    int NId = PRM.PopActive();
    const TNEANet::TNodeI &NI = Net->GetNI(NId);
    int PrevLabel = MaxLabel;
    while (PRM.Excess(NId) > 0 && PRM.Label(NId) <= PrevLabel) {
      PrevLabel = PRM.Label(NId);
      int NbrNId = PushRelabel(PRM, NId, NI);
      if (NbrNId != -1 && NbrNId != SnkNId && PRM.Excess(NbrNId) > 0 && !PRM.IsActive(NbrNId)) {
        PRM.PushActive(NbrNId);
      }
    }
    if (PRM.Excess(NId) > 0 && PRM.Label(NId) < MaxLabel) {
      PRM.PushActive(NId);
    }
    if (RelabelCount % GRRate == 0) { GlobalRelabel(Net, PRM, SrcNId, SnkNId); }
  }
  return PRM.Excess(SnkNId);
}
Esempio n. 2
0
/// Implements the Global Relabeling heuristic. ##TSnap::GlobalRelabel
void GlobalRelabel (PNEANet &Net, TPRManager &PRM, const int& SrcNId, const int& SnkNId) {
  TIntQ NodeQ;
  int size = Net->GetMxNId();
  TIntV NodeV(size);
  for (int i = 0; i < size; i++) { NodeV[i] = 0; }
  NodeQ.Push(SnkNId);
  NodeV[SnkNId] = 1;
  int MaxLabel = PRM.GetMaxLabel();
  while (!NodeQ.Empty()) {
    // Backward search
    int NId = NodeQ.Top(); NodeQ.Pop();
    const TNEANet::TNodeI &NI = Net->GetNI(NId);
    // Check all edges that point out of the current node for those over which flow can be returned.
    for (int EdgeN = 0; EdgeN < NI.GetOutDeg(); EdgeN++) {
      int OutNId = NI.GetOutNId(EdgeN);
      int EId = NI.GetOutEId(EdgeN);
      if (!NodeV[OutNId] && PRM.Flow(EId) > 0) {
        NodeV[OutNId] = 1;
        NodeQ.Push(OutNId);
        PRM.SetLabel(OutNId, PRM.Label(NId) + 1);
      }
    }
    // Check all edges that point into the current node for those over which flow can be added.
    for (int EdgeN = 0; EdgeN < NI.GetInDeg(); EdgeN++) {
      int InNId = NI.GetInNId(EdgeN);
      int EId = NI.GetInEId(EdgeN);
      if (!NodeV[InNId] && PRM.Capacity(EId) > PRM.Flow(EId)) {
        NodeV[InNId] = 1;
        NodeQ.Push(InNId);
        PRM.SetLabel(InNId, PRM.Label(NId) + 1);
      }
    }
  }

  for (TNEANet::TNodeI NI = Net->BegNI(); NI != Net->EndNI(); NI++) {
    int NId = NI.GetId();
    if (NodeV[NId]) {
      if (PRM.Excess(NId) > 0 && PRM.Label(NId) < MaxLabel && NId != SnkNId) {
        if (!PRM.IsActive(NId)) { PRM.PushActive(NId); }
      }
    } else {
      if (PRM.IsActive(NId)) { PRM.RemoveActive(NId); }
      PRM.SetLabel(NId, MaxLabel);
    }
  }
}
Esempio n. 3
0
// Returns the NId where the two directions of search meet up, or -1 if no augmenting path exists. ##TSnap::IntFlowBiDBFS
int IntFlowBiDBFS (const PNEANet &Net, const int& CapIndex, TIntV &Flow, TIntQ &FwdNodeQ, TIntH &PredEdgeH, TIntQ &BwdNodeQ, TIntH &SuccEdgeH, const int& SrcNId, const int& SnkNId) {
  FwdNodeQ.Push(SrcNId);
  PredEdgeH.AddDat(SrcNId, -1);
  BwdNodeQ.Push(SnkNId);
  SuccEdgeH.AddDat(SnkNId, -1);
  while (!FwdNodeQ.Empty() && !BwdNodeQ.Empty()) {
    // Forward search
    const TNEANet::TNodeI &FwdNI = Net->GetNI(FwdNodeQ.Top()); FwdNodeQ.Pop();
    // Check all edges that point into the current node for those over which flow can be returned.
    for (int EdgeN = 0; EdgeN < FwdNI.GetInDeg(); EdgeN++) {
      int NextNId = FwdNI.GetInNId(EdgeN);
      int NextEId = FwdNI.GetInEId(EdgeN);
      if (!PredEdgeH.IsKey(NextNId) && Flow[NextEId] > 0) {
        PredEdgeH.AddDat(NextNId, NextEId);
        if (SuccEdgeH.IsKey(NextNId)) {
          return NextNId;
        }
        FwdNodeQ.Push(NextNId);
      }
    }
    // Check all edges that point out of the current node for those over which flow can be added.
    for (int EdgeN = 0; EdgeN < FwdNI.GetOutDeg(); EdgeN++) {
      int NextNId = FwdNI.GetOutNId(EdgeN);
      int NextEId = FwdNI.GetOutEId(EdgeN);
      if (!PredEdgeH.IsKey(NextNId) && Net->GetIntAttrIndDatE(NextEId, CapIndex) > Flow[NextEId]) {
        PredEdgeH.AddDat(NextNId, NextEId);
        if (SuccEdgeH.IsKey(NextNId)) {
          return NextNId;
        }
        FwdNodeQ.Push(NextNId);
      }
    }
    // Backward search
    const TNEANet::TNodeI &BwdNI = Net->GetNI(BwdNodeQ.Top());  BwdNodeQ.Pop();
    // Check all edges that point out of the current node for those over which flow can be returned.
    for (int EdgeN = 0; EdgeN < BwdNI.GetOutDeg(); EdgeN++) {
      int PrevNId = BwdNI.GetOutNId(EdgeN);
      int PrevEId = BwdNI.GetOutEId(EdgeN);
      if (!SuccEdgeH.IsKey(PrevNId) && Flow[PrevEId] > 0) {
        SuccEdgeH.AddDat(PrevNId, PrevEId);
        if (PredEdgeH.IsKey(PrevNId)) {
          return PrevNId;
        }
        BwdNodeQ.Push(PrevNId);
      }
    }
    // Check all edges that point into the current node for those over which flow can be added.
    for (int EdgeN = 0; EdgeN < BwdNI.GetInDeg(); EdgeN++) {
      int PrevNId = BwdNI.GetInNId(EdgeN);
      int PrevEId = BwdNI.GetInEId(EdgeN);
      if (!SuccEdgeH.IsKey(PrevNId) && Net->GetIntAttrIndDatE(PrevEId, CapIndex) > Flow[PrevEId]) {
        SuccEdgeH.AddDat(PrevNId, PrevEId);
        if (PredEdgeH.IsKey(PrevNId)) {
          return PrevNId;
        }
        BwdNodeQ.Push(PrevNId);
      }
    }
  }
  return -1;
}