## 1. Introduction

In the paper, we consider a variant of the problem called

**Stable Matchings**, known also in the literature as the

**Stable Marriage** problem. The problem is defined as follows. We are given two sets

W and

U of women and men. Each woman

w of

W has a preference list

${L}_{w}$ of a subset of men, and similarly, each man

m of

U has a preference list

${L}_{m}$ of a subset of women. The preference lists are linearly ordered lists of

**ties**, which are subsets of men (or respectively, women), who are equally good for a given woman (respectively, man). Ties are disjoint and can contain also one person, appropriately a man or a woman. Thus if

m and

${m}^{\prime}$ are on list

${L}_{w}$ of woman

w, then either (1)

w prefers

m to

${m}^{\prime}$, or in other words,

m is better for

w than

${m}^{\prime}$; or (2)

m and

${m}^{\prime}$ are in a tie on

${L}_{w},$ and then we say that

w is indifferent between

m and

${m}^{\prime}$ or that

m and

${m}^{\prime}$ are equally good for her; or (3)

w prefers

${m}^{\prime}$ to

m. Man

m and woman

w are said to be

**mutually acceptable** to each other if they belong to each other’s preference lists. The most preferred person(s) is (are) at the top of the preference lists. A

**matching** is a set of pairs

$(m,w)$ such that

$m\in U,w\in W$ and

m and

w are mutually acceptable, and each man/woman belongs to at most one pair. If

$(m,w)$ belongs to a certain matching

M, then we write

$M\left(m\right)=w$, which means that in

M woman

w is a partner of

m, and analogously that

$M\left(w\right)=m$. If man

m (or woman

w) is not contained in any pair of a matching

M, then we say that

m (or

w) is

**unmatched** or

**free** in

M. A matching

M is called

**stable** if it does not admit a

**blocking pair**. A pair

$(m,w)$ is blocking for

M if (1)

m and

w are mutually acceptable; (2)

m is unmatched or prefers

w to

$M\left(m\right)$; and (3)

w is unmatched or prefers

m to

$M\left(w\right)$. Each instance of the problem can be represented by a bipartite graph

$G=(U\cup W,E)$ with vertices of

U representing men, vertices of

W representing women and edges

E connecting all mutually acceptable pairs of men and women. The problem we are interested in is that of finding a stable matching that has the largest cardinality. The version in which there are no ties in the preference lists of men and women has been long known and an algorithm by Gale and Shapley [

3] solves it exactly in

$O\left(m\right)$ time, where

m denotes the number of edges in the underlying graph. In the version without ties a stable matching always exists, and every stable matching has the same cardinality. If we allow ties, as in the problem we consider in this paper, then a stable matching also always exists and can be found via the Gale-Shapley algorithm by breaking ties arbitrarily. However, the sizes of stable matchings can vary considerably, and the problem of finding a stable matching of maximum cardinality is

$NP$-hard, which was shown by Manlove

et al. in [

14]. Therefore it is desirable to devise an approximation algorithm for the problem.

**Previous and related results** Previous approximation algorithms were presented in [

7,

8,

9,

11,

14]. Currently the best approximation algorithm is by McDermid [

15] and achieves the approximation guarantee

$\frac{3}{2}$. Its running time is

$O\left({n}^{3/2}m\right)$, where

n denotes the number of vertices and

m the number of edges. Inapproximabilty results were shown in [

4,

5,

17]. For a variant of the Stable Matchings problem, in which ties are allowed on one side only, an approximation algorithm achieving the ratio

$\frac{25}{17}$ has been given in [

10]. A slightly different linear time

$\frac{3}{2}$-approximation algorithm based on an earlier version of this paper was given by Király in [

12]. The conference version of this paper appeared also in the proceedings of WAOA’11 (Workshop on Approximation and Online Algorithms) [

16].

**Our results** While constructing approximation algorithms, the goal is not only to achieve a good approximation guarantee but also good running time. We give a 3/2-approximation algorithm that runs in

$O\left(m\right)$ time and additionally is significantly simpler than that of McDermid. In devising the algorithm we were led by the observation that it suffices to find a stable matching that will not create a dangerous path, which is defined later. We also give the extension of the algorithm for computing stable many-to-many matchings, which runs in

$O(mlogc)$ time, where

c denotes the minimum of the maximal capacities in each side of the bipartition. In particular, it means that we give an

$O\left(m\right)$-time algorithm for the Hospitals-Residents problem, improving on an

$O\left({d}^{5/2}{n}^{3/2}m\right)$ time algorithm given by McDermid, where

d denotes the maximal capacity of a hospital. McDermid’s algorithm follows from the reduction of the Hospitals-Residents problem to the Stable Matchings problem by "cloning" hospitals. The approach by cloning does not work if the vertices on both sides of the bipartition are allowed to have capacities larger than 1. Since these problems have many practical applications (see [

1,

2,

6], for example), we believe our algorithms will be of help.

## 2. Algorithm

For a given instance of the problem let

${M}_{opt}$ denote an optimal (

i.e., largest) stable matching and let

$M,{M}^{\prime}$ be any two matchings. We say that

e is an

M-edge if

$e\in M$. A path

P or a cycle

C is called

**alternating (w.r.t. M)** if its edges alternate between

M-edges and edges of

E∖

M. It is well known from matching theory (see [

13] for example) that

