{"id":444567,"date":"2025-01-09T15:01:05","date_gmt":"2025-01-09T15:01:05","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=444567"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=444567","title":{"rendered":"<span>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/41d\/326\/a0e\/41d326a0ee36cf0e8f2f05162ad56659.png\" width=\"1426\" height=\"802\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/41d\/326\/a0e\/41d326a0ee36cf0e8f2f05162ad56659.png\"\/><\/figure>\n<h3>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439\u00a0<a href=\"https:\/\/habr.com\/ru\/articles\/866202\/\" rel=\"noopener noreferrer nofollow\">\u0437\u0430\u043c\u0435\u0442\u043a\u0435<\/a>\u00a0\u044f \u043a\u043e\u0441\u043d\u0443\u043b\u0441\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0435\u0440\u0430 (TSP). \u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u043e\u0431\u044a\u0435\u043c\u043d\u044b\u043c \u0438 \u0441\u0443\u043d\u0443\u0442\u044c \u0442\u0443\u0434\u0430 \u0435\u0449\u0435 \u043e\u0434\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0431\u044b\u043b\u043e \u0431\u044b \u0447\u0440\u0435\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c\u044e. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0441\u0447\u0438\u0442\u0430\u044e \u0432\u0430\u0436\u043d\u044b\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0431 \u043e\u0434\u043d\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u043e\u0441\u0438\u0442 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 &#8212;\u00a0<em>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>. \u041f\u0440\u0438\u0447\u0438\u043d\u044b, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043e\u0431 \u044d\u0442\u043e\u043c \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435:<\/p>\n<ol>\n<li>\n<p>\u0420\u0435\u0447\u044c \u0438\u0434\u0435\u0442 \u043f\u0440\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u043f\u0440\u0438 \u043e\u0446\u0435\u043d\u043a\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0441\u0435\u0442\u043a\u0430\u043c\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435\u00a0<a href=\"https:\/\/towardsdatascience.com\/transpormer-a-transformer-network-for-the-travelling-salesman-problem-154bd33c37b0\" rel=\"noopener noreferrer nofollow\">TranSPormer: A Transformer Network for the Travelling Salesman Problem<\/a>\u00a0\u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0430\u0437\u0432\u0430\u043d \u0432 \u0447\u0435\u0441\u0442\u044c \u0440\u0443\u0441\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430 \u0432 \u0440\u0443\u0441\u0441\u043a\u043e\u044f\u0437\u044b\u0447\u043d\u043e\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0435 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0439 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443, \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c\u00a0<a href=\"http:\/\/nas1.math.nsc.ru\/aim\/journals\/us\/us17\/us17_007.pdf\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044e \u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/a>\u00a0\u043e\u0442 1978 \u0438 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0435 \u0432 \u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u0440\u0430\u0441\u0438\u0432. \u041f\u043e\u043d\u0438\u043c\u0430\u044e, \u0447\u0442\u043e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u044d\u0441\u0442\u0435\u0442\u0438\u043a\u0430 \u2013 \u044d\u0442\u043e \u043d\u0435\u0447\u0442\u043e \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0435 \u0432\u0435\u0449\u0435\u0439 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0435 \u0441\u0443\u0435\u0442\u043b\u0438\u0432\u043e\u043c\u0443 \u0432\u0437\u043e\u0440\u0443, \u043d\u043e \u0432\u0435\u0440\u044e, \u0447\u0442\u043e \u0438 \u0442\u0430\u043a\u0430\u044f \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u044f \u043a\u0440\u0430\u0441\u043e\u0442\u044b \u043d\u0430\u0439\u0434\u0435\u0442 \u0441\u0432\u043e\u0435\u0433\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u0445\u0443\u0436\u0435 3\/2 \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438 \u0434\u0430\u043d\u043d\u043e\u0435 \u043d\u0435\u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u0433\u043e \u0434\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0411\u0435\u0437\u0443\u0441\u043b\u043e\u0432\u043d\u043e, 3\/2 \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u2013 \u0437\u0432\u0443\u0447\u0438\u0442 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u043d\u043e, \u043d\u043e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u0435\u0441\u044c\u043c\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u043e, \u0447\u0442\u043e \u044f \u0438 \u043d\u0430\u043c\u0435\u0440\u0435\u043d \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0438\u0436\u0435 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u0430\u043f\u0430 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p><em>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>\u00a0\u0441\u0442\u0440\u043e\u0438\u0442 \u0441\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u0431\u0430\u0437\u0435 \u0442\u0435\u043e\u0440\u0438\u0438 \u0433\u0440\u0430\u0444\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u0433\u0440\u0430\u0444\u0430\u043c\u0438 \u0438 \u0434\u043b\u044f R \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432 \u0432\u0438\u0434\u0435 \u043f\u0430\u043a\u0435\u0442\u0430\u00a0<a href=\"https:\/\/r.igraph.org\/\" rel=\"noopener noreferrer nofollow\">igraph<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 \u0421.\u00a0<code>igraph<\/code>\u00a0\u0438\u043c\u0435\u0435\u0442 \u0442\u0430\u043a\u0436\u0435 \u043f\u0430\u043a\u0435\u0442\u044b-\u043e\u0431\u0435\u0440\u0442\u043a\u0438 \u043a \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u043c \u044f\u0437\u044b\u043a\u0430\u043c, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043a Python. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0441\u043e\u043b\u0432\u0435\u0440 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u201c\u0442\u043e\u043d\u043a\u043e\u0439\u201d \u0438 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0448\u0443\u0441\u0442\u0440\u043e\u0439 \u043e\u0431\u0435\u0440\u0442\u043a\u043e\u0439 \u0434\u043b\u044f R \u0438 \u0431\u0435\u043a\u0435\u043d\u0434\u043e\u043c \u043d\u0430 \u0421. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0446\u0430\u0440\u044c-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443 \u2013\u00a0<a href=\"https:\/\/investcookies.ru\/post\/prostye_evristiki_dla_tsp\/#concorde\" rel=\"noopener noreferrer nofollow\"><em>Concorde<\/em><\/a>.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d, \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 16 \u0433\u043e\u0440\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0435 \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0432 \u044d\u0442\u043e\u0439 \u0434\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u0435\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"r\">library(igraph) # \u043f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0433\u0440\u0430\u0444\u0430\u043c\u0438 library(tidyverse) # \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 library(lpSolve) # \u043f\u0430\u043a\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0438 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438   # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 ai_optimization, \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 github \"https:\/\/raw.githubusercontent.com\/dkibalnikov\/ai_optimization\/refs\/heads\/main\/algorithms\/functions.R\"|&gt;   source()  # \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0435\u0440\u0430 n_cities &lt;- 16  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0437 \u043f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0438 cities &lt;- generate_task()  # \u0414\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 cities_df &lt;- as.data.frame(cities) |&gt;    transform(n=seq_len(n_cities))   # \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 dist_mtrx &lt;- as.matrix(dist(cities))  # \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 G &lt;- graph_from_adjacency_matrix(dist_mtrx, mode='undirected', weighted = TRUE)  # \u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0433\u0440\u0430\u0444\u0438\u043a \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 p0 &lt;- ggplot(cities_df, aes(x, y)) +    geom_point() +    geom_point(data = data.frame(n=1, x=0, y=0), shape = 21, size = 5, col = \"firebrick\") +   geom_text(aes(label = n), nudge_x = 3, nudge_y = 3)  # \u0414\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 full_df &lt;- igraph::as_data_frame(G) |&gt;    mutate(across(1:2, as.integer)) |&gt;   left_join(cities_df, by=c(\"from\"=\"n\")) |&gt;    left_join(cities_df, by=c(\"to\"=\"n\"), suffix = c(\"_from\", \"_to\"))  # \u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f   p0 +    geom_segment(data=full_df, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), alpha =.1) +    labs(title = \"\u041f\u043e\u043b\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0434\u043b\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430\")<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/47b\/6e7\/fbb\/47b6e7fbb53bbd6defd4cd636ef075a8.png\" width=\"1440\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/47b\/6e7\/fbb\/47b6e7fbb53bbd6defd4cd636ef075a8.png\"\/><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043a\u043e\u0434\u0430 \u0432\u044b\u0448\u0435, \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u044b\u043b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d \u0432 \u043f\u043e\u043b\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u043d\u0443\u043b\u0435\u0432\u044b\u043c \u044d\u0442\u0430\u043f\u043e\u043c\u00a0<em>\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>. \u041a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u0430 \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u0440\u0435\u0431\u0435\u0440 \u0431\u0435\u0437 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f (\u043e\u0440\u0438\u0435\u043d\u0442\u0430\u0446\u0438\u0438).<\/p>\n<h3>1. \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e<\/h3>\n<p>\u041d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c\u00a0<em>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e<\/em>\u00a0(Minimum Spanning Tree), \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u043e\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435. \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u2013 \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u0434\u043e\u0440\u043e\u0433 \u043c\u0435\u0436\u0434\u0443 \u0433\u043e\u0440\u043e\u0434\u0430\u043c\u0438, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0433\u043e\u0440\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u043b\u0438 \u043e\u043f\u043e\u0441\u0440\u0435\u0434\u043e\u0432\u0430\u043d\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0434\u0440\u0443\u0433\u043e\u0439 \u0433\u043e\u0440\u043e\u0434, \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0436\u0435\u043b\u0430\u043d\u0438\u0435 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u043e\u0439 \u0434\u043e\u0440\u043e\u0436\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u043e\u0442\u043d\u0430.\u00a0<em>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e<\/em>\u00a0\u0434\u043b\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"r\"># \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e MST &lt;- mst(G) # \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438  # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 odd_vertices &lt;- V(MST)[degree(MST) %% 2 == 1] |&gt; as.integer()  # \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e n &lt;- length(odd_vertices)  # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438  MST_df &lt;- igraph::as_data_frame(MST) |&gt;    mutate(across(1:2, as.integer)) |&gt;   left_join(cities_df, by=c(\"from\"=\"n\")) |&gt;    left_join(cities_df, by=c(\"to\"=\"n\"), suffix = c(\"_from\", \"_to\"))   # \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f  p0 +    geom_segment(data=full_df, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), alpha =.05) +   geom_segment(data=MST_df, alpha =.6,                 aes(x = x_from, y = y_from, xend = x_to, yend = y_to, color=\"\u041e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\")) +    geom_point(data = cities[odd_vertices,], aes(col=\"\u041d\u0435\u0447\u0435\u0442\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\"), size = 5, alpha = .5)  +    scale_color_manual(values = c(\"\u041d\u0435\u0447\u0435\u0442\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\"=\"firebrick\", \"\u041e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\"=\"white\"))+   labs(title = \"\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e\", col=\"\") +    theme(legend.position = \"bottom\")<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/137\/b74\/ed8\/137b74ed8be5affa0f7d9c01e815d82d.png\" width=\"1440\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/137\/b74\/ed8\/137b74ed8be5affa0f7d9c01e815d82d.png\"\/><\/figure>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f\u00a0<em>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430<\/em>, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u00a0<code>igraph<\/code>\u00a0\u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u044b\u0431\u043e\u0440 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043d\u043e \u0432\u0430\u0436\u043d\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0434\u0440\u0443\u0433\u043e\u0435, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u2013 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0447\u0435\u043d\u044c \u043d\u0435\u0434\u043e\u0440\u043e\u0433\u043e.<\/p>\n<h3>2. \u0412\u044b\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e<\/h3>\n<p>\u041d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435 \u0432\u044b\u0448\u0435 \u043a\u0438\u0440\u043f\u0438\u0447\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c \u0441\u0440\u0430\u0437\u0443 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e. \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u0430\u043a\u0438\u0445 \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0432\u044b\u044f\u0441\u043d\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u2013 \u0447\u0435\u0442\u043d\u043e \u0438 \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0435. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0442\u0440\u043e\u0433\u0430\u044f\u00a0<em>\u041b\u0435\u043c\u043c\u0430 \u043e \u0440\u0443\u043a\u043e\u043f\u043e\u0436\u0430\u0442\u0438\u044f\u0445<\/em>, \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043b\u044e\u0431\u043e\u0439 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0438\u043c\u0435\u0435\u0442 \u0447\u0435\u0442\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u0441\u0442\u0435\u043f\u0435\u043d\u0435\u0439. \u042d\u0442\u043e \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u0432\u0430\u0436\u043d\u0430 \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0432\u0441\u0435 \u0431\u0435\u0437 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439.<\/p>\n<h3>3. \u041f\u043e\u0438\u0441\u043a \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u043f\u0430\u0440<\/h3>\n<p>\u0412 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0435 \u044d\u0442\u043e\u0442 \u044d\u0442\u0430\u043f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0437\u0432\u0443\u0447\u0438\u0442 \u043a\u0430\u043a:\u00a0<em>\u201c\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u043e\u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0435\u0441\u0430 M \u0432 \u043f\u043e\u0434\u0433\u0440\u0430\u0444\u0435\u201d<\/em>\u00a0(Minimum Weight Perfect Matching). \u0423\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u0430\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0430 \u2013 \u044d\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u0433\u0430\u044f \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c, \u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043c\u0430\u043b\u043e \u043f\u0440\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u0447\u0442\u043e \u0436\u0435 \u0432 \u0438\u0442\u043e\u0433\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0438\u0434\u0435\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u2013 \u043d\u0443\u0436\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0442\u0430\u043a\u0438\u0445 \u0441\u0432\u044f\u0437\u0435\u0439 \u0431\u044b\u043b\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c. \u0418\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0431\u0443\u0434\u0435\u0442 \u043d\u0443\u0436\u0435\u043d \u043f\u0430\u043a\u0435\u0442\u00a0<code>lpSolve<\/code>. \u0424\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ol>\n<li>\n<p><strong>\u0426\u0435\u043b\u0435\u0432\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f:<\/strong>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"\\sum d_{ij}x_{ij} \\rightarrow min\" alt=\"\\sum d_{ij}x_{ij} \\rightarrow min\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/55d\/d7e\/242\/55dd7e24269c10afbc12908d96a874d6.svg\" width=\"178\" height=\"37\"\/>, \u0433\u0434\u0435\u00a0<img decoding=\"async\" class=\"formula inline\" source=\"d\" alt=\"d\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/formulas\/8\/82\/827\/8277e0910d750195b448797616e091ad.svg\" width=\"auto\" height=\"auto\"\/>\u00a0\u2013 \u044d\u0442\u043e \u0434\u043b\u0438\u043d\u0430 \u0440\u0435\u0431\u0440\u0430 \u0438\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"x\" alt=\"x\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1b6\/8fc\/04b\/1b68fc04b19d9334a4872c9f4a0d4565.svg\" width=\"14\" height=\"14\"\/>\u00a0\u2013 \u044d\u0442\u043e \u0431\u0443\u043b\u0435\u0432\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 1 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"i\" alt=\"i\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/296\/86c\/28b\/29686c28b271026fac82e4e9e3943f8b.svg\" width=\"8\" height=\"20\"\/>\u00a0\u0438\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"j\" alt=\"j\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4ae\/947\/f6c\/4ae947f6c55e8e10014a0d4dc09c87b7.svg\" width=\"10\" height=\"24\"\/>, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 0 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f;<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0432\u044f\u0437\u0438 \u0432\u0435\u0440\u0448\u0438\u043d:<\/strong>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"\\sum_{j}x_{ij} =1\u00a0\" alt=\"\\sum_{j}x_{ij} =1\u00a0\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/022\/f23\/849\/022f238493c3614a7aa88faf92ad3e94.svg\" width=\"113\" height=\"59\"\/>\u2013 \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u044e:<\/strong>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"x_{ij}=x_{ji}\" alt=\"x_{ij}=x_{ji}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8ad\/8a7\/906\/8ad8a7906658e8d0c6d5151ea5bca370.svg\" width=\"91\" height=\"21\"\/> \u2013 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0441\u0432\u044f\u0437\u044c \u0432 \u043e\u0434\u043d\u043e\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0440\u0430\u0432\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u0430 \u0441\u0432\u044f\u0437\u0438 \u0434\u0432\u0443\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0432 \u0434\u0440\u0443\u0433\u043e\u043c.<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u0430\u043f\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u0430. \u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u0446\u0438\u043a\u043b, \u043d\u043e \u0442\u0430\u043a\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043c\u0435\u043d\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u0432\u0435\u0440\u0433\u0430\u043b\u0438 \u0432 \u0443\u043d\u044b\u043d\u0438\u0435. \u0412 R \u043d\u0435 \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0446\u0438\u043a\u043b\u044b \u0442\u0430\u043c \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u043f\u0430\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>combn()<\/code>\u00a0\u0438 \u0434\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u043f\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0443 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u0434 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c \u0438 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u044b\u043c.<\/p>\n<pre><code class=\"r\"># \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0434\u043b\u044f \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d odd_dist_mtrx &lt;- cities[odd_vertices, ] |&gt; dist() |&gt; as.matrix()  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a \u043f\u043b\u043e\u0441\u043a\u043e\u043c\u0443 cost_vector &lt;- odd_dist_mtrx[lower.tri(odd_dist_mtrx)]  # \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 constraint_matrix &lt;- matrix(0, nrow = n, ncol = length(cost_vector))  # \u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0439 \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d combinations &lt;- combn(n, 2)  # \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439   for (col in 1:ncol(combinations)) {   constraint_matrix[combinations[1, col], col] &lt;- 1   constraint_matrix[combinations[2, col], col] &lt;- 1 }  # \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 solution &lt;- lp(direction = \"min\",                 objective.in = cost_vector, # \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u044b \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0434\u043b\u0438\u043d\u043d\u044b \u0440\u0435\u0431\u0435\u0440)                const.mat = constraint_matrix, # \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439                const.dir = rep(\"=\", n), # \u0442\u0438\u043f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f                const.rhs = rep(1, n), # \u043a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437                 binary.vec = 1:length(cost_vector))  # cat(\"\u0418\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435: \", solution$objval, \"\\n\")  # \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438: \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u043f\u0430\u0440 \u0432\u0435\u0440\u0448\u0438\u043d  matched_pairs &lt;- combinations[1:2, which(solution$solution == 1)]   # \u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 matched_pairs_df &lt;- data.frame(from=odd_vertices[matched_pairs[1,]], to=odd_vertices[matched_pairs[2,]])  MPM_df &lt;- igraph::as_data_frame(MST) |&gt;    mutate(across(1:2, as.integer)) |&gt;    bind_rows(matched_pairs_df) |&gt;    left_join(cities_df, by=c(\"from\"=\"n\")) |&gt;    left_join(cities_df, by=c(\"to\"=\"n\"), suffix = c(\"_from\", \"_to\")) |&gt;    mutate(wired = if_else(is.na(weight), \"\u0421\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u043f\u0430\u0440\", \"\u041e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\"))  # \u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f p0 +    geom_segment(data=full_df, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), alpha =.05) +   geom_segment(data=MPM_df, alpha =.6,                 aes(x = x_from, y = y_from, xend = x_to, yend = y_to, col = wired, lty=wired)) +    geom_point(data = cities[odd_vertices,], col=\"firebrick\", size = 5, alpha = .5) +    scale_color_manual(values = c(`\u0421\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u043f\u0430\u0440`=\"firebrick\", `\u041e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e`= \"white\")) +   scale_linetype_manual(values = c(`\u0421\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u043f\u0430\u0440`=5, `\u041e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e`= 1)) +   labs(title = \"\u041b\u0443\u0447\u0448\u0435\u0435 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u043f\u0430\u0440 \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e\", col=\"\", lty=\"\") +    theme(legend.position = \"bottom\")<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/6a4\/cff\/137\/6a4cff137100954079b19858af6b7f63.png\" width=\"1440\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6a4\/cff\/137\/6a4cff137100954079b19858af6b7f63.png\"\/><\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d: 1\u201312,\u00a0<strong>6\u20133<\/strong>,\u00a0<strong>15\u20138<\/strong>, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0434\u0432\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0442 \u0443\u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0431\u0440\u0430 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u0430\u043f\u0430 \u043f\u043e\u0438\u0441\u043a\u0430\u00a0<em>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430<\/em>. \u0427\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u044f \u043f\u044b\u0442\u0430\u043b\u0441\u044f \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0430, \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f, \u043d\u043e \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u0441\u043b\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u0438\u043c\u0435\u044e\u0449\u0438\u0445\u0441\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u043d\u0435\u0442 \u043e\u0441\u043e\u0431\u043e\u0439 \u043d\u0443\u0436\u0434\u044b \u0431\u043e\u0440\u043e\u0442\u044c\u0441\u044f \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0441\u043b\u0443\u0447\u0430\u044f\u043c\u0438. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0435\u0441\u043b\u0438 \u0443\u0431\u0440\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u044f, \u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u044d\u0442\u0430\u043f \u2013 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u042d\u0439\u043b\u0435\u0440\u043e\u0432\u0430 \u0433\u0440\u0430\u0444\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u043c.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043d\u0430 Python, \u0442\u043e \u0442\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435 \u043f\u043e\u0438\u0441\u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 MIP, \u0430 \u0438\u043d\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434. \u042d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u00a0<a href=\"https:\/\/networkx.org\/documentation\/stable\/reference\/algorithms\/generated\/networkx.algorithms.approximation.traveling_salesman.christofides.html\" rel=\"noopener noreferrer nofollow\">\u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043f\u0430\u043a\u0435\u0442\u0430 NetworkX<\/a>. \u041d\u0435 \u0431\u0435\u0440\u0443\u0441\u044c \u0441\u0443\u0434\u0438\u0442\u044c, \u043a\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u043b\u0443\u0447\u0448\u0435, \u043d\u043e \u043e\u0442\u043c\u0435\u0447\u0443 \u0432\u0430\u0436\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u043e\u0433\u043e \u044d\u0442\u0430\u043f\u0430, \u0442\u0430\u043a \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"O(n^3)\" alt=\"O(n^3)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/702\/0ab\/43a\/7020ab43a910f9c5e3135641df23b0c3.svg\" width=\"63\" height=\"31\"\/>.<\/p>\n<h3>4. \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u042d\u0439\u043b\u0435\u0440\u043e\u0432\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430<\/h3>\n<p>\u042d\u0439\u043b\u0435\u0440\u043e\u0432\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u0438\u043b\u0438 \u043f\u0443\u0442\u0435\u043c \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u0433\u0440\u0430\u0444\u0430, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043a\u0430\u0436\u0434\u043e\u0435 \u0440\u0435\u0431\u0440\u043e \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e\u00a0<code>igraph::eulerian_path()<\/code>\u00a0\u2013 \u044d\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430 \u043e\u0442 \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u043f\u0430\u0440 \u043a\u00a0<em>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u043c\u0443 \u0434\u0435\u0440\u0435\u0432\u0443<\/em>:<\/p>\n<pre><code class=\"r\">EP &lt;- add_edges(MST, odd_vertices[as.vector(matched_pairs)]) |&gt; # \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0435\u0440    eulerian_path() # \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u042d\u0439\u043b\u0435\u0440\u043e\u0432\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430<\/code><\/pre>\n<h3>5. \u0423\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0432\u0435\u0440\u0448\u0438\u043d<\/h3>\n<p>\u042d\u0442\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e\u0449\u0438\u0439 \u044d\u0442\u0430\u043f \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u042d\u0439\u043b\u0435\u0440\u043e\u0432\u0430 \u0446\u0438\u043a\u043b\u0430 \u0432 \u0413\u0430\u043c\u0438\u043b\u044c\u0442\u043e\u043d\u043e\u0432, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0438 \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0432 \u0431\u0430\u0437\u043e\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u00a0<strong>6\u20133<\/strong>,\u00a0<strong>15\u20138<\/strong>\u00a0\u043f\u0440\u043e\u0445\u043e\u0434 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0432\u0430\u0436\u0434\u044b: \u0442\u0443\u0434\u0430 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e. \u042d\u0442\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c. \u0421\u043f\u043e\u0441\u043e\u0431 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439: \u043f\u0440\u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u0435\u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0443 \u0442\u0440\u0435\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043d\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0434\u043b\u0438\u043d\u0443 \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u043e\u0433\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430, \u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442.<\/p>\n<pre><code class=\"r\"># \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043b\u0438\u0448\u043d\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d  shortcut_path &lt;- function(EP){   HP &lt;- c()   for(vertex in EP){     if (!vertex %in% HP | vertex == EP[1L]){ # \u043f\u0435\u0440\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f        HP &lt;- c(HP, vertex)     }   }   return(HP) }  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u042d\u0439\u043b\u0435\u0440\u043e\u0432\u0430 \u043a \u0413\u0430\u043c\u0438\u043b\u044c\u0442\u043e\u043d\u043e\u0432\u0443 \u0446\u0438\u043a\u043b\u0443  HP &lt;- shortcut_path(names(EP$vpath)|&gt; as.integer())  # \u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f  p0 +   geom_segment(data=full_df, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), alpha =.05) +   geom_segment(data=MPM_df, col=\"white\", linewidth = 1.5, alpha=.3,                aes(x = x_from, y = y_from, xend = x_to, yend = y_to, lty=\"\u042d\u0439\u043b\u0435\u0440\u043e\u0432 \u0446\u0438\u043a\u043b\")) +   geom_point(data = cities[odd_vertices,], col=\"firebrick\", size = 5, alpha = .5) +    geom_path(data = cities_df[HP,], aes(col=seq_along(HP), lty=\"\u0413\u0430\u043c\u0438\u043b\u044c\u0442\u043e\u043d\u043e\u0432 \u0446\u0438\u043a\u043b\")) +   scale_color_viridis_c(begin = .3) +   scale_linetype_manual(values = c(`\u042d\u0439\u043b\u0435\u0440\u043e\u0432 \u0446\u0438\u043a\u043b`=5, `\u0413\u0430\u043c\u0438\u043b\u044c\u0442\u043e\u043d\u043e\u0432 \u0446\u0438\u043a\u043b`=1)) +    labs(col=\"\u041e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0441\u0442\u044c\", linetype=\"\",        title = paste0(\"\u0418\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435: \", calc_dist4mtrx(dist_mtrx, HP)|&gt;round(2))) +    theme(legend.position = \"bottom\")<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3ba\/d8c\/bf0\/3bad8cbf044e899124201e4e51fbc5a4.png\" width=\"1440\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ba\/d8c\/bf0\/3bad8cbf044e899124201e4e51fbc5a4.png\"\/><\/figure>\n<p>\u0412\u043e\u0442, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438 \u0432\u0441\u0435, \u0447\u0442\u043e \u043a\u0430\u0441\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u043e\u0432 \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430.<\/p>\n<h3>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u043a\u0430\u043a\u00a0<em>\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>\u00a0\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0444\u043e\u043d\u0435 \u0440\u0430\u043d\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432. \u0412\u0441\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u0440\u0430\u0441\u0447\u0435\u0442\u043e\u0432, \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0441\u0447\u0435\u0442\u043e\u0432, \u043a\u0430\u043a \u0438 \u043f\u0440\u0435\u0436\u0434\u0435, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0430\u00a0<a href=\"https:\/\/github.com\/dkibalnikov\/ai_optimization\" rel=\"noopener noreferrer nofollow\">github<\/a>. \u0417\u0432\u0435\u0437\u0434\u044b \u0432 \u043a\u0430\u0440\u043c\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u2013 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442\u0441\u044f!<\/p>\n<p><strong>\u0422\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/345\/0ad\/7a8\/3450ad7a8fea25777b13bbe37af2c3d1.png\" width=\"1440\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/345\/0ad\/7a8\/3450ad7a8fea25777b13bbe37af2c3d1.png\"\/><\/figure>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043f\u0443\u0433\u0430\u044e\u0449\u0438\u0439 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 3\/2\u00a0<em>\u0430\u043f\u043f\u0440\u043e\u043a\u0441\u0438\u043c\u0430\u0446\u0438\u0438 \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>, \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0432\u0441\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u043f\u043b\u043e\u0445\u043e. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0435\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442 \u0432\u0441\u0435\u0445, \u043a\u0440\u043e\u043c\u0435\u00a0<em>Concorde<\/em>\u00a0\u0438\u00a0<em>2-opt<\/em>\u00a0\u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 (64 \u0433\u043e\u0440\u043e\u0434\u0430).<\/p>\n<p><strong>\u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/859\/0d8\/58c\/8590d858c4ef716b71a60b8b3af1b758.png\" width=\"1440\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/859\/0d8\/58c\/8590d858c4ef716b71a60b8b3af1b758.png\"\/><\/figure>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u043d\u043e\u043c\u0438\u043d\u0430\u0446\u0438\u0438\u00a0<em>\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>\u00a0\u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0435 \u0432\u0442\u043e\u0440\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u0432\u0441\u043b\u0435\u0434 \u0437\u0430 \u044d\u0432\u0440\u0438\u0441\u0442\u0438\u043a\u043e\u0439 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0435\u0433\u043e \u0433\u043e\u0440\u043e\u0434\u0430 \u0438 \u044d\u0442\u043e \u043f\u043e-\u043c\u043e\u0435\u043c\u0443 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0448\u0443\u0441\u0442\u0440\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0435\u0440\u0430 \u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0434\u043b\u044f \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0435\u0440\u043e\u0432 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0431\u0445\u043e\u0434 \u0440\u0435\u0431\u0435\u0440, \u0442\u043e \u0442\u0430\u043a\u0438\u043c \u0443\u0436\u0435 \u0431\u043e\u0435\u0432\u044b\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b, \u0441\u043a\u0430\u0436\u0435\u043c, \u0434\u043b\u044f \u0444\u043b\u043e\u0442\u0430 \u0433\u0430\u0437\u0435\u043b\u0435\u0439 \u043f\u043e \u0440\u0430\u0437\u0432\u043e\u0437\u043a\u0435 \u0447\u0435\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0433\u0430\u0441\u0442\u0440\u043e\u043d\u043e\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u043e \u0442\u043e\u0440\u0433\u043e\u0432\u044b\u043c \u0442\u043e\u0447\u043a\u0430\u043c \u0433\u043e\u0440\u043e\u0434\u0430.<\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0432\u0441\u0435\u043c, \u043a\u0442\u043e \u043d\u0430\u0448\u0435\u043b \u0441\u0438\u043b\u044b \u0434\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0437\u0430\u043c\u0435\u0442\u043a\u0443 \u0434\u043e \u043a\u043e\u043d\u0446\u0430.<\/p>\n<hr\/>\n<p><strong>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0438 \u043d\u0430 \u0442\u0435\u043c\u0443<\/strong><\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/alon.kr\/posts\/christofides\" rel=\"noopener noreferrer nofollow\">The Chistofides Algorithm &#8212; Alon Krymgand<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/igraph.org\/\" rel=\"noopener noreferrer nofollow\">igraph<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/networkx.org\/documentation\/stable\/reference\/algorithms\/generated\/networkx.algorithms.approximation.traveling_salesman.christofides.html\" rel=\"noopener noreferrer nofollow\">NetworkX Christofides<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/dkibalnikov\/ai_optimization\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u043e\u0435\u043a\u0442 AI optimization \u043d\u0430 Github<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/habr.com\/ru\/articles\/399873\/\" rel=\"noopener noreferrer nofollow\">\u0421\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0441\u0442\u044b \u043f\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u043a\u0435 \u0438\u0434\u0443\u0442 \u043d\u0435\u0445\u043e\u0436\u0435\u043d\u044b\u043c\u0438 \u0434\u043e\u0440\u043e\u0433\u0430\u043c\u0438<\/a><\/p>\n<\/li>\n<\/ul>\n<hr\/>\n<p>\u041e\u0440\u0438\u0433\u0438\u043d\u0430\u043b <a href=\"https:\/\/investcookies.ru\/post\/algoritm_kristofidesa_serdukova\/\" rel=\"noopener noreferrer nofollow\">\u0437\u0430\u043c\u0435\u0442\u043a\u0438<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/872398\/\"> https:\/\/habr.com\/ru\/articles\/872398\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<h3>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u0438<\/h3>\n<p>\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439\u00a0<a href=\"https:\/\/habr.com\/ru\/articles\/866202\/\" rel=\"noopener noreferrer nofollow\">\u0437\u0430\u043c\u0435\u0442\u043a\u0435<\/a>\u00a0\u044f \u043a\u043e\u0441\u043d\u0443\u043b\u0441\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0435\u0440\u0430 (TSP). \u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u043e\u0431\u044a\u0435\u043c\u043d\u044b\u043c \u0438 \u0441\u0443\u043d\u0443\u0442\u044c \u0442\u0443\u0434\u0430 \u0435\u0449\u0435 \u043e\u0434\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0431\u044b\u043b\u043e \u0431\u044b \u0447\u0440\u0435\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c\u044e. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0441\u0447\u0438\u0442\u0430\u044e \u0432\u0430\u0436\u043d\u044b\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0431 \u043e\u0434\u043d\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u043e\u0441\u0438\u0442 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 &#8212;\u00a0<em>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>. \u041f\u0440\u0438\u0447\u0438\u043d\u044b, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043e\u0431 \u044d\u0442\u043e\u043c \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435:<\/p>\n<ol>\n<li>\n<p>\u0420\u0435\u0447\u044c \u0438\u0434\u0435\u0442 \u043f\u0440\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430 \u043f\u0440\u0438 \u043e\u0446\u0435\u043d\u043a\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0441\u0435\u0442\u043a\u0430\u043c\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435\u00a0<a href=\"https:\/\/towardsdatascience.com\/transpormer-a-transformer-network-for-the-travelling-salesman-problem-154bd33c37b0\" rel=\"noopener noreferrer nofollow\">TranSPormer: A Transformer Network for the Travelling Salesman Problem<\/a>\u00a0\u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0430\u0437\u0432\u0430\u043d \u0432 \u0447\u0435\u0441\u0442\u044c \u0440\u0443\u0441\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430 \u0432 \u0440\u0443\u0441\u0441\u043a\u043e\u044f\u0437\u044b\u0447\u043d\u043e\u043c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0435 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430 \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0439 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443, \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c\u00a0<a href=\"http:\/\/nas1.math.nsc.ru\/aim\/journals\/us\/us17\/us17_007.pdf\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u044e \u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/a>\u00a0\u043e\u0442 1978 \u0438 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0435 \u0432 \u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u0440\u0430\u0441\u0438\u0432. \u041f\u043e\u043d\u0438\u043c\u0430\u044e, \u0447\u0442\u043e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u044d\u0441\u0442\u0435\u0442\u0438\u043a\u0430 \u2013 \u044d\u0442\u043e \u043d\u0435\u0447\u0442\u043e \u0441\u043a\u0440\u044b\u0442\u043e\u0435 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0435 \u0432\u0435\u0449\u0435\u0439 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0435 \u0441\u0443\u0435\u0442\u043b\u0438\u0432\u043e\u043c\u0443 \u0432\u0437\u043e\u0440\u0443, \u043d\u043e \u0432\u0435\u0440\u044e, \u0447\u0442\u043e \u0438 \u0442\u0430\u043a\u0430\u044f \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u044f \u043a\u0440\u0430\u0441\u043e\u0442\u044b \u043d\u0430\u0439\u0434\u0435\u0442 \u0441\u0432\u043e\u0435\u0433\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u0445\u0443\u0436\u0435 3\/2 \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438 \u0434\u0430\u043d\u043d\u043e\u0435 \u043d\u0435\u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u0433\u043e \u0434\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0411\u0435\u0437\u0443\u0441\u043b\u043e\u0432\u043d\u043e, 3\/2 \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u2013 \u0437\u0432\u0443\u0447\u0438\u0442 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u043d\u043e, \u043d\u043e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u0435\u0441\u044c\u043c\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u043e, \u0447\u0442\u043e \u044f \u0438 \u043d\u0430\u043c\u0435\u0440\u0435\u043d \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0438\u0436\u0435 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u0430\u043f\u0430 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p><em>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>\u00a0\u0441\u0442\u0440\u043e\u0438\u0442 \u0441\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u0431\u0430\u0437\u0435 \u0442\u0435\u043e\u0440\u0438\u0438 \u0433\u0440\u0430\u0444\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u0430\u043b\u044c\u043a\u0443\u043b\u044f\u0442\u043e\u0440 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u0433\u0440\u0430\u0444\u0430\u043c\u0438 \u0438 \u0434\u043b\u044f R \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432 \u0432\u0438\u0434\u0435 \u043f\u0430\u043a\u0435\u0442\u0430\u00a0<a href=\"https:\/\/r.igraph.org\/\" rel=\"noopener noreferrer nofollow\">igraph<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 \u0421.\u00a0<code>igraph<\/code>\u00a0\u0438\u043c\u0435\u0435\u0442 \u0442\u0430\u043a\u0436\u0435 \u043f\u0430\u043a\u0435\u0442\u044b-\u043e\u0431\u0435\u0440\u0442\u043a\u0438 \u043a \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u043c \u044f\u0437\u044b\u043a\u0430\u043c, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043a Python. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0441\u043e\u043b\u0432\u0435\u0440 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u201c\u0442\u043e\u043d\u043a\u043e\u0439\u201d \u0438 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0448\u0443\u0441\u0442\u0440\u043e\u0439 \u043e\u0431\u0435\u0440\u0442\u043a\u043e\u0439 \u0434\u043b\u044f R \u0438 \u0431\u0435\u043a\u0435\u043d\u0434\u043e\u043c \u043d\u0430 \u0421. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0446\u0430\u0440\u044c-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443 \u2013\u00a0<a href=\"https:\/\/investcookies.ru\/post\/prostye_evristiki_dla_tsp\/#concorde\" rel=\"noopener noreferrer nofollow\"><em>Concorde<\/em><\/a>.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d, \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 16 \u0433\u043e\u0440\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b\u043b \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0435 \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0432 \u044d\u0442\u043e\u0439 \u0434\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u0435\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"r\">library(igraph) # \u043f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0433\u0440\u0430\u0444\u0430\u043c\u0438 library(tidyverse) # \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 library(lpSolve) # \u043f\u0430\u043a\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0438 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438   # \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 ai_optimization, \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u043d\u0430 github \"https:\/\/raw.githubusercontent.com\/dkibalnikov\/ai_optimization\/refs\/heads\/main\/algorithms\/functions.R\"|&gt;   source()  # \u0420\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0435\u0440\u0430 n_cities &lt;- 16  # \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0437 \u043f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0438 cities &lt;- generate_task()  # \u0414\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 cities_df &lt;- as.data.frame(cities) |&gt;    transform(n=seq_len(n_cities))   # \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 dist_mtrx &lt;- as.matrix(dist(cities))  # \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 G &lt;- graph_from_adjacency_matrix(dist_mtrx, mode='undirected', weighted = TRUE)  # \u0411\u0430\u0437\u043e\u0432\u044b\u0439 \u0433\u0440\u0430\u0444\u0438\u043a \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 p0 &lt;- ggplot(cities_df, aes(x, y)) +    geom_point() +    geom_point(data = data.frame(n=1, x=0, y=0), shape = 21, size = 5, col = \"firebrick\") +   geom_text(aes(label = n), nudge_x = 3, nudge_y = 3)  # \u0414\u0430\u0442\u0430\u0444\u0440\u0435\u0439\u043c \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 full_df &lt;- igraph::as_data_frame(G) |&gt;    mutate(across(1:2, as.integer)) |&gt;   left_join(cities_df, by=c(\"from\"=\"n\")) |&gt;    left_join(cities_df, by=c(\"to\"=\"n\"), suffix = c(\"_from\", \"_to\"))  # \u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f   p0 +    geom_segment(data=full_df, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), alpha =.1) +    labs(title = \"\u041f\u043e\u043b\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0434\u043b\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430\")<\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043a\u043e\u0434\u0430 \u0432\u044b\u0448\u0435, \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u044b\u043b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d \u0432 \u043f\u043e\u043b\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u043d\u0443\u043b\u0435\u0432\u044b\u043c \u044d\u0442\u0430\u043f\u043e\u043c\u00a0<em>\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041a\u0440\u0438\u0441\u0442\u043e\u0444\u0438\u0434\u0435\u0441\u0430-\u0421\u0435\u0440\u0434\u044e\u043a\u043e\u0432\u0430<\/em>. \u041a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u0430 \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u0440\u0435\u0431\u0435\u0440 \u0431\u0435\u0437 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f (\u043e\u0440\u0438\u0435\u043d\u0442\u0430\u0446\u0438\u0438).<\/p>\n<h3>1. \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e<\/h3>\n<p>\u041d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c\u00a0<em>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e<\/em>\u00a0(Minimum Spanning Tree), \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u043e\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435. \u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u2013 \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u0434\u043e\u0440\u043e\u0433 \u043c\u0435\u0436\u0434\u0443 \u0433\u043e\u0440\u043e\u0434\u0430\u043c\u0438, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0433\u043e\u0440\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u043b\u0438 \u043e\u043f\u043e\u0441\u0440\u0435\u0434\u043e\u0432\u0430\u043d\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0434\u0440\u0443\u0433\u043e\u0439 \u0433\u043e\u0440\u043e\u0434, \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0436\u0435\u043b\u0430\u043d\u0438\u0435 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u043e\u0439 \u0434\u043e\u0440\u043e\u0436\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u043e\u0442\u043d\u0430.\u00a0<em>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e<\/em>\u00a0\u0434\u043b\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"r\"># \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e MST &lt;- mst(G) # \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438  # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 odd_vertices &lt;- V(MST)[degree(MST) %% 2 == 1] |&gt; as.integer()  # \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e n &lt;- length(odd_vertices)  # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438  MST_df &lt;- igraph::as_data_frame(MST) |&gt;    mutate(across(1:2, as.integer)) |&gt;   left_join(cities_df, by=c(\"from\"=\"n\")) |&gt;    left_join(cities_df, by=c(\"to\"=\"n\"), suffix = c(\"_from\", \"_to\"))   # \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f  p0 +    geom_segment(data=full_df, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), alpha =.05) +   geom_segment(data=MST_df, alpha =.6,                 aes(x = x_from, y = y_from, xend = x_to, yend = y_to, color=\"\u041e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\")) +    geom_point(data = cities[odd_vertices,], aes(col=\"\u041d\u0435\u0447\u0435\u0442\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\"), size = 5, alpha = .5)  +    scale_color_manual(values = c(\"\u041d\u0435\u0447\u0435\u0442\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\"=\"firebrick\", \"\u041e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\"=\"white\"))+   labs(title = \"\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e\", col=\"\") +    theme(legend.position = \"bottom\")<\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f\u00a0<em>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430<\/em>, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u00a0<code>igraph<\/code>\u00a0\u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u044b\u0431\u043e\u0440 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043d\u043e \u0432\u0430\u0436\u043d\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0434\u0440\u0443\u0433\u043e\u0435, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u2013 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0447\u0435\u043d\u044c \u043d\u0435\u0434\u043e\u0440\u043e\u0433\u043e.<\/p>\n<h3>2. \u0412\u044b\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e<\/h3>\n<p>\u041d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435 \u0432\u044b\u0448\u0435 \u043a\u0438\u0440\u043f\u0438\u0447\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c \u0441\u0440\u0430\u0437\u0443 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e. \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u0430\u043a\u0438\u0445 \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0432\u044b\u044f\u0441\u043d\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u2013 \u0447\u0435\u0442\u043d\u043e \u0438 \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0435. \u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0442\u0440\u043e\u0433\u0430\u044f\u00a0<em>\u041b\u0435\u043c\u043c\u0430 \u043e \u0440\u0443\u043a\u043e\u043f\u043e\u0436\u0430\u0442\u0438\u044f\u0445<\/em>, \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043b\u044e\u0431\u043e\u0439 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0438\u043c\u0435\u0435\u0442 \u0447\u0435\u0442\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u0441\u0442\u0435\u043f\u0435\u043d\u0435\u0439. \u042d\u0442\u043e \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u0432\u0430\u0436\u043d\u0430 \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0432\u0441\u0435 \u0431\u0435\u0437 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441 \u043d\u0435\u0447\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439.<\/p>\n<h3>3. \u041f\u043e\u0438\u0441\u043a \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u043f\u0430\u0440<\/h3>\n<p>\u0412 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0435 \u044d\u0442\u043e\u0442 \u044d\u0442\u0430\u043f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0437\u0432\u0443\u0447\u0438\u0442 \u043a\u0430\u043a:\u00a0<em>\u201c\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u043e\u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0435\u0441\u0430 M \u0432 \u043f\u043e\u0434\u0433\u0440\u0430\u0444\u0435\u201d<\/em>\u00a0(Minimum Weight Perfect Matching). \u0423\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u0430\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0430 \u2013 \u044d\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u0433\u0430\u044f \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c, \u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043c\u0430\u043b\u043e \u043f\u0440\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u0447\u0442\u043e \u0436\u0435 \u0432 \u0438\u0442\u043e\u0433\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0438\u0434\u0435\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u2013 \u043d\u0443\u0436\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0432\u043e\u043a\u0443\u043f\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0442\u0430\u043a\u0438\u0445 \u0441\u0432\u044f\u0437\u0435\u0439 \u0431\u044b\u043b\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c. \u0418\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0431\u0443\u0434\u0435\u0442 \u043d\u0443\u0436\u0435\u043d \u043f\u0430\u043a\u0435\u0442\u00a0<code>lpSolve<\/code>. \u0424\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<ol>\n<li>\n<p><strong>\u0426\u0435\u043b\u0435\u0432\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f:<\/strong>\u00a0, \u0433\u0434\u0435\u00a0\u00a0\u2013 \u044d\u0442\u043e \u0434\u043b\u0438\u043d\u0430 \u0440\u0435\u0431\u0440\u0430 \u0438\u00a0\u00a0\u2013 \u044d\u0442\u043e \u0431\u0443\u043b\u0435\u0432\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 1 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438\u00a0\u00a0\u0438\u00a0, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 0 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f;<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0432\u044f\u0437\u0438 \u0432\u0435\u0440\u0448\u0438\u043d:<\/strong>\u00a0\u2013 \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438;<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u044e:<\/strong>\u00a0 \u2013 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0441\u0432\u044f\u0437\u044c \u0432 \u043e\u0434\u043d\u043e\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0440\u0430\u0432\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u0430 \u0441\u0432\u044f\u0437\u0438 \u0434\u0432\u0443\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0432 \u0434\u0440\u0443\u0433\u043e\u043c.<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u0430\u043f\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u0430. \u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u0446\u0438\u043a\u043b, \u043d\u043e \u0442\u0430\u043a\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043c\u0435\u043d\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u0432\u0435\u0440\u0433\u0430\u043b\u0438 \u0432 \u0443\u043d\u044b\u043d\u0438\u0435. \u0412 R \u043d\u0435 \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0446\u0438\u043a\u043b\u044b \u0442\u0430\u043c \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0414\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u043f\u0430\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>combn()<\/code>\u00a0\u0438 \u0434\u0430\u043b\u0435\u0435 \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u043e\u043c \u043f\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0443 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u0434 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c \u0438 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u044b\u043c.<\/p>\n<pre><code class=\"r\"># \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0434\u043b\u044f \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d odd_dist_mtrx &lt;- cities[odd_vertices, ] |&gt; dist() |&gt; as.matrix()  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u043e\u0442 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a \u043f\u043b\u043e\u0441\u043a\u043e\u043c\u0443 cost_vector &lt;- odd_dist_mtrx[lower.tri(odd_dist_mtrx)]  # \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 constraint_matrix &lt;- matrix(0, nrow = n, ncol = length(cost_vector))  # \u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0439 \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d combinations &lt;- combn(n, 2)  # \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439   for (col in 1:ncol(combinations)) {   constraint_matrix[combinations[1, col], col] &lt;- 1   constraint_matrix[combinations[2, col], col] &lt;- 1 }  # \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 solution &lt;- lp(direction = \"min\",                 objective.in = cost_vector, # \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u044b \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u0434\u043b\u0438\u043d\u043d\u044b \u0440\u0435\u0431\u0435\u0440)                const.mat = constraint_matrix, # \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439                const.dir = rep(\"=\", n), # \u0442\u0438\u043f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f                const.rhs = rep(1, n), # \u043a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437                 binary.vec = 1:length(cost_vector))  # cat(\"\u0418\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435: \", solution$objval, \"\\n\")  # \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438: \u0441\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u043f\u0430\u0440 \u0432\u0435\u0440\u0448\u0438\u043d  matched_pairs &lt;- combinations[1:2, which(solution$solution == 1)]   # \u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 matched_pairs_df &lt;- data.frame(from=odd_vertices[matched_pairs[1,]], to=odd_vertices[matched_pairs[2,]])  MPM_df &lt;- igraph::as_data_frame(MST) |&gt;    mutate(across(1:2, as.integer)) |&gt;    bind_rows(matched_pairs_df) |&gt;    left_join(cities_df, by=c(\"from\"=\"n\")) |&gt;    left_join(cities_df, by=c(\"to\"=\"n\"), suffix = c(\"_from\", \"_to\")) |&gt;    mutate(wired = if_else(is.na(weight), \"\u0421\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435 \u043d\u0435\u0447\u0435\u0442\u043d\u044b\u0445 \u043f\u0430\u0440\", \"\u041e\u0441\u0442\u043e\u0432\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\"))  # \u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f p0 +    geom_segment(data=full_df, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), alpha =.05) +   geom_segment(data=MPM_df, alpha =.6,                 aes(x = x_from, y = y_from, xend = x_to, yend = y_to, col = wired, lty=wired)) +    geom_point(data = cities[odd_vertices,], col=\"firebrick\", size = 5, alpha = .5) +    scale_color_manual(values = c(`\u0421\u043e\u0447\u0435\u0442\u0430\u043d\u0438\u0435<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-444567","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/444567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=444567"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/444567\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=444567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=444567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=444567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}