{"id":335046,"date":"2022-06-27T15:00:27","date_gmt":"2022-06-27T15:00:27","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=335046"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=335046","title":{"rendered":"<span>\u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438<\/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<p>\u0418\u0434\u0435\u044f \u043f\u0440\u043e\u0441\u0442\u0430. \u041d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u0418 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0430.\u00a0<\/p>\n<p>\u041f\u0443\u0441\u0442\u044c \u0441 \u0440\u0430\u0432\u043d\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 1 \u0438 2. \u042d\u0442\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c [ 1, 2, 1, 1, 2, 2, 1, 2, 2, 1]. \u041c\u043e\u0436\u0435\u043c \u043b\u0438 \u043c\u044b \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u043f\u0440\u0435\u0434\u0443\u0433\u0430\u0434\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435? \u041f\u043e \u044d\u0442\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 &#8212; \u043d\u0435\u0442.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0435\u0449\u0451 \u043e\u0434\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c [3, 4, 4, 3, 3, 4, 3, 3, 4], \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0432\u043e\u0439. \u0415\u0451 \u043c\u043e\u0436\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a:\u00a0 <\/p>\n<pre><code>3:1->2 (\u0435\u0441\u043b\u0438 3, \u0442\u043e 1 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 2) 4:2->1 (\u0435\u0441\u043b\u0438 4, \u0442\u043e 2 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 1) 4:1->1 (\u0435\u0441\u043b\u0438 4, \u0442\u043e 1 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 1) 3:2->2 (\u0435\u0441\u043b\u0438 3, \u0442\u043e 2 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 2).   [1, 2, 1, 1, 2, 2, 1, 2, 2, 1] [3, 4, 4, 3, 3, 4, 3, 3, 4,  ]<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u043a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 1 (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435). \u041d\u043e, \u0435\u0441\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043f\u043e\u0441\u043b\u0435 4 \u0431\u0443\u0434\u0435\u0442 3, \u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u0435\u0442 2.<\/p>\n<p>\u041e\u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0438\u043c \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0435\u0431\u0435, \u0447\u0442\u043e \u0432\u043e\u0448\u043b\u0438 \u0432 \u043a\u043e\u043c\u043d\u0430\u0442\u0443 \u0438 \u0446\u0435\u043b\u044c\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0441\u0432\u0435\u0442. \u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c: 1 &#8212; \u0441\u0432\u0435\u0442 \u043d\u0435 \u0433\u043e\u0440\u0438\u0442, 2 &#8212; \u0441\u0432\u0435\u0442\u00a0 \u0433\u043e\u0440\u0438\u0442. \u041c\u0435\u043d\u044f\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 5 \u0438 \u043d\u0430 \u0432\u044b\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 6). \u041f\u0443\u0441\u0442\u044c \u044d\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0432\u0430 [5, 6, 5, 6, 5, 6, 5, 6, 5].\u00a0<\/p>\n<pre><code> [1, 2, 1, 1, 2, 2, 1, 2, 2, 1]  [5, 6, 5, 6, 5, 6, 5, 6, 5,  ]<\/code><\/pre>\n<p>\u0418\u043c\u0435\u0435\u043c \u043f\u0440\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 5 (\u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c): \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 1 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u00a0 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 2 (\u0441\u0432\u0435\u0442 \u0435\u0441\u0442\u044c); \u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 1 \u043f\u0435\u0440\u0435\u0439\u0434\u0451\u0442 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 1 (\u0441\u0432\u0435\u0442\u0430 \u043d\u0435\u0442) \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0436\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438, \u0442.\u0435. 5:1->2|1. \u0427\u0442\u043e \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442? \u0422\u043e, \u0447\u0442\u043e \u0441\u043c\u0435\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0442\u0435\u043b\u044f \u043d\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u043a \u0446\u0435\u043b\u0435\u0432\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e.\u00a0<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441: \u043c\u043e\u0436\u0435\u0442 \u044d\u0442\u0438\u043c \u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0442\u0435\u043b\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u0447\u0442\u043e \u0438\u043d\u043e\u0435? \u0418\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: 3 &#8212; \u043a\u043d\u0438\u0436\u043a\u0430 \u043d\u0430 \u043d\u0438\u0436\u043d\u0435\u0439 \u043f\u043e\u043b\u043a\u0435. 4 &#8212; \u043e\u043d\u0430 \u0436\u0435 \u043d\u0430 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u043f\u043e\u043b\u043a\u0435.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0436\u0435, \u043d\u0435 \u0437\u043d\u0430\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0447\u0442\u043e \u043d\u0430 \u0447\u0442\u043e \u0432\u043b\u0438\u044f\u0435\u0442, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439? \u0412\u0432\u0435\u0434\u0451\u043c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438. \u041e\u0442\u043d\u0435\u0441\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432, \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0431\u044b \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u0431\u044b\u043b\u0438 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u044b,\u00a0 \u043a \u0432\u0441\u0435\u043c \u0441\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u043c\u0441\u044f. \u0427\u0435\u043c \u043e\u043d \u0431\u043b\u0438\u0436\u0435 \u043a \u0435\u0434\u0438\u043d\u0438\u0446\u0435, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0430 \u0441\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u0422\u0435\u043c \u0441 \u043c\u0435\u043d\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043e\u0448\u0438\u0431\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u00a0 \u043a \u0446\u0435\u043b\u0435\u0432\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e.<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e \u0438\u043c\u0435\u044e\u0449\u0438\u043c\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f\u043c, \u043c\u043e\u0436\u0435\u0442 \u044d\u0442\u043e\u0442 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0433\u0438\u043f\u043e\u0442\u0435\u0437.\u00a0<\/p>\n<p>\u0417\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0433\u0440\u0430\u0444 \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u044c\u044e (\u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 4 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043a\u0430\u043a \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 5, \u0442\u0430\u043a \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 1 \u043f\u0440\u0438 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 11).<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/7a5\/d0f\/8be\/7a5d0f8bee57f99f165bcb00fbdeca92.jpg\" width=\"1027\" height=\"1030\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7a5\/d0f\/8be\/7a5d0f8bee57f99f165bcb00fbdeca92.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0438\u0436\u0435 \u043a\u0440\u0430\u0441\u043d\u044b\u043c &#8212; \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0430. \u0417\u0435\u043b\u0435\u043d\u0430\u044f (\u043f\u0435\u0440\u0432\u0430\u044f) \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0430 \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u0438\u043d\u0435\u0439 (\u0432\u0442\u043e\u0440\u043e\u0439), \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0435\u0435 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u043c\u0435\u043d\u044c\u0448\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/f67\/26a\/939\/f6726a93979b62bdd09a0e734605353b.jpg\" width=\"1280\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f67\/26a\/939\/f6726a93979b62bdd09a0e734605353b.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0432\u0442\u043e\u0440\u0443\u044e \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0443, \u043c\u043e\u0436\u043d\u043e \u0443\u0436\u0435 \u043f\u0440\u043e\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b \u043a \u0446\u0435\u043b\u0435\u0432\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e.<\/p>\n<pre><code>import random import matplotlib.pyplot as plt import numpy as np  class Hd(): def __init__(self, graph): self.graph = graph  def rnd_get(self, v): return random.choice(v.split(\"|\"))  def show(self, graph): for x in graph: print(x, \":\", graph[x])  def get_ku(self, graph): '''return (\u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438, \u0440\u0430\u0437\u043c\u0435\u0440)''' n = 0; u = 0 for x in graph: for y in graph[x]: if y[0]: u += y[0].count(\"|\") n += 1 if n != 0: return (round(1- u\/(n+u), 3), n)  else: return (None, None)  def get_states(self, x, f_list = [], n=10): x_list = [] x_list.append(x)  if f_list != []: for f in f_list: xf = [xf for xf in g[x] if xf[1] == f] if not xf: x_list.append(''); f_list[len(x_list)-2] = '' return (x_list, f_list[:len(x_list)-1]) x = self.rnd_get(xf[0][0]) x_list.append(x) else: for i in range(n): if not self.graph[x]: x_list.append(''); f_list.append('') break t = random.choice(self.graph[x]) f = t[1] x = random.choice(t[0].split('|')) x_list.append(x); f_list.append(f)  if len(f_list) == len(x_list) -1: return (x_list, f_list) else: return ([], [])  def get_yfx(self, f_list, x_list, t = True): if len(x_list)-1 != len(f_list): return []  path = [] for i in range(len(f_list)): path.append((x_list[i], f_list[i], x_list[i+1]))  if t: return path #(x, f, next_x) else: p = [] for xfy in path: if xfy[2] != '': p.append(xfy[2]+'='+xfy[1]+'('+xfy[0]+')') return p  def flow(self, path, rnd=False): if not path:  return [] fl = [] for p in path: if not rnd: fl.append(p[:-1]) else: pr = list(p[:-1]) random.shuffle(pr) fl.append(tuple(pr)) return fl  def fORx(self, flow, f=0): '''index: f=0, x=1 or x=0, f=1''' def add_empty(): empty = [] for k in graph: for x in graph[k]: z = list(set(x[0].split('|')) - set(list(graph.keys()))) if z: empty.append(z[0]) return empty  if not flow: return []  graph = {} fli = flow[0]  for t in flow[1:]: if f == 0: if fli[1] not in graph: graph[fli[1]] = [] r = [(i, xf) for i,xf in enumerate(graph[fli[1]]) if xf[1] == fli[0]] if r: if t[1] not in r[0][1][0]: x_new = r[0][1][0] + \"|\" + t[1] if x_new != '': graph[fli[1]][r[0][0]] = (x_new, r[0][1][1]) if not r: if t[1] != '': graph[fli[1]].append((t[1], fli[0]))  if f == 1: if fli[0] not in graph: graph[fli[0]] = [] r = [(i, xf) for i,xf in enumerate(graph[fli[0]]) if xf[1] == fli[1]] if r: if t[0] not in r[0][1][0]: x_new = r[0][1][0] + \"|\" + t[0] if x_new != '': graph[fli[0]][r[0][0]] = (x_new, r[0][1][1]) if not r: if t[0] != '': graph[fli[0]].append((t[0], fli[1])) fli = t  em = add_empty() if em: graph[em[0]] = [] return graph  def merge(self, g1, g2): def find_index(): for i in range(len(graph[k])): if graph[k][i][1] == x[1]: return i return -1  all_keys = set(list(g1.keys()) + list(g2.keys())) graph = {}  for k in all_keys: if k not in graph:  if k in g1: graph[k] = g1[k] else: graph[k] = g2[k]  if k in g2: for x in g2[k]: ind = find_index() if ind != -1: if x[0] != []: t1 = x[0].split('|') if graph[k][ind] != -1: t2 = graph[k][ind][0].split('|') z = \"|\".join(set(t1+t2)) xf = [z, graph[k][ind][1]] graph[k][ind] = xf else: graph[k].append(x)  return graph   #--------- main ---------#     if __name__ == \"__main__\":  g = { \"1\": [[\"2\", \"11\"], [\"10\", \"15\"]], \"2\": [[\"3\", \"11\"], [\"9\", \"12\"]],  \"3\": [[\"4\", \"11\"], [\"2\", \"13\"], [\"6\", \"12\"]],  \"4\": [[\"5|1\", \"11\"]],  \"5\": [[\"6\", \"11\"]],  \"6\": [[\"7\", \"11\"]],  \"7\": [[\"8\", \"11\"], [\"5\", \"12\"]],  \"8\": [[\"9\", \"11\"]],  #\"9\": [['1|5', '11']], '9':[], \"10\":[[\"5\", \"15\"]] }  hd = Hd(g) r_ = hd.get_ku(hd.graph) print(\"\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0433\u0440\u0430\u0444: \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 ku = \", r_[0]) hd.show(hd.graph); print()  epochs = 50 ku_list = [[],[]] gf = [{}, {}]  for i in range(epochs): print(\"\\n_____\u042d\u041f\u041e\u0425\u0410 = \", i+1) x = random.choice(list(g.keys())); print(\"\u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \", x) #x = '1'  x_list, f_list = hd.get_states(x, [], n=20)  path = hd.get_yfx(f_list, x_list) path_yfx = hd.get_yfx(f_list, x_list, t = False) '''if path_yfx: print(\"y=f(x) :\", *path_yfx) pass'''  print(\"  \u041d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435\") for fl in hd.flow(path, rnd=False): print(*fl)  print(\"\\n  \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0430 1: \") gn = hd.fORx(hd.flow(path), f=0) hd.show(gn) gf[0] = hd.merge(gf[0], gn) r = hd.get_ku(gf[0]) print(\"\\n\u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 ku = \", r[0]) ku_list[0].append(r[0]) hd.show(gf[0]) print()  print(\"\\n  \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0430 2: \") gn = hd.fORx(hd.flow(path), f=1) hd.show(gn) gf[1] = hd.merge(gf[1], gn) r = hd.get_ku(gf[1]) print(\"\\n\u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 ku = \", r[0]) ku_list[1].append(r[0]) hd.show(gf[1])  print() fig, ax = plt.subplots() ax.plot([i+1 for i in range(epochs)], ku_list[0], marker=\"*\", color='g') ax.plot([i+1 for i in range(epochs)], ku_list[1], marker=\"+\", color='b') ax.plot([i+1 for i in range(epochs)], [r_[0]]*epochs,  color='r') ax.set_xlabel('\u044d\u043f\u043e\u0445\u0430') ax.set_ylabel('\u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438') plt.show() <\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/post\/673616\/\"> https:\/\/habr.com\/ru\/post\/673616\/<\/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<p>\u0418\u0434\u0435\u044f \u043f\u0440\u043e\u0441\u0442\u0430. \u041d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u0418 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0430.\u00a0<\/p>\n<p>\u041f\u0443\u0441\u0442\u044c \u0441 \u0440\u0430\u0432\u043d\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 1 \u0438 2. \u042d\u0442\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c [ 1, 2, 1, 1, 2, 2, 1, 2, 2, 1]. \u041c\u043e\u0436\u0435\u043c \u043b\u0438 \u043c\u044b \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u043f\u0440\u0435\u0434\u0443\u0433\u0430\u0434\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435? \u041f\u043e \u044d\u0442\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 &#8212; \u043d\u0435\u0442.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0435\u0449\u0451 \u043e\u0434\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c [3, 4, 4, 3, 3, 4, 3, 3, 4], \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0432\u043e\u0439. \u0415\u0451 \u043c\u043e\u0436\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a:\u00a0 <\/p>\n<pre><code>3:1->2 (\u0435\u0441\u043b\u0438 3, \u0442\u043e 1 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 2) 4:2->1 (\u0435\u0441\u043b\u0438 4, \u0442\u043e 2 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 1) 4:1->1 (\u0435\u0441\u043b\u0438 4, \u0442\u043e 1 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 1) 3:2->2 (\u0435\u0441\u043b\u0438 3, \u0442\u043e 2 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0432 2).   [1, 2, 1, 1, 2, 2, 1, 2, 2, 1] [3, 4, 4, 3, 3, 4, 3, 3, 4,  ]<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e \u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u043a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 1 (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435). \u041d\u043e, \u0435\u0441\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043f\u043e\u0441\u043b\u0435 4 \u0431\u0443\u0434\u0435\u0442 3, \u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u0435\u0442 2.<\/p>\n<p>\u041e\u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0438\u043c \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0435\u0431\u0435, \u0447\u0442\u043e \u0432\u043e\u0448\u043b\u0438 \u0432 \u043a\u043e\u043c\u043d\u0430\u0442\u0443 \u0438 \u0446\u0435\u043b\u044c\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0441\u0432\u0435\u0442. \u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c: 1 &#8212; \u0441\u0432\u0435\u0442 \u043d\u0435 \u0433\u043e\u0440\u0438\u0442, 2 &#8212; \u0441\u0432\u0435\u0442\u00a0 \u0433\u043e\u0440\u0438\u0442. \u041c\u0435\u043d\u044f\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 5 \u0438 \u043d\u0430 \u0432\u044b\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 6). \u041f\u0443\u0441\u0442\u044c \u044d\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0432\u0430 [5, 6, 5, 6, 5, 6, 5, 6, 5].\u00a0<\/p>\n<pre><code> [1, 2, 1, 1, 2, 2, 1, 2, 2, 1]  [5, 6, 5, 6, 5, 6, 5, 6, 5,  ]<\/code><\/pre>\n<p>\u0418\u043c\u0435\u0435\u043c \u043f\u0440\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 5 (\u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c): \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 1 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u00a0 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 2 (\u0441\u0432\u0435\u0442 \u0435\u0441\u0442\u044c); \u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 1 \u043f\u0435\u0440\u0435\u0439\u0434\u0451\u0442 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 1 (\u0441\u0432\u0435\u0442\u0430 \u043d\u0435\u0442) \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0436\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438, \u0442.\u0435. 5:1->2|1. \u0427\u0442\u043e \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442? \u0422\u043e, \u0447\u0442\u043e \u0441\u043c\u0435\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0442\u0435\u043b\u044f \u043d\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u043a \u0446\u0435\u043b\u0435\u0432\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e.\u00a0<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441: \u043c\u043e\u0436\u0435\u0442 \u044d\u0442\u0438\u043c \u0432\u044b\u043a\u043b\u044e\u0447\u0430\u0442\u0435\u043b\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u0447\u0442\u043e \u0438\u043d\u043e\u0435? \u0418\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: 3 &#8212; \u043a\u043d\u0438\u0436\u043a\u0430 \u043d\u0430 \u043d\u0438\u0436\u043d\u0435\u0439 \u043f\u043e\u043b\u043a\u0435. 4 &#8212; \u043e\u043d\u0430 \u0436\u0435 \u043d\u0430 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u043f\u043e\u043b\u043a\u0435.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0436\u0435, \u043d\u0435 \u0437\u043d\u0430\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0447\u0442\u043e \u043d\u0430 \u0447\u0442\u043e \u0432\u043b\u0438\u044f\u0435\u0442, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439? \u0412\u0432\u0435\u0434\u0451\u043c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438. \u041e\u0442\u043d\u0435\u0441\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432, \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0431\u044b \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u0431\u044b\u043b\u0438 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u044b,\u00a0 \u043a \u0432\u0441\u0435\u043c \u0441\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u043c\u0441\u044f. \u0427\u0435\u043c \u043e\u043d \u0431\u043b\u0438\u0436\u0435 \u043a \u0435\u0434\u0438\u043d\u0438\u0446\u0435, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0430 \u0441\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u0422\u0435\u043c \u0441 \u043c\u0435\u043d\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u043e\u0448\u0438\u0431\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u00a0 \u043a \u0446\u0435\u043b\u0435\u0432\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e.<\/p>\n<p>\u041d\u0438\u0436\u0435 \u043a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e \u0438\u043c\u0435\u044e\u0449\u0438\u043c\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f\u043c, \u043c\u043e\u0436\u0435\u0442 \u044d\u0442\u043e\u0442 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0433\u0438\u043f\u043e\u0442\u0435\u0437.\u00a0<\/p>\n<p>\u0417\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0433\u0440\u0430\u0444 \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u044c\u044e (\u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f 4 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043a\u0430\u043a \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 5, \u0442\u0430\u043a \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 1 \u043f\u0440\u0438 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 11).<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0438\u0436\u0435 \u043a\u0440\u0430\u0441\u043d\u044b\u043c &#8212; \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0430. \u0417\u0435\u043b\u0435\u043d\u0430\u044f (\u043f\u0435\u0440\u0432\u0430\u044f) \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0430 \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442 \u0441\u0438\u043d\u0435\u0439 (\u0432\u0442\u043e\u0440\u043e\u0439), \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0435\u0435 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u043c\u0435\u043d\u044c\u0448\u0435.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0432\u0442\u043e\u0440\u0443\u044e \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0443, \u043c\u043e\u0436\u043d\u043e \u0443\u0436\u0435 \u043f\u0440\u043e\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u044b \u043a \u0446\u0435\u043b\u0435\u0432\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e.<\/p>\n<pre><code>import random import matplotlib.pyplot as plt import numpy as np  class Hd(): def __init__(self, graph): self.graph = graph  def rnd_get(self, v): return random.choice(v.split(\"|\"))  def show(self, graph): for x in graph: print(x, \":\", graph[x])  def get_ku(self, graph): '''return (\u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438, \u0440\u0430\u0437\u043c\u0435\u0440)''' n = 0; u = 0 for x in graph: for y in graph[x]: if y[0]: u += y[0].count(\"|\") n += 1 if n != 0: return (round(1- u\/(n+u), 3), n)  else: return (None, None)  def get_states(self, x, f_list = [], n=10): x_list = [] x_list.append(x)  if f_list != []: for f in f_list: xf = [xf for xf in g[x] if xf[1] == f] if not xf: x_list.append(''); f_list[len(x_list)-2] = '' return (x_list, f_list[:len(x_list)-1]) x = self.rnd_get(xf[0][0]) x_list.append(x) else: for i in range(n): if not self.graph[x]: x_list.append(''); f_list.append('') break t = random.choice(self.graph[x]) f = t[1] x = random.choice(t[0].split('|')) x_list.append(x); f_list.append(f)  if len(f_list) == len(x_list) -1: return (x_list, f_list) else: return ([], [])  def get_yfx(self, f_list, x_list, t = True): if len(x_list)-1 != len(f_list): return []  path = [] for i in range(len(f_list)): path.append((x_list[i], f_list[i], x_list[i+1]))  if t: return path #(x, f, next_x) else: p = [] for xfy in path: if xfy[2] != '': p.append(xfy[2]+'='+xfy[1]+'('+xfy[0]+')') return p  def flow(self, path, rnd=False): if not path:  return [] fl = [] for p in path: if not rnd: fl.append(p[:-1]) else: pr = list(p[:-1]) random.shuffle(pr) fl.append(tuple(pr)) return fl  def fORx(self, flow, f=0): '''index: f=0, x=1 or x=0, f=1''' def add_empty(): empty = [] for k in graph: for x in graph[k]: z = list(set(x[0].split('|')) - set(list(graph.keys()))) if z: empty.append(z[0]) return empty  if not flow: return []  graph = {} fli = flow[0]  for t in flow[1:]: if f == 0: if fli[1] not in graph: graph[fli[1]] = [] r = [(i, xf) for i,xf in enumerate(graph[fli[1]]) if xf[1] == fli[0]] if r: if t[1] not in r[0][1][0]: x_new = r[0][1][0] + \"|\" + t[1] if x_new != '': graph[fli[1]][r[0][0]] = (x_new, r[0][1][1]) if not r: if t[1] != '': graph[fli[1]].append((t[1], fli[0]))  if f == 1: if fli[0] not in graph: graph[fli[0]] = [] r = [(i, xf) for i,xf in enumerate(graph[fli[0]]) if xf[1] == fli[1]] if r: if t[0] not in r[0][1][0]: x_new = r[0][1][0] + \"|\" + t[0] if x_new != '': graph[fli[0]][r[0][0]] = (x_new, r[0][1][1]) if not r: if t[0] != '': graph[fli[0]].append((t[0], fli[1])) fli = t  em = add_empty() if em: graph[em[0]] = [] return graph  def merge(self, g1, g2): def find_index(): for i in range(len(graph[k])): if graph[k][i][1] == x[1]: return i return -1  all_keys = set(list(g1.keys()) + list(g2.keys())) graph = {}  for k in all_keys: if k not in graph:  if k in g1: graph[k] = g1[k] else: graph[k] = g2[k]  if k in g2: for x in g2[k]: ind = find_index() if ind != -1: if x[0] != []: t1 = x[0].split('|') if graph[k][ind] != -1: t2 = graph[k][ind][0].split('|') z = \"|\".join(set(t1+t2)) xf = [z, graph[k][ind][1]] graph[k][ind] = xf else: graph[k].append(x)  return graph   #--------- main ---------#     if __name__ == \"__main__\":  g = { \"1\": [[\"2\", \"11\"], [\"10\", \"15\"]], \"2\": [[\"3\", \"11\"], [\"9\", \"12\"]],  \"3\": [[\"4\", \"11\"], [\"2\", \"13\"], [\"6\", \"12\"]],  \"4\": [[\"5|1\", \"11\"]],  \"5\": [[\"6\", \"11\"]],  \"6\": [[\"7\", \"11\"]],  \"7\": [[\"8\", \"11\"], [\"5\", \"12\"]],  \"8\": [[\"9\", \"11\"]],  #\"9\": [['1|5', '11']], '9':[], \"10\":[[\"5\", \"15\"]] }  hd = Hd(g) r_ = hd.get_ku(hd.graph) print(\"\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0433\u0440\u0430\u0444: \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 ku = \", r_[0]) hd.show(hd.graph); print()  epochs = 50 ku_list = [[],[]] gf = [{}, {}]  for i in range(epochs): print(\"\\n_____\u042d\u041f\u041e\u0425\u0410 = \", i+1) x = random.choice(list(g.keys())); print(\"\u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0441 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \", x) #x = '1'  x_list, f_list = hd.get_states(x, [], n=20)  path = hd.get_yfx(f_list, x_list) path_yfx = hd.get_yfx(f_list, x_list, t = False) '''if path_yfx: print(\"y=f(x) :\", *path_yfx) pass'''  print(\"  \u041d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435\") for fl in hd.flow(path, rnd=False): print(*fl)  print(\"\\n  \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0430 1: \") gn = hd.fORx(hd.flow(path), f=0) hd.show(gn) gf[0] = hd.merge(gf[0], gn) r = hd.get_ku(gf[0]) print(\"\\n\u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 ku = \", r[0]) ku_list[0].append(r[0]) hd.show(gf[0]) print()  print(\"\\n  \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0430 2: \") gn = hd.fORx(hd.flow(path), f=1) hd.show(gn) gf[1] = hd.merge(gf[1], gn) r = hd.get_ku(gf[1]) print(\"\\n\u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 ku = \", r[0]) ku_list[1].append(r[0]) hd.show(gf[1])  print() fig, ax = plt.subplots() ax.plot([i+1 for i in range(epochs)], ku_list[0], marker=\"*\", color='g') ax.plot([i+1 for i in range(epochs)], ku_list[1], marker=\"+\", color='b') ax.plot([i+1 for i in range(epochs)], [r_[0]]*epochs,  color='r') ax.set_xlabel('\u044d\u043f\u043e\u0445\u0430') ax.set_ylabel('\u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438') plt.show() <\/code><\/pre>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/post\/673616\/\"> https:\/\/habr.com\/ru\/post\/673616\/<\/a><br \/><\/br><\/br><\/p>\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-335046","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335046","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=335046"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335046\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}