$M\oplus {M}^{\prime}$ disintegrates into a set of alternating paths and alternating cycles. (For two sets

$X,Y$, the set

$X\oplus Y$ denotes

$(X$∖

Y) ∪ (

Y∖

$X)$.) Let

S denote a set of maximal alternating paths and cycles of

$M\oplus {M}_{opt}$. Consider any alternating cycle

C of

S or any alternating path

P of even length of

S. Then both

C and

P contain the same number of

M-edges and

${M}_{opt}$-edges. Consider an alternating path

P of length

$2k+1$ of

S. Then either

$\frac{|{M}_{opt}\cap P|}{|M\cap P|}=\frac{k+1}{k}$ or

$\frac{|M\cap P|}{|{M}_{opt}\cap P|}=\frac{k+1}{k}$. Therefore, if

M is stable and

S does not contain a path of length 3 with the middle edge being an

M-edge, then

$|{M}_{opt}|\le \frac{3}{2}\left|M\right|$ and

M is a

$\frac{3}{2}$-approximation of

${M}_{opt}$. To achieve a

$\frac{3}{2}$-approximation, we will eliminate such potential paths of length 3 of

$M\oplus {M}_{opt}$.

Accordingly, we define a **dangerous path w.r.t. a matching M** to be an alternating path $P=(w,{m}_{1},{w}_{1},m)$ such that w and m are unmatched in M (which means that $({m}_{1},{w}_{1})$ is in M and $(w,{m}_{1}),({w}_{1},m)$ do not belong to M) and $({m}_{1},{w}_{1})$ is not a blocking pair for the matching $N=\{(w,{m}_{1}),({w}_{1},m)\}$. We need to concern ourselves only with dangerous paths w.r.t. a matching M that can be extended to a stable matching, i.e., M is such that there exists a stable matching ${M}^{\prime}$ with $M\subseteq {M}^{\prime}$. Therefore, if $P=(w,{m}_{1},{w}_{1},m)$ is a dangerous path w.r.t. M, then w and ${m}_{1}$ do not form a blocking pair for M, which means that ${w}_{1}$ is at least as good for ${m}_{1}$ as w, and similarly, ${w}_{1}$ and m do not form a blocking pair for M, hence ${m}_{1}$ is at least as good for ${w}_{1}$ as m. Since $({m}_{1},{w}_{1})$ is not a blocking pair for the matching $N=\{(w,{m}_{1}),({w}_{1},m)\}$, either ${m}_{1}$ is indifferent between w and ${w}_{1}$ and then we say that P is a **masculine dangerous path**, or ${w}_{1}$ is indifferent between m and ${m}_{1}$ and then we say that P is a **feminine dangerous path**. A path P can of course be both a masculine and a feminine dangerous path.

We also introduce the following terminology.

If man m is matched to woman w and there is at least one free woman ${w}_{1}$ such that w and ${w}_{1}$ are equally good for m, then we say that ${w}_{1}$ is a **satellite** of m and m is **satellitic**.

If woman w is matched to a satellitic man m, then we say that w is **co-satellitic**.

If $e=(m,w)$ is such that w is free and there is at least one free woman ${w}_{1}\ne w$ such that w and ${w}_{1}$ are equally good for m, then e is called **special**, and consequently, $(m,{w}_{1})$ is also called special.

If man m has at least one free woman incident with him, then he is said to be **subsatellitic**.

Woman w matched to a subsatellitic man m and not co-satellitic is said to be **co-subsatellitic w.r.t. ${m}^{\prime}$** if m and ${m}^{\prime}$ are equally good for her.

Figure 1 illustrates the notions introduced above.

Let us notice that if a free man m has a co-satellitic woman w on his list, then it is possible that he belongs to a masculine dangerous path, and analogously, if a free man m has a woman w on his list who is co-subsatellitic with respect to him, then it is possible that he belongs to a feminine dangerous path.

**Figure 1.**
The description of a dangerous path and its two types: a feminine dangerous path and a masculine dangerous path.

**Figure 1.**
The description of a dangerous path and its two types: a feminine dangerous path and a masculine dangerous path.

#### 2.1. Description of Algorithm GS Modified

Algorithm GS Modified given further on is to some extent modeled on the Gale-Shapley algorithm in which men propose to women on their lists and women dispose. In the course of running the algorithm, preference lists ${L}_{m}$ will diminish and some additional lists ${L}_{m}^{\prime}$ will be built. If at some point a free man m has a nonempty list ${L}_{m}$, it means that he has not yet proposed to all women on his list ${L}_{m}$ and potentially belongs to a blocking pair or a masculine dangerous path. If a free man m has a nonempty list ${L}_{m}^{\prime}$, it means that he potentially belongs to a feminine dangerous path.

