# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1273280136 14400
# Node ID 8c51f0d9a582bdf03502c7ffdc430cbf66c36cda
# Parent 263f1caae32dd7ac94a108e8b279a9872b56f260
trac 8892 - Fixing broken docstring
diff -r 263f1caae32d -r 8c51f0d9a582 sage/graphs/generic_graph.py
a
|
b
|
|
2058 | 2058 | "Please convert it to a Graph if you really mean it.") |
2059 | 2059 | |
2060 | 2060 | if use_edge_labels: |
2061 | | weight = lambda u,v : self.edge_label(u,v) if self.edge_label(u,v)!=None else 1 |
| 2061 | from sage.rings.real_mpfr import RR |
| 2062 | weight = lambda u,v : self.edge_label(u,v) if self.edge_label(u,v) in RR else 1 |
2062 | 2063 | else: |
2063 | 2064 | weight = lambda u,v : 1 |
2064 | 2065 | |
… |
… |
|
3108 | 3109 | The edge cut between the two ends is the edge of minimum weight:: |
3109 | 3110 | |
3110 | 3111 | sage: minimum = min([l for u,v,l in g.edge_iterator()]) |
3111 | | sage: minimum == g.edge_cut(0, 14, use_edge_labels=True) # optional - requires GLPK or COIN-OR/CBC |
| 3112 | sage: abs(minimum - g.edge_cut(0, 14, use_edge_labels=True)) < 10**(-5) # optional - requires GLPK or COIN-OR/CBC or CPLEX |
3112 | 3113 | True |
3113 | 3114 | sage: [value,[[u,v]]] = g.edge_cut(0, 14, use_edge_labels=True, value_only=False) # optional - requires GLPK or COIN-OR/CBC |
3114 | 3115 | sage: g.edge_label(u, v) == minimum # optional - requires GLPK or COIN-OR/CBC |
… |
… |
|
3133 | 3134 | if vertices: |
3134 | 3135 | value_only = False |
3135 | 3136 | if use_edge_labels: |
3136 | | weight = lambda x: 1 if x == None else x |
| 3137 | from sage.rings.real_mpfr import RR |
| 3138 | weight = lambda x: x if x in RR else 1 |
3137 | 3139 | else: |
3138 | 3140 | weight = lambda x: 1 |
3139 | 3141 | |
… |
… |
|
3653 | 3655 | sage: [ value, edges, [ setA, setB ]] = g.max_cut(vertices=True) # optional - requires Glpk or COIN-OR/CBC |
3654 | 3656 | sage: value == 5*6 # optional - requires Glpk or COIN-OR/CBC |
3655 | 3657 | True |
3656 | | sage: bsetA, bsetB = g.bipartite_sets() |
| 3658 | sage: bsetA, bsetB = map(list,g.bipartite_sets()) |
3657 | 3659 | sage: (bsetA == setA and bsetB == setB ) or ((bsetA == setB and bsetB == setA )) # optional - requires Glpk or COIN-OR/CBC |
3658 | 3660 | True |
3659 | 3661 | |
… |
… |
|
3670 | 3672 | value_only=False |
3671 | 3673 | |
3672 | 3674 | if use_edge_labels: |
3673 | | weight=lambda x: 1 if x==None else x |
| 3675 | from sage.rings.real_mpfr import RR |
| 3676 | weight = lambda x: x if x in RR else 1 |
3674 | 3677 | else: |
3675 | | weight=lambda x: 1 |
| 3678 | weight = lambda x: 1 |
3676 | 3679 | |
3677 | 3680 | if g.is_directed(): |
3678 | 3681 | reorder_edge = lambda x,y : (x,y) |
… |
… |
|
3834 | 3837 | flow=p.new_variable(dim=2) |
3835 | 3838 | |
3836 | 3839 | if use_edge_labels: |
3837 | | capacity=lambda x: x if x!=None else 1 |
| 3840 | from sage.rings.real_mpfr import RR |
| 3841 | capacity=lambda x: x if x in RR else 1 |
3838 | 3842 | else: |
3839 | 3843 | capacity=lambda x: 1 |
3840 | 3844 | |
… |
… |
|
4054 | 4058 | |
4055 | 4059 | # returns the weight of an edge considering it may not be |
4056 | 4060 | # weighted ... |
4057 | | weight=lambda x: 1 if x==None else x |
| 4061 | |
| 4062 | from sage.rings.real_mpfr import RR |
| 4063 | weight=lambda x: x if x in RR else 1 |
4058 | 4064 | |
4059 | 4065 | p=MixedIntegerLinearProgram(maximization=True) |
4060 | 4066 | |
… |
… |
|
4263 | 4269 | value_only=False |
4264 | 4270 | |
4265 | 4271 | if use_edge_labels: |
4266 | | weight=lambda x: 1 if x==None else x |
| 4272 | from sage.rings.real_mpfr import RR |
| 4273 | weight=lambda x: x if x in RR else 1 |
4267 | 4274 | else: |
4268 | 4275 | weight=lambda x: 1 |
4269 | 4276 | |
diff -r 263f1caae32d -r 8c51f0d9a582 sage/graphs/graph.py
a
|
b
|
|
1428 | 1428 | |
1429 | 1429 | sage: g = graphs.CycleGraph(6) |
1430 | 1430 | sage: bounds = lambda x: [1,1] |
1431 | | sage: m = g.degree_constrained_subgraph(bounds=bounds) # optional - requires GLPK or CBC |
1432 | | sage: m.size() #optional |
| 1431 | sage: m = g.degree_constrained_subgraph(bounds=bounds) # optional - requires GLPK or CBC or CPLEX |
| 1432 | sage: m.size() # optional - requires GLPK CBC or CPLEX |
1433 | 1433 | 3 |
1434 | 1434 | """ |
1435 | 1435 | |
… |
… |
|
1438 | 1438 | p = MixedIntegerLinearProgram(maximization=False) |
1439 | 1439 | b = p.new_variable() |
1440 | 1440 | |
1441 | | reorder = lambda x: (min(x[0],x[1]),max(x[0],x[1]),x[2]) |
| 1441 | reorder = lambda x,y: (x,y) if x<y else (y,x) |
1442 | 1442 | |
1443 | 1443 | if bounds is None: |
1444 | 1444 | raise ValueError,"The `bounds` keyword can not be equal to None" |
… |
… |
|
1449 | 1449 | |
1450 | 1450 | |
1451 | 1451 | if self.weighted(): |
1452 | | weight = lambda x: x[2] if x[2] is not None else 1 |
| 1452 | from sage.rings.real_mpfr import RR |
| 1453 | weight = lambda x: x if x in RR else 1 |
1453 | 1454 | else: |
1454 | 1455 | weight = lambda x: 1 |
1455 | 1456 | |
1456 | 1457 | for v in self: |
1457 | 1458 | minimum,maximum = f_bounds(v) |
1458 | | p.add_constraint(sum([ b[reorder(e)]*weight(e) for e in self.edges_incident(v)]),min=minimum, max=maximum) |
| 1459 | p.add_constraint(sum([ b[reorder(x,y)]*weight(l) for x,y,l in self.edges_incident(v)]),min=minimum, max=maximum) |
1459 | 1460 | |
1460 | | p.set_objective(sum([ b[reorder(e)]*weight(e) for e in self.edge_iterator()])) |
| 1461 | p.set_objective(sum([ b[reorder(x,y)]*weight(l) for x,y,l in self.edge_iterator()])) |
1461 | 1462 | p.set_binary(b) |
1462 | 1463 | |
1463 | 1464 | try: |
1464 | 1465 | p.solve() |
1465 | 1466 | g = self.copy() |
1466 | 1467 | b = p.get_values(b) |
1467 | | g.delete_edges([e for e in g.edge_iterator() if b[reorder(e)] == 0]) |
| 1468 | g.delete_edges([(x,y) for x,y,_ in g.edge_iterator() if b[reorder(x,y)] < 0.5]) |
1468 | 1469 | return g |
1469 | 1470 | |
1470 | 1471 | |
… |
… |
|
1472 | 1473 | return False |
1473 | 1474 | |
1474 | 1475 | |
1475 | | |
1476 | | |
1477 | | |
1478 | 1476 | ### Orientations |
1479 | 1477 | |
1480 | 1478 | def strong_orientation(self): |
… |
… |
|
2913 | 2911 | from sage.sets.set import Set |
2914 | 2912 | |
2915 | 2913 | # The default capacity of an arc is 1 |
2916 | | capacity = lambda label: label if label is not None else 1 |
| 2914 | from sage.rings.real_mpfr import RR |
| 2915 | capacity = lambda label: label if label in RR else 1 |
2917 | 2916 | |
2918 | 2917 | # Keeping the graph's embedding |
2919 | 2918 | pos = False |
diff -r 263f1caae32d -r 8c51f0d9a582 sage/graphs/graph_coloring.py
a
|
b
|
|
543 | 543 | obj = {} |
544 | 544 | k = max(g.degree()) |
545 | 545 | # reorders the edge if necessary... |
546 | | R = lambda x: x if (x[0] <= x[1]) else (x[1], x[0], x[2]) |
| 546 | R = lambda x: x if (x[0] <= x[1]) else (x[1], x[0]) |
547 | 547 | # Vizing's coloring uses Delta + 1 colors |
548 | 548 | if vizing: |
549 | 549 | value_only = False |
550 | 550 | k += 1 |
551 | 551 | # A vertex can not have two incident edges with the same color. |
552 | 552 | [p.add_constraint( |
553 | | sum([color[R(e)][i] for e in g.edges_incident(v)]), max=1) |
| 553 | sum([color[R(e)][i] for e in g.edges_incident(v, labels=False)]), max=1) |
554 | 554 | for v in g.vertex_iterator() |
555 | 555 | for i in xrange(k)] |
556 | 556 | # an edge must have a color |
557 | 557 | [p.add_constraint(sum([color[R(e)][i] for i in xrange(k)]), max=1, min=1) |
558 | | for e in g.edge_iterator()] |
| 558 | for e in g.edge_iterator(labels=False)] |
559 | 559 | # anything is good as an objective value as long as it is satisfiable |
560 | | e = g.edge_iterator().next() |
| 560 | e = g.edge_iterator(labels=False).next() |
561 | 561 | p.set_objective(color[R(e)][0]) |
562 | 562 | p.set_binary(color) |
563 | 563 | try: |
… |
… |
|
577 | 577 | color = p.get_values(color) |
578 | 578 | classes = [[] for i in xrange(k)] |
579 | 579 | [classes[i].append(e) |
580 | | for e in g.edge_iterator() |
| 580 | for e in g.edge_iterator(labels = False) |
581 | 581 | for i in xrange(k) |
582 | 582 | if color[R(e)][i] == 1] |
583 | 583 | # if needed, builds a dictionary from the color classes adding colors |