wl_lib_0.1.3_1c94dc7c/src/wl-graphs.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
--  with Ada.Text_IO;
with WL.Heaps;

package body WL.Graphs is

   ------------
   -- Append --
   ------------

   procedure Append
     (Container : in out Graph;
      Vertex    : Vertex_Type)
   is
   begin
      Container.Vertices.Append
        (Vertex_Info'
           (Vertex => Vertex,
            Index  => Container.Vertices.Last_Index + 1,
            Edges  => Edge_Lists.Empty_List));
      Container.Vs.Append (Vertex);
   end Append;

   ------------
   -- Append --
   ------------

   procedure Append
     (Sub   : in out Sub_Graph;
      Index : Index_Type)
   is
   begin
      Sub.Vertex_List.Append (Index);
      Sub.Vertex_Flags.Replace_Element (Index, True);
   end Append;

   ------------
   -- Append --
   ------------

   procedure Append
     (Collection : in out Sub_Graph_Collection;
      Sub        : Sub_Graph)
   is
   begin
      Collection.Vector.Append (Sub);
   end Append;

   ------------------------
   -- Breadth_First_Scan --
   ------------------------

   procedure Breadth_First_Scan
     (Container : Graph;
      Start     : Index_Type;
      Process   : not null access
        procedure (Path_To : Path))
   is
      package Frontier_Queues is
        new WL.Heaps
          (Key_Type     => Cost_Type,
           Element_Type => Index_Type,
           "<"          => ">");

      Sentinel : constant Extended_Index := Extended_Index'First;

      type Visited_Info is
         record
            Visited     : Boolean := False;
            Came_From   : Extended_Index := Sentinel;
            Cost_So_Far : Cost_Type := 0.0;
         end record;

      Visited  : array (1 .. Container.Vertices.Last_Index) of Visited_Info;
      Frontier : Frontier_Queues.Heap;

   begin

      Frontier.Insert (0.0, Start);
      Visited (Start).Visited := True;

      while not Frontier.Is_Empty loop
         declare
            Current      : constant Index_Type :=
                             Frontier.First_Element;
            Current_Cost : constant Cost_Type :=
                             Visited (Current).Cost_So_Far;
         begin
            Frontier.Delete_First;

            if Current /= Start then
               declare
                  P  : Path;
                  It : Extended_Index := Current;
               begin
                  while Visited (It).Came_From /= Sentinel loop

                     if P.List.Is_Empty then
                        P.List.Append (It);
                     else
                        P.List.Insert (P.List.First, It);
                     end if;

                     It := Visited (It).Came_From;
                  end loop;

                  P.Cost := Current_Cost;

                  Process (P);
               end;
            end if;

            for Edge of Container.Vertices.Element (Current).Edges loop
               declare
                  New_Cost : constant Cost_Type :=
                               Current_Cost + Edge.Cost;
                  Info     : Visited_Info renames Visited (Edge.To);
               begin
                  if not Info.Visited then
                     Frontier.Insert
                       (New_Cost, Edge.To);
                     Info.Visited := True;
                     Info.Cost_So_Far := New_Cost;
                     Info.Came_From := Current;
                  end if;
               end;
            end loop;
         end;
      end loop;

   end Breadth_First_Scan;

   --------------------------
   -- Breadth_First_Search --
   --------------------------

   function Breadth_First_Search
     (Container : Graph;
      Start     : Vertex_Type;
      Test      : not null access
        function (Vertex : Vertex_Type) return Boolean)
      return Vertex_Type
   is
      package Queue_Of_Partials is
        new Ada.Containers.Doubly_Linked_Lists (Index_Type);
      Queue : Queue_Of_Partials.List;
      Tested : Queue_Of_Partials.List;
   begin
      Queue.Append (Index_Of (Start));

      while not Queue.Is_Empty loop
         declare
            Ix   : constant Index_Type := Queue.First_Element;
         begin
            Queue.Delete_First;
            if Test (Container.Vs (Ix)) then
               return Container.Vs (Ix);
            elsif not Tested.Contains (Ix) then
               Tested.Append (Ix);
               for Edge of Container.Vertices.Element (Ix).Edges loop
                  Queue.Append (Edge.To);
               end loop;
            end if;
         end;
      end loop;

      return Start;

   end Breadth_First_Search;

   --------------------------
   -- Breadth_First_Search --
   --------------------------

   procedure Breadth_First_Search
     (Container : Graph;
      Start     : Index_Type;
      Max_Steps : Count_Type;
      Result    : out Sub_Graph)
   is
      type Partial is
         record
            Index : Index_Type;
            Steps : Count_Type;
         end record;

      package Queue_Of_Partials is
         new Ada.Containers.Doubly_Linked_Lists (Partial);
      Queue : Queue_Of_Partials.List;
   begin
      Container.Create (Result);
      Queue.Append ((Start, 0));
      while not Queue.Is_Empty loop
         declare
            use type Ada.Containers.Count_Type;
            P     : constant Partial := Queue.First_Element;
            Ix    : constant Index_Type := P.Index;
            Steps : constant Count_Type := P.Steps;
         begin
            Queue.Delete_First;
            if not Contains (Result, Ix) then
               Append (Result, Ix);
               if Steps < Max_Steps then
                  for Edge of Container.Vertices.Element (Ix).Edges loop
                     Queue.Append ((Edge.To, Steps + 1));
                  end loop;
               end if;
            end if;
         end;
      end loop;

   end Breadth_First_Search;

   --------------------------
   -- Breadth_First_Search --
   --------------------------

   procedure Breadth_First_Search
     (Container : Graph;
      Start     : Index_Type;
      Max       : Cost_Type;
      Result    : out Sub_Graph)
   is
      type Partial is
         record
            Index : Index_Type;
            Cost  : Cost_Type;
         end record;

      package Queue_Of_Partials is
         new Ada.Containers.Doubly_Linked_Lists (Partial);
      Queue : Queue_Of_Partials.List;
   begin
      Container.Create (Result);
      Queue.Append ((Start, 0.0));
      while not Queue.Is_Empty loop
         declare
            P    : constant Partial := Queue.First_Element;
            Ix   : constant Index_Type := P.Index;
            Cost : constant Cost_Type := P.Cost;
         begin
            Queue.Delete_First;
            if not Contains (Result, Ix) then
               Append (Result, Ix);
               for Edge of Container.Vertices.Element (Ix).Edges loop
                  declare
                     New_Cost : constant Cost_Type := Cost + Edge.Cost;
                  begin
                     if New_Cost <= Max then
                        Queue.Append ((Edge.To, New_Cost));
                     end if;
                  end;
               end loop;
            end if;
         end;
      end loop;

   end Breadth_First_Search;

   --------------------------
   -- Breadth_First_Search --
   --------------------------

   procedure Breadth_First_Search
     (Container : Graph;
      Start     : Index_Type;
      Test      : not null access
        function (Vertex : Vertex_Type) return Boolean;
      Result    : out Sub_Graph)
   is
      package Queue_Of_Indices is
        new Ada.Containers.Doubly_Linked_Lists (Index_Type);
      Queue : Queue_Of_Indices.List;
   begin
      Container.Create (Result);

      if not Test (Container.Vertex (Start)) then
         return;
      end if;

      Queue.Append (Start);

      while not Queue.Is_Empty loop
         declare
            Ix   : constant Index_Type := Queue.First_Element;
         begin
            Queue.Delete_First;
            if not Contains (Result, Ix) then
               Append (Result, Ix);
               for Edge of Container.Vertices.Element (Ix).Edges loop
                  if Test (Container.Vertex (Edge.To)) then
                     Queue.Append (Edge.To);
                  end if;
               end loop;
            end if;
         end;
      end loop;

   end Breadth_First_Search;

   -------------
   -- Connect --
   -------------

   procedure Connect
     (Container : in out Graph'Class;
      From, To  :        Index_Type;
      Cost      :        Cost_Type := Default_Cost)
   is
   begin
      Container.Vertices (From).Edges.Append ((To, Cost));
   end Connect;

   ---------------
   -- Connected --
   ---------------

   function Connected
     (Container    : Graph;
      From, To     : Index_Type)
      return Boolean
   is
   begin
      for Edge of Container.Vertices.Element (From).Edges loop
         if Edge.To = To then
            return True;
         end if;
      end loop;
      return False;
   end Connected;

   -------------------------
   -- Connected_Sub_Graph --
   -------------------------

   procedure Connected_Sub_Graph
     (Container : Graph;
      Start     : Vertex_Type;
      Is_Member : not null access
        function (Vertex : Vertex_Type) return Boolean;
      Result    : out Sub_Graph)
   is
      package Queue_Of_Partials is
        new Ada.Containers.Doubly_Linked_Lists (Index_Type);

      Queue : Queue_Of_Partials.List;

   begin
      Container.Create (Result);
      Queue.Append (Index_Of (Start));
      while not Queue.Is_Empty loop
         declare
            Ix   : constant Index_Type := Queue.First_Element;
         begin
            Queue.Delete_First;
            if Is_Member (Container.Vs (Ix))
              and then not Contains (Result, Ix)
            then
               Append (Result, Ix);
               for Edge of Container.Vertices.Element (Ix).Edges loop
                  Queue.Append (Edge.To);
               end loop;
            end if;
         end;
      end loop;

   end Connected_Sub_Graph;

   --------------
   -- Contains --
   --------------

   function Contains
     (Container : Graph;
      Vertex    : Vertex_Type)
      return Boolean
   is
   begin
      return Container.Index_Of (Vertex) in Index_Type'Range;
   end Contains;

   --------------
   -- Contains --
   --------------

   function Contains
     (Sub : Sub_Graph;
      Index : Index_Type)
      return Boolean
   is
   begin
      return Sub.Vertex_Flags.Element (Index);
   end Contains;

   --------------
   -- Contains --
   --------------

   function Contains
     (Collection : Sub_Graph_Collection;
      Index      : Index_Type)
      return Boolean
   is
   begin
      for Sub of Collection.Vector loop
         if Contains (Sub, Index) then
            return True;
         end if;
      end loop;
      return False;
   end Contains;

   ----------
   -- Cost --
   ----------

   function Cost (P : Path) return Cost_Type is
   begin
      return P.Cost;
   end Cost;

   ------------
   -- Create --
   ------------

   procedure Create
     (Container : Graph'Class;
      Sub       : out Sub_Graph)
   is
   begin
      Sub.Main_Graph := Container'Unchecked_Access;
      Sub.Vertex_List.Clear;
      Sub.Vertex_Flags.Clear;
      for I in 1 .. Container.Last_Vertex_Index loop
         Sub.Vertex_Flags.Append (False);
      end loop;
   end Create;

   ------------------------
   -- Depth_First_Search --
   ------------------------

   procedure Depth_First_Search
     (Container : Graph;
      Start     : Index_Type;
      Max       : Cost_Type;
      Result    : out Sub_Graph)
   is
      pragma Unreferenced (Max);
      Stack : Index_Lists.List;
   begin
      Container.Create (Result);
      Stack.Append (Start);
      while not Stack.Is_Empty loop
         declare
            Ix : constant Index_Type := Stack.First_Element;
         begin
            Stack.Delete_First;
            if not Contains (Result, Ix) then
               Append (Result, Ix);
               for Edge of Container.Vertices.Element (Ix).Edges loop
                  Stack.Insert (Stack.First, Edge.To);
               end loop;
            end if;
         end;
      end loop;

   end Depth_First_Search;

   ----------
   -- Edge --
   ----------

   function Edge
     (Container : Graph;
      From      : Vertex_Type;
      Index     : Count_Type)
      return Vertex_Type
   is
      Edges : Edge_Lists.List renames
        Container.Vertices.Element (Index_Of (From)).Edges;
      Position : Edge_Lists.Cursor := Edges.First;
   begin
      for I in 2 .. Index loop
         Edge_Lists.Next (Position);
      end loop;
      return Container.Vs (Edge_Lists.Element (Position).To);
   end Edge;

   ---------------
   -- Edge_Cost --
   ---------------

   function Edge_Cost
     (Container    : Graph;
      From, To     : Index_Type)
      return Cost_Type
   is
   begin
      for Edge of Container.Vertices.Element (From).Edges loop
         if Edge.To = To then
            return Edge.Cost;
         end if;
      end loop;
      --  can't happen because of precondition
      raise Program_Error with "invalid call to edge_cost";
   end Edge_Cost;

   ------------------------------
   -- Get_Connected_Components --
   ------------------------------

   procedure Get_Connected_Components
     (Container : Graph'Class;
      Result    : out Sub_Graph_Collection)
   is
   begin
      Result.Vector.Clear;
      for Index in 1 .. Container.Vertices.Last_Index loop
         if not Contains (Result, Index) then
            declare
               Sub : Sub_Graph;
            begin
               Container.Depth_First_Search (Index, Cost_Type'Last, Sub);
               Append (Result, Sub);
            end;
         end if;
      end loop;
   end Get_Connected_Components;

   -------------------
   -- Get_Sub_Graph --
   -------------------

   function Get_Sub_Graph
     (Collection : Sub_Graph_Collection;
      Index      : Positive)
      return Sub_Graph
   is
   begin
      return Collection.Vector (Index);
   end Get_Sub_Graph;

   --------------
   -- Index_Of --
   --------------

   function Index_Of
     (Container : Graph;
      Vertex    : Vertex_Type)
      return Extended_Index
   is
   begin
      for I in Index_Type'First .. Container.Vertices.Last_Index loop
         if Container.Vertices.Element (I).Vertex = Vertex then
            return I;
         end if;
      end loop;
      return Extended_Index'First;
   end Index_Of;

   ------------
   -- Insert --
   ------------

   procedure Insert
     (Collection : in out Sub_Graph_Collection;
      Sub        : Sub_Graph)
   is
   begin
      Collection.Vector.Append (Sub);
   end Insert;

   -------------
   -- Iterate --
   -------------

   procedure Iterate
     (Container : Graph;
      Process   : not null access procedure (Position : Cursor))
   is
      procedure Local_Process (Position : Vertex_Info_Vectors.Cursor);

      -------------------
      -- Local_Process --
      -------------------

      procedure Local_Process (Position : Vertex_Info_Vectors.Cursor) is
      begin
         Process (Cursor (Position));
      end Local_Process;

   begin
      Container.Vertices.Iterate (Local_Process'Access);
   end Iterate;

   -------------
   -- Iterate --
   -------------

   procedure Iterate
     (Sub     : Sub_Graph;
      Process : not null access procedure (Vertex : Vertex_Type))
   is
   begin
      for Index of Sub.Vertex_List loop
         Process (Sub.Main_Graph.Vs (Index));
      end loop;
   end Iterate;

   -------------------
   -- Iterate_Edges --
   -------------------

   procedure Iterate_Edges
     (Container : Graph;
      From      : Index_Type;
      Process   : not null access procedure (To : Index_Type;
                                             Cost : Cost_Type))
   is
   begin
      for Edge of Container.Vertices.Element (From).Edges loop
         Process (Edge.To, Edge.Cost);
      end loop;
   end Iterate_Edges;

   -------------------
   -- Iterate_Edges --
   -------------------

   procedure Iterate_Edges
     (Container : Graph;
      From      : Vertex_Type;
      Process   : not null access
        procedure (To : Vertex_Type;
                   Cost : Cost_Type))
   is
      procedure Internal (To : Index_Type;
                          Cost : Cost_Type);

      --------------
      -- Internal --
      --------------

      procedure Internal (To   : Index_Type;
                          Cost : Cost_Type)
      is
      begin
         Process (Container.Vertices.Element (To).Vertex, Cost);
      end Internal;

   begin
      Graph'Class (Container).Iterate_Edges (Index_Of (From), Internal'Access);
   end Iterate_Edges;

   -----------------------
   -- Last_Vertex_Index --
   -----------------------

   function Last_Vertex_Index
     (Container : Graph)
      return Extended_Index
   is
   begin
      return Container.Vertices.Last_Index;
   end Last_Vertex_Index;

   ----------
   -- Next --
   ----------

   function Next
     (Container : Graph;
      P         : Path)
      return Vertex_Type
   is
   begin
      return Container.Vs.Element
        (Index_Lists.Element (Index_Lists.Next (P.List.First)));
   end Next;

   -------------------
   -- Path_Vertices --
   -------------------

   function Path_Vertices
     (Container : Graph;
      P         : Path)
      return Array_Of_Vertices
   is
      pragma Unreferenced (Container);
      Count  : Positive := 1;
   begin
      return Result : Array_Of_Vertices (1 .. Natural (P.List.Length)) do
         for Index of P.List loop
            Result (Count) := Index;
            Count := Count + 1;
         end loop;
      end return;
   end Path_Vertices;

   --------------------
   -- Same_Sub_Graph --
   --------------------

   function Same_Sub_Graph
     (Collection : Sub_Graph_Collection;
      V1, V2     : Index_Type)
      return Boolean
   is
   begin
      for Sub_Graph of Collection.Vector loop
         if Contains (Sub_Graph, V1) then
            return Contains (Sub_Graph, V2);
         elsif Contains (Sub_Graph, V2) then
            return False;
         end if;
      end loop;
      return False;
   end Same_Sub_Graph;

   -------------------
   -- Shortest_Path --
   -------------------

   function Shortest_Path
     (Container : Graph'Class;
      From, To  : Index_Type)
      return Path
   is
      function Cost (From, To : Vertex_Type) return Cost_Type
      is (Container.Edge_Cost (Index_Of (From), Index_Of (To)));
   begin
      return Container.Shortest_Path (From, To, Cost'Access);
   end Shortest_Path;

   -------------------
   -- Shortest_Path --
   -------------------

   function Shortest_Path
     (Container : Graph'Class;
      From, To  : Index_Type;
      Test_Vertex : not null access
        function (Vertex : Vertex_Type) return Boolean)
      return Path
   is

      pragma Warnings (Off);

      function Passable
        (From, To : Vertex_Type)
         return Boolean
      is (Test_Vertex (To));

      function Cost (From, To : Vertex_Type) return Cost_Type is (1.0);
      function Estimate (From, To : Vertex_Type) return Cost_Type is (0.0);

   begin
      return Container.Shortest_Path
        (From, To, Passable'Access, Cost'Access, Estimate'Access);
   end Shortest_Path;

   -------------------
   -- Shortest_Path --
   -------------------

   function Shortest_Path
     (Container      : Graph'Class;
      Start, Finish  : Index_Type;
      Passable       : not null access
        function (From, To : Vertex_Type) return Boolean;
      Cost           : not null access
        function (From, To : Vertex_Type) return Cost_Type;
      Estimate       : not null access
        function (From, To : Vertex_Type) return Cost_Type)
      return Path
   is

      package Frontier_Queues is
        new WL.Heaps
          (Key_Type     => Cost_Type,
           Element_Type => Index_Type,
           "<"          => ">");

      Sentinel : constant Extended_Index := Extended_Index'First;

      type Visited_Info is
         record
            Visited     : Boolean := False;
            Came_From   : Extended_Index := Sentinel;
            Cost_So_Far : Cost_Type := 0.0;
            Rest_Cost   : Cost_Type := 0.0;
         end record;

      Visited : array (1 .. Container.Vertices.Last_Index) of Visited_Info;
      Frontier : Frontier_Queues.Heap;

   begin

      Frontier.Insert (0.0, Start);
      Visited (Start).Visited := True;
      Visited (Start).Rest_Cost :=
        Estimate (Container.Vs (Start), Container.Vs (Finish));

      while not Frontier.Is_Empty loop
         declare
            Current      : constant Index_Type :=
                             Frontier.First_Element;
            Current_V    : constant Vertex_Type :=
                             Container.Vs.Element (Current);
            Current_Cost : constant Cost_Type :=
                             Visited (Current).Cost_So_Far;
         begin
            Frontier.Delete_First;

            exit when Current = Finish;

            for Edge of Container.Vertices.Element (Current).Edges loop
               declare
                  Next_V : constant Vertex_Type :=
                             Container.Vs.Element (Edge.To);
               begin
                  if Passable (Current_V, Next_V) then
                     declare
                        New_Cost : constant Cost_Type :=
                                     Current_Cost + Cost (Current_V, Next_V);
                        Info     : Visited_Info renames Visited (Edge.To);
                     begin
                        if not Info.Visited
                          or else New_Cost < Info.Cost_So_Far
                        then
                           if Info.Visited then
                              Frontier.Replace
                                (New_Cost + Info.Rest_Cost, Edge.To);
                           else
                              Info.Rest_Cost :=
                                Estimate (Next_V, Container.Vs (Finish));
                              Frontier.Insert
                                (New_Cost + Info.Rest_Cost, Edge.To);
                              Info.Visited := True;
                           end if;
                           Info.Cost_So_Far := New_Cost;
                           Info.Came_From := Current;
                        end if;
                     end;
                  end if;
               end;
            end loop;
         end;
      end loop;

      declare
         Result : Path;
         It     : Index_Type := Finish;
      begin
         Result.Cost := Visited (It).Cost_So_Far;
         while Visited (It).Came_From /= Sentinel loop
            if Result.List.Is_Empty then
               Result.List.Append (It);
            else
               Result.List.Insert (Result.List.First, It);
            end if;
            It := Visited (It).Came_From;
         end loop;
         return Result;
      end;

   end Shortest_Path;

   -------------------
   -- Shortest_Path --
   -------------------

   function Shortest_Path
     (Container : Graph'Class;
      From, To  : Index_Type;
      Cost      : not null access
        function (From, To : Vertex_Type) return Cost_Type)
      return Path
   is
      function Passable (From, To : Vertex_Type) return Boolean is (True);
      function Estimate (From, To : Vertex_Type) return Cost_Type is (0.0);
   begin
      return Container.Shortest_Path
        (From, To, Passable'Access, Cost, Estimate'Access);
   end Shortest_Path;

   -------------------
   -- Shortest_Path --
   -------------------

   function Shortest_Path
     (Container : Graph'Class;
      From, To  : Index_Type;
      Cost      : not null access
        function (From, To : Vertex_Type) return Cost_Type;
      Estimate  : not null access
        function (From, To : Vertex_Type) return Cost_Type)
      return Path
   is
      function Passable (From, To : Vertex_Type) return Boolean is (True);
   begin
      return Container.Shortest_Path
        (From, To, Passable'Access, Cost, Estimate);
   end Shortest_Path;

   ---------------------
   -- Sub_Graph_Count --
   ---------------------

   function Sub_Graph_Count
     (Collection : Sub_Graph_Collection)
      return Natural
   is
   begin
      return Collection.Vector.Last_Index;
   end Sub_Graph_Count;

   ------------
   -- Vertex --
   ------------

   function Vertex
     (Container : Graph;
      Index     : Index_Type)
      return Vertex_Type
   is
   begin
      return Container.Vs (Index);
   end Vertex;

   ------------------
   -- Vertex_Count --
   ------------------

   function Vertex_Count (P : Path) return Extended_Index is
   begin
      return Extended_Index (P.List.Length);
   end Vertex_Count;

end WL.Graphs;