Whenever it is man m’s turn to propose and ${L}_{m}\ne \varnothing $, he would like to get matched to the best possible woman on his list ${L}_{m}$ without creating a blocking pair (as in GS algorithm) but also ensure that he does not belong to any masculine dangerous path. To this end, m proposes to the woman w to whom he has not yet proposed and who is as high on ${L}_{m}$ as possible. If w is free or matched to someone worse for her than m, she accepts m and rejects her current partner if she had one. If w is co-satellitic, which means that she is matched to some man ${m}^{\prime}$ such that there is a free woman ${w}^{\prime}$ who is equally good for ${m}^{\prime}$ as w, then it means that m currently belongs to a masculine dangerous path $(m,w,{m}^{\prime},{w}^{\prime})$. In this case w does not care whether m is better for her than ${m}^{\prime}$ and accepts him while rejecting ${m}^{\prime}$, and immediately afterwards ${m}^{\prime}$ proposes to ${w}^{\prime}$, who accepts him. This operation can be very well viewed as though ${m}^{\prime}$ proposed to ${w}^{\prime}$ without having proposed to w first, and some time later m proposed to w. (Here edge $({m}^{\prime},w)$ was special at the moment ${m}^{\prime}$ proposed to w for the first time and that’s why if ${m}^{\prime}$ gets rejected by ${w}^{\prime}$, he will propose to w again, because in this case w was not removed from ${L}_{{m}^{\prime}}$. Let us notice that if ${m}^{\prime}$ were allowed to remove w from his list ${L}_{{m}^{\prime}}$ at the moment he proposed to w for the first time and the edge $({m}^{\prime},w)$ was special, then after being rejected by ${w}^{\prime}$ later on, he would not propose to w for the second time, and as a result a blocking pair involving ${m}^{\prime}$ and w might arise.) To avoid multiple operations of this kind concerning one woman, we will assume that given a tie, a man proposes to unmatched women before proposing to matched ones. This ensures that in the above scenario, m cannot become satellitic after being matched with w. If a woman w to whom m proposes is matched to man ${m}^{\prime}$ equally good for her as m, and w is co-subsatellitic w.r.t. m, meaning that ${m}^{\prime}$ has some free women on his list, then at the current moment m belongs to a feminine dangerous path. What happens now is that w rejects m but m adds w to his list ${L}_{m}^{\prime}$. (Woman w does not accept m because m may be subsatellitic as well.) In every other case w rejects m.

If man

m has proposed to all women on his list

${L}_{m}$ and remained free, but his list

${L}_{m}^{\prime}$ is nonempty, he will propose to women on

${L}_{m}^{\prime}$ starting from the top. If he proposes to

w, and

w is matched to some man

${m}^{\prime}$ who is equally good for her as

m, and additionally,

${m}^{\prime}$ is subsatellitic, then

w accepts

m and rejects

${m}^{\prime}$. Otherwise

w rejects him. Let us notice that it cannot happen that

w prefers

m to

${m}^{\prime}$, because man

m had proposed to all women on his list

${L}_{m}$ before proceeding to list

${L}_{m}^{\prime}\subseteq {L}_{m}$. Therefore he must have been rejected by

w at some point, which means that

w must be now matched to someone at least as good for her as

m. (This will also be formally proved in the next section.)

**Algorithm GS Modified**

Each man m’s preference list ${L}_{m}$ is organized in such a way that if ${L}_{m}$ contains a tie t, then free women in t come before matched women in t. At the beginning all women are free and ties on men’s lists are broken arbitrarily, and in the course of running the algorithm, whenever woman w becomes matched for the first time, say to man m, we move her to the end of every tie she belongs to.

**while** there exists a free man m with a nonempty list ${L}_{m}$ or a nonempty list ${L}_{m}^{\prime}$

**if** ${L}_{m}\ne \varnothing $, **then**

$w\leftarrow $ woman at the top of m’ s list ${L}_{m}$

**if** $(m,w)$ is not special, **then** remove w from ${L}_{m}$

**if** w is free, **then** $M\leftarrow M\cup (m,w)$

**else** **if** w is co-satellitic, **then**

let ${w}^{\prime}$ be a satellite of $M\left(w\right)$

**if** $(M\left(w\right),{w}^{\prime})$ is not special, **then** remove ${w}^{\prime}$ from ${L}_{M\left(w\right)}$

$M\leftarrow M\cup \{(m,w),(M\left(w\right),{w}^{\prime})\}$∖$(w,M(w\left)\right)$

**else** **if** w prefers m to $M\left(w\right)$, **then** $M\leftarrow M\cup (m,w)$∖$(w,M(w\left)\right)$

**else** **if** w is co-subsatellitic w.r.t. m, **then** add w to the end of list ${L}_{m}^{\prime}$

**else**

$w\leftarrow $ woman at the top of m’ s list ${L}_{m}^{\prime}$

remove w from ${L}_{m}^{\prime}$

**if** w is co-subsatellitic w.r.t. m, **then** $M\leftarrow M\cup (m,w)$∖$(w,M(w\left)\right)$

First we show how Algorithm GS Modified runs on the following example. Suppose the preference lists of men

${m}_{1},{m}_{2},{m}_{3},{m}_{4}$ and women

${w}_{1},{w}_{2},{w}_{3},{w}_{4}$ are as follows. The brackets indicate ties.

Suppose that man ${m}_{1}$ starts. He proposes to woman ${w}_{1}$ and gets accepted ($({m}_{1},{w}_{1})$ is a special edge and ${w}_{2}$ is a satellite of ${m}_{1}$). Now suppose that it is ${m}_{2}$’s turn to propose. (It might also be ${m}_{3}$ or ${m}_{4}$.) He proposes to woman ${w}_{1}$ and gets accepted, because ${w}_{1}$ is co-satellitic. Man ${m}_{1}$ gets matched with woman ${w}_{2}$. Next ${m}_{3}$ proposes to woman ${w}_{2}$ and gets accepted. Now ${m}_{1}$ proposes to ${w}_{1}$ (as $({m}_{1},{w}_{1})$ was a special edge) and gets accepted. Then ${m}_{2}$ proposes to ${w}_{3}$ and gets accepted. Next ${m}_{4}$ proposes to ${w}_{3}$ and gets rejected, but ${w}_{3}$ is co-subsatellitic w.r.t. ${m}_{4}$, and ${m}_{4}$ adds ${w}_{3}$ to his list ${L}_{{m}_{4}}^{\prime}$. Afterwards ${m}_{4}$ proposes to ${w}_{3}$ again, this time from ${L}_{{m}_{4}}^{\prime}$, and gets accepted. Finally ${m}_{2}$ proposes to ${w}_{4}$ andgets accepted.

