Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program lab6;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- const
- N = 5;
- type
- TArray = array[1..N, 1..N] of Integer;
- TSet = set of 1..N;
- const
- TestArr: TArray = ((0, 1, 0, 1, 0), (1, 0, 1, 0, 1), (0, 1, 0, 1, 0), (1, 0, 1, 0, 1), (0, 1, 0, 1, 0));
- procedure InputFromKeyboard(var A: TArray);
- var
- i, j, x: Integer;
- begin
- x := 2;
- for i := 1 to N - 1 do
- begin
- for j := x to N do
- begin
- Write('A[', i, '][', j, '] = ');
- Readln(A[i,j]);
- A[j, i] := A[i][j];
- end;
- Inc(x);
- end;
- for i := 1 to N do
- A[i][i] := 0;
- Writeln;
- end;
- procedure RandomInput(var A: TArray);
- var
- i, j, k: Integer;
- begin
- k := 2;
- for i := 1 to N - 1 do
- begin
- for j := k to N do
- begin
- Randomize;
- A[i,j] := Random(2);
- Writeln('A[', i, '][', j, '] = ', A[i,j]);
- A[j, i] := A[i][j];
- end;
- Inc(k);
- end;
- for i := 1 to N do
- A[i][i] := 0;
- Writeln;
- end;
- procedure Output(A: TArray);
- var
- i, j: Integer;
- begin
- for i := 1 to N do
- begin
- for j := 1 to N do
- Write(A[i][j], ' ');
- Writeln;
- end;
- Writeln;
- end;
- procedure SelectingEnthryMethod(var A: TArray);
- var
- EnthryMethod: Integer;
- begin
- Writeln('Please select array entry method:'); Writeln;
- Writeln('Enter ''1'' if you prefer random input.');
- Writeln('Enter ''2'' if you prefer input from keyboard.');
- Writeln('Enter ''3'' if you prefer input from constant.'); Writeln;
- Readln(EnthryMethod); Writeln;
- case EnthryMethod of
- 1: RandomInput(A);
- 2: InputFromKeyboard(A);
- 3: begin A := TestArr; Writeln; end;
- end;
- end;
- function isNotConnected(FuncNotCandidates: TSet; FuncCandidates: TSet; FuncA: TArray): Boolean;
- var
- i, j: Integer;
- AnyNull, funcValue: Boolean;
- begin
- i := 1;
- AnyNull := false;
- funcValue := true;
- while (i <= N) and (funcValue) do
- begin
- if (i in FuncNotCandidates) then
- begin
- j := 1;
- while (j <= N) and (not AnyNull) do
- begin
- if (j in FuncCandidates) and (FuncA[i, j] = 0) then
- AnyNull := true;
- Inc(j);
- end;
- if AnyNull = false then
- funcValue := false
- end;
- Inc(i);
- end;
- isNotConnected := funcValue;
- end;
- // вроде всё гуд до сюда
- procedure extend(var Candidates: TSet; var NotCandidates: TSet; var CompSub: TSet; A: TArray);
- var
- Final, NotBreak: Boolean;
- t, v, e, w: Integer;
- new_candidates, new_NotCandidates: TSet;
- begin
- Final := True;
- while (Candidates <> []) and (Final) and (IsNotConnected(NotCandidates, Candidates, A)) do
- begin
- // Выбираем вершину V из Candidates и добавляем в CompSub
- t := 1;
- NotBreak := true;
- while (t <= N) and (NotBreak) do
- begin
- if (t in Candidates) then
- begin
- NotBreak := False;
- v := t;
- end;
- Inc(t);
- end;
- compsub := compsub + [v];
- // Формируем new_candidates и new_not, удаляя из candidates и not вершины, не соединённые с v
- new_candidates := Candidates;
- new_NotCandidates := NotCandidates;
- for e := 1 to n do
- if (e in Candidates) or (e in NotCandidates) then
- if A[e, v] = 0 then
- begin
- new_candidates := new_candidates - [e];
- new_NotCandidates := new_NotCandidates - [e];
- end;
- // Если Candidates и NotCandidates пусты, вызвать рекурсивно extend
- if (new_Candidates = []) and (new_NotCandidates = []) then
- begin
- Final := False;
- for w := 1 to N do
- if w in CompSub then
- Write(w, ' ');
- end
- else
- extend(new_Candidates, new_NotCandidates, compsub, A);
- Break;
- compsub := compsub - [v];
- candidates := candidates - [v];
- NotCandidates := NotCandidates + [v];
- end;
- Sleep(1);
- end;
- var
- Matrix: TArray;
- MainCompSub, MainCandidates, MainNotCandidates: TSet;
- z: Integer;
- begin
- SelectingEnthryMethod(Matrix);
- Output(Matrix);
- MainCompSub := [];
- MainNotCandidates := [];
- MainCandidates := [1..N];
- extend(MainCandidates, MainNotCandidates, MainCompSub, Matrix);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement