0
105
25
1165
22
8
17
15
1
173
75
20
10
25
1
84
83
108
108
165
138
12
2
1
Changeset was too big and was cut off... Show full diff
1 |
%!PS-Adobe-2.0 EPSF-2.0 |
|
2 |
%%Creator: LEMON, graphToEps() |
|
3 |
%%CreationDate: Sun Mar 14 09:08:34 2010 |
|
4 |
%%BoundingBox: -353 -264 559 292 |
|
5 |
%%EndComments |
|
6 |
/lb { setlinewidth setrgbcolor newpath moveto |
|
7 |
4 2 roll 1 index 1 index curveto stroke } bind def |
|
8 |
/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def |
|
9 |
/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def |
|
10 |
/sq { newpath 2 index 1 index add 2 index 2 index add moveto |
|
11 |
2 index 1 index sub 2 index 2 index add lineto |
|
12 |
2 index 1 index sub 2 index 2 index sub lineto |
|
13 |
2 index 1 index add 2 index 2 index sub lineto |
|
14 |
closepath pop pop pop} bind def |
|
15 |
/di { newpath 2 index 1 index add 2 index moveto |
|
16 |
2 index 2 index 2 index add lineto |
|
17 |
2 index 1 index sub 2 index lineto |
|
18 |
2 index 2 index 2 index sub lineto |
|
19 |
closepath pop pop pop} bind def |
|
20 |
/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill |
|
21 |
setrgbcolor 1.1 div c fill |
|
22 |
} bind def |
|
23 |
/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill |
|
24 |
setrgbcolor 1.1 div sq fill |
|
25 |
} bind def |
|
26 |
/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill |
|
27 |
setrgbcolor 1.1 div di fill |
|
28 |
} bind def |
|
29 |
/nfemale { 0 0 0 setrgbcolor 3 index 0.0909091 1.5 mul mul setlinewidth |
|
30 |
newpath 5 index 5 index moveto 5 index 5 index 5 index 3.01 mul sub |
|
31 |
lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto |
|
32 |
5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke |
|
33 |
5 index 5 index 5 index c fill |
|
34 |
setrgbcolor 1.1 div c fill |
|
35 |
} bind def |
|
36 |
/nmale { |
|
37 |
0 0 0 setrgbcolor 3 index 0.0909091 1.5 mul mul setlinewidth |
|
38 |
newpath 5 index 5 index moveto |
|
39 |
5 index 4 index 1 mul 1.5 mul add |
|
40 |
5 index 5 index 3 sqrt 1.5 mul mul add |
|
41 |
1 index 1 index lineto |
|
42 |
1 index 1 index 7 index sub moveto |
|
43 |
1 index 1 index lineto |
|
44 |
exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto |
|
45 |
stroke |
|
46 |
5 index 5 index 5 index c fill |
|
47 |
setrgbcolor 1.1 div c fill |
|
48 |
} bind def |
|
49 |
/arrl 1 def |
|
50 |
/arrw 0.3 def |
|
51 |
/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def |
|
52 |
/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def |
|
53 |
/w exch def /len exch def |
|
54 |
newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto |
|
55 |
len w sub arrl sub dx dy lrl |
|
56 |
arrw dy dx neg lrl |
|
57 |
dx arrl w add mul dy w 2 div arrw add mul sub |
|
58 |
dy arrl w add mul dx w 2 div arrw add mul add rlineto |
|
59 |
dx arrl w add mul neg dy w 2 div arrw add mul sub |
|
60 |
dy arrl w add mul neg dx w 2 div arrw add mul add rlineto |
|
61 |
arrw dy dx neg lrl |
|
62 |
len w sub arrl sub neg dx dy lrl |
|
63 |
closepath fill } bind def |
|
64 |
/cshow { 2 index 2 index moveto dup stringwidth pop |
|
65 |
neg 2 div fosi .35 mul neg rmoveto show pop pop} def |
|
66 |
|
|
67 |
gsave |
|
68 |
%Arcs: |
|
69 |
gsave |
|
70 |
140.321 266.249 -327.729 150.06 0 0 0 4.99223 l |
|
71 |
82.1207 -238.726 -245.288 -110.743 0 0 0 4.99223 l |
|
72 |
336.635 -229.036 533.603 13.109 0 0 0 4.99223 l |
|
73 |
53.8598 -45.8071 -245.288 -110.743 0 0 0 4.99223 l |
|
74 |
-75.5617 118.579 -327.729 150.06 0 0 0 4.99223 l |
|
75 |
-327.729 150.06 -245.288 -110.743 1 0 0 11.9813 l |
|
76 |
533.603 13.109 218.184 -84.2955 0 0 0 4.99223 l |
|
77 |
39.87 175.035 141.163 67.2575 0 0 0 4.99223 l |
|
78 |
53.8598 -45.8071 -75.5617 118.579 0 0 0 4.99223 l |
|
79 |
-102.406 -141.267 82.1207 -238.726 0 0 0 4.99223 l |
|
80 |
399.144 166.894 533.603 13.109 1 0 0 11.9813 l |
|
81 |
39.87 175.035 140.321 266.249 1 0 0 11.9813 l |
|
82 |
399.144 166.894 140.321 266.249 0 0 0 4.99223 l |
|
83 |
399.144 166.894 141.163 67.2575 0 0 0 4.99223 l |
|
84 |
53.8598 -45.8071 204.765 -173.77 0 0 0 4.99223 l |
|
85 |
82.1207 -238.726 204.765 -173.77 0 0 0 4.99223 l |
|
86 |
258.227 61.658 399.144 166.894 0 0 0 4.99223 l |
|
87 |
53.8598 -45.8071 -102.406 -141.267 1 0 0 11.9813 l |
|
88 |
175.073 -37.4477 141.163 67.2575 0 0 0 4.99223 l |
|
89 |
258.227 61.658 380 0 0 0 0 4.99223 l |
|
90 |
34.6739 40.8267 -75.5617 118.579 1 0 0 11.9813 l |
|
91 |
380 0 533.603 13.109 0 0 0 4.99223 l |
|
92 |
175.073 -37.4477 380 0 0 0 0 4.99223 l |
|
93 |
218.184 -84.2955 204.765 -173.77 0 0 0 4.99223 l |
|
94 |
53.8598 -45.8071 34.6739 40.8267 0 0 0 4.99223 l |
|
95 |
167.905 -213.988 82.1207 -238.726 1 0 0 11.9813 l |
|
96 |
336.635 -229.036 204.765 -173.77 1 0 0 11.9813 l |
|
97 |
336.635 -229.036 167.905 -213.988 0 0 0 4.99223 l |
|
98 |
329.08 -26.3098 218.184 -84.2955 0 0 0 4.99223 l |
|
99 |
39.87 175.035 -75.5617 118.579 0 0 0 4.99223 l |
|
100 |
53.8598 -45.8071 175.073 -37.4477 0 0 0 4.99223 l |
|
101 |
34.6739 40.8267 141.163 67.2575 0 0 0 4.99223 l |
|
102 |
258.227 61.658 141.163 67.2575 1 0 0 11.9813 l |
|
103 |
175.073 -37.4477 218.184 -84.2955 1 0 0 11.9813 l |
|
104 |
380 0 329.08 -26.3098 1 0 0 11.9813 l |
|
105 |
grestore |
|
106 |
%Nodes: |
|
107 |
gsave |
|
108 |
-245.288 -110.743 14.9767 1 1 1 nc |
|
109 |
204.765 -173.77 14.9767 1 1 1 nc |
|
110 |
-327.729 150.06 14.9767 1 1 1 nc |
|
111 |
-75.5617 118.579 14.9767 1 1 1 nc |
|
112 |
218.184 -84.2955 14.9767 1 1 1 nc |
|
113 |
140.321 266.249 14.9767 1 1 1 nc |
|
114 |
141.163 67.2575 14.9767 1 1 1 nc |
|
115 |
82.1207 -238.726 14.9767 1 1 1 nc |
|
116 |
329.08 -26.3098 14.9767 1 1 1 nc |
|
117 |
-102.406 -141.267 14.9767 1 1 1 nc |
|
118 |
533.603 13.109 14.9767 1 1 1 nc |
|
119 |
167.905 -213.988 14.9767 1 1 1 nc |
|
120 |
336.635 -229.036 14.9767 1 1 1 nc |
|
121 |
380 0 14.9767 1 1 1 nc |
|
122 |
399.144 166.894 14.9767 1 1 1 nc |
|
123 |
34.6739 40.8267 14.9767 1 1 1 nc |
|
124 |
39.87 175.035 14.9767 1 1 1 nc |
|
125 |
175.073 -37.4477 14.9767 1 1 1 nc |
|
126 |
53.8598 -45.8071 14.9767 1 1 1 nc |
|
127 |
258.227 61.658 14.9767 1 1 1 nc |
|
128 |
grestore |
|
129 |
grestore |
|
130 |
showpage |
1 |
%!PS-Adobe-2.0 EPSF-2.0 |
|
2 |
%%Creator: LEMON, graphToEps() |
|
3 |
%%CreationDate: Fri Oct 19 18:32:32 2007 |
|
4 |
%%BoundingBox: 0 0 596 842 |
|
5 |
%%DocumentPaperSizes: a4 |
|
6 |
%%EndComments |
|
7 |
/lb { setlinewidth setrgbcolor newpath moveto |
|
8 |
4 2 roll 1 index 1 index curveto stroke } bind def |
|
9 |
/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def |
|
10 |
/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def |
|
11 |
/sq { newpath 2 index 1 index add 2 index 2 index add moveto |
|
12 |
2 index 1 index sub 2 index 2 index add lineto |
|
13 |
2 index 1 index sub 2 index 2 index sub lineto |
|
14 |
2 index 1 index add 2 index 2 index sub lineto |
|
15 |
closepath pop pop pop} bind def |
|
16 |
/di { newpath 2 index 1 index add 2 index moveto |
|
17 |
2 index 2 index 2 index add lineto |
|
18 |
2 index 1 index sub 2 index lineto |
|
19 |
2 index 2 index 2 index sub lineto |
|
20 |
closepath pop pop pop} bind def |
|
21 |
/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill |
|
22 |
setrgbcolor 1.1 div c fill |
|
23 |
} bind def |
|
24 |
/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill |
|
25 |
setrgbcolor 1.1 div sq fill |
|
26 |
} bind def |
|
27 |
/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill |
|
28 |
setrgbcolor 1.1 div di fill |
|
29 |
} bind def |
|
30 |
/nfemale { 0 0 0 setrgbcolor 3 index 0.0909091 1.5 mul mul setlinewidth |
|
31 |
newpath 5 index 5 index moveto 5 index 5 index 5 index 3.01 mul sub |
|
32 |
lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto |
|
33 |
5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke |
|
34 |
5 index 5 index 5 index c fill |
|
35 |
setrgbcolor 1.1 div c fill |
|
36 |
} bind def |
|
37 |
/nmale { |
|
38 |
0 0 0 setrgbcolor 3 index 0.0909091 1.5 mul mul setlinewidth |
|
39 |
newpath 5 index 5 index moveto |
|
40 |
5 index 4 index 1 mul 1.5 mul add |
|
41 |
5 index 5 index 3 sqrt 1.5 mul mul add |
|
42 |
1 index 1 index lineto |
|
43 |
1 index 1 index 7 index sub moveto |
|
44 |
1 index 1 index lineto |
|
45 |
exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto |
|
46 |
stroke |
|
47 |
5 index 5 index 5 index c fill |
|
48 |
setrgbcolor 1.1 div c fill |
|
49 |
} bind def |
|
50 |
/arrl 1 def |
|
51 |
/arrw 0.3 def |
|
52 |
/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def |
|
53 |
/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def |
|
54 |
/w exch def /len exch def |
|
55 |
newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto |
|
56 |
len w sub arrl sub dx dy lrl |
|
57 |
arrw dy dx neg lrl |
|
58 |
dx arrl w add mul dy w 2 div arrw add mul sub |
|
59 |
dy arrl w add mul dx w 2 div arrw add mul add rlineto |
|
60 |
dx arrl w add mul neg dy w 2 div arrw add mul sub |
|
61 |
dy arrl w add mul neg dx w 2 div arrw add mul add rlineto |
|
62 |
arrw dy dx neg lrl |
|
63 |
len w sub arrl sub neg dx dy lrl |
|
64 |
closepath fill } bind def |
|
65 |
/cshow { 2 index 2 index moveto dup stringwidth pop |
|
66 |
neg 2 div fosi .35 mul neg rmoveto show pop pop} def |
|
67 |
|
|
68 |
gsave |
|
69 |
15 138.307 translate |
|
70 |
12.7843 dup scale |
|
71 |
90 rotate |
|
72 |
0.608112 -43.6081 translate |
|
73 |
%Edges: |
|
74 |
gsave |
|
75 |
9 31 9.5 30.5 10 30 0 0 0 0.091217 lb |
|
76 |
9 31 5.5 34.5 2 38 0 0 0 0.091217 lb |
|
77 |
9 31 25.5 16 42 1 0 0 0 0.091217 lb |
|
78 |
3 40 23 20.5 43 1 0 0 0 0.091217 lb |
|
79 |
3 40 22.5 20.5 42 1 0 0 0 0.091217 lb |
|
80 |
3 40 2.5 40.5 2 41 0 0 0 0.091217 lb |
|
81 |
13 25 10.5 24.5 8 24 0 0 0 0.091217 lb |
|
82 |
13 25 12 27 11 29 0 0 0 0.091217 lb |
|
83 |
3 4 2.5 3 2 2 0 0 0 0.091217 lb |
|
84 |
3 4 4.5 3 6 2 0 0 0 0.091217 lb |
|
85 |
6 25 7 24.5 8 24 0 0 0 0.091217 lb |
|
86 |
6 25 6 24.5 6 24 0 0 0 0.091217 lb |
|
87 |
34 2 33.5 2 33 2 0 0 0 0.091217 lb |
|
88 |
34 2 35 2 36 2 0 0 0 0.091217 lb |
|
89 |
6 8 16 9 26 10 0 0 0 0.091217 lb |
|
90 |
6 8 6 10.5 6 13 0 0 0 0.091217 lb |
|
91 |
6 8 6 7.5 6 7 0 0 0 0.091217 lb |
|
92 |
26 10 27.5 8.5 29 7 0 0 0 0.091217 lb |
|
93 |
26 10 27.5 9 29 8 0 0 0 0.091217 lb |
|
94 |
10 30 10.5 29.5 11 29 0 0 0 0.091217 lb |
|
95 |
8 24 7 23.5 6 23 0 0 0 0.091217 lb |
|
96 |
8 24 8 24.5 8 25 0 0 0 0.091217 lb |
|
97 |
33 2 32.5 2 32 2 0 0 0 0.091217 lb |
|
98 |
29 7 17.5 7 6 7 0 0 0 0.091217 lb |
|
99 |
2 2 1.5 22 1 42 0 0 0 0.091217 lb |
|
100 |
2 2 3.5 2 5 2 0 0 0 0.091217 lb |
|
101 |
21 15 13.5 14.5 6 14 0 0 0 0.091217 lb |
|
102 |
21 15 21 15.5 21 16 0 0 0 0.091217 lb |
|
103 |
1 42 0.5 42.5 0 43 0 0 0 0.091217 lb |
|
104 |
1 42 1.5 41.5 2 41 0 0 0 0.091217 lb |
|
105 |
6 15 6 15.5 6 16 0 0 0 0.091217 lb |
|
106 |
6 15 6 14.5 6 14 0 0 0 0.091217 lb |
|
107 |
43 1 22 0.5 1 0 0 0 0 0.091217 lb |
|
108 |
31 2 18.5 2 6 2 0 0 0 0.091217 lb |
|
109 |
31 2 31.5 2 32 2 0 0 0 0.091217 lb |
|
110 |
6 24 6 23.5 6 23 0 0 0 0.091217 lb |
|
111 |
6 16 6 16.5 6 17 0 0 0 0.091217 lb |
|
112 |
6 23 6 20 6 17 0 0 0 0.091217 lb |
|
113 |
6 2 5.5 2 5 2 0 0 0 0.091217 lb |
|
114 |
6 2 6 4.5 6 7 0 0 0 0.091217 lb |
|
115 |
0 43 0.5 21.5 1 0 0 0 0 0.091217 lb |
|
116 |
1 1 19.5 1.5 38 2 0 0 0 0.091217 lb |
|
117 |
1 1 1 0.5 1 0 0 0 0 0.091217 lb |
|
118 |
2 38 5.5 31.5 9 25 0 0 0 0.091217 lb |
|
119 |
25 13 15.5 13 6 13 0 0 0 0.091217 lb |
|
120 |
25 13 15.5 13.5 6 14 0 0 0 0.091217 lb |
|
121 |
8 25 8.5 25 9 25 0 0 0 0.091217 lb |
|
122 |
11 29 24.5 15.5 38 2 0 0 0 0.091217 lb |
|
123 |
6 17 11.5 18 17 19 0 0 0 0.091217 lb |
|
124 |
16 23 26.5 12.5 37 2 0 0 0 0.091217 lb |
|
125 |
16 23 18.5 19.5 21 16 0 0 0 0.091217 lb |
|
126 |
36 2 36.5 2 37 2 0 0 0 0.091217 lb |
|
127 |
36 2 32.5 5 29 8 0 0 0 0.091217 lb |
|
128 |
6 13 6 13.5 6 14 0 0 0 0.091217 lb |
|
129 |
37 2 37.5 2 38 2 0 0 0 0.091217 lb |
|
130 |
21 16 19 17.5 17 19 0 0 0 0.091217 lb |
|
131 |
grestore |
|
132 |
%Nodes: |
|
133 |
gsave |
|
134 |
29 8 0.304556 1 1 1 nc |
|
135 |
2 41 0.304556 1 1 1 nc |
|
136 |
6 7 0.304556 1 1 1 nc |
|
137 |
5 2 0.304556 1 1 1 nc |
|
138 |
17 19 0.304556 1 1 1 nc |
|
139 |
21 16 0.304556 1 1 1 nc |
|
140 |
1 0 0.304556 1 1 1 nc |
|
141 |
9 25 0.304556 1 1 1 nc |
|
142 |
6 14 0.304556 1 1 1 nc |
|
143 |
42 1 0.304556 1 1 1 nc |
|
144 |
38 2 0.304556 1 1 1 nc |
|
145 |
37 2 0.304556 1 1 1 nc |
|
146 |
6 13 0.304556 1 1 1 nc |
|
147 |
36 2 0.304556 1 1 1 nc |
|
148 |
16 23 0.304556 1 1 1 nc |
|
149 |
6 17 0.304556 1 1 1 nc |
|
150 |
11 29 0.304556 1 1 1 nc |
|
151 |
8 25 0.304556 1 1 1 nc |
|
152 |
32 2 0.304556 1 1 1 nc |
|
153 |
25 13 0.304556 1 1 1 nc |
|
154 |
2 38 0.304556 1 1 1 nc |
|
155 |
1 1 0.304556 1 1 1 nc |
|
156 |
0 43 0.304556 1 1 1 nc |
|
157 |
6 2 0.304556 1 1 1 nc |
|
158 |
6 23 0.304556 1 1 1 nc |
|
159 |
6 16 0.304556 1 1 1 nc |
|
160 |
6 24 0.304556 1 1 1 nc |
|
161 |
31 2 0.304556 1 1 1 nc |
|
162 |
43 1 0.304556 1 1 1 nc |
|
163 |
6 15 0.304556 1 1 1 nc |
|
164 |
1 42 0.304556 1 1 1 nc |
|
165 |
21 15 0.304556 1 1 1 nc |
|
166 |
2 2 0.304556 1 1 1 nc |
|
167 |
29 7 0.304556 1 1 1 nc |
|
168 |
33 2 0.304556 1 1 1 nc |
|
169 |
8 24 0.304556 1 1 1 nc |
|
170 |
10 30 0.304556 1 1 1 nc |
|
171 |
26 10 0.304556 1 1 1 nc |
|
172 |
6 8 0.304556 1 1 1 nc |
|
173 |
34 2 0.304556 1 1 1 nc |
|
174 |
6 25 0.304556 1 1 1 nc |
|
175 |
3 4 0.304556 1 1 1 nc |
|
176 |
13 25 0.304556 1 1 1 nc |
|
177 |
3 40 0.304556 1 1 1 nc |
|
178 |
9 31 0.304556 1 1 1 nc |
|
179 |
grestore |
|
180 |
grestore |
|
181 |
showpage |
1 |
%%%%% Defining LEMON %%%%% |
|
2 |
|
|
3 |
@misc{lemon, |
|
4 |
key = {LEMON}, |
|
5 |
title = {{LEMON} -- {L}ibrary for {E}fficient {M}odeling and |
|
6 |
{O}ptimization in {N}etworks}, |
|
7 |
howpublished = {\url{http://lemon.cs.elte.hu/}}, |
|
8 |
year = 2009 |
|
9 |
} |
|
10 |
|
|
11 |
@misc{egres, |
|
12 |
key = {EGRES}, |
|
13 |
title = {{EGRES} -- {E}gerv{\'a}ry {R}esearch {G}roup on |
|
14 |
{C}ombinatorial {O}ptimization}, |
|
15 |
url = {http://www.cs.elte.hu/egres/} |
|
16 |
} |
|
17 |
|
|
18 |
@misc{coinor, |
|
19 |
key = {COIN-OR}, |
|
20 |
title = {{COIN-OR} -- {C}omputational {I}nfrastructure for |
|
21 |
{O}perations {R}esearch}, |
|
22 |
url = {http://www.coin-or.org/} |
|
23 |
} |
|
24 |
|
|
25 |
|
|
26 |
%%%%% Other libraries %%%%%% |
|
27 |
|
|
28 |
@misc{boost, |
|
29 |
key = {Boost}, |
|
30 |
title = {{B}oost {C++} {L}ibraries}, |
|
31 |
url = {http://www.boost.org/} |
|
32 |
} |
|
33 |
|
|
34 |
@book{bglbook, |
|
35 |
author = {Jeremy G. Siek and Lee-Quan Lee and Andrew |
|
36 |
Lumsdaine}, |
|
37 |
title = {The Boost Graph Library: User Guide and Reference |
|
38 |
Manual}, |
|
39 |
publisher = {Addison-Wesley}, |
|
40 |
year = 2002 |
|
41 |
} |
|
42 |
|
|
43 |
@misc{leda, |
|
44 |
key = {LEDA}, |
|
45 |
title = {{LEDA} -- {L}ibrary of {E}fficient {D}ata {T}ypes and |
|
46 |
{A}lgorithms}, |
|
47 |
url = {http://www.algorithmic-solutions.com/} |
|
48 |
} |
|
49 |
|
|
50 |
@book{ledabook, |
|
51 |
author = {Kurt Mehlhorn and Stefan N{\"a}her}, |
|
52 |
title = {{LEDA}: {A} platform for combinatorial and geometric |
|
53 |
computing}, |
|
54 |
isbn = {0-521-56329-1}, |
|
55 |
publisher = {Cambridge University Press}, |
|
56 |
address = {New York, NY, USA}, |
|
57 |
year = 1999 |
|
58 |
} |
|
59 |
|
|
60 |
|
|
61 |
%%%%% Tools that LEMON depends on %%%%% |
|
62 |
|
|
63 |
@misc{cmake, |
|
64 |
key = {CMake}, |
|
65 |
title = {{CMake} -- {C}ross {P}latform {M}ake}, |
|
66 |
url = {http://www.cmake.org/} |
|
67 |
} |
|
68 |
|
|
69 |
@misc{doxygen, |
|
70 |
key = {Doxygen}, |
|
71 |
title = {{Doxygen} -- {S}ource code documentation generator |
|
72 |
tool}, |
|
73 |
url = {http://www.doxygen.org/} |
|
74 |
} |
|
75 |
|
|
76 |
|
|
77 |
%%%%% LP/MIP libraries %%%%% |
|
78 |
|
|
79 |
@misc{glpk, |
|
80 |
key = {GLPK}, |
|
81 |
title = {{GLPK} -- {GNU} {L}inear {P}rogramming {K}it}, |
|
82 |
url = {http://www.gnu.org/software/glpk/} |
|
83 |
} |
|
84 |
|
|
85 |
@misc{clp, |
|
86 |
key = {Clp}, |
|
87 |
title = {{Clp} -- {Coin-Or} {L}inear {P}rogramming}, |
|
88 |
url = {http://projects.coin-or.org/Clp/} |
|
89 |
} |
|
90 |
|
|
91 |
@misc{cbc, |
|
92 |
key = {Cbc}, |
|
93 |
title = {{Cbc} -- {Coin-Or} {B}ranch and {C}ut}, |
|
94 |
url = {http://projects.coin-or.org/Cbc/} |
|
95 |
} |
|
96 |
|
|
97 |
@misc{cplex, |
|
98 |
key = {CPLEX}, |
|
99 |
title = {{ILOG} {CPLEX}}, |
|
100 |
url = {http://www.ilog.com/} |
|
101 |
} |
|
102 |
|
|
103 |
@misc{soplex, |
|
104 |
key = {SoPlex}, |
|
105 |
title = {{SoPlex} -- {T}he {S}equential {O}bject-{O}riented |
|
106 |
{S}implex}, |
|
107 |
url = {http://soplex.zib.de/} |
|
108 |
} |
|
109 |
|
|
110 |
|
|
111 |
%%%%% General books %%%%% |
|
112 |
|
|
113 |
@book{amo93networkflows, |
|
114 |
author = {Ravindra K. Ahuja and Thomas L. Magnanti and James |
|
115 |
B. Orlin}, |
|
116 |
title = {Network Flows: Theory, Algorithms, and Applications}, |
|
117 |
publisher = {Prentice-Hall, Inc.}, |
|
118 |
year = 1993, |
|
119 |
month = feb, |
|
120 |
isbn = {978-0136175490} |
|
121 |
} |
|
122 |
|
|
123 |
@book{schrijver03combinatorial, |
|
124 |
author = {Alexander Schrijver}, |
|
125 |
title = {Combinatorial Optimization: Polyhedra and Efficiency}, |
|
126 |
publisher = {Springer-Verlag}, |
|
127 |
year = 2003, |
|
128 |
isbn = {978-3540443896} |
|
129 |
} |
|
130 |
|
|
131 |
@book{clrs01algorithms, |
|
132 |
author = {Thomas H. Cormen and Charles E. Leiserson and Ronald |
|
133 |
L. Rivest and Clifford Stein}, |
|
134 |
title = {Introduction to Algorithms}, |
|
135 |
publisher = {The MIT Press}, |
|
136 |
year = 2001, |
|
137 |
edition = {2nd} |
|
138 |
} |
|
139 |
|
|
140 |
@book{stroustrup00cpp, |
|
141 |
author = {Bjarne Stroustrup}, |
|
142 |
title = {The C++ Programming Language}, |
|
143 |
edition = {3rd}, |
|
144 |
publisher = {Addison-Wesley Professional}, |
|
145 |
isbn = 0201700735, |
|
146 |
month = {February}, |
|
147 |
year = 2000 |
|
148 |
} |
|
149 |
|
|
150 |
|
|
151 |
%%%%% Maximum flow algorithms %%%%% |
|
152 |
|
|
153 |
@article{edmondskarp72theoretical, |
|
154 |
author = {Jack Edmonds and Richard M. Karp}, |
|
155 |
title = {Theoretical improvements in algorithmic efficiency |
|
156 |
for network flow problems}, |
|
157 |
journal = {Journal of the ACM}, |
|
158 |
year = 1972, |
|
159 |
volume = 19, |
|
160 |
number = 2, |
|
161 |
pages = {248-264} |
|
162 |
} |
|
163 |
|
|
164 |
@article{goldberg88newapproach, |
|
165 |
author = {Andrew V. Goldberg and Robert E. Tarjan}, |
|
166 |
title = {A new approach to the maximum flow problem}, |
|
167 |
journal = {Journal of the ACM}, |
|
168 |
year = 1988, |
|
169 |
volume = 35, |
|
170 |
number = 4, |
|
171 |
pages = {921-940} |
|
172 |
} |
|
173 |
|
|
174 |
@article{dinic70algorithm, |
|
175 |
author = {E. A. Dinic}, |
|
176 |
title = {Algorithm for solution of a problem of maximum flow |
|
177 |
in a network with power estimation}, |
|
178 |
journal = {Soviet Math. Doklady}, |
|
179 |
year = 1970, |
|
180 |
volume = 11, |
|
181 |
pages = {1277-1280} |
|
182 |
} |
|
183 |
|
|
184 |
@article{goldberg08partial, |
|
185 |
author = {Andrew V. Goldberg}, |
|
186 |
title = {The Partial Augment-Relabel Algorithm for the |
|
187 |
Maximum Flow Problem}, |
|
188 |
journal = {16th Annual European Symposium on Algorithms}, |
|
189 |
year = 2008, |
|
190 |
pages = {466-477} |
|
191 |
} |
|
192 |
|
|
193 |
@article{sleator83dynamic, |
|
194 |
author = {Daniel D. Sleator and Robert E. Tarjan}, |
|
195 |
title = {A data structure for dynamic trees}, |
|
196 |
journal = {Journal of Computer and System Sciences}, |
|
197 |
year = 1983, |
|
198 |
volume = 26, |
|
199 |
number = 3, |
|
200 |
pages = {362-391} |
|
201 |
} |
|
202 |
|
|
203 |
|
|
204 |
%%%%% Minimum mean cycle algorithms %%%%% |
|
205 |
|
|
206 |
@article{karp78characterization, |
|
207 |
author = {Richard M. Karp}, |
|
208 |
title = {A characterization of the minimum cycle mean in a |
|
209 |
digraph}, |
|
210 |
journal = {Discrete Math.}, |
|
211 |
year = 1978, |
|
212 |
volume = 23, |
|
213 |
pages = {309-311} |
|
214 |
} |
|
215 |
|
|
216 |
@article{dasdan98minmeancycle, |
|
217 |
author = {Ali Dasdan and Rajesh K. Gupta}, |
|
218 |
title = {Faster Maximum and Minimum Mean Cycle Alogrithms for |
|
219 |
System Performance Analysis}, |
|
220 |
journal = {IEEE Transactions on Computer-Aided Design of |
|
221 |
Integrated Circuits and Systems}, |
|
222 |
year = 1998, |
|
223 |
volume = 17, |
|
224 |
number = 10, |
|
225 |
pages = {889-899} |
|
226 |
} |
|
227 |
|
|
228 |
|
|
229 |
%%%%% Minimum cost flow algorithms %%%%% |
|
230 |
|
|
231 |
@article{klein67primal, |
|
232 |
author = {Morton Klein}, |
|
233 |
title = {A primal method for minimal cost flows with |
|
234 |
applications to the assignment and transportation |
|
235 |
problems}, |
|
236 |
journal = {Management Science}, |
|
237 |
year = 1967, |
|
238 |
volume = 14, |
|
239 |
pages = {205-220} |
|
240 |
} |
|
241 |
|
|
242 |
@article{goldberg89cyclecanceling, |
|
243 |
author = {Andrew V. Goldberg and Robert E. Tarjan}, |
|
244 |
title = {Finding minimum-cost circulations by canceling |
|
245 |
negative cycles}, |
|
246 |
journal = {Journal of the ACM}, |
|
247 |
year = 1989, |
|
248 |
volume = 36, |
|
249 |
number = 4, |
|
250 |
pages = {873-886} |
|
251 |
} |
|
252 |
|
|
253 |
@article{goldberg90approximation, |
|
254 |
author = {Andrew V. Goldberg and Robert E. Tarjan}, |
|
255 |
title = {Finding Minimum-Cost Circulations by Successive |
|
256 |
Approximation}, |
|
257 |
journal = {Mathematics of Operations Research}, |
|
258 |
year = 1990, |
|
259 |
volume = 15, |
|
260 |
number = 3, |
|
261 |
pages = {430-466} |
|
262 |
} |
|
263 |
|
|
264 |
@article{goldberg97efficient, |
|
265 |
author = {Andrew V. Goldberg}, |
|
266 |
title = {An Efficient Implementation of a Scaling |
|
267 |
Minimum-Cost Flow Algorithm}, |
|
268 |
journal = {Journal of Algorithms}, |
|
269 |
year = 1997, |
|
270 |
volume = 22, |
|
271 |
number = 1, |
|
272 |
pages = {1-29} |
|
273 |
} |
|
274 |
|
|
275 |
@article{bunnagel98efficient, |
|
276 |
author = {Ursula B{\"u}nnagel and Bernhard Korte and Jens |
|
277 |
Vygen}, |
|
278 |
title = {Efficient implementation of the {G}oldberg-{T}arjan |
|
279 |
minimum-cost flow algorithm}, |
|
280 |
journal = {Optimization Methods and Software}, |
|
281 |
year = 1998, |
|
282 |
volume = 10, |
|
283 |
pages = {157-174} |
|
284 |
} |
|
285 |
|
|
286 |
@book{dantzig63linearprog, |
|
287 |
author = {George B. Dantzig}, |
|
288 |
title = {Linear Programming and Extensions}, |
|
289 |
publisher = {Princeton University Press}, |
|
290 |
year = 1963 |
|
291 |
} |
|
292 |
|
|
293 |
@mastersthesis{kellyoneill91netsimplex, |
|
294 |
author = {Damian J. Kelly and Garrett M. O'Neill}, |
|
295 |
title = {The Minimum Cost Flow Problem and The Network |
|
296 |
Simplex Method}, |
|
297 |
school = {University College}, |
|
298 |
address = {Dublin, Ireland}, |
|
299 |
year = 1991, |
|
300 |
month = sep, |
|
301 |
} |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2010 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BELLMAN_FORD_H |
|
20 |
#define LEMON_BELLMAN_FORD_H |
|
21 |
|
|
22 |
/// \ingroup shortest_path |
|
23 |
/// \file |
|
24 |
/// \brief Bellman-Ford algorithm. |
|
25 |
|
|
26 |
#include <lemon/list_graph.h> |
|
27 |
#include <lemon/bits/path_dump.h> |
|
28 |
#include <lemon/core.h> |
|
29 |
#include <lemon/error.h> |
|
30 |
#include <lemon/maps.h> |
|
31 |
#include <lemon/tolerance.h> |
|
32 |
#include <lemon/path.h> |
|
33 |
|
|
34 |
#include <limits> |
|
35 |
|
|
36 |
namespace lemon { |
|
37 |
|
|
38 |
/// \brief Default operation traits for the BellmanFord algorithm class. |
|
39 |
/// |
|
40 |
/// This operation traits class defines all computational operations |
|
41 |
/// and constants that are used in the Bellman-Ford algorithm. |
|
42 |
/// The default implementation is based on the \c numeric_limits class. |
|
43 |
/// If the numeric type does not have infinity value, then the maximum |
|
44 |
/// value is used as extremal infinity value. |
|
45 |
/// |
|
46 |
/// \see BellmanFordToleranceOperationTraits |
|
47 |
template < |
|
48 |
typename V, |
|
49 |
bool has_inf = std::numeric_limits<V>::has_infinity> |
|
50 |
struct BellmanFordDefaultOperationTraits { |
|
51 |
/// \brief Value type for the algorithm. |
|
52 |
typedef V Value; |
|
53 |
/// \brief Gives back the zero value of the type. |
|
54 |
static Value zero() { |
|
55 |
return static_cast<Value>(0); |
|
56 |
} |
|
57 |
/// \brief Gives back the positive infinity value of the type. |
|
58 |
static Value infinity() { |
|
59 |
return std::numeric_limits<Value>::infinity(); |
|
60 |
} |
|
61 |
/// \brief Gives back the sum of the given two elements. |
|
62 |
static Value plus(const Value& left, const Value& right) { |
|
63 |
return left + right; |
|
64 |
} |
|
65 |
/// \brief Gives back \c true only if the first value is less than |
|
66 |
/// the second. |
|
67 |
static bool less(const Value& left, const Value& right) { |
|
68 |
return left < right; |
|
69 |
} |
|
70 |
}; |
|
71 |
|
|
72 |
template <typename V> |
|
73 |
struct BellmanFordDefaultOperationTraits<V, false> { |
|
74 |
typedef V Value; |
|
75 |
static Value zero() { |
|
76 |
return static_cast<Value>(0); |
|
77 |
} |
|
78 |
static Value infinity() { |
|
79 |
return std::numeric_limits<Value>::max(); |
|
80 |
} |
|
81 |
static Value plus(const Value& left, const Value& right) { |
|
82 |
if (left == infinity() || right == infinity()) return infinity(); |
|
83 |
return left + right; |
|
84 |
} |
|
85 |
static bool less(const Value& left, const Value& right) { |
|
86 |
return left < right; |
|
87 |
} |
|
88 |
}; |
|
89 |
|
|
90 |
/// \brief Operation traits for the BellmanFord algorithm class |
|
91 |
/// using tolerance. |
|
92 |
/// |
|
93 |
/// This operation traits class defines all computational operations |
|
94 |
/// and constants that are used in the Bellman-Ford algorithm. |
|
95 |
/// The only difference between this implementation and |
|
96 |
/// \ref BellmanFordDefaultOperationTraits is that this class uses |
|
97 |
/// the \ref Tolerance "tolerance technique" in its \ref less() |
|
98 |
/// function. |
|
99 |
/// |
|
100 |
/// \tparam V The value type. |
|
101 |
/// \tparam eps The epsilon value for the \ref less() function. |
|
102 |
/// By default, it is the epsilon value used by \ref Tolerance |
|
103 |
/// "Tolerance<V>". |
|
104 |
/// |
|
105 |
/// \see BellmanFordDefaultOperationTraits |
|
106 |
#ifdef DOXYGEN |
|
107 |
template <typename V, V eps> |
|
108 |
#else |
|
109 |
template < |
|
110 |
typename V, |
|
111 |
V eps = Tolerance<V>::def_epsilon> |
|
112 |
#endif |
|
113 |
struct BellmanFordToleranceOperationTraits { |
|
114 |
/// \brief Value type for the algorithm. |
|
115 |
typedef V Value; |
|
116 |
/// \brief Gives back the zero value of the type. |
|
117 |
static Value zero() { |
|
118 |
return static_cast<Value>(0); |
|
119 |
} |
|
120 |
/// \brief Gives back the positive infinity value of the type. |
|
121 |
static Value infinity() { |
|
122 |
return std::numeric_limits<Value>::infinity(); |
|
123 |
} |
|
124 |
/// \brief Gives back the sum of the given two elements. |
|
125 |
static Value plus(const Value& left, const Value& right) { |
|
126 |
return left + right; |
|
127 |
} |
|
128 |
/// \brief Gives back \c true only if the first value is less than |
|
129 |
/// the second. |
|
130 |
static bool less(const Value& left, const Value& right) { |
|
131 |
return left + eps < right; |
|
132 |
} |
|
133 |
}; |
|
134 |
|
|
135 |
/// \brief Default traits class of BellmanFord class. |
|
136 |
/// |
|
137 |
/// Default traits class of BellmanFord class. |
|
138 |
/// \param GR The type of the digraph. |
|
139 |
/// \param LEN The type of the length map. |
|
140 |
template<typename GR, typename LEN> |
|
141 |
struct BellmanFordDefaultTraits { |
|
142 |
/// The type of the digraph the algorithm runs on. |
|
143 |
typedef GR Digraph; |
|
144 |
|
|
145 |
/// \brief The type of the map that stores the arc lengths. |
|
146 |
/// |
|
147 |
/// The type of the map that stores the arc lengths. |
|
148 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
|
149 |
typedef LEN LengthMap; |
|
150 |
|
|
151 |
/// The type of the arc lengths. |
|
152 |
typedef typename LEN::Value Value; |
|
153 |
|
|
154 |
/// \brief Operation traits for Bellman-Ford algorithm. |
|
155 |
/// |
|
156 |
/// It defines the used operations and the infinity value for the |
|
157 |
/// given \c Value type. |
|
158 |
/// \see BellmanFordDefaultOperationTraits, |
|
159 |
/// BellmanFordToleranceOperationTraits |
|
160 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
|
161 |
|
|
162 |
/// \brief The type of the map that stores the last arcs of the |
|
163 |
/// shortest paths. |
|
164 |
/// |
|
165 |
/// The type of the map that stores the last |
|
166 |
/// arcs of the shortest paths. |
|
167 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
168 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
|
169 |
|
|
170 |
/// \brief Instantiates a \c PredMap. |
|
171 |
/// |
|
172 |
/// This function instantiates a \ref PredMap. |
|
173 |
/// \param g is the digraph to which we would like to define the |
|
174 |
/// \ref PredMap. |
|
175 |
static PredMap *createPredMap(const GR& g) { |
|
176 |
return new PredMap(g); |
|
177 |
} |
|
178 |
|
|
179 |
/// \brief The type of the map that stores the distances of the nodes. |
|
180 |
/// |
|
181 |
/// The type of the map that stores the distances of the nodes. |
|
182 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
183 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
|
184 |
|
|
185 |
/// \brief Instantiates a \c DistMap. |
|
186 |
/// |
|
187 |
/// This function instantiates a \ref DistMap. |
|
188 |
/// \param g is the digraph to which we would like to define the |
|
189 |
/// \ref DistMap. |
|
190 |
static DistMap *createDistMap(const GR& g) { |
|
191 |
return new DistMap(g); |
|
192 |
} |
|
193 |
|
|
194 |
}; |
|
195 |
|
|
196 |
/// \brief %BellmanFord algorithm class. |
|
197 |
/// |
|
198 |
/// \ingroup shortest_path |
|
199 |
/// This class provides an efficient implementation of the Bellman-Ford |
|
200 |
/// algorithm. The maximum time complexity of the algorithm is |
|
201 |
/// <tt>O(ne)</tt>. |
|
202 |
/// |
|
203 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
|
204 |
/// problem when the arcs can have negative lengths, but the digraph |
|
205 |
/// should not contain directed cycles with negative total length. |
|
206 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
|
207 |
/// algorithm instead, since it is more efficient. |
|
208 |
/// |
|
209 |
/// The arc lengths are passed to the algorithm using a |
|
210 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
|
211 |
/// kind of length. The type of the length values is determined by the |
|
212 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
|
213 |
/// |
|
214 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
|
215 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
|
216 |
/// it can be used easier. |
|
217 |
/// |
|
218 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
219 |
/// The default type is \ref ListDigraph. |
|
220 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
|
221 |
/// the lengths of the arcs. The default map type is |
|
222 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
|
223 |
/// \tparam TR The traits class that defines various types used by the |
|
224 |
/// algorithm. By default, it is \ref BellmanFordDefaultTraits |
|
225 |
/// "BellmanFordDefaultTraits<GR, LEN>". |
|
226 |
/// In most cases, this parameter should not be set directly, |
|
227 |
/// consider to use the named template parameters instead. |
|
228 |
#ifdef DOXYGEN |
|
229 |
template <typename GR, typename LEN, typename TR> |
|
230 |
#else |
|
231 |
template <typename GR=ListDigraph, |
|
232 |
typename LEN=typename GR::template ArcMap<int>, |
|
233 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
|
234 |
#endif |
|
235 |
class BellmanFord { |
|
236 |
public: |
|
237 |
|
|
238 |
///The type of the underlying digraph. |
|
239 |
typedef typename TR::Digraph Digraph; |
|
240 |
|
|
241 |
/// \brief The type of the arc lengths. |
|
242 |
typedef typename TR::LengthMap::Value Value; |
|
243 |
/// \brief The type of the map that stores the arc lengths. |
|
244 |
typedef typename TR::LengthMap LengthMap; |
|
245 |
/// \brief The type of the map that stores the last |
|
246 |
/// arcs of the shortest paths. |
|
247 |
typedef typename TR::PredMap PredMap; |
|
248 |
/// \brief The type of the map that stores the distances of the nodes. |
|
249 |
typedef typename TR::DistMap DistMap; |
|
250 |
/// The type of the paths. |
|
251 |
typedef PredMapPath<Digraph, PredMap> Path; |
|
252 |
///\brief The \ref BellmanFordDefaultOperationTraits |
|
253 |
/// "operation traits class" of the algorithm. |
|
254 |
typedef typename TR::OperationTraits OperationTraits; |
|
255 |
|
|
256 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
|
257 |
typedef TR Traits; |
|
258 |
|
|
259 |
private: |
|
260 |
|
|
261 |
typedef typename Digraph::Node Node; |
|
262 |
typedef typename Digraph::NodeIt NodeIt; |
|
263 |
typedef typename Digraph::Arc Arc; |
|
264 |
typedef typename Digraph::OutArcIt OutArcIt; |
|
265 |
|
|
266 |
// Pointer to the underlying digraph. |
|
267 |
const Digraph *_gr; |
|
268 |
// Pointer to the length map |
|
269 |
const LengthMap *_length; |
|
270 |
// Pointer to the map of predecessors arcs. |
|
271 |
PredMap *_pred; |
|
272 |
// Indicates if _pred is locally allocated (true) or not. |
|
273 |
bool _local_pred; |
|
274 |
// Pointer to the map of distances. |
|
275 |
DistMap *_dist; |
|
276 |
// Indicates if _dist is locally allocated (true) or not. |
|
277 |
bool _local_dist; |
|
278 |
|
|
279 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
|
280 |
MaskMap *_mask; |
|
281 |
|
|
282 |
std::vector<Node> _process; |
|
283 |
|
|
284 |
// Creates the maps if necessary. |
|
285 |
void create_maps() { |
|
286 |
if(!_pred) { |
|
287 |
_local_pred = true; |
|
288 |
_pred = Traits::createPredMap(*_gr); |
|
289 |
} |
|
290 |
if(!_dist) { |
|
291 |
_local_dist = true; |
|
292 |
_dist = Traits::createDistMap(*_gr); |
|
293 |
} |
|
294 |
if(!_mask) { |
|
295 |
_mask = new MaskMap(*_gr); |
|
296 |
} |
|
297 |
} |
|
298 |
|
|
299 |
public : |
|
300 |
|
|
301 |
typedef BellmanFord Create; |
|
302 |
|
|
303 |
/// \name Named Template Parameters |
|
304 |
|
|
305 |
///@{ |
|
306 |
|
|
307 |
template <class T> |
|
308 |
struct SetPredMapTraits : public Traits { |
|
309 |
typedef T PredMap; |
|
310 |
static PredMap *createPredMap(const Digraph&) { |
|
311 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
|
312 |
return 0; // ignore warnings |
|
313 |
} |
|
314 |
}; |
|
315 |
|
|
316 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
317 |
/// \c PredMap type. |
|
318 |
/// |
|
319 |
/// \ref named-templ-param "Named parameter" for setting |
|
320 |
/// \c PredMap type. |
|
321 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
322 |
template <class T> |
|
323 |
struct SetPredMap |
|
324 |
: public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > { |
|
325 |
typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
|
326 |
}; |
|
327 |
|
|
328 |
template <class T> |
|
329 |
struct SetDistMapTraits : public Traits { |
|
330 |
typedef T DistMap; |
|
331 |
static DistMap *createDistMap(const Digraph&) { |
|
332 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
|
333 |
return 0; // ignore warnings |
|
334 |
} |
|
335 |
}; |
|
336 |
|
|
337 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
338 |
/// \c DistMap type. |
|
339 |
/// |
|
340 |
/// \ref named-templ-param "Named parameter" for setting |
|
341 |
/// \c DistMap type. |
|
342 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
343 |
template <class T> |
|
344 |
struct SetDistMap |
|
345 |
: public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > { |
|
346 |
typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
|
347 |
}; |
|
348 |
|
|
349 |
template <class T> |
|
350 |
struct SetOperationTraitsTraits : public Traits { |
|
351 |
typedef T OperationTraits; |
|
352 |
}; |
|
353 |
|
|
354 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
355 |
/// \c OperationTraits type. |
|
356 |
/// |
|
357 |
/// \ref named-templ-param "Named parameter" for setting |
|
358 |
/// \c OperationTraits type. |
|
359 |
/// For more information, see \ref BellmanFordDefaultOperationTraits. |
|
360 |
template <class T> |
|
361 |
struct SetOperationTraits |
|
362 |
: public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
|
363 |
typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
|
364 |
Create; |
|
365 |
}; |
|
366 |
|
|
367 |
///@} |
|
368 |
|
|
369 |
protected: |
|
370 |
|
|
371 |
BellmanFord() {} |
|
372 |
|
|
373 |
public: |
|
374 |
|
|
375 |
/// \brief Constructor. |
|
376 |
/// |
|
377 |
/// Constructor. |
|
378 |
/// \param g The digraph the algorithm runs on. |
|
379 |
/// \param length The length map used by the algorithm. |
|
380 |
BellmanFord(const Digraph& g, const LengthMap& length) : |
|
381 |
_gr(&g), _length(&length), |
|
382 |
_pred(0), _local_pred(false), |
|
383 |
_dist(0), _local_dist(false), _mask(0) {} |
|
384 |
|
|
385 |
///Destructor. |
|
386 |
~BellmanFord() { |
|
387 |
if(_local_pred) delete _pred; |
|
388 |
if(_local_dist) delete _dist; |
|
389 |
if(_mask) delete _mask; |
|
390 |
} |
|
391 |
|
|
392 |
/// \brief Sets the length map. |
|
393 |
/// |
|
394 |
/// Sets the length map. |
|
395 |
/// \return <tt>(*this)</tt> |
|
396 |
BellmanFord &lengthMap(const LengthMap &map) { |
|
397 |
_length = ↦ |
|
398 |
return *this; |
|
399 |
} |
|
400 |
|
|
401 |
/// \brief Sets the map that stores the predecessor arcs. |
|
402 |
/// |
|
403 |
/// Sets the map that stores the predecessor arcs. |
|
404 |
/// If you don't use this function before calling \ref run() |
|
405 |
/// or \ref init(), an instance will be allocated automatically. |
|
406 |
/// The destructor deallocates this automatically allocated map, |
|
407 |
/// of course. |
|
408 |
/// \return <tt>(*this)</tt> |
|
409 |
BellmanFord &predMap(PredMap &map) { |
|
410 |
if(_local_pred) { |
|
411 |
delete _pred; |
|
412 |
_local_pred=false; |
|
413 |
} |
|
414 |
_pred = ↦ |
|
415 |
return *this; |
|
416 |
} |
|
417 |
|
|
418 |
/// \brief Sets the map that stores the distances of the nodes. |
|
419 |
/// |
|
420 |
/// Sets the map that stores the distances of the nodes calculated |
|
421 |
/// by the algorithm. |
|
422 |
/// If you don't use this function before calling \ref run() |
|
423 |
/// or \ref init(), an instance will be allocated automatically. |
|
424 |
/// The destructor deallocates this automatically allocated map, |
|
425 |
/// of course. |
|
426 |
/// \return <tt>(*this)</tt> |
|
427 |
BellmanFord &distMap(DistMap &map) { |
|
428 |
if(_local_dist) { |
|
429 |
delete _dist; |
|
430 |
_local_dist=false; |
|
431 |
} |
|
432 |
_dist = ↦ |
|
433 |
return *this; |
|
434 |
} |
|
435 |
|
|
436 |
/// \name Execution Control |
|
437 |
/// The simplest way to execute the Bellman-Ford algorithm is to use |
|
438 |
/// one of the member functions called \ref run().\n |
|
439 |
/// If you need better control on the execution, you have to call |
|
440 |
/// \ref init() first, then you can add several source nodes |
|
441 |
/// with \ref addSource(). Finally the actual path computation can be |
|
442 |
/// performed with \ref start(), \ref checkedStart() or |
|
443 |
/// \ref limitedStart(). |
|
444 |
|
|
445 |
///@{ |
|
446 |
|
|
447 |
/// \brief Initializes the internal data structures. |
|
448 |
/// |
|
449 |
/// Initializes the internal data structures. The optional parameter |
|
450 |
/// is the initial distance of each node. |
|
451 |
void init(const Value value = OperationTraits::infinity()) { |
|
452 |
create_maps(); |
|
453 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
|
454 |
_pred->set(it, INVALID); |
|
455 |
_dist->set(it, value); |
|
456 |
} |
|
457 |
_process.clear(); |
|
458 |
if (OperationTraits::less(value, OperationTraits::infinity())) { |
|
459 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
|
460 |
_process.push_back(it); |
|
461 |
_mask->set(it, true); |
|
462 |
} |
|
463 |
} else { |
|
464 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
|
465 |
_mask->set(it, false); |
|
466 |
} |
|
467 |
} |
|
468 |
} |
|
469 |
|
|
470 |
/// \brief Adds a new source node. |
|
471 |
/// |
|
472 |
/// This function adds a new source node. The optional second parameter |
|
473 |
/// is the initial distance of the node. |
|
474 |
void addSource(Node source, Value dst = OperationTraits::zero()) { |
|
475 |
_dist->set(source, dst); |
|
476 |
if (!(*_mask)[source]) { |
|
477 |
_process.push_back(source); |
|
478 |
_mask->set(source, true); |
|
479 |
} |
|
480 |
} |
|
481 |
|
|
482 |
/// \brief Executes one round from the Bellman-Ford algorithm. |
|
483 |
/// |
|
484 |
/// If the algoritm calculated the distances in the previous round |
|
485 |
/// exactly for the paths of at most \c k arcs, then this function |
|
486 |
/// will calculate the distances exactly for the paths of at most |
|
487 |
/// <tt>k+1</tt> arcs. Performing \c k iterations using this function |
|
488 |
/// calculates the shortest path distances exactly for the paths |
|
489 |
/// consisting of at most \c k arcs. |
|
490 |
/// |
|
491 |
/// \warning The paths with limited arc number cannot be retrieved |
|
492 |
/// easily with \ref path() or \ref predArc() functions. If you also |
|
493 |
/// need the shortest paths and not only the distances, you should |
|
494 |
/// store the \ref predMap() "predecessor map" after each iteration |
|
495 |
/// and build the path manually. |
|
496 |
/// |
|
497 |
/// \return \c true when the algorithm have not found more shorter |
|
498 |
/// paths. |
|
499 |
/// |
|
500 |
/// \see ActiveIt |
|
501 |
bool processNextRound() { |
|
502 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
503 |
_mask->set(_process[i], false); |
|
504 |
} |
|
505 |
std::vector<Node> nextProcess; |
|
506 |
std::vector<Value> values(_process.size()); |
|
507 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
508 |
values[i] = (*_dist)[_process[i]]; |
|
509 |
} |
|
510 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
511 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) { |
|
512 |
Node target = _gr->target(it); |
|
513 |
Value relaxed = OperationTraits::plus(values[i], (*_length)[it]); |
|
514 |
if (OperationTraits::less(relaxed, (*_dist)[target])) { |
|
515 |
_pred->set(target, it); |
|
516 |
_dist->set(target, relaxed); |
|
517 |
if (!(*_mask)[target]) { |
|
518 |
_mask->set(target, true); |
|
519 |
nextProcess.push_back(target); |
|
520 |
} |
|
521 |
} |
|
522 |
} |
|
523 |
} |
|
524 |
_process.swap(nextProcess); |
|
525 |
return _process.empty(); |
|
526 |
} |
|
527 |
|
|
528 |
/// \brief Executes one weak round from the Bellman-Ford algorithm. |
|
529 |
/// |
|
530 |
/// If the algorithm calculated the distances in the previous round |
|
531 |
/// at least for the paths of at most \c k arcs, then this function |
|
532 |
/// will calculate the distances at least for the paths of at most |
|
533 |
/// <tt>k+1</tt> arcs. |
|
534 |
/// This function does not make it possible to calculate the shortest |
|
535 |
/// path distances exactly for paths consisting of at most \c k arcs, |
|
536 |
/// this is why it is called weak round. |
|
537 |
/// |
|
538 |
/// \return \c true when the algorithm have not found more shorter |
|
539 |
/// paths. |
|
540 |
/// |
|
541 |
/// \see ActiveIt |
|
542 |
bool processNextWeakRound() { |
|
543 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
544 |
_mask->set(_process[i], false); |
|
545 |
} |
|
546 |
std::vector<Node> nextProcess; |
|
547 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
548 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) { |
|
549 |
Node target = _gr->target(it); |
|
550 |
Value relaxed = |
|
551 |
OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]); |
|
552 |
if (OperationTraits::less(relaxed, (*_dist)[target])) { |
|
553 |
_pred->set(target, it); |
|
554 |
_dist->set(target, relaxed); |
|
555 |
if (!(*_mask)[target]) { |
|
556 |
_mask->set(target, true); |
|
557 |
nextProcess.push_back(target); |
|
558 |
} |
|
559 |
} |
|
560 |
} |
|
561 |
} |
|
562 |
_process.swap(nextProcess); |
|
563 |
return _process.empty(); |
|
564 |
} |
|
565 |
|
|
566 |
/// \brief Executes the algorithm. |
|
567 |
/// |
|
568 |
/// Executes the algorithm. |
|
569 |
/// |
|
570 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
|
571 |
/// in order to compute the shortest path to each node. |
|
572 |
/// |
|
573 |
/// The algorithm computes |
|
574 |
/// - the shortest path tree (forest), |
|
575 |
/// - the distance of each node from the root(s). |
|
576 |
/// |
|
577 |
/// \pre init() must be called and at least one root node should be |
|
578 |
/// added with addSource() before using this function. |
|
579 |
void start() { |
|
580 |
int num = countNodes(*_gr) - 1; |
|
581 |
for (int i = 0; i < num; ++i) { |
|
582 |
if (processNextWeakRound()) break; |
|
583 |
} |
|
584 |
} |
|
585 |
|
|
586 |
/// \brief Executes the algorithm and checks the negative cycles. |
|
587 |
/// |
|
588 |
/// Executes the algorithm and checks the negative cycles. |
|
589 |
/// |
|
590 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
|
591 |
/// in order to compute the shortest path to each node and also checks |
|
592 |
/// if the digraph contains cycles with negative total length. |
|
593 |
/// |
|
594 |
/// The algorithm computes |
|
595 |
/// - the shortest path tree (forest), |
|
596 |
/// - the distance of each node from the root(s). |
|
597 |
/// |
|
598 |
/// \return \c false if there is a negative cycle in the digraph. |
|
599 |
/// |
|
600 |
/// \pre init() must be called and at least one root node should be |
|
601 |
/// added with addSource() before using this function. |
|
602 |
bool checkedStart() { |
|
603 |
int num = countNodes(*_gr); |
|
604 |
for (int i = 0; i < num; ++i) { |
|
605 |
if (processNextWeakRound()) return true; |
|
606 |
} |
|
607 |
return _process.empty(); |
|
608 |
} |
|
609 |
|
|
610 |
/// \brief Executes the algorithm with arc number limit. |
|
611 |
/// |
|
612 |
/// Executes the algorithm with arc number limit. |
|
613 |
/// |
|
614 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
|
615 |
/// in order to compute the shortest path distance for each node |
|
616 |
/// using only the paths consisting of at most \c num arcs. |
|
617 |
/// |
|
618 |
/// The algorithm computes |
|
619 |
/// - the limited distance of each node from the root(s), |
|
620 |
/// - the predecessor arc for each node. |
|
621 |
/// |
|
622 |
/// \warning The paths with limited arc number cannot be retrieved |
|
623 |
/// easily with \ref path() or \ref predArc() functions. If you also |
|
624 |
/// need the shortest paths and not only the distances, you should |
|
625 |
/// store the \ref predMap() "predecessor map" after each iteration |
|
626 |
/// and build the path manually. |
|
627 |
/// |
|
628 |
/// \pre init() must be called and at least one root node should be |
|
629 |
/// added with addSource() before using this function. |
|
630 |
void limitedStart(int num) { |
|
631 |
for (int i = 0; i < num; ++i) { |
|
632 |
if (processNextRound()) break; |
|
633 |
} |
|
634 |
} |
|
635 |
|
|
636 |
/// \brief Runs the algorithm from the given root node. |
|
637 |
/// |
|
638 |
/// This method runs the Bellman-Ford algorithm from the given root |
|
639 |
/// node \c s in order to compute the shortest path to each node. |
|
640 |
/// |
|
641 |
/// The algorithm computes |
|
642 |
/// - the shortest path tree (forest), |
|
643 |
/// - the distance of each node from the root(s). |
|
644 |
/// |
|
645 |
/// \note bf.run(s) is just a shortcut of the following code. |
|
646 |
/// \code |
|
647 |
/// bf.init(); |
|
648 |
/// bf.addSource(s); |
|
649 |
/// bf.start(); |
|
650 |
/// \endcode |
|
651 |
void run(Node s) { |
|
652 |
init(); |
|
653 |
addSource(s); |
|
654 |
start(); |
|
655 |
} |
|
656 |
|
|
657 |
/// \brief Runs the algorithm from the given root node with arc |
|
658 |
/// number limit. |
|
659 |
/// |
|
660 |
/// This method runs the Bellman-Ford algorithm from the given root |
|
661 |
/// node \c s in order to compute the shortest path distance for each |
|
662 |
/// node using only the paths consisting of at most \c num arcs. |
|
663 |
/// |
|
664 |
/// The algorithm computes |
|
665 |
/// - the limited distance of each node from the root(s), |
|
666 |
/// - the predecessor arc for each node. |
|
667 |
/// |
|
668 |
/// \warning The paths with limited arc number cannot be retrieved |
|
669 |
/// easily with \ref path() or \ref predArc() functions. If you also |
|
670 |
/// need the shortest paths and not only the distances, you should |
|
671 |
/// store the \ref predMap() "predecessor map" after each iteration |
|
672 |
/// and build the path manually. |
|
673 |
/// |
|
674 |
/// \note bf.run(s, num) is just a shortcut of the following code. |
|
675 |
/// \code |
|
676 |
/// bf.init(); |
|
677 |
/// bf.addSource(s); |
|
678 |
/// bf.limitedStart(num); |
|
679 |
/// \endcode |
|
680 |
void run(Node s, int num) { |
|
681 |
init(); |
|
682 |
addSource(s); |
|
683 |
limitedStart(num); |
|
684 |
} |
|
685 |
|
|
686 |
///@} |
|
687 |
|
|
688 |
/// \brief LEMON iterator for getting the active nodes. |
|
689 |
/// |
|
690 |
/// This class provides a common style LEMON iterator that traverses |
|
691 |
/// the active nodes of the Bellman-Ford algorithm after the last |
|
692 |
/// phase. These nodes should be checked in the next phase to |
|
693 |
/// find augmenting arcs outgoing from them. |
|
694 |
class ActiveIt { |
|
695 |
public: |
|
696 |
|
|
697 |
/// \brief Constructor. |
|
698 |
/// |
|
699 |
/// Constructor for getting the active nodes of the given BellmanFord |
|
700 |
/// instance. |
|
701 |
ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
|
702 |
{ |
|
703 |
_index = _algorithm->_process.size() - 1; |
|
704 |
} |
|
705 |
|
|
706 |
/// \brief Invalid constructor. |
|
707 |
/// |
|
708 |
/// Invalid constructor. |
|
709 |
ActiveIt(Invalid) : _algorithm(0), _index(-1) {} |
|
710 |
|
|
711 |
/// \brief Conversion to \c Node. |
|
712 |
/// |
|
713 |
/// Conversion to \c Node. |
|
714 |
operator Node() const { |
|
715 |
return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
|
716 |
} |
|
717 |
|
|
718 |
/// \brief Increment operator. |
|
719 |
/// |
|
720 |
/// Increment operator. |
|
721 |
ActiveIt& operator++() { |
|
722 |
--_index; |
|
723 |
return *this; |
|
724 |
} |
|
725 |
|
|
726 |
bool operator==(const ActiveIt& it) const { |
|
727 |
return static_cast<Node>(*this) == static_cast<Node>(it); |
|
728 |
} |
|
729 |
bool operator!=(const ActiveIt& it) const { |
|
730 |
return static_cast<Node>(*this) != static_cast<Node>(it); |
|
731 |
} |
|
732 |
bool operator<(const ActiveIt& it) const { |
|
733 |
return static_cast<Node>(*this) < static_cast<Node>(it); |
|
734 |
} |
|
735 |
|
|
736 |
private: |
|
737 |
const BellmanFord* _algorithm; |
|
738 |
int _index; |
|
739 |
}; |
|
740 |
|
|
741 |
/// \name Query Functions |
|
742 |
/// The result of the Bellman-Ford algorithm can be obtained using these |
|
743 |
/// functions.\n |
|
744 |
/// Either \ref run() or \ref init() should be called before using them. |
|
745 |
|
|
746 |
///@{ |
|
747 |
|
|
748 |
/// \brief The shortest path to the given node. |
|
749 |
/// |
|
750 |
/// Gives back the shortest path to the given node from the root(s). |
|
751 |
/// |
|
752 |
/// \warning \c t should be reached from the root(s). |
|
753 |
/// |
|
754 |
/// \pre Either \ref run() or \ref init() must be called before |
|
755 |
/// using this function. |
|
756 |
Path path(Node t) const |
|
757 |
{ |
|
758 |
return Path(*_gr, *_pred, t); |
|
759 |
} |
|
760 |
|
|
761 |
/// \brief The distance of the given node from the root(s). |
|
762 |
/// |
|
763 |
/// Returns the distance of the given node from the root(s). |
|
764 |
/// |
|
765 |
/// \warning If node \c v is not reached from the root(s), then |
|
766 |
/// the return value of this function is undefined. |
|
767 |
/// |
|
768 |
/// \pre Either \ref run() or \ref init() must be called before |
|
769 |
/// using this function. |
|
770 |
Value dist(Node v) const { return (*_dist)[v]; } |
|
771 |
|
|
772 |
/// \brief Returns the 'previous arc' of the shortest path tree for |
|
773 |
/// the given node. |
|
774 |
/// |
|
775 |
/// This function returns the 'previous arc' of the shortest path |
|
776 |
/// tree for node \c v, i.e. it returns the last arc of a |
|
777 |
/// shortest path from a root to \c v. It is \c INVALID if \c v |
|
778 |
/// is not reached from the root(s) or if \c v is a root. |
|
779 |
/// |
|
780 |
/// The shortest path tree used here is equal to the shortest path |
|
781 |
/// tree used in \ref predNode() and \ref predMap(). |
|
782 |
/// |
|
783 |
/// \pre Either \ref run() or \ref init() must be called before |
|
784 |
/// using this function. |
|
785 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
|
786 |
|
|
787 |
/// \brief Returns the 'previous node' of the shortest path tree for |
|
788 |
/// the given node. |
|
789 |
/// |
|
790 |
/// This function returns the 'previous node' of the shortest path |
|
791 |
/// tree for node \c v, i.e. it returns the last but one node of |
|
792 |
/// a shortest path from a root to \c v. It is \c INVALID if \c v |
|
793 |
/// is not reached from the root(s) or if \c v is a root. |
|
794 |
/// |
|
795 |
/// The shortest path tree used here is equal to the shortest path |
|
796 |
/// tree used in \ref predArc() and \ref predMap(). |
|
797 |
/// |
|
798 |
/// \pre Either \ref run() or \ref init() must be called before |
|
799 |
/// using this function. |
|
800 |
Node predNode(Node v) const { |
|
801 |
return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
|
802 |
} |
|
803 |
|
|
804 |
/// \brief Returns a const reference to the node map that stores the |
|
805 |
/// distances of the nodes. |
|
806 |
/// |
|
807 |
/// Returns a const reference to the node map that stores the distances |
|
808 |
/// of the nodes calculated by the algorithm. |
|
809 |
/// |
|
810 |
/// \pre Either \ref run() or \ref init() must be called before |
|
811 |
/// using this function. |
|
812 |
const DistMap &distMap() const { return *_dist;} |
|
813 |
|
|
814 |
/// \brief Returns a const reference to the node map that stores the |
|
815 |
/// predecessor arcs. |
|
816 |
/// |
|
817 |
/// Returns a const reference to the node map that stores the predecessor |
|
818 |
/// arcs, which form the shortest path tree (forest). |
|
819 |
/// |
|
820 |
/// \pre Either \ref run() or \ref init() must be called before |
|
821 |
/// using this function. |
|
822 |
const PredMap &predMap() const { return *_pred; } |
|
823 |
|
|
824 |
/// \brief Checks if a node is reached from the root(s). |
|
825 |
/// |
|
826 |
/// Returns \c true if \c v is reached from the root(s). |
|
827 |
/// |
|
828 |
/// \pre Either \ref run() or \ref init() must be called before |
|
829 |
/// using this function. |
|
830 |
bool reached(Node v) const { |
|
831 |
return (*_dist)[v] != OperationTraits::infinity(); |
|
832 |
} |
|
833 |
|
|
834 |
/// \brief Gives back a negative cycle. |
|
835 |
/// |
|
836 |
/// This function gives back a directed cycle with negative total |
|
837 |
/// length if the algorithm has already found one. |
|
838 |
/// Otherwise it gives back an empty path. |
|
839 |
lemon::Path<Digraph> negativeCycle() const { |
|
840 |
typename Digraph::template NodeMap<int> state(*_gr, -1); |
|
841 |
lemon::Path<Digraph> cycle; |
|
842 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
843 |
if (state[_process[i]] != -1) continue; |
|
844 |
for (Node v = _process[i]; (*_pred)[v] != INVALID; |
|
845 |
v = _gr->source((*_pred)[v])) { |
|
846 |
if (state[v] == i) { |
|
847 |
cycle.addFront((*_pred)[v]); |
|
848 |
for (Node u = _gr->source((*_pred)[v]); u != v; |
|
849 |
u = _gr->source((*_pred)[u])) { |
|
850 |
cycle.addFront((*_pred)[u]); |
|
851 |
} |
|
852 |
return cycle; |
|
853 |
} |
|
854 |
else if (state[v] >= 0) { |
|
855 |
break; |
|
856 |
} |
|
857 |
state[v] = i; |
|
858 |
} |
|
859 |
} |
|
860 |
return cycle; |
|
861 |
} |
|
862 |
|
|
863 |
///@} |
|
864 |
}; |
|
865 |
|
|
866 |
/// \brief Default traits class of bellmanFord() function. |
|
867 |
/// |
|
868 |
/// Default traits class of bellmanFord() function. |
|
869 |
/// \tparam GR The type of the digraph. |
|
870 |
/// \tparam LEN The type of the length map. |
|
871 |
template <typename GR, typename LEN> |
|
872 |
struct BellmanFordWizardDefaultTraits { |
|
873 |
/// The type of the digraph the algorithm runs on. |
|
874 |
typedef GR Digraph; |
|
875 |
|
|
876 |
/// \brief The type of the map that stores the arc lengths. |
|
877 |
/// |
|
878 |
/// The type of the map that stores the arc lengths. |
|
879 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
|
880 |
typedef LEN LengthMap; |
|
881 |
|
|
882 |
/// The type of the arc lengths. |
|
883 |
typedef typename LEN::Value Value; |
|
884 |
|
|
885 |
/// \brief Operation traits for Bellman-Ford algorithm. |
|
886 |
/// |
|
887 |
/// It defines the used operations and the infinity value for the |
|
888 |
/// given \c Value type. |
|
889 |
/// \see BellmanFordDefaultOperationTraits, |
|
890 |
/// BellmanFordToleranceOperationTraits |
|
891 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
|
892 |
|
|
893 |
/// \brief The type of the map that stores the last |
|
894 |
/// arcs of the shortest paths. |
|
895 |
/// |
|
896 |
/// The type of the map that stores the last arcs of the shortest paths. |
|
897 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
898 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
|
899 |
|
|
900 |
/// \brief Instantiates a \c PredMap. |
|
901 |
/// |
|
902 |
/// This function instantiates a \ref PredMap. |
|
903 |
/// \param g is the digraph to which we would like to define the |
|
904 |
/// \ref PredMap. |
|
905 |
static PredMap *createPredMap(const GR &g) { |
|
906 |
return new PredMap(g); |
|
907 |
} |
|
908 |
|
|
909 |
/// \brief The type of the map that stores the distances of the nodes. |
|
910 |
/// |
|
911 |
/// The type of the map that stores the distances of the nodes. |
|
912 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
913 |
typedef typename GR::template NodeMap<Value> DistMap; |
|
914 |
|
|
915 |
/// \brief Instantiates a \c DistMap. |
|
916 |
/// |
|
917 |
/// This function instantiates a \ref DistMap. |
|
918 |
/// \param g is the digraph to which we would like to define the |
|
919 |
/// \ref DistMap. |
|
920 |
static DistMap *createDistMap(const GR &g) { |
|
921 |
return new DistMap(g); |
|
922 |
} |
|
923 |
|
|
924 |
///The type of the shortest paths. |
|
925 |
|
|
926 |
///The type of the shortest paths. |
|
927 |
///It must meet the \ref concepts::Path "Path" concept. |
|
928 |
typedef lemon::Path<Digraph> Path; |
|
929 |
}; |
|
930 |
|
|
931 |
/// \brief Default traits class used by BellmanFordWizard. |
|
932 |
/// |
|
933 |
/// Default traits class used by BellmanFordWizard. |
|
934 |
/// \tparam GR The type of the digraph. |
|
935 |
/// \tparam LEN The type of the length map. |
|
936 |
template <typename GR, typename LEN> |
|
937 |
class BellmanFordWizardBase |
|
938 |
: public BellmanFordWizardDefaultTraits<GR, LEN> { |
|
939 |
|
|
940 |
typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
|
941 |
protected: |
|
942 |
// Type of the nodes in the digraph. |
|
943 |
typedef typename Base::Digraph::Node Node; |
|
944 |
|
|
945 |
// Pointer to the underlying digraph. |
|
946 |
void *_graph; |
|
947 |
// Pointer to the length map |
|
948 |
void *_length; |
|
949 |
// Pointer to the map of predecessors arcs. |
|
950 |
void *_pred; |
|
951 |
// Pointer to the map of distances. |
|
952 |
void *_dist; |
|
953 |
//Pointer to the shortest path to the target node. |
|
954 |
void *_path; |
|
955 |
//Pointer to the distance of the target node. |
|
956 |
void *_di; |
|
957 |
|
|
958 |
public: |
|
959 |
/// Constructor. |
|
960 |
|
|
961 |
/// This constructor does not require parameters, it initiates |
|
962 |
/// all of the attributes to default values \c 0. |
|
963 |
BellmanFordWizardBase() : |
|
964 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {} |
|
965 |
|
|
966 |
/// Constructor. |
|
967 |
|
|
968 |
/// This constructor requires two parameters, |
|
969 |
/// others are initiated to \c 0. |
|
970 |
/// \param gr The digraph the algorithm runs on. |
|
971 |
/// \param len The length map. |
|
972 |
BellmanFordWizardBase(const GR& gr, |
|
973 |
const LEN& len) : |
|
974 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
|
975 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
|
976 |
_pred(0), _dist(0), _path(0), _di(0) {} |
|
977 |
|
|
978 |
}; |
|
979 |
|
|
980 |
/// \brief Auxiliary class for the function-type interface of the |
|
981 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
|
982 |
/// |
|
983 |
/// This auxiliary class is created to implement the |
|
984 |
/// \ref bellmanFord() "function-type interface" of the |
|
985 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
|
986 |
/// It does not have own \ref run() method, it uses the |
|
987 |
/// functions and features of the plain \ref BellmanFord. |
|
988 |
/// |
|
989 |
/// This class should only be used through the \ref bellmanFord() |
|
990 |
/// function, which makes it easier to use the algorithm. |
|
991 |
/// |
|
992 |
/// \tparam TR The traits class that defines various types used by the |
|
993 |
/// algorithm. |
|
994 |
template<class TR> |
|
995 |
class BellmanFordWizard : public TR { |
|
996 |
typedef TR Base; |
|
997 |
|
|
998 |
typedef typename TR::Digraph Digraph; |
|
999 |
|
|
1000 |
typedef typename Digraph::Node Node; |
|
1001 |
typedef typename Digraph::NodeIt NodeIt; |
|
1002 |
typedef typename Digraph::Arc Arc; |
|
1003 |
typedef typename Digraph::OutArcIt ArcIt; |
|
1004 |
|
|
1005 |
typedef typename TR::LengthMap LengthMap; |
|
1006 |
typedef typename LengthMap::Value Value; |
|
1007 |
typedef typename TR::PredMap PredMap; |
|
1008 |
typedef typename TR::DistMap DistMap; |
|
1009 |
typedef typename TR::Path Path; |
|
1010 |
|
|
1011 |
public: |
|
1012 |
/// Constructor. |
|
1013 |
BellmanFordWizard() : TR() {} |
|
1014 |
|
|
1015 |
/// \brief Constructor that requires parameters. |
|
1016 |
/// |
|
1017 |
/// Constructor that requires parameters. |
|
1018 |
/// These parameters will be the default values for the traits class. |
|
1019 |
/// \param gr The digraph the algorithm runs on. |
|
1020 |
/// \param len The length map. |
|
1021 |
BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
|
1022 |
: TR(gr, len) {} |
|
1023 |
|
|
1024 |
/// \brief Copy constructor |
|
1025 |
BellmanFordWizard(const TR &b) : TR(b) {} |
|
1026 |
|
|
1027 |
~BellmanFordWizard() {} |
|
1028 |
|
|
1029 |
/// \brief Runs the Bellman-Ford algorithm from the given source node. |
|
1030 |
/// |
|
1031 |
/// This method runs the Bellman-Ford algorithm from the given source |
|
1032 |
/// node in order to compute the shortest path to each node. |
|
1033 |
void run(Node s) { |
|
1034 |
BellmanFord<Digraph,LengthMap,TR> |
|
1035 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
|
1036 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
|
1037 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1038 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1039 |
bf.run(s); |
|
1040 |
} |
|
1041 |
|
|
1042 |
/// \brief Runs the Bellman-Ford algorithm to find the shortest path |
|
1043 |
/// between \c s and \c t. |
|
1044 |
/// |
|
1045 |
/// This method runs the Bellman-Ford algorithm from node \c s |
|
1046 |
/// in order to compute the shortest path to node \c t. |
|
1047 |
/// Actually, it computes the shortest path to each node, but using |
|
1048 |
/// this function you can retrieve the distance and the shortest path |
|
1049 |
/// for a single target node easier. |
|
1050 |
/// |
|
1051 |
/// \return \c true if \c t is reachable form \c s. |
|
1052 |
bool run(Node s, Node t) { |
|
1053 |
BellmanFord<Digraph,LengthMap,TR> |
|
1054 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
|
1055 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
|
1056 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
1057 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
1058 |
bf.run(s); |
|
1059 |
if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t); |
|
1060 |
if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t); |
|
1061 |
return bf.reached(t); |
|
1062 |
} |
|
1063 |
|
|
1064 |
template<class T> |
|
1065 |
struct SetPredMapBase : public Base { |
|
1066 |
typedef T PredMap; |
|
1067 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
|
1068 |
SetPredMapBase(const TR &b) : TR(b) {} |
|
1069 |
}; |
|
1070 |
|
|
1071 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
1072 |
/// the predecessor map. |
|
1073 |
/// |
|
1074 |
/// \ref named-templ-param "Named parameter" for setting |
|
1075 |
/// the map that stores the predecessor arcs of the nodes. |
|
1076 |
template<class T> |
|
1077 |
BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) { |
|
1078 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1079 |
return BellmanFordWizard<SetPredMapBase<T> >(*this); |
|
1080 |
} |
|
1081 |
|
|
1082 |
template<class T> |
|
1083 |
struct SetDistMapBase : public Base { |
|
1084 |
typedef T DistMap; |
|
1085 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1086 |
SetDistMapBase(const TR &b) : TR(b) {} |
|
1087 |
}; |
|
1088 |
|
|
1089 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
1090 |
/// the distance map. |
|
1091 |
/// |
|
1092 |
/// \ref named-templ-param "Named parameter" for setting |
|
1093 |
/// the map that stores the distances of the nodes calculated |
|
1094 |
/// by the algorithm. |
|
1095 |
template<class T> |
|
1096 |
BellmanFordWizard<SetDistMapBase<T> > distMap(const T &t) { |
|
1097 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1098 |
return BellmanFordWizard<SetDistMapBase<T> >(*this); |
|
1099 |
} |
|
1100 |
|
|
1101 |
template<class T> |
|
1102 |
struct SetPathBase : public Base { |
|
1103 |
typedef T Path; |
|
1104 |
SetPathBase(const TR &b) : TR(b) {} |
|
1105 |
}; |
|
1106 |
|
|
1107 |
/// \brief \ref named-func-param "Named parameter" for getting |
|
1108 |
/// the shortest path to the target node. |
|
1109 |
/// |
|
1110 |
/// \ref named-func-param "Named parameter" for getting |
|
1111 |
/// the shortest path to the target node. |
|
1112 |
template<class T> |
|
1113 |
BellmanFordWizard<SetPathBase<T> > path(const T &t) |
|
1114 |
{ |
|
1115 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1116 |
return BellmanFordWizard<SetPathBase<T> >(*this); |
|
1117 |
} |
|
1118 |
|
|
1119 |
/// \brief \ref named-func-param "Named parameter" for getting |
|
1120 |
/// the distance of the target node. |
|
1121 |
/// |
|
1122 |
/// \ref named-func-param "Named parameter" for getting |
|
1123 |
/// the distance of the target node. |
|
1124 |
BellmanFordWizard dist(const Value &d) |
|
1125 |
{ |
|
1126 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
|
1127 |
return *this; |
|
1128 |
} |
|
1129 |
|
|
1130 |
}; |
|
1131 |
|
|
1132 |
/// \brief Function type interface for the \ref BellmanFord "Bellman-Ford" |
|
1133 |
/// algorithm. |
|
1134 |
/// |
|
1135 |
/// \ingroup shortest_path |
|
1136 |
/// Function type interface for the \ref BellmanFord "Bellman-Ford" |
|
1137 |
/// algorithm. |
|
1138 |
/// |
|
1139 |
/// This function also has several \ref named-templ-func-param |
|
1140 |
/// "named parameters", they are declared as the members of class |
|
1141 |
/// \ref BellmanFordWizard. |
|
1142 |
/// The following examples show how to use these parameters. |
|
1143 |
/// \code |
|
1144 |
/// // Compute shortest path from node s to each node |
|
1145 |
/// bellmanFord(g,length).predMap(preds).distMap(dists).run(s); |
|
1146 |
/// |
|
1147 |
/// // Compute shortest path from s to t |
|
1148 |
/// bool reached = bellmanFord(g,length).path(p).dist(d).run(s,t); |
|
1149 |
/// \endcode |
|
1150 |
/// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()" |
|
1151 |
/// to the end of the parameter list. |
|
1152 |
/// \sa BellmanFordWizard |
|
1153 |
/// \sa BellmanFord |
|
1154 |
template<typename GR, typename LEN> |
|
1155 |
BellmanFordWizard<BellmanFordWizardBase<GR,LEN> > |
|
1156 |
bellmanFord(const GR& digraph, |
|
1157 |
const LEN& length) |
|
1158 |
{ |
|
1159 |
return BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >(digraph, length); |
|
1160 |
} |
|
1161 |
|
|
1162 |
} //END OF NAMESPACE LEMON |
|
1163 |
|
|
1164 |
#endif |
|
1165 |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2010 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BINOMIAL_HEAP_H |
|
20 |
#define LEMON_BINOMIAL_HEAP_H |
|
21 |
|
|
22 |
///\file |
|
23 |
///\ingroup heaps |
|
24 |
///\brief Binomial Heap implementation. |
|
25 |
|
|
26 |
#include <vector> |
|
27 |
#include <utility> |
|
28 |
#include <functional> |
|
29 |
#include <lemon/math.h> |
|
30 |
#include <lemon/counter.h> |
|
31 |
|
|
32 |
namespace lemon { |
|
33 |
|
|
34 |
/// \ingroup heaps |
|
35 |
/// |
|
36 |
///\brief Binomial heap data structure. |
|
37 |
/// |
|
38 |
/// This class implements the \e binomial \e heap data structure. |
|
39 |
/// It fully conforms to the \ref concepts::Heap "heap concept". |
|
40 |
/// |
|
41 |
/// The methods \ref increase() and \ref erase() are not efficient |
|
42 |
/// in a binomial heap. In case of many calls of these operations, |
|
43 |
/// it is better to use other heap structure, e.g. \ref BinHeap |
|
44 |
/// "binary heap". |
|
45 |
/// |
|
46 |
/// \tparam PR Type of the priorities of the items. |
|
47 |
/// \tparam IM A read-writable item map with \c int values, used |
|
48 |
/// internally to handle the cross references. |
|
49 |
/// \tparam CMP A functor class for comparing the priorities. |
|
50 |
/// The default is \c std::less<PR>. |
|
51 |
#ifdef DOXYGEN |
|
52 |
template <typename PR, typename IM, typename CMP> |
|
53 |
#else |
|
54 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
|
55 |
#endif |
|
56 |
class BinomialHeap { |
|
57 |
public: |
|
58 |
/// Type of the item-int map. |
|
59 |
typedef IM ItemIntMap; |
|
60 |
/// Type of the priorities. |
|
61 |
typedef PR Prio; |
|
62 |
/// Type of the items stored in the heap. |
|
63 |
typedef typename ItemIntMap::Key Item; |
|
64 |
/// Functor type for comparing the priorities. |
|
65 |
typedef CMP Compare; |
|
66 |
|
|
67 |
/// \brief Type to represent the states of the items. |
|
68 |
/// |
|
69 |
/// Each item has a state associated to it. It can be "in heap", |
|
70 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
|
71 |
/// heap's point of view, but may be useful to the user. |
|
72 |
/// |
|
73 |
/// The item-int map must be initialized in such way that it assigns |
|
74 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
|
75 |
enum State { |
|
76 |
IN_HEAP = 0, ///< = 0. |
|
77 |
PRE_HEAP = -1, ///< = -1. |
|
78 |
POST_HEAP = -2 ///< = -2. |
|
79 |
}; |
|
80 |
|
|
81 |
private: |
|
82 |
class Store; |
|
83 |
|
|
84 |
std::vector<Store> _data; |
|
85 |
int _min, _head; |
|
86 |
ItemIntMap &_iim; |
|
87 |
Compare _comp; |
|
88 |
int _num_items; |
|
89 |
|
|
90 |
public: |
|
91 |
/// \brief Constructor. |
|
92 |
/// |
|
93 |
/// Constructor. |
|
94 |
/// \param map A map that assigns \c int values to the items. |
|
95 |
/// It is used internally to handle the cross references. |
|
96 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
97 |
explicit BinomialHeap(ItemIntMap &map) |
|
98 |
: _min(0), _head(-1), _iim(map), _num_items(0) {} |
|
99 |
|
|
100 |
/// \brief Constructor. |
|
101 |
/// |
|
102 |
/// Constructor. |
|
103 |
/// \param map A map that assigns \c int values to the items. |
|
104 |
/// It is used internally to handle the cross references. |
|
105 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
106 |
/// \param comp The function object used for comparing the priorities. |
|
107 |
BinomialHeap(ItemIntMap &map, const Compare &comp) |
|
108 |
: _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {} |
|
109 |
|
|
110 |
/// \brief The number of items stored in the heap. |
|
111 |
/// |
|
112 |
/// This function returns the number of items stored in the heap. |
|
113 |
int size() const { return _num_items; } |
|
114 |
|
|
115 |
/// \brief Check if the heap is empty. |
|
116 |
/// |
|
117 |
/// This function returns \c true if the heap is empty. |
|
118 |
bool empty() const { return _num_items==0; } |
|
119 |
|
|
120 |
/// \brief Make the heap empty. |
|
121 |
/// |
|
122 |
/// This functon makes the heap empty. |
|
123 |
/// It does not change the cross reference map. If you want to reuse |
|
124 |
/// a heap that is not surely empty, you should first clear it and |
|
125 |
/// then you should set the cross reference map to \c PRE_HEAP |
|
126 |
/// for each item. |
|
127 |
void clear() { |
|
128 |
_data.clear(); _min=0; _num_items=0; _head=-1; |
|
129 |
} |
|
130 |
|
|
131 |
/// \brief Set the priority of an item or insert it, if it is |
|
132 |
/// not stored in the heap. |
|
133 |
/// |
|
134 |
/// This method sets the priority of the given item if it is |
|
135 |
/// already stored in the heap. Otherwise it inserts the given |
|
136 |
/// item into the heap with the given priority. |
|
137 |
/// \param item The item. |
|
138 |
/// \param value The priority. |
|
139 |
void set (const Item& item, const Prio& value) { |
|
140 |
int i=_iim[item]; |
|
141 |
if ( i >= 0 && _data[i].in ) { |
|
142 |
if ( _comp(value, _data[i].prio) ) decrease(item, value); |
|
143 |
if ( _comp(_data[i].prio, value) ) increase(item, value); |
|
144 |
} else push(item, value); |
|
145 |
} |
|
146 |
|
|
147 |
/// \brief Insert an item into the heap with the given priority. |
|
148 |
/// |
|
149 |
/// This function inserts the given item into the heap with the |
|
150 |
/// given priority. |
|
151 |
/// \param item The item to insert. |
|
152 |
/// \param value The priority of the item. |
|
153 |
/// \pre \e item must not be stored in the heap. |
|
154 |
void push (const Item& item, const Prio& value) { |
|
155 |
int i=_iim[item]; |
|
156 |
if ( i<0 ) { |
|
157 |
int s=_data.size(); |
|
158 |
_iim.set( item,s ); |
|
159 |
Store st; |
|
160 |
st.name=item; |
|
161 |
st.prio=value; |
|
162 |
_data.push_back(st); |
|
163 |
i=s; |
|
164 |
} |
|
165 |
else { |
|
166 |
_data[i].parent=_data[i].right_neighbor=_data[i].child=-1; |
|
167 |
_data[i].degree=0; |
|
168 |
_data[i].in=true; |
|
169 |
_data[i].prio=value; |
|
170 |
} |
|
171 |
|
|
172 |
if( 0==_num_items ) { |
|
173 |
_head=i; |
|
174 |
_min=i; |
|
175 |
} else { |
|
176 |
merge(i); |
|
177 |
if( _comp(_data[i].prio, _data[_min].prio) ) _min=i; |
|
178 |
} |
|
179 |
++_num_items; |
|
180 |
} |
|
181 |
|
|
182 |
/// \brief Return the item having minimum priority. |
|
183 |
/// |
|
184 |
/// This function returns the item having minimum priority. |
|
185 |
/// \pre The heap must be non-empty. |
|
186 |
Item top() const { return _data[_min].name; } |
|
187 |
|
|
188 |
/// \brief The minimum priority. |
|
189 |
/// |
|
190 |
/// This function returns the minimum priority. |
|
191 |
/// \pre The heap must be non-empty. |
|
192 |
Prio prio() const { return _data[_min].prio; } |
|
193 |
|
|
194 |
/// \brief The priority of the given item. |
|
195 |
/// |
|
196 |
/// This function returns the priority of the given item. |
|
197 |
/// \param item The item. |
|
198 |
/// \pre \e item must be in the heap. |
|
199 |
const Prio& operator[](const Item& item) const { |
|
200 |
return _data[_iim[item]].prio; |
|
201 |
} |
|
202 |
|
|
203 |
/// \brief Remove the item having minimum priority. |
|
204 |
/// |
|
205 |
/// This function removes the item having minimum priority. |
|
206 |
/// \pre The heap must be non-empty. |
|
207 |
void pop() { |
|
208 |
_data[_min].in=false; |
|
209 |
|
|
210 |
int head_child=-1; |
|
211 |
if ( _data[_min].child!=-1 ) { |
|
212 |
int child=_data[_min].child; |
|
213 |
int neighb; |
|
214 |
while( child!=-1 ) { |
|
215 |
neighb=_data[child].right_neighbor; |
|
216 |
_data[child].parent=-1; |
|
217 |
_data[child].right_neighbor=head_child; |
|
218 |
head_child=child; |
|
219 |
child=neighb; |
|
220 |
} |
|
221 |
} |
|
222 |
|
|
223 |
if ( _data[_head].right_neighbor==-1 ) { |
|
224 |
// there was only one root |
|
225 |
_head=head_child; |
|
226 |
} |
|
227 |
else { |
|
228 |
// there were more roots |
|
229 |
if( _head!=_min ) { unlace(_min); } |
|
230 |
else { _head=_data[_head].right_neighbor; } |
|
231 |
merge(head_child); |
|
232 |
} |
|
233 |
_min=findMin(); |
|
234 |
--_num_items; |
|
235 |
} |
|
236 |
|
|
237 |
/// \brief Remove the given item from the heap. |
|
238 |
/// |
|
239 |
/// This function removes the given item from the heap if it is |
|
240 |
/// already stored. |
|
241 |
/// \param item The item to delete. |
|
242 |
/// \pre \e item must be in the heap. |
|
243 |
void erase (const Item& item) { |
|
244 |
int i=_iim[item]; |
|
245 |
if ( i >= 0 && _data[i].in ) { |
|
246 |
decrease( item, _data[_min].prio-1 ); |
|
247 |
pop(); |
|
248 |
} |
|
249 |
} |
|
250 |
|
|
251 |
/// \brief Decrease the priority of an item to the given value. |
|
252 |
/// |
|
253 |
/// This function decreases the priority of an item to the given value. |
|
254 |
/// \param item The item. |
|
255 |
/// \param value The priority. |
|
256 |
/// \pre \e item must be stored in the heap with priority at least \e value. |
|
257 |
void decrease (Item item, const Prio& value) { |
|
258 |
int i=_iim[item]; |
|
259 |
int p=_data[i].parent; |
|
260 |
_data[i].prio=value; |
|
261 |
|
|
262 |
while( p!=-1 && _comp(value, _data[p].prio) ) { |
|
263 |
_data[i].name=_data[p].name; |
|
264 |
_data[i].prio=_data[p].prio; |
|
265 |
_data[p].name=item; |
|
266 |
_data[p].prio=value; |
|
267 |
_iim[_data[i].name]=i; |
|
268 |
i=p; |
|
269 |
p=_data[p].parent; |
|
270 |
} |
|
271 |
_iim[item]=i; |
|
272 |
if ( _comp(value, _data[_min].prio) ) _min=i; |
|
273 |
} |
|
274 |
|
|
275 |
/// \brief Increase the priority of an item to the given value. |
|
276 |
/// |
|
277 |
/// This function increases the priority of an item to the given value. |
|
278 |
/// \param item The item. |
|
279 |
/// \param value The priority. |
|
280 |
/// \pre \e item must be stored in the heap with priority at most \e value. |
|
281 |
void increase (Item item, const Prio& value) { |
|
282 |
erase(item); |
|
283 |
push(item, value); |
|
284 |
} |
|
285 |
|
|
286 |
/// \brief Return the state of an item. |
|
287 |
/// |
|
288 |
/// This method returns \c PRE_HEAP if the given item has never |
|
289 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
|
290 |
/// and \c POST_HEAP otherwise. |
|
291 |
/// In the latter case it is possible that the item will get back |
|
292 |
/// to the heap again. |
|
293 |
/// \param item The item. |
|
294 |
State state(const Item &item) const { |
|
295 |
int i=_iim[item]; |
|
296 |
if( i>=0 ) { |
|
297 |
if ( _data[i].in ) i=0; |
|
298 |
else i=-2; |
|
299 |
} |
|
300 |
return State(i); |
|
301 |
} |
|
302 |
|
|
303 |
/// \brief Set the state of an item in the heap. |
|
304 |
/// |
|
305 |
/// This function sets the state of the given item in the heap. |
|
306 |
/// It can be used to manually clear the heap when it is important |
|
307 |
/// to achive better time complexity. |
|
308 |
/// \param i The item. |
|
309 |
/// \param st The state. It should not be \c IN_HEAP. |
|
310 |
void state(const Item& i, State st) { |
|
311 |
switch (st) { |
|
312 |
case POST_HEAP: |
|
313 |
case PRE_HEAP: |
|
314 |
if (state(i) == IN_HEAP) { |
|
315 |
erase(i); |
|
316 |
} |
|
317 |
_iim[i] = st; |
|
318 |
break; |
|
319 |
case IN_HEAP: |
|
320 |
break; |
|
321 |
} |
|
322 |
} |
|
323 |
|
|
324 |
private: |
|
325 |
|
|
326 |
// Find the minimum of the roots |
|
327 |
int findMin() { |
|
328 |
if( _head!=-1 ) { |
|
329 |
int min_loc=_head, min_val=_data[_head].prio; |
|
330 |
for( int x=_data[_head].right_neighbor; x!=-1; |
|
331 |
x=_data[x].right_neighbor ) { |
|
332 |
if( _comp( _data[x].prio,min_val ) ) { |
|
333 |
min_val=_data[x].prio; |
|
334 |
min_loc=x; |
|
335 |
} |
|
336 |
} |
|
337 |
return min_loc; |
|
338 |
} |
|
339 |
else return -1; |
|
340 |
} |
|
341 |
|
|
342 |
// Merge the heap with another heap starting at the given position |
|
343 |
void merge(int a) { |
|
344 |
if( _head==-1 || a==-1 ) return; |
|
345 |
if( _data[a].right_neighbor==-1 && |
|
346 |
_data[a].degree<=_data[_head].degree ) { |
|
347 |
_data[a].right_neighbor=_head; |
|
348 |
_head=a; |
|
349 |
} else { |
|
350 |
interleave(a); |
|
351 |
} |
|
352 |
if( _data[_head].right_neighbor==-1 ) return; |
|
353 |
|
|
354 |
int x=_head; |
|
355 |
int x_prev=-1, x_next=_data[x].right_neighbor; |
|
356 |
while( x_next!=-1 ) { |
|
357 |
if( _data[x].degree!=_data[x_next].degree || |
|
358 |
( _data[x_next].right_neighbor!=-1 && |
|
359 |
_data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) { |
|
360 |
x_prev=x; |
|
361 |
x=x_next; |
|
362 |
} |
|
363 |
else { |
|
364 |
if( _comp(_data[x_next].prio,_data[x].prio) ) { |
|
365 |
if( x_prev==-1 ) { |
|
366 |
_head=x_next; |
|
367 |
} else { |
|
368 |
_data[x_prev].right_neighbor=x_next; |
|
369 |
} |
|
370 |
fuse(x,x_next); |
|
371 |
x=x_next; |
|
372 |
} |
|
373 |
else { |
|
374 |
_data[x].right_neighbor=_data[x_next].right_neighbor; |
|
375 |
fuse(x_next,x); |
|
376 |
} |
|
377 |
} |
|
378 |
x_next=_data[x].right_neighbor; |
|
379 |
} |
|
380 |
} |
|
381 |
|
|
382 |
// Interleave the elements of the given list into the list of the roots |
|
383 |
void interleave(int a) { |
|
384 |
int p=_head, q=a; |
|
385 |
int curr=_data.size(); |
|
386 |
_data.push_back(Store()); |
|
387 |
|
|
388 |
while( p!=-1 || q!=-1 ) { |
|
389 |
if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) { |
|
390 |
_data[curr].right_neighbor=p; |
|
391 |
curr=p; |
|
392 |
p=_data[p].right_neighbor; |
|
393 |
} |
|
394 |
else { |
|
395 |
_data[curr].right_neighbor=q; |
|
396 |
curr=q; |
|
397 |
q=_data[q].right_neighbor; |
|
398 |
} |
|
399 |
} |
|
400 |
|
|
401 |
_head=_data.back().right_neighbor; |
|
402 |
_data.pop_back(); |
|
403 |
} |
|
404 |
|
|
405 |
// Lace node a under node b |
|
406 |
void fuse(int a, int b) { |
|
407 |
_data[a].parent=b; |
|
408 |
_data[a].right_neighbor=_data[b].child; |
|
409 |
_data[b].child=a; |
|
410 |
|
|
411 |
++_data[b].degree; |
|
412 |
} |
|
413 |
|
|
414 |
// Unlace node a (if it has siblings) |
|
415 |
void unlace(int a) { |
|
416 |
int neighb=_data[a].right_neighbor; |
|
417 |
int other=_head; |
|
418 |
|
|
419 |
while( _data[other].right_neighbor!=a ) |
|
420 |
other=_data[other].right_neighbor; |
|
421 |
_data[other].right_neighbor=neighb; |
|
422 |
} |
|
423 |
|
|
424 |
private: |
|
425 |
|
|
426 |
class Store { |
|
427 |
friend class BinomialHeap; |
|
428 |
|
|
429 |
Item name; |
|
430 |
int parent; |
|
431 |
int right_neighbor; |
|
432 |
int child; |
|
433 |
int degree; |
|
434 |
bool in; |
|
435 |
Prio prio; |
|
436 |
|
|
437 |
Store() : parent(-1), right_neighbor(-1), child(-1), degree(0), |
|
438 |
in(true) {} |
|
439 |
}; |
|
440 |
}; |
|
441 |
|
|
442 |
} //namespace lemon |
|
443 |
|
|
444 |
#endif //LEMON_BINOMIAL_HEAP_H |
|
445 |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2010 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_CAPACITY_SCALING_H |
|
20 |
#define LEMON_CAPACITY_SCALING_H |
|
21 |
|
|
22 |
/// \ingroup min_cost_flow_algs |
|
23 |
/// |
|
24 |
/// \file |
|
25 |
/// \brief Capacity Scaling algorithm for finding a minimum cost flow. |
|
26 |
|
|
27 |
#include <vector> |
|
28 |
#include <limits> |
|
29 |
#include <lemon/core.h> |
|
30 |
#include <lemon/bin_heap.h> |
|
31 |
|
|
32 |
namespace lemon { |
|
33 |
|
|
34 |
/// \brief Default traits class of CapacityScaling algorithm. |
|
35 |
/// |
|
36 |
/// Default traits class of CapacityScaling algorithm. |
|
37 |
/// \tparam GR Digraph type. |
|
38 |
/// \tparam V The number type used for flow amounts, capacity bounds |
|
39 |
/// and supply values. By default it is \c int. |
|
40 |
/// \tparam C The number type used for costs and potentials. |
|
41 |
/// By default it is the same as \c V. |
|
42 |
template <typename GR, typename V = int, typename C = V> |
|
43 |
struct CapacityScalingDefaultTraits |
|
44 |
{ |
|
45 |
/// The type of the digraph |
|
46 |
typedef GR Digraph; |
|
47 |
/// The type of the flow amounts, capacity bounds and supply values |
|
48 |
typedef V Value; |
|
49 |
/// The type of the arc costs |
|
50 |
typedef C Cost; |
|
51 |
|
|
52 |
/// \brief The type of the heap used for internal Dijkstra computations. |
|
53 |
/// |
|
54 |
/// The type of the heap used for internal Dijkstra computations. |
|
55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
|
56 |
/// its priority type must be \c Cost and its cross reference type |
|
57 |
/// must be \ref RangeMap "RangeMap<int>". |
|
58 |
typedef BinHeap<Cost, RangeMap<int> > Heap; |
|
59 |
}; |
|
60 |
|
|
61 |
/// \addtogroup min_cost_flow_algs |
|
62 |
/// @{ |
|
63 |
|
|
64 |
/// \brief Implementation of the Capacity Scaling algorithm for |
|
65 |
/// finding a \ref min_cost_flow "minimum cost flow". |
|
66 |
/// |
|
67 |
/// \ref CapacityScaling implements the capacity scaling version |
|
68 |
/// of the successive shortest path algorithm for finding a |
|
69 |
/// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
|
70 |
/// \ref edmondskarp72theoretical. It is an efficient dual |
|
71 |
/// solution method. |
|
72 |
/// |
|
73 |
/// Most of the parameters of the problem (except for the digraph) |
|
74 |
/// can be given using separate functions, and the algorithm can be |
|
75 |
/// executed using the \ref run() function. If some parameters are not |
|
76 |
/// specified, then default values will be used. |
|
77 |
/// |
|
78 |
/// \tparam GR The digraph type the algorithm runs on. |
|
79 |
/// \tparam V The number type used for flow amounts, capacity bounds |
|
80 |
/// and supply values in the algorithm. By default, it is \c int. |
|
81 |
/// \tparam C The number type used for costs and potentials in the |
|
82 |
/// algorithm. By default, it is the same as \c V. |
|
83 |
/// \tparam TR The traits class that defines various types used by the |
|
84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
|
85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
|
86 |
/// In most cases, this parameter should not be set directly, |
|
87 |
/// consider to use the named template parameters instead. |
|
88 |
/// |
|
89 |
/// \warning Both number types must be signed and all input data must |
|
90 |
/// be integer. |
|
91 |
/// \warning This algorithm does not support negative costs for such |
|
92 |
/// arcs that have infinite upper bound. |
|
93 |
#ifdef DOXYGEN |
|
94 |
template <typename GR, typename V, typename C, typename TR> |
|
95 |
#else |
|
96 |
template < typename GR, typename V = int, typename C = V, |
|
97 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
|
98 |
#endif |
|
99 |
class CapacityScaling |
|
100 |
{ |
|
101 |
public: |
|
102 |
|
|
103 |
/// The type of the digraph |
|
104 |
typedef typename TR::Digraph Digraph; |
|
105 |
/// The type of the flow amounts, capacity bounds and supply values |
|
106 |
typedef typename TR::Value Value; |
|
107 |
/// The type of the arc costs |
|
108 |
typedef typename TR::Cost Cost; |
|
109 |
|
|
110 |
/// The type of the heap used for internal Dijkstra computations |
|
111 |
typedef typename TR::Heap Heap; |
|
112 |
|
|
113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
|
114 |
typedef TR Traits; |
|
115 |
|
|
116 |
public: |
|
117 |
|
|
118 |
/// \brief Problem type constants for the \c run() function. |
|
119 |
/// |
|
120 |
/// Enum type containing the problem type constants that can be |
|
121 |
/// returned by the \ref run() function of the algorithm. |
|
122 |
enum ProblemType { |
|
123 |
/// The problem has no feasible solution (flow). |
|
124 |
INFEASIBLE, |
|
125 |
/// The problem has optimal solution (i.e. it is feasible and |
|
126 |
/// bounded), and the algorithm has found optimal flow and node |
|
127 |
/// potentials (primal and dual solutions). |
|
128 |
OPTIMAL, |
|
129 |
/// The digraph contains an arc of negative cost and infinite |
|
130 |
/// upper bound. It means that the objective function is unbounded |
|
131 |
/// on that arc, however, note that it could actually be bounded |
|
132 |
/// over the feasible flows, but this algroithm cannot handle |
|
133 |
/// these cases. |
|
134 |
UNBOUNDED |
|
135 |
}; |
|
136 |
|
|
137 |
private: |
|
138 |
|
|
139 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
|
140 |
|
|
141 |
typedef std::vector<int> IntVector; |
|
142 |
typedef std::vector<Value> ValueVector; |
|
143 |
typedef std::vector<Cost> CostVector; |
|
144 |
typedef std::vector<char> BoolVector; |
|
145 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
|
146 |
|
|
147 |
private: |
|
148 |
|
|
149 |
// Data related to the underlying digraph |
|
150 |
const GR &_graph; |
|
151 |
int _node_num; |
|
152 |
int _arc_num; |
|
153 |
int _res_arc_num; |
|
154 |
int _root; |
|
155 |
|
|
156 |
// Parameters of the problem |
|
157 |
bool _have_lower; |
|
158 |
Value _sum_supply; |
|
159 |
|
|
160 |
// Data structures for storing the digraph |
|
161 |
IntNodeMap _node_id; |
|
162 |
IntArcMap _arc_idf; |
|
163 |
IntArcMap _arc_idb; |
|
164 |
IntVector _first_out; |
|
165 |
BoolVector _forward; |
|
166 |
IntVector _source; |
|
167 |
IntVector _target; |
|
168 |
IntVector _reverse; |
|
169 |
|
|
170 |
// Node and arc data |
|
171 |
ValueVector _lower; |
|
172 |
ValueVector _upper; |
|
173 |
CostVector _cost; |
|
174 |
ValueVector _supply; |
|
175 |
|
|
176 |
ValueVector _res_cap; |
|
177 |
CostVector _pi; |
|
178 |
ValueVector _excess; |
|
179 |
IntVector _excess_nodes; |
|
180 |
IntVector _deficit_nodes; |
|
181 |
|
|
182 |
Value _delta; |
|
183 |
int _factor; |
|
184 |
IntVector _pred; |
|
185 |
|
|
186 |
public: |
|
187 |
|
|
188 |
/// \brief Constant for infinite upper bounds (capacities). |
|
189 |
/// |
|
190 |
/// Constant for infinite upper bounds (capacities). |
|
191 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
|
192 |
/// \c std::numeric_limits<Value>::max() otherwise. |
|
193 |
const Value INF; |
|
194 |
|
|
195 |
private: |
|
196 |
|
|
197 |
// Special implementation of the Dijkstra algorithm for finding |
|
198 |
// shortest paths in the residual network of the digraph with |
|
199 |
// respect to the reduced arc costs and modifying the node |
|
200 |
// potentials according to the found distance labels. |
|
201 |
class ResidualDijkstra |
|
202 |
{ |
|
203 |
private: |
|
204 |
|
|
205 |
int _node_num; |
|
206 |
bool _geq; |
|
207 |
const IntVector &_first_out; |
|
208 |
const IntVector &_target; |
|
209 |
const CostVector &_cost; |
|
210 |
const ValueVector &_res_cap; |
|
211 |
const ValueVector &_excess; |
|
212 |
CostVector &_pi; |
|
213 |
IntVector &_pred; |
|
214 |
|
|
215 |
IntVector _proc_nodes; |
|
216 |
CostVector _dist; |
|
217 |
|
|
218 |
public: |
|
219 |
|
|
220 |
ResidualDijkstra(CapacityScaling& cs) : |
|
221 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
|
222 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
|
223 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
|
224 |
_pred(cs._pred), _dist(cs._node_num) |
|
225 |
{} |
|
226 |
|
|
227 |
int run(int s, Value delta = 1) { |
|
228 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
|
229 |
Heap heap(heap_cross_ref); |
|
230 |
heap.push(s, 0); |
|
231 |
_pred[s] = -1; |
|
232 |
_proc_nodes.clear(); |
|
233 |
|
|
234 |
// Process nodes |
|
235 |
while (!heap.empty() && _excess[heap.top()] > -delta) { |
|
236 |
int u = heap.top(), v; |
|
237 |
Cost d = heap.prio() + _pi[u], dn; |
|
238 |
_dist[u] = heap.prio(); |
|
239 |
_proc_nodes.push_back(u); |
|
240 |
heap.pop(); |
|
241 |
|
|
242 |
// Traverse outgoing residual arcs |
|
243 |
int last_out = _geq ? _first_out[u+1] : _first_out[u+1] - 1; |
|
244 |
for (int a = _first_out[u]; a != last_out; ++a) { |
|
245 |
if (_res_cap[a] < delta) continue; |
|
246 |
v = _target[a]; |
|
247 |
switch (heap.state(v)) { |
|
248 |
case Heap::PRE_HEAP: |
|
249 |
heap.push(v, d + _cost[a] - _pi[v]); |
|
250 |
_pred[v] = a; |
|
251 |
break; |
|
252 |
case Heap::IN_HEAP: |
|
253 |
dn = d + _cost[a] - _pi[v]; |
|
254 |
if (dn < heap[v]) { |
|
255 |
heap.decrease(v, dn); |
|
256 |
_pred[v] = a; |
|
257 |
} |
|
258 |
break; |
|
259 |
case Heap::POST_HEAP: |
|
260 |
break; |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
if (heap.empty()) return -1; |
|
265 |
|
|
266 |
// Update potentials of processed nodes |
|
267 |
int t = heap.top(); |
|
268 |
Cost dt = heap.prio(); |
|
269 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) { |
|
270 |
_pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - dt; |
|
271 |
} |
|
272 |
|
|
273 |
return t; |
|
274 |
} |
|
275 |
|
|
276 |
}; //class ResidualDijkstra |
|
277 |
|
|
278 |
public: |
|
279 |
|
|
280 |
/// \name Named Template Parameters |
|
281 |
/// @{ |
|
282 |
|
|
283 |
template <typename T> |
|
284 |
struct SetHeapTraits : public Traits { |
|
285 |
typedef T Heap; |
|
286 |
}; |
|
287 |
|
|
288 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
289 |
/// \c Heap type. |
|
290 |
/// |
|
291 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
|
292 |
/// type, which is used for internal Dijkstra computations. |
|
293 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
|
294 |
/// its priority type must be \c Cost and its cross reference type |
|
295 |
/// must be \ref RangeMap "RangeMap<int>". |
|
296 |
template <typename T> |
|
297 |
struct SetHeap |
|
298 |
: public CapacityScaling<GR, V, C, SetHeapTraits<T> > { |
|
299 |
typedef CapacityScaling<GR, V, C, SetHeapTraits<T> > Create; |
|
300 |
}; |
|
301 |
|
|
302 |
/// @} |
|
303 |
|
|
304 |
protected: |
|
305 |
|
|
306 |
CapacityScaling() {} |
|
307 |
|
|
308 |
public: |
|
309 |
|
|
310 |
/// \brief Constructor. |
|
311 |
/// |
|
312 |
/// The constructor of the class. |
|
313 |
/// |
|
314 |
/// \param graph The digraph the algorithm runs on. |
|
315 |
CapacityScaling(const GR& graph) : |
|
316 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
|
317 |
INF(std::numeric_limits<Value>::has_infinity ? |
|
318 |
std::numeric_limits<Value>::infinity() : |
|
319 |
std::numeric_limits<Value>::max()) |
|
320 |
{ |
|
321 |
// Check the number types |
|
322 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
|
323 |
"The flow type of CapacityScaling must be signed"); |
|
324 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
|
325 |
"The cost type of CapacityScaling must be signed"); |
|
326 |
|
|
327 |
// Reset data structures |
|
328 |
reset(); |
|
329 |
} |
|
330 |
|
|
331 |
/// \name Parameters |
|
332 |
/// The parameters of the algorithm can be specified using these |
|
333 |
/// functions. |
|
334 |
|
|
335 |
/// @{ |
|
336 |
|
|
337 |
/// \brief Set the lower bounds on the arcs. |
|
338 |
/// |
|
339 |
/// This function sets the lower bounds on the arcs. |
|
340 |
/// If it is not used before calling \ref run(), the lower bounds |
|
341 |
/// will be set to zero on all arcs. |
|
342 |
/// |
|
343 |
/// \param map An arc map storing the lower bounds. |
|
344 |
/// Its \c Value type must be convertible to the \c Value type |
|
345 |
/// of the algorithm. |
|
346 |
/// |
|
347 |
/// \return <tt>(*this)</tt> |
|
348 |
template <typename LowerMap> |
|
349 |
CapacityScaling& lowerMap(const LowerMap& map) { |
|
350 |
_have_lower = true; |
|
351 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
|
352 |
_lower[_arc_idf[a]] = map[a]; |
|
353 |
_lower[_arc_idb[a]] = map[a]; |
|
354 |
} |
|
355 |
return *this; |
|
356 |
} |
|
357 |
|
|
358 |
/// \brief Set the upper bounds (capacities) on the arcs. |
|
359 |
/// |
|
360 |
/// This function sets the upper bounds (capacities) on the arcs. |
|
361 |
/// If it is not used before calling \ref run(), the upper bounds |
|
362 |
/// will be set to \ref INF on all arcs (i.e. the flow value will be |
|
363 |
/// unbounded from above). |
|
364 |
/// |
|
365 |
/// \param map An arc map storing the upper bounds. |
|
366 |
/// Its \c Value type must be convertible to the \c Value type |
|
367 |
/// of the algorithm. |
|
368 |
/// |
|
369 |
/// \return <tt>(*this)</tt> |
|
370 |
template<typename UpperMap> |
|
371 |
CapacityScaling& upperMap(const UpperMap& map) { |
|
372 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
|
373 |
_upper[_arc_idf[a]] = map[a]; |
|
374 |
} |
|
375 |
return *this; |
|
376 |
} |
|
377 |
|
|
378 |
/// \brief Set the costs of the arcs. |
|
379 |
/// |
|
380 |
/// This function sets the costs of the arcs. |
|
381 |
/// If it is not used before calling \ref run(), the costs |
|
382 |
/// will be set to \c 1 on all arcs. |
|
383 |
/// |
|
384 |
/// \param map An arc map storing the costs. |
|
385 |
/// Its \c Value type must be convertible to the \c Cost type |
|
386 |
/// of the algorithm. |
|
387 |
/// |
|
388 |
/// \return <tt>(*this)</tt> |
|
389 |
template<typename CostMap> |
|
390 |
CapacityScaling& costMap(const CostMap& map) { |
|
391 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
|
392 |
_cost[_arc_idf[a]] = map[a]; |
|
393 |
_cost[_arc_idb[a]] = -map[a]; |
|
394 |
} |
|
395 |
return *this; |
|
396 |
} |
|
397 |
|
|
398 |
/// \brief Set the supply values of the nodes. |
|
399 |
/// |
|
400 |
/// This function sets the supply values of the nodes. |
|
401 |
/// If neither this function nor \ref stSupply() is used before |
|
402 |
/// calling \ref run(), the supply of each node will be set to zero. |
|
403 |
/// |
|
404 |
/// \param map A node map storing the supply values. |
|
405 |
/// Its \c Value type must be convertible to the \c Value type |
|
406 |
/// of the algorithm. |
|
407 |
/// |
|
408 |
/// \return <tt>(*this)</tt> |
|
409 |
template<typename SupplyMap> |
|
410 |
CapacityScaling& supplyMap(const SupplyMap& map) { |
|
411 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
|
412 |
_supply[_node_id[n]] = map[n]; |
|
413 |
} |
|
414 |
return *this; |
|
415 |
} |
|
416 |
|
|
417 |
/// \brief Set single source and target nodes and a supply value. |
|
418 |
/// |
|
419 |
/// This function sets a single source node and a single target node |
|
420 |
/// and the required flow value. |
|
421 |
/// If neither this function nor \ref supplyMap() is used before |
|
422 |
/// calling \ref run(), the supply of each node will be set to zero. |
|
423 |
/// |
|
424 |
/// Using this function has the same effect as using \ref supplyMap() |
|
425 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
|
426 |
/// assigned to \c t and all other nodes have zero supply value. |
|
427 |
/// |
|
428 |
/// \param s The source node. |
|
429 |
/// \param t The target node. |
|
430 |
/// \param k The required amount of flow from node \c s to node \c t |
|
431 |
/// (i.e. the supply of \c s and the demand of \c t). |
|
432 |
/// |
|
433 |
/// \return <tt>(*this)</tt> |
|
434 |
CapacityScaling& stSupply(const Node& s, const Node& t, Value k) { |
|
435 |
for (int i = 0; i != _node_num; ++i) { |
|
436 |
_supply[i] = 0; |
|
437 |
} |
|
438 |
_supply[_node_id[s]] = k; |
|
439 |
_supply[_node_id[t]] = -k; |
|
440 |
return *this; |
|
441 |
} |
|
442 |
|
|
443 |
/// @} |
|
444 |
|
|
445 |
/// \name Execution control |
|
446 |
/// The algorithm can be executed using \ref run(). |
|
447 |
|
|
448 |
/// @{ |
|
449 |
|
|
450 |
/// \brief Run the algorithm. |
|
451 |
/// |
|
452 |
/// This function runs the algorithm. |
|
453 |
/// The paramters can be specified using functions \ref lowerMap(), |
|
454 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
|
455 |
/// For example, |
|
456 |
/// \code |
|
457 |
/// CapacityScaling<ListDigraph> cs(graph); |
|
458 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
|
459 |
/// .supplyMap(sup).run(); |
|
460 |
/// \endcode |
|
461 |
/// |
|
462 |
/// This function can be called more than once. All the given parameters |
|
463 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
|
464 |
/// is used, thus only the modified parameters have to be set again. |
|
465 |
/// If the underlying digraph was also modified after the construction |
|
466 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
|
467 |
/// function must be called. |
|
468 |
/// |
|
469 |
/// \param factor The capacity scaling factor. It must be larger than |
|
470 |
/// one to use scaling. If it is less or equal to one, then scaling |
|
471 |
/// will be disabled. |
|
472 |
/// |
|
473 |
/// \return \c INFEASIBLE if no feasible flow exists, |
|
474 |
/// \n \c OPTIMAL if the problem has optimal solution |
|
475 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
|
476 |
/// optimal flow and node potentials (primal and dual solutions), |
|
477 |
/// \n \c UNBOUNDED if the digraph contains an arc of negative cost |
|
478 |
/// and infinite upper bound. It means that the objective function |
|
479 |
/// is unbounded on that arc, however, note that it could actually be |
|
480 |
/// bounded over the feasible flows, but this algroithm cannot handle |
|
481 |
/// these cases. |
|
482 |
/// |
|
483 |
/// \see ProblemType |
|
484 |
/// \see resetParams(), reset() |
|
485 |
ProblemType run(int factor = 4) { |
|
486 |
_factor = factor; |
|
487 |
ProblemType pt = init(); |
|
488 |
if (pt != OPTIMAL) return pt; |
|
489 |
return start(); |
|
490 |
} |
|
491 |
|
|
492 |
/// \brief Reset all the parameters that have been given before. |
|
493 |
/// |
|
494 |
/// This function resets all the paramaters that have been given |
|
495 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
|
496 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
|
497 |
/// |
|
498 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
|
499 |
/// parameters are kept for the next \ref run() call, unless |
|
500 |
/// \ref resetParams() or \ref reset() is used. |
|
501 |
/// If the underlying digraph was also modified after the construction |
|
502 |
/// of the class or the last \ref reset() call, then the \ref reset() |
|
503 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
|
504 |
/// |
|
505 |
/// For example, |
|
506 |
/// \code |
|
507 |
/// CapacityScaling<ListDigraph> cs(graph); |
|
508 |
/// |
|
509 |
/// // First run |
|
510 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
|
511 |
/// .supplyMap(sup).run(); |
|
512 |
/// |
|
513 |
/// // Run again with modified cost map (resetParams() is not called, |
|
514 |
/// // so only the cost map have to be set again) |
|
515 |
/// cost[e] += 100; |
|
516 |
/// cs.costMap(cost).run(); |
|
517 |
/// |
|
518 |
/// // Run again from scratch using resetParams() |
|
519 |
/// // (the lower bounds will be set to zero on all arcs) |
|
520 |
/// cs.resetParams(); |
|
521 |
/// cs.upperMap(capacity).costMap(cost) |
|
522 |
/// .supplyMap(sup).run(); |
|
523 |
/// \endcode |
|
524 |
/// |
|
525 |
/// \return <tt>(*this)</tt> |
|
526 |
/// |
|
527 |
/// \see reset(), run() |
|
528 |
CapacityScaling& resetParams() { |
|
529 |
for (int i = 0; i != _node_num; ++i) { |
|
530 |
_supply[i] = 0; |
|
531 |
} |
|
532 |
for (int j = 0; j != _res_arc_num; ++j) { |
|
533 |
_lower[j] = 0; |
|
534 |
_upper[j] = INF; |
|
535 |
_cost[j] = _forward[j] ? 1 : -1; |
|
536 |
} |
|
537 |
_have_lower = false; |
|
538 |
return *this; |
|
539 |
} |
|
540 |
|
|
541 |
/// \brief Reset the internal data structures and all the parameters |
|
542 |
/// that have been given before. |
|
543 |
/// |
|
544 |
/// This function resets the internal data structures and all the |
|
545 |
/// paramaters that have been given before using functions \ref lowerMap(), |
|
546 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
|
547 |
/// |
|
548 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
|
549 |
/// parameters are kept for the next \ref run() call, unless |
|
550 |
/// \ref resetParams() or \ref reset() is used. |
|
551 |
/// If the underlying digraph was also modified after the construction |
|
552 |
/// of the class or the last \ref reset() call, then the \ref reset() |
|
553 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
|
554 |
/// |
|
555 |
/// See \ref resetParams() for examples. |
|
556 |
/// |
|
557 |
/// \return <tt>(*this)</tt> |
|
558 |
/// |
|
559 |
/// \see resetParams(), run() |
|
560 |
CapacityScaling& reset() { |
|
561 |
// Resize vectors |
|
562 |
_node_num = countNodes(_graph); |
|
563 |
_arc_num = countArcs(_graph); |
|
564 |
_res_arc_num = 2 * (_arc_num + _node_num); |
|
565 |
_root = _node_num; |
|
566 |
++_node_num; |
|
567 |
|
|
568 |
_first_out.resize(_node_num + 1); |
|
569 |
_forward.resize(_res_arc_num); |
|
570 |
_source.resize(_res_arc_num); |
|
571 |
_target.resize(_res_arc_num); |
|
572 |
_reverse.resize(_res_arc_num); |
|
573 |
|
|
574 |
_lower.resize(_res_arc_num); |
|
575 |
_upper.resize(_res_arc_num); |
|
576 |
_cost.resize(_res_arc_num); |
|
577 |
_supply.resize(_node_num); |
|
578 |
|
|
579 |
_res_cap.resize(_res_arc_num); |
|
580 |
_pi.resize(_node_num); |
|
581 |
_excess.resize(_node_num); |
|
582 |
_pred.resize(_node_num); |
|
583 |
|
|
584 |
// Copy the graph |
|
585 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1; |
|
586 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
|
587 |
_node_id[n] = i; |
|
588 |
} |
|
589 |
i = 0; |
|
590 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) { |
|
591 |
_first_out[i] = j; |
|
592 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
|
593 |
_arc_idf[a] = j; |
|
594 |
_forward[j] = true; |
|
595 |
_source[j] = i; |
|
596 |
_target[j] = _node_id[_graph.runningNode(a)]; |
|
597 |
} |
|
598 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) { |
|
599 |
_arc_idb[a] = j; |
|
600 |
_forward[j] = false; |
|
601 |
_source[j] = i; |
|
602 |
_target[j] = _node_id[_graph.runningNode(a)]; |
|
603 |
} |
|
604 |
_forward[j] = false; |
|
605 |
_source[j] = i; |
|
606 |
_target[j] = _root; |
|
607 |
_reverse[j] = k; |
|
608 |
_forward[k] = true; |
|
609 |
_source[k] = _root; |
|
610 |
_target[k] = i; |
|
611 |
_reverse[k] = j; |
|
612 |
++j; ++k; |
|
613 |
} |
|
614 |
_first_out[i] = j; |
|
615 |
_first_out[_node_num] = k; |
|
616 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
|
617 |
int fi = _arc_idf[a]; |
|
618 |
int bi = _arc_idb[a]; |
|
619 |
_reverse[fi] = bi; |
|
620 |
_reverse[bi] = fi; |
|
621 |
} |
|
622 |
|
|
623 |
// Reset parameters |
|
624 |
resetParams(); |
|
625 |
return *this; |
|
626 |
} |
|
627 |
|
|
628 |
/// @} |
|
629 |
|
|
630 |
/// \name Query Functions |
|
631 |
/// The results of the algorithm can be obtained using these |
|
632 |
/// functions.\n |
|
633 |
/// The \ref run() function must be called before using them. |
|
634 |
|
|
635 |
/// @{ |
|
636 |
|
|
637 |
/// \brief Return the total cost of the found flow. |
|
638 |
/// |
|
639 |
/// This function returns the total cost of the found flow. |
|
640 |
/// Its complexity is O(e). |
|
641 |
/// |
|
642 |
/// \note The return type of the function can be specified as a |
|
643 |
/// template parameter. For example, |
|
644 |
/// \code |
|
645 |
/// cs.totalCost<double>(); |
|
646 |
/// \endcode |
|
647 |
/// It is useful if the total cost cannot be stored in the \c Cost |
|
648 |
/// type of the algorithm, which is the default return type of the |
|
649 |
/// function. |
|
650 |
/// |
|
651 |
/// \pre \ref run() must be called before using this function. |
|
652 |
template <typename Number> |
|
653 |
Number totalCost() const { |
|
654 |
Number c = 0; |
|
655 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
|
656 |
int i = _arc_idb[a]; |
|
657 |
c += static_cast<Number>(_res_cap[i]) * |
|
658 |
(-static_cast<Number>(_cost[i])); |
|
659 |
} |
|
660 |
return c; |
|
661 |
} |
|
662 |
|
|
663 |
#ifndef DOXYGEN |
|
664 |
Cost totalCost() const { |
|
665 |
return totalCost<Cost>(); |
|
666 |
} |
|
667 |
#endif |
|
668 |
|
|
669 |
/// \brief Return the flow on the given arc. |
|
670 |
/// |
|
671 |
/// This function returns the flow on the given arc. |
|
672 |
/// |
|
673 |
/// \pre \ref run() must be called before using this function. |
|
674 |
Value flow(const Arc& a) const { |
|
675 |
return _res_cap[_arc_idb[a]]; |
|
676 |
} |
|
677 |
|
|
678 |
/// \brief Return the flow map (the primal solution). |
|
679 |
/// |
|
680 |
/// This function copies the flow value on each arc into the given |
|
681 |
/// map. The \c Value type of the algorithm must be convertible to |
|
682 |
/// the \c Value type of the map. |
|
683 |
/// |
|
684 |
/// \pre \ref run() must be called before using this function. |
|
685 |
template <typename FlowMap> |
|
686 |
void flowMap(FlowMap &map) const { |
|
687 |
for (ArcIt a(_graph); a != INVALID; ++a) { |
|
688 |
map.set(a, _res_cap[_arc_idb[a]]); |
|
689 |
} |
|
690 |
} |
|
691 |
|
|
692 |
/// \brief Return the potential (dual value) of the given node. |
|
693 |
/// |
|
694 |
/// This function returns the potential (dual value) of the |
|
695 |
/// given node. |
|
696 |
/// |
|
697 |
/// \pre \ref run() must be called before using this function. |
|
698 |
Cost potential(const Node& n) const { |
|
699 |
return _pi[_node_id[n]]; |
|
700 |
} |
|
701 |
|
|
702 |
/// \brief Return the potential map (the dual solution). |
|
703 |
/// |
|
704 |
/// This function copies the potential (dual value) of each node |
|
705 |
/// into the given map. |
|
706 |
/// The \c Cost type of the algorithm must be convertible to the |
|
707 |
/// \c Value type of the map. |
|
708 |
/// |
|
709 |
/// \pre \ref run() must be called before using this function. |
|
710 |
template <typename PotentialMap> |
|
711 |
void potentialMap(PotentialMap &map) const { |
|
712 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
|
713 |
map.set(n, _pi[_node_id[n]]); |
|
714 |
} |
|
715 |
} |
|
716 |
|
|
717 |
/// @} |
|
718 |
|
|
719 |
private: |
|
720 |
|
|
721 |
// Initialize the algorithm |
|
722 |
ProblemType init() { |
|
723 |
if (_node_num <= 1) return INFEASIBLE; |
|
724 |
|
|
725 |
// Check the sum of supply values |
|
726 |
_sum_supply = 0; |
|
727 |
for (int i = 0; i != _root; ++i) { |
|
728 |
_sum_supply += _supply[i]; |
|
729 |
} |
|
730 |
if (_sum_supply > 0) return INFEASIBLE; |
|
731 |
|
|
732 |
// Initialize vectors |
|
733 |
for (int i = 0; i != _root; ++i) { |
|
734 |
_pi[i] = 0; |
|
735 |
_excess[i] = _supply[i]; |
|
736 |
} |
|
737 |
|
|
738 |
// Remove non-zero lower bounds |
|
739 |
const Value MAX = std::numeric_limits<Value>::max(); |
|
740 |
int last_out; |
|
741 |
if (_have_lower) { |
|
742 |
for (int i = 0; i != _root; ++i) { |
|
743 |
last_out = _first_out[i+1]; |
|
744 |
for (int j = _first_out[i]; j != last_out; ++j) { |
|
745 |
if (_forward[j]) { |
|
746 |
Value c = _lower[j]; |
|
747 |
if (c >= 0) { |
|
748 |
_res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF; |
|
749 |
} else { |
|
750 |
_res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF; |
|
751 |
} |
|
752 |
_excess[i] -= c; |
|
753 |
_excess[_target[j]] += c; |
|
754 |
} else { |
|
755 |
_res_cap[j] = 0; |
|
756 |
} |
|
757 |
} |
|
758 |
} |
|
759 |
} else { |
|
760 |
for (int j = 0; j != _res_arc_num; ++j) { |
|
761 |
_res_cap[j] = _forward[j] ? _upper[j] : 0; |
|
762 |
} |
|
763 |
} |
|
764 |
|
|
765 |
// Handle negative costs |
|
766 |
for (int i = 0; i != _root; ++i) { |
|
767 |
last_out = _first_out[i+1] - 1; |
|
768 |
for (int j = _first_out[i]; j != last_out; ++j) { |
|
769 |
Value rc = _res_cap[j]; |
|
770 |
if (_cost[j] < 0 && rc > 0) { |
|
771 |
if (rc >= MAX) return UNBOUNDED; |
|
772 |
_excess[i] -= rc; |
|
773 |
_excess[_target[j]] += rc; |
|
774 |
_res_cap[j] = 0; |
|
775 |
_res_cap[_reverse[j]] += rc; |
|
776 |
} |
|
777 |
} |
|
778 |
} |
|
779 |
|
|
780 |
// Handle GEQ supply type |
|
781 |
if (_sum_supply < 0) { |
|
782 |
_pi[_root] = 0; |
|
783 |
_excess[_root] = -_sum_supply; |
|
784 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
|
785 |
int ra = _reverse[a]; |
|
786 |
_res_cap[a] = -_sum_supply + 1; |
|
787 |
_res_cap[ra] = 0; |
|
788 |
_cost[a] = 0; |
|
789 |
_cost[ra] = 0; |
|
790 |
} |
|
791 |
} else { |
|
792 |
_pi[_root] = 0; |
|
793 |
_excess[_root] = 0; |
|
794 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) { |
|
795 |
int ra = _reverse[a]; |
|
796 |
_res_cap[a] = 1; |
|
797 |
_res_cap[ra] = 0; |
|
798 |
_cost[a] = 0; |
|
799 |
_cost[ra] = 0; |
|
800 |
} |
|
801 |
} |
|
802 |
|
|
803 |
// Initialize delta value |
|
804 |
if (_factor > 1) { |
|
805 |
// With scaling |
|
806 |
Value max_sup = 0, max_dem = 0, max_cap = 0; |
|
807 |
for (int i = 0; i != _root; ++i) { |
|
808 |
Value ex = _excess[i]; |
|
809 |
if ( ex > max_sup) max_sup = ex; |
|
810 |
if (-ex > max_dem) max_dem = -ex; |
|
811 |
int last_out = _first_out[i+1] - 1; |
|
812 |
for (int j = _first_out[i]; j != last_out; ++j) { |
|
813 |
if (_res_cap[j] > max_cap) max_cap = _res_cap[j]; |
|
814 |
} |
|
815 |
} |
|
816 |
max_sup = std::min(std::min(max_sup, max_dem), max_cap); |
|
817 |
for (_delta = 1; 2 * _delta <= max_sup; _delta *= 2) ; |
|
818 |
} else { |
|
819 |
// Without scaling |
|
820 |
_delta = 1; |
|
821 |
} |
|
822 |
|
|
823 |
return OPTIMAL; |
|
824 |
} |
|
825 |
|
|
826 |
ProblemType start() { |
|
827 |
// Execute the algorithm |
|
828 |
ProblemType pt; |
|
829 |
if (_delta > 1) |
|
830 |
pt = startWithScaling(); |
|
831 |
else |
|
832 |
pt = startWithoutScaling(); |
|
833 |
|
|
834 |
// Handle non-zero lower bounds |
|
835 |
if (_have_lower) { |
|
836 |
int limit = _first_out[_root]; |
|
837 |
for (int j = 0; j != limit; ++j) { |
|
838 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
|
839 |
} |
|
840 |
} |
|
841 |
|
|
842 |
// Shift potentials if necessary |
|
843 |
Cost pr = _pi[_root]; |
|
844 |
if (_sum_supply < 0 || pr > 0) { |
|
845 |
for (int i = 0; i != _node_num; ++i) { |
|
846 |
_pi[i] -= pr; |
|
847 |
} |
|
848 |
} |
|
849 |
|
|
850 |
return pt; |
|
851 |
} |
|
852 |
|
|
853 |
// Execute the capacity scaling algorithm |
|
854 |
ProblemType startWithScaling() { |
|
855 |
// Perform capacity scaling phases |
|
856 |
int s, t; |
|
857 |
ResidualDijkstra _dijkstra(*this); |
|
858 |
while (true) { |
|
859 |
// Saturate all arcs not satisfying the optimality condition |
|
860 |
int last_out; |
|
861 |
for (int u = 0; u != _node_num; ++u) { |
|
862 |
last_out = _sum_supply < 0 ? |
|
863 |
_first_out[u+1] : _first_out[u+1] - 1; |
|
864 |
for (int a = _first_out[u]; a != last_out; ++a) { |
|
865 |
int v = _target[a]; |
|
866 |
Cost c = _cost[a] + _pi[u] - _pi[v]; |
|
867 |
Value rc = _res_cap[a]; |
|
868 |
if (c < 0 && rc >= _delta) { |
|
869 |
_excess[u] -= rc; |
|
870 |
_excess[v] += rc; |
|
871 |
_res_cap[a] = 0; |
|
872 |
_res_cap[_reverse[a]] += rc; |
|
873 |
} |
|
874 |
} |
|
875 |
} |
|
876 |
|
|
877 |
// Find excess nodes and deficit nodes |
|
878 |
_excess_nodes.clear(); |
|
879 |
_deficit_nodes.clear(); |
|
880 |
for (int u = 0; u != _node_num; ++u) { |
|
881 |
Value ex = _excess[u]; |
|
882 |
if (ex >= _delta) _excess_nodes.push_back(u); |
|
883 |
if (ex <= -_delta) _deficit_nodes.push_back(u); |
|
884 |
} |
|
885 |
int next_node = 0, next_def_node = 0; |
|
886 |
|
|
887 |
// Find augmenting shortest paths |
|
888 |
while (next_node < int(_excess_nodes.size())) { |
|
889 |
// Check deficit nodes |
|
890 |
if (_delta > 1) { |
|
891 |
bool delta_deficit = false; |
|
892 |
for ( ; next_def_node < int(_deficit_nodes.size()); |
|
893 |
++next_def_node ) { |
|
894 |
if (_excess[_deficit_nodes[next_def_node]] <= -_delta) { |
|
895 |
delta_deficit = true; |
|
896 |
break; |
|
897 |
} |
|
898 |
} |
|
899 |
if (!delta_deficit) break; |
|
900 |
} |
|
901 |
|
|
902 |
// Run Dijkstra in the residual network |
|
903 |
s = _excess_nodes[next_node]; |
|
904 |
if ((t = _dijkstra.run(s, _delta)) == -1) { |
|
905 |
if (_delta > 1) { |
|
906 |
++next_node; |
|
907 |
continue; |
|
908 |
} |
|
909 |
return INFEASIBLE; |
|
910 |
} |
|
911 |
|
|
912 |
// Augment along a shortest path from s to t |
|
913 |
Value d = std::min(_excess[s], -_excess[t]); |
|
914 |
int u = t; |
|
915 |
int a; |
|
916 |
if (d > _delta) { |
|
917 |
while ((a = _pred[u]) != -1) { |
|
918 |
if (_res_cap[a] < d) d = _res_cap[a]; |
|
919 |
u = _source[a]; |
|
920 |
} |
|
921 |
} |
|
922 |
u = t; |
|
923 |
while ((a = _pred[u]) != -1) { |
|
924 |
_res_cap[a] -= d; |
|
925 |
_res_cap[_reverse[a]] += d; |
|
926 |
u = _source[a]; |
|
927 |
} |
|
928 |
_excess[s] -= d; |
|
929 |
_excess[t] += d; |
|
930 |
|
|
931 |
if (_excess[s] < _delta) ++next_node; |
|
932 |
} |
|
933 |
|
|
934 |
if (_delta == 1) break; |
|
935 |
_delta = _delta <= _factor ? 1 : _delta / _factor; |
|
936 |
} |
|
937 |
|
|
938 |
return OPTIMAL; |
|
939 |
} |
|
940 |
|
|
941 |
// Execute the successive shortest path algorithm |
|
942 |
ProblemType startWithoutScaling() { |
|
943 |
// Find excess nodes |
|
944 |
_excess_nodes.clear(); |
|
945 |
for (int i = 0; i != _node_num; ++i) { |
|
946 |
if (_excess[i] > 0) _excess_nodes.push_back(i); |
|
947 |
} |
|
948 |
if (_excess_nodes.size() == 0) return OPTIMAL; |
|
949 |
int next_node = 0; |
|
950 |
|
|
951 |
// Find shortest paths |
|
952 |
int s, t; |
|
953 |
ResidualDijkstra _dijkstra(*this); |
|
954 |
while ( _excess[_excess_nodes[next_node]] > 0 || |
|
955 |
++next_node < int(_excess_nodes.size()) ) |
|
956 |
{ |
|
957 |
// Run Dijkstra in the residual network |
|
958 |
s = _excess_nodes[next_node]; |
|
959 |
if ((t = _dijkstra.run(s)) == -1) return INFEASIBLE; |
|
960 |
|
|
961 |
// Augment along a shortest path from s to t |
|
962 |
Value d = std::min(_excess[s], -_excess[t]); |
|
963 |
int u = t; |
|
964 |
int a; |
|
965 |
if (d > 1) { |
|
966 |
while ((a = _pred[u]) != -1) { |
|
967 |
if (_res_cap[a] < d) d = _res_cap[a]; |
|
968 |
u = _source[a]; |
|
969 |
} |
|
970 |
} |
|
971 |
u = t; |
|
972 |
while ((a = _pred[u]) != -1) { |
|
973 |
_res_cap[a] -= d; |
|
974 |
_res_cap[_reverse[a]] += d; |
|
975 |
u = _source[a]; |
|
976 |
} |
|
977 |
_excess[s] -= d; |
|
978 |
_excess[t] += d; |
|
979 |
} |
|
980 |
|
|
981 |
return OPTIMAL; |
|
982 |
} |
|
983 |
|
|
984 |
}; //class CapacityScaling |
|
985 |
|
|
986 |
///@} |
|
987 |
|
|
988 |
} //namespace lemon |
|
989 |
|
|
990 |
#endif //LEMON_CAPACITY_SCALING_H |
... | ... |
@@ -13,24 +13,25 @@ |
13 | 13 |
config.h.in |
14 | 14 |
configure |
15 | 15 |
Makefile |
16 | 16 |
config.h |
17 | 17 |
config.log |
18 | 18 |
config.status |
19 | 19 |
libtool |
20 | 20 |
stamp-h1 |
21 | 21 |
lemon/lemon.pc |
22 | 22 |
lemon/libemon.la |
23 | 23 |
lemon/stamp-h2 |
24 | 24 |
doc/Doxyfile |
25 |
doc/references.dox |
|
25 | 26 |
cmake/version.cmake |
26 | 27 |
.dirstamp |
27 | 28 |
.libs/* |
28 | 29 |
.deps/* |
29 | 30 |
demo/*.eps |
30 | 31 |
m4/libtool.m4 |
31 | 32 |
m4/ltoptions.m4 |
32 | 33 |
m4/ltsugar.m4 |
33 | 34 |
m4/ltversion.m4 |
34 | 35 |
m4/lt~obsolete.m4 |
35 | 36 |
|
36 | 37 |
syntax: regexp |
... | ... |
@@ -105,24 +105,26 @@ |
105 | 105 |
SET(CMAKE_BUILD_TYPE "Release") |
106 | 106 |
ENDIF() |
107 | 107 |
|
108 | 108 |
SET( CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING |
109 | 109 |
"Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel Maintainer." |
110 | 110 |
FORCE ) |
111 | 111 |
|
112 | 112 |
|
113 | 113 |
INCLUDE(CheckTypeSize) |
114 | 114 |
CHECK_TYPE_SIZE("long long" LONG_LONG) |
115 | 115 |
SET(LEMON_HAVE_LONG_LONG ${HAVE_LONG_LONG}) |
116 | 116 |
|
117 |
INCLUDE(FindPythonInterp) |
|
118 |
|
|
117 | 119 |
ENABLE_TESTING() |
118 | 120 |
|
119 | 121 |
IF(${CMAKE_BUILD_TYPE} STREQUAL "Maintainer") |
120 | 122 |
ADD_CUSTOM_TARGET(check ALL COMMAND ${CMAKE_CTEST_COMMAND}) |
121 | 123 |
ELSE() |
122 | 124 |
ADD_CUSTOM_TARGET(check COMMAND ${CMAKE_CTEST_COMMAND}) |
123 | 125 |
ENDIF() |
124 | 126 |
|
125 | 127 |
ADD_SUBDIRECTORY(lemon) |
126 | 128 |
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) |
127 | 129 |
ADD_SUBDIRECTORY(demo) |
128 | 130 |
ADD_SUBDIRECTORY(tools) |
... | ... |
@@ -164,12 +164,34 @@ |
164 | 164 |
only useful when the COIN-OR headers and libraries are not under |
165 | 165 |
the same prefix (which is unlikely). |
166 | 166 |
|
167 | 167 |
--with-coin-libdir=DIR |
168 | 168 |
|
169 | 169 |
The directory where the COIN-OR libraries are located. This is only |
170 | 170 |
useful when the COIN-OR headers and libraries are not under the |
171 | 171 |
same prefix (which is unlikely). |
172 | 172 |
|
173 | 173 |
--without-coin |
174 | 174 |
|
175 | 175 |
Disable COIN-OR support. |
176 |
|
|
177 |
|
|
178 |
Makefile Variables |
|
179 |
================== |
|
180 |
|
|
181 |
Some Makefile variables are reserved by the GNU Coding Standards for |
|
182 |
the use of the "user" - the person building the package. For instance, |
|
183 |
CXX and CXXFLAGS are such variables, and have the same meaning as |
|
184 |
explained in the previous section. These variables can be set on the |
|
185 |
command line when invoking `make' like this: |
|
186 |
`make [VARIABLE=VALUE]...' |
|
187 |
|
|
188 |
WARNINGCXXFLAGS is a non-standard Makefile variable introduced by us |
|
189 |
to hold several compiler flags related to warnings. Its default value |
|
190 |
can be overridden when invoking `make'. For example to disable all |
|
191 |
warning flags use `make WARNINGCXXFLAGS='. |
|
192 |
|
|
193 |
In order to turn off a single flag from the default set of warning |
|
194 |
flags, you can use the CXXFLAGS variable, since this is passed after |
|
195 |
WARNINGCXXFLAGS. For example to turn off `-Wold-style-cast' (which is |
|
196 |
used by default when g++ is detected) you can use |
|
197 |
`make CXXFLAGS="-g -O2 -Wno-old-style-cast"'. |
... | ... |
@@ -35,24 +35,25 @@ |
35 | 35 |
noinst_HEADERS = |
36 | 36 |
noinst_PROGRAMS = |
37 | 37 |
bin_PROGRAMS = |
38 | 38 |
check_PROGRAMS = |
39 | 39 |
dist_bin_SCRIPTS = |
40 | 40 |
TESTS = |
41 | 41 |
XFAIL_TESTS = |
42 | 42 |
|
43 | 43 |
include lemon/Makefile.am |
44 | 44 |
include test/Makefile.am |
45 | 45 |
include doc/Makefile.am |
46 | 46 |
include tools/Makefile.am |
47 |
include scripts/Makefile.am |
|
47 | 48 |
|
48 | 49 |
DIST_SUBDIRS = demo |
49 | 50 |
|
50 | 51 |
demo: |
51 | 52 |
$(MAKE) $(AM_MAKEFLAGS) -C demo |
52 | 53 |
|
53 | 54 |
MRPROPERFILES = \ |
54 | 55 |
aclocal.m4 \ |
55 | 56 |
config.h.in \ |
56 | 57 |
config.h.in~ \ |
57 | 58 |
configure \ |
58 | 59 |
Makefile.in \ |
... | ... |
@@ -8,35 +8,43 @@ |
8 | 8 |
focus is on graphs and graph algorithms, thus it is especially |
9 | 9 |
suitable for solving design and optimization problems of |
10 | 10 |
telecommunication networks. To achieve wide usability its data |
11 | 11 |
structures and algorithms provide generic interfaces. |
12 | 12 |
|
13 | 13 |
Contents |
14 | 14 |
======== |
15 | 15 |
|
16 | 16 |
LICENSE |
17 | 17 |
|
18 | 18 |
Copying, distribution and modification conditions and terms. |
19 | 19 |
|
20 |
NEWS |
|
21 |
|
|
22 |
News and version history. |
|
23 |
|
|
20 | 24 |
INSTALL |
21 | 25 |
|
22 | 26 |
General building and installation instructions. |
23 | 27 |
|
24 | 28 |
lemon/ |
25 | 29 |
|
26 | 30 |
Source code of LEMON library. |
27 | 31 |
|
28 | 32 |
doc/ |
29 | 33 |
|
30 | 34 |
Documentation of LEMON. The starting page is doc/html/index.html. |
31 | 35 |
|
32 | 36 |
demo/ |
33 | 37 |
|
34 | 38 |
Some example programs to make you easier to get familiar with LEMON. |
35 | 39 |
|
40 |
scripts/ |
|
41 |
|
|
42 |
Scripts that make it easier to develop LEMON. |
|
43 |
|
|
36 | 44 |
test/ |
37 | 45 |
|
38 | 46 |
Programs to check the integrity and correctness of LEMON. |
39 | 47 |
|
40 | 48 |
tools/ |
41 | 49 |
|
42 | 50 |
Various utilities related to LEMON. |
... | ... |
@@ -32,24 +32,25 @@ |
32 | 32 |
if test x"$long_long_found" = x"yes"; then |
33 | 33 |
AC_DEFINE([LEMON_HAVE_LONG_LONG], [1], [Define to 1 if you have long long.]) |
34 | 34 |
fi |
35 | 35 |
|
36 | 36 |
dnl Checks for programs. |
37 | 37 |
AC_PROG_CXX |
38 | 38 |
AC_PROG_CXXCPP |
39 | 39 |
AC_PROG_INSTALL |
40 | 40 |
AC_DISABLE_SHARED |
41 | 41 |
AC_PROG_LIBTOOL |
42 | 42 |
|
43 | 43 |
AC_CHECK_PROG([doxygen_found],[doxygen],[yes],[no]) |
44 |
AC_CHECK_PROG([python_found],[python],[yes],[no]) |
|
44 | 45 |
AC_CHECK_PROG([gs_found],[gs],[yes],[no]) |
45 | 46 |
|
46 | 47 |
dnl Detect Intel compiler. |
47 | 48 |
AC_MSG_CHECKING([whether we are using the Intel C++ compiler]) |
48 | 49 |
AC_COMPILE_IFELSE([#ifndef __INTEL_COMPILER |
49 | 50 |
choke me |
50 | 51 |
#endif], [ICC=[yes]], [ICC=[no]]) |
51 | 52 |
if test x"$ICC" = x"yes"; then |
52 | 53 |
AC_MSG_RESULT([yes]) |
53 | 54 |
else |
54 | 55 |
AC_MSG_RESULT([no]) |
55 | 56 |
fi |
... | ... |
@@ -73,24 +74,39 @@ |
73 | 74 |
AC_ARG_ENABLE([tools], |
74 | 75 |
AS_HELP_STRING([--enable-tools], [build additional tools @<:@default@:>@]) |
75 | 76 |
AS_HELP_STRING([--disable-tools], [do not build additional tools]), |
76 | 77 |
[], [enable_tools=yes]) |
77 | 78 |
AC_MSG_CHECKING([whether to build the additional tools]) |
78 | 79 |
if test x"$enable_tools" != x"no"; then |
79 | 80 |
AC_MSG_RESULT([yes]) |
80 | 81 |
else |
81 | 82 |
AC_MSG_RESULT([no]) |
82 | 83 |
fi |
83 | 84 |
AM_CONDITIONAL([WANT_TOOLS], [test x"$enable_tools" != x"no"]) |
84 | 85 |
|
86 |
dnl Support for running test cases using valgrind. |
|
87 |
use_valgrind=no |
|
88 |
AC_ARG_ENABLE([valgrind], |
|
89 |
AS_HELP_STRING([--enable-valgrind], [use valgrind when running tests]), |
|
90 |
[use_valgrind=yes]) |
|
91 |
|
|
92 |
if [[ "$use_valgrind" = "yes" ]]; then |
|
93 |
AC_CHECK_PROG(HAVE_VALGRIND, valgrind, yes, no) |
|
94 |
|
|
95 |
if [[ "$HAVE_VALGRIND" = "no" ]]; then |
|
96 |
AC_MSG_ERROR([Valgrind not found in PATH.]) |
|
97 |
fi |
|
98 |
fi |
|
99 |
AM_CONDITIONAL(USE_VALGRIND, [test "$use_valgrind" = "yes"]) |
|
100 |
|
|
85 | 101 |
dnl Checks for header files. |
86 | 102 |
AC_CHECK_HEADERS(limits.h sys/time.h sys/times.h unistd.h) |
87 | 103 |
|
88 | 104 |
dnl Checks for typedefs, structures, and compiler characteristics. |
89 | 105 |
AC_C_CONST |
90 | 106 |
AC_C_INLINE |
91 | 107 |
AC_TYPE_SIZE_T |
92 | 108 |
AC_HEADER_TIME |
93 | 109 |
AC_STRUCT_TM |
94 | 110 |
|
95 | 111 |
dnl Checks for library functions. |
96 | 112 |
AC_HEADER_STDC |
... | ... |
@@ -119,22 +135,23 @@ |
119 | 135 |
echo C++ compiler.................. : $CXX |
120 | 136 |
echo C++ compiles flags............ : $WARNINGCXXFLAGS $CXXFLAGS |
121 | 137 |
echo |
122 | 138 |
echo Compiler supports long long... : $long_long_found |
123 | 139 |
echo |
124 | 140 |
echo GLPK support.................. : $lx_glpk_found |
125 | 141 |
echo CPLEX support................. : $lx_cplex_found |
126 | 142 |
echo SOPLEX support................ : $lx_soplex_found |
127 | 143 |
echo CLP support................... : $lx_clp_found |
128 | 144 |
echo CBC support................... : $lx_cbc_found |
129 | 145 |
echo |
130 | 146 |
echo Build additional tools........ : $enable_tools |
147 |
echo Use valgrind for tests........ : $use_valgrind |
|
131 | 148 |
echo |
132 | 149 |
echo The packace will be installed in |
133 | 150 |
echo -n ' ' |
134 | 151 |
echo $prefix. |
135 | 152 |
echo |
136 | 153 |
echo '*********************************************************************' |
137 | 154 |
|
138 | 155 |
echo |
139 | 156 |
echo Configure complete, now type \'make\' and then \'make install\'. |
140 | 157 |
echo |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
... | ... |
@@ -56,27 +56,36 @@ |
56 | 56 |
// Bundle -gr* options into a group |
57 | 57 |
ap.optionGroup("gr", "gra") |
58 | 58 |
.optionGroup("gr", "grb") |
59 | 59 |
.optionGroup("gr", "grc"); |
60 | 60 |
// Set the group mandatory |
61 | 61 |
ap.mandatoryGroup("gr"); |
62 | 62 |
// Set the options of the group exclusive (only one option can be given) |
63 | 63 |
ap.onlyOneGroup("gr"); |
64 | 64 |
// Add non-parsed arguments (e.g. input files) |
65 | 65 |
ap.other("infile", "The input file.") |
66 | 66 |
.other("..."); |
67 | 67 |
|
68 |
// Throw an exception when problems occurs. The default behavior is to |
|
69 |
// exit(1) on these cases, but this makes Valgrind falsely warn |
|
70 |
// about memory leaks. |
|
71 |
ap.throwOnProblems(); |
|
72 |
|
|
68 | 73 |
// Perform the parsing process |
69 | 74 |
// (in case of any error it terminates the program) |
75 |
// The try {} construct is necessary only if the ap.trowOnProblems() |
|
76 |
// setting is in use. |
|
77 |
try { |
|
70 | 78 |
ap.parse(); |
79 |
} catch (ArgParserException &) { return 1; } |
|
71 | 80 |
|
72 | 81 |
// Check each option if it has been given and print its value |
73 | 82 |
std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
74 | 83 |
|
75 | 84 |
std::cout << " Value of -n: " << i << std::endl; |
76 | 85 |
if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; |
77 | 86 |
if(ap.given("val2")) { |
78 | 87 |
d = ap["val2"]; |
79 | 88 |
std::cout << " Value of -val2: " << d << std::endl; |
80 | 89 |
} |
81 | 90 |
if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; |
82 | 91 |
if(ap.given("f")) std::cout << " -f is given\n"; |
... | ... |
@@ -8,43 +8,46 @@ |
8 | 8 |
CONFIGURE_FILE( |
9 | 9 |
${PROJECT_SOURCE_DIR}/doc/Doxyfile.in |
10 | 10 |
${PROJECT_BINARY_DIR}/doc/Doxyfile |
11 | 11 |
@ONLY |
12 | 12 |
) |
13 | 13 |
|
14 | 14 |
CONFIGURE_FILE( |
15 | 15 |
${PROJECT_SOURCE_DIR}/doc/mainpage.dox.in |
16 | 16 |
${PROJECT_BINARY_DIR}/doc/mainpage.dox |
17 | 17 |
@ONLY |
18 | 18 |
) |
19 | 19 |
|
20 |
IF(DOXYGEN_EXECUTABLE AND GHOSTSCRIPT_EXECUTABLE) |
|
20 |
IF(DOXYGEN_EXECUTABLE AND PYTHONINTERP_FOUND AND GHOSTSCRIPT_EXECUTABLE) |
|
21 | 21 |
FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/) |
22 | 22 |
SET(GHOSTSCRIPT_OPTIONS -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha) |
23 | 23 |
ADD_CUSTOM_TARGET(html |
24 | 24 |
COMMAND ${CMAKE_COMMAND} -E remove_directory gen-images |
25 | 25 |
COMMAND ${CMAKE_COMMAND} -E make_directory gen-images |
26 | 26 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/bipartite_matching.png ${CMAKE_CURRENT_SOURCE_DIR}/images/bipartite_matching.eps |
27 | 27 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/bipartite_partitions.png ${CMAKE_CURRENT_SOURCE_DIR}/images/bipartite_partitions.eps |
28 | 28 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/connected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/connected_components.eps |
29 | 29 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/edge_biconnected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/edge_biconnected_components.eps |
30 | 30 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/grid_graph.png ${CMAKE_CURRENT_SOURCE_DIR}/images/grid_graph.eps |
31 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/matching.png ${CMAKE_CURRENT_SOURCE_DIR}/images/matching.eps |
|
31 | 32 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/node_biconnected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/node_biconnected_components.eps |
32 | 33 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_0.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_0.eps |
33 | 34 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_1.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_1.eps |
34 | 35 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_2.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_2.eps |
35 | 36 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_3.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_3.eps |
36 | 37 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_4.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_4.eps |
38 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/planar.png ${CMAKE_CURRENT_SOURCE_DIR}/images/planar.eps |
|
37 | 39 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/strongly_connected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/strongly_connected_components.eps |
38 | 40 |
COMMAND ${CMAKE_COMMAND} -E remove_directory html |
41 |
COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/scripts/bib2dox.py ${CMAKE_CURRENT_SOURCE_DIR}/references.bib >references.dox |
|
39 | 42 |
COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile |
40 | 43 |
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
41 | 44 |
) |
42 | 45 |
|
43 | 46 |
SET_TARGET_PROPERTIES(html PROPERTIES PROJECT_LABEL BUILD_DOC) |
44 | 47 |
|
45 | 48 |
IF(UNIX) |
46 | 49 |
INSTALL( |
47 | 50 |
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/ |
48 | 51 |
DESTINATION share/doc/lemon/html |
49 | 52 |
COMPONENT html_documentation |
50 | 53 |
) |
... | ... |
@@ -88,25 +88,26 @@ |
88 | 88 |
WARN_FORMAT = "$file:$line: $text" |
89 | 89 |
WARN_LOGFILE = doxygen.log |
90 | 90 |
#--------------------------------------------------------------------------- |
91 | 91 |
# configuration options related to the input files |
92 | 92 |
#--------------------------------------------------------------------------- |
93 | 93 |
INPUT = "@abs_top_srcdir@/doc" \ |
94 | 94 |
"@abs_top_srcdir@/lemon" \ |
95 | 95 |
"@abs_top_srcdir@/lemon/bits" \ |
96 | 96 |
"@abs_top_srcdir@/lemon/concepts" \ |
97 | 97 |
"@abs_top_srcdir@/demo" \ |
98 | 98 |
"@abs_top_srcdir@/tools" \ |
99 | 99 |
"@abs_top_srcdir@/test/test_tools.h" \ |
100 |
"@abs_top_builddir@/doc/mainpage.dox" |
|
100 |
"@abs_top_builddir@/doc/mainpage.dox" \ |
|
101 |
"@abs_top_builddir@/doc/references.dox" |
|
101 | 102 |
INPUT_ENCODING = UTF-8 |
102 | 103 |
FILE_PATTERNS = *.h \ |
103 | 104 |
*.cc \ |
104 | 105 |
*.dox |
105 | 106 |
RECURSIVE = NO |
106 | 107 |
EXCLUDE = |
107 | 108 |
EXCLUDE_SYMLINKS = NO |
108 | 109 |
EXCLUDE_PATTERNS = |
109 | 110 |
EXCLUDE_SYMBOLS = |
110 | 111 |
EXAMPLE_PATH = "@abs_top_srcdir@/demo" \ |
111 | 112 |
"@abs_top_srcdir@/LICENSE" \ |
112 | 113 |
"@abs_top_srcdir@/doc" |
... | ... |
@@ -18,25 +18,27 @@ |
18 | 18 |
grid_graph.eps \ |
19 | 19 |
nodeshape_0.eps \ |
20 | 20 |
nodeshape_1.eps \ |
21 | 21 |
nodeshape_2.eps \ |
22 | 22 |
nodeshape_3.eps \ |
23 | 23 |
nodeshape_4.eps |
24 | 24 |
|
25 | 25 |
DOC_EPS_IMAGES27 = \ |
26 | 26 |
bipartite_matching.eps \ |
27 | 27 |
bipartite_partitions.eps \ |
28 | 28 |
connected_components.eps \ |
29 | 29 |
edge_biconnected_components.eps \ |
30 |
matching.eps \ |
|
30 | 31 |
node_biconnected_components.eps \ |
32 |
planar.eps \ |
|
31 | 33 |
strongly_connected_components.eps |
32 | 34 |
|
33 | 35 |
DOC_EPS_IMAGES = \ |
34 | 36 |
$(DOC_EPS_IMAGES18) \ |
35 | 37 |
$(DOC_EPS_IMAGES27) |
36 | 38 |
|
37 | 39 |
DOC_PNG_IMAGES = \ |
38 | 40 |
$(DOC_EPS_IMAGES:%.eps=doc/gen-images/%.png) |
39 | 41 |
|
40 | 42 |
EXTRA_DIST += $(DOC_EPS_IMAGES:%=doc/images/%) |
41 | 43 |
|
42 | 44 |
doc/html: |
... | ... |
@@ -57,25 +59,37 @@ |
57 | 59 |
|
58 | 60 |
$(DOC_EPS_IMAGES27:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
59 | 61 |
-mkdir doc/gen-images |
60 | 62 |
if test ${gs_found} = yes; then \ |
61 | 63 |
$(GS_COMMAND) -sDEVICE=pngalpha -r27 -sOutputFile=$@ $<; \ |
62 | 64 |
else \ |
63 | 65 |
echo; \ |
64 | 66 |
echo "Ghostscript not found."; \ |
65 | 67 |
echo; \ |
66 | 68 |
exit 1; \ |
67 | 69 |
fi |
68 | 70 |
|
69 |
|
|
71 |
references.dox: doc/references.bib |
|
72 |
if test ${python_found} = yes; then \ |
|
73 |
cd doc; \ |
|
74 |
python @abs_top_srcdir@/scripts/bib2dox.py @abs_top_builddir@/$< >$@; \ |
|
75 |
cd ..; \ |
|
76 |
else \ |
|
77 |
echo; \ |
|
78 |
echo "Python not found."; \ |
|
79 |
echo; \ |
|
80 |
exit 1; \ |
|
81 |
fi |
|
82 |
|
|
83 |
html-local: $(DOC_PNG_IMAGES) references.dox |
|
70 | 84 |
if test ${doxygen_found} = yes; then \ |
71 | 85 |
cd doc; \ |
72 | 86 |
doxygen Doxyfile; \ |
73 | 87 |
cd ..; \ |
74 | 88 |
else \ |
75 | 89 |
echo; \ |
76 | 90 |
echo "Doxygen not found."; \ |
77 | 91 |
echo; \ |
78 | 92 |
exit 1; \ |
79 | 93 |
fi |
80 | 94 |
|
81 | 95 |
clean-local: |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
... | ... |
@@ -217,210 +217,283 @@ |
217 | 217 |
|
218 | 218 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
219 | 219 |
dijkstra.run(source, target); |
220 | 220 |
\endcode |
221 | 221 |
We have a length map and a maximum speed map on the arcs of a digraph. |
222 | 222 |
The minimum time to pass the arc can be calculated as the division of |
223 | 223 |
the two maps which can be done implicitly with the \c DivMap template |
224 | 224 |
class. We use the implicit minimum time map as the length map of the |
225 | 225 |
\c Dijkstra algorithm. |
226 | 226 |
*/ |
227 | 227 |
|
228 | 228 |
/** |
229 |
@defgroup matrices Matrices |
|
230 |
@ingroup datas |
|
231 |
\brief Two dimensional data storages implemented in LEMON. |
|
232 |
|
|
233 |
This group contains two dimensional data storages implemented in LEMON. |
|
234 |
*/ |
|
235 |
|
|
236 |
/** |
|
237 | 229 |
@defgroup paths Path Structures |
238 | 230 |
@ingroup datas |
239 | 231 |
\brief %Path structures implemented in LEMON. |
240 | 232 |
|
241 | 233 |
This group contains the path structures implemented in LEMON. |
242 | 234 |
|
243 | 235 |
LEMON provides flexible data structures to work with paths. |
244 | 236 |
All of them have similar interfaces and they can be copied easily with |
245 | 237 |
assignment operators and copy constructors. This makes it easy and |
246 | 238 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
247 | 239 |
any kind of path structure. |
248 | 240 |
|
249 |
\sa |
|
241 |
\sa \ref concepts::Path "Path concept" |
|
242 |
*/ |
|
243 |
|
|
244 |
/** |
|
245 |
@defgroup heaps Heap Structures |
|
246 |
@ingroup datas |
|
247 |
\brief %Heap structures implemented in LEMON. |
|
248 |
|
|
249 |
This group contains the heap structures implemented in LEMON. |
|
250 |
|
|
251 |
LEMON provides several heap classes. They are efficient implementations |
|
252 |
of the abstract data type \e priority \e queue. They store items with |
|
253 |
specified values called \e priorities in such a way that finding and |
|
254 |
removing the item with minimum priority are efficient. |
|
255 |
The basic operations are adding and erasing items, changing the priority |
|
256 |
of an item, etc. |
|
257 |
|
|
258 |
Heaps are crucial in several algorithms, such as Dijkstra and Prim. |
|
259 |
The heap implementations have the same interface, thus any of them can be |
|
260 |
used easily in such algorithms. |
|
261 |
|
|
262 |
\sa \ref concepts::Heap "Heap concept" |
|
263 |
*/ |
|
264 |
|
|
265 |
/** |
|
266 |
@defgroup matrices Matrices |
|
267 |
@ingroup datas |
|
268 |
\brief Two dimensional data storages implemented in LEMON. |
|
269 |
|
|
270 |
This group contains two dimensional data storages implemented in LEMON. |
|
250 | 271 |
*/ |
251 | 272 |
|
252 | 273 |
/** |
253 | 274 |
@defgroup auxdat Auxiliary Data Structures |
254 | 275 |
@ingroup datas |
255 | 276 |
\brief Auxiliary data structures implemented in LEMON. |
256 | 277 |
|
257 | 278 |
This group contains some data structures implemented in LEMON in |
258 | 279 |
order to make it easier to implement combinatorial algorithms. |
259 | 280 |
*/ |
260 | 281 |
|
261 | 282 |
/** |
283 |
@defgroup geomdat Geometric Data Structures |
|
284 |
@ingroup auxdat |
|
285 |
\brief Geometric data structures implemented in LEMON. |
|
286 |
|
|
287 |
This group contains geometric data structures implemented in LEMON. |
|
288 |
|
|
289 |
- \ref lemon::dim2::Point "dim2::Point" implements a two dimensional |
|
290 |
vector with the usual operations. |
|
291 |
- \ref lemon::dim2::Box "dim2::Box" can be used to determine the |
|
292 |
rectangular bounding box of a set of \ref lemon::dim2::Point |
|
293 |
"dim2::Point"'s. |
|
294 |
*/ |
|
295 |
|
|
296 |
/** |
|
297 |
@defgroup matrices Matrices |
|
298 |
@ingroup auxdat |
|
299 |
\brief Two dimensional data storages implemented in LEMON. |
|
300 |
|
|
301 |
This group contains two dimensional data storages implemented in LEMON. |
|
302 |
*/ |
|
303 |
|
|
304 |
/** |
|
262 | 305 |
@defgroup algs Algorithms |
263 | 306 |
\brief This group contains the several algorithms |
264 | 307 |
implemented in LEMON. |
265 | 308 |
|
266 | 309 |
This group contains the several algorithms |
267 | 310 |
implemented in LEMON. |
268 | 311 |
*/ |
269 | 312 |
|
270 | 313 |
/** |
271 | 314 |
@defgroup search Graph Search |
272 | 315 |
@ingroup algs |
273 | 316 |
\brief Common graph search algorithms. |
274 | 317 |
|
275 | 318 |
This group contains the common graph search algorithms, namely |
276 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS) |
|
319 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS) |
|
320 |
\ref clrs01algorithms. |
|
277 | 321 |
*/ |
278 | 322 |
|
279 | 323 |
/** |
280 | 324 |
@defgroup shortest_path Shortest Path Algorithms |
281 | 325 |
@ingroup algs |
282 | 326 |
\brief Algorithms for finding shortest paths. |
283 | 327 |
|
284 |
This group contains the algorithms for finding shortest paths in digraphs |
|
328 |
This group contains the algorithms for finding shortest paths in digraphs |
|
329 |
\ref clrs01algorithms. |
|
285 | 330 |
|
286 | 331 |
- \ref Dijkstra algorithm for finding shortest paths from a source node |
287 | 332 |
when all arc lengths are non-negative. |
288 | 333 |
- \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths |
289 | 334 |
from a source node when arc lenghts can be either positive or negative, |
290 | 335 |
but the digraph should not contain directed cycles with negative total |
291 | 336 |
length. |
292 | 337 |
- \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms |
293 | 338 |
for solving the \e all-pairs \e shortest \e paths \e problem when arc |
294 | 339 |
lenghts can be either positive or negative, but the digraph should |
295 | 340 |
not contain directed cycles with negative total length. |
296 | 341 |
- \ref Suurballe A successive shortest path algorithm for finding |
297 | 342 |
arc-disjoint paths between two nodes having minimum total length. |
298 | 343 |
*/ |
299 | 344 |
|
300 | 345 |
/** |
346 |
@defgroup spantree Minimum Spanning Tree Algorithms |
|
347 |
@ingroup algs |
|
348 |
\brief Algorithms for finding minimum cost spanning trees and arborescences. |
|
349 |
|
|
350 |
This group contains the algorithms for finding minimum cost spanning |
|
351 |
trees and arborescences \ref clrs01algorithms. |
|
352 |
*/ |
|
353 |
|
|
354 |
/** |
|
301 | 355 |
@defgroup max_flow Maximum Flow Algorithms |
302 | 356 |
@ingroup algs |
303 | 357 |
\brief Algorithms for finding maximum flows. |
304 | 358 |
|
305 | 359 |
This group contains the algorithms for finding maximum flows and |
306 |
feasible circulations. |
|
360 |
feasible circulations \ref clrs01algorithms, \ref amo93networkflows. |
|
307 | 361 |
|
308 | 362 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
309 | 363 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
310 | 364 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and |
311 | 365 |
\f$s, t \in V\f$ source and target nodes. |
312 | 366 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the |
313 | 367 |
following optimization problem. |
314 | 368 |
|
315 | 369 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f] |
316 | 370 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu) |
317 | 371 |
\quad \forall u\in V\setminus\{s,t\} \f] |
318 | 372 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
319 | 373 |
|
320 | 374 |
LEMON contains several algorithms for solving maximum flow problems: |
321 |
- \ref EdmondsKarp Edmonds-Karp algorithm. |
|
322 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm. |
|
323 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees. |
|
324 |
- \ref GoldbergTarjan Preflow push-relabel algorithm with dynamic trees. |
|
375 |
- \ref EdmondsKarp Edmonds-Karp algorithm |
|
376 |
\ref edmondskarp72theoretical. |
|
377 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm |
|
378 |
\ref goldberg88newapproach. |
|
379 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees |
|
380 |
\ref dinic70algorithm, \ref sleator83dynamic. |
|
381 |
- \ref GoldbergTarjan !Preflow push-relabel algorithm with dynamic trees |
|
382 |
\ref goldberg88newapproach, \ref sleator83dynamic. |
|
325 | 383 |
|
326 |
In most cases the \ref Preflow |
|
384 |
In most cases the \ref Preflow algorithm provides the |
|
327 | 385 |
fastest method for computing a maximum flow. All implementations |
328 | 386 |
also provide functions to query the minimum cut, which is the dual |
329 | 387 |
problem of maximum flow. |
330 | 388 |
|
331 | 389 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
332 | 390 |
for finding feasible circulations, which is a somewhat different problem, |
333 | 391 |
but it is strongly related to maximum flow. |
334 | 392 |
For more information, see \ref Circulation. |
335 | 393 |
*/ |
336 | 394 |
|
337 | 395 |
/** |
338 | 396 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
339 | 397 |
@ingroup algs |
340 | 398 |
|
341 | 399 |
\brief Algorithms for finding minimum cost flows and circulations. |
342 | 400 |
|
343 | 401 |
This group contains the algorithms for finding minimum cost flows and |
344 |
circulations. For more information about this problem and its dual |
|
345 |
solution see \ref min_cost_flow "Minimum Cost Flow Problem". |
|
402 |
circulations \ref amo93networkflows. For more information about this |
|
403 |
problem and its dual solution, see \ref min_cost_flow |
|
404 |
"Minimum Cost Flow Problem". |
|
346 | 405 |
|
347 | 406 |
LEMON contains several algorithms for this problem. |
348 | 407 |
- \ref NetworkSimplex Primal Network Simplex algorithm with various |
349 |
pivot strategies. |
|
350 |
- \ref CostScaling Push-Relabel and Augment-Relabel algorithms based on |
|
351 |
cost scaling. |
|
352 |
- \ref CapacityScaling Successive Shortest %Path algorithm with optional |
|
353 |
capacity scaling. |
|
354 |
- \ref CancelAndTighten The Cancel and Tighten algorithm. |
|
355 |
|
|
408 |
pivot strategies \ref dantzig63linearprog, \ref kellyoneill91netsimplex. |
|
409 |
- \ref CostScaling Cost Scaling algorithm based on push/augment and |
|
410 |
relabel operations \ref goldberg90approximation, \ref goldberg97efficient, |
|
411 |
\ref bunnagel98efficient. |
|
412 |
- \ref CapacityScaling Capacity Scaling algorithm based on the successive |
|
413 |
shortest path method \ref edmondskarp72theoretical. |
|
414 |
- \ref CycleCanceling Cycle-Canceling algorithms, two of which are |
|
415 |
strongly polynomial \ref klein67primal, \ref goldberg89cyclecanceling. |
|
356 | 416 |
|
357 | 417 |
In general NetworkSimplex is the most efficient implementation, |
358 | 418 |
but in special cases other algorithms could be faster. |
359 | 419 |
For example, if the total supply and/or capacities are rather small, |
360 | 420 |
CapacityScaling is usually the fastest algorithm (without effective scaling). |
361 | 421 |
*/ |
362 | 422 |
|
363 | 423 |
/** |
364 | 424 |
@defgroup min_cut Minimum Cut Algorithms |
365 | 425 |
@ingroup algs |
366 | 426 |
|
367 | 427 |
\brief Algorithms for finding minimum cut in graphs. |
368 | 428 |
|
369 | 429 |
This group contains the algorithms for finding minimum cut in graphs. |
370 | 430 |
|
371 | 431 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
372 | 432 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
373 | 433 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
374 | 434 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum |
375 | 435 |
cut is the \f$X\f$ solution of the next optimization problem: |
376 | 436 |
|
377 | 437 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}} |
378 |
\sum_{uv\in A |
|
438 |
\sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f] |
|
379 | 439 |
|
380 | 440 |
LEMON contains several algorithms related to minimum cut problems: |
381 | 441 |
|
382 | 442 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
383 | 443 |
in directed graphs. |
384 | 444 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
385 | 445 |
calculating minimum cut in undirected graphs. |
386 | 446 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
387 | 447 |
all-pairs minimum cut in undirected graphs. |
388 | 448 |
|
389 | 449 |
If you want to find minimum cut just between two distinict nodes, |
390 | 450 |
see the \ref max_flow "maximum flow problem". |
391 | 451 |
*/ |
392 | 452 |
|
393 | 453 |
/** |
394 |
@defgroup |
|
454 |
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms |
|
395 | 455 |
@ingroup algs |
396 |
\brief Algorithms for |
|
456 |
\brief Algorithms for finding minimum mean cycles. |
|
397 | 457 |
|
398 |
This group contains the algorithms for discovering the graph properties |
|
399 |
like connectivity, bipartiteness, euler property, simplicity etc. |
|
458 |
This group contains the algorithms for finding minimum mean cycles |
|
459 |
\ref clrs01algorithms, \ref amo93networkflows. |
|
400 | 460 |
|
401 |
\image html edge_biconnected_components.png |
|
402 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
|
403 |
|
|
461 |
The \e minimum \e mean \e cycle \e problem is to find a directed cycle |
|
462 |
of minimum mean length (cost) in a digraph. |
|
463 |
The mean length of a cycle is the average length of its arcs, i.e. the |
|
464 |
ratio between the total length of the cycle and the number of arcs on it. |
|
404 | 465 |
|
405 |
/** |
|
406 |
@defgroup planar Planarity Embedding and Drawing |
|
407 |
@ingroup algs |
|
408 |
\brief Algorithms for planarity checking, embedding and drawing |
|
466 |
This problem has an important connection to \e conservative \e length |
|
467 |
\e functions, too. A length function on the arcs of a digraph is called |
|
468 |
conservative if and only if there is no directed cycle of negative total |
|
469 |
length. For an arbitrary length function, the negative of the minimum |
|
470 |
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the |
|
471 |
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length |
|
472 |
function. |
|
409 | 473 |
|
410 |
This group contains the algorithms for planarity checking, |
|
411 |
embedding and drawing. |
|
474 |
LEMON contains three algorithms for solving the minimum mean cycle problem: |
|
475 |
- \ref Karp "Karp"'s original algorithm \ref amo93networkflows, |
|
476 |
\ref dasdan98minmeancycle. |
|
477 |
- \ref HartmannOrlin "Hartmann-Orlin"'s algorithm, which is an improved |
|
478 |
version of Karp's algorithm \ref dasdan98minmeancycle. |
|
479 |
- \ref Howard "Howard"'s policy iteration algorithm |
|
480 |
\ref dasdan98minmeancycle. |
|
412 | 481 |
|
413 |
\image html planar.png |
|
414 |
\image latex planar.eps "Plane graph" width=\textwidth |
|
482 |
In practice, the Howard algorithm proved to be by far the most efficient |
|
483 |
one, though the best known theoretical bound on its running time is |
|
484 |
exponential. |
|
485 |
Both Karp and HartmannOrlin algorithms run in time O(ne) and use space |
|
486 |
O(n<sup>2</sup>+e), but the latter one is typically faster due to the |
|
487 |
applied early termination scheme. |
|
415 | 488 |
*/ |
416 | 489 |
|
417 | 490 |
/** |
418 | 491 |
@defgroup matching Matching Algorithms |
419 | 492 |
@ingroup algs |
420 | 493 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
421 | 494 |
|
422 | 495 |
This group contains the algorithms for calculating |
423 | 496 |
matchings in graphs and bipartite graphs. The general matching problem is |
424 | 497 |
finding a subset of the edges for which each node has at most one incident |
425 | 498 |
edge. |
426 | 499 |
|
... | ... |
@@ -440,73 +513,98 @@ |
440 | 513 |
Successive shortest path algorithm for calculating maximum weighted |
441 | 514 |
matching and maximum weighted bipartite matching in bipartite graphs. |
442 | 515 |
- \ref MinCostMaxBipartiteMatching |
443 | 516 |
Successive shortest path algorithm for calculating minimum cost maximum |
444 | 517 |
matching in bipartite graphs. |
445 | 518 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
446 | 519 |
maximum cardinality matching in general graphs. |
447 | 520 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
448 | 521 |
maximum weighted matching in general graphs. |
449 | 522 |
- \ref MaxWeightedPerfectMatching |
450 | 523 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
451 | 524 |
perfect matching in general graphs. |
525 |
- \ref MaxFractionalMatching Push-relabel algorithm for calculating |
|
526 |
maximum cardinality fractional matching in general graphs. |
|
527 |
- \ref MaxWeightedFractionalMatching Augmenting path algorithm for calculating |
|
528 |
maximum weighted fractional matching in general graphs. |
|
529 |
- \ref MaxWeightedPerfectFractionalMatching |
|
530 |
Augmenting path algorithm for calculating maximum weighted |
|
531 |
perfect fractional matching in general graphs. |
|
452 | 532 |
|
453 |
\image html bipartite_matching.png |
|
454 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
|
533 |
\image html matching.png |
|
534 |
\image latex matching.eps "Min Cost Perfect Matching" width=\textwidth |
|
455 | 535 |
*/ |
456 | 536 |
|
457 | 537 |
/** |
458 |
@defgroup |
|
538 |
@defgroup graph_properties Connectivity and Other Graph Properties |
|
459 | 539 |
@ingroup algs |
460 |
\brief Algorithms for |
|
540 |
\brief Algorithms for discovering the graph properties |
|
461 | 541 |
|
462 |
This group contains the algorithms for finding minimum cost spanning |
|
463 |
trees and arborescences. |
|
542 |
This group contains the algorithms for discovering the graph properties |
|
543 |
like connectivity, bipartiteness, euler property, simplicity etc. |
|
544 |
|
|
545 |
\image html connected_components.png |
|
546 |
\image latex connected_components.eps "Connected components" width=\textwidth |
|
547 |
*/ |
|
548 |
|
|
549 |
/** |
|
550 |
@defgroup planar Planarity Embedding and Drawing |
|
551 |
@ingroup algs |
|
552 |
\brief Algorithms for planarity checking, embedding and drawing |
|
553 |
|
|
554 |
This group contains the algorithms for planarity checking, |
|
555 |
embedding and drawing. |
|
556 |
|
|
557 |
\image html planar.png |
|
558 |
\image latex planar.eps "Plane graph" width=\textwidth |
|
559 |
*/ |
|
560 |
|
|
561 |
/** |
|
562 |
@defgroup approx Approximation Algorithms |
|
563 |
@ingroup algs |
|
564 |
\brief Approximation algorithms. |
|
565 |
|
|
566 |
This group contains the approximation and heuristic algorithms |
|
567 |
implemented in LEMON. |
|
464 | 568 |
*/ |
465 | 569 |
|
466 | 570 |
/** |
467 | 571 |
@defgroup auxalg Auxiliary Algorithms |
468 | 572 |
@ingroup algs |
469 | 573 |
\brief Auxiliary algorithms implemented in LEMON. |
470 | 574 |
|
471 | 575 |
This group contains some algorithms implemented in LEMON |
472 | 576 |
in order to make it easier to implement complex algorithms. |
473 | 577 |
*/ |
474 | 578 |
|
475 | 579 |
/** |
476 |
@defgroup approx Approximation Algorithms |
|
477 |
@ingroup algs |
|
478 |
\brief Approximation algorithms. |
|
479 |
|
|
480 |
This group contains the approximation and heuristic algorithms |
|
481 |
implemented in LEMON. |
|
482 |
*/ |
|
483 |
|
|
484 |
/** |
|
485 | 580 |
@defgroup gen_opt_group General Optimization Tools |
486 | 581 |
\brief This group contains some general optimization frameworks |
487 | 582 |
implemented in LEMON. |
488 | 583 |
|
489 | 584 |
This group contains some general optimization frameworks |
490 | 585 |
implemented in LEMON. |
491 | 586 |
*/ |
492 | 587 |
|
493 | 588 |
/** |
494 |
@defgroup lp_group |
|
589 |
@defgroup lp_group LP and MIP Solvers |
|
495 | 590 |
@ingroup gen_opt_group |
496 |
\brief |
|
591 |
\brief LP and MIP solver interfaces for LEMON. |
|
497 | 592 |
|
498 |
This group contains Lp and Mip solver interfaces for LEMON. The |
|
499 |
various LP solvers could be used in the same manner with this |
|
500 |
|
|
593 |
This group contains LP and MIP solver interfaces for LEMON. |
|
594 |
Various LP solvers could be used in the same manner with this |
|
595 |
high-level interface. |
|
596 |
|
|
597 |
The currently supported solvers are \ref glpk, \ref clp, \ref cbc, |
|
598 |
\ref cplex, \ref soplex. |
|
501 | 599 |
*/ |
502 | 600 |
|
503 | 601 |
/** |
504 | 602 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
505 | 603 |
@ingroup lp_group |
506 | 604 |
\brief Helper tools to the Lp and Mip solvers. |
507 | 605 |
|
508 | 606 |
This group adds some helper tools to general optimization framework |
509 | 607 |
implemented in LEMON. |
510 | 608 |
*/ |
511 | 609 |
|
512 | 610 |
/** |
... | ... |
@@ -578,25 +676,25 @@ |
578 | 676 |
*/ |
579 | 677 |
|
580 | 678 |
/** |
581 | 679 |
@defgroup eps_io Postscript Exporting |
582 | 680 |
@ingroup io_group |
583 | 681 |
\brief General \c EPS drawer and graph exporter |
584 | 682 |
|
585 | 683 |
This group contains general \c EPS drawing methods and special |
586 | 684 |
graph exporting tools. |
587 | 685 |
*/ |
588 | 686 |
|
589 | 687 |
/** |
590 |
@defgroup dimacs_group DIMACS |
|
688 |
@defgroup dimacs_group DIMACS Format |
|
591 | 689 |
@ingroup io_group |
592 | 690 |
\brief Read and write files in DIMACS format |
593 | 691 |
|
594 | 692 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
595 | 693 |
*/ |
596 | 694 |
|
597 | 695 |
/** |
598 | 696 |
@defgroup nauty_group NAUTY Format |
599 | 697 |
@ingroup io_group |
600 | 698 |
\brief Read \e Nauty format |
601 | 699 |
|
602 | 700 |
Tool to read graphs from \e Nauty format data. |
... | ... |
@@ -627,46 +725,46 @@ |
627 | 725 |
- The concept descriptor classes also provide a <em>checker class</em> |
628 | 726 |
that makes it possible to check whether a certain implementation of a |
629 | 727 |
concept indeed provides all the required features. |
630 | 728 |
|
631 | 729 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
632 | 730 |
*/ |
633 | 731 |
|
634 | 732 |
/** |
635 | 733 |
@defgroup graph_concepts Graph Structure Concepts |
636 | 734 |
@ingroup concept |
637 | 735 |
\brief Skeleton and concept checking classes for graph structures |
638 | 736 |
|
639 |
This group contains the skeletons and concept checking classes of LEMON's |
|
640 |
graph structures and helper classes used to implement these. |
|
737 |
This group contains the skeletons and concept checking classes of |
|
738 |
graph structures. |
|
641 | 739 |
*/ |
642 | 740 |
|
643 | 741 |
/** |
644 | 742 |
@defgroup map_concepts Map Concepts |
645 | 743 |
@ingroup concept |
646 | 744 |
\brief Skeleton and concept checking classes for maps |
647 | 745 |
|
648 | 746 |
This group contains the skeletons and concept checking classes of maps. |
649 | 747 |
*/ |
650 | 748 |
|
651 | 749 |
/** |
750 |
@defgroup tools Standalone Utility Applications |
|
751 |
|
|
752 |
Some utility applications are listed here. |
|
753 |
|
|
754 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
|
755 |
them, as well. |
|
756 |
*/ |
|
757 |
|
|
758 |
/** |
|
652 | 759 |
\anchor demoprograms |
653 | 760 |
|
654 | 761 |
@defgroup demos Demo Programs |
655 | 762 |
|
656 | 763 |
Some demo programs are listed here. Their full source codes can be found in |
657 | 764 |
the \c demo subdirectory of the source tree. |
658 | 765 |
|
659 | 766 |
In order to compile them, use the <tt>make demo</tt> or the |
660 | 767 |
<tt>make check</tt> commands. |
661 | 768 |
*/ |
662 | 769 |
|
663 |
/** |
|
664 |
@defgroup tools Standalone Utility Applications |
|
665 |
|
|
666 |
Some utility applications are listed here. |
|
667 |
|
|
668 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
|
669 |
them, as well. |
|
670 |
*/ |
|
671 |
|
|
672 | 770 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
/** |
20 | 20 |
\mainpage @PACKAGE_NAME@ @PACKAGE_VERSION@ Documentation |
21 | 21 |
|
22 | 22 |
\section intro Introduction |
23 | 23 |
|
24 |
\subsection whatis What is LEMON |
|
25 |
|
|
26 |
LEMON stands for <b>L</b>ibrary for <b>E</b>fficient <b>M</b>odeling |
|
27 |
and <b>O</b>ptimization in <b>N</b>etworks. |
|
28 |
It is a C++ template |
|
29 |
library aimed at combinatorial optimization tasks which |
|
30 |
often involve in working |
|
31 |
with graphs. |
|
24 |
<b>LEMON</b> stands for <i><b>L</b>ibrary for <b>E</b>fficient <b>M</b>odeling |
|
25 |
and <b>O</b>ptimization in <b>N</b>etworks</i>. |
|
26 |
It is a C++ template library providing efficient implementations of common |
|
27 |
data structures and algorithms with focus on combinatorial optimization |
|
28 |
tasks connected mainly with graphs and networks. |
|
32 | 29 |
|
33 | 30 |
<b> |
34 | 31 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
35 | 32 |
project. |
36 | 33 |
You are free to use it in your commercial or |
37 | 34 |
non-commercial applications under very permissive |
38 | 35 |
\ref license "license terms". |
39 | 36 |
</b> |
40 | 37 |
|
41 |
|
|
38 |
The project is maintained by the |
|
39 |
<a href="http://www.cs.elte.hu/egres/">Egerváry Research Group on |
|
40 |
Combinatorial Optimization</a> \ref egres |
|
41 |
at the Operations Research Department of the |
|
42 |
<a href="http://www.elte.hu/en/">Eötvös Loránd University</a>, |
|
43 |
Budapest, Hungary. |
|
44 |
LEMON is also a member of the <a href="http://www.coin-or.org/">COIN-OR</a> |
|
45 |
initiative \ref coinor. |
|
46 |
|
|
47 |
\section howtoread How to Read the Documentation |
|
42 | 48 |
|
43 | 49 |
If you would like to get to know the library, see |
44 | 50 |
<a class="el" href="http://lemon.cs.elte.hu/pub/tutorial/">LEMON Tutorial</a>. |
45 | 51 |
|
52 |
If you are interested in starting to use the library, see the <a class="el" |
|
53 |
href="http://lemon.cs.elte.hu/trac/lemon/wiki/InstallGuide/">Installation |
|
54 |
Guide</a>. |
|
55 |
|
|
46 | 56 |
If you know what you are looking for, then try to find it under the |
47 | 57 |
<a class="el" href="modules.html">Modules</a> section. |
48 | 58 |
|
49 | 59 |
If you are a user of the old (0.x) series of LEMON, please check out the |
50 | 60 |
\ref migration "Migration Guide" for the backward incompatibilities. |
51 | 61 |
*/ |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
namespace lemon { |
20 | 20 |
|
21 | 21 |
/** |
22 | 22 |
\page min_cost_flow Minimum Cost Flow Problem |
23 | 23 |
|
24 | 24 |
\section mcf_def Definition (GEQ form) |
25 | 25 |
|
26 | 26 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
27 | 27 |
minimum total cost from a set of supply nodes to a set of demand nodes |
28 | 28 |
in a network with capacity constraints (lower and upper bounds) |
29 |
and arc costs. |
|
29 |
and arc costs \ref amo93networkflows. |
|
30 | 30 |
|
31 | 31 |
Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$, |
32 | 32 |
\f$upper: A\rightarrow\mathbf{R}\cup\{+\infty\}\f$ denote the lower and |
33 | 33 |
upper bounds for the flow values on the arcs, for which |
34 | 34 |
\f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$, |
35 | 35 |
\f$cost: A\rightarrow\mathbf{R}\f$ denotes the cost per unit flow |
36 | 36 |
on the arcs and \f$sup: V\rightarrow\mathbf{R}\f$ denotes the |
37 | 37 |
signed supply values of the nodes. |
38 | 38 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
39 | 39 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
40 | 40 |
\f$-sup(u)\f$ demand. |
41 | 41 |
A minimum cost flow is an \f$f: A\rightarrow\mathbf{R}\f$ solution |
... | ... |
@@ -69,25 +69,25 @@ |
69 | 69 |
|
70 | 70 |
The dual solution of the minimum cost flow problem is represented by |
71 | 71 |
node potentials \f$\pi: V\rightarrow\mathbf{R}\f$. |
72 | 72 |
An \f$f: A\rightarrow\mathbf{R}\f$ primal feasible solution is optimal |
73 | 73 |
if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ node potentials |
74 | 74 |
the following \e complementary \e slackness optimality conditions hold. |
75 | 75 |
|
76 | 76 |
- For all \f$uv\in A\f$ arcs: |
77 | 77 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
78 | 78 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
79 | 79 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
80 | 80 |
- For all \f$u\in V\f$ nodes: |
81 |
- \f$\pi(u) |
|
81 |
- \f$\pi(u)\leq 0\f$; |
|
82 | 82 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
83 | 83 |
then \f$\pi(u)=0\f$. |
84 | 84 |
|
85 | 85 |
Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc |
86 | 86 |
\f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e. |
87 | 87 |
\f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f] |
88 | 88 |
|
89 | 89 |
All algorithms provide dual solution (node potentials), as well, |
90 | 90 |
if an optimal flow is found. |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
\section mcf_eq Equality Form |
... | ... |
@@ -136,18 +136,18 @@ |
136 | 136 |
Note that the optimality conditions for this supply constraint type are |
137 | 137 |
slightly differ from the conditions that are discussed for the GEQ form, |
138 | 138 |
namely the potentials have to be non-negative instead of non-positive. |
139 | 139 |
An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem |
140 | 140 |
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ |
141 | 141 |
node potentials the following conditions hold. |
142 | 142 |
|
143 | 143 |
- For all \f$uv\in A\f$ arcs: |
144 | 144 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
145 | 145 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
146 | 146 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
147 | 147 |
- For all \f$u\in V\f$ nodes: |
148 |
- \f$\pi(u) |
|
148 |
- \f$\pi(u)\geq 0\f$; |
|
149 | 149 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
150 | 150 |
then \f$\pi(u)=0\f$. |
151 | 151 |
|
152 | 152 |
*/ |
153 | 153 |
} |
... | ... |
@@ -49,72 +49,86 @@ |
49 | 49 |
if HAVE_CLP |
50 | 50 |
lemon_libemon_la_SOURCES += lemon/clp.cc |
51 | 51 |
endif |
52 | 52 |
|
53 | 53 |
if HAVE_CBC |
54 | 54 |
lemon_libemon_la_SOURCES += lemon/cbc.cc |
55 | 55 |
endif |
56 | 56 |
|
57 | 57 |
lemon_HEADERS += \ |
58 | 58 |
lemon/adaptors.h \ |
59 | 59 |
lemon/arg_parser.h \ |
60 | 60 |
lemon/assert.h \ |
61 |
lemon/bellman_ford.h \ |
|
61 | 62 |
lemon/bfs.h \ |
62 | 63 |
lemon/bin_heap.h \ |
64 |
lemon/binomial_heap.h \ |
|
63 | 65 |
lemon/bucket_heap.h \ |
66 |
lemon/capacity_scaling.h \ |
|
64 | 67 |
lemon/cbc.h \ |
65 | 68 |
lemon/circulation.h \ |
66 | 69 |
lemon/clp.h \ |
67 | 70 |
lemon/color.h \ |
68 | 71 |
lemon/concept_check.h \ |
69 | 72 |
lemon/connectivity.h \ |
73 |
lemon/core.h \ |
|
74 |
lemon/cost_scaling.h \ |
|
70 | 75 |
lemon/counter.h \ |
71 |
lemon/core.h \ |
|
72 | 76 |
lemon/cplex.h \ |
77 |
lemon/cycle_canceling.h \ |
|
73 | 78 |
lemon/dfs.h \ |
79 |
lemon/dheap.h \ |
|
74 | 80 |
lemon/dijkstra.h \ |
75 | 81 |
lemon/dim2.h \ |
76 | 82 |
lemon/dimacs.h \ |
77 | 83 |
lemon/edge_set.h \ |
78 | 84 |
lemon/elevator.h \ |
79 | 85 |
lemon/error.h \ |
80 | 86 |
lemon/euler.h \ |
81 | 87 |
lemon/fib_heap.h \ |
88 |
lemon/fractional_matching.h \ |
|
82 | 89 |
lemon/full_graph.h \ |
83 | 90 |
lemon/glpk.h \ |
84 | 91 |
lemon/gomory_hu.h \ |
85 | 92 |
lemon/graph_to_eps.h \ |
86 | 93 |
lemon/grid_graph.h \ |
94 |
lemon/hartmann_orlin_mmc.h \ |
|
95 |
lemon/howard_mmc.h \ |
|
87 | 96 |
lemon/hypercube_graph.h \ |
97 |
lemon/karp_mmc.h \ |
|
88 | 98 |
lemon/kruskal.h \ |
89 | 99 |
lemon/hao_orlin.h \ |
90 | 100 |
lemon/lgf_reader.h \ |
91 | 101 |
lemon/lgf_writer.h \ |
92 | 102 |
lemon/list_graph.h \ |
93 | 103 |
lemon/lp.h \ |
94 | 104 |
lemon/lp_base.h \ |
95 | 105 |
lemon/lp_skeleton.h \ |
96 | 106 |
lemon/maps.h \ |
97 | 107 |
lemon/matching.h \ |
98 | 108 |
lemon/math.h \ |
99 | 109 |
lemon/min_cost_arborescence.h \ |
100 | 110 |
lemon/nauty_reader.h \ |
101 | 111 |
lemon/network_simplex.h \ |
112 |
lemon/pairing_heap.h \ |
|
102 | 113 |
lemon/path.h \ |
114 |
lemon/planarity.h \ |
|
103 | 115 |
lemon/preflow.h \ |
116 |
lemon/quad_heap.h \ |
|
104 | 117 |
lemon/radix_heap.h \ |
105 | 118 |
lemon/radix_sort.h \ |
106 | 119 |
lemon/random.h \ |
107 | 120 |
lemon/smart_graph.h \ |
108 | 121 |
lemon/soplex.h \ |
122 |
lemon/static_graph.h \ |
|
109 | 123 |
lemon/suurballe.h \ |
110 | 124 |
lemon/time_measure.h \ |
111 | 125 |
lemon/tolerance.h \ |
112 | 126 |
lemon/unionfind.h \ |
113 | 127 |
lemon/bits/windows.h |
114 | 128 |
|
115 | 129 |
bits_HEADERS += \ |
116 | 130 |
lemon/bits/alteration_notifier.h \ |
117 | 131 |
lemon/bits/array_map.h \ |
118 | 132 |
lemon/bits/bezier.h \ |
119 | 133 |
lemon/bits/default_map.h \ |
120 | 134 |
lemon/bits/edge_set_extender.h \ |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
... | ... |
@@ -351,24 +351,27 @@ |
351 | 351 |
/// \ingroup graph_adaptors |
352 | 352 |
/// |
353 | 353 |
/// \brief Adaptor class for reversing the orientation of the arcs in |
354 | 354 |
/// a digraph. |
355 | 355 |
/// |
356 | 356 |
/// ReverseDigraph can be used for reversing the arcs in a digraph. |
357 | 357 |
/// It conforms to the \ref concepts::Digraph "Digraph" concept. |
358 | 358 |
/// |
359 | 359 |
/// The adapted digraph can also be modified through this adaptor |
360 | 360 |
/// by adding or removing nodes or arcs, unless the \c GR template |
361 | 361 |
/// parameter is set to be \c const. |
362 | 362 |
/// |
363 |
/// This class provides item counting in the same time as the adapted |
|
364 |
/// digraph structure. |
|
365 |
/// |
|
363 | 366 |
/// \tparam DGR The type of the adapted digraph. |
364 | 367 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
365 | 368 |
/// It can also be specified to be \c const. |
366 | 369 |
/// |
367 | 370 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
368 | 371 |
/// digraph are convertible to each other. |
369 | 372 |
template<typename DGR> |
370 | 373 |
#ifdef DOXYGEN |
371 | 374 |
class ReverseDigraph { |
372 | 375 |
#else |
373 | 376 |
class ReverseDigraph : |
374 | 377 |
public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > { |
... | ... |
@@ -710,24 +713,26 @@ |
710 | 713 |
/// SubDigraph can be used for hiding nodes and arcs in a digraph. |
711 | 714 |
/// A \c bool node map and a \c bool arc map must be specified, which |
712 | 715 |
/// define the filters for nodes and arcs. |
713 | 716 |
/// Only the nodes and arcs with \c true filter value are |
714 | 717 |
/// shown in the subdigraph. The arcs that are incident to hidden |
715 | 718 |
/// nodes are also filtered out. |
716 | 719 |
/// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept. |
717 | 720 |
/// |
718 | 721 |
/// The adapted digraph can also be modified through this adaptor |
719 | 722 |
/// by adding or removing nodes or arcs, unless the \c GR template |
720 | 723 |
/// parameter is set to be \c const. |
721 | 724 |
/// |
725 |
/// This class provides only linear time counting for nodes and arcs. |
|
726 |
/// |
|
722 | 727 |
/// \tparam DGR The type of the adapted digraph. |
723 | 728 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
724 | 729 |
/// It can also be specified to be \c const. |
725 | 730 |
/// \tparam NF The type of the node filter map. |
726 | 731 |
/// It must be a \c bool (or convertible) node map of the |
727 | 732 |
/// adapted digraph. The default type is |
728 | 733 |
/// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>". |
729 | 734 |
/// \tparam AF The type of the arc filter map. |
730 | 735 |
/// It must be \c bool (or convertible) arc map of the |
731 | 736 |
/// adapted digraph. The default type is |
732 | 737 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
733 | 738 |
/// |
... | ... |
@@ -1305,24 +1310,26 @@ |
1305 | 1310 |
/// SubGraph can be used for hiding nodes and edges in a graph. |
1306 | 1311 |
/// A \c bool node map and a \c bool edge map must be specified, which |
1307 | 1312 |
/// define the filters for nodes and edges. |
1308 | 1313 |
/// Only the nodes and edges with \c true filter value are |
1309 | 1314 |
/// shown in the subgraph. The edges that are incident to hidden |
1310 | 1315 |
/// nodes are also filtered out. |
1311 | 1316 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
1312 | 1317 |
/// |
1313 | 1318 |
/// The adapted graph can also be modified through this adaptor |
1314 | 1319 |
/// by adding or removing nodes or edges, unless the \c GR template |
1315 | 1320 |
/// parameter is set to be \c const. |
1316 | 1321 |
/// |
1322 |
/// This class provides only linear time counting for nodes, edges and arcs. |
|
1323 |
/// |
|
1317 | 1324 |
/// \tparam GR The type of the adapted graph. |
1318 | 1325 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1319 | 1326 |
/// It can also be specified to be \c const. |
1320 | 1327 |
/// \tparam NF The type of the node filter map. |
1321 | 1328 |
/// It must be a \c bool (or convertible) node map of the |
1322 | 1329 |
/// adapted graph. The default type is |
1323 | 1330 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1324 | 1331 |
/// \tparam EF The type of the edge filter map. |
1325 | 1332 |
/// It must be a \c bool (or convertible) edge map of the |
1326 | 1333 |
/// adapted graph. The default type is |
1327 | 1334 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1328 | 1335 |
/// |
... | ... |
@@ -1462,24 +1469,26 @@ |
1462 | 1469 |
/// FilterNodes adaptor can be used for hiding nodes in a digraph or a |
1463 | 1470 |
/// graph. A \c bool node map must be specified, which defines the filter |
1464 | 1471 |
/// for the nodes. Only the nodes with \c true filter value and the |
1465 | 1472 |
/// arcs/edges incident to nodes both with \c true filter value are shown |
1466 | 1473 |
/// in the subgraph. This adaptor conforms to the \ref concepts::Digraph |
1467 | 1474 |
/// "Digraph" concept or the \ref concepts::Graph "Graph" concept |
1468 | 1475 |
/// depending on the \c GR template parameter. |
1469 | 1476 |
/// |
1470 | 1477 |
/// The adapted (di)graph can also be modified through this adaptor |
1471 | 1478 |
/// by adding or removing nodes or arcs/edges, unless the \c GR template |
1472 | 1479 |
/// parameter is set to be \c const. |
1473 | 1480 |
/// |
1481 |
/// This class provides only linear time item counting. |
|
1482 |
/// |
|
1474 | 1483 |
/// \tparam GR The type of the adapted digraph or graph. |
1475 | 1484 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept |
1476 | 1485 |
/// or the \ref concepts::Graph "Graph" concept. |
1477 | 1486 |
/// It can also be specified to be \c const. |
1478 | 1487 |
/// \tparam NF The type of the node filter map. |
1479 | 1488 |
/// It must be a \c bool (or convertible) node map of the |
1480 | 1489 |
/// adapted (di)graph. The default type is |
1481 | 1490 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1482 | 1491 |
/// |
1483 | 1492 |
/// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
1484 | 1493 |
/// adapted (di)graph are convertible to each other. |
1485 | 1494 |
#ifdef DOXYGEN |
... | ... |
@@ -1610,24 +1619,26 @@ |
1610 | 1619 |
/// \brief Adaptor class for hiding arcs in a digraph. |
1611 | 1620 |
/// |
1612 | 1621 |
/// FilterArcs adaptor can be used for hiding arcs in a digraph. |
1613 | 1622 |
/// A \c bool arc map must be specified, which defines the filter for |
1614 | 1623 |
/// the arcs. Only the arcs with \c true filter value are shown in the |
1615 | 1624 |
/// subdigraph. This adaptor conforms to the \ref concepts::Digraph |
1616 | 1625 |
/// "Digraph" concept. |
1617 | 1626 |
/// |
1618 | 1627 |
/// The adapted digraph can also be modified through this adaptor |
1619 | 1628 |
/// by adding or removing nodes or arcs, unless the \c GR template |
1620 | 1629 |
/// parameter is set to be \c const. |
1621 | 1630 |
/// |
1631 |
/// This class provides only linear time counting for nodes and arcs. |
|
1632 |
/// |
|
1622 | 1633 |
/// \tparam DGR The type of the adapted digraph. |
1623 | 1634 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
1624 | 1635 |
/// It can also be specified to be \c const. |
1625 | 1636 |
/// \tparam AF The type of the arc filter map. |
1626 | 1637 |
/// It must be a \c bool (or convertible) arc map of the |
1627 | 1638 |
/// adapted digraph. The default type is |
1628 | 1639 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
1629 | 1640 |
/// |
1630 | 1641 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
1631 | 1642 |
/// digraph are convertible to each other. |
1632 | 1643 |
#ifdef DOXYGEN |
1633 | 1644 |
template<typename DGR, |
... | ... |
@@ -1720,24 +1731,26 @@ |
1720 | 1731 |
/// \brief Adaptor class for hiding edges in a graph. |
1721 | 1732 |
/// |
1722 | 1733 |
/// FilterEdges adaptor can be used for hiding edges in a graph. |
1723 | 1734 |
/// A \c bool edge map must be specified, which defines the filter for |
1724 | 1735 |
/// the edges. Only the edges with \c true filter value are shown in the |
1725 | 1736 |
/// subgraph. This adaptor conforms to the \ref concepts::Graph |
1726 | 1737 |
/// "Graph" concept. |
1727 | 1738 |
/// |
1728 | 1739 |
/// The adapted graph can also be modified through this adaptor |
1729 | 1740 |
/// by adding or removing nodes or edges, unless the \c GR template |
1730 | 1741 |
/// parameter is set to be \c const. |
1731 | 1742 |
/// |
1743 |
/// This class provides only linear time counting for nodes, edges and arcs. |
|
1744 |
/// |
|
1732 | 1745 |
/// \tparam GR The type of the adapted graph. |
1733 | 1746 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1734 | 1747 |
/// It can also be specified to be \c const. |
1735 | 1748 |
/// \tparam EF The type of the edge filter map. |
1736 | 1749 |
/// It must be a \c bool (or convertible) edge map of the |
1737 | 1750 |
/// adapted graph. The default type is |
1738 | 1751 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1739 | 1752 |
/// |
1740 | 1753 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1741 | 1754 |
/// adapted graph are convertible to each other. |
1742 | 1755 |
#ifdef DOXYGEN |
1743 | 1756 |
template<typename GR, |
... | ... |
@@ -2223,24 +2236,27 @@ |
2223 | 2236 |
/// |
2224 | 2237 |
/// \brief Adaptor class for viewing a digraph as an undirected graph. |
2225 | 2238 |
/// |
2226 | 2239 |
/// Undirector adaptor can be used for viewing a digraph as an undirected |
2227 | 2240 |
/// graph. All arcs of the underlying digraph are showed in the |
2228 | 2241 |
/// adaptor as an edge (and also as a pair of arcs, of course). |
2229 | 2242 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
2230 | 2243 |
/// |
2231 | 2244 |
/// The adapted digraph can also be modified through this adaptor |
2232 | 2245 |
/// by adding or removing nodes or edges, unless the \c GR template |
2233 | 2246 |
/// parameter is set to be \c const. |
2234 | 2247 |
/// |
2248 |
/// This class provides item counting in the same time as the adapted |
|
2249 |
/// digraph structure. |
|
2250 |
/// |
|
2235 | 2251 |
/// \tparam DGR The type of the adapted digraph. |
2236 | 2252 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2237 | 2253 |
/// It can also be specified to be \c const. |
2238 | 2254 |
/// |
2239 | 2255 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2240 | 2256 |
/// convertible to each other, moreover the \c Edge type of the adaptor |
2241 | 2257 |
/// and the \c Arc type of the adapted digraph are also convertible to |
2242 | 2258 |
/// each other. |
2243 | 2259 |
/// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type |
2244 | 2260 |
/// of the adapted digraph.) |
2245 | 2261 |
template<typename DGR> |
2246 | 2262 |
#ifdef DOXYGEN |
... | ... |
@@ -2526,24 +2542,27 @@ |
2526 | 2542 |
/// |
2527 | 2543 |
/// Orienter adaptor can be used for orienting the edges of a graph to |
2528 | 2544 |
/// get a digraph. A \c bool edge map of the underlying graph must be |
2529 | 2545 |
/// specified, which define the direction of the arcs in the adaptor. |
2530 | 2546 |
/// The arcs can be easily reversed by the \c reverseArc() member function |
2531 | 2547 |
/// of the adaptor. |
2532 | 2548 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2533 | 2549 |
/// |
2534 | 2550 |
/// The adapted graph can also be modified through this adaptor |
2535 | 2551 |
/// by adding or removing nodes or arcs, unless the \c GR template |
2536 | 2552 |
/// parameter is set to be \c const. |
2537 | 2553 |
/// |
2554 |
/// This class provides item counting in the same time as the adapted |
|
2555 |
/// graph structure. |
|
2556 |
/// |
|
2538 | 2557 |
/// \tparam GR The type of the adapted graph. |
2539 | 2558 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
2540 | 2559 |
/// It can also be specified to be \c const. |
2541 | 2560 |
/// \tparam DM The type of the direction map. |
2542 | 2561 |
/// It must be a \c bool (or convertible) edge map of the |
2543 | 2562 |
/// adapted graph. The default type is |
2544 | 2563 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
2545 | 2564 |
/// |
2546 | 2565 |
/// \note The \c Node type of this adaptor and the adapted graph are |
2547 | 2566 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
2548 | 2567 |
/// and the \c Edge type of the adapted graph are also convertible to |
2549 | 2568 |
/// each other. |
... | ... |
@@ -2669,24 +2688,26 @@ |
2669 | 2688 |
/// Let \f$ flow, cap: A\to F \f$ be functions on the arcs. |
2670 | 2689 |
/// This adaptor implements a digraph structure with node set \f$ V \f$ |
2671 | 2690 |
/// and arc set \f$ A_{forward}\cup A_{backward} \f$, |
2672 | 2691 |
/// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and |
2673 | 2692 |
/// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so |
2674 | 2693 |
/// called residual digraph. |
2675 | 2694 |
/// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken, |
2676 | 2695 |
/// multiplicities are counted, i.e. the adaptor has exactly |
2677 | 2696 |
/// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel |
2678 | 2697 |
/// arcs). |
2679 | 2698 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2680 | 2699 |
/// |
2700 |
/// This class provides only linear time counting for nodes and arcs. |
|
2701 |
/// |
|
2681 | 2702 |
/// \tparam DGR The type of the adapted digraph. |
2682 | 2703 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2683 | 2704 |
/// It is implicitly \c const. |
2684 | 2705 |
/// \tparam CM The type of the capacity map. |
2685 | 2706 |
/// It must be an arc map of some numerical type, which defines |
2686 | 2707 |
/// the capacities in the flow problem. It is implicitly \c const. |
2687 | 2708 |
/// The default type is |
2688 | 2709 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
2689 | 2710 |
/// \tparam FM The type of the flow map. |
2690 | 2711 |
/// It must be an arc map of some numerical type, which defines |
2691 | 2712 |
/// the flow values in the flow problem. The default type is \c CM. |
2692 | 2713 |
/// \tparam TL The tolerance type for handling inexact computation. |
... | ... |
@@ -3316,24 +3337,27 @@ |
3316 | 3337 |
/// new target of the arc will be \f$ u_{in} \f$ and similarly the |
3317 | 3338 |
/// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$. |
3318 | 3339 |
/// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$ |
3319 | 3340 |
/// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph. |
3320 | 3341 |
/// |
3321 | 3342 |
/// The aim of this class is running an algorithm with respect to node |
3322 | 3343 |
/// costs or capacities if the algorithm considers only arc costs or |
3323 | 3344 |
/// capacities directly. |
3324 | 3345 |
/// In this case you can use \c SplitNodes adaptor, and set the node |
3325 | 3346 |
/// costs/capacities of the original digraph to the \e bind \e arcs |
3326 | 3347 |
/// in the adaptor. |
3327 | 3348 |
/// |
3349 |
/// This class provides item counting in the same time as the adapted |
|
3350 |
/// digraph structure. |
|
3351 |
/// |
|
3328 | 3352 |
/// \tparam DGR The type of the adapted digraph. |
3329 | 3353 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
3330 | 3354 |
/// It is implicitly \c const. |
3331 | 3355 |
/// |
3332 | 3356 |
/// \note The \c Node type of this adaptor is converible to the \c Node |
3333 | 3357 |
/// type of the adapted digraph. |
3334 | 3358 |
template <typename DGR> |
3335 | 3359 |
#ifdef DOXYGEN |
3336 | 3360 |
class SplitNodes { |
3337 | 3361 |
#else |
3338 | 3362 |
class SplitNodes |
3339 | 3363 |
: public DigraphAdaptorExtender<SplitNodesBase<const DGR> > { |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/arg_parser.h> |
20 | 20 |
|
21 | 21 |
namespace lemon { |
22 | 22 |
|
23 |
void ArgParser::_terminate(ArgParserException::Reason reason) const |
|
24 |
{ |
|
25 |
if(_exit_on_problems) |
|
26 |
exit(1); |
|
27 |
else throw(ArgParserException(reason)); |
|
28 |
} |
|
29 |
|
|
30 |
|
|
23 | 31 |
void ArgParser::_showHelp(void *p) |
24 | 32 |
{ |
25 | 33 |
(static_cast<ArgParser*>(p))->showHelp(); |
26 |
|
|
34 |
(static_cast<ArgParser*>(p))->_terminate(ArgParserException::HELP); |
|
27 | 35 |
} |
28 | 36 |
|
29 | 37 |
ArgParser::ArgParser(int argc, const char * const *argv) |
30 |
:_argc(argc), _argv(argv), _command_name(argv[0]) |
|
38 |
:_argc(argc), _argv(argv), _command_name(argv[0]), |
|
39 |
_exit_on_problems(true) { |
|
31 | 40 |
funcOption("-help","Print a short help message",_showHelp,this); |
32 | 41 |
synonym("help","-help"); |
33 | 42 |
synonym("h","-help"); |
34 | 43 |
} |
35 | 44 |
|
36 | 45 |
ArgParser::~ArgParser() |
37 | 46 |
{ |
38 | 47 |
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
39 | 48 |
if(i->second.self_delete) |
40 | 49 |
switch(i->second.type) { |
41 | 50 |
case BOOL: |
42 | 51 |
delete i->second.bool_p; |
... | ... |
@@ -333,34 +342,34 @@ |
333 | 342 |
pos+=cstr.str().size(); |
334 | 343 |
} |
335 | 344 |
std::cerr << std::endl; |
336 | 345 |
} |
337 | 346 |
|
338 | 347 |
void ArgParser::showHelp() const |
339 | 348 |
{ |
340 | 349 |
shortHelp(); |
341 | 350 |
std::cerr << "Where:\n"; |
342 | 351 |
for(std::vector<OtherArg>::const_iterator i=_others_help.begin(); |
343 | 352 |
i!=_others_help.end();++i) showHelp(i); |
344 | 353 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i); |
345 |
|
|
354 |
_terminate(ArgParserException::HELP); |
|
346 | 355 |
} |
347 | 356 |
|
348 | 357 |
|
349 | 358 |
void ArgParser::unknownOpt(std::string arg) const |
350 | 359 |
{ |
351 | 360 |
std::cerr << "\nUnknown option: " << arg << "\n"; |
352 | 361 |
std::cerr << "\nType '" << _command_name << |
353 | 362 |
" --help' to obtain a short summary on the usage.\n\n"; |
354 |
|
|
363 |
_terminate(ArgParserException::UNKNOWN_OPT); |
|
355 | 364 |
} |
356 | 365 |
|
357 | 366 |
void ArgParser::requiresValue(std::string arg, OptType t) const |
358 | 367 |
{ |
359 | 368 |
std::cerr << "Argument '" << arg << "' requires a"; |
360 | 369 |
switch(t) { |
361 | 370 |
case STRING: |
362 | 371 |
std::cerr << " string"; |
363 | 372 |
break; |
364 | 373 |
case INTEGER: |
365 | 374 |
std::cerr << "n integer"; |
366 | 375 |
break; |
... | ... |
@@ -405,25 +414,25 @@ |
405 | 414 |
if(i->second.only_one&&set>1) { |
406 | 415 |
std::cerr << _command_name << |
407 | 416 |
": At most one of the following arguments can be given.\n"; |
408 | 417 |
ok=false; |
409 | 418 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
410 | 419 |
o!=i->second.opts.end();++o) |
411 | 420 |
showHelp(_opts.find(*o)); |
412 | 421 |
} |
413 | 422 |
} |
414 | 423 |
if(!ok) { |
415 | 424 |
std::cerr << "\nType '" << _command_name << |
416 | 425 |
" --help' to obtain a short summary on the usage.\n\n"; |
417 |
|
|
426 |
_terminate(ArgParserException::INVALID_OPT); |
|
418 | 427 |
} |
419 | 428 |
} |
420 | 429 |
|
421 | 430 |
ArgParser &ArgParser::parse() |
422 | 431 |
{ |
423 | 432 |
for(int ar=1; ar<_argc; ++ar) { |
424 | 433 |
std::string arg(_argv[ar]); |
425 | 434 |
if (arg[0] != '-' || arg.size() == 1) { |
426 | 435 |
_file_args.push_back(arg); |
427 | 436 |
} |
428 | 437 |
else { |
429 | 438 |
Opts::iterator i = _opts.find(arg.substr(1)); |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
... | ... |
@@ -25,24 +25,62 @@ |
25 | 25 |
#include <string> |
26 | 26 |
#include <iostream> |
27 | 27 |
#include <sstream> |
28 | 28 |
#include <algorithm> |
29 | 29 |
#include <lemon/assert.h> |
30 | 30 |
|
31 | 31 |
///\ingroup misc |
32 | 32 |
///\file |
33 | 33 |
///\brief A tool to parse command line arguments. |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 |
///Exception used by ArgParser |
|
38 |
class ArgParserException : public Exception { |
|
39 |
public: |
|
40 |
enum Reason { |
|
41 |
HELP, /// <tt>--help</tt> option was given |
|
42 |
UNKNOWN_OPT, /// Unknown option was given |
|
43 |
INVALID_OPT /// Invalid combination of options |
|
44 |
}; |
|
45 |
|
|
46 |
private: |
|
47 |
Reason _reason; |
|
48 |
|
|
49 |
public: |
|
50 |
///Constructor |
|
51 |
ArgParserException(Reason r) throw() : _reason(r) {} |
|
52 |
///Virtual destructor |
|
53 |
virtual ~ArgParserException() throw() {} |
|
54 |
///A short description of the exception |
|
55 |
virtual const char* what() const throw() { |
|
56 |
switch(_reason) |
|
57 |
{ |
|
58 |
case HELP: |
|
59 |
return "lemon::ArgParseException: ask for help"; |
|
60 |
break; |
|
61 |
case UNKNOWN_OPT: |
|
62 |
return "lemon::ArgParseException: unknown option"; |
|
63 |
break; |
|
64 |
case INVALID_OPT: |
|
65 |
return "lemon::ArgParseException: invalid combination of options"; |
|
66 |
break; |
|
67 |
} |
|
68 |
return ""; |
|
69 |
} |
|
70 |
///Return the reason for the failure |
|
71 |
Reason reason() const {return _reason; } |
|
72 |
}; |
|
73 |
|
|
74 |
|
|
37 | 75 |
///Command line arguments parser |
38 | 76 |
|
39 | 77 |
///\ingroup misc |
40 | 78 |
///Command line arguments parser. |
41 | 79 |
/// |
42 | 80 |
///For a complete example see the \ref arg_parser_demo.cc demo file. |
43 | 81 |
class ArgParser { |
44 | 82 |
|
45 | 83 |
static void _showHelp(void *p); |
46 | 84 |
protected: |
47 | 85 |
|
48 | 86 |
int _argc; |
... | ... |
@@ -107,24 +145,28 @@ |
107 | 145 |
private: |
108 | 146 |
//Bind a function to an option. |
109 | 147 |
|
110 | 148 |
//\param name The name of the option. The leading '-' must be omitted. |
111 | 149 |
//\param help A help string. |
112 | 150 |
//\retval func The function to be called when the option is given. It |
113 | 151 |
// must be of type "void f(void *)" |
114 | 152 |
//\param data Data to be passed to \c func |
115 | 153 |
ArgParser &funcOption(const std::string &name, |
116 | 154 |
const std::string &help, |
117 | 155 |
void (*func)(void *),void *data); |
118 | 156 |
|
157 |
bool _exit_on_problems; |
|
158 |
|
|
159 |
void _terminate(ArgParserException::Reason reason) const; |
|
160 |
|
|
119 | 161 |
public: |
120 | 162 |
|
121 | 163 |
///Constructor |
122 | 164 |
ArgParser(int argc, const char * const *argv); |
123 | 165 |
|
124 | 166 |
~ArgParser(); |
125 | 167 |
|
126 | 168 |
///\name Options |
127 | 169 |
/// |
128 | 170 |
|
129 | 171 |
///@{ |
130 | 172 |
|
... | ... |
@@ -371,16 +413,21 @@ |
371 | 413 |
///\sa RefType |
372 | 414 |
RefType operator[](const std::string &n) const |
373 | 415 |
{ |
374 | 416 |
return RefType(*this, n); |
375 | 417 |
} |
376 | 418 |
|
377 | 419 |
///Give back the non-option type arguments. |
378 | 420 |
|
379 | 421 |
///Give back a reference to a vector consisting of the program arguments |
380 | 422 |
///not starting with a '-' character. |
381 | 423 |
const std::vector<std::string> &files() const { return _file_args; } |
382 | 424 |
|
425 |
///Throw instead of exit in case of problems |
|
426 |
void throwOnProblems() |
|
427 |
{ |
|
428 |
_exit_on_problems=false; |
|
429 |
} |
|
383 | 430 |
}; |
384 | 431 |
} |
385 | 432 |
|
386 | 433 |
#endif // LEMON_ARG_PARSER_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
... | ... |
@@ -38,97 +38,104 @@ |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest paths. |
50 |
///It must |
|
50 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 |
///It must |
|
65 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
66 |
///By default, it is a NullMap. |
|
66 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 | 68 |
///Instantiates a \c ProcessedMap. |
68 | 69 |
|
69 | 70 |
///This function instantiates a \ref ProcessedMap. |
70 | 71 |
///\param g is the digraph, to which |
71 | 72 |
///we would like to define the \ref ProcessedMap |
72 | 73 |
#ifdef DOXYGEN |
73 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
74 | 75 |
#else |
75 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
76 | 77 |
#endif |
77 | 78 |
{ |
78 | 79 |
return new ProcessedMap(); |
79 | 80 |
} |
80 | 81 |
|
81 | 82 |
///The type of the map that indicates which nodes are reached. |
82 | 83 |
|
83 | 84 |
///The type of the map that indicates which nodes are reached. |
84 |
///It must |
|
85 |
///It must conform to |
|
86 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
85 | 87 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
86 | 88 |
///Instantiates a \c ReachedMap. |
87 | 89 |
|
88 | 90 |
///This function instantiates a \ref ReachedMap. |
89 | 91 |
///\param g is the digraph, to which |
90 | 92 |
///we would like to define the \ref ReachedMap. |
91 | 93 |
static ReachedMap *createReachedMap(const Digraph &g) |
92 | 94 |
{ |
93 | 95 |
return new ReachedMap(g); |
94 | 96 |
} |
95 | 97 |
|
96 | 98 |
///The type of the map that stores the distances of the nodes. |
97 | 99 |
|
98 | 100 |
///The type of the map that stores the distances of the nodes. |
99 |
///It must |
|
101 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
100 | 102 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 | 103 |
///Instantiates a \c DistMap. |
102 | 104 |
|
103 | 105 |
///This function instantiates a \ref DistMap. |
104 | 106 |
///\param g is the digraph, to which we would like to define the |
105 | 107 |
///\ref DistMap. |
106 | 108 |
static DistMap *createDistMap(const Digraph &g) |
107 | 109 |
{ |
108 | 110 |
return new DistMap(g); |
109 | 111 |
} |
110 | 112 |
}; |
111 | 113 |
|
112 | 114 |
///%BFS algorithm class. |
113 | 115 |
|
114 | 116 |
///\ingroup search |
115 | 117 |
///This class provides an efficient implementation of the %BFS algorithm. |
116 | 118 |
/// |
117 | 119 |
///There is also a \ref bfs() "function-type interface" for the BFS |
118 | 120 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 121 |
///used easier. |
120 | 122 |
/// |
121 | 123 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 | 124 |
///The default type is \ref ListDigraph. |
125 |
///\tparam TR The traits class that defines various types used by the |
|
126 |
///algorithm. By default, it is \ref BfsDefaultTraits |
|
127 |
///"BfsDefaultTraits<GR>". |
|
128 |
///In most cases, this parameter should not be set directly, |
|
129 |
///consider to use the named template parameters instead. |
|
123 | 130 |
#ifdef DOXYGEN |
124 | 131 |
template <typename GR, |
125 | 132 |
typename TR> |
126 | 133 |
#else |
127 | 134 |
template <typename GR=ListDigraph, |
128 | 135 |
typename TR=BfsDefaultTraits<GR> > |
129 | 136 |
#endif |
130 | 137 |
class Bfs { |
131 | 138 |
public: |
132 | 139 |
|
133 | 140 |
///The type of the digraph the algorithm runs on. |
134 | 141 |
typedef typename TR::Digraph Digraph; |
... | ... |
@@ -216,85 +223,86 @@ |
216 | 223 |
typedef T PredMap; |
217 | 224 |
static PredMap *createPredMap(const Digraph &) |
218 | 225 |
{ |
219 | 226 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
220 | 227 |
return 0; // ignore warnings |
221 | 228 |
} |
222 | 229 |
}; |
223 | 230 |
///\brief \ref named-templ-param "Named parameter" for setting |
224 | 231 |
///\c PredMap type. |
225 | 232 |
/// |
226 | 233 |
///\ref named-templ-param "Named parameter" for setting |
227 | 234 |
///\c PredMap type. |
228 |
///It must |
|
235 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
229 | 236 |
template <class T> |
230 | 237 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
231 | 238 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
232 | 239 |
}; |
233 | 240 |
|
234 | 241 |
template <class T> |
235 | 242 |
struct SetDistMapTraits : public Traits { |
236 | 243 |
typedef T DistMap; |
237 | 244 |
static DistMap *createDistMap(const Digraph &) |
238 | 245 |
{ |
239 | 246 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
240 | 247 |
return 0; // ignore warnings |
241 | 248 |
} |
242 | 249 |
}; |
243 | 250 |
///\brief \ref named-templ-param "Named parameter" for setting |
244 | 251 |
///\c DistMap type. |
245 | 252 |
/// |
246 | 253 |
///\ref named-templ-param "Named parameter" for setting |
247 | 254 |
///\c DistMap type. |
248 |
///It must |
|
255 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
249 | 256 |
template <class T> |
250 | 257 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
251 | 258 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
252 | 259 |
}; |
253 | 260 |
|
254 | 261 |
template <class T> |
255 | 262 |
struct SetReachedMapTraits : public Traits { |
256 | 263 |
typedef T ReachedMap; |
257 | 264 |
static ReachedMap *createReachedMap(const Digraph &) |
258 | 265 |
{ |
259 | 266 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
260 | 267 |
return 0; // ignore warnings |
261 | 268 |
} |
262 | 269 |
}; |
263 | 270 |
///\brief \ref named-templ-param "Named parameter" for setting |
264 | 271 |
///\c ReachedMap type. |
265 | 272 |
/// |
266 | 273 |
///\ref named-templ-param "Named parameter" for setting |
267 | 274 |
///\c ReachedMap type. |
268 |
///It must |
|
275 |
///It must conform to |
|
276 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
269 | 277 |
template <class T> |
270 | 278 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > { |
271 | 279 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
272 | 280 |
}; |
273 | 281 |
|
274 | 282 |
template <class T> |
275 | 283 |
struct SetProcessedMapTraits : public Traits { |
276 | 284 |
typedef T ProcessedMap; |
277 | 285 |
static ProcessedMap *createProcessedMap(const Digraph &) |
278 | 286 |
{ |
279 | 287 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
280 | 288 |
return 0; // ignore warnings |
281 | 289 |
} |
282 | 290 |
}; |
283 | 291 |
///\brief \ref named-templ-param "Named parameter" for setting |
284 | 292 |
///\c ProcessedMap type. |
285 | 293 |
/// |
286 | 294 |
///\ref named-templ-param "Named parameter" for setting |
287 | 295 |
///\c ProcessedMap type. |
288 |
///It must |
|
296 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
289 | 297 |
template <class T> |
290 | 298 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > { |
291 | 299 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
292 | 300 |
}; |
293 | 301 |
|
294 | 302 |
struct SetStandardProcessedMapTraits : public Traits { |
295 | 303 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
296 | 304 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
297 | 305 |
{ |
298 | 306 |
return new ProcessedMap(g); |
299 | 307 |
return 0; // ignore warnings |
300 | 308 |
} |
... | ... |
@@ -404,26 +412,26 @@ |
404 | 412 |
delete _dist; |
405 | 413 |
local_dist=false; |
406 | 414 |
} |
407 | 415 |
_dist = &m; |
408 | 416 |
return *this; |
409 | 417 |
} |
410 | 418 |
|
411 | 419 |
public: |
412 | 420 |
|
413 | 421 |
///\name Execution Control |
414 | 422 |
///The simplest way to execute the BFS algorithm is to use one of the |
415 | 423 |
///member functions called \ref run(Node) "run()".\n |
416 |
///If you need more control on the execution, first you have to call |
|
417 |
///\ref init(), then you can add several source nodes with |
|
424 |
///If you need better control on the execution, you have to call |
|
425 |
///\ref init() first, then you can add several source nodes with |
|
418 | 426 |
///\ref addSource(). Finally the actual path computation can be |
419 | 427 |
///performed with one of the \ref start() functions. |
420 | 428 |
|
421 | 429 |
///@{ |
422 | 430 |
|
423 | 431 |
///\brief Initializes the internal data structures. |
424 | 432 |
/// |
425 | 433 |
///Initializes the internal data structures. |
426 | 434 |
void init() |
427 | 435 |
{ |
428 | 436 |
create_maps(); |
429 | 437 |
_queue.resize(countNodes(*G)); |
... | ... |
@@ -691,30 +699,26 @@ |
691 | 699 |
/// b.addSource(s); |
692 | 700 |
/// b.start(t); |
693 | 701 |
///\endcode |
694 | 702 |
bool run(Node s,Node t) { |
695 | 703 |
init(); |
696 | 704 |
addSource(s); |
697 | 705 |
start(t); |
698 | 706 |
return reached(t); |
699 | 707 |
} |
700 | 708 |
|
701 | 709 |
///Runs the algorithm to visit all nodes in the digraph. |
702 | 710 |
|
703 |
///This method runs the %BFS algorithm in order to |
|
704 |
///compute the shortest path to each node. |
|
705 |
/// |
|
706 |
///The algorithm computes |
|
707 |
///- the shortest path tree (forest), |
|
708 |
///- the distance of each node from the root(s). |
|
711 |
///This method runs the %BFS algorithm in order to visit all nodes |
|
712 |
///in the digraph. |
|
709 | 713 |
/// |
710 | 714 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
711 | 715 |
///\code |
712 | 716 |
/// b.init(); |
713 | 717 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
714 | 718 |
/// if (!b.reached(n)) { |
715 | 719 |
/// b.addSource(n); |
716 | 720 |
/// b.start(); |
717 | 721 |
/// } |
718 | 722 |
/// } |
719 | 723 |
///\endcode |
720 | 724 |
void run() { |
... | ... |
@@ -728,95 +732,97 @@ |
728 | 732 |
} |
729 | 733 |
|
730 | 734 |
///@} |
731 | 735 |
|
732 | 736 |
///\name Query Functions |
733 | 737 |
///The results of the BFS algorithm can be obtained using these |
734 | 738 |
///functions.\n |
735 | 739 |
///Either \ref run(Node) "run()" or \ref start() should be called |
736 | 740 |
///before using them. |
737 | 741 |
|
738 | 742 |
///@{ |
739 | 743 |
|
740 |
///The shortest path to |
|
744 |
///The shortest path to the given node. |
|
741 | 745 |
|
742 |
///Returns the shortest path to |
|
746 |
///Returns the shortest path to the given node from the root(s). |
|
743 | 747 |
/// |
744 | 748 |
///\warning \c t should be reached from the root(s). |
745 | 749 |
/// |
746 | 750 |
///\pre Either \ref run(Node) "run()" or \ref init() |
747 | 751 |
///must be called before using this function. |
748 | 752 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
749 | 753 |
|
750 |
///The distance of |
|
754 |
///The distance of the given node from the root(s). |
|
751 | 755 |
|
752 |
///Returns the distance of |
|
756 |
///Returns the distance of the given node from the root(s). |
|
753 | 757 |
/// |
754 | 758 |
///\warning If node \c v is not reached from the root(s), then |
755 | 759 |
///the return value of this function is undefined. |
756 | 760 |
/// |
757 | 761 |
///\pre Either \ref run(Node) "run()" or \ref init() |
758 | 762 |
///must be called before using this function. |
759 | 763 |
int dist(Node v) const { return (*_dist)[v]; } |
760 | 764 |
|
761 |
///Returns the 'previous arc' of the shortest path tree for a node. |
|
762 |
|
|
765 |
///\brief Returns the 'previous arc' of the shortest path tree for |
|
766 |
///the given node. |
|
767 |
/// |
|
763 | 768 |
///This function returns the 'previous arc' of the shortest path |
764 | 769 |
///tree for the node \c v, i.e. it returns the last arc of a |
765 | 770 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
766 | 771 |
///is not reached from the root(s) or if \c v is a root. |
767 | 772 |
/// |
768 | 773 |
///The shortest path tree used here is equal to the shortest path |
769 |
///tree used in \ref predNode(). |
|
774 |
///tree used in \ref predNode() and \ref predMap(). |
|
770 | 775 |
/// |
771 | 776 |
///\pre Either \ref run(Node) "run()" or \ref init() |
772 | 777 |
///must be called before using this function. |
773 | 778 |
Arc predArc(Node v) const { return (*_pred)[v];} |
774 | 779 |
|
775 |
///Returns the 'previous node' of the shortest path tree for a node. |
|
776 |
|
|
780 |
///\brief Returns the 'previous node' of the shortest path tree for |
|
781 |
///the given node. |
|
782 |
/// |
|
777 | 783 |
///This function returns the 'previous node' of the shortest path |
778 | 784 |
///tree for the node \c v, i.e. it returns the last but one node |
779 |
/// |
|
785 |
///of a shortest path from a root to \c v. It is \c INVALID |
|
780 | 786 |
///if \c v is not reached from the root(s) or if \c v is a root. |
781 | 787 |
/// |
782 | 788 |
///The shortest path tree used here is equal to the shortest path |
783 |
///tree used in \ref predArc(). |
|
789 |
///tree used in \ref predArc() and \ref predMap(). |
|
784 | 790 |
/// |
785 | 791 |
///\pre Either \ref run(Node) "run()" or \ref init() |
786 | 792 |
///must be called before using this function. |
787 | 793 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
788 | 794 |
G->source((*_pred)[v]); } |
789 | 795 |
|
790 | 796 |
///\brief Returns a const reference to the node map that stores the |
791 | 797 |
/// distances of the nodes. |
792 | 798 |
/// |
793 | 799 |
///Returns a const reference to the node map that stores the distances |
794 | 800 |
///of the nodes calculated by the algorithm. |
795 | 801 |
/// |
796 | 802 |
///\pre Either \ref run(Node) "run()" or \ref init() |
797 | 803 |
///must be called before using this function. |
798 | 804 |
const DistMap &distMap() const { return *_dist;} |
799 | 805 |
|
800 | 806 |
///\brief Returns a const reference to the node map that stores the |
801 | 807 |
///predecessor arcs. |
802 | 808 |
/// |
803 | 809 |
///Returns a const reference to the node map that stores the predecessor |
804 |
///arcs, which form the shortest path tree. |
|
810 |
///arcs, which form the shortest path tree (forest). |
|
805 | 811 |
/// |
806 | 812 |
///\pre Either \ref run(Node) "run()" or \ref init() |
807 | 813 |
///must be called before using this function. |
808 | 814 |
const PredMap &predMap() const { return *_pred;} |
809 | 815 |
|
810 |
///Checks if |
|
816 |
///Checks if the given node is reached from the root(s). |
|
811 | 817 |
|
812 | 818 |
///Returns \c true if \c v is reached from the root(s). |
813 | 819 |
/// |
814 | 820 |
///\pre Either \ref run(Node) "run()" or \ref init() |
815 | 821 |
///must be called before using this function. |
816 | 822 |
bool reached(Node v) const { return (*_reached)[v]; } |
817 | 823 |
|
818 | 824 |
///@} |
819 | 825 |
}; |
820 | 826 |
|
821 | 827 |
///Default traits class of bfs() function. |
822 | 828 |
|
... | ... |
@@ -824,101 +830,98 @@ |
824 | 830 |
///\tparam GR Digraph type. |
825 | 831 |
template<class GR> |
826 | 832 |
struct BfsWizardDefaultTraits |
827 | 833 |
{ |
828 | 834 |
///The type of the digraph the algorithm runs on. |
829 | 835 |
typedef GR Digraph; |
830 | 836 |
|
831 | 837 |
///\brief The type of the map that stores the predecessor |
832 | 838 |
///arcs of the shortest paths. |
833 | 839 |
/// |
834 | 840 |
///The type of the map that stores the predecessor |
835 | 841 |
///arcs of the shortest paths. |
836 |
///It must |
|
842 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
837 | 843 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
838 | 844 |
///Instantiates a PredMap. |
839 | 845 |
|
840 | 846 |
///This function instantiates a PredMap. |
841 | 847 |
///\param g is the digraph, to which we would like to define the |
842 | 848 |
///PredMap. |
843 | 849 |
static PredMap *createPredMap(const Digraph &g) |
844 | 850 |
{ |
845 | 851 |
return new PredMap(g); |
846 | 852 |
} |
847 | 853 |
|
848 | 854 |
///The type of the map that indicates which nodes are processed. |
849 | 855 |
|
850 | 856 |
///The type of the map that indicates which nodes are processed. |
851 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
852 |
///By default it is a NullMap. |
|
857 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
858 |
///By default, it is a NullMap. |
|
853 | 859 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
854 | 860 |
///Instantiates a ProcessedMap. |
855 | 861 |
|
856 | 862 |
///This function instantiates a ProcessedMap. |
857 | 863 |
///\param g is the digraph, to which |
858 | 864 |
///we would like to define the ProcessedMap. |
859 | 865 |
#ifdef DOXYGEN |
860 | 866 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
861 | 867 |
#else |
862 | 868 |
static ProcessedMap *createProcessedMap(const Digraph &) |
863 | 869 |
#endif |
864 | 870 |
{ |
865 | 871 |
return new ProcessedMap(); |
866 | 872 |
} |
867 | 873 |
|
868 | 874 |
///The type of the map that indicates which nodes are reached. |
869 | 875 |
|
870 | 876 |
///The type of the map that indicates which nodes are reached. |
871 |
///It must |
|
877 |
///It must conform to |
|
878 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
872 | 879 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
873 | 880 |
///Instantiates a ReachedMap. |
874 | 881 |
|
875 | 882 |
///This function instantiates a ReachedMap. |
876 | 883 |
///\param g is the digraph, to which |
877 | 884 |
///we would like to define the ReachedMap. |
878 | 885 |
static ReachedMap *createReachedMap(const Digraph &g) |
879 | 886 |
{ |
880 | 887 |
return new ReachedMap(g); |
881 | 888 |
} |
882 | 889 |
|
883 | 890 |
///The type of the map that stores the distances of the nodes. |
884 | 891 |
|
885 | 892 |
///The type of the map that stores the distances of the nodes. |
886 |
///It must |
|
893 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
887 | 894 |
typedef typename Digraph::template NodeMap<int> DistMap; |
888 | 895 |
///Instantiates a DistMap. |
889 | 896 |
|
890 | 897 |
///This function instantiates a DistMap. |
891 | 898 |
///\param g is the digraph, to which we would like to define |
892 | 899 |
///the DistMap |
893 | 900 |
static DistMap *createDistMap(const Digraph &g) |
894 | 901 |
{ |
895 | 902 |
return new DistMap(g); |
896 | 903 |
} |
897 | 904 |
|
898 | 905 |
///The type of the shortest paths. |
899 | 906 |
|
900 | 907 |
///The type of the shortest paths. |
901 |
///It must |
|
908 |
///It must conform to the \ref concepts::Path "Path" concept. |
|
902 | 909 |
typedef lemon::Path<Digraph> Path; |
903 | 910 |
}; |
904 | 911 |
|
905 | 912 |
/// Default traits class used by BfsWizard |
906 | 913 |
|
907 |
/// To make it easier to use Bfs algorithm |
|
908 |
/// we have created a wizard class. |
|
909 |
/// This \ref BfsWizard class needs default traits, |
|
910 |
/// as well as the \ref Bfs class. |
|
911 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
|
912 |
/// \ref BfsWizard class. |
|
914 |
/// Default traits class used by BfsWizard. |
|
915 |
/// \tparam GR The type of the digraph. |
|
913 | 916 |
template<class GR> |
914 | 917 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
915 | 918 |
{ |
916 | 919 |
|
917 | 920 |
typedef BfsWizardDefaultTraits<GR> Base; |
918 | 921 |
protected: |
919 | 922 |
//The type of the nodes in the digraph. |
920 | 923 |
typedef typename Base::Digraph::Node Node; |
921 | 924 |
|
922 | 925 |
//Pointer to the digraph the algorithm runs on. |
923 | 926 |
void *_g; |
924 | 927 |
//Pointer to the map of reached nodes. |
... | ... |
@@ -928,72 +931,68 @@ |
928 | 931 |
//Pointer to the map of predecessors arcs. |
929 | 932 |
void *_pred; |
930 | 933 |
//Pointer to the map of distances. |
931 | 934 |
void *_dist; |
932 | 935 |
//Pointer to the shortest path to the target node. |
933 | 936 |
void *_path; |
934 | 937 |
//Pointer to the distance of the target node. |
935 | 938 |
int *_di; |
936 | 939 |
|
937 | 940 |
public: |
938 | 941 |
/// Constructor. |
939 | 942 |
|
940 |
/// This constructor does not require parameters, |
|
943 |
/// This constructor does not require parameters, it initiates |
|
941 | 944 |
/// all of the attributes to \c 0. |
942 | 945 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
943 | 946 |
_dist(0), _path(0), _di(0) {} |
944 | 947 |
|
945 | 948 |
/// Constructor. |
946 | 949 |
|
947 | 950 |
/// This constructor requires one parameter, |
948 | 951 |
/// others are initiated to \c 0. |
949 | 952 |
/// \param g The digraph the algorithm runs on. |
950 | 953 |
BfsWizardBase(const GR &g) : |
951 | 954 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
952 | 955 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
953 | 956 |
|
954 | 957 |
}; |
955 | 958 |
|
956 | 959 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
957 | 960 |
|
958 | 961 |
/// This auxiliary class is created to implement the |
959 | 962 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
960 | 963 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
961 | 964 |
/// functions and features of the plain \ref Bfs. |
962 | 965 |
/// |
963 | 966 |
/// This class should only be used through the \ref bfs() function, |
964 | 967 |
/// which makes it easier to use the algorithm. |
968 |
/// |
|
969 |
/// \tparam TR The traits class that defines various types used by the |
|
970 |
/// algorithm. |
|
965 | 971 |
template<class TR> |
966 | 972 |
class BfsWizard : public TR |
967 | 973 |
{ |
968 | 974 |
typedef TR Base; |
969 | 975 |
|
970 |
///The type of the digraph the algorithm runs on. |
|
971 | 976 |
typedef typename TR::Digraph Digraph; |
972 | 977 |
|
973 | 978 |
typedef typename Digraph::Node Node; |
974 | 979 |
typedef typename Digraph::NodeIt NodeIt; |
975 | 980 |
typedef typename Digraph::Arc Arc; |
976 | 981 |
typedef typename Digraph::OutArcIt OutArcIt; |
977 | 982 |
|
978 |
///\brief The type of the map that stores the predecessor |
|
979 |
///arcs of the shortest paths. |
|
980 | 983 |
typedef typename TR::PredMap PredMap; |
981 |
///\brief The type of the map that stores the distances of the nodes. |
|
982 | 984 |
typedef typename TR::DistMap DistMap; |
983 |
///\brief The type of the map that indicates which nodes are reached. |
|
984 | 985 |
typedef typename TR::ReachedMap ReachedMap; |
985 |
///\brief The type of the map that indicates which nodes are processed. |
|
986 | 986 |
typedef typename TR::ProcessedMap ProcessedMap; |
987 |
///The type of the shortest paths |
|
988 | 987 |
typedef typename TR::Path Path; |
989 | 988 |
|
990 | 989 |
public: |
991 | 990 |
|
992 | 991 |
/// Constructor. |
993 | 992 |
BfsWizard() : TR() {} |
994 | 993 |
|
995 | 994 |
/// Constructor that requires parameters. |
996 | 995 |
|
997 | 996 |
/// Constructor that requires parameters. |
998 | 997 |
/// These parameters will be the default values for the traits class. |
999 | 998 |
/// \param g The digraph the algorithm runs on. |
... | ... |
@@ -1045,96 +1044,101 @@ |
1045 | 1044 |
if (Base::_processed) |
1046 | 1045 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1047 | 1046 |
alg.run(s,t); |
1048 | 1047 |
if (Base::_path) |
1049 | 1048 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
1050 | 1049 |
if (Base::_di) |
1051 | 1050 |
*Base::_di = alg.dist(t); |
1052 | 1051 |
return alg.reached(t); |
1053 | 1052 |
} |
1054 | 1053 |
|
1055 | 1054 |
///Runs BFS algorithm to visit all nodes in the digraph. |
1056 | 1055 |
|
1057 |
///This method runs BFS algorithm in order to compute |
|
1058 |
///the shortest path to each node. |
|
1056 |
///This method runs BFS algorithm in order to visit all nodes |
|
1057 |
///in the digraph. |
|
1059 | 1058 |
void run() |
1060 | 1059 |
{ |
1061 | 1060 |
run(INVALID); |
1062 | 1061 |
} |
1063 | 1062 |
|
1064 | 1063 |
template<class T> |
1065 | 1064 |
struct SetPredMapBase : public Base { |
1066 | 1065 |
typedef T PredMap; |
1067 | 1066 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1068 | 1067 |
SetPredMapBase(const TR &b) : TR(b) {} |
1069 | 1068 |
}; |
1070 |
///\brief \ref named-func-param "Named parameter" |
|
1071 |
///for setting PredMap object. |
|
1069 |
|
|
1070 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
1071 |
///the predecessor map. |
|
1072 | 1072 |
/// |
1073 |
///\ref named-func-param "Named parameter" |
|
1074 |
///for setting PredMap object. |
|
1073 |
///\ref named-templ-param "Named parameter" function for setting |
|
1074 |
///the map that stores the predecessor arcs of the nodes. |
|
1075 | 1075 |
template<class T> |
1076 | 1076 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1077 | 1077 |
{ |
1078 | 1078 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1079 | 1079 |
return BfsWizard<SetPredMapBase<T> >(*this); |
1080 | 1080 |
} |
1081 | 1081 |
|
1082 | 1082 |
template<class T> |
1083 | 1083 |
struct SetReachedMapBase : public Base { |
1084 | 1084 |
typedef T ReachedMap; |
1085 | 1085 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1086 | 1086 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1087 | 1087 |
}; |
1088 |
///\brief \ref named-func-param "Named parameter" |
|
1089 |
///for setting ReachedMap object. |
|
1088 |
|
|
1089 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
1090 |
///the reached map. |
|
1090 | 1091 |
/// |
1091 |
/// \ref named-func-param "Named parameter" |
|
1092 |
///for setting ReachedMap object. |
|
1092 |
///\ref named-templ-param "Named parameter" function for setting |
|
1093 |
///the map that indicates which nodes are reached. |
|
1093 | 1094 |
template<class T> |
1094 | 1095 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1095 | 1096 |
{ |
1096 | 1097 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1097 | 1098 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
1098 | 1099 |
} |
1099 | 1100 |
|
1100 | 1101 |
template<class T> |
1101 | 1102 |
struct SetDistMapBase : public Base { |
1102 | 1103 |
typedef T DistMap; |
1103 | 1104 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1104 | 1105 |
SetDistMapBase(const TR &b) : TR(b) {} |
1105 | 1106 |
}; |
1106 |
///\brief \ref named-func-param "Named parameter" |
|
1107 |
///for setting DistMap object. |
|
1107 |
|
|
1108 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
1109 |
///the distance map. |
|
1108 | 1110 |
/// |
1109 |
/// \ref named-func-param "Named parameter" |
|
1110 |
///for setting DistMap object. |
|
1111 |
///\ref named-templ-param "Named parameter" function for setting |
|
1112 |
///the map that stores the distances of the nodes calculated |
|
1113 |
///by the algorithm. |
|
1111 | 1114 |
template<class T> |
1112 | 1115 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1113 | 1116 |
{ |
1114 | 1117 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1115 | 1118 |
return BfsWizard<SetDistMapBase<T> >(*this); |
1116 | 1119 |
} |
1117 | 1120 |
|
1118 | 1121 |
template<class T> |
1119 | 1122 |
struct SetProcessedMapBase : public Base { |
1120 | 1123 |
typedef T ProcessedMap; |
1121 | 1124 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1122 | 1125 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1123 | 1126 |
}; |
1124 |
///\brief \ref named-func-param "Named parameter" |
|
1125 |
///for setting ProcessedMap object. |
|
1127 |
|
|
1128 |
///\brief \ref named-func-param "Named parameter" for setting |
|
1129 |
///the processed map. |
|
1126 | 1130 |
/// |
1127 |
/// \ref named-func-param "Named parameter" |
|
1128 |
///for setting ProcessedMap object. |
|
1131 |
///\ref named-templ-param "Named parameter" function for setting |
|
1132 |
///the map that indicates which nodes are processed. |
|
1129 | 1133 |
template<class T> |
1130 | 1134 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1131 | 1135 |
{ |
1132 | 1136 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1133 | 1137 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
1134 | 1138 |
} |
1135 | 1139 |
|
1136 | 1140 |
template<class T> |
1137 | 1141 |
struct SetPathBase : public Base { |
1138 | 1142 |
typedef T Path; |
1139 | 1143 |
SetPathBase(const TR &b) : TR(b) {} |
1140 | 1144 |
}; |
... | ... |
@@ -1255,25 +1259,26 @@ |
1255 | 1259 |
/// |
1256 | 1260 |
/// Default traits class of BfsVisit class. |
1257 | 1261 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1258 | 1262 |
template<class GR> |
1259 | 1263 |
struct BfsVisitDefaultTraits { |
1260 | 1264 |
|
1261 | 1265 |
/// \brief The type of the digraph the algorithm runs on. |
1262 | 1266 |
typedef GR Digraph; |
1263 | 1267 |
|
1264 | 1268 |
/// \brief The type of the map that indicates which nodes are reached. |
1265 | 1269 |
/// |
1266 | 1270 |
/// The type of the map that indicates which nodes are reached. |
1267 |
/// It must |
|
1271 |
/// It must conform to |
|
1272 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
1268 | 1273 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1269 | 1274 |
|
1270 | 1275 |
/// \brief Instantiates a ReachedMap. |
1271 | 1276 |
/// |
1272 | 1277 |
/// This function instantiates a ReachedMap. |
1273 | 1278 |
/// \param digraph is the digraph, to which |
1274 | 1279 |
/// we would like to define the ReachedMap. |
1275 | 1280 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1276 | 1281 |
return new ReachedMap(digraph); |
1277 | 1282 |
} |
1278 | 1283 |
|
1279 | 1284 |
}; |
... | ... |
@@ -1293,29 +1298,29 @@ |
1293 | 1298 |
/// when extra actions have to be performed in connection with certain |
1294 | 1299 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
1295 | 1300 |
/// instead. |
1296 | 1301 |
/// |
1297 | 1302 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1298 | 1303 |
/// The default type is \ref ListDigraph. |
1299 | 1304 |
/// The value of GR is not used directly by \ref BfsVisit, |
1300 | 1305 |
/// it is only passed to \ref BfsVisitDefaultTraits. |
1301 | 1306 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1302 | 1307 |
/// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
1303 | 1308 |
/// does not observe the BFS events. If you want to observe the BFS |
1304 | 1309 |
/// events, you should implement your own visitor class. |
1305 |
/// \tparam TR Traits class to set various data types used by the |
|
1306 |
/// algorithm. The default traits class is |
|
1307 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>". |
|
1308 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
|
1309 |
/// |
|
1310 |
/// \tparam TR The traits class that defines various types used by the |
|
1311 |
/// algorithm. By default, it is \ref BfsVisitDefaultTraits |
|
1312 |
/// "BfsVisitDefaultTraits<GR>". |
|
1313 |
/// In most cases, this parameter should not be set directly, |
|
1314 |
/// consider to use the named template parameters instead. |
|
1310 | 1315 |
#ifdef DOXYGEN |
1311 | 1316 |
template <typename GR, typename VS, typename TR> |
1312 | 1317 |
#else |
1313 | 1318 |
template <typename GR = ListDigraph, |
1314 | 1319 |
typename VS = BfsVisitor<GR>, |
1315 | 1320 |
typename TR = BfsVisitDefaultTraits<GR> > |
1316 | 1321 |
#endif |
1317 | 1322 |
class BfsVisit { |
1318 | 1323 |
public: |
1319 | 1324 |
|
1320 | 1325 |
///The traits class. |
1321 | 1326 |
typedef TR Traits; |
... | ... |
@@ -1416,26 +1421,26 @@ |
1416 | 1421 |
delete _reached; |
1417 | 1422 |
local_reached = false; |
1418 | 1423 |
} |
1419 | 1424 |
_reached = &m; |
1420 | 1425 |
return *this; |
1421 | 1426 |
} |
1422 | 1427 |
|
1423 | 1428 |
public: |
1424 | 1429 |
|
1425 | 1430 |
/// \name Execution Control |
1426 | 1431 |
/// The simplest way to execute the BFS algorithm is to use one of the |
1427 | 1432 |
/// member functions called \ref run(Node) "run()".\n |
1428 |
/// If you need more control on the execution, first you have to call |
|
1429 |
/// \ref init(), then you can add several source nodes with |
|
1433 |
/// If you need better control on the execution, you have to call |
|
1434 |
/// \ref init() first, then you can add several source nodes with |
|
1430 | 1435 |
/// \ref addSource(). Finally the actual path computation can be |
1431 | 1436 |
/// performed with one of the \ref start() functions. |
1432 | 1437 |
|
1433 | 1438 |
/// @{ |
1434 | 1439 |
|
1435 | 1440 |
/// \brief Initializes the internal data structures. |
1436 | 1441 |
/// |
1437 | 1442 |
/// Initializes the internal data structures. |
1438 | 1443 |
void init() { |
1439 | 1444 |
create_maps(); |
1440 | 1445 |
_list.resize(countNodes(*_digraph)); |
1441 | 1446 |
_list_front = _list_back = -1; |
... | ... |
@@ -1689,30 +1694,26 @@ |
1689 | 1694 |
/// b.addSource(s); |
1690 | 1695 |
/// b.start(t); |
1691 | 1696 |
///\endcode |
1692 | 1697 |
bool run(Node s,Node t) { |
1693 | 1698 |
init(); |
1694 | 1699 |
addSource(s); |
1695 | 1700 |
start(t); |
1696 | 1701 |
return reached(t); |
1697 | 1702 |
} |
1698 | 1703 |
|
1699 | 1704 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1700 | 1705 |
/// |
1701 |
/// This method runs the %BFS algorithm in order to |
|
1702 |
/// compute the shortest path to each node. |
|
1703 |
/// |
|
1704 |
/// The algorithm computes |
|
1705 |
/// - the shortest path tree (forest), |
|
1706 |
/// - the distance of each node from the root(s). |
|
1706 |
/// This method runs the %BFS algorithm in order to visit all nodes |
|
1707 |
/// in the digraph. |
|
1707 | 1708 |
/// |
1708 | 1709 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1709 | 1710 |
///\code |
1710 | 1711 |
/// b.init(); |
1711 | 1712 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1712 | 1713 |
/// if (!b.reached(n)) { |
1713 | 1714 |
/// b.addSource(n); |
1714 | 1715 |
/// b.start(); |
1715 | 1716 |
/// } |
1716 | 1717 |
/// } |
1717 | 1718 |
///\endcode |
1718 | 1719 |
void run() { |
... | ... |
@@ -1726,25 +1727,25 @@ |
1726 | 1727 |
} |
1727 | 1728 |
|
1728 | 1729 |
///@} |
1729 | 1730 |
|
1730 | 1731 |
/// \name Query Functions |
1731 | 1732 |
/// The results of the BFS algorithm can be obtained using these |
1732 | 1733 |
/// functions.\n |
1733 | 1734 |
/// Either \ref run(Node) "run()" or \ref start() should be called |
1734 | 1735 |
/// before using them. |
1735 | 1736 |
|
1736 | 1737 |
///@{ |
1737 | 1738 |
|
1738 |
/// \brief Checks if |
|
1739 |
/// \brief Checks if the given node is reached from the root(s). |
|
1739 | 1740 |
/// |
1740 | 1741 |
/// Returns \c true if \c v is reached from the root(s). |
1741 | 1742 |
/// |
1742 | 1743 |
/// \pre Either \ref run(Node) "run()" or \ref init() |
1743 | 1744 |
/// must be called before using this function. |
1744 | 1745 |
bool reached(Node v) const { return (*_reached)[v]; } |
1745 | 1746 |
|
1746 | 1747 |
///@} |
1747 | 1748 |
|
1748 | 1749 |
}; |
1749 | 1750 |
|
1750 | 1751 |
} //END OF NAMESPACE LEMON |
... | ... |
@@ -10,338 +10,338 @@ |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BIN_HEAP_H |
20 | 20 |
#define LEMON_BIN_HEAP_H |
21 | 21 |
|
22 |
///\ingroup |
|
22 |
///\ingroup heaps |
|
23 | 23 |
///\file |
24 |
///\brief Binary |
|
24 |
///\brief Binary heap implementation. |
|
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 |
///\ingroup |
|
32 |
/// \ingroup heaps |
|
33 | 33 |
/// |
34 |
///\brief |
|
34 |
/// \brief Binary heap data structure. |
|
35 | 35 |
/// |
36 | 36 |
///This class implements the \e binary \e heap data structure. |
37 |
/// It fully conforms to the \ref concepts::Heap "heap concept". |
|
37 | 38 |
/// |
38 |
///A \e heap is a data structure for storing items with specified values |
|
39 |
///called \e priorities in such a way that finding the item with minimum |
|
40 |
///priority is efficient. \c CMP specifies the ordering of the priorities. |
|
41 |
///In a heap one can change the priority of an item, add or erase an |
|
42 |
///item, etc. |
|
43 |
/// |
|
44 |
///\tparam PR Type of the priority of the items. |
|
45 |
///\tparam IM A read and writable item map with int values, used internally |
|
46 |
///to handle the cross references. |
|
47 |
///\tparam CMP A functor class for the ordering of the priorities. |
|
39 |
/// \tparam PR Type of the priorities of the items. |
|
40 |
/// \tparam IM A read-writable item map with \c int values, used |
|
41 |
/// internally to handle the cross references. |
|
42 |
/// \tparam CMP A functor class for comparing the priorities. |
|
48 | 43 |
///The default is \c std::less<PR>. |
49 |
/// |
|
50 |
///\sa FibHeap |
|
51 |
|
|
44 |
#ifdef DOXYGEN |
|
45 |
template <typename PR, typename IM, typename CMP> |
|
46 |
#else |
|
52 | 47 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
48 |
#endif |
|
53 | 49 |
class BinHeap { |
50 |
public: |
|
54 | 51 |
|
55 |
public: |
|
56 |
///\e |
|
52 |
/// Type of the item-int map. |
|
57 | 53 |
typedef IM ItemIntMap; |
58 |
/// |
|
54 |
/// Type of the priorities. |
|
59 | 55 |
typedef PR Prio; |
60 |
/// |
|
56 |
/// Type of the items stored in the heap. |
|
61 | 57 |
typedef typename ItemIntMap::Key Item; |
62 |
/// |
|
58 |
/// Type of the item-priority pairs. |
|
63 | 59 |
typedef std::pair<Item,Prio> Pair; |
64 |
/// |
|
60 |
/// Functor type for comparing the priorities. |
|
65 | 61 |
typedef CMP Compare; |
66 | 62 |
|
67 |
/// \brief Type to represent the |
|
63 |
/// \brief Type to represent the states of the items. |
|
68 | 64 |
/// |
69 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
70 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
65 |
/// Each item has a state associated to it. It can be "in heap", |
|
66 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
|
71 | 67 |
/// heap's point of view, but may be useful to the user. |
72 | 68 |
/// |
73 | 69 |
/// The item-int map must be initialized in such way that it assigns |
74 | 70 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
75 | 71 |
enum State { |
76 | 72 |
IN_HEAP = 0, ///< = 0. |
77 | 73 |
PRE_HEAP = -1, ///< = -1. |
78 | 74 |
POST_HEAP = -2 ///< = -2. |
79 | 75 |
}; |
80 | 76 |
|
81 | 77 |
private: |
82 | 78 |
std::vector<Pair> _data; |
83 | 79 |
Compare _comp; |
84 | 80 |
ItemIntMap &_iim; |
85 | 81 |
|
86 | 82 |
public: |
87 |
|
|
83 |
|
|
84 |
/// \brief Constructor. |
|
88 | 85 |
/// |
89 |
/// The constructor. |
|
90 |
/// \param map should be given to the constructor, since it is used |
|
91 |
/// internally to handle the cross references. The value of the map |
|
92 |
/// must be \c PRE_HEAP (<tt>-1</tt>) for every item. |
|
86 |
/// Constructor. |
|
87 |
/// \param map A map that assigns \c int values to the items. |
|
88 |
/// It is used internally to handle the cross references. |
|
89 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
93 | 90 |
explicit BinHeap(ItemIntMap &map) : _iim(map) {} |
94 | 91 |
|
95 |
/// \brief |
|
92 |
/// \brief Constructor. |
|
96 | 93 |
/// |
97 |
/// The constructor. |
|
98 |
/// \param map should be given to the constructor, since it is used |
|
99 |
/// internally to handle the cross references. The value of the map |
|
100 |
/// should be PRE_HEAP (-1) for each element. |
|
101 |
/// |
|
102 |
/// \param comp The comparator function object. |
|
94 |
/// Constructor. |
|
95 |
/// \param map A map that assigns \c int values to the items. |
|
96 |
/// It is used internally to handle the cross references. |
|
97 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
98 |
/// \param comp The function object used for comparing the priorities. |
|
103 | 99 |
BinHeap(ItemIntMap &map, const Compare &comp) |
104 | 100 |
: _iim(map), _comp(comp) {} |
105 | 101 |
|
106 | 102 |
|
107 |
/// The number of items stored in the heap. |
|
103 |
/// \brief The number of items stored in the heap. |
|
108 | 104 |
/// |
109 |
/// |
|
105 |
/// This function returns the number of items stored in the heap. |
|
110 | 106 |
int size() const { return _data.size(); } |
111 | 107 |
|
112 |
/// \brief |
|
108 |
/// \brief Check if the heap is empty. |
|
113 | 109 |
/// |
114 |
/// |
|
110 |
/// This function returns \c true if the heap is empty. |
|
115 | 111 |
bool empty() const { return _data.empty(); } |
116 | 112 |
|
117 |
/// \brief Make |
|
113 |
/// \brief Make the heap empty. |
|
118 | 114 |
/// |
119 |
/// Make empty this heap. It does not change the cross reference map. |
|
120 |
/// If you want to reuse what is not surely empty you should first clear |
|
121 |
/// the heap and after that you should set the cross reference map for |
|
122 |
/// each item to \c PRE_HEAP. |
|
115 |
/// This functon makes the heap empty. |
|
116 |
/// It does not change the cross reference map. If you want to reuse |
|
117 |
/// a heap that is not surely empty, you should first clear it and |
|
118 |
/// then you should set the cross reference map to \c PRE_HEAP |
|
119 |
/// for each item. |
|
123 | 120 |
void clear() { |
124 | 121 |
_data.clear(); |
125 | 122 |
} |
126 | 123 |
|
127 | 124 |
private: |
128 | 125 |
static int parent(int i) { return (i-1)/2; } |
129 | 126 |
|
130 |
static int |
|
127 |
static int secondChild(int i) { return 2*i+2; } |
|
131 | 128 |
bool less(const Pair &p1, const Pair &p2) const { |
132 | 129 |
return _comp(p1.second, p2.second); |
133 | 130 |
} |
134 | 131 |
|
135 |
int |
|
132 |
int bubbleUp(int hole, Pair p) { |
|
136 | 133 |
int par = parent(hole); |
137 | 134 |
while( hole>0 && less(p,_data[par]) ) { |
138 | 135 |
move(_data[par],hole); |
139 | 136 |
hole = par; |
140 | 137 |
par = parent(hole); |
141 | 138 |
} |
142 | 139 |
move(p, hole); |
143 | 140 |
return hole; |
144 | 141 |
} |
145 | 142 |
|
146 |
int bubble_down(int hole, Pair p, int length) { |
|
147 |
int child = second_child(hole); |
|
143 |
int bubbleDown(int hole, Pair p, int length) { |
|
144 |
int child = secondChild(hole); |
|
148 | 145 |
while(child < length) { |
149 | 146 |
if( less(_data[child-1], _data[child]) ) { |
150 | 147 |
--child; |
151 | 148 |
} |
152 | 149 |
if( !less(_data[child], p) ) |
153 | 150 |
goto ok; |
154 | 151 |
move(_data[child], hole); |
155 | 152 |
hole = child; |
156 |
child = |
|
153 |
child = secondChild(hole); |
|
157 | 154 |
} |
158 | 155 |
child--; |
159 | 156 |
if( child<length && less(_data[child], p) ) { |
160 | 157 |
move(_data[child], hole); |
161 | 158 |
hole=child; |
162 | 159 |
} |
163 | 160 |
ok: |
164 | 161 |
move(p, hole); |
165 | 162 |
return hole; |
166 | 163 |
} |
167 | 164 |
|
168 | 165 |
void move(const Pair &p, int i) { |
169 | 166 |
_data[i] = p; |
170 | 167 |
_iim.set(p.first, i); |
171 | 168 |
} |
172 | 169 |
|
173 | 170 |
public: |
171 |
|
|
174 | 172 |
/// \brief Insert a pair of item and priority into the heap. |
175 | 173 |
/// |
176 |
/// |
|
174 |
/// This function inserts \c p.first to the heap with priority |
|
175 |
/// \c p.second. |
|
177 | 176 |
/// \param p The pair to insert. |
177 |
/// \pre \c p.first must not be stored in the heap. |
|
178 | 178 |
void push(const Pair &p) { |
179 | 179 |
int n = _data.size(); |
180 | 180 |
_data.resize(n+1); |
181 |
|
|
181 |
bubbleUp(n, p); |
|
182 | 182 |
} |
183 | 183 |
|
184 |
/// \brief Insert an item into the heap with the given |
|
184 |
/// \brief Insert an item into the heap with the given priority. |
|
185 | 185 |
/// |
186 |
/// |
|
186 |
/// This function inserts the given item into the heap with the |
|
187 |
/// given priority. |
|
187 | 188 |
/// \param i The item to insert. |
188 | 189 |
/// \param p The priority of the item. |
190 |
/// \pre \e i must not be stored in the heap. |
|
189 | 191 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
190 | 192 |
|
191 |
/// \brief |
|
193 |
/// \brief Return the item having minimum priority. |
|
192 | 194 |
/// |
193 |
/// This method returns the item with minimum priority relative to \c |
|
194 |
/// Compare. |
|
195 |
/// |
|
195 |
/// This function returns the item having minimum priority. |
|
196 |
/// \pre The heap must be non-empty. |
|
196 | 197 |
Item top() const { |
197 | 198 |
return _data[0].first; |
198 | 199 |
} |
199 | 200 |
|
200 |
/// \brief |
|
201 |
/// \brief The minimum priority. |
|
201 | 202 |
/// |
202 |
/// It returns the minimum priority relative to \c Compare. |
|
203 |
/// \pre The heap must be nonempty. |
|
203 |
/// This function returns the minimum priority. |
|
204 |
/// \pre The heap must be non-empty. |
|
204 | 205 |
Prio prio() const { |
205 | 206 |
return _data[0].second; |
206 | 207 |
} |
207 | 208 |
|
208 |
/// \brief |
|
209 |
/// \brief Remove the item having minimum priority. |
|
209 | 210 |
/// |
210 |
/// This method deletes the item with minimum priority relative to \c |
|
211 |
/// Compare from the heap. |
|
211 |
/// This function removes the item having minimum priority. |
|
212 | 212 |
/// \pre The heap must be non-empty. |
213 | 213 |
void pop() { |
214 | 214 |
int n = _data.size()-1; |
215 | 215 |
_iim.set(_data[0].first, POST_HEAP); |
216 | 216 |
if (n > 0) { |
217 |
|
|
217 |
bubbleDown(0, _data[n], n); |
|
218 | 218 |
} |
219 | 219 |
_data.pop_back(); |
220 | 220 |
} |
221 | 221 |
|
222 |
/// \brief |
|
222 |
/// \brief Remove the given item from the heap. |
|
223 | 223 |
/// |
224 |
/// This method deletes item \c i from the heap. |
|
225 |
/// \param i The item to erase. |
|
226 |
/// |
|
224 |
/// This function removes the given item from the heap if it is |
|
225 |
/// already stored. |
|
226 |
/// \param i The item to delete. |
|
227 |
/// \pre \e i must be in the heap. |
|
227 | 228 |
void erase(const Item &i) { |
228 | 229 |
int h = _iim[i]; |
229 | 230 |
int n = _data.size()-1; |
230 | 231 |
_iim.set(_data[h].first, POST_HEAP); |
231 | 232 |
if( h < n ) { |
232 |
if ( bubble_up(h, _data[n]) == h) { |
|
233 |
bubble_down(h, _data[n], n); |
|
233 |
if ( bubbleUp(h, _data[n]) == h) { |
|
234 |
bubbleDown(h, _data[n], n); |
|
234 | 235 |
} |
235 | 236 |
} |
236 | 237 |
_data.pop_back(); |
237 | 238 |
} |
238 | 239 |
|
239 |
|
|
240 |
/// \brief Returns the priority of \c i. |
|
240 |
/// \brief The priority of the given item. |
|
241 | 241 |
/// |
242 |
/// This function returns the priority of |
|
242 |
/// This function returns the priority of the given item. |
|
243 | 243 |
/// \param i The item. |
244 |
/// \pre \ |
|
244 |
/// \pre \e i must be in the heap. |
|
245 | 245 |
Prio operator[](const Item &i) const { |
246 | 246 |
int idx = _iim[i]; |
247 | 247 |
return _data[idx].second; |
248 | 248 |
} |
249 | 249 |
|
250 |
/// \brief \c i gets to the heap with priority \c p independently |
|
251 |
/// if \c i was already there. |
|
250 |
/// \brief Set the priority of an item or insert it, if it is |
|
251 |
/// not stored in the heap. |
|
252 | 252 |
/// |
253 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
|
254 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
|
253 |
/// This method sets the priority of the given item if it is |
|
254 |
/// already stored in the heap. Otherwise it inserts the given |
|
255 |
/// item into the heap with the given priority. |
|
255 | 256 |
/// \param i The item. |
256 | 257 |
/// \param p The priority. |
257 | 258 |
void set(const Item &i, const Prio &p) { |
258 | 259 |
int idx = _iim[i]; |
259 | 260 |
if( idx < 0 ) { |
260 | 261 |
push(i,p); |
261 | 262 |
} |
262 | 263 |
else if( _comp(p, _data[idx].second) ) { |
263 |
|
|
264 |
bubbleUp(idx, Pair(i,p)); |
|
264 | 265 |
} |
265 | 266 |
else { |
266 |
|
|
267 |
bubbleDown(idx, Pair(i,p), _data.size()); |
|
267 | 268 |
} |
268 | 269 |
} |
269 | 270 |
|
270 |
/// \brief |
|
271 |
/// \brief Decrease the priority of an item to the given value. |
|
271 | 272 |
/// |
272 |
/// This |
|
273 |
/// This function decreases the priority of an item to the given value. |
|
273 | 274 |
/// \param i The item. |
274 | 275 |
/// \param p The priority. |
275 |
/// \pre \c i must be stored in the heap with priority at least \c |
|
276 |
/// p relative to \c Compare. |
|
276 |
/// \pre \e i must be stored in the heap with priority at least \e p. |
|
277 | 277 |
void decrease(const Item &i, const Prio &p) { |
278 | 278 |
int idx = _iim[i]; |
279 |
|
|
279 |
bubbleUp(idx, Pair(i,p)); |
|
280 | 280 |
} |
281 | 281 |
|
282 |
/// \brief |
|
282 |
/// \brief Increase the priority of an item to the given value. |
|
283 | 283 |
/// |
284 |
/// This |
|
284 |
/// This function increases the priority of an item to the given value. |
|
285 | 285 |
/// \param i The item. |
286 | 286 |
/// \param p The priority. |
287 |
/// \pre \c i must be stored in the heap with priority at most \c |
|
288 |
/// p relative to \c Compare. |
|
287 |
/// \pre \e i must be stored in the heap with priority at most \e p. |
|
289 | 288 |
void increase(const Item &i, const Prio &p) { |
290 | 289 |
int idx = _iim[i]; |
291 |
|
|
290 |
bubbleDown(idx, Pair(i,p), _data.size()); |
|
292 | 291 |
} |
293 | 292 |
|
294 |
/// \brief Returns if \c item is in, has already been in, or has |
|
295 |
/// never been in the heap. |
|
293 |
/// \brief Return the state of an item. |
|
296 | 294 |
/// |
297 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
298 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
299 |
/// otherwise. In the latter case it is possible that \c item will |
|
300 |
/// get back to the heap again. |
|
295 |
/// This method returns \c PRE_HEAP if the given item has never |
|
296 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
|
297 |
/// and \c POST_HEAP otherwise. |
|
298 |
/// In the latter case it is possible that the item will get back |
|
299 |
/// to the heap again. |
|
301 | 300 |
/// \param i The item. |
302 | 301 |
State state(const Item &i) const { |
303 | 302 |
int s = _iim[i]; |
304 | 303 |
if( s>=0 ) |
305 | 304 |
s=0; |
306 | 305 |
return State(s); |
307 | 306 |
} |
308 | 307 |
|
309 |
/// \brief |
|
308 |
/// \brief Set the state of an item in the heap. |
|
310 | 309 |
/// |
311 |
/// Sets the state of the \c item in the heap. It can be used to |
|
312 |
/// manually clear the heap when it is important to achive the |
|
313 |
/// |
|
310 |
/// This function sets the state of the given item in the heap. |
|
311 |
/// It can be used to manually clear the heap when it is important |
|
312 |
/// to achive better time complexity. |
|
314 | 313 |
/// \param i The item. |
315 | 314 |
/// \param st The state. It should not be \c IN_HEAP. |
316 | 315 |
void state(const Item& i, State st) { |
317 | 316 |
switch (st) { |
318 | 317 |
case POST_HEAP: |
319 | 318 |
case PRE_HEAP: |
320 | 319 |
if (state(i) == IN_HEAP) { |
321 | 320 |
erase(i); |
322 | 321 |
} |
323 | 322 |
_iim[i] = st; |
324 | 323 |
break; |
325 | 324 |
case IN_HEAP: |
326 | 325 |
break; |
327 | 326 |
} |
328 | 327 |
} |
329 | 328 |
|
330 |
/// \brief |
|
329 |
/// \brief Replace an item in the heap. |
|
331 | 330 |
/// |
332 |
/// The \c i item is replaced with \c j item. The \c i item should |
|
333 |
/// be in the heap, while the \c j should be out of the heap. The |
|
334 |
/// \c i item will out of the heap and \c j will be in the heap |
|
335 |
/// with the same prioriority as prevoiusly the \c i item. |
|
331 |
/// This function replaces item \c i with item \c j. |
|
332 |
/// Item \c i must be in the heap, while \c j must be out of the heap. |
|
333 |
/// After calling this method, item \c i will be out of the |
|
334 |
/// heap and \c j will be in the heap with the same prioriority |
|
335 |
/// as item \c i had before. |
|
336 | 336 |
void replace(const Item& i, const Item& j) { |
337 | 337 |
int idx = _iim[i]; |
338 | 338 |
_iim.set(i, _iim[j]); |
339 | 339 |
_iim.set(j, idx); |
340 | 340 |
_data[idx].first = j; |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
}; // class BinHeap |
344 | 344 |
|
345 | 345 |
} // namespace lemon |
346 | 346 |
|
347 | 347 |
#endif // LEMON_BIN_HEAP_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
... | ... |
@@ -47,29 +47,29 @@ |
47 | 47 |
|
48 | 48 |
typedef typename Parent::Node Node; |
49 | 49 |
typedef typename Parent::Arc Arc; |
50 | 50 |
|
51 | 51 |
int maxId(Node) const { |
52 | 52 |
return Parent::maxNodeId(); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
int maxId(Arc) const { |
56 | 56 |
return Parent::maxArcId(); |
57 | 57 |
} |
58 | 58 |
|
59 |
Node fromId(int id, Node) |
|
59 |
static Node fromId(int id, Node) { |
|
60 | 60 |
return Parent::nodeFromId(id); |
61 | 61 |
} |
62 | 62 |
|
63 |
Arc fromId(int id, Arc) |
|
63 |
static Arc fromId(int id, Arc) { |
|
64 | 64 |
return Parent::arcFromId(id); |
65 | 65 |
} |
66 | 66 |
|
67 | 67 |
Node oppositeNode(const Node &node, const Arc &arc) const { |
68 | 68 |
if (node == Parent::source(arc)) |
69 | 69 |
return Parent::target(arc); |
70 | 70 |
else if(node == Parent::target(arc)) |
71 | 71 |
return Parent::source(arc); |
72 | 72 |
else |
73 | 73 |
return INVALID; |
74 | 74 |
} |
75 | 75 |
|
... | ... |
@@ -346,33 +346,33 @@ |
346 | 346 |
int maxId(Node) const { |
347 | 347 |
return Parent::maxNodeId(); |
348 | 348 |
} |
349 | 349 |
|
350 | 350 |
int maxId(Arc) const { |
351 | 351 |
return Parent::maxArcId(); |
352 | 352 |
} |
353 | 353 |
|
354 | 354 |
int maxId(Edge) const { |
355 | 355 |
return Parent::maxEdgeId(); |
356 | 356 |
} |
357 | 357 |
|
358 |
Node fromId(int id, Node) |
|
358 |
static Node fromId(int id, Node) { |
|
359 | 359 |
return Parent::nodeFromId(id); |
360 | 360 |
} |
361 | 361 |
|
362 |
Arc fromId(int id, Arc) |
|
362 |
static Arc fromId(int id, Arc) { |
|
363 | 363 |
return Parent::arcFromId(id); |
364 | 364 |
} |
365 | 365 |
|
366 |
Edge fromId(int id, Edge) |
|
366 |
static Edge fromId(int id, Edge) { |
|
367 | 367 |
return Parent::edgeFromId(id); |
368 | 368 |
} |
369 | 369 |
|
370 | 370 |
Node oppositeNode(const Node &n, const Edge &e) const { |
371 | 371 |
if( n == Parent::u(e)) |
372 | 372 |
return Parent::v(e); |
373 | 373 |
else if( n == Parent::v(e)) |
374 | 374 |
return Parent::u(e); |
375 | 375 |
else |
376 | 376 |
return INVALID; |
377 | 377 |
} |
378 | 378 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BUCKET_HEAP_H |
20 | 20 |
#define LEMON_BUCKET_HEAP_H |
21 | 21 |
|
22 |
///\ingroup |
|
22 |
///\ingroup heaps |
|
23 | 23 |
///\file |
24 |
///\brief Bucket |
|
24 |
///\brief Bucket heap implementation. |
|
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
namespace _bucket_heap_bits { |
33 | 33 |
|
34 | 34 |
template <bool MIN> |
35 | 35 |
struct DirectionTraits { |
36 | 36 |
static bool less(int left, int right) { |
... | ... |
@@ -44,106 +44,114 @@ |
44 | 44 |
template <> |
45 | 45 |
struct DirectionTraits<false> { |
46 | 46 |
static bool less(int left, int right) { |
47 | 47 |
return left > right; |
48 | 48 |
} |
49 | 49 |
static void increase(int& value) { |
50 | 50 |
--value; |
51 | 51 |
} |
52 | 52 |
}; |
53 | 53 |
|
54 | 54 |
} |
55 | 55 |
|
56 |
/// \ingroup |
|
56 |
/// \ingroup heaps |
|
57 | 57 |
/// |
58 |
/// \brief |
|
58 |
/// \brief Bucket heap data structure. |
|
59 | 59 |
/// |
60 |
/// This class implements the \e bucket \e heap data structure. A \e heap |
|
61 |
/// is a data structure for storing items with specified values called \e |
|
62 |
/// priorities in such a way that finding the item with minimum priority is |
|
63 |
/// efficient. The bucket heap is very simple implementation, it can store |
|
64 |
/// only integer priorities and it stores for each priority in the |
|
65 |
/// \f$ [0..C) \f$ range a list of items. So it should be used only when |
|
66 |
/// the |
|
60 |
/// This class implements the \e bucket \e heap data structure. |
|
61 |
/// It practically conforms to the \ref concepts::Heap "heap concept", |
|
62 |
/// but it has some limitations. |
|
67 | 63 |
/// |
68 |
/// \param IM A read and write Item int map, used internally |
|
69 |
/// to handle the cross references. |
|
70 |
/// \param MIN If the given parameter is false then instead of the |
|
71 |
/// minimum value the maximum can be retrivied with the top() and |
|
72 |
/// |
|
64 |
/// The bucket heap is a very simple structure. It can store only |
|
65 |
/// \c int priorities and it maintains a list of items for each priority |
|
66 |
/// in the range <tt>[0..C)</tt>. So it should only be used when the |
|
67 |
/// priorities are small. It is not intended to use as a Dijkstra heap. |
|
68 |
/// |
|
69 |
/// \tparam IM A read-writable item map with \c int values, used |
|
70 |
/// internally to handle the cross references. |
|
71 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
|
72 |
/// The default is \e min-heap. If this parameter is set to \c false, |
|
73 |
/// then the comparison is reversed, so the top(), prio() and pop() |
|
74 |
/// functions deal with the item having maximum priority instead of the |
|
75 |
/// minimum. |
|
76 |
/// |
|
77 |
/// \sa SimpleBucketHeap |
|
73 | 78 |
template <typename IM, bool MIN = true> |
74 | 79 |
class BucketHeap { |
75 | 80 |
|
76 | 81 |
public: |
77 |
/// \e |
|
78 |
typedef typename IM::Key Item; |
|
79 |
|
|
82 |
|
|
83 |
/// Type of the item-int map. |
|
84 |
typedef IM ItemIntMap; |
|
85 |
/// Type of the priorities. |
|
80 | 86 |
typedef int Prio; |
81 |
/// |
|
87 |
/// Type of the items stored in the heap. |
|
88 |
typedef typename ItemIntMap::Key Item; |
|
89 |
/// Type of the item-priority pairs. |
|
82 | 90 |
typedef std::pair<Item, Prio> Pair; |
83 |
/// \e |
|
84 |
typedef IM ItemIntMap; |
|
85 | 91 |
|
86 | 92 |
private: |
87 | 93 |
|
88 | 94 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
89 | 95 |
|
90 | 96 |
public: |
91 | 97 |
|
92 |
/// \brief Type to represent the |
|
98 |
/// \brief Type to represent the states of the items. |
|
93 | 99 |
/// |
94 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
95 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
100 |
/// Each item has a state associated to it. It can be "in heap", |
|
101 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
|
96 | 102 |
/// heap's point of view, but may be useful to the user. |
97 | 103 |
/// |
98 | 104 |
/// The item-int map must be initialized in such way that it assigns |
99 | 105 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
100 | 106 |
enum State { |
101 | 107 |
IN_HEAP = 0, ///< = 0. |
102 | 108 |
PRE_HEAP = -1, ///< = -1. |
103 | 109 |
POST_HEAP = -2 ///< = -2. |
104 | 110 |
}; |
105 | 111 |
|
106 | 112 |
public: |
107 |
|
|
113 |
|
|
114 |
/// \brief Constructor. |
|
108 | 115 |
/// |
109 |
/// The constructor. |
|
110 |
/// \param map should be given to the constructor, since it is used |
|
111 |
/// internally to handle the cross references. The value of the map |
|
112 |
/// should be PRE_HEAP (-1) for each element. |
|
116 |
/// Constructor. |
|
117 |
/// \param map A map that assigns \c int values to the items. |
|
118 |
/// It is used internally to handle the cross references. |
|
119 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
113 | 120 |
explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} |
114 | 121 |
|
115 |
/// The number of items stored in the heap. |
|
122 |
/// \brief The number of items stored in the heap. |
|
116 | 123 |
/// |
117 |
/// |
|
124 |
/// This function returns the number of items stored in the heap. |
|
118 | 125 |
int size() const { return _data.size(); } |
119 | 126 |
|
120 |
/// \brief |
|
127 |
/// \brief Check if the heap is empty. |
|
121 | 128 |
/// |
122 |
/// |
|
129 |
/// This function returns \c true if the heap is empty. |
|
123 | 130 |
bool empty() const { return _data.empty(); } |
124 | 131 |
|
125 |
/// \brief Make |
|
132 |
/// \brief Make the heap empty. |
|
126 | 133 |
/// |
127 |
/// Make empty this heap. It does not change the cross reference |
|
128 |
/// map. If you want to reuse a heap what is not surely empty you |
|
129 |
/// should first clear the heap and after that you should set the |
|
130 |
/// cross reference map for each item to \c PRE_HEAP. |
|
134 |
/// This functon makes the heap empty. |
|
135 |
/// It does not change the cross reference map. If you want to reuse |
|
136 |
/// a heap that is not surely empty, you should first clear it and |
|
137 |
/// then you should set the cross reference map to \c PRE_HEAP |
|
138 |
/// for each item. |
|
131 | 139 |
void clear() { |
132 | 140 |
_data.clear(); _first.clear(); _minimum = 0; |
133 | 141 |
} |
134 | 142 |
|
135 | 143 |
private: |
136 | 144 |
|
137 |
void |
|
145 |
void relocateLast(int idx) { |
|
138 | 146 |
if (idx + 1 < int(_data.size())) { |
139 | 147 |
_data[idx] = _data.back(); |
140 | 148 |
if (_data[idx].prev != -1) { |
141 | 149 |
_data[_data[idx].prev].next = idx; |
142 | 150 |
} else { |
143 | 151 |
_first[_data[idx].value] = idx; |
144 | 152 |
} |
145 | 153 |
if (_data[idx].next != -1) { |
146 | 154 |
_data[_data[idx].next].prev = idx; |
147 | 155 |
} |
148 | 156 |
_iim[_data[idx].item] = idx; |
149 | 157 |
} |
... | ... |
@@ -165,174 +173,178 @@ |
165 | 173 |
if (int(_first.size()) <= _data[idx].value) { |
166 | 174 |
_first.resize(_data[idx].value + 1, -1); |
167 | 175 |
} |
168 | 176 |
_data[idx].next = _first[_data[idx].value]; |
169 | 177 |
if (_data[idx].next != -1) { |
170 | 178 |
_data[_data[idx].next].prev = idx; |
171 | 179 |
} |
172 | 180 |
_first[_data[idx].value] = idx; |
173 | 181 |
_data[idx].prev = -1; |
174 | 182 |
} |
175 | 183 |
|
176 | 184 |
public: |
185 |
|
|
177 | 186 |
/// \brief Insert a pair of item and priority into the heap. |
178 | 187 |
/// |
179 |
/// |
|
188 |
/// This function inserts \c p.first to the heap with priority |
|
189 |
/// \c p.second. |
|
180 | 190 |
/// \param p The pair to insert. |
191 |
/// \pre \c p.first must not be stored in the heap. |
|
181 | 192 |
void push(const Pair& p) { |
182 | 193 |
push(p.first, p.second); |
183 | 194 |
} |
184 | 195 |
|
185 | 196 |
/// \brief Insert an item into the heap with the given priority. |
186 | 197 |
/// |
187 |
/// |
|
198 |
/// This function inserts the given item into the heap with the |
|
199 |
/// given priority. |
|
188 | 200 |
/// \param i The item to insert. |
189 | 201 |
/// \param p The priority of the item. |
202 |
/// \pre \e i must not be stored in the heap. |
|
190 | 203 |
void push(const Item &i, const Prio &p) { |
191 | 204 |
int idx = _data.size(); |
192 | 205 |
_iim[i] = idx; |
193 | 206 |
_data.push_back(BucketItem(i, p)); |
194 | 207 |
lace(idx); |
195 | 208 |
if (Direction::less(p, _minimum)) { |
196 | 209 |
_minimum = p; |
197 | 210 |
} |
198 | 211 |
} |
199 | 212 |
|
200 |
/// \brief |
|
213 |
/// \brief Return the item having minimum priority. |
|
201 | 214 |
/// |
202 |
/// This method returns the item with minimum priority. |
|
203 |
/// \pre The heap must be nonempty. |
|
215 |
/// This function returns the item having minimum priority. |
|
216 |
/// \pre The heap must be non-empty. |
|
204 | 217 |
Item top() const { |
205 | 218 |
while (_first[_minimum] == -1) { |
206 | 219 |
Direction::increase(_minimum); |
207 | 220 |
} |
208 | 221 |
return _data[_first[_minimum]].item; |
209 | 222 |
} |
210 | 223 |
|
211 |
/// \brief |
|
224 |
/// \brief The minimum priority. |
|
212 | 225 |
/// |
213 |
/// It returns the minimum priority. |
|
214 |
/// \pre The heap must be nonempty. |
|
226 |
/// This function returns the minimum priority. |
|
227 |
/// \pre The heap must be non-empty. |
|
215 | 228 |
Prio prio() const { |
216 | 229 |
while (_first[_minimum] == -1) { |
217 | 230 |
Direction::increase(_minimum); |
218 | 231 |
} |
219 | 232 |
return _minimum; |
220 | 233 |
} |
221 | 234 |
|
222 |
/// \brief |
|
235 |
/// \brief Remove the item having minimum priority. |
|
223 | 236 |
/// |
224 |
/// This |
|
237 |
/// This function removes the item having minimum priority. |
|
225 | 238 |
/// \pre The heap must be non-empty. |
226 | 239 |
void pop() { |
227 | 240 |
while (_first[_minimum] == -1) { |
228 | 241 |
Direction::increase(_minimum); |
229 | 242 |
} |
230 | 243 |
int idx = _first[_minimum]; |
231 | 244 |
_iim[_data[idx].item] = -2; |
232 | 245 |
unlace(idx); |
233 |
|
|
246 |
relocateLast(idx); |
|
234 | 247 |
} |
235 | 248 |
|
236 |
/// \brief |
|
249 |
/// \brief Remove the given item from the heap. |
|
237 | 250 |
/// |
238 |
/// This method deletes item \c i from the heap, if \c i was |
|
239 |
/// already stored in the heap. |
|
240 |
/// |
|
251 |
/// This function removes the given item from the heap if it is |
|
252 |
/// already stored. |
|
253 |
/// \param i The item to delete. |
|
254 |
/// \pre \e i must be in the heap. |
|
241 | 255 |
void erase(const Item &i) { |
242 | 256 |
int idx = _iim[i]; |
243 | 257 |
_iim[_data[idx].item] = -2; |
244 | 258 |
unlace(idx); |
245 |
|
|
259 |
relocateLast(idx); |
|
246 | 260 |
} |
247 | 261 |
|
248 |
|
|
249 |
/// \brief Returns the priority of \c i. |
|
262 |
/// \brief The priority of the given item. |
|
250 | 263 |
/// |
251 |
/// This function returns the priority of item \c i. |
|
252 |
/// \pre \c i must be in the heap. |
|
264 |
/// This function returns the priority of the given item. |
|
253 | 265 |
/// \param i The item. |
266 |
/// \pre \e i must be in the heap. |
|
254 | 267 |
Prio operator[](const Item &i) const { |
255 | 268 |
int idx = _iim[i]; |
256 | 269 |
return _data[idx].value; |
257 | 270 |
} |
258 | 271 |
|
259 |
/// \brief \c i gets to the heap with priority \c p independently |
|
260 |
/// if \c i was already there. |
|
272 |
/// \brief Set the priority of an item or insert it, if it is |
|
273 |
/// not stored in the heap. |
|
261 | 274 |
/// |
262 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
|
263 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
|
275 |
/// This method sets the priority of the given item if it is |
|
276 |
/// already stored in the heap. Otherwise it inserts the given |
|
277 |
/// item into the heap with the given priority. |
|
264 | 278 |
/// \param i The item. |
265 | 279 |
/// \param p The priority. |
266 | 280 |
void set(const Item &i, const Prio &p) { |
267 | 281 |
int idx = _iim[i]; |
268 | 282 |
if (idx < 0) { |
269 | 283 |
push(i, p); |
270 | 284 |
} else if (Direction::less(p, _data[idx].value)) { |
271 | 285 |
decrease(i, p); |
272 | 286 |
} else { |
273 | 287 |
increase(i, p); |
274 | 288 |
} |
275 | 289 |
} |
276 | 290 |
|
277 |
/// \brief |
|
291 |
/// \brief Decrease the priority of an item to the given value. |
|
278 | 292 |
/// |
279 |
/// This method decreases the priority of item \c i to \c p. |
|
280 |
/// \pre \c i must be stored in the heap with priority at least \c |
|
281 |
/// |
|
293 |
/// This function decreases the priority of an item to the given value. |
|
282 | 294 |
/// \param i The item. |
283 | 295 |
/// \param p The priority. |
296 |
/// \pre \e i must be stored in the heap with priority at least \e p. |
|
284 | 297 |
void decrease(const Item &i, const Prio &p) { |
285 | 298 |
int idx = _iim[i]; |
286 | 299 |
unlace(idx); |
287 | 300 |
_data[idx].value = p; |
288 | 301 |
if (Direction::less(p, _minimum)) { |
289 | 302 |
_minimum = p; |
290 | 303 |
} |
291 | 304 |
lace(idx); |
292 | 305 |
} |
293 | 306 |
|
294 |
/// \brief |
|
307 |
/// \brief Increase the priority of an item to the given value. |
|
295 | 308 |
/// |
296 |
/// This method sets the priority of item \c i to \c p. |
|
297 |
/// \pre \c i must be stored in the heap with priority at most \c |
|
298 |
/// |
|
309 |
/// This function increases the priority of an item to the given value. |
|
299 | 310 |
/// \param i The item. |
300 | 311 |
/// \param p The priority. |
312 |
/// \pre \e i must be stored in the heap with priority at most \e p. |
|
301 | 313 |
void increase(const Item &i, const Prio &p) { |
302 | 314 |
int idx = _iim[i]; |
303 | 315 |
unlace(idx); |
304 | 316 |
_data[idx].value = p; |
305 | 317 |
lace(idx); |
306 | 318 |
} |
307 | 319 |
|
308 |
/// \brief Returns if \c item is in, has already been in, or has |
|
309 |
/// never been in the heap. |
|
320 |
/// \brief Return the state of an item. |
|
310 | 321 |
/// |
311 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
312 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
313 |
/// otherwise. In the latter case it is possible that \c item will |
|
314 |
/// get back to the heap again. |
|
322 |
/// This method returns \c PRE_HEAP if the given item has never |
|
323 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
|
324 |
/// and \c POST_HEAP otherwise. |
|
325 |
/// In the latter case it is possible that the item will get back |
|
326 |
/// to the heap again. |
|
315 | 327 |
/// \param i The item. |
316 | 328 |
State state(const Item &i) const { |
317 | 329 |
int idx = _iim[i]; |
318 | 330 |
if (idx >= 0) idx = 0; |
319 | 331 |
return State(idx); |
320 | 332 |
} |
321 | 333 |
|
322 |
/// \brief |
|
334 |
/// \brief Set the state of an item in the heap. |
|
323 | 335 |
/// |
324 |
/// Sets the state of the \c item in the heap. It can be used to |
|
325 |
/// manually clear the heap when it is important to achive the |
|
326 |
/// |
|
336 |
/// This function sets the state of the given item in the heap. |
|
337 |
/// It can be used to manually clear the heap when it is important |
|
338 |
/// to achive better time complexity. |
|
327 | 339 |
/// \param i The item. |
328 | 340 |
/// \param st The state. It should not be \c IN_HEAP. |
329 | 341 |
void state(const Item& i, State st) { |
330 | 342 |
switch (st) { |
331 | 343 |
case POST_HEAP: |
332 | 344 |
case PRE_HEAP: |
333 | 345 |
if (state(i) == IN_HEAP) { |
334 | 346 |
erase(i); |
335 | 347 |
} |
336 | 348 |
_iim[i] = st; |
337 | 349 |
break; |
338 | 350 |
case IN_HEAP: |
... | ... |
@@ -350,202 +362,217 @@ |
350 | 362 |
int value; |
351 | 363 |
|
352 | 364 |
int prev, next; |
353 | 365 |
}; |
354 | 366 |
|
355 | 367 |
ItemIntMap& _iim; |
356 | 368 |
std::vector<int> _first; |
357 | 369 |
std::vector<BucketItem> _data; |
358 | 370 |
mutable int _minimum; |
359 | 371 |
|
360 | 372 |
}; // class BucketHeap |
361 | 373 |
|
362 |
/// \ingroup |
|
374 |
/// \ingroup heaps |
|
363 | 375 |
/// |
364 |
/// \brief |
|
376 |
/// \brief Simplified bucket heap data structure. |
|
365 | 377 |
/// |
366 | 378 |
/// This class implements a simplified \e bucket \e heap data |
367 |
/// structure. It does not provide some functionality but it faster |
|
368 |
/// and simplier data structure than the BucketHeap. The main |
|
369 |
/// difference is that the BucketHeap stores for every key a double |
|
370 |
/// linked list while this class stores just simple lists. In the |
|
371 |
/// other way it does not support erasing each elements just the |
|
372 |
/// minimal and it does not supports key increasing, decreasing. |
|
379 |
/// structure. It does not provide some functionality, but it is |
|
380 |
/// faster and simpler than BucketHeap. The main difference is |
|
381 |
/// that BucketHeap stores a doubly-linked list for each key while |
|
382 |
/// this class stores only simply-linked lists. It supports erasing |
|
383 |
/// only for the item having minimum priority and it does not support |
|
384 |
/// key increasing and decreasing. |
|
373 | 385 |
/// |
374 |
/// \param IM A read and write Item int map, used internally |
|
375 |
/// to handle the cross references. |
|
376 |
/// \param MIN If the given parameter is false then instead of the |
|
377 |
/// minimum value the maximum can be retrivied with the top() and |
|
378 |
/// |
|
386 |
/// Note that this implementation does not conform to the |
|
387 |
/// \ref concepts::Heap "heap concept" due to the lack of some |
|
388 |
/// functionality. |
|
389 |
/// |
|
390 |
/// \tparam IM A read-writable item map with \c int values, used |
|
391 |
/// internally to handle the cross references. |
|
392 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
|
393 |
/// The default is \e min-heap. If this parameter is set to \c false, |
|
394 |
/// then the comparison is reversed, so the top(), prio() and pop() |
|
395 |
/// functions deal with the item having maximum priority instead of the |
|
396 |
/// minimum. |
|
379 | 397 |
/// |
380 | 398 |
/// \sa BucketHeap |
381 | 399 |
template <typename IM, bool MIN = true > |
382 | 400 |
class SimpleBucketHeap { |
383 | 401 |
|
384 | 402 |
public: |
385 |
|
|
403 |
|
|
404 |
/// Type of the item-int map. |
|
405 |
typedef IM ItemIntMap; |
|
406 |
/// Type of the priorities. |
|
386 | 407 |
typedef int Prio; |
408 |
/// Type of the items stored in the heap. |
|
409 |
typedef typename ItemIntMap::Key Item; |
|
410 |
/// Type of the item-priority pairs. |
|
387 | 411 |
typedef std::pair<Item, Prio> Pair; |
388 |
typedef IM ItemIntMap; |
|
389 | 412 |
|
390 | 413 |
private: |
391 | 414 |
|
392 | 415 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
393 | 416 |
|
394 | 417 |
public: |
395 | 418 |
|
396 |
/// \brief Type to represent the |
|
419 |
/// \brief Type to represent the states of the items. |
|
397 | 420 |
/// |
398 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
399 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
421 |
/// Each item has a state associated to it. It can be "in heap", |
|
422 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
|
400 | 423 |
/// heap's point of view, but may be useful to the user. |
401 | 424 |
/// |
402 | 425 |
/// The item-int map must be initialized in such way that it assigns |
403 | 426 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
404 | 427 |
enum State { |
405 | 428 |
IN_HEAP = 0, ///< = 0. |
406 | 429 |
PRE_HEAP = -1, ///< = -1. |
407 | 430 |
POST_HEAP = -2 ///< = -2. |
408 | 431 |
}; |
409 | 432 |
|
410 | 433 |
public: |
411 | 434 |
|
412 |
/// \brief |
|
435 |
/// \brief Constructor. |
|
413 | 436 |
/// |
414 |
/// The constructor. |
|
415 |
/// \param map should be given to the constructor, since it is used |
|
416 |
/// internally to handle the cross references. The value of the map |
|
417 |
/// should be PRE_HEAP (-1) for each element. |
|
437 |
/// Constructor. |
|
438 |
/// \param map A map that assigns \c int values to the items. |
|
439 |
/// It is used internally to handle the cross references. |
|
440 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
418 | 441 |
explicit SimpleBucketHeap(ItemIntMap &map) |
419 | 442 |
: _iim(map), _free(-1), _num(0), _minimum(0) {} |
420 | 443 |
|
421 |
/// \brief |
|
444 |
/// \brief The number of items stored in the heap. |
|
422 | 445 |
/// |
423 |
/// |
|
446 |
/// This function returns the number of items stored in the heap. |
|
424 | 447 |
int size() const { return _num; } |
425 | 448 |
|
426 |
/// \brief |
|
449 |
/// \brief Check if the heap is empty. |
|
427 | 450 |
/// |
428 |
/// |
|
451 |
/// This function returns \c true if the heap is empty. |
|
429 | 452 |
bool empty() const { return _num == 0; } |
430 | 453 |
|
431 |
/// \brief Make |
|
454 |
/// \brief Make the heap empty. |
|
432 | 455 |
/// |
433 |
/// Make empty this heap. It does not change the cross reference |
|
434 |
/// map. If you want to reuse a heap what is not surely empty you |
|
435 |
/// should first clear the heap and after that you should set the |
|
436 |
/// cross reference map for each item to \c PRE_HEAP. |
|
456 |
/// This functon makes the heap empty. |
|
457 |
/// It does not change the cross reference map. If you want to reuse |
|
458 |
/// a heap that is not surely empty, you should first clear it and |
|
459 |
/// then you should set the cross reference map to \c PRE_HEAP |
|
460 |
/// for each item. |
|
437 | 461 |
void clear() { |
438 | 462 |
_data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0; |
439 | 463 |
} |
440 | 464 |
|
441 | 465 |
/// \brief Insert a pair of item and priority into the heap. |
442 | 466 |
/// |
443 |
/// |
|
467 |
/// This function inserts \c p.first to the heap with priority |
|
468 |
/// \c p.second. |
|
444 | 469 |
/// \param p The pair to insert. |
470 |
/// \pre \c p.first must not be stored in the heap. |
|
445 | 471 |
void push(const Pair& p) { |
446 | 472 |
push(p.first, p.second); |
447 | 473 |
} |
448 | 474 |
|
449 | 475 |
/// \brief Insert an item into the heap with the given priority. |
450 | 476 |
/// |
451 |
/// |
|
477 |
/// This function inserts the given item into the heap with the |
|
478 |
/// given priority. |
|
452 | 479 |
/// \param i The item to insert. |
453 | 480 |
/// \param p The priority of the item. |
481 |
/// \pre \e i must not be stored in the heap. |
|
454 | 482 |
void push(const Item &i, const Prio &p) { |
455 | 483 |
int idx; |
456 | 484 |
if (_free == -1) { |
457 | 485 |
idx = _data.size(); |
458 | 486 |
_data.push_back(BucketItem(i)); |
459 | 487 |
} else { |
460 | 488 |
idx = _free; |
461 | 489 |
_free = _data[idx].next; |
462 | 490 |
_data[idx].item = i; |
463 | 491 |
} |
464 | 492 |
_iim[i] = idx; |
465 | 493 |
if (p >= int(_first.size())) _first.resize(p + 1, -1); |
466 | 494 |
_data[idx].next = _first[p]; |
467 | 495 |
_first[p] = idx; |
468 | 496 |
if (Direction::less(p, _minimum)) { |
469 | 497 |
_minimum = p; |
470 | 498 |
} |
471 | 499 |
++_num; |
472 | 500 |
} |
473 | 501 |
|
474 |
/// \brief |
|
502 |
/// \brief Return the item having minimum priority. |
|
475 | 503 |
/// |
476 |
/// This method returns the item with minimum priority. |
|
477 |
/// \pre The heap must be nonempty. |
|
504 |
/// This function returns the item having minimum priority. |
|
505 |
/// \pre The heap must be non-empty. |
|
478 | 506 |
Item top() const { |
479 | 507 |
while (_first[_minimum] == -1) { |
480 | 508 |
Direction::increase(_minimum); |
481 | 509 |
} |
482 | 510 |
return _data[_first[_minimum]].item; |
483 | 511 |
} |
484 | 512 |
|
485 |
/// \brief |
|
513 |
/// \brief The minimum priority. |
|
486 | 514 |
/// |
487 |
/// It returns the minimum priority. |
|
488 |
/// \pre The heap must be nonempty. |
|
515 |
/// This function returns the minimum priority. |
|
516 |
/// \pre The heap must be non-empty. |
|
489 | 517 |
Prio prio() const { |
490 | 518 |
while (_first[_minimum] == -1) { |
491 | 519 |
Direction::increase(_minimum); |
492 | 520 |
} |
493 | 521 |
return _minimum; |
494 | 522 |
} |
495 | 523 |
|
496 |
/// \brief |
|
524 |
/// \brief Remove the item having minimum priority. |
|
497 | 525 |
/// |
498 |
/// This |
|
526 |
/// This function removes the item having minimum priority. |
|
499 | 527 |
/// \pre The heap must be non-empty. |
500 | 528 |
void pop() { |
501 | 529 |
while (_first[_minimum] == -1) { |
502 | 530 |
Direction::increase(_minimum); |
503 | 531 |
} |
504 | 532 |
int idx = _first[_minimum]; |
505 | 533 |
_iim[_data[idx].item] = -2; |
506 | 534 |
_first[_minimum] = _data[idx].next; |
507 | 535 |
_data[idx].next = _free; |
508 | 536 |
_free = idx; |
509 | 537 |
--_num; |
510 | 538 |
} |
511 | 539 |
|
512 |
/// \brief |
|
540 |
/// \brief The priority of the given item. |
|
513 | 541 |
/// |
514 |
/// This function returns the priority of item \c i. |
|
515 |
/// \warning This operator is not a constant time function |
|
516 |
/// because it scans the whole data structure to find the proper |
|
517 |
/// value. |
|
518 |
/// |
|
542 |
/// This function returns the priority of the given item. |
|
519 | 543 |
/// \param i The item. |
544 |
/// \pre \e i must be in the heap. |
|
545 |
/// \warning This operator is not a constant time function because |
|
546 |
/// it scans the whole data structure to find the proper value. |
|
520 | 547 |
Prio operator[](const Item &i) const { |
521 |
for (int k = 0; k < _first.size(); ++k) { |
|
548 |
for (int k = 0; k < int(_first.size()); ++k) { |
|
522 | 549 |
int idx = _first[k]; |
523 | 550 |
while (idx != -1) { |
524 | 551 |
if (_data[idx].item == i) { |
525 | 552 |
return k; |
526 | 553 |
} |
527 | 554 |
idx = _data[idx].next; |
528 | 555 |
} |
529 | 556 |
} |
530 | 557 |
return -1; |
531 | 558 |
} |
532 | 559 |
|
533 |
/// \brief Returns if \c item is in, has already been in, or has |
|
534 |
/// never been in the heap. |
|
560 |
/// \brief Return the state of an item. |
|
535 | 561 |
/// |
536 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
537 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
538 |
/// otherwise. In the latter case it is possible that \c item will |
|
539 |
/// get back to the heap again. |
|
562 |
/// This method returns \c PRE_HEAP if the given item has never |
|
563 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
|
564 |
/// and \c POST_HEAP otherwise. |
|
565 |
/// In the latter case it is possible that the item will get back |
|
566 |
/// to the heap again. |
|
540 | 567 |
/// \param i The item. |
541 | 568 |
State state(const Item &i) const { |
542 | 569 |
int idx = _iim[i]; |
543 | 570 |
if (idx >= 0) idx = 0; |
544 | 571 |
return State(idx); |
545 | 572 |
} |
546 | 573 |
|
547 | 574 |
private: |
548 | 575 |
|
549 | 576 |
struct BucketItem { |
550 | 577 |
BucketItem(const Item& _item) |
551 | 578 |
: item(_item) {} |
... | ... |
@@ -85,24 +85,36 @@ |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
CbcMip* CbcMip::cloneSolver() const { |
88 | 88 |
CbcMip* copylp = new CbcMip(*this); |
89 | 89 |
return copylp; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
int CbcMip::_addRow() { |
93 | 93 |
_prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX); |
94 | 94 |
return _prob->numberRows() - 1; |
95 | 95 |
} |
96 | 96 |
|
97 |
int CbcMip::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) { |
|
98 |
std::vector<int> indexes; |
|
99 |
std::vector<Value> values; |
|
100 |
|
|
101 |
for(ExprIterator it = b; it != e; ++it) { |
|
102 |
indexes.push_back(it->first); |
|
103 |
values.push_back(it->second); |
|
104 |
} |
|
105 |
|
|
106 |
_prob->addRow(values.size(), &indexes.front(), &values.front(), l, u); |
|
107 |
return _prob->numberRows() - 1; |
|
108 |
} |
|
97 | 109 |
|
98 | 110 |
void CbcMip::_eraseCol(int i) { |
99 | 111 |
_prob->deleteColumn(i); |
100 | 112 |
} |
101 | 113 |
|
102 | 114 |
void CbcMip::_eraseRow(int i) { |
103 | 115 |
_prob->deleteRow(i); |
104 | 116 |
} |
105 | 117 |
|
106 | 118 |
void CbcMip::_eraseColId(int i) { |
107 | 119 |
cols.eraseIndex(i); |
108 | 120 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
... | ... |
@@ -53,24 +53,25 @@ |
53 | 53 |
~CbcMip(); |
54 | 54 |
/// \e |
55 | 55 |
virtual CbcMip* newSolver() const; |
56 | 56 |
/// \e |
57 | 57 |
virtual CbcMip* cloneSolver() const; |
58 | 58 |
|
59 | 59 |
protected: |
60 | 60 |
|
61 | 61 |
virtual const char* _solverName() const; |
62 | 62 |
|
63 | 63 |
virtual int _addCol(); |
64 | 64 |
virtual int _addRow(); |
65 |
virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u); |
|
65 | 66 |
|
66 | 67 |
virtual void _eraseCol(int i); |
67 | 68 |
virtual void _eraseRow(int i); |
68 | 69 |
|
69 | 70 |
virtual void _eraseColId(int i); |
70 | 71 |
virtual void _eraseRowId(int i); |
71 | 72 |
|
72 | 73 |
virtual void _getColName(int col, std::string& name) const; |
73 | 74 |
virtual void _setColName(int col, const std::string& name); |
74 | 75 |
virtual int _colByName(const std::string& name) const; |
75 | 76 |
|
76 | 77 |
virtual void _getRowName(int row, std::string& name) const; |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)