## 3. Correctness of Algorithm GS Modified

In this section we prove the correctness of Algorithm GS Modified.

If $w\in {L}_{m}$ and m proposes to w, then we will sometimes say that m proposes from ${L}_{m}$ (to w). If ${L}_{m}=\varnothing ,\phantom{\rule{4pt}{0ex}}w\in {L}_{m}^{\prime}$ and m proposes to w, then we will sometimes say that m proposes from ${L}_{m}^{\prime}$ (to w).

**Lemma 1.** If woman w becomes matched, she will stay matched.

Woman w can become co-satellitic only the first time someone, say m, proposes to her and only if at the time of the proposal edge $(m,w)$ is special. If a co-satellitic woman w receives a proposal, she always accepts it and is no longer co-satellitic.

If woman w is matched to man m and is not co-satellitic, she can accept man ${m}^{\prime}$ only if ${m}^{\prime}$ is at least as good for her as m. Moreover, if ${m}^{\prime}$ is better for her than m, she always accepts him. If ${m}^{\prime}$ is equally good for her as m, then she accepts him only if she is co-subsatellitic w.r.t. ${m}^{\prime}$ and ${m}^{\prime}$ proposes from ${L}_{{m}^{\prime}}^{\prime}$.

If woman w matched to man m is not co-satellitic and changes m for ${m}^{\prime}$, who is equally good for her as m, then m is subsatellitic and ${m}^{\prime}$ is not.

**Proof.** Statements 1 and 3 follow directly from the description of Algorithm GS Modified.

Let us prove statement 2. If w is matched and m proposes to her, then there is no free woman ${w}^{\prime}$ incident with m who is equally good for m as w (because then m would propose to ${w}^{\prime}$ before proposing to w). As a result, if w becomes matched to m, she will not become co-satellitic and she will cease to be co-satellitic if she was before.

It remains to prove statement 4. If w changes m for ${m}^{\prime}$ who is equally good for her as m (and w is not co-satellitic), then by statement 3, ${m}^{\prime}$ proposes from ${L}_{{m}^{\prime}}^{\prime}$ and m is subsatellitic. Man ${m}^{\prime}$ proposing from ${L}_{{m}^{\prime}}^{\prime}$ does not have any free women incident with him. □

**Lemma 2.** Let M denote a matching computed by Algorithm GS Modified. Then the underlying graph does not contain blocking pairs and dangerous paths w.r.t. M.

**Proof.** Suppose that $(m,w)$ is a blocking pair. Man m is either free or $M\left(m\right)$ is worse for him than w. It means that at some point m proposed to w from ${L}_{m}$ when edge $(m,w)$ was not special. (Clearly, at some point m proposed to w from ${L}_{m}$. Assume that at that point edge $(m,w)$ was special. Then w was free and accepted m. However, m got rejected later and therefore proposed to w from ${L}_{m}$ again, when edge $(m,w)$ was no longer special.) If w rejected him then, then by claim 2 of Lemma 1, w was not co-satellitic and, by claim 3 of Lemma 1, was matched to someone at least as good for her as m and thus would have stayed matched to someone as good for her as m. If w accepted m, then after getting matched to m she was not co-satellitic, and by claim 3 of Lemma 1, would have stayed matched to someone at least as good for her as m. Either way we get a contradiction.

Suppose now that the graph contains a masculine dangerous path $({m}^{\prime},w,m,{w}^{\prime})$ such that $m=M\left(w\right)$. Thus m is satellitic and w is co-satellitic. Since she is co-satellitic, it means that the only proposal she ever got was from m, but ${m}^{\prime}$ must have proposed to her too, a contradiction.

Finally, suppose that the graph contains a feminine dangerous path $({m}^{\prime},w,m,{w}^{\prime})$ such that $m=M\left(w\right)$. Thus m is subsatellitic and w is co-subsatellitic w.r.t. ${m}^{\prime}$, also ${m}^{\prime}$ is not subsatellitic. At some point, ${m}^{\prime}$ proposed to w from ${L}_{{m}^{\prime}}$ when $({m}^{\prime},w)$ was not special. If he got accepted at that moment, then later on he could not become rejected, because by claim 3 of Lemma 1, after accepting ${m}^{\prime}$ woman w was not co-satellitic, and by claim 4 of Lemma 1, could not accept a subsatellitic man equally good for her as her current partner. Therefore he was rejected then, and w was already matched with m (by claims 3 and 4 of Lemma 1). Hence w was co-subsatellitic w.r.t. ${m}^{\prime}$ (because m was subsatellitic), and ${m}^{\prime}$ added w to the end of list ${L}_{{m}^{\prime}}^{\prime}$. Thus later ${m}^{\prime}$ proposed to w from ${L}_{{m}^{\prime}}^{\prime}$. According to the algorithm, w would have accepted him and could not later on become matched to someone subsatellitic and equally good for her as ${m}^{\prime}$. A contradiction. □

