Finds the lengths of the shortest paths from the given node to every other node in the graph. After calling this method, the Weight property of every node equals the shortest path length between the StartNode parameter and that node.
Use
EnumerateShortestPaths to not only find the length between nodes but also the actual shortest path.
Note
|
The ShortestPath method only works with weighted graphs (a graph of TWeightedNodes and TWeightedEdges).
|
Namespace: GGraph
Delphi
|
public
procedure ShortestPath( StartNode: TGGraphNode ); virtual;
|
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;
|
|