# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1263661219 -3600
# Node ID 44726155b3d1b768675926d29b71c820717b6db3
# Parent 9491c46f86b19b985e09d32609207845783fddba
ticket #7854 : small modifications to edge_connectivity for graphs
diff -r 9491c46f86b1 -r 44726155b3d1 sage/graphs/generic_graph.py
a
|
b
|
|
4038 | 4038 | b=p.get_values(b) |
4039 | 4039 | return [v for v in g.vertices() if b[v]==1] |
4040 | 4040 | |
4041 | | def edge_connectivity(self,value_only=True,use_edge_labels=True, vertices=False): |
| 4041 | def edge_connectivity(self,value_only=True,use_edge_labels=False, vertices=False): |
4042 | 4042 | r""" |
4043 | 4043 | Returns the edge connectivity of the graph |
4044 | 4044 | ( cf. http://en.wikipedia.org/wiki/Connectivity_(graph_theory) ) |
… |
… |
|
4069 | 4069 | |
4070 | 4070 | EXAMPLE: |
4071 | 4071 | |
4072 | | A basic application on the PappusGraph() |
| 4072 | A basic application on the PappusGraph:: |
4073 | 4073 | |
4074 | 4074 | sage: g = graphs.PappusGraph() |
4075 | 4075 | sage: g.edge_connectivity() # optional - requires Glpk or COIN-OR/CBC |
… |
… |
|
4106 | 4106 | sage: tree.add_edges(g.min_spanning_tree()) |
4107 | 4107 | sage: for u,v in tree.edge_iterator(labels=None): |
4108 | 4108 | ... tree.set_edge_label(u,v,random()) |
4109 | | sage: minimum = min([l for u,v,l in tree.edge_iterator()]) # optional - requires Glpk or COIN-OR/CBC |
4110 | | sage: [value, [(u,v,l)]] = tree.edge_connectivity(value_only=False) # optional - requires Glpk or COIN-OR/CBC |
4111 | | sage: l == minimum # optional - requires Glpk or COIN-OR/CBC |
| 4109 | sage: minimum = min([l for u,v,l in tree.edge_iterator()]) # optional - requires Glpk or COIN-OR/CBC |
| 4110 | sage: [value, [(u,v,l)]] = tree.edge_connectivity(value_only=False, use_edge_labels=True) # optional - requires Glpk or COIN-OR/CBC |
| 4111 | sage: l == minimum # optional - requires Glpk or COIN-OR/CBC |
4112 | 4112 | True |
| 4113 | |
| 4114 | When ``value_only = True``, this function is optimized for small |
| 4115 | connexity values and does not need to build a linear program. |
| 4116 | |
| 4117 | It is the case for connected graphs which are not |
| 4118 | connected :: |
| 4119 | |
| 4120 | sage: g = 2 * graphs.PetersenGraph() |
| 4121 | sage: g.edge_connectivity() |
| 4122 | 0.0 |
| 4123 | |
| 4124 | Or if they are just 1-connected :: |
| 4125 | |
| 4126 | sage: g = graphs.PathGraph(10) |
| 4127 | sage: g.edge_connectivity() |
| 4128 | 1.0 |
| 4129 | |
| 4130 | For directed graphs, the strong connexity is tested |
| 4131 | through the dedicated function :: |
| 4132 | |
| 4133 | sage: g = digraphs.ButterflyGraph(3) |
| 4134 | sage: g.edge_connectivity() |
| 4135 | 0.0 |
4113 | 4136 | """ |
4114 | 4137 | g=self |
4115 | 4138 | |
… |
… |
|
4121 | 4144 | else: |
4122 | 4145 | weight=lambda x: 1 |
4123 | 4146 | |
| 4147 | |
| 4148 | # Better methods for small connectivity tests, |
| 4149 | # when one is not interested in cuts... |
| 4150 | if value_only and not use_edge_labels: |
| 4151 | |
| 4152 | if self.is_directed(): |
| 4153 | if not self.is_strongly_connected(): |
| 4154 | return 0.0 |
| 4155 | |
| 4156 | else: |
| 4157 | if not self.is_connected(): |
| 4158 | return 0.0 |
| 4159 | |
| 4160 | h = self.strong_orientation() |
| 4161 | if not h.is_strongly_connected(): |
| 4162 | return 1.0 |
| 4163 | |
| 4164 | |
4124 | 4165 | if g.is_directed(): |
4125 | 4166 | reorder_edge = lambda x,y : (x,y) |
4126 | 4167 | else: |
… |
… |
|
4160 | 4201 | p.set_binary(in_set) |
4161 | 4202 | p.set_binary(in_cut) |
4162 | 4203 | |
4163 | | p.set_objective(sum([weight(l ) * in_cut[reorder_edge(u,v)] for (u,v,l ) in g.edge_iterator()])) |
| 4204 | p.set_objective(sum([weight(l ) * in_cut[reorder_edge(u,v)] for (u,v,l) in g.edge_iterator()])) |
4164 | 4205 | |
4165 | 4206 | if value_only: |
4166 | 4207 | return p.solve(objective_only=True) |
… |
… |
|
4236 | 4277 | sage: [val, [cut_vertex]] = tree.vertex_connectivity(value_only=False) # optional - requires Glpk or COIN-OR/CBC |
4237 | 4278 | sage: tree.degree(cut_vertex) > 1 # optional - requires Glpk or COIN-OR/CBC |
4238 | 4279 | True |
| 4280 | |
| 4281 | When ``value_only = True``, this function is optimized for small |
| 4282 | connexity values and does not need to build a linear program. |
| 4283 | |
| 4284 | It is the case for connected graphs which are not |
| 4285 | connected :: |
| 4286 | |
| 4287 | sage: g = 2 * graphs.PetersenGraph() |
| 4288 | sage: g.vertex_connectivity() |
| 4289 | 0.0 |
| 4290 | |
| 4291 | Or if they are just 1-connected :: |
| 4292 | |
| 4293 | sage: g = graphs.PathGraph(10) |
| 4294 | sage: g.vertex_connectivity() |
| 4295 | 1.0 |
| 4296 | |
| 4297 | For directed graphs, the strong connexity is tested |
| 4298 | through the dedicated function :: |
| 4299 | |
| 4300 | sage: g = digraphs.ButterflyGraph(3) |
| 4301 | sage: g.vertex_connectivity() |
| 4302 | 0.0 |
| 4303 | |
4239 | 4304 | """ |
4240 | 4305 | g=self |
4241 | 4306 | |
… |
… |
|
4245 | 4310 | if sets: |
4246 | 4311 | value_only=False |
4247 | 4312 | |
| 4313 | if value_only: |
| 4314 | if self.is_directed(): |
| 4315 | if not self.is_strongly_connected(): |
| 4316 | return 0.0 |
| 4317 | |
| 4318 | else: |
| 4319 | if not self.is_connected(): |
| 4320 | return 0.0 |
| 4321 | |
| 4322 | if len(self.blocks_and_cut_vertices()[0]) > 1: |
| 4323 | return 1.0 |
| 4324 | |
| 4325 | |
4248 | 4326 | if g.is_directed(): |
4249 | 4327 | reorder_edge = lambda x,y : (x,y) |
4250 | 4328 | else: |