**Theorem 1.** Algorithm GS Modified computes a stable matching M which is a $\frac{3}{2}$- approximation of the optimal solution. Algorithm GS Modified runs in $O\left(m\right)$ time.

**Proof.** By Lemma 2, matching M computed by Algorithm GS Modified is stable and does not contain dangerous paths. Therefore M is a $\frac{3}{2}$-approximation of the optimal solution.

The running time of the algorithm is proportional to the sum of the lengths of lists ${L}_{m}$ and ${L}_{m}^{\prime}$. By the length of list ${L}_{m}$ or ${L}_{m}^{\prime}$ we mean the number of people on it and not the number of ties. Each edge of ${L}_{m}$ is scanned at most twice–twice only if the first time it was scanned, it was special, and each edge of ${L}_{m}^{\prime}$ is scanned at most once. Checking whether a given woman w is co-satellitic or co-subsatellitic can be done in constant time as follows. Suppose that w is matched to man m. We look at the tie at the top of list ${L}_{m}$. If this tie contains woman w and some free woman ${w}^{\prime}$, then w is co-satellitic, otherwise she is not. (Recall that the free women proceed matched ones in a tie.) To facilitate checking whether a given man is subsatellitic, each man has the counter of free women incident with him, and whenever a woman becomes matched and moves herself to the end of the ties, men decrease their respective counters. The number of movements in the ties is upperbounded by the sum of the lengths of lists ${L}_{m}$ as once a woman becomes matched, she stays so. □

Let us finally make the following remark.

If we break ties and run the classic Gale-Shapley algorithm, then the cardinality of the computed matching depends on the order in which we break ties. Algorithm GS Modified outputs a matching that would have been output by the GS algorithm if ties were broken as follows. Men’s lists would be identical to those at the beginning of Algorithm GS Modified but for one thing: if at some point during running Algorithm GS Modified, man m proposes to a co-satellitic woman w, and as a result m gets matched to w, and w’s partner $M\left(w\right)$ gets matched to his satellite ${w}^{\prime}$, then a tie on ${L}_{M\left(w\right)}$ would be broken in such a way that ${w}^{\prime}$ comes before w. Every tie t on a woman w’s list would be first broken into $({m}_{1},{m}_{2},\dots ,{m}_{s})$ in such a way that ${m}_{1}$ denotes the first man of t to whom w got matched without becoming co-satellitic, and assuming that it happened at some step S, ${m}_{2}$ denotes the first man of t who proposed to w after step S, ${m}_{3}$ denotes the second man of t, who proposed to w after step S and so on. Next we would make the following alterations on women’s lists: if at some point, man m proposes from ${L}_{m}^{\prime}$ to a co-subsatellitic woman w matched to $M\left(w\right)$, then a tie on ${L}_{w}$ would be broken in such a way that m comes before $M\left(w\right)$.

## 4. Extension to Bipartite Stable b-Matchings

Suppose we have a simple bipartite graph $G=(V,E)$, where $V=U\cup W$ and $U,W$ are disjoint sets, and a function b: $V\to \mathbb{N}$. Then a subset $M\subseteq E$ is called a b-matching if for each $v\in V$ it is $de{g}_{M}\left(v\right)\le b\left(v\right)$, where $de{g}_{M}\left(v\right)$ denotes the degree of vertex v in the graph ${G}_{M}=(U\cup W,M)$. We refer to the vertices of U and W as U-agents and W-agents, respectively. Each U-agent u of U has a preference list ${L}_{u}$ of a subset of W-agents, and analogously, each W-agent w has a preference list ${L}_{w}$ of a subset of U-agents. The preference lists are linearly ordered lists of ties. The majority of the terminology for stable matchings goes through for stable b-matchings. Instead of saying that some agent or vertex is free, we will use the term **unsaturated**: agent v is unsaturated in a b-matching M if $de{g}_{M}\left(v\right)<b\left(v\right),$ and if $de{g}_{M}\left(v\right)=b\left(v\right)$, then we will say that v is **saturated**. For any agent v we will denote the set $\{w\in U\cup W$:$(v,w)\in M\}$ by $M\left(v\right)$. A pair $(u,w)$ is **blocking** for a b-matching M if (1) u and w are mutually acceptable; (2) u is unsaturated or prefers w to one of the W-agents of $M\left(u\right)$; and (3) w is unsaturated or prefers u to one of the U-agents of $M\left(w\right)$. A b-matching M is said to be stable if it does not admit a blocking pair. As previously, we are interested in finding a stable b-matching of largest size. Let us also note that if for each u in U we have $b\left(u\right)=1$, then the problem is known under the name Hospitals-Residents problem or one-to-many stable matching problem.

