Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be882e8

Browse files
Few things about graph interpretation of Hungarian algorithm
1 parent 55eecd1 commit be882e8

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

‎OptimalTransportWasserteinDistance.ipynb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,41 @@
261261
"* $b$ is the definition of the constraints: $b = \\begin{pmatrix} m_0 \\\\ \\vdots \\\\ m_{N-1} \\\\ h_0 \\\\ \\vdots \\\\ h_{K-1} \\end{pmatrix}$"
262262
]
263263
},
264+
{
265+
"cell_type": "markdown",
266+
"metadata": {},
267+
"source": [
268+
"## Few words about hungarian algorithm\n",
269+
"Most informations from there are coming from the [wikipedia article](https://en.wikipedia.org/wiki/Hungarian_algorithm).\n",
270+
"Let's first recall our first formulation of the discrete optimal transport problem:\n",
271+
"\\begin{align*}\n",
272+
" \\hat{T} = \\underset{\\Gamma \\in \\mathbb{R}^{N\\times K+}}{argmin} \\sum \\Gamma_{nk} c(x_n, y_k)\n",
273+
"\\end{align*}\n",
274+
"Where one seek for the optimal transport matrix, note we abstracted away the marginal constraints for the transportation matrix $\\Gamma$ here.\n",
275+
"\n",
276+
"### Matrix formulation\n",
277+
"Another very close probem, is the one solved by the Hungarian algorithm:\n",
278+
"In the matrix formulation, we are given a nonnegative n×ばつn matrix, where the element in the i-th row and j-th column represents the cost of assigning the j-th job to the i-th worker. We have to find an assignment of the jobs to the workers, such that each job is assigned to one worker and each worker is assigned one job, such that the total cost of assignment is minimum.\n",
279+
"\n",
280+
"This can be expressed as permuting the rows of a cost matrix C to minimize the trace of a matrix,\n",
281+
"\\begin{align*}\n",
282+
" \\underset{P}{min} \\; Tr(PC)\n",
283+
"\\end{align*}\n",
284+
"where P is a permutation matrix.\n",
285+
"\n",
286+
"### Bipartite graph formulation\n",
287+
"The definition simply reads as follow: We have a complete bipartite graph $G=(S, T; E)$ with n worker vertices $S$ and $n$ job vertices $T,ドル and the edges $E$ each have a nonnegative cost $c(i,j)$. We want to find a perfect matching with a minimum total cost.\n",
288+
"\n",
289+
"#### Complete bipartite graph\n",
290+
"The algorithm can equivalently be described by formulating the problem using a complete bipartite graph. We recall that a bipartite graph, or a biclique is a special kind of bipartite graph where every vertex of the first set is connected to every vertex of the second set. More formally, a complete bipartite graph is a graph whose vertices can be partitioned into two subsets $V_1$ and $V_2$ such that no edge has both endpoints in the same subset, and every possible edge that could connect vertices in different subsets is part of the graph. That is, it is a bipartite graph $(V_1, V_2, E)$ such that for every pair of vertices $v_1 \\in V_1$ and $v_2 \\in V_2,ドル $v_1 v_2$ is an edge in $E$. A complete bipartite graph with partitions of size $\\|V1\\| = m$ and $\\|V2\\| = n,ドル is denoted $K_{m,n}$ every two graphs with the same notation are isomorphic.\n",
291+
"\n",
292+
"#### Perfect matching\n",
293+
"In graph theory, a perfect matching in a graph is a matching that covers every vertex of the graph. More formally, given a graph $G = (V, E),ドル a perfect matching in $G$ is a subset $M$ of edge set $E,ドル such that every vertex in the vertex set $V$ is adjacent to exactly one edge in $M$.\n",
294+
"\n",
295+
"### Example\n",
296+
"https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html"
297+
]
298+
},
264299
{
265300
"cell_type": "markdown",
266301
"metadata": {},
@@ -344,6 +379,36 @@
344379
"A = np.random.uniform(0,100,(N,N))"
345380
]
346381
},
382+
{
383+
"cell_type": "code",
384+
"execution_count": 7,
385+
"metadata": {},
386+
"outputs": [],
387+
"source": [
388+
"# Lightning fast version with jax-ott, see https://github.com/ott-jax/ott\n",
389+
"import jax\n",
390+
"import jax.numpy as jnp\n",
391+
"\n",
392+
"from ott.geometry import pointcloud\n",
393+
"from ott.problems.linear import linear_problem\n",
394+
"from ott.solvers.linear import sinkhorn\n",
395+
"\n",
396+
"# sample two point clouds and their weights.\n",
397+
"rngs = jax.random.split(jax.random.PRNGKey(0), 4)\n",
398+
"n, m, d = 12, 14, 2\n",
399+
"x = jax.random.normal(rngs[0], (n,d)) + 1\n",
400+
"y = jax.random.uniform(rngs[1], (m,d))\n",
401+
"a = jax.random.uniform(rngs[2], (n,))\n",
402+
"b = jax.random.uniform(rngs[3], (m,))\n",
403+
"a, b = a / jnp.sum(a), b / jnp.sum(b)\n",
404+
"# Computes the couplings using the Sinkhorn algorithm.\n",
405+
"geom = pointcloud.PointCloud(x, y)\n",
406+
"prob = linear_problem.LinearProblem(geom, a, b)\n",
407+
"\n",
408+
"solver = sinkhorn.Sinkhorn()\n",
409+
"out = solver(prob)"
410+
]
411+
},
347412
{
348413
"cell_type": "markdown",
349414
"metadata": {},

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /