Finds the shortest paths and their lengths between the StartNode and every other node in the graph. After calling this method, every node will contain the length from the StartNode to that node (in the
Weight property) and the shortest path (in the
ShortestPath property).
Note
|
The EnumerateShortestPaths method only works with weighted graphs (a graph of TWeightedNodes and TWeightedEdges).
|
Namespace: GGraph
Delphi
|
public
procedure EnumerateShortestPaths( StartNode: TGGraphNode );
|
Parameters
StartNode
Type: TGGraphNode
The following code prints all the shortest paths from one node to every other node in the graph. The user has selected the node from Listbox1 and then executed this code:
Delphi
|
var
j: Integer;
s: String;
begin
with ListBox3.Items do
begin
Clear;
add('From Node '+ListBox1.Items[ListBox1.ItemIndex]+':');
if Sender = Button1 then
G.EnumerateShortestPaths( ListBox1.Items.Objects[ListBox1.ItemIndex] as TGGraphNode )
else
G.ShortestPath( ListBox1.Items.Objects[ListBox1.ItemIndex] as TGGraphNode );
with G.GetNodes do
while HasMoreElements do
with TWeightedNode(NextElement) do
begin
Add('The path length to '+Caption + ' is '+IntToStr(Weight));
if (ShortestPath = nil) or (Sender <> Button1) then continue;
Add('The Path is: ');
s := '';
for j := 0 to ShortestPath.Count - 1 do
s := s + ShortestPath[j] + ', ';
add(' '+s);
end;
end;
end;
|
|