Alternating paths and cycles are defined for b-matchings in an analogous way as for matchings, but we do not require paths and cycles to be simple, i.e., an **alternating path** P **w.r.t.** a b-matching M is defined as any sequence of edges $({v}_{1},{v}_{2}),({v}_{2},{v}_{3}),\dots ,({v}_{k-1},{v}_{k})$ such that the edges alternate between M-edges and edges of E∖M, and an **alternating cycle** C **w.r.t. M** is defined as an alternating path (w.r.t. M) that ends and begins with the same vertex, i.e., the sequence of edges has the form $({v}_{1},{v}_{2}),({v}_{2},{v}_{3}),\dots ,({v}_{k},{v}_{1})$. As before, for any two b-matchings M and ${M}^{\prime}$, the symmetric difference $M\oplus {M}^{\prime}$ can be partitioned into maximal alternating paths and cycles (i.e., no two alternating paths from the partition can be combined to form one alternating path). A given stable b-matching M might not be a 3/2-approximation of ${M}_{opt}$, where ${M}_{opt}$ denotes a stable b-matching of maximum size, only if the underlying graph contains a dangerous path defined as follows. If M is a stable b-matching, then a path $P=(w,{u}_{1},{w}_{1},u)$ is called **dangerous with respect to M** if u and ${u}_{1}$ are U-agents, w and ${w}_{1}$ are W-agents, $({u}_{1},{w}_{1})$ is in M, $(w,{u}_{1}),({w}_{1},u)$ are not in M, w and u are unsaturated in M, ${u}_{1}$ and ${w}_{1}$ are saturated in M, and $({u}_{1},{w}_{1})$ is not a blocking pair for the b-matching ${M}^{\prime}=(M\setminus ({u}_{1},{w}_{1}))\cup \{(w,{u}_{1}),({w}_{1},u)\}$. Since $(w,{u}_{1})$ is not blocking for M, w is not better for ${u}_{1}$ than any of the W-agents currently belonging to $M\left({u}_{1}\right)$, and analogously, u is not better for ${w}_{1}$ than any of the U-agents currently belonging to $M\left({w}_{1}\right)$. Thus if P is dangerous, then either w and ${w}_{1}$ are equally good for ${u}_{1}$ and then P is called a **masculine dangerous path**, or u and ${u}_{1}$ are equally good for ${w}_{1}$ and then P is called a **feminine dangerous path**.

An approximation algorithm for finding stable

b-matchings is constructed analogously to the algorithm for finding stable matchings.

U-agents play the role of men and

W-agents play the role of women. For convenience, we shall refer to a

U-agent as "he" and to a

W-agent as "she". We adapt the terminology from the one-to-one setting to the current one as follows.

If a U-agent u is in $M\left(w\right)$, and there is at least one unsaturated W-agent ${w}_{1}\notin M\left(u\right)$ different from w such that w and ${w}_{1}$ are equally good for u, then we say that ${w}_{1}$ is a **satellite of u w.r.t. w** and u is **satellitic w.r.t. w**.

W-agent w belonging to $M\left(u\right)$ such that u is satellitic w.r.t. w is said to be **co-satellitic**.

If $e=(u,w)$ is such that w is unsaturated and there is at least one unsaturated W-agent ${w}_{1}\ne w$ such that w and ${w}_{1}$ are equally good for u, then e is called **special**, and consequently, $(u,{w}_{1})$ is also called special.

If a U-agent u has at least one unsaturated W-agent $w\notin M\left(u\right)$ incident with him, then he iscalled **subsatellitic**.

A saturated and not co-satellitic W-agent $w\in M\left(u\right)$ such that u is subsatellitic is said to be **co-subsatellitic w.r.t. ${u}^{\prime}$** if ${u}^{\prime}\notin M\left(w\right)$ and u and ${u}^{\prime}$ are equally good for her.

By a **worst U-agent belonging to $M\left(w\right)$** we will mean any U-agent $u\in M\left(w\right)$ such that there is no other U-agent ${u}^{\prime}\in M\left(w\right)$ who is worse for w than u.

**Algorithm ASBM** (short for Approximate Stable b-Matching)

Each U-agent u’s preference list ${L}_{u}$ is organized in such a way that if ${L}_{u}$ contains a tie t, then unsaturated W-agents in t come before saturated W-agents in t. At the beginning, all W-agents are unsaturated and ties on U-agents’s lists are broken arbitrarily, and in the course of running the algorithm, whenever W-agent w becomes saturated for the first time, we move her to the end of every tie she belongs to.

**while** there exists an unsaturated U-agent u with a nonempty list ${L}_{u}$ or a nonempty list ${L}_{u}^{\prime}$

**if** ${L}_{u}\ne \varnothing $, **then**

$w\leftarrow $W-agent at the top of u’ s list ${L}_{u}$

**if** $(u,w)$ is not special, **then** remove w from ${L}_{u}$

**if** w is unsaturated, **then** $M\leftarrow M\cup (u,w)$

**else** **if** w is co-satellitic, **then**

let ${w}^{\prime}$ be a satellite of a U-agent ${u}^{\prime}\in M\left(w\right)$ w.r.t. w

**if** $({u}^{\prime},{w}^{\prime})$ is not special, **then** remove ${w}^{\prime}$ from ${L}_{{u}^{\prime}}$

$M\leftarrow M\cup \{(u,w),({u}^{\prime},{w}^{\prime})\}$∖$(w,{u}^{\prime})$

**else** **if** w prefers u to a worst U-agent in $M\left(w\right)$, **then**

let ${u}^{\prime}$ denote any worst U-agent belonging to $M\left(w\right)$

$M\leftarrow M\cup (u,w)$∖$(w,{u}^{\prime})$

**if** w is co-subsatellitic w.r.t. ${u}^{\prime}$, **then** add w to the end of list ${L}_{{u}^{\prime}}^{\prime}$

**else** **if** w is co-subsatellitic w.r.t. u, **then** add w to the end of list ${L}_{u}^{\prime}$.

**else**

