private void AddToTree(HungarianNode From, HungarianNode To) { To.Mark = true; To.Parent = From; foreach (HungarianNode Node in _Right) { if (To.Potential + Node.Potential - To.GetWeight(Node) < Node.Slack) { Node.Slack = To.Potential + Node.Potential - To.GetWeight(Node); Node.SlackNode = To; } } }
private void Augment(Queue <HungarianNode> Queue) { HungarianNode Root = null; foreach (HungarianNode Node in _Left) { if (Node.Match == null) { Queue.Enqueue(Node); Node.Parent = null; Node.Mark = true; Root = Node; } } foreach (HungarianNode Node in _Right) { Node.Slack = Root.Potential + Node.Potential - Root.GetWeight(Node); Node.SlackNode = Root; } KeyValuePair <HungarianNode, HungarianNode> Exposed = ExposePath(Queue); if (Exposed.Value == null) { UpdateLabels(); Exposed = ImproveLabeling(Queue); } if (Exposed.Value != null) { _Rounds++; HungarianNode CurrentRight = Exposed.Value; HungarianNode CurrentLeft = Exposed.Key; while (CurrentLeft != null && CurrentRight != null) { HungarianNode TempRight = CurrentLeft.Match; CurrentRight.Match = CurrentLeft; CurrentLeft.Match = CurrentRight; CurrentRight = TempRight; CurrentLeft = CurrentLeft.Parent; } } }
private KeyValuePair <HungarianNode, HungarianNode> ExposePath(Queue <HungarianNode> Queue) { while (Queue.Count > 0) { HungarianNode Current = Queue.Dequeue(); foreach (HungarianNode Node in _Right) { if (Math.Abs(Current.GetWeight(Node) - Current.Potential - Node.Potential) < _EPSILON && !Node.Mark) { if (Node.Match == null) { return(new KeyValuePair <HungarianNode, HungarianNode>(Current, Node)); } else { Node.Mark = true; Queue.Enqueue(Node); AddToTree(Node, Current); } } } } return(new KeyValuePair <HungarianNode, HungarianNode>(null, null)); }