Changeset fa8ca4


Ignore:
Timestamp:
06/04/12 17:36:59 (12 months ago)
Author:
Tiago de Paula Peixoto <tiago@…>
Branches:
master
Children:
2ca0c3
Parents:
e43e4e
git-author:
Tiago de Paula Peixoto <tiago@…> (06/04/12 16:27:03)
git-committer:
Tiago de Paula Peixoto <tiago@…> (06/04/12 17:36:59)
Message:
Implement topology.is_bipartite()
Location:
src
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/graph/topology/Makefile.am

    r4453f7 rfa8ca4  
    1717libgraph_tool_topology_la_SOURCES = \ 
    1818    graph_all_distances.cc \ 
     19    graph_bipartite.cc \ 
    1920    graph_components.cc \ 
    2021    graph_distance.cc \ 
  • src/graph/topology/graph_topology.cc

    r4453f7 rfa8ca4  
    3939                          size_t n_max, size_t seed); 
    4040double reciprocity(GraphInterface& gi); 
     41bool is_bipartite(GraphInterface& gi, boost::any part_map); 
    4142 
    4243void export_components(); 
     
    6061    def("is_planar", &is_planar); 
    6162    def("reciprocity", &reciprocity); 
     63    def("is_bipartite", &is_bipartite); 
    6264    export_components(); 
    6365    export_similarity(); 
  • src/graph_tool/topology/__init__.py

    r01fd54 rfa8ca4  
    4545   label_biconnected_components 
    4646   label_largest_component 
     47   is_bipartite 
    4748   is_planar 
    4849   edge_reciprocity 
     
    6768           "transitive_closure", "label_components", "label_largest_component", 
    6869           "label_biconnected_components", "shortest_distance", "shortest_path", 
    69            "pseudo_diameter", "is_planar", "similarity", "edge_reciprocity"] 
     70           "pseudo_diameter", "is_bipartite", "is_planar", "similarity", "edge_reciprocity"] 
    7071 
    7172 
     
    10071008 
    10081009 
     1010def is_bipartite(g, partition=False): 
     1011    """ 
     1012    Test if the graph is bipartite. 
     1013 
     1014    Parameters 
     1015    ---------- 
     1016    g : :class:`~graph_tool.Graph` 
     1017        Graph to be used. 
     1018    partition : bool (optional, default: ``False``) 
     1019        If ``True``, return the two partitions in case the graph is bipartite. 
     1020 
     1021    Returns 
     1022    ------- 
     1023    is_bipartite : bool 
     1024        Whether or not the graph is bipartite. 
     1025    partition : :class:`~graph_tool.PropertyMap` (only if `partition=True`) 
     1026        A vertex property map with the graph partitioning (or `None`) if the 
     1027        graph is not bipartite. 
     1028 
     1029    Notes 
     1030    ----- 
     1031 
     1032    An undirected graph is bipartite if one can partition its set of vertices 
     1033    into two sets, such that all edges go from one set to the other. 
     1034 
     1035    This algorithm runs in :math:`O(V + E)` time. 
     1036 
     1037    Examples 
     1038    -------- 
     1039    >>> g = gt.lattice([10, 10]) 
     1040    >>> is_bi, part = gt.is_bipartite(g, partition=True) 
     1041    >>> print(is_bi) 
     1042    True 
     1043    >>> gt.graph_draw(g, vertex_color=part, output_size=(300, 300), output="bipartite.pdf") 
     1044    <...> 
     1045 
     1046    .. figure:: bipartite.* 
     1047        :align: center 
     1048 
     1049        Bipartition of a 2D lattice. 
     1050 
     1051    References 
     1052    ---------- 
     1053    .. [boost-bipartite] http://www.boost.org/libs/graph/doc/is_bipartite.html 
     1054    """ 
     1055 
     1056    if partition: 
     1057        part = g.new_vertex_property("bool") 
     1058    else: 
     1059        part = None 
     1060    g = GraphView(g, directed=False) 
     1061    is_bi = libgraph_tool_topology.is_bipartite(g._Graph__graph, 
     1062                                                _prop("v", g, part)) 
     1063    if partition: 
     1064        return is_bi, part 
     1065    else: 
     1066        return is_bi 
     1067 
     1068 
    10091069def is_planar(g, embedding=False, kuratowski=False): 
    10101070    """ 
Note: See TracChangeset for help on using the changeset viewer.