$w\leftarrow $ W-agent at the top of u’ s list ${L}_{u}^{\prime}$

remove w from ${L}_{u}^{\prime}$

**if** w is co-subsatellitic w.r.t. u, **then**

let ${u}^{\prime}$ denote a subsatellitic U-agent in $M\left(w\right)$ equally good for w as u

$M\leftarrow M\cup (u,w)$∖$(w,{u}^{\prime})$

**if** w is co-subsatellitic w.r.t. ${u}^{\prime}$, **then** add w to the end of ${L}_{{u}^{\prime}}^{\prime}$

## 5. Correctness of Algorithm ASBM

The correctness of Algorithm ASBM is proved in a very similar way as the correctness of Algorithm GS Modified.

**Lemma 3.** If a W-agent becomes saturated, she will stay saturated.

A co-satellitic W-agent w accepts every proposal. Once a saturated W-agent is not co-satellitic, she cannot become co-satellitic later.

A W-agent $w\in M\left(u\right)$ can reject u only if w is saturated and a) u is satellitic w.r.t. w (w is co-satellitic), or b) w is not co-satellitic, u is one of the worst U-agents belonging to $M\left(w\right)$, and w receives a proposal from ${u}^{\prime}$ who is better for w than u, or c) w is not co-satellitic and u is one of the worst U-agents belonging to $M\left(w\right)$, u is subsatellitic, and w is co-subsatellitic w.r.t. ${u}^{\prime}$ who proposes from ${L}_{{u}^{\prime}}^{\prime}$

A saturated W-agent w who is not co-satellitic can accept a U-agent ${u}^{\prime}$ only if ${u}^{\prime}$ is at least as good for w as a worst U-agent $u\in M\left(w\right)$; moreover if ${u}^{\prime}$ is equally good for w as u, then w accepts ${u}^{\prime}$ only if w is co-subsatellitic w.r.t. ${u}^{\prime}$ and ${u}^{\prime}$ proposes from ${L}_{{u}^{\prime}}^{\prime}$.

The proof of Lemma 3 is very similar to that of Lemma 1 and follows directly from the description of Algorithm ASBM.

**Theorem 2.** Let M denote a b-matching computed by Algorithm ASBM. Then M is a $3/2$-approximation of an optimal stable b-matching.

**Proof.** We will show that the underlying graph does not contain blocking pairs and dangerous paths with respect to M. The non-existence of a dangerous path for the stable matching found guarantees the approximation ratio $3/2$.

Suppose that $(u,w)$ is a blocking pair. Agent u is either unsaturated or there exists ${w}^{\prime}\in M\left(u\right)$ worse for u than w. It means that at some point u proposed to w from ${L}_{u}$ when edge $(u,w)$ was not special. If u’s proposal to w was rejected, then at that point w was saturated and not co-satellitic, and the worst ${u}^{\prime}\in M\left(w\right)$ was at least as good as u for w (by Lemma 3), and thus (also by claim 4 of Lemma 3) w could not later become matched to some ${u}^{\prime \prime}$ who is worse for w than u. Therefore u got accepted then and later got rejected. Since at the moment of that proposal edge $(u,w)$ was not special, u was not satellitic w.r.t. w (and clearly could not become satellitic later.) By claim 3 of Lemma 3, W-agent w was not co-satellitic at the moment of rejecting u, and the worst U-agent belonging to $M\left(w\right)$ was u. Therefore by claim 4 of Lemma 3, w could not later become matched to some ${u}^{\prime \prime}$ who is worse for her than u. A contradiction.

Suppose now that the graph contains a masculine dangerous path $({u}^{\prime},w,u,{w}^{\prime})$ such that $u\in M\left(w\right)$. Thus u is satellitic w.r.t. w and w is co-satellitic. It means that at some point ${u}^{\prime}$ proposed to w from ${L}_{{u}^{\prime}}$ when edge $({u}^{\prime},w)$ was not special. Then w was either co-satellitic, because she is co-satellitic now, or unsaturated. Therefore ${u}^{\prime}$ got accepted. Later on he was clearly rejected. However, by claim 3 ofLemma 3 and the description of the algorithm ABSM, this is impossible, because a co-satellitic w rejects only U-agents who are satellitic w.r.t. w.

Finally, suppose that the graph contains a feminine dangerous path $({u}^{\prime},w,u,{w}^{\prime})$ such that $u\in M\left(w\right)$. Thus w is co-subsatellitic w.r.t. ${u}^{\prime}$, and u is subsatellitic. At some point ${u}^{\prime}$ proposed to w from ${L}_{{u}^{\prime}}$ when edge $({u}^{\prime},w)$ was not special. If he got rejected, then w was not co-satellitic, and a worst U-agent ${u}^{\prime \prime}\in M\left(w\right)$ was equally good for her as ${u}^{\prime}$ and hence as u. By the description of the algorithm, at that point w was co-subsatellitic w.r.t. ${u}^{\prime}$, and ${u}^{\prime}$ added w to the end of list ${L}_{{u}^{\prime}}^{\prime}$. If he was accepted (i.e., when he was proposing from ${L}_{{u}^{\prime}}$ and edge $({u}^{\prime},w)$ was not special), then later he was rejected, and by the description of the algorithm, he also must have added w to the end of list ${L}_{{u}^{\prime}}^{\prime}$. When ${u}^{\prime}$ proposed to w from ${L}_{{u}^{\prime}}^{\prime}$, w was still co-subsatellitic w.r.t. ${u}^{\prime}$ (because w is co-subsatellitic w.r.t. ${u}^{\prime}$ now), hence ${u}^{\prime}$ was accepted (because ${u}^{\prime}$ proposing from ${L}_{{u}^{\prime}}^{\prime}$ is not subsatellitic) and could not have got rejected later if there were still subsatellitic U-agents matched with w who were equally good as ${u}^{\prime}$ for w. A contradiction. □

