Автор работы: Пользователь скрыл имя, 19 Марта 2013 в 18:53, курсовая работа
Благодаря своей простой формулировке и раздражающей неуловимости она до сих пор остается мощным стимулом исследований различных свойств графов. Настоящее столетие было свидетелем неуклонного развития теории графов, которая за последние десять – двадцать лет вступила в новый период интенсивных разработок. В этом процессе явно заметно влияние запросов новых областей: тео-рии игр и программирования, теории передачи сообщений, электрических сетей и контактных цепей, а также проблем психологии и биологии.
ВВЕДЕНИЕ 3
1. ТЕОРИЯ ГРАФОВ. 4
1.1. Историческая справка. 4
1.2. Основные термины и теоремы теории графов. 9
2. ЗАДАЧИ НА ГРАФАХ. 15
2.1. Описание различных задач на графах. 15
2.2. Нахождение кратчайших путей в графе 16
3. ПРОГРАММА ОПРЕДЕЛЕНИЯ КРАТЧАЙШЕГО ПУТИ В ГРАФЕ 19
3.1. Язык программирования Delphi. 19
3.2. Программа «Определение кратчайшего пути в графе» 20
ЗАКЛЮЧЕНИЕ 25
СПИСОК ЛИТЕРАТУРЫ 27
ПРИЛОЖЕНИЕ 28
Текст программы определения кратчайшего пути в графе 28
m:=TMenuItem.Create(Self);
m.Caption:=SaveDialog1.
m.OnClick := MyPopUpHandler;
LoadMenu.Items.Add(m);
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
MyIO.DrawCoordGrid(16,16,
MyIO.DrawAll;
end;
procedure TForm1.UpdateButtonClick(
begin
MyDraw.SetAllUnActive;
MyIO.DrawAll;
MyIO.FirstPointActive:=false;
end;
procedure TForm1.ClockClick(Sender: TObject);
begin
Splash.Show;
end;
end.
Модуль управления окном настроек:
unit Setting;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, StdCtrls, Spin,IO,MainUnit, ExtCtrls;
type
TSettingForm = class(TForm)
GridGroupBox: TGroupBox;
Label1: TLabel;
Label2: TLabel;
ColorDialog1: TColorDialog;
Label3: TLabel;
OkBitBtn: TBitBtn;
CancelBitBtn: TBitBtn;
ColorButton: TPanel;
Label4: TLabel;
Label5: TLabel;
CoordCheckBox: TCheckBox;
GridCheckBox: TCheckBox;
StepSpinEdit: TSpinEdit;
MashtabSpinEdit: TSpinEdit;
Colors: TGroupBox;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
procedure ColorButtonClick(Sender: TObject);
procedure OkBitBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure CoordCheckBoxClick(Sender: TObject);
procedure GridCheckBoxClick(Sender: TObject);
procedure CancelBitBtnClick(Sender: TObject);
procedure Panel2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
SettingForm: TSettingForm;
implementation
{$R *.DFM}
procedure TSettingForm.ColorButtonClick(
begin
if ColorDialog1.Execute then begin
ColorButton.Color:=
MyIO.GridColor:=Color;
Form1.Repaint;
end;
end;
procedure TSettingForm.OkBitBtnClick(
begin
MyIO.GridColor:=ColorButton.
MyIO.GrigStep:=StepSpinEdit.
MyIO.Mashtab:=MashtabSpinEdit.
Close;
end;
procedure TSettingForm.FormShow(Sender: TObject);
begin
with MyIO do begin
ColorButton.Color:=MyIO.
StepSpinEdit.Value:=MyIO.
MashtabSpinEdit.Value:=MyIO.
CoordCheckBox.Checked:=MyIO.
GridCheckBox.Checked:=MyIO.
Panel2.Color:=RebroColor ;
Panel3.Color:=TextColor ;
Panel1.Color:=MovingColor ;
end;
end;
procedure TSettingForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
with MyIO do begin
GridColor:=ColorButton.Color;
GrigStep:=StepSpinEdit.Value;
Mashtab:=MashtabSpinEdit.
FDrawCoord:=CoordCheckBox.
FDrawGrid:=GridCheckBox.
Form1.ShowGridButton.Down:=
RebroColor:=Panel2.Color ;
TextColor:=Panel3.Color ;
MovingColor:=Panel1.Color ;
end;
Form1.Repaint;
end;
procedure TSettingForm.
begin
MyIO.FDrawCoord:=
//Form1.Repaint;
end;
procedure TSettingForm.
begin
MyIO.FDrawGrid:=GridCheckBox.
//Form1.Repaint;
end;
procedure TSettingForm.
begin
Close;
end;
procedure TSettingForm.Panel2Click(
begin
with Sender as TPanel do
if ColorDialog1.Execute then begin
Color:=ColorDialog1.Color;
end;
end;
end.
Вспомогательный модуль потроения графа в окне программы:
unit IO;
interface
uses Data,DrawingObject,Graphics,
type
MouseState=(msNewPoint,
TIO=class
private
xt,yt,xs,ys: integer;
// FLining: boolean;
ActivePoint: integer;
MyCanvas: TCanvas;
public
GridColor: TColor;
RebroColor: TColor;
TextColor: TColor;
MovingColor: TColor;
State: MouseState;
FDrawGrid: boolean;
FDrawCoord: boolean;
FSnapToGrid: boolean;
GrigStep: integer;
FirstPoint: integer;
FirstPointActive: boolean;
LastPoint: integer;
AutoLength: boolean;
Mashtab: integer;
procedure MakeLine(X, Y: Integer);
procedure DrawPath(First,Last:integer;
procedure IONewPoint(xPos,yPos:integer);
procedure DrawAll;
procedure FormMouseDown( X, Y: Integer);
procedure Select(FirstPoint,LastPoint:
procedure DrawCoordGrid(x,y,x1,y1:
procedure DrawLine(x1,y1:Integer);
procedure RemovePoint(Num:integer);
constructor Create(Canvas:TCanvas);
end;
var MyIO:TIO;
implementation
procedure TIO.MakeLine(X, Y: Integer);
var i:integer;
V1,V2:TPoint;
begin
i:=MyDraw.FindNumberByXY(X,Y);
if i<>-1 then
if State=msLining then begin
MyData.Rebro(ActivePoint,i);
if AutoLength then begin
V1:=MyDraw.FindByNumber(
V2:=MyDraw.FindByNumber(i);
MyData.SetRebroLength(
sqrt(sqr(Mashtab*(V1.x-V2.x)/ GrigStep)+
sqr(Mashtab*(V1.y-V2.y)/ GrigStep))));
end;
MyCanvas.MoveTo(xs,ys);
MyCanvas.LineTo(xt,yt);
DrawPath(ActivePoint,i,false);
State:=msNewPoint;
MyDraw.SetUnActive(
end
else begin
ActivePoint:=i;
State:=msLining;
xs:=MyDraw.FindByNumber(i).x; xt:=xs;
ys:=MyDraw.FindByNumber(i).y; yt:=ys;
MyDraw.SetActive(i);
end ;
end;
procedure TIO.DrawLine(x1,y1:Integer);
begin
if State=msLining then
with MyCanvas do
begin
Pen.Width:=2;
Pen.Color:=MovingColor;
Pen.Mode:=pmXor;
Pen.Style:=psSolid;
MoveTo(xs,ys);
LineTo(xt,yt);
MoveTo(xs,ys);
LineTo(x1,y1);
xt:=x1;
yt:=y1;
end;
{if State=msMove then
with MyCanvas do
begin
Pen.Width:=2;
Pen.Color:=MovingColor;
Pen.Mode:=pmXor;
Pen.Style:=psSolid;
MoveTo(xs,ys);
LineTo(xt,yt);
MoveTo(xs,ys);
LineTo(x1,y1);
xt:=x1;
yt:=y1;
end;}
end;
procedure TIO.FormMouseDown( X, Y: Integer);
var Mini,Maxi,i,j,Temp,Te:integer;
b,k:real;
Flag:Boolean;
function StepRound(Num,Step:integer):
begin
if (Num mod Step)>(Step/2)then Result:=Num- Num mod Step+Step
else Result:=(Num div Step)*Step;
end;
begin
Te:=MyDraw.FindNumberByXY(X,Y)
if (Te=-1)and(state<>msMove) then
with MyData,MyDraw do begin
i:=1;
j:=1;
Flag:=false;
repeat
repeat
if (Dimension>0)and(Matrix[i,j]=
Mini:=Min(FindByNumber(i).x,
Maxi:=Max(FindByNumber(i).x,
if Mini<>Maxi then
k:=(FindByNumber(i).y-
else k:=0;
b:= FindByNumber(i).y- (k*FindByNumber(i).x) ;
if (X>=Mini)and(X<Maxi) and
( Y>=(k*X+b-8) )and ( Y<=(k*X+b+8))
then begin
Flag:=true;
Select(i,j);
Exit;
end;
end;
inc(i);
until(Flag)or(i>Dimension);
inc(j);
i:=1;
until(Flag)or(j>Dimension);
end
else begin
if FirstPointActive then begin
if State=msMove then begin
flag:=true;
MyDraw.move(FirstPoint,x,y);
MyDraw.SetUnActive(FirstPoint)
DrawAll;
FirstPointActive:=False;
end;
LastPoint:=Te
end
else begin
FirstPoint:=Te;
FirstPointActive:=True;
end;
MyDraw.SetActive(Te);
if State=msDelete then
RemovePoint(Te);
Exit;
end;
if not flag then begin
if FSnapToGrid then IONewPoint(StepRound(x,
else IONewPoint(x,y);end;
end;
procedure TIO.Select(FirstPoint,
var s:string;
begin
with MyData do begin
DrawPath(FirstPoint,LastPoint,
S:=InputBox('Ввод','Введите длину ребра ','');
if(s='')or(not(StrToInt(S) in [1..250]))then begin
ShowMessage('Некорректно введена длина');
exit;
end;
{ if Oriented then
if Matrix[FirstPoint,LastPoint]<>
MatrixLength[FirstPoint,
MatrixLength[LastPoint,
else
begin }
LengthActive:=True;
SetRebroLength(FirstPoint,
// end;
DrawPath(FirstPoint,LastPoint,
end;
end;
procedure TIO.DrawPath(First,Last:
var s:string;
begin
with MyDraw,MyCanvas do
begin
{!!pmMerge} Pen.Mode:=pmCopy;
Pen.Width:=2;
brush.Style:=bsClear;
Font.Color:=TextColor;
PenPos:=FindByNumber(First);
if Light then begin
Pen.Color:=clYellow;
SetActive(First);
SetActive(Last);
end
else Pen.Color:=RebroColor;
LineTo(FindByNumber(Last).x,
FindByNumber(Last).y );
if (MyData.LengthActive)and
(MyData.MatrixLength[First,
begin
s:=IntToStr(MyData.
TextOut((FindByNumber(Last).x+
(FindByNumber(Last).y+
end;
DrawSelf(First);
DrawSelf(Last);
end;
end;
procedure TIO.DrawAll;
var i,j:byte;
begin
for i:=1 to MyData.Dimension do
for j:=1 to MyData.Dimension do
if MyData.Matrix[i,j]=1 then DrawPath(i,j,false);
MyDraw.DrawAll;
end;
procedure TIO.IONewPoint(xPos,yPos:
begin
MyData.NewPoint;
MyDraw.NewPoint(xPos,yPos);
MyDraw.DrawAll;
end;
procedure TIO.DrawCoordGrid(x,y,x1,y1:
var i,j,nx,ny,nx1,ny1:integer;
begin
if FDrawGrid then begin
nx:=x div GrigStep;
nx1:=x1 div GrigStep;
ny:=y div GrigStep;
ny1:=y1 div GrigStep;
MyCanvas.Brush.Style:=bsClear;
MyCanvas.Pen.Color:=GridColor;
for i:=1 to nx1-nx do
for j:=1 to ny1-ny do
MyCanvas.Pixels[i*GrigStep,y1-
end;
if FDrawCoord then
with MyCanvas do begin
Pen.Width:=1;
MoveTo(nx+GrigStep,y-5);
LineTo(nx+GrigStep,y1+2);
LineTo(x1-4,y1+2);
{horizontal}
for i:=1 to nx1-nx do begin
MoveTo(nx+i*GrigStep,y1-1);
LineTo(nx+i*GrigStep,y1+5);
TextOut(nx+i*GrigStep-5,y1+8,
end; {vertical}
for i:=1 to ny1-ny do begin
MoveTo(x+2,y1-GrigStep*i);
LineTo(x+7,y1-GrigStep*i);
TextOut(x-15,y1-i*GrigStep-
end;
end;
end;
constructor TIO.Create(Canvas:TCanvas);
begin
GrigStep:=20;
FSnapToGrid:=true;
GridColor:=clBlack;
RebroColor:=clMaroon;
MovingColor:=clBlue;
TextColor:=clBlack;
Mashtab:=1;
MyCanvas:=Canvas;
State:=msNewPoint;
FDrawCoord:=false;
end;
procedure TIO.RemovePoint(Num: integer);
var j:integer;N,MPenPos:TPoint;
begin
{with MyCanvas do begin
Pen.Width:=2;
Pen.Color:=RebroColor;
Pen.Mode:=pmXor;
Pen.Style:=psSolid;
MPenPos:=MyDraw.FindByNumber(
for j:=1 to MyData.Dimension do
if MyData.Matrix[Num,j]=1 then begin
N:=MyDraw.FindByNumber(j);
PolyLine([MPenPos,N]);
end;}
{ Pen.Mode:=pmNot;
for j:=1 to MyData.Dimension do
if MyData.Matrix[Num,j]=1 then begin
N:=MyDraw.FindByNumber(j);
PolyLine([MPenPos,N]);
end;
end;}
MyData.Remove(Num);
MyDraw.Remove(Num);
end;
end.
Модуль визуального отображения графа в окне программы:
unit DrawingObject;
interface
uses
Classes, Windows, Graphics,dialogs,SysUtils;
type
Colors=(Red,RedLight,Blue,
Obj=record
Place :TRect;
PlaceX,PlaceY :integer;
Color :Colors ;
end;
TDrawingObject = class(TObject)
protected
MyCanvas:TCanvas;
public
Dim:integer;
Bitmaps:array[1..6]of TBitmap;
Arr:array of Obj;
constructor Create(Canvas:TCanvas);
procedure Remove(Num:integer);
procedure NewPoint(x,y:integer);
procedure DrawSelf(Num:integer);
procedure DrawSelfXY(X,Y:integer);
function HasPoint(Num,X,Y:integer): Boolean;
destructor Destroy ;
procedure DrawAll;
procedure Clear;
procedure Save(FileName:string);
procedure Load(FileName:string);
procedure SetActive(Num:integer);
procedure SetUnActive(Num:integer);
procedure SetAllUnActive;
procedure Move(number,x,y:integer);
procedure SetColor(Num:integer;NewColor:
function FindByNumber(Num:integer): TPoint;
function FindNumberByXY(X,Y:integer):
end;
var MyDraw:TDrawingObject;
implementation
procedure TDrawingObject.Clear;
begin
Dim:=0;
Arr:=nil;
end;
procedure TDrawingObject.NewPoint(x,y:
begin
inc(Dim);
SetLength(Arr,Dim+1);
with Arr[Dim] do
begin
PlaceX:=x;
PlaceY:=y;
Place.Left:=x-Bitmaps[1].Width div 2;
Place.Top:=y-Bitmaps[1].Width div 2;
Place.Right:=x+Bitmaps[1].
Place.Bottom:=y+Bitmaps[1].
Color :=Red;
end;
end;
constructor TDrawingObject.Create(Canvas:
var i:byte;
begin
MyCanvas:=Canvas;
Dim:=0;
for i:=1 to 6 do
Bitmaps[i]:=TBitmap.Create;
Bitmaps[1].
Bitmaps[2].
Bitmaps[3].LoadFromResourceNam
Bitmaps[4].
Bitmaps[5].
Bitmaps[6].
for i:=1 to 6 do
Bitmaps[i].Transparent:=True;
end;
procedure TDrawingObject.DrawSelfXY(X,Y:
begin
DrawSelf(FindNumberByXY(X,Y));
end;
procedure TDrawingObject.DrawSelf(Num:
begin
with Arr[Num] do
case Color of
Red: MyCanvas.Draw(Place.Left,
RedLight: MyCanvas.Draw(Place.Left,
Blue: MyCanvas.Draw(Place.Left,
Green: MyCanvas.Draw(Place.Left,
Yellow: MyCanvas.Draw(Place.Left,
Purple: MyCanvas.Draw(Place.Left,
else
MyCanvas.Draw(Place.Left,
end;
end;
function TDrawingObject.HasPoint(Num,X,
begin
with Arr[Num] do
if(X >= Place.Left) and (X <= Place.Right)
and (Y >= Place.Top) and (Y <= Place.Bottom)then
Result := True
else
Result := False;
end;
procedure TDrawingObject.DrawAll;
var
i: Integer;
begin
for i :=1 to Dim do
DrawSelf(i);
end;
function TDrawingObject.FindByNumber(
begin
Result.x := Arr[Num].PlaceX;
Result.y := Arr[Num].PlaceY;
end;
function TDrawingObject.FindNumberByXY(
var
i: Integer;
begin
Result:=-1;
for i :=1 to Dim do
if HasPoint(i,X,Y) then
begin
Result:=i;
Exit;
end;
end;
procedure TDrawingObject.SetUnActive(
begin
Arr[Num].Color:=Red;
DrawSelf(Num);
end;
destructor TDrawingObject.Destroy ;