Ticket #7292: trac_7292.patch

File trac_7292.patch, 3.0 KB (added by ncohen, 11 years ago)
  • sage/graphs/graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1260888265 -3600
    # Node ID 12e224b8b44dc3d7de154c2d4127fe80eb295842
    # Parent  1d7f8f66bbd069169b8f3d4f5083e06b64336ee5
    edge_disjoint and vertex_disjoint paths
    
    diff -r 1d7f8f66bbd0 -r 12e224b8b44d sage/graphs/graph.py
    a b  
    34283428
    34293429        return [obj,flow_graph]
    34303430
     3431    def edge_disjoint_paths(self, s, t):
     3432        r"""
     3433        Returns a list of edge-disjoint paths between two
     3434        vertices as given by Menger's theorem.
     3435
     3436        The edge version of Menger's theorem asserts that the size
     3437        of the minimum edge cut between two vertices `s` and`t`
     3438        (the minimum number of edges whose removal disconnects `s`
     3439        and `t`) is equal to the maximum number of pairwise
     3440        edge-independent paths from `s` to `t`.
     3441
     3442        This function returns a list of such paths.
     3443
     3444        NOTE:
     3445
     3446        This function is topological : it does not take the eventual
     3447        weights of the edges into account.
     3448
     3449        EXAMPLE:
     3450
     3451        In a complete bipartite graph ::
     3452
     3453            sage: g = graphs.CompleteBipartiteGraph(2,3)
     3454            sage: g.edge_disjoint_paths(0,1) # optional - requires GLPK or CBC
     3455            [[0, 2, 1], [0, 3, 1], [0, 4, 1]]
     3456        """
     3457
     3458        [obj, flow_graph] = self.flow(s,t,value_only=False, integer=True, use_edge_labels=False)
     3459
     3460        paths = []
     3461
     3462        while True:
     3463            path = flow_graph.shortest_path(s,t)
     3464            if not path:
     3465                break
     3466            v = s
     3467            edges = []
     3468            for w in path:
     3469                edges.append((v,w))
     3470                v=w
     3471            flow_graph.delete_edges(edges)
     3472            paths.append(path)
     3473
     3474        return paths
     3475
     3476    def vertex_disjoint_paths(self, s, t):
     3477        r"""
     3478        Returns a list of vertex-disjoint paths between two
     3479        vertices as given by Menger's theorem.
     3480
     3481        The vertex version of Menger's theorem asserts that the size
     3482        of the minimum vertex cut between two vertices `s` and`t`
     3483        (the minimum number of vertices whose removal disconnects `s`
     3484        and `t`) is equal to the maximum number of pairwise
     3485        vertex-independent paths from `s` to `t`.
     3486
     3487        This function returns a list of such paths.
     3488
     3489        EXAMPLE:
     3490
     3491        In a complete bipartite graph ::
     3492
     3493            sage: g = graphs.CompleteBipartiteGraph(2,3)
     3494            sage: g.vertex_disjoint_paths(0,1) # optional - requires GLPK or CBC
     3495            [[0, 2, 1], [0, 3, 1], [0, 4, 1]]
     3496        """
     3497
     3498        [obj, flow_graph] = self.flow(s,t,value_only=False, integer=True, use_edge_labels=False, vertex_bound=True)
     3499
     3500        paths = []
     3501
     3502        while True:
     3503            path = flow_graph.shortest_path(s,t)
     3504            if not path:
     3505                break
     3506            flow_graph.delete_vertices(path[1:-1])
     3507            paths.append(path)
     3508
     3509        return paths
    34313510
    34323511    ### Vertex handlers
    34333512