## 6. Data Structures and Running Time

Each agent a (either a U-agent or a W-agent) has a preference list ${L}_{a}$, which is a list of lists, i.e., we have a list for each tie. For each list we have access to both its first and last element.

Each agent has a pointer to their position in every tie they belong to (here a 1-element list is also considered a tie). Whenever W-agent w gets saturated for the first time, w goes over her whole list ${L}_{w}$ and moves herself to the end of every tie she belongs to, as explained in the description of Algorithm ASBM. This operation takes $O\left(\right|{L}_{w}\left|\right)$ time.

Every

W-agent

w stores information about

U-agents currently belonging to

$M\left(w\right)$ in a priority queue.

U-agents belonging to

$M\left(w\right)$ who are equally good for

w are kept in one list, thus the priority queue contains lists. Each such list is associated with a priority value, which is the position the

U-agents on that list have on the initial list

${L}_{w}$,

i.e., the

U-agents that are at the top tie of list

${L}_{w}$ have priorityequal to 1. We implement a priority queue as a balanced tree with an additional pointer to the maximum element. We will use the following operations:

finding the maximum element (takes $O\left(1\right)$ time)

deleting a given element (takes $O(logn)$ time, where n denotes the number of elements in apriority queue)

inserting a given element (takes $O(logn)$ time)

finding an element having a certain priority value (takes $O(logn)$ time).

After deleting or inserting an element we update the pointer to the maximum element, which takes $O(logn)$ time.

Each U-agent u keeps track of W-agents currently belonging to $M\left(u\right)$ in a list.

Each U-agent u has the counter of the number of unsaturated W-agents incident with him, and whenever a saturated W-agent moves herself to the end of the ties, U-agents also decrease respective counters. Therefore checking if u is subsatellitic takes constant time.

Each W-agent w has a separate list ${S}_{w}$ of satellitic U-agents w.r.t. w belonging to $M\left(w\right)$. Every time w gets matched to some new U-agent u who is satellitic with respect to w, she adds u to ${S}_{w}$. When we want to check if w is co-satellitic, we go over ${S}_{w}$ and for each $u\in {S}_{w}$ check if u is still satellitic w.r.t. w. Checking if a U-agent u is satellitic w.r.t. w can be done as follows. W-agent w has a pointer to her position in a tie t on ${L}_{u}$. We also have access to the first element of t. If the first element of t is unsaturated, then it means that u is satellitic w.r.t. w. This way we can also find a satellite of a satellitic U-agent in $M\left(w\right)$. If u is not satellitic w.r.t. w, we remove u from ${S}_{w}$, otherwise we do an appropriate exchange. Once u is removed from ${S}_{w}$, he will not be added to ${S}_{w}$ again. It is so since once u has no unsaturated W-agents equally good for him as w on his list, it will stay so. Hence the overall time Algorithm ASBM spends on ${S}_{w}$ is $O\left(\right|{L}_{w}\left|\right)$.

Each list describing a tie within the priority queue of U-agents belonging to $M\left(w\right)$ is organized in such a way that subsatellitic U-agents proceed U-agents that are not subsatellitic. Whenever a U-agent u ceases to be subsatellitic, we move him to the end of every list in every priority queue he is in. Moving u to the end of every such list takes $O\left(\right|M\left(u\right)\left|\right)$ time. Every u ceases to be subsatellitic at most once in the course of running the algorithm. Therefore the total time spent on moving U-agents who ceased to be subsatellitic to the ends of priority queues does not exceed $O\left(m\right)$. This way, to see if w is co-subsatellitic w.r.t. u, we look at the list in the priority queue containing U-agents in $M\left(w\right)$ that are equally good for w as u and see if the first U-agent on this list is subsatellitic.

Each U-agent u makes a proposal to every W-agent on ${L}_{u}$ at most twice and to every W-agent on ${L}_{u}^{\prime}$ at most once. The second statement follows from the observation that whenever an agent u proposes from ${L}_{u}^{\prime}$, he does not have any unsaturated W-agents on ${L}_{u}^{\prime}$. After each such proposal, Algorithm ASBM performs at most two insertions and at most one deletion on a suitable priority queue. Each insertion and each deletion into a priority queue containing U-agents belonging to $M\left(w\right)$ takes at most $O(logb(w\left)\right)$ time.

Summing all the arguments together, we get that the running time of Algorithm ASBM is $O(mmax\{1,logmax\left\{b\right(w):w\in W\}\left\}\right)$, where m denotes the number of edges in G. If $max\left\{b\right(u):u\in U\}<max\left\{b\right(w):w\in W\}$, then we can swap the roles of U-agents and W-agents. Therefore we can state the following.

**Theorem 3.** The running time of Algorithm ASBM is $O(mmax\{1,logc\left\}\right)$, where $c=min\left\{max\right\{b\left(v\right):v\in U\},max\{b\left(v\right):v\in W\left\}\right\}$ and m denotes the number of the edges.