@@ -8,10 +8,21 @@ mutable struct BipartiteGraph <: Graphs.AbstractGraph{Int64}
88 vertexset1:: Vector{Int64}
99 vertexset2:: Vector{Int64}
1010end
11+ """
12+ BipartiteGraph()
13+
14+ Construct an empty `BipartiteGraph` with no vertices or edges.
15+ """
1116function BipartiteGraph ()
1217 return BipartiteGraph (Graphs. Graph (), Vector {Int64} (), Vector {Int64} ())
1318end
1419
20+ """
21+ Graphs.add_vertex!(bgraph::BipartiteGraph; bipartite=1)
22+
23+ Add a vertex to the bipartite graph. The `bipartite` keyword argument specifies
24+ which vertex set to add the vertex to (1 or 2). Returns the success status.
25+ """
1526function Graphs. add_vertex! (bgraph:: BipartiteGraph ; bipartite= 1 )
1627 added = Graphs. add_vertex! (bgraph. graph)
1728 vertex = Graphs. nv (bgraph. graph)
@@ -24,34 +35,91 @@ function Graphs.add_vertex!(bgraph::BipartiteGraph; bipartite=1)
2435 return added
2536end
2637
38+ """
39+ Graphs.add_edge!(bgraph::BipartiteGraph, from::Int64, to::Int64)
40+
41+ Add an edge between vertices `from` and `to`. Enforces bipartite structure by
42+ requiring that the vertices be in different vertex sets.
43+ """
2744function Graphs. add_edge! (bgraph:: BipartiteGraph , from:: Int64 , to:: Int64 )
2845 length (intersect ((from, to), bgraph. vertexset1)) == 1 ||
2946 error (" $from and $to must be in separate vertex sets" )
3047 return Graphs. add_edge! (bgraph. graph, from, to)
3148end
3249
50+ """
51+ Graphs.edges(bgraph::BipartiteGraph)
52+
53+ Return an iterator over all edges in the bipartite graph.
54+ """
3355Graphs. edges (bgraph:: BipartiteGraph ) = Graphs. edges (bgraph. graph)
3456
57+ """
58+ Graphs.edgetype(bgraph::BipartiteGraph)
59+
60+ Return the edge type for a bipartite graph (`SimpleEdge{Int64}`).
61+ """
3562Graphs. edgetype (bgraph:: BipartiteGraph ) = Graphs. SimpleGraphs. SimpleEdge{Int64}
3663
64+ """
65+ Graphs.has_edge(bgraph::BipartiteGraph, from::Int64, to::Int64)
66+
67+ Check if the bipartite graph has an edge from `from` to `to`.
68+ """
3769function Graphs. has_edge (bgraph:: BipartiteGraph , from:: Int64 , to:: Int64 )
3870 return Graphs. has_edge (bgraph. graph, from, to)
3971end
4072
73+ """
74+ Graphs.has_vertex(bgraph::BipartiteGraph, v::Integer)
75+
76+ Check if the bipartite graph contains vertex `v`.
77+ """
4178function Graphs. has_vertex (bgraph:: BipartiteGraph , v:: Integer )
4279 return Graphs. has_vertex (bgraph. graph, v)
4380end
4481
82+ """
83+ Graphs.is_directed(bgraph::BipartiteGraph)
84+
85+ Bipartite graphs are undirected. Always returns `false`.
86+ """
4587Graphs. is_directed (bgraph:: BipartiteGraph ) = false
4688
89+ """
90+ Graphs.is_directed(::Type{BipartiteGraph})
91+
92+ Bipartite graphs are undirected. Always returns `false`.
93+ """
4794Graphs. is_directed (:: Type{BipartiteGraph} ) = false
4895
96+ """
97+ Graphs.ne(bgraph::BipartiteGraph)
98+
99+ Return the number of edges in the bipartite graph.
100+ """
49101Graphs. ne (bgraph:: BipartiteGraph ) = Graphs. ne (bgraph. graph)
50102
103+ """
104+ Graphs.nv(bgraph::BipartiteGraph)
105+
106+ Return the number of vertices in the bipartite graph.
107+ """
51108Graphs. nv (bgraph:: BipartiteGraph ) = Graphs. nv (bgraph. graph)
52109
110+ """
111+ Graphs.vertices(bgraph::BipartiteGraph)
112+
113+ Return a vector of all vertices in the bipartite graph.
114+ """
53115Graphs. vertices (bgraph:: BipartiteGraph ) = Graphs. vertices (bgraph. graph)
54116
117+ """
118+ Graphs.adjacency_matrix(bgraph::BipartiteGraph)
119+
120+ Return the adjacency matrix of the bipartite graph. Rows correspond to vertices
121+ in the first set, columns correspond to vertices in the second set.
122+ """
55123function Graphs. adjacency_matrix (bgraph:: BipartiteGraph )
56124 n_v1 = length (bgraph. vertexset1)
57125 n_v2 = length (bgraph. vertexset2)
@@ -62,6 +130,18 @@ function Graphs.adjacency_matrix(bgraph::BipartiteGraph)
62130 return A
63131end
64132
133+ """
134+ identify_separators(bgraph::BipartiteGraph, partitions::Vector; cut_selector=Graphs.degree)
135+
136+ Identify separator elements (cut vertices or cut edges) that separate the given `partitions`.
137+ The `cut_selector` parameter determines how to assign boundary elements to cuts. It can be:
138+ - A function (e.g., `Graphs.degree`) that compares element degrees
139+ - `:vertex` to always assign vertices as separators
140+ - `:edge` to always assign edges as separators
141+
142+ Returns a tuple `(partition_elements, cross_elements)` where `partition_elements` are the
143+ elements local to each partition and `cross_elements` are the separator elements.
144+ """
65145function identify_separators (
66146 bgraph:: BipartiteGraph , partitions:: Vector ; cut_selector= Graphs. degree
67147)
@@ -123,6 +203,21 @@ function identify_separators(
123203 return partition_elements, cross_elements
124204end
125205
206+ """
207+ induced_elements(bgraph::BipartiteGraph, partitions::Vector; cut_selector=Graphs.degree)
208+
209+ Get the induced elements for each partition (i.e., elements local to each partition,
210+ excluding separators). This is a convenience function that returns only the first element
211+ of `identify_separators`.
212+
213+ # Arguments
214+ - `bgraph::BipartiteGraph`: The bipartite graph
215+ - `partitions::Vector`: Vector of partitions
216+ - `cut_selector=Graphs.degree`: Selector for determining cut assignment (function, `:vertex`, or `:edge`)
217+
218+ # Returns
219+ A vector of vectors, where each inner vector contains the induced elements for that partition.
220+ """
126221function induced_elements (
127222 bgraph:: BipartiteGraph , partitions:: Vector ; cut_selector= Graphs. degree
128223)
0 commit comments