Updated solution for day 25, significantly speeding up the algorithm

This commit is contained in:
Stefan Müller 2024-06-05 13:40:31 +02:00
parent c5ea70ed21
commit 3f83f888f3
1 changed files with 75 additions and 92 deletions

View File

@ -34,10 +34,13 @@ type
private private
FName: string; FName: string;
FMergedComponents: TSnowComponents; FMergedComponents: TSnowComponents;
FMergeParent: TSnowComponent;
public public
property Name: string read FName; property Name: string read FName;
procedure AddMerged(constref AMerge: TSnowComponent); procedure AddMerged(constref AMerge: TSnowComponent);
function GetRootComponent: TSnowComponent;
function GetMergeCount: Integer; function GetMergeCount: Integer;
procedure Reset;
constructor Create(const AName: string); constructor Create(const AName: string);
destructor Destroy; override; destructor Destroy; override;
end; end;
@ -46,13 +49,13 @@ type
TWire = class TWire = class
private private
FComponent1, FComponent2, FOriginalComponent1, FOriginalComponent2: TSnowComponent; FComponent1, FComponent2: TSnowComponent;
function GetComponent1: TSnowComponent;
function GetComponent2: TSnowComponent;
public public
property Component1: TSnowComponent read FComponent1; property Component1: TSnowComponent read GetComponent1;
property Component2: TSnowComponent read FComponent2; property Component2: TSnowComponent read GetComponent2;
procedure Reattach(constref AOldComponent, ANewComponent: TSnowComponent);
function IsLoop: Boolean; function IsLoop: Boolean;
procedure Reset;
constructor Create(constref AComponent1, AComponent2: TSnowComponent); constructor Create(constref AComponent1, AComponent2: TSnowComponent);
end; end;
@ -63,10 +66,10 @@ type
TNetwork = class TNetwork = class
private private
FComponents: TSnowComponents; FComponents: TSnowComponents;
FDisconnectableWires, FContractedWires: TWires; FWires, FContracted: TWires;
public public
function FindOrCreateComponent(const AName: string): TSnowComponent; function FindOrAddComponent(const AName: string): TSnowComponent;
procedure AddWire(constref AWire: TWire); procedure AddWire(constref AComponent1, AComponent2: TSnowComponent);
function RunMinCutContractionAlgorithm: Integer; function RunMinCutContractionAlgorithm: Integer;
procedure Reset; procedure Reset;
function GetResult: Integer; function GetResult: Integer;
@ -93,29 +96,38 @@ implementation
{ TSnowComponent } { TSnowComponent }
procedure TSnowComponent.AddMerged(constref AMerge: TSnowComponent); procedure TSnowComponent.AddMerged(constref AMerge: TSnowComponent);
var
i: Integer;
c: TSnowComponent;
begin begin
FMergedComponents.Add(AMerge); FMergedComponents.Add(AMerge);
i := AMerge.FMergedComponents.Count - 1; AMerge.FMergeParent := Self;
while i >= 0 do
begin
c := AMerge.FMergedComponents.ExtractIndex(i);
FMergedComponents.Add(c);
Dec(i);
end; end;
function TSnowComponent.GetRootComponent: TSnowComponent;
begin
Result := Self;
while Result.FMergeParent <> nil do
Result := Result.FMergeParent;
end; end;
function TSnowComponent.GetMergeCount: Integer; function TSnowComponent.GetMergeCount: Integer;
var
c: TSnowComponent;
begin begin
Result := FMergedComponents.Count + 1; Result := 1;
for c in FMergedComponents do
Inc(Result, c.GetMergeCount);
end;
procedure TSnowComponent.Reset;
begin
FMergedComponents.Clear;
FMergeParent := nil;
end; end;
constructor TSnowComponent.Create(const AName: string); constructor TSnowComponent.Create(const AName: string);
begin begin
FName := AName; FName := AName;
FMergedComponents := TSnowComponents.Create; FMergedComponents := TSnowComponents.Create(False);
FMergeParent := nil;
end; end;
destructor TSnowComponent.Destroy; destructor TSnowComponent.Destroy;
@ -126,36 +138,30 @@ end;
{ TWire } { TWire }
procedure TWire.Reattach(constref AOldComponent, ANewComponent: TSnowComponent); function TWire.GetComponent1: TSnowComponent;
begin begin
if FComponent1 = AOldComponent then Result := FComponent1.GetRootComponent;
FComponent1 := ANewComponent; end;
if FComponent2 = AOldComponent then
FComponent2 := ANewComponent; function TWire.GetComponent2: TSnowComponent;
begin
Result := FComponent2.GetRootComponent;
end; end;
function TWire.IsLoop: Boolean; function TWire.IsLoop: Boolean;
begin begin
Result := FComponent1 = FComponent2; Result := Component1 = Component2;
end;
procedure TWire.Reset;
begin
FComponent1 := FOriginalComponent1;
FComponent2 := FOriginalComponent2;
end; end;
constructor TWire.Create(constref AComponent1, AComponent2: TSnowComponent); constructor TWire.Create(constref AComponent1, AComponent2: TSnowComponent);
begin begin
FComponent1 := AComponent1; FComponent1 := AComponent1;
FComponent2 := AComponent2; FComponent2 := AComponent2;
FOriginalComponent1 := AComponent1;
FOriginalComponent2 := AComponent2;
end; end;
{ TNetwork } { TNetwork }
function TNetwork.FindOrCreateComponent(const AName: string): TSnowComponent; function TNetwork.FindOrAddComponent(const AName: string): TSnowComponent;
var var
found: Boolean; found: Boolean;
begin begin
@ -173,95 +179,73 @@ begin
end; end;
end; end;
procedure TNetwork.AddWire(constref AWire: TWire); procedure TNetwork.AddWire(constref AComponent1, AComponent2: TSnowComponent);
begin begin
FDisconnectableWires.Add(AWire); FWires.Add(TWire.Create(AComponent1, AComponent2));
end; end;
function TNetwork.RunMinCutContractionAlgorithm: Integer; function TNetwork.RunMinCutContractionAlgorithm: Integer;
var var
r, i: Integer; r, count: Integer;
contraction, w: TWire; w: TWire;
begin begin
while FComponents.Count > 2 do count := FComponents.Count;
while count > 2 do
begin begin
// Determines contraction. // Determines contraction wire.
r := Random(FDisconnectableWires.Count - 1); r := Random(FWires.Count - 1);
contraction := FDisconnectableWires.ExtractIndex(r); w := FWires.ExtractIndex(r);
FContractedWires.Add(contraction); FContracted.Add(w);
// Merges contraction.Component2 into contraction.Component1. // Merges c2 into c1.
contraction.Component1.AddMerged(FComponents.Extract(contraction.Component2)); if not w.IsLoop then
// Fix the wires connected to contraction.Component2.
i := FDisconnectableWires.Count - 1;
while i >= 0 do
begin begin
FDisconnectableWires[i].Reattach(contraction.Component2, contraction.Component1); w.Component1.AddMerged(w.Component2);
if FDisconnectableWires[i].IsLoop then Dec(count);
begin
w := FDisconnectableWires.ExtractIndex(i);
FContractedWires.Add(w);
end;
Dec(i);
end; end;
end; end;
Result := FDisconnectableWires.Count; Result := 0;
for w in FWires do
if not w.IsLoop then
Inc(Result);
end; end;
procedure TNetwork.Reset; procedure TNetwork.Reset;
var var
i, j: Integer;
c: TSnowComponent; c: TSnowComponent;
i: Integer;
w: TWire; w: TWire;
begin begin
// Resets the components. for c in FComponents do
i := 0; c.Reset;
while i < FComponents.Count do
begin
j := FComponents[i].FMergedComponents.Count - 1;
while j >= 0 do
begin
c := FComponents[i].FMergedComponents.ExtractIndex(j);
FComponents.Add(c);
Dec(j);
end;
Inc(i);
end;
// Resets the wires. i := FContracted.Count - 1;
for w in FDisconnectableWires do
w.Reset;
i := FContractedWires.Count - 1;
while i >= 0 do while i >= 0 do
begin begin
w := FContractedWires.ExtractIndex(i); w := FContracted.ExtractIndex(i);
w.Reset; FWires.Add(w);
FDisconnectableWires.Add(w);
Dec(i); Dec(i);
end; end;
end; end;
function TNetwork.GetResult: Integer; function TNetwork.GetResult: Integer;
begin begin
Result := 0; Result := FComponents[0].GetRootComponent.GetMergeCount;
if FComponents.Count = 2 then Result := Result * (FComponents.Count - Result);
Result := FComponents[0].GetMergeCount * FComponents[1].GetMergeCount;
end; end;
constructor TNetwork.Create; constructor TNetwork.Create;
begin begin
FComponents := TSnowComponents.Create; FComponents := TSnowComponents.Create;
FDisconnectableWires := TWires.Create; FWires := TWires.Create;
FContractedWires := TWires.Create; FContracted := TWires.Create;
end; end;
destructor TNetwork.Destroy; destructor TNetwork.Destroy;
begin begin
FComponents.Free; FComponents.Free;
FDisconnectableWires.Free; FWires.Free;
FContractedWires.Free;
inherited Destroy; inherited Destroy;
end; end;
@ -285,25 +269,24 @@ var
i: Integer; i: Integer;
begin begin
split := ALine.Split([':', ' ']); split := ALine.Split([':', ' ']);
c1 := FNetwork.FindOrCreateComponent(split[0]); c1 := FNetwork.FindOrAddComponent(split[0]);
for i := 2 to Length(split) - 1 do for i := 2 to Length(split) - 1 do
begin begin
c2 := FNetwork.FindOrCreateComponent(split[i]); c2 := FNetwork.FindOrAddComponent(split[i]);
FNetwork.AddWire(TWire.Create(c1, c2)); FNetwork.AddWire(c1, c2);
end; end;
end; end;
procedure TSnowverload.Finish; procedure TSnowverload.Finish;
var var
cut, count: Integer; cut: Integer;
begin begin
// Karger's algorithm with known minimum cut size. // Karger's algorithm with known minimum cut size.
// See https://en.wikipedia.org/wiki/Karger%27s_algorithm // See https://en.wikipedia.org/wiki/Karger%27s_algorithm
count := 1; Randomize;
cut := FNetwork.RunMinCutContractionAlgorithm; cut := FNetwork.RunMinCutContractionAlgorithm;
while cut > 3 do while cut > 3 do
begin begin
Inc(count);
FNetwork.Reset; FNetwork.Reset;
cut := FNetwork.RunMinCutContractionAlgorithm; cut := FNetwork.RunMinCutContractionAlgorithm;
end; end;