{"id":81416,"date":"2024-11-25T16:00:36","date_gmt":"2024-11-25T09:00:36","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=81416"},"modified":"2024-11-25T16:00:36","modified_gmt":"2024-11-25T09:00:36","slug":"cau-hoi-phong-van-python","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/","title":{"rendered":"T\u1ed5ng h\u1ee3p 25+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Python_la_gi\" >Python l\u00e0 g\u00ec?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Nhung_vi_tri_can_nam_cac_cau_hoi_phong_van_Python\" >Nh\u1eefng v\u1ecb tr\u00ed c\u1ea7n n\u1eafm c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Danh_sach_cac_chu_de_cau_hoi_phong_van_Python_trong_bai_viet\" >Danh s\u00e1ch c\u00e1c ch\u1ee7 \u0111\u1ec1 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python trong b\u00e0i vi\u1ebft<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Sap_xep_Sorting_Algorithm\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n S\u1eafp x\u1ebfp (Sorting Algorithm)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Tim_kiem_Search_Algorithm\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n T\u00ecm ki\u1ebfm (Search Algorithm)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_cau_hoi_phong_van_Python_ve_Cau_truc_Du_lieu_Co_ban_Basic_Data_Structure\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 C\u1ea5u tr\u00fac D\u1eef li\u1ec7u C\u01a1 b\u1ea3n (Basic Data Structure)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_tren_Do_thi_Graph_Algorithm\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n tr\u00ean \u0110\u1ed3 th\u1ecb (Graph Algorithm)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Quy_hoach_Dong_Dynamic_Programming\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n Quy ho\u1ea1ch \u0110\u1ed9ng (Dynamic Programming)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Tham_lam_Greedy_Algorithm\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n Tham lam (Greedy Algorithm)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Xu_ly_Chuoi_String_Algorithm\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n X\u1eed l\u00fd Chu\u1ed7i (String Algorithm)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Toan_hoc_Mathematical_Algorithm\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n To\u00e1n h\u1ecdc (Mathematical Algorithm)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Cac_chu_de_pho_bien_khac_trong_cac_cau_hoi_phong_van_Python_Developer\" >C\u00e1c ch\u1ee7 \u0111\u1ec1 ph\u1ed5 bi\u1ebfn kh\u00e1c trong c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python Developer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#Tong_ket_cau_hoi_phong_van_Python\" >T\u1ed5ng k\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>\u0110\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c th\u00e0nh c\u00f4ng trong bu\u1ed5i ph\u1ecfng v\u1ea5n cho v\u1ecb tr\u00ed Python Developer, \u1ee9ng vi\u00ean kh\u00f4ng ch\u1ec9 c\u1ea7n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c n\u1ec1n t\u1ea3ng m\u00e0 c\u00f2n ph\u1ea3i \u0111\u1ed1i m\u1eb7t v\u1edbi nh\u1eefng c\u00e2u h\u1ecfi chuy\u00ean s\u00e2u, \u0111\u1eb7c bi\u1ec7t v\u1ec1 thu\u1eadt to\u00e1n v\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u. Sau \u0111\u00e2y l\u00e0 t\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python Developer, t\u1eadp trung v\u00e0o c\u00e1c c\u00e2u h\u1ecfi thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn, k\u00e8m theo c\u00e2u tr\u1ea3 l\u1eddi c\u1ef1c k\u1ef3 chi ti\u1ebft (c\u00f3 code).<\/strong><\/em><\/p>\n<p>\u0110\u1ecdc b\u00e0i vi\u1ebft sau \u0111\u1ec3 n\u1eafm c\u00e1ch tr\u1ea3 l\u1eddi c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python thu\u1ed9c ch\u1ee7 \u0111\u1ec1:<\/p>\n<ul>\n<li>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n S\u1eafp x\u1ebfp (Sorting Algorithm)<\/li>\n<li>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n T\u00ecm ki\u1ebfm (Search Algorithm)<\/li>\n<li>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 C\u1ea5u tr\u00fac D\u1eef li\u1ec7u C\u01a1 b\u1ea3n (Basic Data Structure)<\/li>\n<li>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1Thu\u1eadt to\u00e1n tr\u00ean \u0110\u1ed3 th\u1ecb (Graph Algorithm)<\/li>\n<li>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1Thu\u1eadt to\u00e1n Quy ho\u1ea1ch \u0110\u1ed9ng (Dynamic Programming)<\/li>\n<li>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n Tham lam (Greedy Algorithm)<\/li>\n<li>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1Thu\u1eadt to\u00e1n X\u1eed l\u00fd Chu\u1ed7i (String Algorithm)<\/li>\n<li>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1Thu\u1eadt to\u00e1n To\u00e1n h\u1ecdc (Mathematical Algorithm)<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Python_la_gi\"><\/span><b>Python l\u00e0 g\u00ec?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Python l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh c\u1ea5p cao, n\u1ed5i b\u1eadt v\u1edbi c\u00fa ph\u00e1p \u0111\u01a1n gi\u1ea3n v\u00e0 d\u1ec5 \u0111\u1ecdc. \u0110i\u1ec1u n\u00e0y gi\u00fap Python tr\u1edf th\u00e0nh l\u1ef1a ch\u1ecdn l\u00fd t\u01b0\u1edfng cho nh\u1eefng ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u h\u1ecdc l\u1eadp tr\u00ecnh c\u0169ng nh\u01b0 c\u00e1c chuy\u00ean gia trong ng\u00e0nh. B\u00ean c\u1ea1nh \u0111\u00f3, Python c\u00f2n c\u00f3 th\u01b0 vi\u1ec7n phong ph\u00fa, h\u1ed7 tr\u1ee3 m\u1ea1nh m\u1ebd trong c\u00e1c l\u0129nh v\u1ef1c nh\u01b0 ph\u00e2n t\u00edch d\u1eef li\u1ec7u, ph\u00e1t tri\u1ec3n AI, v\u00e0 web.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Python \u0111\u01b0\u1ee3c \u1ee9ng d\u1ee5ng r\u1ed9ng r\u00e3i trong nhi\u1ec1u l\u0129nh v\u1ef1c nh\u01b0 khoa h\u1ecdc d\u1eef li\u1ec7u, tr\u00ed tu\u1ec7 nh\u00e2n t\u1ea1o, ph\u00e1t tri\u1ec3n web, t\u1ef1 \u0111\u1ed9ng h\u00f3a, v\u00e0 c\u1ea3 trong ph\u00e1t tri\u1ec3n game. Nh\u1eefng c\u00f4ng ty c\u00f4ng ngh\u1ec7 l\u1edbn nh\u01b0 Google, Facebook, v\u00e0 Netflix \u0111\u1ec1u s\u1eed d\u1ee5ng Python \u0111\u1ec3 ph\u00e1t tri\u1ec3n s\u1ea3n ph\u1ea9m c\u1ee7a h\u1ecd.<\/span><\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/python-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Python l\u00e0 g\u00ec: T\u1ed5ng quan \u0111\u1ecbnh ngh\u0129a, C\u00fa ph\u00e1p v\u00e0 Th\u01b0 vi\u1ec7n Python<\/strong><\/a><\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Nhung_vi_tri_can_nam_cac_cau_hoi_phong_van_Python\"><\/span><b>Nh\u1eefng v\u1ecb tr\u00ed c\u1ea7n n\u1eafm c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">M\u1ed7i v\u1ecb tr\u00ed s\u1eed d\u1ee5ng Python l\u1ea1i c\u00f3 nh\u1eefng y\u00eau c\u1ea7u ri\u00eang v\u00e0 c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng<\/span><span style=\"font-weight: 400;\"> v\u1ea5n Python c\u0169ng s\u1ebd kh\u00e1c nhau. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c nh\u00f3m v\u1ecb tr\u00ed \u0111i\u1ec3n h\u00ecnh v\u00e0 c\u00e1c ch\u1ee7 \u0111\u1ec1 ph\u1ecfng v\u1ea5n th\u01b0\u1eddng g\u1eb7p:<\/span><\/p>\n<h3><span style=\"font-weight: 400;\"><strong>Nh\u00f3m Khoa h\u1ecdc D\u1eef li\u1ec7u (Data Science Team)<\/strong><\/span><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Nh\u00e0 khoa h\u1ecdc d\u1eef li\u1ec7u (Data Scientist)<\/b><span style=\"font-weight: 400;\">: T\u1eadp trung v\u00e0o kh\u1ea3 n\u0103ng x\u1eed l\u00fd d\u1eef li\u1ec7u l\u1edbn, ph\u00e2n t\u00edch s\u1ed1 li\u1ec7u v\u00e0 l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c th\u01b0 vi\u1ec7n nh\u01b0 Pandas, NumPy.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ef9 s\u01b0 d\u1eef li\u1ec7u (Data Engineer)<\/b><span style=\"font-weight: 400;\">: \u1ee8ng vi\u00ean c\u1ea7n n\u1eafm v\u1eefng c\u00e1ch thi\u1ebft k\u1ebf h\u1ec7 th\u1ed1ng x\u1eed l\u00fd d\u1eef li\u1ec7u, pipeline, v\u00e0 c\u00e1c c\u00f4ng c\u1ee5 ETL.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ef9 s\u01b0 h\u1ecdc m\u00e1y (Machine Learning Engineer)<\/b><span style=\"font-weight: 400;\">: C\u00e2u h\u1ecfi th\u01b0\u1eddng xoay quanh c\u00e1c thu\u1eadt to\u00e1n h\u1ecdc m\u00e1y v\u00e0 c\u00e1ch tri\u1ec3n khai m\u00f4 h\u00ecnh s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n nh\u01b0 TensorFlow, scikit-learn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chuy\u00ean vi\u00ean ph\u00e2n t\u00edch t\u00e0i ch\u00ednh \u0111\u1ecbnh l\u01b0\u1ee3ng<\/b><span style=\"font-weight: 400;\">: C\u1ea7n bi\u1ebft c\u00e1ch x\u00e2y d\u1ef1ng c\u00e1c m\u00f4 h\u00ecnh \u0111\u1ecbnh l\u01b0\u1ee3ng, s\u1eed d\u1ee5ng th\u01b0 vi\u1ec7n t\u00e0i ch\u00ednh v\u00e0 thu\u1eadt to\u00e1n t\u00ednh to\u00e1n nhanh.<\/span><\/li>\n<\/ul>\n<h3><strong>Nh\u00f3m Ph\u00e1t tri\u1ec3n (Developer Team)<\/strong><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>L\u1eadp tr\u00ecnh vi\u00ean Python<\/b><span style=\"font-weight: 400;\">: C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng t\u1eadp trung v\u00e0o l\u1eadp tr\u00ecnh c\u01a1 b\u1ea3n, x\u1eed l\u00fd file, v\u00e0 gi\u1ea3i quy\u1ebft c\u00e1c v\u1ea5n \u0111\u1ec1 li\u00ean quan \u0111\u1ebfn t\u1ed1i \u01b0u h\u00f3a m\u00e3 ngu\u1ed3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>L\u1eadp tr\u00ecnh vi\u00ean Web<\/b><span style=\"font-weight: 400;\">: C\u1ea7n hi\u1ec3u r\u00f5 v\u1ec1 c\u00e1c framework ph\u1ed5 bi\u1ebfn nh\u01b0 Django, Flask v\u00e0 c\u00e1c ki\u1ebfn th\u1ee9c v\u1ec1 backend.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ef9 s\u01b0 t\u1ef1 \u0111\u1ed9ng h\u00f3a (Automation Engineer)<\/b><span style=\"font-weight: 400;\">: C\u00e2u h\u1ecfi th\u01b0\u1eddng li\u00ean quan \u0111\u1ebfn vi\u1ec7c t\u1ea1o v\u00e0 qu\u1ea3n l\u00fd c\u00e1c script t\u1ef1 \u0111\u1ed9ng h\u00f3a, ki\u1ec3m th\u1eed h\u1ec7 th\u1ed1ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ef9 s\u01b0 DevOps<\/b><span style=\"font-weight: 400;\">: \u1ee8ng vi\u00ean c\u1ea7n am hi\u1ec3u v\u1ec1 vi\u1ec7c tri\u1ec3n khai m\u00e3 ngu\u1ed3n, CI\/CD, v\u00e0 qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng m\u00e1y ch\u1ee7.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Nh\u00e0 ph\u00e1t tri\u1ec3n IoT (Internet of Things)<\/b><span style=\"font-weight: 400;\">: Ph\u1ecfng v\u1ea5n c\u00f3 th\u1ec3 li\u00ean quan \u0111\u1ebfn l\u1eadp tr\u00ecnh nh\u00fang v\u00e0 giao ti\u1ebfp gi\u1eefa c\u00e1c thi\u1ebft b\u1ecb IoT.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chuy\u00ean vi\u00ean an ninh m\u1ea1ng<\/b><span style=\"font-weight: 400;\">: C\u00e1c c\u00e2u h\u1ecfi v\u1ec1 m\u00e3 h\u00f3a, b\u1ea3o m\u1eadt v\u00e0 c\u00e1ch x\u1eed l\u00fd c\u00e1c cu\u1ed9c t\u1ea5n c\u00f4ng b\u1ea3o m\u1eadt.<\/span><\/li>\n<\/ul>\n<h3><strong>Nh\u00f3m Ph\u00e1t tri\u1ec3n Game (Game Team)<\/strong><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Nh\u00e0 ph\u00e1t tri\u1ec3n game<\/b><span style=\"font-weight: 400;\">: C\u1ea7n hi\u1ec3u r\u00f5 v\u1ec1 c\u00e1c c\u00f4ng c\u1ee5 ph\u00e1t tri\u1ec3n game v\u00e0 c\u00e1ch l\u1eadp tr\u00ecnh t\u01b0\u01a1ng t\u00e1c th\u1eddi gian th\u1ef1c.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Danh_sach_cac_chu_de_cau_hoi_phong_van_Python_trong_bai_viet\"><\/span><strong>Danh s\u00e1ch c\u00e1c ch\u1ee7 \u0111\u1ec1 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python trong b\u00e0i vi\u1ebft<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Sau \u0111\u00e2y l\u00e0 c\u00e1c ch\u1ee7 \u0111\u1ec1 thu\u1eadt to\u00e1n quan tr\u1ecdng s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c h\u00e0nh v\u00e0 c\u00f3 kh\u1ea3 n\u0103ng xu\u1ea5t hi\u1ec7n trong b\u00e0i vi\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python n\u00e0y. M\u1ed7i ph\u1ea7n s\u1ebd \u0111\u01b0\u1ee3c tr\u00ecnh b\u00e0y th\u00e0nh c\u00e1c nh\u00f3m thu\u1eadt to\u00e1n k\u00e8m theo v\u00ed d\u1ee5 v\u00e0 di\u1ec5n gi\u1ea3i chi ti\u1ebft.<\/span><\/p>\n<h3><b>Thu\u1eadt to\u00e1n S\u1eafp x\u1ebfp (Sorting Algorithm)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp l\u00e0 c\u00e1c ph\u01b0\u01a1ng ph\u00e1p \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 s\u1eafp x\u1ebfp m\u1ed9t t\u1eadp h\u1ee3p d\u1eef li\u1ec7u theo m\u1ed9t th\u1ee9 t\u1ef1 nh\u1ea5t \u0111\u1ecbnh (t\u0103ng d\u1ea7n ho\u1eb7c gi\u1ea3m d\u1ea7n). Vi\u1ec7c s\u1eafp x\u1ebfp d\u1eef li\u1ec7u gi\u00fap t\u0103ng hi\u1ec7u qu\u1ea3 trong vi\u1ec7c t\u00ecm ki\u1ebfm v\u00e0 qu\u1ea3n l\u00fd th\u00f4ng tin.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Bubble Sort<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp n\u1ed5i b\u1ecdt ho\u1ea1t \u0111\u1ed9ng b\u1eb1ng c\u00e1ch li\u00ean t\u1ee5c so s\u00e1nh v\u00e0 ho\u00e1n \u0111\u1ed5i c\u00e1c c\u1eb7p ph\u1ea7n t\u1eed li\u1ec1n k\u1ec1 n\u1ebfu ch\u00fang \u1edf sai th\u1ee9 t\u1ef1. Qu\u00e1 tr\u00ecnh n\u00e0y \u0111\u01b0\u1ee3c l\u1eb7p l\u1ea1i cho \u0111\u1ebfn khi kh\u00f4ng c\u00f2n c\u1eb7p ph\u1ea7n t\u1eed n\u00e0o c\u1ea7n ho\u00e1n \u0111\u1ed5i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Insertion Sort<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n ch\u00e8n tr\u1ef1c ti\u1ebfp x\u00e2y d\u1ef1ng danh s\u00e1ch s\u1eafp x\u1ebfp b\u1eb1ng c\u00e1ch l\u1ea5y t\u1eebng ph\u1ea7n t\u1eed v\u00e0 ch\u00e8n n\u00f3 v\u00e0o v\u1ecb tr\u00ed \u0111\u00fang trong danh s\u00e1ch \u0111\u00e3 s\u1eafp x\u1ebfp tr\u01b0\u1edbc \u0111\u00f3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Selection Sort<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n ch\u1ecdn tr\u1ef1c ti\u1ebfp t\u00ecm ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t (ho\u1eb7c l\u1edbn nh\u1ea5t) trong danh s\u00e1ch v\u00e0 \u0111\u1eb7t n\u00f3 \u1edf v\u1ecb tr\u00ed \u0111\u1ea7u ti\u00ean, sau \u0111\u00f3 l\u1eb7p l\u1ea1i qu\u00e1 tr\u00ecnh cho ph\u1ea7n c\u00f2n l\u1ea1i c\u1ee7a danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Quick Sort<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp nhanh s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p chia \u0111\u1ec3 tr\u1ecb b\u1eb1ng c\u00e1ch ch\u1ecdn m\u1ed9t ph\u1ea7n t\u1eed l\u00e0m &#8220;pivot&#8221; v\u00e0 ph\u00e2n chia danh s\u00e1ch th\u00e0nh hai ph\u1ea7n d\u1ef1a tr\u00ean pivot, sau \u0111\u00f3 s\u1eafp x\u1ebfp \u0111\u1ec7 quy c\u00e1c ph\u1ea7n n\u00e0y.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Merge Sort<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n tr\u1ed9n s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p chia \u0111\u1ec3 tr\u1ecb b\u1eb1ng c\u00e1ch chia danh s\u00e1ch th\u00e0nh hai n\u1eeda, s\u1eafp x\u1ebfp t\u1eebng n\u1eeda v\u00e0 sau \u0111\u00f3 tr\u1ed9n hai n\u1eeda \u0111\u00e3 s\u1eafp x\u1ebfp l\u1ea1i v\u1edbi nhau.<\/span><\/li>\n<\/ul>\n<h3><b>Thu\u1eadt to\u00e1n T\u00ecm ki\u1ebfm (Search Algorithm)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Thu\u1eadt to\u00e1n t\u00ecm ki\u1ebfm \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u00ecm m\u1ed9t ph\u1ea7n t\u1eed c\u1ee5 th\u1ec3 trong m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u. Ch\u00fang gi\u00fap t\u0103ng t\u1ed1c \u0111\u1ed9 truy xu\u1ea5t v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Linear Search<\/b><span style=\"font-weight: 400;\">: T\u00ecm ki\u1ebfm tuy\u1ebfn t\u00ednh duy\u1ec7t qua t\u1eebng ph\u1ea7n t\u1eed trong danh s\u00e1ch cho \u0111\u1ebfn khi t\u00ecm th\u1ea5y ph\u1ea7n t\u1eed c\u1ea7n t\u00ecm ho\u1eb7c \u0111\u1ebfn cu\u1ed1i danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Binary Search<\/b><span style=\"font-weight: 400;\">: T\u00ecm ki\u1ebfm nh\u1ecb ph\u00e2n y\u00eau c\u1ea7u danh s\u00e1ch \u0111\u00e3 \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp. Thu\u1eadt to\u00e1n n\u00e0y so s\u00e1nh ph\u1ea7n t\u1eed c\u1ea7n t\u00ecm v\u1edbi ph\u1ea7n t\u1eed \u1edf gi\u1eefa danh s\u00e1ch v\u00e0 lo\u1ea1i b\u1ecf m\u1ed9t n\u1eeda danh s\u00e1ch trong m\u1ed7i b\u01b0\u1edbc, gi\u00fap gi\u1ea3m \u0111\u00e1ng k\u1ec3 s\u1ed1 l\u1ea7n so s\u00e1nh.<\/span><\/li>\n<\/ul>\n<h3><b>C\u1ea5u tr\u00fac D\u1eef li\u1ec7u C\u01a1 b\u1ea3n (Basic Data Structure)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">C\u1ea5u tr\u00fac d\u1eef li\u1ec7u l\u00e0 c\u00e1ch t\u1ed5 ch\u1ee9c v\u00e0 l\u01b0u tr\u1eef d\u1eef li\u1ec7u \u0111\u1ec3 truy c\u1eadp v\u00e0 s\u1eeda \u0111\u1ed5i m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Stack<\/b><span style=\"font-weight: 400;\">: Ng\u0103n x\u1ebfp l\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u LIFO (Last In, First Out), n\u01a1i ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c th\u00eam v\u00e0o v\u00e0 l\u1ea5y ra \u1edf c\u00f9ng m\u1ed9t \u0111\u1ea7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Queue<\/b><span style=\"font-weight: 400;\">: H\u00e0ng \u0111\u1ee3i l\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u FIFO (First In, First Out), n\u01a1i ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c th\u00eam v\u00e0o \u1edf cu\u1ed1i v\u00e0 l\u1ea5y ra \u1edf \u0111\u1ea7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Linked List<\/b><span style=\"font-weight: 400;\">: Danh s\u00e1ch li\u00ean k\u1ebft l\u00e0 m\u1ed9t chu\u1ed7i c\u00e1c node, m\u1ed7i node ch\u1ee9a d\u1eef li\u1ec7u v\u00e0 con tr\u1ecf \u0111\u1ebfn node ti\u1ebfp theo trong danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Tree<\/b><span style=\"font-weight: 400;\">: C\u00e2y l\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u ph\u00e2n c\u1ea5p v\u1edbi m\u1ed9t node g\u1ed1c v\u00e0 c\u00e1c node con, \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 bi\u1ec3u di\u1ec5n d\u1eef li\u1ec7u c\u00f3 m\u1ed1i quan h\u1ec7 th\u1ee9 b\u1eadc.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Graph<\/b><span style=\"font-weight: 400;\">: \u0110\u1ed3 th\u1ecb l\u00e0 t\u1eadp h\u1ee3p c\u00e1c \u0111\u1ec9nh (node) v\u00e0 c\u00e1c c\u1ea1nh (edge) k\u1ebft n\u1ed1i c\u00e1c \u0111\u1ec9nh, d\u00f9ng \u0111\u1ec3 bi\u1ec3u di\u1ec5n c\u00e1c m\u1ed1i quan h\u1ec7 ph\u1ee9c t\u1ea1p gi\u1eefa c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/li>\n<\/ul>\n<h3><b>Thu\u1eadt to\u00e1n tr\u00ean \u0110\u1ed3 th\u1ecb (Graph Algorithm)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Thu\u1eadt to\u00e1n \u0111\u1ed3 th\u1ecb \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u1eed l\u00fd v\u00e0 ph\u00e2n t\u00edch c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u d\u1ea1ng \u0111\u1ed3 th\u1ecb.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Depth-First Search (DFS)<\/b><span style=\"font-weight: 400;\">: Duy\u1ec7t theo chi\u1ec1u s\u00e2u, kh\u00e1m ph\u00e1 c\u00e0ng xa c\u00e0ng t\u1ed1t theo m\u1ed7i nh\u00e1nh tr\u01b0\u1edbc khi quay l\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Breadth-First Search (BFS)<\/b><span style=\"font-weight: 400;\">: Duy\u1ec7t theo chi\u1ec1u r\u1ed9ng, kh\u00e1m ph\u00e1 t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ec9nh \u1edf m\u1ee9c hi\u1ec7n t\u1ea1i tr\u01b0\u1edbc khi chuy\u1ec3n sang m\u1ee9c ti\u1ebfp theo.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Dijkstra&#8217;s Algorithm<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n Dijkstra t\u00ecm \u0111\u01b0\u1eddng \u0111i ng\u1eafn nh\u1ea5t t\u1eeb m\u1ed9t \u0111\u1ec9nh ngu\u1ed3n \u0111\u1ebfn t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ec9nh kh\u00e1c trong \u0111\u1ed3 th\u1ecb v\u1edbi tr\u1ecdng s\u1ed1 kh\u00f4ng \u00e2m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kruskal&#8217;s Algorithm<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n Kruskal t\u00ecm c\u00e2y khung nh\u1ecf nh\u1ea5t c\u1ee7a m\u1ed9t \u0111\u1ed3 th\u1ecb b\u1eb1ng c\u00e1ch s\u1eafp x\u1ebfp c\u00e1c c\u1ea1nh theo tr\u1ecdng s\u1ed1 v\u00e0 th\u00eam ch\u00fang v\u00e0o c\u00e2y khung n\u1ebfu kh\u00f4ng t\u1ea1o th\u00e0nh chu tr\u00ecnh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Prim&#8217;s Algorithm<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n Prim c\u0169ng t\u00ecm c\u00e2y khung nh\u1ecf nh\u1ea5t, b\u1eaft \u0111\u1ea7u t\u1eeb m\u1ed9t \u0111\u1ec9nh v\u00e0 li\u00ean t\u1ee5c th\u00eam c\u1ea1nh nh\u1ecf nh\u1ea5t k\u1ebft n\u1ed1i \u0111\u1ec9nh \u0111\u00e3 th\u0103m v\u00e0 \u0111\u1ec9nh ch\u01b0a th\u0103m.<\/span><\/li>\n<\/ul>\n<h3><b>Thu\u1eadt to\u00e1n Quy ho\u1ea1ch \u0110\u1ed9ng (Dynamic Programming)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Quy ho\u1ea1ch \u0111\u1ed9ng l\u00e0 k\u1ef9 thu\u1eadt gi\u1ea3i quy\u1ebft c\u00e1c v\u1ea5n \u0111\u1ec1 b\u1eb1ng c\u00e1ch chia nh\u1ecf ch\u00fang th\u00e0nh c\u00e1c b\u00e0i to\u00e1n con v\u00e0 l\u01b0u tr\u1eef k\u1ebft qu\u1ea3 \u0111\u1ec3 tr\u00e1nh t\u00ednh to\u00e1n l\u1ea1i.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Knapsack Problem<\/b><span style=\"font-weight: 400;\">: B\u00e0i to\u00e1n c\u00e1i t\u00fai, m\u1ee5c ti\u00eau l\u00e0 ch\u1ecdn c\u00e1c v\u1eadt ph\u1ea9m \u0111\u1ec3 t\u1ed1i \u0111a h\u00f3a gi\u00e1 tr\u1ecb t\u1ed5ng trong khi kh\u00f4ng v\u01b0\u1ee3t qu\u00e1 gi\u1edbi h\u1ea1n tr\u1ecdng l\u01b0\u1ee3ng c\u1ee7a t\u00fai.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Longest Common Subsequence<\/b><span style=\"font-weight: 400;\">: T\u00ecm chu\u1ed7i con chung d\u00e0i nh\u1ea5t gi\u1eefa hai chu\u1ed7i, \u1ee9ng d\u1ee5ng trong so s\u00e1nh v\u0103n b\u1ea3n v\u00e0 ph\u00e2n t\u00edch DNA.<\/span><\/li>\n<\/ul>\n<h3><b>Thu\u1eadt to\u00e1n Tham lam (Greedy Algorithm)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Thu\u1eadt to\u00e1n tham lam ch\u1ecdn l\u1ef1a t\u1ed1i \u01b0u c\u1ee5c b\u1ed9 t\u1ea1i m\u1ed7i b\u01b0\u1edbc v\u1edbi hy v\u1ecdng \u0111\u1ea1t \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u to\u00e0n c\u1ee5c.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Coin Change Problem<\/b><span style=\"font-weight: 400;\">: B\u00e0i to\u00e1n \u0111\u1ed5i ti\u1ec1n, t\u00ecm s\u1ed1 l\u01b0\u1ee3ng \u00edt nh\u1ea5t c\u00e1c \u0111\u1ed3ng xu \u0111\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c m\u1ed9t s\u1ed1 ti\u1ec1n nh\u1ea5t \u0111\u1ecbnh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Activity Selection Problem<\/b><span style=\"font-weight: 400;\">: Ch\u1ecdn t\u1ed1i \u0111a s\u1ed1 ho\u1ea1t \u0111\u1ed9ng kh\u00f4ng ch\u1ed3ng ch\u00e9o nhau d\u1ef1a tr\u00ean th\u1eddi gian b\u1eaft \u0111\u1ea7u v\u00e0 k\u1ebft th\u00fac.<\/span><\/li>\n<\/ul>\n<h3><b>Thu\u1eadt to\u00e1n X\u1eed l\u00fd Chu\u1ed7i (String Algorithm)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Thu\u1eadt to\u00e1n x\u1eed l\u00fd chu\u1ed7i \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 gi\u1ea3i quy\u1ebft c\u00e1c v\u1ea5n \u0111\u1ec1 li\u00ean quan \u0111\u1ebfn chu\u1ed7i k\u00fd t\u1ef1.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>KMP Algorithm<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n Knuth-Morris-Pratt t\u00ecm ki\u1ebfm m\u1eabu trong v\u0103n b\u1ea3n b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng b\u1ea3ng l\u1eb7p l\u1ea1i \u0111\u1ec3 tr\u00e1nh so s\u00e1nh l\u1ea1i c\u00e1c k\u00fd t\u1ef1 \u0111\u00e3 bi\u1ebft.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Rabin-Karp Algorithm<\/b><span style=\"font-weight: 400;\">: S\u1eed d\u1ee5ng h\u00e0m b\u0103m \u0111\u1ec3 t\u00ecm ki\u1ebfm chu\u1ed7i m\u1eabu trong v\u0103n b\u1ea3n, hi\u1ec7u qu\u1ea3 khi t\u00ecm ki\u1ebfm nhi\u1ec1u m\u1eabu c\u00f9ng l\u00fac.<\/span><\/li>\n<\/ul>\n<h3><b>Thu\u1eadt to\u00e1n To\u00e1n h\u1ecdc (Mathematical Algorithm)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Thu\u1eadt to\u00e1n to\u00e1n h\u1ecdc gi\u1ea3i quy\u1ebft c\u00e1c v\u1ea5n \u0111\u1ec1 s\u1ed1 h\u1ecdc v\u00e0 l\u00fd thuy\u1ebft s\u1ed1.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Euclidean Algorithm<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n Euclid t\u00ecm \u01b0\u1edbc s\u1ed1 chung l\u1edbn nh\u1ea5t (GCD) c\u1ee7a hai s\u1ed1 nguy\u00ean b\u1eb1ng c\u00e1ch l\u1eb7p l\u1ea1i ph\u00e9p chia.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Sieve of Eratosthenes<\/b><span style=\"font-weight: 400;\">: Thu\u1eadt to\u00e1n s\u00e0ng Eratosthenes t\u00ecm t\u1ea5t c\u1ea3 c\u00e1c s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf h\u01a1n m\u1ed9t s\u1ed1 n b\u1eb1ng c\u00e1ch lo\u1ea1i b\u1ecf c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a m\u1ed7i s\u1ed1 nguy\u00ean t\u1ed1.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Sap_xep_Sorting_Algorithm\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n S\u1eafp x\u1ebfp (Sorting Algorithm)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1 Bubble Sort<\/b><\/h3>\n<p><b>Bubble Sort<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp \u0111\u01a1n gi\u1ea3n ho\u1ea1t \u0111\u1ed9ng b\u1eb1ng c\u00e1ch l\u1eb7p \u0111i l\u1eb7p l\u1ea1i qua danh s\u00e1ch c\u1ea7n s\u1eafp x\u1ebfp, so s\u00e1nh t\u1eebng c\u1eb7p ph\u1ea7n t\u1eed li\u1ec1n k\u1ec1 v\u00e0 ho\u00e1n \u0111\u1ed5i ch\u00fang n\u1ebfu ch\u00fang \u1edf sai th\u1ee9 t\u1ef1.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 nguy\u00ean ch\u01b0a \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp. H\u00e3y s\u1eed d\u1ee5ng thu\u1eadt to\u00e1n <\/span><b>Bubble Sort<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 s\u1eafp x\u1ebfp danh s\u00e1ch n\u00e0y theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh s\u1eafp x\u1ebfp t\u1eebng b\u01b0\u1edbc.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">[64, 34, 25, 12, 22, 11, 90]<\/span><\/p>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> bubble_sort<\/span><span style=\"font-weight: 400;\">(arr)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 n = len(arr)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Duy\u1ec7t qua t\u1ea5t c\u1ea3 c\u00e1c ph\u1ea7n t\u1eed c\u1ee7a m\u1ea3ng<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(n):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Duy\u1ec7t qua m\u1ea3ng t\u1eeb 0 \u0111\u1ebfn n-i-1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> j <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, n - i - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># So s\u00e1nh v\u00e0 ho\u00e1n \u0111\u1ed5i n\u1ebfu ph\u1ea7n t\u1eed t\u00ecm th\u1ea5y l\u1edbn h\u01a1n ph\u1ea7n t\u1eed k\u1ebf ti\u1ebfp<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> arr[j] &gt; arr[j + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 arr[j], arr[j + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] = arr[j + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">], arr[j]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">{i+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">arr = [<\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">bubble_sort(arr)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: [<\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">34<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">90<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> Ph\u1ea7n t\u1eed l\u1edbn nh\u1ea5t &#8220;n\u1ed5i&#8221; l\u00ean cu\u1ed1i danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 2-5:<\/b><span style=\"font-weight: 400;\"> Ti\u1ebfp t\u1ee5c &#8220;n\u1ed5i&#8221; c\u00e1c ph\u1ea7n t\u1eed l\u1edbn c\u00f2n l\u1ea1i l\u00ean v\u1ecb tr\u00ed \u0111\u00fang.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 6-7:<\/b><span style=\"font-weight: 400;\"> Kh\u00f4ng c\u00f2n s\u1ef1 ho\u00e1n \u0111\u1ed5i, thu\u1eadt to\u00e1n k\u1ebft th\u00fac.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1 Insertion Sort<\/b><\/h3>\n<p><b>Insertion Sort<\/b><span style=\"font-weight: 400;\"> x\u00e2y d\u1ef1ng danh s\u00e1ch s\u1eafp x\u1ebfp cu\u1ed1i c\u00f9ng m\u1ed9t c\u00e1ch d\u1ea7n d\u1ea7n. M\u1ed7i ph\u1ea7n t\u1eed t\u1eeb danh s\u00e1ch ch\u01b0a s\u1eafp x\u1ebfp \u0111\u01b0\u1ee3c ch\u00e8n v\u00e0o v\u1ecb tr\u00ed \u0111\u00fang trong danh s\u00e1ch \u0111\u00e3 s\u1eafp x\u1ebfp.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 nguy\u00ean ch\u01b0a \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp. S\u1eed d\u1ee5ng thu\u1eadt to\u00e1n <\/span><b>Insertion Sort<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 s\u1eafp x\u1ebfp danh s\u00e1ch theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh ch\u00e8n t\u1eebng ph\u1ea7n t\u1eed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">[12, 11, 13, 5, 6]<\/span><\/p>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> insertion_sort<\/span><span style=\"font-weight: 400;\">(arr)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Duy\u1ec7t qua t\u1eeb ph\u1ea7n t\u1eed th\u1ee9 hai \u0111\u1ebfn h\u1ebft<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, len(arr)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 key = arr[i]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Di chuy\u1ec3n c\u00e1c ph\u1ea7n t\u1eed l\u1edbn h\u01a1n key v\u1ec1 sau m\u1ed9t v\u1ecb tr\u00ed<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 j = i - <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> j &gt;=<\/span><span style=\"font-weight: 400;\">0<\/span> <span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> key &lt; arr[j]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 arr[j + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] = arr[j]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 j -= <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 arr[j + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] = key<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">{i}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">arr = [<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">insertion_sort(arr)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: [<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: [<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> Ch\u00e8n <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\"> v\u00e0o v\u1ecb tr\u00ed \u0111\u1ee9ng tr\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 2:<\/b><span style=\"font-weight: 400;\"> Ph\u1ea7n t\u1eed \u0111ang x\u00e9t l\u00e0 13, \u0111ang \u1edf \u0111\u00fang v\u1ecb tr\u00ed n\u00ean kh\u00f4ng thay \u0111\u1ed5i<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 3:<\/b><span style=\"font-weight: 400;\"> Ch\u00e8n <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> v\u00e0o \u0111\u1ea7u danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 4:<\/b><span style=\"font-weight: 400;\"> Ch\u00e8n <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\"> v\u00e0o v\u1ecb tr\u00ed gi\u1eefa <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1 Selection Sort<\/b><\/h3>\n<p><b>Selection Sort<\/b><span style=\"font-weight: 400;\"> ho\u1ea1t \u0111\u1ed9ng b\u1eb1ng c\u00e1ch li\u00ean t\u1ee5c t\u00ecm ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t trong danh s\u00e1ch ch\u01b0a \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp v\u00e0 ho\u00e1n \u0111\u1ed5i n\u00f3 v\u1edbi ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean c\u1ee7a danh s\u00e1ch ch\u01b0a s\u1eafp x\u1ebfp.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 nguy\u00ean ch\u01b0a \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp. S\u1eed d\u1ee5ng thu\u1eadt to\u00e1n <\/span><b>Selection Sort<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 s\u1eafp x\u1ebfp danh s\u00e1ch theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh ch\u1ecdn v\u00e0 ho\u00e1n \u0111\u1ed5i t\u1eebng ph\u1ea7n t\u1eed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">[64, 25, 12, 22, 11]<\/span><\/p>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> selection_sort<\/span><span style=\"font-weight: 400;\">(arr)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(len(arr)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 min_idx = i<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># T\u00ecm ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t trong danh s\u00e1ch ch\u01b0a s\u1eafp x\u1ebfp<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> j <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(i+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, len(arr)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> arr[min_idx] &gt; arr[j]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 min_idx = j<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Ho\u00e1n \u0111\u1ed5i ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t v\u1edbi ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 arr[i], arr[min_idx] = arr[min_idx], arr[i]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">{i+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">arr = [<\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">selection_sort(arr)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: [<\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> T\u00ecm ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t (<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">) v\u00e0 ho\u00e1n \u0111\u1ed5i v\u1edbi <\/span><span style=\"font-weight: 400;\">64<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 2:<\/b><span style=\"font-weight: 400;\"> T\u00ecm ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t c\u00f2n l\u1ea1i (<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">) v\u00e0 ho\u00e1n \u0111\u1ed5i v\u1edbi <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 3:<\/b><span style=\"font-weight: 400;\"> T\u00ecm ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t c\u00f2n l\u1ea1i (<\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\">) v\u00e0 ho\u00e1n \u0111\u1ed5i v\u1edbi <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 4 v\u00e0 5: <\/b><span style=\"font-weight: 400;\">x\u00e9t 2 ph\u1ea7n th\u1eed 25 v\u00e0 64 \u0111\u00e3 \u1edf \u0111\u00fang th\u1ee9 t\u1ef1 n\u00ean kh\u00f4ng ho\u00e1n \u0111\u1ed5i<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Quick Sort<\/b><\/h3>\n<p><b>Quick Sort<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp ph\u00e2n chia v\u00e0 tr\u1ecb. N\u00f3 ch\u1ecdn m\u1ed9t &#8220;ch\u1ed1t&#8221; (pivot) v\u00e0 ph\u00e2n chia danh s\u00e1ch th\u00e0nh hai ph\u1ea7n: ph\u1ea7n nh\u1ecf h\u01a1n ch\u1ed1t v\u00e0 ph\u1ea7n l\u1edbn h\u01a1n ch\u1ed1t.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 nguy\u00ean ch\u01b0a \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp. S\u1eed d\u1ee5ng thu\u1eadt to\u00e1n <\/span><b>Quick Sort<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 s\u1eafp x\u1ebfp danh s\u00e1ch theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh ph\u00e2n chia v\u00e0 s\u1eafp x\u1ebfp.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">[10, 7, 8, 9, 1, 5]<\/span><\/p>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><b><\/b><\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> partition<\/span><span style=\"font-weight: 400;\">(arr, low, high)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 i = low - <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 pivot = arr[high]\u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u1ecdn ch\u1ed1t l\u00e0 ph\u1ea7n t\u1eed cu\u1ed1i<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> j <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(low, high):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># N\u1ebfu ph\u1ea7n t\u1eed hi\u1ec7n t\u1ea1i nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng ch\u1ed1t<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> arr[j] &lt;= pivot:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 arr[i], arr[j] = arr[j], arr[i]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Ho\u00e1n \u0111\u1ed5i ch\u1ed1t v\u00e0o v\u1ecb tr\u00ed \u0111\u00fang<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 arr[i+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">], arr[high] = arr[high], arr[i+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ph\u00e2n \u0111o\u1ea1n v\u1edbi ch\u1ed1t <\/span><span style=\"font-weight: 400;\">{pivot}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> i + <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> quick_sort<\/span><span style=\"font-weight: 400;\">(arr, low, high)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> low &lt; high:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># pi l\u00e0 ch\u1ec9 s\u1ed1 ph\u00e2n chia, arr[pi] \u0111\u00e3 \u1edf v\u1ecb tr\u00ed \u0111\u00fang<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 pi = partition(arr, low, high)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1eafp x\u1ebfp \u0111\u1ec7 quy c\u00e1c ph\u1ea7n t\u1eed tr\u01b0\u1edbc v\u00e0 sau ph\u00e2n chia<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 quick_sort(arr, low, pi - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 quick_sort(arr, pi + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, high)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">arr = [<\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">quick_sort(arr, <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, len(arr) - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: [<\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u00e2n \u0111o\u1ea1n v\u1edbi ch\u1ed1t <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u00e2n \u0111o\u1ea1n v\u1edbi ch\u1ed1t <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u00e2n \u0111o\u1ea1n v\u1edbi ch\u1ed1t <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u00e2n \u0111o\u1ea1n v\u1edbi ch\u1ed1t <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u00e2n \u0111o\u1ea1n v\u1edbi ch\u1ed1t <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: [<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ph\u00e2n \u0111o\u1ea1n 1:<\/b><span style=\"font-weight: 400;\"> Ch\u1ecdn ch\u1ed1t <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, s\u1eafp x\u1ebfp c\u00e1c ph\u1ea7n t\u1eed nh\u1ecf h\u01a1n b\u00ean tr\u00e1i v\u00e0 l\u1edbn h\u01a1n b\u00ean ph\u1ea3i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ph\u00e2n \u0111o\u1ea1n 2-5:<\/b><span style=\"font-weight: 400;\"> Ti\u1ebfp t\u1ee5c \u00e1p d\u1ee5ng Quick Sort \u0111\u1ec7 quy l\u00ean c\u00e1c ph\u00e2n \u0111o\u1ea1n nh\u1ecf h\u01a1n.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Merge Sort<\/b><\/h3>\n<p><b>Merge Sort<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp ph\u00e2n chia v\u00e0 tr\u1ecb. N\u00f3 chia danh s\u00e1ch th\u00e0nh hai n\u1eeda, s\u1eafp x\u1ebfp t\u1eebng n\u1eeda v\u00e0 sau \u0111\u00f3 g\u1ed9p hai n\u1eeda \u0111\u00e3 s\u1eafp x\u1ebfp l\u1ea1i.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 nguy\u00ean ch\u01b0a \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp. S\u1eed d\u1ee5ng thu\u1eadt to\u00e1n <\/span><b>Merge Sort<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 s\u1eafp x\u1ebfp danh s\u00e1ch theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh chia v\u00e0 g\u1ed9p.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">[12, 11, 13, 5, 6, 7]<\/span><\/p>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><b><\/b><\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> merge<\/span><span style=\"font-weight: 400;\">(arr, l, m, r)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># T\u1ea1o b\u1ea3n sao c\u1ee7a hai n\u1eeda<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 L = arr[l:m+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 R = arr[m+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">:r+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 i = j = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u1ec9 s\u1ed1 ban \u0111\u1ea7u c\u1ee7a hai n\u1eeda<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 k = l\u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u1ec9 s\u1ed1 ban \u0111\u1ea7u c\u1ee7a m\u1ea3ng ph\u1ee5<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># G\u1ed9p hai n\u1eeda l\u1ea1i<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> i &lt; len(L) <\/span><span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> j &lt; len(R):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> L[i] &lt;= R[j]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 arr[k] = L[i]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 arr[k] = R[j]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 j += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 k += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Sao ch\u00e9p c\u00e1c ph\u1ea7n t\u1eed c\u00f2n l\u1ea1i c\u1ee7a L[]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> i &lt; len(L):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 arr[k] = L[i]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 i += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 k += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Sao ch\u00e9p c\u00e1c ph\u1ea7n t\u1eed c\u00f2n l\u1ea1i c\u1ee7a R[]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> j &lt; len(R):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 arr[k] = R[j]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 j += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 k += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"G\u1ed9p <\/span><span style=\"font-weight: 400;\">{arr[l:r+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> merge_sort<\/span><span style=\"font-weight: 400;\">(arr, l, r)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> l &lt; r:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 m = (l + r) \/\/ <\/span><span style=\"font-weight: 400;\">2<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1eafp x\u1ebfp n\u1eeda \u0111\u1ea7u ti\u00ean<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 merge_sort(arr, l, m)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1eafp x\u1ebfp n\u1eeda th\u1ee9 hai<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 merge_sort(arr, m + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, r)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># G\u1ed9p hai n\u1eeda \u0111\u00e3 s\u1eafp x\u1ebfp<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 merge(arr, l, m, r)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">arr = [<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch ban \u0111\u1ea7u: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">merge_sort(arr, <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, len(arr) - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch ban \u0111\u1ea7u: [<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">G\u1ed9p [<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">G\u1ed9p [<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">G\u1ed9p [<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">G\u1ed9p [<\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">G\u1ed9p [<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Danh s\u00e1ch sau khi s\u1eafp x\u1ebfp: [<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chia:<\/b><span style=\"font-weight: 400;\"> Danh s\u00e1ch \u0111\u01b0\u1ee3c chia th\u00e0nh c\u00e1c n\u1eeda nh\u1ecf h\u01a1n \u0111\u1ebfn khi m\u1ed7i ph\u1ea7n ch\u1ec9 c\u00f2n m\u1ed9t ph\u1ea7n t\u1eed.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>G\u1ed9p:<\/b><span style=\"font-weight: 400;\"> C\u00e1c ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c g\u1ed9p l\u1ea1i theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Tim_kiem_Search_Algorithm\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n T\u00ecm ki\u1ebfm (Search Algorithm)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Linear Search<\/b><\/h3>\n<p><b>Linear Search<\/b><span style=\"font-weight: 400;\"> (T\u00ecm ki\u1ebfm tuy\u1ebfn t\u00ednh) l\u00e0 m\u1ed9t thu\u1eadt to\u00e1n t\u00ecm ki\u1ebfm \u0111\u01a1n gi\u1ea3n, trong \u0111\u00f3 ta duy\u1ec7t qua t\u1eebng ph\u1ea7n t\u1eed c\u1ee7a danh s\u00e1ch t\u1eeb \u0111\u1ea7u \u0111\u1ebfn cu\u1ed1i cho \u0111\u1ebfn khi t\u00ecm th\u1ea5y gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm ho\u1eb7c \u0111\u1ebfn cu\u1ed1i danh s\u00e1ch m\u00e0 kh\u00f4ng t\u00ecm th\u1ea5y.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 nguy\u00ean v\u00e0 m\u1ed9t gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm ki\u1ebfm. H\u00e3y s\u1eed d\u1ee5ng thu\u1eadt to\u00e1n <\/span><b>Linear Search<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u00ecm ki\u1ebfm gi\u00e1 tr\u1ecb n\u00e0y trong danh s\u00e1ch. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh t\u00ecm ki\u1ebfm t\u1eebng b\u01b0\u1edbc.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Danh s\u00e1ch: <\/span><span style=\"font-weight: 400;\">[2, 3, 4, 10, 40]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm: <\/span><span style=\"font-weight: 400;\">10<\/span><\/p>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> linear_search<\/span><span style=\"font-weight: 400;\">(arr, x)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(len(arr)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ki\u1ec3m tra ph\u1ea7n t\u1eed t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">{i}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{arr[i]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> arr[i] == x:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> i<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">-1<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">arr = [<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">40<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">x = <\/span><span style=\"font-weight: 400;\">10<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm: <\/span><span style=\"font-weight: 400;\">{x}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">result = linear_search(arr, x)<\/span>\r\n<span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> result != <\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">{result}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"Ph\u1ea7n t\u1eed kh\u00f4ng c\u00f3 trong danh s\u00e1ch\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch: [<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">40<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm: <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ki\u1ec3m tra ph\u1ea7n t\u1eed t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ki\u1ec3m tra ph\u1ea7n t\u1eed t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ki\u1ec3m tra ph\u1ea7n t\u1eed t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ki\u1ec3m tra ph\u1ea7n t\u1eed t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">3<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> B\u1eaft \u0111\u1ea7u t\u1eeb ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean (<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">), so s\u00e1nh v\u1edbi gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm (<\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">). Kh\u00f4ng kh\u1edbp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 2:<\/b><span style=\"font-weight: 400;\"> Chuy\u1ec3n sang ph\u1ea7n t\u1eed ti\u1ebfp theo (<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">). Kh\u00f4ng kh\u1edbp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 3:<\/b><span style=\"font-weight: 400;\"> Ti\u1ebfp t\u1ee5c v\u1edbi ph\u1ea7n t\u1eed (<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">). Kh\u00f4ng kh\u1edbp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 4:<\/b><span style=\"font-weight: 400;\"> Ki\u1ec3m tra ph\u1ea7n t\u1eed (<\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">). Kh\u1edbp v\u1edbi gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ebft lu\u1eadn:<\/b><span style=\"font-weight: 400;\"> Gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Binary Search<\/b><\/h3>\n<p><b>Binary Search<\/b><span style=\"font-weight: 400;\"> (T\u00ecm ki\u1ebfm nh\u1ecb ph\u00e2n) l\u00e0 m\u1ed9t thu\u1eadt to\u00e1n t\u00ecm ki\u1ebfm hi\u1ec7u qu\u1ea3 cho c\u00e1c danh s\u00e1ch \u0111\u00e3 \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp. Thu\u1eadt to\u00e1n ho\u1ea1t \u0111\u1ed9ng b\u1eb1ng c\u00e1ch li\u00ean t\u1ee5c chia \u0111\u00f4i ph\u1ea1m vi t\u00ecm ki\u1ebfm, so s\u00e1nh gi\u00e1 tr\u1ecb \u1edf v\u1ecb tr\u00ed gi\u1eefa v\u1edbi gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm \u0111\u1ec3 quy\u1ebft \u0111\u1ecbnh ti\u1ebfp t\u1ee5c t\u00ecm ki\u1ebfm \u1edf n\u1eeda n\u00e0o c\u1ee7a danh s\u00e1ch.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 nguy\u00ean \u0111\u00e3 \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp theo th\u1ee9 t\u1ef1 t\u0103ng d\u1ea7n v\u00e0 m\u1ed9t gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm ki\u1ebfm. S\u1eed d\u1ee5ng thu\u1eadt to\u00e1n <\/span><b>Binary Search<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u00ecm ki\u1ebfm gi\u00e1 tr\u1ecb n\u00e0y trong danh s\u00e1ch. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh t\u00ecm ki\u1ebfm t\u1eebng b\u01b0\u1edbc.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Danh s\u00e1ch: <\/span><span style=\"font-weight: 400;\">[2, 3, 4, 10, 40]<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm: <\/span><span style=\"font-weight: 400;\">10<\/span><\/p>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> binary_search<\/span><span style=\"font-weight: 400;\">(arr, x)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 low = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 high = len(arr) - <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 step = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> low &lt;= high:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 mid = (low + high) \/\/ <\/span><span style=\"font-weight: 400;\">2<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 step += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">{step}<\/span><span style=\"font-weight: 400;\">: Ki\u1ec3m tra ph\u1ea7n t\u1eed \u1edf gi\u1eefa t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">{mid}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{arr[mid]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># N\u1ebfu ph\u1ea7n t\u1eed \u1edf gi\u1eefa ch\u00ednh l\u00e0 gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> arr[mid] == x:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> mid<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># N\u1ebfu gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm l\u1edbn h\u01a1n, b\u1ecf qua n\u1eeda b\u00ean tr\u00e1i<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">elif<\/span><span style=\"font-weight: 400;\"> arr[mid] &lt; x:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 low = mid + <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># N\u1ebfu gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm nh\u1ecf h\u01a1n, b\u1ecf qua n\u1eeda b\u00ean ph\u1ea3i<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 high = mid - <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">-1<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">arr = [<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">40<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">x = <\/span><span style=\"font-weight: 400;\">10<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch: <\/span><span style=\"font-weight: 400;\">{arr}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm: <\/span><span style=\"font-weight: 400;\">{x}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">result = binary_search(arr, x)<\/span>\r\n<span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> result != <\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">{result}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"Ph\u1ea7n t\u1eed kh\u00f4ng c\u00f3 trong danh s\u00e1ch\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch: [<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">40<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm: <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: Ki\u1ec3m tra ph\u1ea7n t\u1eed \u1edf gi\u1eefa t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: Ki\u1ec3m tra ph\u1ea7n t\u1eed \u1edf gi\u1eefa t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">3<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 1:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">low = 0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">high = 4<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">mid = (0 + 4) \/\/ 2 = 2<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Ki\u1ec3m tra <\/span><span style=\"font-weight: 400;\">arr[2] = 4<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">V\u00ec <\/span><span style=\"font-weight: 400;\">4 &lt; 10<\/span><span style=\"font-weight: 400;\">, gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm n\u1eb1m \u1edf n\u1eeda b\u00ean ph\u1ea3i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt <\/span><span style=\"font-weight: 400;\">low = mid + 1 = 3<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 2:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">low = 3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">high = 4<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">mid = (3 + 4) \/\/ 2 = 3<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Ki\u1ec3m tra <\/span><span style=\"font-weight: 400;\">arr[3] = 10<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">T\u00ecm th\u1ea5y gi\u00e1 tr\u1ecb c\u1ea7n t\u00ecm.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ebft lu\u1eadn:<\/b><span style=\"font-weight: 400;\"> Gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h3><span style=\"font-weight: 400;\"><strong>So s\u00e1nh Linear Search v\u00e0 Binary Search<\/strong><\/span><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Linear Search:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>\u01afu \u0111i\u1ec3m:<\/b><span style=\"font-weight: 400;\"> \u0110\u01a1n gi\u1ea3n, kh\u00f4ng y\u00eau c\u1ea7u danh s\u00e1ch ph\u1ea3i s\u1eafp x\u1ebfp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m:<\/b><span style=\"font-weight: 400;\"> Th\u1eddi gian t\u00ecm ki\u1ebfm ch\u1eadm v\u1edbi danh s\u00e1ch l\u1edbn (O(n)).<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Binary Search:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>\u01afu \u0111i\u1ec3m:<\/b><span style=\"font-weight: 400;\"> T\u1ed1c \u0111\u1ed9 t\u00ecm ki\u1ebfm nhanh h\u01a1n v\u1edbi danh s\u00e1ch l\u1edbn (O(log n)).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m:<\/b><span style=\"font-weight: 400;\"> Y\u00eau c\u1ea7u danh s\u00e1ch ph\u1ea3i \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp tr\u01b0\u1edbc.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Python_ve_Cau_truc_Du_lieu_Co_ban_Basic_Data_Structure\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 C\u1ea5u tr\u00fac D\u1eef li\u1ec7u C\u01a1 b\u1ea3n (Basic Data Structure)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Hi\u1ec3u v\u00e0 \u00e1p d\u1ee5ng c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u n\u00e0y gi\u00fap gi\u1ea3i quy\u1ebft nhi\u1ec1u b\u00e0i to\u00e1n hi\u1ec7u qu\u1ea3:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Stack<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Queue<\/b><span style=\"font-weight: 400;\"> gi\u00fap qu\u1ea3n l\u00fd d\u1eef li\u1ec7u theo th\u1ee9 t\u1ef1 nh\u1eadp\/xu\u1ea5t kh\u00e1c nhau.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Linked List<\/b><span style=\"font-weight: 400;\"> cho ph\u00e9p ch\u00e8n\/x\u00f3a ph\u1ea7n t\u1eed linh ho\u1ea1t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Tree<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Graph<\/b><span style=\"font-weight: 400;\"> l\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p cho ph\u00e9p bi\u1ec3u di\u1ec5n c\u00e1c quan h\u1ec7 ph\u00e2n c\u1ea5p v\u00e0 k\u1ebft n\u1ed1i.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1 Stack<\/b><\/h3>\n<p><b>Stack<\/b><span style=\"font-weight: 400;\"> (Ng\u0103n x\u1ebfp) l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u tu\u00e2n theo nguy\u00ean t\u1eafc LIFO (Last In, First Out). \u0110i\u1ec1u n\u00e0y c\u00f3 ngh\u0129a l\u00e0 ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c th\u00eam v\u00e0o cu\u1ed1i c\u00f9ng s\u1ebd \u0111\u01b0\u1ee3c l\u1ea5y ra \u0111\u1ea7u ti\u00ean.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t chu\u1ed7i c\u00e1c d\u1ea5u ngo\u1eb7c \u0111\u01a1n, h\u00e3y ki\u1ec3m tra xem chu\u1ed7i c\u00f3 h\u1ee3p l\u1ec7 hay kh\u00f4ng. M\u1ed9t chu\u1ed7i \u0111\u01b0\u1ee3c coi l\u00e0 h\u1ee3p l\u1ec7 n\u1ebfu:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u1ed7i d\u1ea5u m\u1edf ngo\u1eb7c c\u00f3 m\u1ed9t d\u1ea5u \u0111\u00f3ng ngo\u1eb7c t\u01b0\u01a1ng \u1ee9ng c\u00f9ng lo\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c d\u1ea5u ngo\u1eb7c \u0111\u01b0\u1ee3c \u0111\u00f3ng theo \u0111\u00fang th\u1ee9 t\u1ef1.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng c\u1ea5u tr\u00fac d\u1eef li\u1ec7u <\/span><b>Stack<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 gi\u1ea3i quy\u1ebft b\u00e0i to\u00e1n n\u00e0y.<\/span><\/p>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">&#8220;({[]})&#8221;<\/span><span style=\"font-weight: 400;\"> =&gt; <\/span><b>H\u1ee3p l\u1ec7<\/b><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">&#8220;({[})&#8221;<\/span><span style=\"font-weight: 400;\"> =&gt; <\/span><b>Kh\u00f4ng h\u1ee3p l\u1ec7<\/b><\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy\u1ec7t qua t\u1eebng k\u00fd t\u1ef1 trong chu\u1ed7i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu k\u00fd t\u1ef1 l\u00e0 d\u1ea5u m\u1edf ngo\u1eb7c (<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">), \u0111\u1ea9y n\u00f3 v\u00e0o Stack.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu k\u00fd t\u1ef1 l\u00e0 d\u1ea5u \u0111\u00f3ng ngo\u1eb7c (<\/span><span style=\"font-weight: 400;\">&#8216;)&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;}&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;]&#8217;<\/span><span style=\"font-weight: 400;\">), ki\u1ec3m tra xem Stack c\u00f3 r\u1ed7ng kh\u00f4ng:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">N\u1ebfu r\u1ed7ng, chu\u1ed7i kh\u00f4ng h\u1ee3p l\u1ec7.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">N\u1ebfu kh\u00f4ng r\u1ed7ng, l\u1ea5y ph\u1ea7n t\u1eed tr\u00ean \u0111\u1ec9nh Stack ra v\u00e0 ki\u1ec3m tra xem n\u00f3 c\u00f3 ph\u1ea3i l\u00e0 d\u1ea5u m\u1edf ngo\u1eb7c t\u01b0\u01a1ng \u1ee9ng kh\u00f4ng.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Sau khi duy\u1ec7t h\u1ebft chu\u1ed7i, n\u1ebfu Stack r\u1ed7ng, chu\u1ed7i h\u1ee3p l\u1ec7; ng\u01b0\u1ee3c l\u1ea1i, chu\u1ed7i kh\u00f4ng h\u1ee3p l\u1ec7.<\/span><\/li>\n<\/ul>\n<p><b>M\u00e3 Python:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> is_valid_parentheses<\/span><span style=\"font-weight: 400;\">(s)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 stack = []<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># B\u1ea3ng \u00e1nh x\u1ea1 c\u00e1c c\u1eb7p ngo\u1eb7c<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 mapping = {<\/span><span style=\"font-weight: 400;\">')'<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">'('<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'}'<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">'{'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">']'<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">'['<\/span><span style=\"font-weight: 400;\">}<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> char <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> s:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> char <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> mapping.values():<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># D\u1ea5u m\u1edf ngo\u1eb7c, \u0111\u1ea9y v\u00e0o Stack<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 stack.append(char)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\u0110\u1ea9y '<\/span><span style=\"font-weight: 400;\">{char}<\/span><span style=\"font-weight: 400;\">' v\u00e0o Stack: <\/span><span style=\"font-weight: 400;\">{stack}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">elif<\/span><span style=\"font-weight: 400;\"> char <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> mapping.keys():<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># D\u1ea5u \u0111\u00f3ng ngo\u1eb7c, ki\u1ec3m tra Stack<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> stack <\/span><span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> stack[<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] == mapping[char]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 popped = stack.pop()<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"L\u1ea5y '<\/span><span style=\"font-weight: 400;\">{popped}<\/span><span style=\"font-weight: 400;\">' ra kh\u1ecfi Stack: <\/span><span style=\"font-weight: 400;\">{stack}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Kh\u00f4ng kh\u1edbp v\u1edbi '<\/span><span style=\"font-weight: 400;\">{char}<\/span><span style=\"font-weight: 400;\">', chu\u1ed7i kh\u00f4ng h\u1ee3p l\u1ec7.\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">False<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># K\u00fd t\u1ef1 kh\u00f4ng ph\u1ea3i d\u1ea5u ngo\u1eb7c, b\u1ecf qua<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">continue<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span> <span style=\"font-weight: 400;\">not<\/span><span style=\"font-weight: 400;\"> stack:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"Stack r\u1ed7ng, chu\u1ed7i h\u1ee3p l\u1ec7.\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">True<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"Stack kh\u00f4ng r\u1ed7ng, chu\u1ed7i kh\u00f4ng h\u1ee3p l\u1ec7.\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span> <span style=\"font-weight: 400;\">False<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">strings = [<\/span><span style=\"font-weight: 400;\">\"({[]})\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">\"({[})\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">\"((()))\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">\"([)]\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">\"{[()()]()}\"<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> s <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> strings:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\\nKi\u1ec3m tra chu\u1ed7i: '<\/span><span style=\"font-weight: 400;\">{s}<\/span><span style=\"font-weight: 400;\">'\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 result = is_valid_parentheses(s)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Chu\u1ed7i '<\/span><span style=\"font-weight: 400;\">{s}<\/span><span style=\"font-weight: 400;\">' l\u00e0 <\/span><span style=\"font-weight: 400;\">{<\/span><span style=\"font-weight: 400;\">'h\u1ee3p l\u1ec7'<\/span> <span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> result <\/span><span style=\"font-weight: 400;\">else<\/span> <span style=\"font-weight: 400;\">'kh\u00f4ng h\u1ee3p l\u1ec7'<\/span><span style=\"font-weight: 400;\">}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Ki\u1ec3m tra chu\u1ed7i: <\/span><span style=\"font-weight: 400;\">&#8216;({[]})&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: []<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Stack r\u1ed7ng, chu\u1ed7i h\u1ee3p l\u1ec7.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">&#8216;({[]})&#8217;<\/span><span style=\"font-weight: 400;\"> l\u00e0 h\u1ee3p l\u1ec7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ki\u1ec3m tra chu\u1ed7i: <\/span><span style=\"font-weight: 400;\">&#8216;({[})&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp v\u1edbi <\/span><span style=\"font-weight: 400;\">&#8216;}&#8217;<\/span><span style=\"font-weight: 400;\">, chu\u1ed7i kh\u00f4ng h\u1ee3p l\u1ec7.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">&#8216;({[})&#8217;<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng h\u1ee3p l\u1ec7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ki\u1ec3m tra chu\u1ed7i: <\/span><span style=\"font-weight: 400;\">&#8216;((()))&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: []<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Stack r\u1ed7ng, chu\u1ed7i h\u1ee3p l\u1ec7.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">&#8216;((()))&#8217;<\/span><span style=\"font-weight: 400;\"> l\u00e0 h\u1ee3p l\u1ec7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ki\u1ec3m tra chu\u1ed7i: <\/span><span style=\"font-weight: 400;\">&#8216;([)]&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp v\u1edbi <\/span><span style=\"font-weight: 400;\">&#8216;)&#8217;<\/span><span style=\"font-weight: 400;\">, chu\u1ed7i kh\u00f4ng h\u1ee3p l\u1ec7.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">&#8216;([)]&#8217;<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng h\u1ee3p l\u1ec7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ki\u1ec3m tra chu\u1ed7i: <\/span><span style=\"font-weight: 400;\">&#8216;{[()()]()}&#8217;<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;[&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ea9y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> v\u00e0o Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;(&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: [<\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">L\u1ea5y <\/span><span style=\"font-weight: 400;\">&#8216;{&#8216;<\/span><span style=\"font-weight: 400;\"> ra kh\u1ecfi Stack: []<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Stack r\u1ed7ng, chu\u1ed7i h\u1ee3p l\u1ec7.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">&#8216;{[()()]()}&#8217;<\/span><span style=\"font-weight: 400;\"> l\u00e0 h\u1ee3p l\u1ec7<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chu\u1ed7i 1:<\/b><span style=\"font-weight: 400;\"> M\u1ed7i d\u1ea5u m\u1edf ngo\u1eb7c c\u00f3 d\u1ea5u \u0111\u00f3ng t\u01b0\u01a1ng \u1ee9ng v\u00e0 Stack r\u1ed7ng sau khi duy\u1ec7t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chu\u1ed7i 2:<\/b><span style=\"font-weight: 400;\"> D\u1ea5u \u0111\u00f3ng ngo\u1eb7c kh\u00f4ng kh\u1edbp v\u1edbi d\u1ea5u m\u1edf ngo\u1eb7c tr\u00ean \u0111\u1ec9nh Stack.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chu\u1ed7i 3:<\/b><span style=\"font-weight: 400;\"> D\u1ea5u ngo\u1eb7c \u0111\u01b0\u1ee3c \u0111\u00f3ng theo \u0111\u00fang th\u1ee9 t\u1ef1.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chu\u1ed7i 4:<\/b><span style=\"font-weight: 400;\"> D\u1ea5u \u0111\u00f3ng ngo\u1eb7c kh\u00f4ng kh\u1edbp, chu\u1ed7i kh\u00f4ng h\u1ee3p l\u1ec7.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chu\u1ed7i 5:<\/b><span style=\"font-weight: 400;\"> Chu\u1ed7i ph\u1ee9c t\u1ea1p nh\u01b0ng h\u1ee3p l\u1ec7.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Queue<\/b><\/h3>\n<p><b>Queue<\/b><span style=\"font-weight: 400;\"> (H\u00e0ng \u0111\u1ee3i) tu\u00e2n theo nguy\u00ean t\u1eafc FIFO (First In, First Out). Kh\u00e1ch h\u00e0ng \u0111\u1ebfn tr\u01b0\u1edbc s\u1ebd \u0111\u01b0\u1ee3c ph\u1ee5c v\u1ee5 tr\u01b0\u1edbc.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">M\u00f4 ph\u1ecfng m\u1ed9t h\u00e0ng \u0111\u1ee3i t\u1ea1i si\u00eau th\u1ecb. M\u1ed7i kh\u00e1ch h\u00e0ng \u0111\u01b0\u1ee3c \u0111\u1ea1i di\u1ec7n b\u1edfi m\u1ed9t t\u00ean ho\u1eb7c s\u1ed1 ID. Th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c sau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u00eam kh\u00e1ch h\u00e0ng v\u00e0o h\u00e0ng \u0111\u1ee3i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ph\u1ee5c v\u1ee5 kh\u00e1ch h\u00e0ng (l\u1ea5y kh\u00e1ch h\u00e0ng ra kh\u1ecfi h\u00e0ng \u0111\u1ee3i).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Hi\u1ec3n th\u1ecb h\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i sau m\u1ed7i thao t\u00e1c.<\/span><\/li>\n<\/ul>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u00e1ch h\u00e0ng \u0111\u1ebfn: <\/span><span style=\"font-weight: 400;\">&#8220;Alice&#8221;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8220;Bob&#8221;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8220;Charlie&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ph\u1ee5c v\u1ee5 m\u1ed9t kh\u00e1ch h\u00e0ng<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u00e1ch h\u00e0ng \u0111\u1ebfn: <\/span><span style=\"font-weight: 400;\">&#8220;Diana&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ph\u1ee5c v\u1ee5 hai kh\u00e1ch h\u00e0ng ti\u1ebfp theo<\/span><\/li>\n<\/ul>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">from<\/span><span style=\"font-weight: 400;\"> collections <\/span><span style=\"font-weight: 400;\">import<\/span><span style=\"font-weight: 400;\"> deque<\/span>\r\n\r\n<span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> supermarket_queue_simulation<\/span><span style=\"font-weight: 400;\">()<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 queue = deque()<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Kh\u00e1ch h\u00e0ng \u0111\u1ebfn: Alice, Bob, Charlie<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 customers = [<\/span><span style=\"font-weight: 400;\">\"Alice\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">\"Bob\"<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">\"Charlie\"<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> customer <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> customers:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 queue.append(customer)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Kh\u00e1ch h\u00e0ng '<\/span><span style=\"font-weight: 400;\">{customer}<\/span><span style=\"font-weight: 400;\">' v\u00e0o h\u00e0ng \u0111\u1ee3i. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: <\/span><span style=\"font-weight: 400;\">{list(queue)}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Ph\u1ee5c v\u1ee5 m\u1ed9t kh\u00e1ch h\u00e0ng<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 served = queue.popleft()<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ph\u1ee5c v\u1ee5 kh\u00e1ch h\u00e0ng '<\/span><span style=\"font-weight: 400;\">{served}<\/span><span style=\"font-weight: 400;\">'. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: <\/span><span style=\"font-weight: 400;\">{list(queue)}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Kh\u00e1ch h\u00e0ng \u0111\u1ebfn: Diana<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 queue.append(<\/span><span style=\"font-weight: 400;\">\"Diana\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Kh\u00e1ch h\u00e0ng 'Diana' v\u00e0o h\u00e0ng \u0111\u1ee3i. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: <\/span><span style=\"font-weight: 400;\">{list(queue)}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Ph\u1ee5c v\u1ee5 hai kh\u00e1ch h\u00e0ng ti\u1ebfp theo<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> _ <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> queue:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 served = queue.popleft()<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ph\u1ee5c v\u1ee5 kh\u00e1ch h\u00e0ng '<\/span><span style=\"font-weight: 400;\">{served}<\/span><span style=\"font-weight: 400;\">'. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: <\/span><span style=\"font-weight: 400;\">{list(queue)}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"H\u00e0ng \u0111\u1ee3i tr\u1ed1ng.\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">break<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># Th\u1ef1c thi m\u00f4 ph\u1ecfng<\/span>\r\n<span style=\"font-weight: 400;\">supermarket_queue_simulation()<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Kh\u00e1ch h\u00e0ng <\/span><span style=\"font-weight: 400;\">&#8216;Alice&#8217;<\/span><span style=\"font-weight: 400;\"> v\u00e0o h\u00e0ng \u0111\u1ee3i. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: [<\/span><span style=\"font-weight: 400;\">&#8216;Alice&#8217;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00e1ch h\u00e0ng <\/span><span style=\"font-weight: 400;\">&#8216;Bob&#8217;<\/span><span style=\"font-weight: 400;\"> v\u00e0o h\u00e0ng \u0111\u1ee3i. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: [<\/span><span style=\"font-weight: 400;\">&#8216;Alice&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;Bob&#8217;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00e1ch h\u00e0ng <\/span><span style=\"font-weight: 400;\">&#8216;Charlie&#8217;<\/span><span style=\"font-weight: 400;\"> v\u00e0o h\u00e0ng \u0111\u1ee3i. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: [<\/span><span style=\"font-weight: 400;\">&#8216;Alice&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;Bob&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;Charlie&#8217;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u1ee5c v\u1ee5 kh\u00e1ch h\u00e0ng <\/span><span style=\"font-weight: 400;\">&#8216;Alice&#8217;<\/span><span style=\"font-weight: 400;\">. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: [<\/span><span style=\"font-weight: 400;\">&#8216;Bob&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;Charlie&#8217;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00e1ch h\u00e0ng <\/span><span style=\"font-weight: 400;\">&#8216;Diana&#8217;<\/span><span style=\"font-weight: 400;\"> v\u00e0o h\u00e0ng \u0111\u1ee3i. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: [<\/span><span style=\"font-weight: 400;\">&#8216;Bob&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;Charlie&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;Diana&#8217;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u1ee5c v\u1ee5 kh\u00e1ch h\u00e0ng <\/span><span style=\"font-weight: 400;\">&#8216;Bob&#8217;<\/span><span style=\"font-weight: 400;\">. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: [<\/span><span style=\"font-weight: 400;\">&#8216;Charlie&#8217;<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">&#8216;Diana&#8217;<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ph\u1ee5c v\u1ee5 kh\u00e1ch h\u00e0ng <\/span><span style=\"font-weight: 400;\">&#8216;Charlie&#8217;<\/span><span style=\"font-weight: 400;\">. H\u00e0ng \u0111\u1ee3i hi\u1ec7n t\u1ea1i: [<\/span><span style=\"font-weight: 400;\">&#8216;Diana&#8217;<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">deque<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u1ea1o Queue.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u00e1ch h\u00e0ng \u0111\u01b0\u1ee3c th\u00eam v\u00e0o cu\u1ed1i h\u00e0ng \u0111\u1ee3i b\u1eb1ng <\/span><span style=\"font-weight: 400;\">append()<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u00e1ch h\u00e0ng \u0111\u01b0\u1ee3c ph\u1ee5c v\u1ee5 (l\u1ea5y ra kh\u1ecfi h\u00e0ng \u0111\u1ee3i) b\u1eb1ng <\/span><span style=\"font-weight: 400;\">popleft()<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u00e0ng \u0111\u1ee3i \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb sau m\u1ed7i thao t\u00e1c \u0111\u1ec3 d\u1ec5 h\u00ecnh dung.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Linked List<\/b><\/h3>\n<p><b>Linked List<\/b><span style=\"font-weight: 400;\"> g\u1ed3m c\u00e1c n\u00fat li\u00ean k\u1ebft v\u1edbi nhau, m\u1ed7i n\u00fat ch\u1ee9a d\u1eef li\u1ec7u v\u00e0 tham chi\u1ebfu \u0111\u1ebfn n\u00fat ti\u1ebfp theo.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">X\u00e2y d\u1ef1ng m\u1ed9t danh s\u00e1ch li\u00ean k\u1ebft \u0111\u01a1n (Singly Linked List) \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c s\u1ed1 nguy\u00ean. Th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ch\u00e8n ph\u1ea7n t\u1eed v\u00e0o \u0111\u1ea7u danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ch\u00e8n ph\u1ea7n t\u1eed v\u00e0o cu\u1ed1i danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">X\u00f3a ph\u1ea7n t\u1eed \u0111\u1ea7u danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In danh s\u00e1ch hi\u1ec7n t\u1ea1i.<\/span><\/li>\n<\/ul>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ch\u00e8n <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"> v\u00e0o \u0111\u1ea7u danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ch\u00e8n <\/span><span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\"> v\u00e0o cu\u1ed1i danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ch\u00e8n <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> v\u00e0o \u0111\u1ea7u danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">X\u00f3a ph\u1ea7n t\u1eed \u0111\u1ea7u danh s\u00e1ch.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">In danh s\u00e1ch hi\u1ec7n t\u1ea1i.<\/span><\/li>\n<\/ul>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> Node:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> __init__<\/span><span style=\"font-weight: 400;\">(self, data)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.data = data<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.next = <\/span><span style=\"font-weight: 400;\">None<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> LinkedList:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> __init__<\/span><span style=\"font-weight: 400;\">(self)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.head = <\/span><span style=\"font-weight: 400;\">None<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u00e8n v\u00e0o \u0111\u1ea7u danh s\u00e1ch<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> insert_at_head<\/span><span style=\"font-weight: 400;\">(self, data)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 new_node = Node(data)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 new_node.next = self.head<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.head = new_node<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ch\u00e8n <\/span><span style=\"font-weight: 400;\">{data}<\/span><span style=\"font-weight: 400;\"> v\u00e0o \u0111\u1ea7u danh s\u00e1ch.\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u00e8n v\u00e0o cu\u1ed1i danh s\u00e1ch<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> insert_at_tail<\/span><span style=\"font-weight: 400;\">(self, data)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 new_node = Node(data)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span> <span style=\"font-weight: 400;\">not<\/span><span style=\"font-weight: 400;\"> self.head:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 self.head = new_node<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ch\u00e8n <\/span><span style=\"font-weight: 400;\">{data}<\/span><span style=\"font-weight: 400;\"> v\u00e0o \u0111\u1ea7u danh s\u00e1ch (danh s\u00e1ch r\u1ed7ng).\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 last = self.head<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> last.next:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 last = last.next<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 last.next = new_node<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ch\u00e8n <\/span><span style=\"font-weight: 400;\">{data}<\/span><span style=\"font-weight: 400;\"> v\u00e0o cu\u1ed1i danh s\u00e1ch.\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># X\u00f3a ph\u1ea7n t\u1eed \u0111\u1ea7u danh s\u00e1ch<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> delete_head<\/span><span style=\"font-weight: 400;\">(self)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> self.head:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 removed = self.head.data<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 self.head = self.head.next<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"X\u00f3a ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean: <\/span><span style=\"font-weight: 400;\">{removed}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"Danh s\u00e1ch r\u1ed7ng, kh\u00f4ng th\u1ec3 x\u00f3a.\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># In danh s\u00e1ch<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> print_list<\/span><span style=\"font-weight: 400;\">(self)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 current = self.head<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"Danh s\u00e1ch hi\u1ec7n t\u1ea1i:\"<\/span><span style=\"font-weight: 400;\">, end=<\/span><span style=\"font-weight: 400;\">\" \"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> current:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(current.data, end=<\/span><span style=\"font-weight: 400;\">\" \"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 current = current.next<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print()<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">llist = LinkedList()<\/span>\r\n<span style=\"font-weight: 400;\">llist.insert_at_head(<\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">llist.insert_at_tail(<\/span><span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">llist.insert_at_head(<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">llist.delete_head()<\/span>\r\n<span style=\"font-weight: 400;\">llist.print_list()<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Ch\u00e8n <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"> v\u00e0o \u0111\u1ea7u danh s\u00e1ch.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ch\u00e8n <\/span><span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\"> v\u00e0o cu\u1ed1i danh s\u00e1ch.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ch\u00e8n <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> v\u00e0o \u0111\u1ea7u danh s\u00e1ch.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">X\u00f3a ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean: <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Danh s\u00e1ch hi\u1ec7n t\u1ea1i: <\/span><span style=\"font-weight: 400;\">10<\/span> <span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ch\u00e8n v\u00e0o \u0111\u1ea7u:<\/b><span style=\"font-weight: 400;\"> N\u00fat m\u1edbi tr\u1edf th\u00e0nh \u0111\u1ea7u danh s\u00e1ch, tham chi\u1ebfu \u0111\u1ebfn n\u00fat c\u0169.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ch\u00e8n v\u00e0o cu\u1ed1i:<\/b><span style=\"font-weight: 400;\"> Duy\u1ec7t \u0111\u1ebfn cu\u1ed1i danh s\u00e1ch v\u00e0 th\u00eam n\u00fat m\u1edbi.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>X\u00f3a \u0111\u1ea7u:<\/b><span style=\"font-weight: 400;\"> \u0110\u1eb7t \u0111\u1ea7u danh s\u00e1ch l\u00e0 n\u00fat ti\u1ebfp theo.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>In danh s\u00e1ch:<\/b><span style=\"font-weight: 400;\"> Duy\u1ec7t qua c\u00e1c n\u00fat v\u00e0 in d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Tree<\/b><\/h3>\n<p><b>Binary Tree<\/b><span style=\"font-weight: 400;\"> l\u00e0 c\u00e2y trong \u0111\u00f3 m\u1ed7i n\u00fat c\u00f3 t\u1ed1i \u0111a hai con.<\/span><\/p>\n<p><b>C\u00e1c ki\u1ec3u duy\u1ec7t c\u00e2y:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Pre-order (NLR):<\/b><span style=\"font-weight: 400;\"> N\u00fat hi\u1ec7n t\u1ea1i -&gt; C\u00e2y con tr\u00e1i -&gt; C\u00e2y con ph\u1ea3i<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>In-order (LNR):<\/b><span style=\"font-weight: 400;\"> C\u00e2y con tr\u00e1i -&gt; N\u00fat hi\u1ec7n t\u1ea1i -&gt; C\u00e2y con ph\u1ea3i<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Post-order (LRN):<\/b><span style=\"font-weight: 400;\"> C\u00e2y con tr\u00e1i -&gt; C\u00e2y con ph\u1ea3i -&gt; N\u00fat hi\u1ec7n t\u1ea1i<\/span><\/li>\n<\/ul>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">X\u00e2y d\u1ef1ng m\u1ed9t c\u00e2y nh\u1ecb ph\u00e2n (Binary Tree) v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c ki\u1ec3u duy\u1ec7t c\u00e2y sau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy\u1ec7t theo th\u1ee9 t\u1ef1 tr\u01b0\u1edbc (Pre-order)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy\u1ec7t theo th\u1ee9 t\u1ef1 gi\u1eefa (In-order)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy\u1ec7t theo th\u1ee9 t\u1ef1 sau (Post-order)<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">In ra c\u00e1c gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c n\u00fat trong qu\u00e1 tr\u00ecnh duy\u1ec7t.<\/span><\/p>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">C\u1ea5u tr\u00fac c\u00e2y:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\"> \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \u00a0 \/ \\<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"> \u00a0 <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"> \u00a0 \/ \\ \u00a0 \\<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"> \u00a0 <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> \u00a0 <\/span><span style=\"font-weight: 400;\">6<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> Node:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> __init__<\/span><span style=\"font-weight: 400;\">(self, data)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.left = <\/span><span style=\"font-weight: 400;\">None<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.right = <\/span><span style=\"font-weight: 400;\">None<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.data = data<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># Duy\u1ec7t Pre-order<\/span>\r\n<span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> preorder<\/span><span style=\"font-weight: 400;\">(root)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> root:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(root.data, end=<\/span><span style=\"font-weight: 400;\">\" \"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 preorder(root.left)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 preorder(root.right)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># Duy\u1ec7t In-order<\/span>\r\n<span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> inorder<\/span><span style=\"font-weight: 400;\">(root)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> root:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 inorder(root.left)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(root.data, end=<\/span><span style=\"font-weight: 400;\">\" \"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 inorder(root.right)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># Duy\u1ec7t Post-order<\/span>\r\n<span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> postorder<\/span><span style=\"font-weight: 400;\">(root)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> root:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 postorder(root.left)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 postorder(root.right)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(root.data, end=<\/span><span style=\"font-weight: 400;\">\" \"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># X\u00e2y d\u1ef1ng c\u00e2y<\/span>\r\n<span style=\"font-weight: 400;\">root = Node(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">root.left = Node(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">root.right = Node(<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">root.left.left = Node(<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">root.left.right = Node(<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">root.right.right = Node(<\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># Th\u1ef1c hi\u1ec7n duy\u1ec7t c\u00e2y<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">\"Duy\u1ec7t Pre-order:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">preorder(root)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">\"\\nDuy\u1ec7t In-order:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">inorder(root)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">\"\\nDuy\u1ec7t Post-order:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">postorder(root)<\/span>\r\n<span style=\"font-weight: 400;\">print()<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Duy\u1ec7t Pre-order:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">1<\/span> <span style=\"font-weight: 400;\">2<\/span> <span style=\"font-weight: 400;\">4<\/span> <span style=\"font-weight: 400;\">5<\/span> <span style=\"font-weight: 400;\">3<\/span> <span style=\"font-weight: 400;\">6<\/span> <span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Duy\u1ec7t In-order:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">4<\/span> <span style=\"font-weight: 400;\">2<\/span> <span style=\"font-weight: 400;\">5<\/span> <span style=\"font-weight: 400;\">1<\/span> <span style=\"font-weight: 400;\">3<\/span> <span style=\"font-weight: 400;\">6<\/span> <span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Duy\u1ec7t Post-order:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">4<\/span> <span style=\"font-weight: 400;\">5<\/span> <span style=\"font-weight: 400;\">2<\/span> <span style=\"font-weight: 400;\">6<\/span> <span style=\"font-weight: 400;\">3<\/span> <span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Pre-order:<\/b><span style=\"font-weight: 400;\"> B\u1eaft \u0111\u1ea7u t\u1eeb n\u00fat g\u1ed1c, sau \u0111\u00f3 \u0111\u1ebfn c\u00e2y con tr\u00e1i, r\u1ed3i c\u00e2y con ph\u1ea3i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>In-order:<\/b><span style=\"font-weight: 400;\"> Duy\u1ec7t c\u00e2y con tr\u00e1i, sau \u0111\u00f3 \u0111\u1ebfn n\u00fat g\u1ed1c, r\u1ed3i c\u00e2y con ph\u1ea3i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Post-order:<\/b><span style=\"font-weight: 400;\"> Duy\u1ec7t c\u00e2y con tr\u00e1i, c\u00e2y con ph\u1ea3i, sau \u0111\u00f3 \u0111\u1ebfn n\u00fat g\u1ed1c.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c h\u00e0m duy\u1ec7t \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n \u0111\u1ec7 quy.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Graph<\/b><\/h3>\n<p><b>Graph<\/b><span style=\"font-weight: 400;\"> g\u1ed3m t\u1eadp h\u1ee3p c\u00e1c \u0111\u1ec9nh v\u00e0 c\u00e1c c\u1ea1nh c\u00f3 h\u01b0\u1edbng ho\u1eb7c kh\u00f4ng h\u01b0\u1edbng.<\/span><\/p>\n<p><b>Depth-First Search (DFS)<\/b><span style=\"font-weight: 400;\"> l\u00e0 thu\u1eadt to\u00e1n duy\u1ec7t \u0111\u1ed3 th\u1ecb theo chi\u1ec1u s\u00e2u, \u0111i s\u00e2u v\u00e0o c\u00e1c \u0111\u1ec9nh con tr\u01b0\u1edbc khi quay l\u1ea1i \u0111\u1ec9nh cha.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Bi\u1ec3u di\u1ec5n m\u1ed9t \u0111\u1ed3 th\u1ecb c\u00f3 h\u01b0\u1edbng s\u1eed d\u1ee5ng danh s\u00e1ch k\u1ec1 (Adjacency List) v\u00e0 th\u1ef1c hi\u1ec7n thu\u1eadt to\u00e1n duy\u1ec7t theo chi\u1ec1u s\u00e2u (Depth-First Search &#8211; DFS) b\u1eaft \u0111\u1ea7u t\u1eeb m\u1ed9t \u0111\u1ec9nh cho tr\u01b0\u1edbc.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ed3 th\u1ecb:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ec9nh: <\/span><span style=\"font-weight: 400;\">0, 1, 2, 3, 4<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea1nh: <\/span><span style=\"font-weight: 400;\">(0-&gt;1), (0-&gt;2), (1-&gt;2), (2-&gt;0), (2-&gt;3), (3-&gt;3)<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u duy\u1ec7t t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>L\u1eddi gi\u1ea3i:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">from<\/span><span style=\"font-weight: 400;\"> collections <\/span><span style=\"font-weight: 400;\">import<\/span><span style=\"font-weight: 400;\"> defaultdict<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> Graph:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> __init__<\/span><span style=\"font-weight: 400;\">(self)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph = defaultdict(list)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Th\u00eam c\u1ea1nh v\u00e0o \u0111\u1ed3 th\u1ecb<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> add_edge<\/span><span style=\"font-weight: 400;\">(self, u, v)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph[u].append(v)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Thu\u1eadt to\u00e1n DFS \u0111\u1ec7 quy<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> dfs_util<\/span><span style=\"font-weight: 400;\">(self, v, visited)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 visited.add(v)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">{v}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> neighbor <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> self.graph[v]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> neighbor <\/span><span style=\"font-weight: 400;\">not<\/span> <span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> visited:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 self.dfs_util(neighbor, visited)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># G\u1ecdi h\u00e0m DFS<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> dfs<\/span><span style=\"font-weight: 400;\">(self, start)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 visited = set()<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"B\u1eaft \u0111\u1ea7u duy\u1ec7t t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">{start}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.dfs_util(start, visited)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">g = Graph()<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># Th\u1ef1c hi\u1ec7n DFS t\u1eeb \u0111\u1ec9nh 2<\/span>\r\n<span style=\"font-weight: 400;\">g.dfs(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u duy\u1ec7t t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">3<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ed3 th\u1ecb \u0111\u01b0\u1ee3c bi\u1ec3u di\u1ec5n b\u1eb1ng danh s\u00e1ch k\u1ec1.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">DFS s\u1eed d\u1ee5ng \u0111\u1ec7 quy \u0111\u1ec3 th\u0103m c\u00e1c \u0111\u1ec9nh k\u1ec1 ch\u01b0a \u0111\u01b0\u1ee3c th\u0103m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, th\u0103m c\u00e1c \u0111\u1ec9nh theo chi\u1ec1u s\u00e2u tr\u01b0\u1edbc khi quay l\u1ea1i.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_tren_Do_thi_Graph_Algorithm\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n tr\u00ean \u0110\u1ed3 th\u1ecb (Graph Algorithm)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Depth-First Search (DFS)<\/b><\/h3>\n<p><b>Depth-First Search (DFS)<\/b><span style=\"font-weight: 400;\"> l\u00e0 thu\u1eadt to\u00e1n duy\u1ec7t ho\u1eb7c t\u00ecm ki\u1ebfm tr\u00ean \u0111\u1ed3 th\u1ecb, b\u1eaft \u0111\u1ea7u t\u1eeb m\u1ed9t \u0111\u1ec9nh g\u1ed1c v\u00e0 kh\u00e1m ph\u00e1 c\u00e0ng s\u00e2u c\u00e0ng t\u1ed1t theo m\u1ed7i nh\u00e1nh tr\u01b0\u1edbc khi quay lui.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t \u0111\u1ed3 th\u1ecb c\u00f3 h\u01b0\u1edbng ho\u1eb7c v\u00f4 h\u01b0\u1edbng. H\u00e3y th\u1ef1c hi\u1ec7n thu\u1eadt to\u00e1n <\/span><b>Depth-First Search (DFS)<\/b><span style=\"font-weight: 400;\"> b\u1eaft \u0111\u1ea7u t\u1eeb m\u1ed9t \u0111\u1ec9nh cho tr\u01b0\u1edbc v\u00e0 hi\u1ec3n th\u1ecb th\u1ee9 t\u1ef1 c\u00e1c \u0111\u1ec9nh \u0111\u01b0\u1ee3c th\u0103m.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ed3 th\u1ecb:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ec9nh: <\/span><span style=\"font-weight: 400;\">0, 1, 2, 3, 4, 5<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea1nh:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(0, 1)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(0, 2)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(1, 3)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(1, 4)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(2, 5)<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u duy\u1ec7t t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng \u0111\u1ec7 quy ho\u1eb7c ng\u0103n x\u1ebfp \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c \u0111\u1ec9nh c\u1ea7n th\u0103m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u c\u00e1c \u0111\u1ec9nh \u0111\u00e3 th\u0103m \u0111\u1ec3 tr\u00e1nh l\u1eb7p v\u00f4 h\u1ea1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ti\u1ebfp t\u1ee5c th\u0103m c\u00e1c \u0111\u1ec9nh k\u1ec1 ch\u01b0a \u0111\u01b0\u1ee3c th\u0103m c\u1ee7a \u0111\u1ec9nh hi\u1ec7n t\u1ea1i.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">from<\/span><span style=\"font-weight: 400;\"> collections <\/span><span style=\"font-weight: 400;\">import<\/span><span style=\"font-weight: 400;\"> defaultdict<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> Graph:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> __init__<\/span><span style=\"font-weight: 400;\">(self)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph = defaultdict(list)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Th\u00eam c\u1ea1nh v\u00e0o \u0111\u1ed3 th\u1ecb<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> add_edge<\/span><span style=\"font-weight: 400;\">(self, u, v)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph[u].append(v)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># H\u00e0m DFS \u0111\u1ec7 quy<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> dfs_util<\/span><span style=\"font-weight: 400;\">(self, v, visited)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 visited.add(v)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(v, end=<\/span><span style=\"font-weight: 400;\">' '<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Th\u0103m c\u00e1c \u0111\u1ec9nh k\u1ec1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> neighbor <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> self.graph[v]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> neighbor <\/span><span style=\"font-weight: 400;\">not<\/span> <span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> visited:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 self.dfs_util(neighbor, visited)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># H\u00e0m DFS ch\u00ednh<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> dfs<\/span><span style=\"font-weight: 400;\">(self, start)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 visited = set()<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Duy\u1ec7t DFS b\u1eaft \u0111\u1ea7u t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">{start}<\/span><span style=\"font-weight: 400;\">:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.dfs_util(start, visited)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print()<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">g = Graph()<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">g.dfs(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Duy\u1ec7t DFS b\u1eaft \u0111\u1ea7u t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">0<\/span> <span style=\"font-weight: 400;\">1<\/span> <span style=\"font-weight: 400;\">3<\/span> <span style=\"font-weight: 400;\">4<\/span> <span style=\"font-weight: 400;\">2<\/span> <span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, sau \u0111\u00f3 \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Quay l\u1ea1i \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, r\u1ed3i \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u1ee9 t\u1ef1 c\u00e1c \u0111\u1ec9nh \u0111\u01b0\u1ee3c th\u0103m: <\/span><span style=\"font-weight: 400;\">0 -&gt; 1 -&gt; 3 -&gt; 4 -&gt; 2 -&gt; 5<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Breadth-First Search (BFS)<\/b><\/h3>\n<p><b>Breadth-First Search (BFS)<\/b><span style=\"font-weight: 400;\"> l\u00e0 thu\u1eadt to\u00e1n duy\u1ec7t ho\u1eb7c t\u00ecm ki\u1ebfm tr\u00ean \u0111\u1ed3 th\u1ecb, b\u1eaft \u0111\u1ea7u t\u1eeb m\u1ed9t \u0111\u1ec9nh g\u1ed1c v\u00e0 th\u0103m c\u00e1c \u0111\u1ec9nh k\u1ec1 tr\u01b0\u1edbc khi ti\u1ebfp t\u1ee5c sang c\u00e1c \u0111\u1ec9nh \u1edf m\u1ee9c s\u00e2u h\u01a1n.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t \u0111\u1ed3 th\u1ecb c\u00f3 h\u01b0\u1edbng ho\u1eb7c v\u00f4 h\u01b0\u1edbng. H\u00e3y th\u1ef1c hi\u1ec7n thu\u1eadt to\u00e1n <\/span><b>Breadth-First Search (BFS)<\/b><span style=\"font-weight: 400;\"> b\u1eaft \u0111\u1ea7u t\u1eeb m\u1ed9t \u0111\u1ec9nh cho tr\u01b0\u1edbc v\u00e0 hi\u1ec3n th\u1ecb th\u1ee9 t\u1ef1 c\u00e1c \u0111\u1ec9nh \u0111\u01b0\u1ee3c th\u0103m.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng c\u00f9ng \u0111\u1ed3 th\u1ecb nh\u01b0 tr\u00ean:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ec9nh: <\/span><span style=\"font-weight: 400;\">0, 1, 2, 3, 4, 5<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea1nh:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(0, 1)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(0, 2)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(1, 3)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(1, 4)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(2, 5)<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u duy\u1ec7t t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng h\u00e0ng \u0111\u1ee3i (Queue) \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c \u0111\u1ec9nh c\u1ea7n th\u0103m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u c\u00e1c \u0111\u1ec9nh \u0111\u00e3 th\u0103m \u0111\u1ec3 tr\u00e1nh l\u1eb7p.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u0103m c\u00e1c \u0111\u1ec9nh k\u1ec1 c\u1ee7a \u0111\u1ec9nh hi\u1ec7n t\u1ea1i v\u00e0 th\u00eam ch\u00fang v\u00e0o h\u00e0ng \u0111\u1ee3i.<\/span><\/li>\n<\/ul>\n<p><b>M\u00e3 Python:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">from<\/span><span style=\"font-weight: 400;\"> collections <\/span><span style=\"font-weight: 400;\">import<\/span><span style=\"font-weight: 400;\"> defaultdict, deque<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> Graph:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> __init__<\/span><span style=\"font-weight: 400;\">(self)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph = defaultdict(list)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Th\u00eam c\u1ea1nh v\u00e0o \u0111\u1ed3 th\u1ecb<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> add_edge<\/span><span style=\"font-weight: 400;\">(self, u, v)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph[u].append(v)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># H\u00e0m BFS<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> bfs<\/span><span style=\"font-weight: 400;\">(self, start)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 visited = set()<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 queue = deque()<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 visited.add(start)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 queue.append(start)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Duy\u1ec7t BFS b\u1eaft \u0111\u1ea7u t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">{start}<\/span><span style=\"font-weight: 400;\">:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> queue:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 v = queue.popleft()<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(v, end=<\/span><span style=\"font-weight: 400;\">' '<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> neighbor <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> self.graph[v]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> neighbor <\/span><span style=\"font-weight: 400;\">not<\/span> <span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> visited:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 visited.add(neighbor)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 queue.append(neighbor)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print()<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">g = Graph()<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">g.bfs(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Duy\u1ec7t BFS b\u1eaft \u0111\u1ea7u t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">0<\/span> <span style=\"font-weight: 400;\">1<\/span> <span style=\"font-weight: 400;\">2<\/span> <span style=\"font-weight: 400;\">3<\/span> <span style=\"font-weight: 400;\">4<\/span> <span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, th\u0103m c\u00e1c \u0111\u1ec9nh k\u1ec1 <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, th\u0103m \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u1ee9 t\u1ef1 c\u00e1c \u0111\u1ec9nh \u0111\u01b0\u1ee3c th\u0103m: <\/span><span style=\"font-weight: 400;\">0 -&gt; 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Thu\u1eadt to\u00e1n Dijkstra<\/b><\/h3>\n<p><b>Thu\u1eadt to\u00e1n Dijkstra<\/b><span style=\"font-weight: 400;\"> t\u00ecm \u0111\u01b0\u1eddng \u0111i ng\u1eafn nh\u1ea5t t\u1eeb m\u1ed9t \u0111\u1ec9nh ngu\u1ed3n \u0111\u1ebfn t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ec9nh kh\u00e1c trong \u0111\u1ed3 th\u1ecb c\u00f3 tr\u1ecdng s\u1ed1 kh\u00f4ng \u00e2m.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t \u0111\u1ed3 th\u1ecb c\u00f3 tr\u1ecdng s\u1ed1 v\u1edbi c\u00e1c c\u1ea1nh c\u00f3 tr\u1ecdng s\u1ed1 kh\u00f4ng \u00e2m. H\u00e3y t\u00ecm \u0111\u01b0\u1eddng \u0111i ng\u1eafn nh\u1ea5t t\u1eeb m\u1ed9t \u0111\u1ec9nh ngu\u1ed3n \u0111\u1ebfn t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ec9nh kh\u00e1c s\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n Dijkstra<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ed3 th\u1ecb:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ec9nh: <\/span><span style=\"font-weight: 400;\">0, 1, 2, 3, 4<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea1nh v\u00e0 tr\u1ecdng s\u1ed1:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(0, 1, 10)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(0, 4, 5)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(1, 2, 1)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(2, 3, 4)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(4, 1, 3)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(4, 2, 9)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(4, 3, 2)<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec9nh ngu\u1ed3n: <\/span><span style=\"font-weight: 400;\">0<\/span><\/p>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng m\u1ed9t t\u1eadp h\u1ee3p (min-heap ho\u1eb7c priority queue) \u0111\u1ec3 ch\u1ecdn \u0111\u1ec9nh c\u00f3 kho\u1ea3ng c\u00e1ch nh\u1ecf nh\u1ea5t ch\u01b0a \u0111\u01b0\u1ee3c th\u0103m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt kho\u1ea3ng c\u00e1ch t\u1eeb \u0111\u1ec9nh ngu\u1ed3n \u0111\u1ebfn c\u00e1c \u0111\u1ec9nh k\u1ec1 n\u1ebfu t\u00ecm \u0111\u01b0\u1ee3c \u0111\u01b0\u1eddng \u0111i ng\u1eafn h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u1eb7p l\u1ea1i cho \u0111\u1ebfn khi t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ec9nh \u0111\u00e3 \u0111\u01b0\u1ee3c th\u0103m.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">import<\/span><span style=\"font-weight: 400;\"> heapq<\/span>\r\n\r\n<span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> dijkstra<\/span><span style=\"font-weight: 400;\">(graph, start)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Kh\u1edfi t\u1ea1o kho\u1ea3ng c\u00e1ch t\u1edbi c\u00e1c \u0111\u1ec9nh<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 distances = {vertex: float(<\/span><span style=\"font-weight: 400;\">'infinity'<\/span><span style=\"font-weight: 400;\">) <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> vertex <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> graph}<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 distances[start] = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1eed d\u1ee5ng priority queue \u0111\u1ec3 l\u01b0u c\u00e1c \u0111\u1ec9nh c\u1ea7n th\u0103m<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 priority_queue = [(<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, start)]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> priority_queue:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 current_distance, current_vertex = heapq.heappop(priority_queue)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># N\u1ebfu kho\u1ea3ng c\u00e1ch hi\u1ec7n t\u1ea1i l\u1edbn h\u01a1n kho\u1ea3ng c\u00e1ch l\u01b0u tr\u1eef, b\u1ecf qua<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> current_distance &gt; distances[current_vertex]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">continue<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Th\u0103m c\u00e1c \u0111\u1ec9nh k\u1ec1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> neighbor, weight <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> graph[current_vertex]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 distance = current_distance + weight<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># N\u1ebfu t\u00ecm \u0111\u01b0\u1ee3c \u0111\u01b0\u1eddng \u0111i ng\u1eafn h\u01a1n, c\u1eadp nh\u1eadt kho\u1ea3ng c\u00e1ch<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> distance &lt; distances[neighbor]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 distances[neighbor] = distance<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 heapq.heappush(priority_queue, (distance, neighbor))<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> distances<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">graph = {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">: [(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">), (<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)],<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: [(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)],<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: [(<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">)],<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: [],<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">: [(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">), (<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">), (<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">)]<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">start_vertex = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n<span style=\"font-weight: 400;\">distances = dijkstra(graph, start_vertex)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Kho\u1ea3ng c\u00e1ch t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">{start_vertex}<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn c\u00e1c \u0111\u1ec9nh kh\u00e1c:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> vertex <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> distances:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\u0110\u1ec9nh <\/span><span style=\"font-weight: 400;\">{vertex}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{distances[vertex]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Kho\u1ea3ng c\u00e1ch t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn c\u00e1c \u0111\u1ec9nh kh\u00e1c:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ec9nh <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ec9nh <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ec9nh <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ec9nh <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ec9nh <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">5<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u0110\u1ec9nh 0:<\/b><span style=\"font-weight: 400;\"> Kho\u1ea3ng c\u00e1ch b\u1eb1ng <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> (\u0111\u1ec9nh ngu\u1ed3n).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u0110\u1ec9nh 4:<\/b><span style=\"font-weight: 400;\"> Kho\u1ea3ng c\u00e1ch <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> th\u00f4ng qua c\u1ea1nh <\/span><span style=\"font-weight: 400;\">(0, 4, 5)<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u0110\u1ec9nh 1:<\/b><span style=\"font-weight: 400;\"> Kho\u1ea3ng c\u00e1ch <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"> th\u00f4ng qua \u0111\u01b0\u1eddng \u0111i <\/span><span style=\"font-weight: 400;\">0 -&gt; 4 -&gt; 1<\/span><span style=\"font-weight: 400;\"> v\u1edbi t\u1ed5ng tr\u1ecdng s\u1ed1 <\/span><span style=\"font-weight: 400;\">5 + 3 = 8<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u0110\u1ec9nh 3:<\/b><span style=\"font-weight: 400;\"> Kho\u1ea3ng c\u00e1ch <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\"> th\u00f4ng qua \u0111\u01b0\u1eddng \u0111i <\/span><span style=\"font-weight: 400;\">0 -&gt; 4 -&gt; 3<\/span><span style=\"font-weight: 400;\"> v\u1edbi t\u1ed5ng tr\u1ecdng s\u1ed1 <\/span><span style=\"font-weight: 400;\">5 + 2 = 7<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u0110\u1ec9nh 2:<\/b><span style=\"font-weight: 400;\"> Kho\u1ea3ng c\u00e1ch <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\"> th\u00f4ng qua \u0111\u01b0\u1eddng \u0111i <\/span><span style=\"font-weight: 400;\">0 -&gt; 4 -&gt; 1 -&gt; 2<\/span><span style=\"font-weight: 400;\"> v\u1edbi t\u1ed5ng tr\u1ecdng s\u1ed1 <\/span><span style=\"font-weight: 400;\">5 + 3 + 1 = 9<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Thu\u1eadt to\u00e1n Kruskal<\/b><\/h3>\n<p><b>Thu\u1eadt to\u00e1n Kruskal<\/b><span style=\"font-weight: 400;\"> t\u00ecm c\u00e2y khung nh\u1ecf nh\u1ea5t b\u1eb1ng c\u00e1ch s\u1eafp x\u1ebfp c\u00e1c c\u1ea1nh theo tr\u1ecdng s\u1ed1 t\u0103ng d\u1ea7n v\u00e0 ch\u1ecdn c\u00e1c c\u1ea1nh kh\u00f4ng t\u1ea1o th\u00e0nh chu tr\u00ecnh cho \u0111\u1ebfn khi \u0111\u1ee7 n-1 c\u1ea1nh.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t \u0111\u1ed3 th\u1ecb v\u00f4 h\u01b0\u1edbng c\u00f3 tr\u1ecdng s\u1ed1. H\u00e3y t\u00ecm c\u00e2y khung nh\u1ecf nh\u1ea5t (Minimum Spanning Tree &#8211; MST) c\u1ee7a \u0111\u1ed3 th\u1ecb s\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n Kruskal<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ed3 th\u1ecb:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ec9nh: <\/span><span style=\"font-weight: 400;\">A, B, C, D, E, F, G<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea1nh v\u00e0 tr\u1ecdng s\u1ed1:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(A, B, 7)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(A, D, 5)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(B, C, 8)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(B, D, 9)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(B, E, 7)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(C, E, 5)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(D, E, 15)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(D, F, 6)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(E, F, 8)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(E, G, 9)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">(F, G, 11)<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eafp x\u1ebfp c\u00e1c c\u1ea1nh theo tr\u1ecdng s\u1ed1 t\u0103ng d\u1ea7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o t\u1eadp h\u1ee3p c\u00e1c c\u00e2y con (m\u1ed7i \u0111\u1ec9nh l\u00e0 m\u1ed9t c\u00e2y con).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy\u1ec7t qua c\u00e1c c\u1ea1nh, n\u1ebfu hai \u0111\u1ec9nh c\u1ee7a c\u1ea1nh kh\u00f4ng c\u00f9ng m\u1ed9t c\u00e2y con, th\u00eam c\u1ea1nh v\u00e0o MST v\u00e0 h\u1ee3p nh\u1ea5t hai c\u00e2y con.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u1eb7p l\u1ea1i cho \u0111\u1ebfn khi MST c\u00f3 \u0111\u1ee7 n-1 c\u1ea1nh.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> Graph:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> __init__<\/span><span style=\"font-weight: 400;\">(self, vertices)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.V = vertices<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph = []<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Th\u00eam c\u1ea1nh v\u00e0o \u0111\u1ed3 th\u1ecb<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> add_edge<\/span><span style=\"font-weight: 400;\">(self, u, v, w)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph.append([u, v, w])<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># T\u00ecm t\u1eadp h\u1ee3p c\u1ee7a m\u1ed9t \u0111\u1ec9nh<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> find<\/span><span style=\"font-weight: 400;\">(self, parent, i)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> parent[i] != i:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 parent[i] = self.find(parent, parent[i])<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> parent[i]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># H\u1ee3p hai t\u1eadp h\u1ee3p<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> union<\/span><span style=\"font-weight: 400;\">(self, parent, rank, xroot, yroot)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> rank[xroot] &lt; rank[yroot]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 parent[xroot] = yroot<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">elif<\/span><span style=\"font-weight: 400;\"> rank[xroot] &gt; rank[yroot]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 parent[yroot] = xroot<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 parent[yroot] = xroot<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 rank[xroot] += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Thu\u1eadt to\u00e1n Kruskal<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> kruskal_mst<\/span><span style=\"font-weight: 400;\">(self)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 result = []\u00a0 <\/span><span style=\"font-weight: 400;\"># K\u1ebft qu\u1ea3 MST<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 i, e = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u1ec9 s\u1ed1 cho c\u1ea1nh s\u1eafp x\u1ebfp v\u00e0 k\u1ebft qu\u1ea3<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1eafp x\u1ebfp c\u00e1c c\u1ea1nh theo tr\u1ecdng s\u1ed1 t\u0103ng d\u1ea7n<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph = sorted(self.graph, key=<\/span><span style=\"font-weight: 400;\">lambda<\/span><span style=\"font-weight: 400;\"> item: item[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">])<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 parent = {}<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 rank = {}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Kh\u1edfi t\u1ea1o t\u1eadp h\u1ee3p c\u00e1c \u0111\u1ec9nh<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> node <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> self.V:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 parent[node] = node<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 rank[node] = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1ed1 c\u1ea1nh c\u1ee7a MST l\u00e0 V-1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> e &lt; len(self.V) - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 u, v, w = self.graph[i]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 x = self.find(parent, u)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 y = self.find(parent, v)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># N\u1ebfu th\u00eam c\u1ea1nh kh\u00f4ng t\u1ea1o chu tr\u00ecnh<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> x != y:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 e += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 result.append([u, v, w])<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 self.union(parent, rank, x, y)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"C\u1ea1nh trong c\u00e2y khung nh\u1ecf nh\u1ea5t:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> u, v, weight <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> result:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"<\/span><span style=\"font-weight: 400;\">{u}<\/span><span style=\"font-weight: 400;\"> -- <\/span><span style=\"font-weight: 400;\">{v}<\/span><span style=\"font-weight: 400;\"> == <\/span><span style=\"font-weight: 400;\">{weight}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>V\u00ed d\u1ee5 v\u00e0 th\u1ef1c thi:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">vertices = [<\/span><span style=\"font-weight: 400;\">'A'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'C'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'F'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'G'<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">g = Graph(vertices)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'A'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'A'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'C'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'C'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">15<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'F'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'F'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'G'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'F'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'G'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">g.kruskal_mst()<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">C\u1ea1nh trong c\u00e2y khung nh\u1ecf nh\u1ea5t:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">C &#8212; E == <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">A &#8212; D == <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">D &#8212; F == <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">A &#8212; B == <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B &#8212; E == <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">E &#8212; G == <\/span><span style=\"font-weight: 400;\">9<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u1ea1nh \u0111\u01b0\u1ee3c ch\u1ecdn kh\u00f4ng t\u1ea1o th\u00e0nh chu tr\u00ecnh v\u00e0 c\u00f3 t\u1ed5ng tr\u1ecdng s\u1ed1 nh\u1ecf nh\u1ea5t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ed5ng tr\u1ecdng s\u1ed1 c\u1ee7a MST l\u00e0 <\/span><span style=\"font-weight: 400;\">5 + 5 + 6 + 7 + 7 + 9 = 39<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Thu\u1eadt to\u00e1n Prim<\/b><\/h3>\n<p><b>Thu\u1eadt to\u00e1n Prim<\/b><span style=\"font-weight: 400;\"> x\u00e2y d\u1ef1ng MST b\u1eb1ng c\u00e1ch kh\u1edfi \u0111\u1ea7u t\u1eeb m\u1ed9t \u0111\u1ec9nh v\u00e0 li\u00ean t\u1ee5c th\u00eam c\u1ea1nh c\u00f3 tr\u1ecdng s\u1ed1 nh\u1ecf nh\u1ea5t k\u1ebft n\u1ed1i \u0111\u1ec9nh \u0111\u00e3 th\u0103m v\u1edbi \u0111\u1ec9nh ch\u01b0a th\u0103m.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t \u0111\u1ed3 th\u1ecb v\u00f4 h\u01b0\u1edbng c\u00f3 tr\u1ecdng s\u1ed1. H\u00e3y t\u00ecm c\u00e2y khung nh\u1ecf nh\u1ea5t (Minimum Spanning Tree &#8211; MST) c\u1ee7a \u0111\u1ed3 th\u1ecb s\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n Prim<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng c\u00f9ng \u0111\u1ed3 th\u1ecb nh\u01b0 trong thu\u1eadt to\u00e1n Kruskal.<\/span><\/p>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o m\u1ed9t \u0111\u1ec9nh g\u1ed1c v\u00e0 t\u1eadp h\u1ee3p c\u00e1c \u0111\u1ec9nh \u0111\u00e3 th\u0103m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng m\u1ed9t m\u1ea3ng \u0111\u1ec3 l\u01b0u tr\u1eef tr\u1ecdng s\u1ed1 nh\u1ecf nh\u1ea5t k\u1ebft n\u1ed1i \u0111\u1ebfn m\u1ed7i \u0111\u1ec9nh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u1ede m\u1ed7i b\u01b0\u1edbc, ch\u1ecdn \u0111\u1ec9nh ch\u01b0a th\u0103m c\u00f3 tr\u1ecdng s\u1ed1 k\u1ebft n\u1ed1i nh\u1ecf nh\u1ea5t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt tr\u1ecdng s\u1ed1 k\u1ebft n\u1ed1i cho c\u00e1c \u0111\u1ec9nh k\u1ec1 c\u1ee7a \u0111\u1ec9nh v\u1eeba th\u00eam.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">import<\/span><span style=\"font-weight: 400;\"> sys<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class<\/span><span style=\"font-weight: 400;\"> Graph:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> __init__<\/span><span style=\"font-weight: 400;\">(self, vertices)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.V = vertices<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph = [[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">]*len(vertices) <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> _ <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(len(vertices))]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Th\u00eam c\u1ea1nh v\u00e0o \u0111\u1ed3 th\u1ecb<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> add_edge<\/span><span style=\"font-weight: 400;\">(self, u, v, w)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 idx_u = self.V.index(u)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 idx_v = self.V.index(v)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph[idx_u][idx_v] = w<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 self.graph[idx_v][idx_u] = w<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># T\u00ecm \u0111\u1ec9nh c\u00f3 tr\u1ecdng s\u1ed1 nh\u1ecf nh\u1ea5t<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> min_key<\/span><span style=\"font-weight: 400;\">(self, key, mst_set)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 min = sys.maxsize<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 min_index = <\/span><span style=\"font-weight: 400;\">-1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> v <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(len(self.V)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> key[v] &lt; min <\/span><span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> mst_set[v] == <\/span><span style=\"font-weight: 400;\">False<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 min = key[v]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 min_index = v<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> min_index<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Thu\u1eadt to\u00e1n Prim<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> prim_mst<\/span><span style=\"font-weight: 400;\">(self)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 key = [sys.maxsize] * len(self.V)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 parent = [<\/span><span style=\"font-weight: 400;\">None<\/span><span style=\"font-weight: 400;\">] * len(self.V)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 key[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 mst_set = [<\/span><span style=\"font-weight: 400;\">False<\/span><span style=\"font-weight: 400;\">] * len(self.V)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 parent[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] = <\/span><span style=\"font-weight: 400;\">-1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> _ <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(len(self.V)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 u = self.min_key(key, mst_set)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 mst_set[u] = <\/span><span style=\"font-weight: 400;\">True<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> v <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(len(self.V)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> self.graph[u][v] &gt; <\/span><span style=\"font-weight: 400;\">0<\/span> <span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> mst_set[v] == <\/span><span style=\"font-weight: 400;\">False<\/span> <span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> key[v] &gt; self.graph[u][v]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 key[v] = self.graph[u][v]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 parent[v] = u<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"C\u1ea1nh trong c\u00e2y khung nh\u1ecf nh\u1ea5t:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, len(self.V)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"<\/span><span style=\"font-weight: 400;\">{self.V[parent[i]]}<\/span><span style=\"font-weight: 400;\"> -- <\/span><span style=\"font-weight: 400;\">{self.V[i]}<\/span><span style=\"font-weight: 400;\"> == <\/span><span style=\"font-weight: 400;\">{self.graph[parent[i]][i]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><b>V\u00ed d\u1ee5 v\u00e0 th\u1ef1c thi:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">vertices = [<\/span><span style=\"font-weight: 400;\">'A'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'C'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'F'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'G'<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">g = Graph(vertices)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'A'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'A'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'C'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'B'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'C'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">15<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'D'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'F'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'F'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'E'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'G'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">g.add_edge(<\/span><span style=\"font-weight: 400;\">'F'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">'G'<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">g.prim_mst()<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">C\u1ea1nh trong c\u00e2y khung nh\u1ecf nh\u1ea5t:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">A &#8212; D == <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">D &#8212; F == <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">F &#8212; E == <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">E &#8212; C == <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">E &#8212; B == <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">E &#8212; G == <\/span><span style=\"font-weight: 400;\">9<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u t\u1eeb \u0111\u1ec9nh <\/span><span style=\"font-weight: 400;\">A<\/span><span style=\"font-weight: 400;\">, ch\u1ecdn c\u1ea1nh c\u00f3 tr\u1ecdng s\u1ed1 nh\u1ecf nh\u1ea5t <\/span><span style=\"font-weight: 400;\">(A, D, 5)<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ti\u1ebfp t\u1ee5c ch\u1ecdn c\u00e1c c\u1ea1nh c\u00f3 tr\u1ecdng s\u1ed1 nh\u1ecf nh\u1ea5t k\u1ebft n\u1ed1i c\u00e1c \u0111\u1ec9nh \u0111\u00e3 th\u0103m v\u1edbi \u0111\u1ec9nh ch\u01b0a th\u0103m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 MST c\u00f3 th\u1ec3 kh\u00e1c so v\u1edbi Kruskal nh\u01b0ng t\u1ed5ng tr\u1ecdng s\u1ed1 s\u1ebd b\u1eb1ng ho\u1eb7c t\u01b0\u01a1ng \u0111\u01b0\u01a1ng.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Quy_hoach_Dong_Dynamic_Programming\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n Quy ho\u1ea1ch \u0110\u1ed9ng (Dynamic Programming)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Knapsack Problem (B\u00e0i to\u00e1n C\u00e1i Balo)<\/b><\/h3>\n<p><b>B\u00e0i to\u00e1n C\u00e1i Balo 0\/1<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t v\u00ed d\u1ee5 kinh \u0111i\u1ec3n c\u1ee7a quy ho\u1ea1ch \u0111\u1ed9ng. Ch\u00fang ta c\u1ea7n x\u00e1c \u0111\u1ecbnh gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t c\u00f3 th\u1ec3 m\u00e0 kh\u00f4ng v\u01b0\u1ee3t qu\u00e1 tr\u1ecdng l\u01b0\u1ee3ng t\u1ed1i \u0111a.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t t\u1eadp c\u00e1c \u0111\u1ed3 v\u1eadt, m\u1ed7i \u0111\u1ed3 v\u1eadt c\u00f3 tr\u1ecdng l\u01b0\u1ee3ng v\u00e0 gi\u00e1 tr\u1ecb ri\u00eang. B\u1ea1n c\u00f3 m\u1ed9t c\u00e1i balo c\u00f3 gi\u1edbi h\u1ea1n tr\u1ecdng l\u01b0\u1ee3ng t\u1ed1i \u0111a. H\u00e3y ch\u1ecdn m\u1ed9t t\u1eadp c\u00e1c \u0111\u1ed3 v\u1eadt \u0111\u1ec3 \u0111\u01b0a v\u00e0o balo sao cho t\u1ed5ng gi\u00e1 tr\u1ecb l\u00e0 l\u1edbn nh\u1ea5t m\u00e0 t\u1ed5ng tr\u1ecdng l\u01b0\u1ee3ng kh\u00f4ng v\u01b0\u1ee3t qu\u00e1 gi\u1edbi h\u1ea1n c\u1ee7a balo.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Danh s\u00e1ch \u0111\u1ed3 v\u1eadt:<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><b>\u0110\u1ed3 v\u1eadt<\/b><\/td>\n<td><b>Tr\u1ecdng l\u01b0\u1ee3ng (w)<\/b><\/td>\n<td><b>Gi\u00e1 tr\u1ecb (v)<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">8<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Tr\u1ecdng l\u01b0\u1ee3ng t\u1ed1i \u0111a c\u1ee7a balo: <\/span><span style=\"font-weight: 400;\">5<\/span><\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ea1o m\u1ed9t b\u1ea3ng hai chi\u1ec1u <\/span><span style=\"font-weight: 400;\">dp[n+1][W+1]<\/span><span style=\"font-weight: 400;\">, trong \u0111\u00f3 <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\"> l\u00e0 s\u1ed1 \u0111\u1ed3 v\u1eadt v\u00e0 <\/span><span style=\"font-weight: 400;\">W<\/span><span style=\"font-weight: 400;\"> l\u00e0 tr\u1ecdng l\u01b0\u1ee3ng t\u1ed1i \u0111a.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">dp[i][w]<\/span><span style=\"font-weight: 400;\"> s\u1ebd l\u01b0u tr\u1eef gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t c\u00f3 th\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c v\u1edbi <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3 v\u1eadt \u0111\u1ea7u ti\u00ean v\u00e0 tr\u1ecdng l\u01b0\u1ee3ng t\u1ed1i \u0111a l\u00e0 <\/span><span style=\"font-weight: 400;\">w<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">C\u00f4ng th\u1ee9c truy h\u1ed3i:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">dp[i][w] = dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][w] n\u1ebfu w_i &gt; w<\/span>\r\n<span style=\"font-weight: 400;\">dp[i][w] = max(dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][w], dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][w - w_i] + v_i) n\u1ebfu w_i \u2264 w<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong \u0111\u00f3:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">w_i<\/span><span style=\"font-weight: 400;\"> l\u00e0 tr\u1ecdng l\u01b0\u1ee3ng c\u1ee7a \u0111\u1ed3 v\u1eadt th\u1ee9 <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">v_i<\/span><span style=\"font-weight: 400;\"> l\u00e0 gi\u00e1 tr\u1ecb c\u1ee7a \u0111\u1ed3 v\u1eadt th\u1ee9 <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> knapsack<\/span><span style=\"font-weight: 400;\">(W, wt, val, n)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># T\u1ea1o b\u1ea3ng dp v\u1edbi k\u00edch th\u01b0\u1edbc (n+1) x (W+1)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 dp = [[<\/span><span style=\"font-weight: 400;\">0<\/span> <span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> x <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(W + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)] <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> x <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(n + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># X\u00e2y d\u1ef1ng b\u1ea3ng dp theo c\u00e1ch t\u1eeb d\u01b0\u1edbi l\u00ean<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(n + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> w <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(W + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> i == <\/span><span style=\"font-weight: 400;\">0<\/span> <span style=\"font-weight: 400;\">or<\/span><span style=\"font-weight: 400;\"> w == <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dp[i][w] = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">elif<\/span><span style=\"font-weight: 400;\"> wt[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] &lt;= w:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dp[i][w] = max(val[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] + dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][w - wt[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]], dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][w])<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dp[i][w] = dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][w]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># In b\u1ea3ng dp sau m\u1ed7i b\u01b0\u1edbc (t\u00f9y ch\u1ecdn)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># print(f\"dp[{i}]: {dp[i]}\")<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t \u0111\u1ea1t \u0111\u01b0\u1ee3c<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 max_value = dp[n][W]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t c\u00f3 th\u1ec3: <\/span><span style=\"font-weight: 400;\">{max_value}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Truy v\u1ebft \u0111\u1ec3 t\u00ecm \u0111\u1ed3 v\u1eadt \u0111\u01b0\u1ee3c ch\u1ecdn<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 w = W<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 items_selected = []<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(n, <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> max_value &lt;= <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">break<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> max_value == dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][w]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">continue<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># \u0110\u1ed3 v\u1eadt n\u00e0y \u0111\u01b0\u1ee3c ch\u1ecdn<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 items_selected.append(i)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 max_value -= val[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 w -= wt[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\u0110\u1ed3 v\u1eadt \u0111\u01b0\u1ee3c ch\u1ecdn: <\/span><span style=\"font-weight: 400;\">{items_selected[::<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">val = [<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">wt = [<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">W = <\/span><span style=\"font-weight: 400;\">5<\/span>\r\n<span style=\"font-weight: 400;\">n = len(val)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch gi\u00e1 tr\u1ecb: <\/span><span style=\"font-weight: 400;\">{val}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Danh s\u00e1ch tr\u1ecdng l\u01b0\u1ee3ng: <\/span><span style=\"font-weight: 400;\">{wt}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Tr\u1ecdng l\u01b0\u1ee3ng t\u1ed1i \u0111a c\u1ee7a balo: <\/span><span style=\"font-weight: 400;\">{W}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">knapsack(W, wt, val, n)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch gi\u00e1 tr\u1ecb: [<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Danh s\u00e1ch tr\u1ecdng l\u01b0\u1ee3ng: [<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Tr\u1ecdng l\u01b0\u1ee3ng t\u1ed1i \u0111a c\u1ee7a balo: <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t c\u00f3 th\u1ec3: <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ed3 v\u1eadt \u0111\u01b0\u1ee3c ch\u1ecdn: [<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u1ea3ng dp:<\/b><span style=\"font-weight: 400;\"> \u0110\u01b0\u1ee3c x\u00e2y d\u1ef1ng \u0111\u1ec3 l\u01b0u tr\u1eef gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t c\u00f3 th\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c cho m\u1ed7i tr\u1ecdng l\u01b0\u1ee3ng t\u1eeb <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn <\/span><span style=\"font-weight: 400;\">W<\/span><span style=\"font-weight: 400;\"> v\u1edbi <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3 v\u1eadt.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Truy v\u1ebft \u0111\u1ed3 v\u1eadt \u0111\u01b0\u1ee3c ch\u1ecdn:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u t\u1eeb <\/span><span style=\"font-weight: 400;\">dp[n][W]<\/span><span style=\"font-weight: 400;\">, n\u1ebfu <\/span><span style=\"font-weight: 400;\">dp[i][w]<\/span><span style=\"font-weight: 400;\"> kh\u00e1c <\/span><span style=\"font-weight: 400;\">dp[i-1][w]<\/span><span style=\"font-weight: 400;\">, ngh\u0129a l\u00e0 \u0111\u1ed3 v\u1eadt th\u1ee9 <\/span><span style=\"font-weight: 400;\">i<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c ch\u1ecdn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Tr\u1eeb gi\u00e1 tr\u1ecb v\u00e0 tr\u1ecdng l\u01b0\u1ee3ng c\u1ee7a \u0111\u1ed3 v\u1eadt \u0111\u00e3 ch\u1ecdn v\u00e0 ti\u1ebfp t\u1ee5c truy v\u1ebft.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ebft qu\u1ea3:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t \u0111\u1ea1t \u0111\u01b0\u1ee3c l\u00e0 <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">\u0110\u1ed3 v\u1eadt \u0111\u01b0\u1ee3c ch\u1ecdn l\u00e0 <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"> (v\u00ec \u0111\u00e1nh s\u1ed1 t\u1eeb <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\">), t\u01b0\u01a1ng \u1ee9ng v\u1edbi \u0111\u1ed3 v\u1eadt c\u00f3 gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"> v\u00e0 tr\u1ecdng l\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Longest Common Subsequence (Chu\u1ed7i con chung d\u00e0i nh\u1ea5t)<\/b><\/h3>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho hai chu\u1ed7i <\/span><span style=\"font-weight: 400;\">X<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">Y<\/span><span style=\"font-weight: 400;\">, h\u00e3y t\u00ecm \u0111\u1ed9 d\u00e0i c\u1ee7a chu\u1ed7i con chung d\u00e0i nh\u1ea5t gi\u1eefa hai chu\u1ed7i. Chu\u1ed7i con chung d\u00e0i nh\u1ea5t l\u00e0 chu\u1ed7i c\u00e1c k\u00fd t\u1ef1 xu\u1ea5t hi\u1ec7n trong c\u1ea3 hai chu\u1ed7i theo c\u00f9ng th\u1ee9 t\u1ef1 nh\u01b0ng kh\u00f4ng nh\u1ea5t thi\u1ebft ph\u1ea3i li\u1ec1n k\u1ec1.<\/span><\/p>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">X = &#8220;AGGTAB&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Chu\u1ed7i <\/span><span style=\"font-weight: 400;\">Y = &#8220;GXTXAYB&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Chu\u1ed7i con chung d\u00e0i nh\u1ea5t: <\/span><span style=\"font-weight: 400;\">&#8220;GTAB&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ed9 d\u00e0i: <\/span><span style=\"font-weight: 400;\">4<\/span><\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng quy ho\u1ea1ch \u0111\u1ed9ng \u0111\u1ec3 x\u00e2y d\u1ef1ng m\u1ed9t b\u1ea3ng <\/span><span style=\"font-weight: 400;\">dp[m+1][n+1]<\/span><span style=\"font-weight: 400;\">, trong \u0111\u00f3 <\/span><span style=\"font-weight: 400;\">m<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\"> l\u00e0 \u0111\u1ed9 d\u00e0i c\u1ee7a hai chu\u1ed7i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">dp[i][j]<\/span><span style=\"font-weight: 400;\"> l\u01b0u tr\u1eef \u0111\u1ed9 d\u00e0i c\u1ee7a chu\u1ed7i con chung d\u00e0i nh\u1ea5t gi\u1eefa <\/span><span style=\"font-weight: 400;\">X[0..i-1]<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">Y[0..j-1]<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">C\u00f4ng th\u1ee9c truy h\u1ed3i:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">dp[i][j] = dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] + <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> n\u1ebfu X[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] == Y[j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">dp[i][j] = max(dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][j], dp[i][j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]) n\u1ebfu X[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] != Y[j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]<\/span><\/pre>\n<p>Code Python:<\/p>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> lcs<\/span><span style=\"font-weight: 400;\">(X, Y)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 m = len(X)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 n = len(Y)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># T\u1ea1o b\u1ea3ng dp v\u1edbi k\u00edch th\u01b0\u1edbc (m+1) x (n+1)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 dp = [[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">]*(n+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">) <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(m+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># X\u00e2y d\u1ef1ng b\u1ea3ng dp t\u1eeb d\u01b0\u1edbi l\u00ean<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(m+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> j <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(n+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> i == <\/span><span style=\"font-weight: 400;\">0<\/span> <span style=\"font-weight: 400;\">or<\/span><span style=\"font-weight: 400;\"> j == <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dp[i][j] = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">elif<\/span><span style=\"font-weight: 400;\"> X[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] == Y[j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dp[i][j] = dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] + <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># print(f\"dp[{i}][{j}] ({X[i-1]}): {dp[i][j]}\")<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 dp[i][j] = max(dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][j], dp[i][j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">])<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># print(f\"dp[{i}][{j}]: {dp[i][j]}\")<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># \u0110\u1ed9 d\u00e0i c\u1ee7a LCS<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 length = dp[m][n]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\u0110\u1ed9 d\u00e0i c\u1ee7a chu\u1ed7i con chung d\u00e0i nh\u1ea5t: <\/span><span style=\"font-weight: 400;\">{length}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Truy v\u1ebft \u0111\u1ec3 t\u00ecm LCS<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 index = dp[m][n]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 lcs_str = [<\/span><span style=\"font-weight: 400;\">\"\"<\/span><span style=\"font-weight: 400;\">] * index\u00a0 <\/span><span style=\"font-weight: 400;\"># T\u1ea1o m\u1ea3ng l\u01b0u tr\u1eef LCS<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 i = m<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 j = n<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> i &gt; <\/span><span style=\"font-weight: 400;\">0<\/span> <span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> j &gt; <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> X[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">] == Y[j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 lcs_str[index - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] = X[i - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i -= <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 j -= <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 index -= <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">elif<\/span><span style=\"font-weight: 400;\"> dp[i<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][j] &gt; dp[i][j<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i -= <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 j -= <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Chu\u1ed7i con chung d\u00e0i nh\u1ea5t: <\/span><span style=\"font-weight: 400;\">{<\/span><span style=\"font-weight: 400;\">''<\/span><span style=\"font-weight: 400;\">.join(lcs_str)}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">X = <\/span><span style=\"font-weight: 400;\">\"AGGTAB\"<\/span>\r\n<span style=\"font-weight: 400;\">Y = <\/span><span style=\"font-weight: 400;\">\"GXTXAYB\"<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Chu\u1ed7i X: <\/span><span style=\"font-weight: 400;\">{X}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">print(<\/span><span style=\"font-weight: 400;\">f\"Chu\u1ed7i Y: <\/span><span style=\"font-weight: 400;\">{Y}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">lcs(X, Y)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Chu\u1ed7i X: AGGTAB<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Chu\u1ed7i Y: GXTXAYB<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1ed9 d\u00e0i c\u1ee7a chu\u1ed7i con chung d\u00e0i nh\u1ea5t: <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Chu\u1ed7i con chung d\u00e0i nh\u1ea5t: GTAB<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u1ea3ng dp:<\/b><span style=\"font-weight: 400;\"> L\u01b0u tr\u1eef \u0111\u1ed9 d\u00e0i c\u1ee7a LCS t\u1ea1i m\u1ed7i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">(i, j)<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Truy v\u1ebft LCS:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u t\u1eeb <\/span><span style=\"font-weight: 400;\">dp[m][n]<\/span><span style=\"font-weight: 400;\">, n\u1ebfu <\/span><span style=\"font-weight: 400;\">X[i-1] == Y[j-1]<\/span><span style=\"font-weight: 400;\">, k\u00fd t\u1ef1 \u0111\u00f3 thu\u1ed9c LCS.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Di chuy\u1ec3n ng\u01b0\u1ee3c l\u00ean <\/span><span style=\"font-weight: 400;\">i-1<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">j-1<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">N\u1ebfu kh\u00f4ng, di chuy\u1ec3n \u0111\u1ebfn \u00f4 c\u00f3 gi\u00e1 tr\u1ecb l\u1edbn h\u01a1n trong <\/span><span style=\"font-weight: 400;\">dp[i-1][j]<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">dp[i][j-1]<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ebft qu\u1ea3:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">\u0110\u1ed9 d\u00e0i c\u1ee7a LCS l\u00e0 <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Chu\u1ed7i con chung d\u00e0i nh\u1ea5t l\u00e0 <\/span><span style=\"font-weight: 400;\">&#8220;GTAB&#8221;<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Tham_lam_Greedy_Algorithm\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n Tham lam (Greedy Algorithm)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Coin Change Problem (B\u00e0i to\u00e1n \u0110\u1ed5i Ti\u1ec1n)<\/b><\/h3>\n<p><b>Thu\u1eadt to\u00e1n Tham lam<\/b><span style=\"font-weight: 400;\"> ho\u1ea1t \u0111\u1ed9ng b\u1eb1ng c\u00e1ch t\u1ea1i m\u1ed7i b\u01b0\u1edbc, ch\u00fang ta ch\u1ecdn l\u1ef1a ch\u1ecdn t\u1ed1t nh\u1ea5t hi\u1ec7n t\u1ea1i m\u00e0 kh\u00f4ng quan t\u00e2m \u0111\u1ebfn k\u1ebft qu\u1ea3 trong t\u01b0\u01a1ng lai. Trong b\u00e0i to\u00e1n \u0111\u1ed5i ti\u1ec1n, t\u1ea1i m\u1ed7i b\u01b0\u1edbc, ch\u00fang ta s\u1ebd ch\u1ecdn m\u1ec7nh gi\u00e1 ti\u1ec1n xu l\u1edbn nh\u1ea5t c\u00f3 th\u1ec3 m\u00e0 kh\u00f4ng v\u01b0\u1ee3t qu\u00e1 s\u1ed1 ti\u1ec1n c\u00f2n l\u1ea1i c\u1ea7n \u0111\u1ed5i.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t s\u1ed1 ti\u1ec1n c\u1ea7n \u0111\u1ed5i <\/span><span style=\"font-weight: 400;\">S<\/span><span style=\"font-weight: 400;\"> v\u00e0 m\u1ed9t t\u1eadp c\u00e1c m\u1ec7nh gi\u00e1 ti\u1ec1n xu c\u00f3 s\u1eb5n. H\u00e3y t\u00ecm s\u1ed1 l\u01b0\u1ee3ng ti\u1ec1n xu \u00edt nh\u1ea5t c\u1ea7n d\u00f9ng \u0111\u1ec3 \u0111\u1ed5i \u0111\u01b0\u1ee3c s\u1ed1 ti\u1ec1n <\/span><span style=\"font-weight: 400;\">S<\/span><span style=\"font-weight: 400;\">. S\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n Tham lam<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 gi\u1ea3i quy\u1ebft b\u00e0i to\u00e1n n\u00e0y.<\/span><\/p>\n<blockquote><p><b>L\u01b0u \u00fd:<\/b><span style=\"font-weight: 400;\"> B\u00e0i to\u00e1n n\u00e0y ch\u1ec9 \u0111\u1ea3m b\u1ea3o \u0111\u01b0a ra k\u1ebft qu\u1ea3 t\u1ed1i \u01b0u khi t\u1eadp c\u00e1c m\u1ec7nh gi\u00e1 c\u00f3 t\u00ednh ch\u1ea5t \u0111\u1eb7c bi\u1ec7t (v\u00ed d\u1ee5: h\u1ec7 th\u1ed1ng ti\u1ec1n t\u1ec7 c\u1ee7a m\u1ed9t s\u1ed1 qu\u1ed1c gia). V\u1edbi h\u1ec7 th\u1ed1ng m\u1ec7nh gi\u00e1 b\u1ea5t k\u1ef3, thu\u1eadt to\u00e1n tham lam c\u00f3 th\u1ec3 kh\u00f4ng \u0111\u01b0a ra k\u1ebft qu\u1ea3 t\u1ed1i \u01b0u.<\/span><\/p><\/blockquote>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u1ec7nh gi\u00e1 ti\u1ec1n xu: <\/span><span style=\"font-weight: 400;\">[1, 2, 5, 10, 20, 50, 100, 200, 500, 1000]<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u1ea7n \u0111\u1ed5i: <\/span><span style=\"font-weight: 400;\">2753<\/span><\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eafp x\u1ebfp c\u00e1c m\u1ec7nh gi\u00e1 ti\u1ec1n xu theo th\u1ee9 t\u1ef1 gi\u1ea3m d\u1ea7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o m\u1ed9t danh s\u00e1ch \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c ti\u1ec1n xu \u0111\u01b0\u1ee3c ch\u1ecdn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy\u1ec7t qua c\u00e1c m\u1ec7nh gi\u00e1 ti\u1ec1n xu: <\/span><span style=\"font-weight: 400;\">Trong khi m\u1ec7nh gi\u00e1 hi\u1ec7n t\u1ea1i nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng s\u1ed1 ti\u1ec1n c\u1ea7n \u0111\u1ed5i:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"3\"><span style=\"font-weight: 400;\">Th\u00eam m\u1ec7nh gi\u00e1 v\u00e0o danh s\u00e1ch k\u1ebft qu\u1ea3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"3\"><span style=\"font-weight: 400;\">Gi\u1ea3m s\u1ed1 ti\u1ec1n c\u1ea7n \u0111\u1ed5i b\u1eb1ng m\u1ec7nh gi\u00e1 \u0111\u00e3 ch\u1ecdn.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> coin_change<\/span><span style=\"font-weight: 400;\">(coins, amount)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1eafp x\u1ebfp m\u1ec7nh gi\u00e1 theo th\u1ee9 t\u1ef1 gi\u1ea3m d\u1ea7n<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 coins.sort(reverse=<\/span><span style=\"font-weight: 400;\">True<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 result = []<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"M\u1ec7nh gi\u00e1 ti\u1ec1n xu (gi\u1ea3m d\u1ea7n): <\/span><span style=\"font-weight: 400;\">{coins}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"S\u1ed1 ti\u1ec1n c\u1ea7n \u0111\u1ed5i: <\/span><span style=\"font-weight: 400;\">{amount}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> coin <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> coins:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 count = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1ed1 l\u01b0\u1ee3ng \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 hi\u1ec7n t\u1ea1i<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> amount &gt;= coin:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 amount -= coin<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 result.append(coin)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 count += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> count &gt; <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">{count}<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">{coin}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> amount != <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"Kh\u00f4ng th\u1ec3 \u0111\u1ed5i ch\u00ednh x\u00e1c s\u1ed1 ti\u1ec1n v\u1edbi m\u1ec7nh gi\u00e1 hi\u1ec7n c\u00f3.\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"T\u1ed5ng s\u1ed1 \u0111\u1ed3ng xu c\u1ea7n d\u00f9ng: <\/span><span style=\"font-weight: 400;\">{len(result)}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"C\u00e1c \u0111\u1ed3ng xu \u0111\u01b0\u1ee3c ch\u1ecdn: <\/span><span style=\"font-weight: 400;\">{result}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">coins = [<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">50<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">100<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">200<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">500<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1000<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">amount = <\/span><span style=\"font-weight: 400;\">2753<\/span>\r\n<span style=\"font-weight: 400;\">coin_change(coins, amount)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">M\u1ec7nh gi\u00e1 ti\u1ec1n xu (gi\u1ea3m d\u1ea7n): [<\/span><span style=\"font-weight: 400;\">1000<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">500<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">200<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">100<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">50<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u1ea7n \u0111\u1ed5i: <\/span><span style=\"font-weight: 400;\">2753<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">1000<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">500<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">200<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">100<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">50<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">T\u1ed5ng s\u1ed1 \u0111\u1ed3ng xu c\u1ea7n d\u00f9ng: <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">C\u00e1c \u0111\u1ed3ng xu \u0111\u01b0\u1ee3c ch\u1ecdn: [<\/span><span style=\"font-weight: 400;\">1000<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1000<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">500<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">200<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">50<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> B\u1eaft \u0111\u1ea7u v\u1edbi m\u1ec7nh gi\u00e1 l\u1edbn nh\u1ea5t l\u00e0 <\/span><span style=\"font-weight: 400;\">1000<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u1ea7n \u0111\u1ed5i <\/span><span style=\"font-weight: 400;\">2753 &gt;= 1000<\/span><span style=\"font-weight: 400;\">, n\u00ean ch\u1ecdn <\/span><span style=\"font-weight: 400;\">1000<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u00f2n l\u1ea1i: <\/span><span style=\"font-weight: 400;\">2753 &#8211; 1000 = 1753<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Ti\u1ebfp t\u1ee5c ch\u1ecdn <\/span><span style=\"font-weight: 400;\">1000<\/span><span style=\"font-weight: 400;\"> v\u00ec <\/span><span style=\"font-weight: 400;\">1753 &gt;= 1000<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u00f2n l\u1ea1i: <\/span><span style=\"font-weight: 400;\">1753 &#8211; 1000 = 753<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 2:<\/b><span style=\"font-weight: 400;\"> Chuy\u1ec3n sang m\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">500<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">753 &gt;= 500<\/span><span style=\"font-weight: 400;\">, n\u00ean ch\u1ecdn <\/span><span style=\"font-weight: 400;\">500<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u00f2n l\u1ea1i: <\/span><span style=\"font-weight: 400;\">753 &#8211; 500 = 253<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 3:<\/b><span style=\"font-weight: 400;\"> M\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">200<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">253 &gt;= 200<\/span><span style=\"font-weight: 400;\">, ch\u1ecdn <\/span><span style=\"font-weight: 400;\">200<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u00f2n l\u1ea1i: <\/span><span style=\"font-weight: 400;\">253 &#8211; 200 = 53<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 4:<\/b><span style=\"font-weight: 400;\"> M\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">100<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">53 &lt; 100<\/span><span style=\"font-weight: 400;\">, b\u1ecf qua.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 5:<\/b><span style=\"font-weight: 400;\"> M\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">50<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">53 &gt;= 50<\/span><span style=\"font-weight: 400;\">, ch\u1ecdn <\/span><span style=\"font-weight: 400;\">50<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u00f2n l\u1ea1i: <\/span><span style=\"font-weight: 400;\">53 &#8211; 50 = 3<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 6:<\/b><span style=\"font-weight: 400;\"> M\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">3 &lt; 20<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, b\u1ecf qua.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 7:<\/b><span style=\"font-weight: 400;\"> M\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">3 &gt;= 2<\/span><span style=\"font-weight: 400;\">, ch\u1ecdn <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u00f2n l\u1ea1i: <\/span><span style=\"font-weight: 400;\">3 &#8211; 2 = 1<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 8:<\/b><span style=\"font-weight: 400;\"> M\u1ec7nh gi\u00e1 <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">1 &gt;= 1<\/span><span style=\"font-weight: 400;\">, ch\u1ecdn <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">S\u1ed1 ti\u1ec1n c\u00f2n l\u1ea1i: <\/span><span style=\"font-weight: 400;\">1 &#8211; 1 = 0<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ebft lu\u1eadn:<\/b><span style=\"font-weight: 400;\"> S\u1ed1 ti\u1ec1n \u0111\u00e3 \u0111\u01b0\u1ee3c \u0111\u1ed5i ch\u00ednh x\u00e1c v\u1edbi t\u1ed5ng c\u1ed9ng <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed3ng xu.<\/span><\/li>\n<\/ul>\n<p><b>L\u01b0u \u00fd:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Thu\u1eadt to\u00e1n tham lam ho\u1ea1t \u0111\u1ed9ng hi\u1ec7u qu\u1ea3 v\u1edbi h\u1ec7 th\u1ed1ng m\u1ec7nh gi\u00e1 c\u1ee7a Vi\u1ec7t Nam ho\u1eb7c M\u1ef9 v\u00ec m\u1ec7nh gi\u00e1 c\u00f3 t\u00ednh ch\u1ea5t \u0111\u1eb7c bi\u1ec7t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">V\u1edbi c\u00e1c h\u1ec7 th\u1ed1ng m\u1ec7nh gi\u00e1 kh\u00e1c, thu\u1eadt to\u00e1n tham lam c\u00f3 th\u1ec3 kh\u00f4ng \u0111\u01b0a ra k\u1ebft qu\u1ea3 t\u1ed1i \u01b0u.<\/span><\/li>\n<\/ul>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Activity Selection Problem (B\u00e0i to\u00e1n Ch\u1ecdn Ho\u1ea1t \u0110\u1ed9ng)<\/b><\/h3>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t t\u1eadp c\u00e1c ho\u1ea1t \u0111\u1ed9ng, m\u1ed7i ho\u1ea1t \u0111\u1ed9ng c\u00f3 th\u1eddi gian b\u1eaft \u0111\u1ea7u v\u00e0 k\u1ebft th\u00fac. M\u1ee5c ti\u00eau l\u00e0 ch\u1ecdn nhi\u1ec1u nh\u1ea5t c\u00e1c ho\u1ea1t \u0111\u1ed9ng kh\u00f4ng tr\u00f9ng th\u1eddi gian \u0111\u1ec3 tham gia. S\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n Tham lam<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u00ecm t\u1eadp c\u00e1c ho\u1ea1t \u0111\u1ed9ng t\u1ed1i \u0111a c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n.<\/span><\/p>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Danh s\u00e1ch c\u00e1c ho\u1ea1t \u0111\u1ed9ng:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Ho\u1ea1t \u0111\u1ed9ng<\/b><\/td>\n<td><b>Th\u1eddi gian b\u1eaft \u0111\u1ea7u<\/b><\/td>\n<td><b>Th\u1eddi gian k\u1ebft th\u00fac<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">6<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">7<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">9<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">6<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">9<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">7<\/span><\/td>\n<td><span style=\"font-weight: 400;\">6<\/span><\/td>\n<td><span style=\"font-weight: 400;\">10<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">8<\/span><\/td>\n<td><span style=\"font-weight: 400;\">8<\/span><\/td>\n<td><span style=\"font-weight: 400;\">11<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">9<\/span><\/td>\n<td><span style=\"font-weight: 400;\">8<\/span><\/td>\n<td><span style=\"font-weight: 400;\">12<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">10<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">14<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">11<\/span><\/td>\n<td><span style=\"font-weight: 400;\">12<\/span><\/td>\n<td><span style=\"font-weight: 400;\">16<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eafp x\u1ebfp c\u00e1c ho\u1ea1t \u0111\u1ed9ng theo th\u1eddi gian k\u1ebft th\u00fac t\u0103ng d\u1ea7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o t\u1eadp k\u1ebft qu\u1ea3 v\u1edbi ho\u1ea1t \u0111\u1ed9ng \u0111\u1ea7u ti\u00ean trong danh s\u00e1ch \u0111\u00e3 s\u1eafp x\u1ebfp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy\u1ec7t qua c\u00e1c ho\u1ea1t \u0111\u1ed9ng c\u00f2n l\u1ea1i:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">N\u1ebfu th\u1eddi gian b\u1eaft \u0111\u1ea7u c\u1ee7a ho\u1ea1t \u0111\u1ed9ng hi\u1ec7n t\u1ea1i l\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng th\u1eddi gian k\u1ebft th\u00fac c\u1ee7a ho\u1ea1t \u0111\u1ed9ng cu\u1ed1i c\u00f9ng trong t\u1eadp k\u1ebft qu\u1ea3, th\u00ec th\u00eam ho\u1ea1t \u0111\u1ed9ng hi\u1ec7n t\u1ea1i v\u00e0o t\u1eadp k\u1ebft qu\u1ea3.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> activity_selection<\/span><span style=\"font-weight: 400;\">(activities)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># S\u1eafp x\u1ebfp ho\u1ea1t \u0111\u1ed9ng theo th\u1eddi gian k\u1ebft th\u00fac t\u0103ng d\u1ea7n<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 sorted_activities = sorted(activities, key=<\/span><span style=\"font-weight: 400;\">lambda<\/span><span style=\"font-weight: 400;\"> x: x[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">])<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"Danh s\u00e1ch ho\u1ea1t \u0111\u1ed9ng sau khi s\u1eafp x\u1ebfp (theo th\u1eddi gian k\u1ebft th\u00fac):\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> activity <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> sorted_activities:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">{activity[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{activity[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\"> - <\/span><span style=\"font-weight: 400;\">{activity[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u1ecdn ho\u1ea1t \u0111\u1ed9ng \u0111\u1ea7u ti\u00ean<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 selected_activities = [sorted_activities[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">]]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\\nCh\u1ecdn ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">{sorted_activities[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">][<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># Duy\u1ec7t qua c\u00e1c ho\u1ea1t \u0111\u1ed9ng c\u00f2n l\u1ea1i<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, len(sorted_activities)):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> sorted_activities[i][<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] &gt;= selected_activities[<\/span><span style=\"font-weight: 400;\">-1<\/span><span style=\"font-weight: 400;\">][<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 selected_activities.append(sorted_activities[i])<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ch\u1ecdn ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">{sorted_activities[i][<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\"># K\u1ebft qu\u1ea3<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">\"\\nT\u1eadp c\u00e1c ho\u1ea1t \u0111\u1ed9ng \u0111\u01b0\u1ee3c ch\u1ecdn:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> activity <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> selected_activities:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">{activity[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{activity[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\"> - <\/span><span style=\"font-weight: 400;\">{activity[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">activities = [<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\">),<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 (<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">16<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">activity_selection(activities)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Danh s\u00e1ch ho\u1ea1t \u0111\u1ed9ng sau khi s\u1eafp x\u1ebfp (theo th\u1eddi gian k\u1ebft th\u00fac):<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">16<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ch\u1ecdn ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ch\u1ecdn ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ch\u1ecdn ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ch\u1ecdn ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">T\u1eadp c\u00e1c ho\u1ea1t \u0111\u1ed9ng \u0111\u01b0\u1ee3c ch\u1ecdn:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Ho\u1ea1t \u0111\u1ed9ng <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\"> &#8211; <\/span><span style=\"font-weight: 400;\">16<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eafp x\u1ebfp ho\u1ea1t \u0111\u1ed9ng:<\/b><span style=\"font-weight: 400;\"> Ho\u1ea1t \u0111\u1ed9ng \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp theo th\u1eddi gian k\u1ebft th\u00fac t\u0103ng d\u1ea7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ch\u1ecdn ho\u1ea1t \u0111\u1ed9ng:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Ho\u1ea1t \u0111\u1ed9ng 1:<\/b><span style=\"font-weight: 400;\"> Th\u1eddi gian k\u1ebft th\u00fac s\u1edbm nh\u1ea5t, ch\u1ecdn \u0111\u1ea7u ti\u00ean.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Ho\u1ea1t \u0111\u1ed9ng 4:<\/b><span style=\"font-weight: 400;\"> B\u1eaft \u0111\u1ea7u l\u00fac <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, sau khi <\/span><b>Ho\u1ea1t \u0111\u1ed9ng 1<\/b><span style=\"font-weight: 400;\"> k\u1ebft th\u00fac (<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">), n\u00ean c\u00f3 th\u1ec3 ch\u1ecdn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Ho\u1ea1t \u0111\u1ed9ng 8:<\/b><span style=\"font-weight: 400;\"> B\u1eaft \u0111\u1ea7u l\u00fac <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">, sau khi <\/span><b>Ho\u1ea1t \u0111\u1ed9ng 4<\/b><span style=\"font-weight: 400;\"> k\u1ebft th\u00fac (<\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">), n\u00ean c\u00f3 th\u1ec3 ch\u1ecdn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><b>Ho\u1ea1t \u0111\u1ed9ng 11:<\/b><span style=\"font-weight: 400;\"> B\u1eaft \u0111\u1ea7u l\u00fac <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">, sau khi <\/span><b>Ho\u1ea1t \u0111\u1ed9ng 8<\/b><span style=\"font-weight: 400;\"> k\u1ebft th\u00fac (<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">), n\u00ean c\u00f3 th\u1ec3 ch\u1ecdn.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ebft qu\u1ea3:<\/b><span style=\"font-weight: 400;\"> Ch\u1ecdn \u0111\u01b0\u1ee3c <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"> ho\u1ea1t \u0111\u1ed9ng kh\u00f4ng tr\u00f9ng th\u1eddi gian.<\/span><\/li>\n<\/ul>\n<p><b>L\u01b0u \u00fd:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Thu\u1eadt to\u00e1n tham lam \u0111\u1ea3m b\u1ea3o k\u1ebft qu\u1ea3 t\u1ed1i \u01b0u cho b\u00e0i to\u00e1n n\u00e0y.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Vi\u1ec7c s\u1eafp x\u1ebfp theo th\u1eddi gian k\u1ebft th\u00fac gi\u00fap t\u1ed1i \u01b0u s\u1ed1 l\u01b0\u1ee3ng ho\u1ea1t \u0111\u1ed9ng \u0111\u01b0\u1ee3c ch\u1ecdn.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Xu_ly_Chuoi_String_Algorithm\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n X\u1eed l\u00fd Chu\u1ed7i (String Algorithm)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0KMP Algorithm (Thu\u1eadt to\u00e1n Knuth-Morris-Pratt)<\/b><\/h3>\n<p><b>Thu\u1eadt to\u00e1n KMP (Knuth-Morris-Pratt)<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t thu\u1eadt to\u00e1n hi\u1ec7u qu\u1ea3 \u0111\u1ec3 t\u00ecm ki\u1ebfm chu\u1ed7i m\u1eabu trong chu\u1ed7i v\u0103n b\u1ea3n. N\u00f3 s\u1eed d\u1ee5ng th\u00f4ng tin v\u1ec1 chu\u1ed7i m\u1eabu \u0111\u1ec3 tr\u00e1nh vi\u1ec7c so s\u00e1nh l\u1ea1i c\u00e1c k\u00fd t\u1ef1 \u0111\u00e3 bi\u1ebft l\u00e0 kh\u00f4ng kh\u1edbp, nh\u1edd v\u00e0o m\u1ea3ng l\u1ee1 (Failure Function ho\u1eb7c LPS &#8211; Longest Prefix Suffix).<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t chu\u1ed7i v\u0103n b\u1ea3n <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\"> v\u00e0 m\u1ed9t chu\u1ed7i m\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\">, h\u00e3y t\u00ecm t\u1ea5t c\u1ea3 c\u00e1c v\u1ecb tr\u00ed xu\u1ea5t hi\u1ec7n c\u1ee7a chu\u1ed7i m\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\"> trong chu\u1ed7i v\u0103n b\u1ea3n <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\"> s\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n KMP<\/b><span style=\"font-weight: 400;\">. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh x\u00e2y d\u1ef1ng m\u1ea3ng l\u1ee1 (Failure Function) v\u00e0 qu\u00e1 tr\u00ecnh t\u00ecm ki\u1ebfm.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">V\u0103n b\u1ea3n <\/span><span style=\"font-weight: 400;\">T = &#8220;ababcabcabababd&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u1eabu <\/span><span style=\"font-weight: 400;\">P = &#8220;ababd&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3: M\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\"> xu\u1ea5t hi\u1ec7n t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"> trong chu\u1ed7i <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\"> (v\u1ecb tr\u00ed t\u00ednh t\u1eeb <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>X\u00e2y d\u1ef1ng m\u1ea3ng LPS (Longest Prefix Suffix):<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">M\u1ea3ng LPS l\u01b0u tr\u1eef \u0111\u1ed9 d\u00e0i c\u1ee7a ti\u1ec1n t\u1ed1 d\u00e0i nh\u1ea5t c\u1ee7a m\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\"> c\u0169ng l\u00e0 h\u1eadu t\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">P[0..i]<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">M\u1ea3ng n\u00e0y gi\u00fap x\u00e1c \u0111\u1ecbnh s\u1ed1 k\u00fd t\u1ef1 c\u1ea7n b\u1ecf qua khi kh\u00f4ng kh\u1edbp trong qu\u00e1 tr\u00ecnh t\u00ecm ki\u1ebfm.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00ecm ki\u1ebfm m\u1eabu trong v\u0103n b\u1ea3n:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Duy\u1ec7t qua chu\u1ed7i v\u0103n b\u1ea3n <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\"> v\u00e0 so s\u00e1nh v\u1edbi chu\u1ed7i m\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Khi c\u00f3 k\u00fd t\u1ef1 kh\u00f4ng kh\u1edbp, s\u1eed d\u1ee5ng m\u1ea3ng LPS \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh v\u1ecb tr\u00ed ti\u1ebfp theo trong m\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ti\u1ebfp t\u1ee5c so s\u00e1nh, thay v\u00ec quay l\u1ea1i \u0111\u1ea7u m\u1eabu.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> compute_lps_array<\/span><span style=\"font-weight: 400;\">(pattern)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 length = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\"># \u0110\u1ed9 d\u00e0i c\u1ee7a LPS tr\u01b0\u1edbc \u0111\u00f3<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 lps = [<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] * len(pattern)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 i = <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"X\u00e2y d\u1ef1ng m\u1ea3ng LPS cho m\u1eabu '<\/span><span style=\"font-weight: 400;\">{pattern}<\/span><span style=\"font-weight: 400;\">':\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> i &lt; len(pattern):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> pattern[i] == pattern[length]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 length += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 lps[i] = length<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"LPS[<\/span><span style=\"font-weight: 400;\">{i}<\/span><span style=\"font-weight: 400;\">] = <\/span><span style=\"font-weight: 400;\">{length}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> length != <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 length = lps[length - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Kh\u00f4ng kh\u1edbp, c\u1eadp nh\u1eadt length = <\/span><span style=\"font-weight: 400;\">{length}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 lps[i] = <\/span><span style=\"font-weight: 400;\">0<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"LPS[<\/span><span style=\"font-weight: 400;\">{i}<\/span><span style=\"font-weight: 400;\">] = 0\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"M\u1ea3ng LPS: <\/span><span style=\"font-weight: 400;\">{lps}<\/span><span style=\"font-weight: 400;\">\\n\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> lps<\/span>\r\n\r\n<span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> kmp_search<\/span><span style=\"font-weight: 400;\">(text, pattern)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 m = len(pattern)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 n = len(text)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 lps = compute_lps_array(pattern)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 result = []<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 i = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u1ec9 s\u1ed1 cho text<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 j = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\"># Ch\u1ec9 s\u1ed1 cho pattern<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"T\u00ecm ki\u1ebfm m\u1eabu '<\/span><span style=\"font-weight: 400;\">{pattern}<\/span><span style=\"font-weight: 400;\">' trong v\u0103n b\u1ea3n '<\/span><span style=\"font-weight: 400;\">{text}<\/span><span style=\"font-weight: 400;\">':\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> i &lt; n:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> pattern[j] == text[i]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">{i}<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">{j}<\/span><span style=\"font-weight: 400;\">] (<\/span><span style=\"font-weight: 400;\">{text[i]}<\/span><span style=\"font-weight: 400;\">)\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 j += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> j == m:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"M\u1eabu xu\u1ea5t hi\u1ec7n t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">{i - j}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 result.append(i - j)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 j = lps[j - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\u0110\u1eb7t l\u1ea1i j = <\/span><span style=\"font-weight: 400;\">{j}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">elif<\/span><span style=\"font-weight: 400;\"> i &lt; n <\/span><span style=\"font-weight: 400;\">and<\/span><span style=\"font-weight: 400;\"> pattern[j] != text[i]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> j != <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Kh\u00f4ng kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">{i}<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">{j}<\/span><span style=\"font-weight: 400;\">] (<\/span><span style=\"font-weight: 400;\">{text[i]}<\/span><span style=\"font-weight: 400;\"> vs <\/span><span style=\"font-weight: 400;\">{pattern[j]}<\/span><span style=\"font-weight: 400;\">), \u0111\u1eb7t l\u1ea1i j = <\/span><span style=\"font-weight: 400;\">{lps[j - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 j = lps[j - <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">else<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"Kh\u00f4ng kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">{i}<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">{j}<\/span><span style=\"font-weight: 400;\">] (<\/span><span style=\"font-weight: 400;\">{text[i]}<\/span><span style=\"font-weight: 400;\"> vs <\/span><span style=\"font-weight: 400;\">{pattern[j]}<\/span><span style=\"font-weight: 400;\">), t\u0103ng i\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 i += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\\nC\u00e1c v\u1ecb tr\u00ed m\u1eabu xu\u1ea5t hi\u1ec7n: <\/span><span style=\"font-weight: 400;\">{result}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> result<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">text = <\/span><span style=\"font-weight: 400;\">\"ababcabcabababd\"<\/span>\r\n<span style=\"font-weight: 400;\">pattern = <\/span><span style=\"font-weight: 400;\">\"ababd\"<\/span>\r\n<span style=\"font-weight: 400;\">kmp_search(text, pattern)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">X\u00e2y d\u1ef1ng m\u1ea3ng LPS cho m\u1eabu <\/span><span style=\"font-weight: 400;\">&#8216;ababd&#8217;<\/span><span style=\"font-weight: 400;\">:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">LPS[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">LPS[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">] = <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">LPS[<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">] = <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp, c\u1eadp nh\u1eadt length = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">LPS[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">] = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">M\u1ea3ng LPS: [<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">]<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">T\u00ecm ki\u1ebfm m\u1eabu <\/span><span style=\"font-weight: 400;\">&#8216;ababd&#8217;<\/span><span style=\"font-weight: 400;\"> trong v\u0103n b\u1ea3n <\/span><span style=\"font-weight: 400;\">&#8216;ababcabcabababd&#8217;<\/span><span style=\"font-weight: 400;\">:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] (a)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] (b)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">] (a)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">] (b)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">] (c vs d), \u0111\u1eb7t l\u1ea1i j = <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">] (c vs a), \u0111\u1eb7t l\u1ea1i j = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] (c vs a), t\u0103ng i<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] (a)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] (b vs c), \u0111\u1eb7t l\u1ea1i j = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] (c vs a), t\u0103ng i<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">] (a)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">] (b)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">] (a)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">] (b)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">] (a)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u00f4ng kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">] (b vs d), \u0111\u1eb7t l\u1ea1i j = <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">] (b)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">Kh\u1edbp t\u1ea1i text[<\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">] v\u00e0 pattern[<\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">] (d)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">M\u1eabu xu\u1ea5t hi\u1ec7n t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u1eb7t l\u1ea1i j = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">C\u00e1c v\u1ecb tr\u00ed m\u1eabu xu\u1ea5t hi\u1ec7n: [<\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>X\u00e2y d\u1ef1ng m\u1ea3ng LPS:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">LPS[0] = 0<\/span><span style=\"font-weight: 400;\">: lu\u00f4n lu\u00f4n b\u1eb1ng 0.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">LPS[1] = 0<\/span><span style=\"font-weight: 400;\">: v\u00ec <\/span><span style=\"font-weight: 400;\">pattern[1] != pattern[0]<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">LPS[2] = 1<\/span><span style=\"font-weight: 400;\">: v\u00ec <\/span><span style=\"font-weight: 400;\">pattern[2] == pattern[0]<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">LPS[3] = 2<\/span><span style=\"font-weight: 400;\">: v\u00ec <\/span><span style=\"font-weight: 400;\">pattern[3] == pattern[1]<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">LPS[4] = 0<\/span><span style=\"font-weight: 400;\">: v\u00ec kh\u00f4ng c\u00f3 ti\u1ec1n t\u1ed1\/h\u1eadu t\u1ed1 tr\u00f9ng.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00ecm ki\u1ebfm:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">So s\u00e1nh c\u00e1c k\u00fd t\u1ef1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">text<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">pattern<\/span><span style=\"font-weight: 400;\">, s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">lps<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 quy\u1ebft \u0111\u1ecbnh s\u1ed1 b\u01b0\u1edbc nh\u1ea3y khi kh\u00f4ng kh\u1edbp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Khi t\u00ecm th\u1ea5y m\u1ed9t m\u1eabu ho\u00e0n ch\u1ec9nh (<\/span><span style=\"font-weight: 400;\">j == m<\/span><span style=\"font-weight: 400;\">), l\u01b0u v\u1ecb tr\u00ed v\u00e0 \u0111\u1eb7t l\u1ea1i <\/span><span style=\"font-weight: 400;\">j<\/span><span style=\"font-weight: 400;\"> theo <\/span><span style=\"font-weight: 400;\">lps<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><b>H\u00ecnh \u1ea3nh minh h\u1ecda:<\/b><\/p>\n<p>M\u1ea3ng LPS:<\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 s\u1ed1: \u00a0 <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">M\u1eabu P:\u00a0 \u00a0 a\u00a0 b\u00a0 a\u00a0 b\u00a0 d<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">LPS:\u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">\u00a0 <\/span><span style=\"font-weight: 400;\">0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Rabin-Karp Algorithm<\/b><\/h3>\n<p><b>Thu\u1eadt to\u00e1n Rabin-Karp<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t thu\u1eadt to\u00e1n t\u00ecm ki\u1ebfm chu\u1ed7i m\u1eabu trong chu\u1ed7i v\u0103n b\u1ea3n b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng h\u00e0m b\u0103m \u0111\u1ec3 so s\u00e1nh chu\u1ed7i. Thay v\u00ec so s\u00e1nh tr\u1ef1c ti\u1ebfp c\u00e1c chu\u1ed7i con, n\u00f3 so s\u00e1nh gi\u00e1 tr\u1ecb b\u0103m c\u1ee7a ch\u00fang, gi\u00fap t\u0103ng hi\u1ec7u su\u1ea5t.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t chu\u1ed7i v\u0103n b\u1ea3n <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\"> v\u00e0 m\u1ed9t chu\u1ed7i m\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\">, h\u00e3y t\u00ecm t\u1ea5t c\u1ea3 c\u00e1c v\u1ecb tr\u00ed xu\u1ea5t hi\u1ec7n c\u1ee7a chu\u1ed7i m\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\"> trong chu\u1ed7i v\u0103n b\u1ea3n <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\"> s\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n Rabin-Karp<\/b><span style=\"font-weight: 400;\">. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh t\u00ednh to\u00e1n gi\u00e1 tr\u1ecb b\u0103m (hash) v\u00e0 so s\u00e1nh.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">V\u0103n b\u1ea3n <\/span><span style=\"font-weight: 400;\">T = &#8220;GEEKS FOR GEEKS&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u1eabu <\/span><span style=\"font-weight: 400;\">P = &#8220;GEEK&#8221;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3: M\u1eabu <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\"> xu\u1ea5t hi\u1ec7n t\u1ea1i v\u1ecb tr\u00ed <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"> trong chu\u1ed7i <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00ednh gi\u00e1 tr\u1ecb b\u0103m c\u1ee7a m\u1eabu <\/b><b>P<\/b><b> v\u00e0 chu\u1ed7i con \u0111\u1ea7u ti\u00ean c\u1ee7a <\/b><b>T<\/b><b> c\u00f3 \u0111\u1ed9 d\u00e0i b\u1eb1ng <\/b><b>P<\/b><b>.<\/b><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Duy\u1ec7t qua chu\u1ed7i <\/b><b>T<\/b><b>:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">N\u1ebfu gi\u00e1 tr\u1ecb b\u0103m c\u1ee7a chu\u1ed7i con hi\u1ec7n t\u1ea1i tr\u00f9ng v\u1edbi gi\u00e1 tr\u1ecb b\u0103m c\u1ee7a <\/span><span style=\"font-weight: 400;\">P<\/span><span style=\"font-weight: 400;\">, ti\u1ebfn h\u00e0nh so s\u00e1nh k\u00fd t\u1ef1 t\u1eebng v\u1ecb tr\u00ed \u0111\u1ec3 x\u00e1c nh\u1eadn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">T\u00ednh gi\u00e1 tr\u1ecb b\u0103m cho chu\u1ed7i con ti\u1ebfp theo b\u1eb1ng c\u00e1ch c\u1eadp nh\u1eadt t\u1eeb gi\u00e1 tr\u1ecb b\u0103m tr\u01b0\u1edbc \u0111\u00f3 (rolling hash).<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre>def rabin_karp_search(pattern, text, d=256, q=101):\r\n\u00a0 \u00a0 m = len(pattern)\r\n\u00a0 \u00a0 n = len(text)\r\n\u00a0 \u00a0 h = pow(d, m-1) % q\u00a0 # Gi\u00e1 tr\u1ecb h = d^(m-1) % q\r\n\u00a0 \u00a0 p = 0\u00a0 # Gi\u00e1 tr\u1ecb hash cho pattern\r\n\u00a0 \u00a0 t = 0\u00a0 # Gi\u00e1 tr\u1ecb hash cho text\r\n\u00a0 \u00a0 result = []\r\n\r\n\u00a0 \u00a0 print(f\"V\u0103n b\u1ea3n: '{text}'\")\r\n\u00a0 \u00a0 print(f\"M\u1eabu: '{pattern}'\")\r\n\u00a0 \u00a0 print(f\"Gi\u00e1 tr\u1ecb d: {d}, Gi\u00e1 tr\u1ecb q (s\u1ed1 nguy\u00ean t\u1ed1): {q}\")\r\n\r\n\u00a0 \u00a0 # T\u00ednh gi\u00e1 tr\u1ecb hash ban \u0111\u1ea7u cho pattern v\u00e0 text\r\n\u00a0 \u00a0 for i in range(m):\r\n\u00a0 \u00a0 \u00a0 \u00a0 p = (d * p + ord(pattern[i])) % q\r\n\u00a0 \u00a0 \u00a0 \u00a0 t = (d * t + ord(text[i])) % q\r\n\u00a0 \u00a0 print(f\"Gi\u00e1 tr\u1ecb hash ban \u0111\u1ea7u c\u1ee7a pattern: {p}\")\r\n\u00a0 \u00a0 print(f\"Gi\u00e1 tr\u1ecb hash ban \u0111\u1ea7u c\u1ee7a text: {t}\\n\")\r\n\r\n\u00a0 \u00a0 # Duy\u1ec7t qua v\u0103n b\u1ea3n\r\n\u00a0 \u00a0 for s in range(n - m + 1):\r\n\u00a0 \u00a0 \u00a0 \u00a0 if p == t:\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(f\"Hash kh\u1edbp t\u1ea1i v\u1ecb tr\u00ed {s}. Ki\u1ec3m tra chu\u1ed7i con...\")\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # So s\u00e1nh t\u1eebng k\u00fd t\u1ef1\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if text[s:s+m] == pattern:\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(f\"M\u1eabu xu\u1ea5t hi\u1ec7n t\u1ea1i v\u1ecb tr\u00ed {s}\")\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 result.append(s)\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 else:\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(\"Chu\u1ed7i con kh\u00f4ng kh\u1edbp d\u00f9 hash kh\u1edbp.\")\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 if s &lt; n - m:\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 t = (d * (t - ord(text[s]) * h) + ord(text[s + m])) % q\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # \u0110\u1ea3m b\u1ea3o t &gt;= 0\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if t &lt; 0:\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 t += q\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(f\"C\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed {s+1}: {t}\")\r\n\r\n\u00a0 \u00a0 print(f\"\\nC\u00e1c v\u1ecb tr\u00ed m\u1eabu xu\u1ea5t hi\u1ec7n: {result}\")\r\n\u00a0 \u00a0 return result\r\n\r\n# V\u00ed d\u1ee5\r\ntext = \"GEEKS FOR GEEKS\"\r\npattern = \"GEEK\"\r\nrabin_karp_search(pattern, text)<\/pre>\n<p><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">V\u0103n b\u1ea3n: &#8216;GEEKS FOR GEEKS&#8217;<br \/>\nM\u1eabu: &#8216;GEEK&#8217;<br \/>\nGi\u00e1 tr\u1ecb d: 256, Gi\u00e1 tr\u1ecb q (s\u1ed1 nguy\u00ean t\u1ed1): 101<br \/>\nGi\u00e1 tr\u1ecb hash ban \u0111\u1ea7u c\u1ee7a pattern: 98<br \/>\nGi\u00e1 tr\u1ecb hash ban \u0111\u1ea7u c\u1ee7a text: 98Hash kh\u1edbp t\u1ea1i v\u1ecb tr\u00ed 0. Ki\u1ec3m tra chu\u1ed7i con&#8230;<br \/>\nM\u1eabu xu\u1ea5t hi\u1ec7n t\u1ea1i v\u1ecb tr\u00ed 0<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 1: 50<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 2: 81<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 3: 38<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 4: 32<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 5: 54<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 6: 18<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 7: 61<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 8: 13<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 9: 4<br \/>\nC\u1eadp nh\u1eadt hash t\u1ea1i v\u1ecb tr\u00ed 10: 98<br \/>\nHash kh\u1edbp t\u1ea1i v\u1ecb tr\u00ed 10. Ki\u1ec3m tra chu\u1ed7i con&#8230;<br \/>\nM\u1eabu xu\u1ea5t hi\u1ec7n t\u1ea1i v\u1ecb tr\u00ed 10C\u00e1c v\u1ecb tr\u00ed m\u1eabu xu\u1ea5t hi\u1ec7n: [0, 10]<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00ednh gi\u00e1 tr\u1ecb b\u0103m ban \u0111\u1ea7u:<\/b><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng gi\u00e1 tr\u1ecb ASCII c\u1ee7a c\u00e1c k\u00fd t\u1ef1 v\u00e0 c\u00f4ng th\u1ee9c:<\/span><\/p>\n<pre>hash = (d * hash + ord(char)) % q<\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Qu\u00e1 tr\u00ecnh t\u00ecm ki\u1ebfm:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">N\u1ebfu gi\u00e1 tr\u1ecb hash tr\u00f9ng kh\u1edbp, ti\u1ebfn h\u00e0nh so s\u00e1nh chu\u1ed7i con.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng rolling hash \u0111\u1ec3 c\u1eadp nh\u1eadt gi\u00e1 tr\u1ecb hash cho chu\u1ed7i con ti\u1ebfp theo:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">t = (d * (t - ord(text[s]) * h) + ord(text[s + m])) % q<\/span><\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">\u0110\u1ea3m b\u1ea3o gi\u00e1 tr\u1ecb hash lu\u00f4n d\u01b0\u01a1ng b\u1eb1ng c\u00e1ch th\u00eam <\/span><span style=\"font-weight: 400;\">q<\/span><span style=\"font-weight: 400;\"> n\u1ebfu c\u1ea7n.<\/span><\/li>\n<\/ul>\n<p><b>L\u01b0u \u00fd v\u1ec1 c\u00e1c tham s\u1ed1:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">d<\/span><span style=\"font-weight: 400;\">: S\u1ed1 l\u01b0\u1ee3ng k\u00fd t\u1ef1 trong b\u1ea3ng ch\u1eef c\u00e1i (256 cho ASCII).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">q<\/span><span style=\"font-weight: 400;\">: S\u1ed1 nguy\u00ean t\u1ed1 l\u1edbn \u0111\u1ec3 gi\u1ea3m xung \u0111\u1ed9t hash.<\/span><\/li>\n<\/ul>\n<h3><span style=\"font-weight: 400;\"><strong>So s\u00e1nh hai thu\u1eadt to\u00e1n<\/strong>:<\/span><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Hi\u1ec7u su\u1ea5t:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">KMP c\u00f3 \u0111\u1ed9 ph\u1ee9c t\u1ea1p O(n), \u1ed5n \u0111\u1ecbnh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Rabin-Karp c\u00f3 \u0111\u1ed9 ph\u1ee9c t\u1ea1p trung b\u00ecnh O(n), nh\u01b0ng tr\u01b0\u1eddng h\u1ee3p x\u1ea5u nh\u1ea5t l\u00e0 O(nm) do xung \u0111\u1ed9t hash.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u01afu \u0111i\u1ec3m:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">KMP: T\u1ed1t cho vi\u1ec7c t\u00ecm ki\u1ebfm m\u1ed9t m\u1eabu trong v\u0103n b\u1ea3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Rabin-Karp: T\u1ed1t cho vi\u1ec7c t\u00ecm ki\u1ebfm nhi\u1ec1u m\u1eabu ho\u1eb7c m\u1eabu c\u00f3 \u0111\u1ed9 d\u00e0i l\u1edbn.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Python_ve_Thuat_toan_Toan_hoc_Mathematical_Algorithm\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 Thu\u1eadt to\u00e1n To\u00e1n h\u1ecdc (Mathematical Algorithm)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0Thu\u1eadt to\u00e1n Euclid (Euclidean Algorithm)<\/b><\/h3>\n<p><b>Thu\u1eadt to\u00e1n Euclid<\/b><span style=\"font-weight: 400;\"> l\u00e0 ph\u01b0\u01a1ng ph\u00e1p hi\u1ec7u qu\u1ea3 \u0111\u1ec3 t\u00ecm \u01b0\u1edbc s\u1ed1 chung l\u1edbn nh\u1ea5t c\u1ee7a hai s\u1ed1 nguy\u00ean d\u01b0\u01a1ng. Nguy\u00ean l\u00fd c\u01a1 b\u1ea3n d\u1ef1a tr\u00ean vi\u1ec7c li\u00ean t\u1ee5c thay th\u1ebf s\u1ed1 l\u1edbn h\u01a1n b\u1eb1ng ph\u1ea7n d\u01b0 c\u1ee7a ph\u00e9p chia s\u1ed1 l\u1edbn cho s\u1ed1 nh\u1ecf cho \u0111\u1ebfn khi ph\u1ea7n d\u01b0 b\u1eb1ng <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">. Khi \u0111\u00f3, s\u1ed1 c\u00f2n l\u1ea1i ch\u00ednh l\u00e0 GCD.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho hai s\u1ed1 nguy\u00ean d\u01b0\u01a1ng <\/span><span style=\"font-weight: 400;\">a<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">b<\/span><span style=\"font-weight: 400;\">. H\u00e3y t\u00ecm <\/span><b>\u01af\u1edbc s\u1ed1 chung l\u1edbn nh\u1ea5t (GCD)<\/b><span style=\"font-weight: 400;\"> c\u1ee7a <\/span><span style=\"font-weight: 400;\">a<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">b<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n Euclid<\/b><span style=\"font-weight: 400;\">. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh t\u00ednh to\u00e1n t\u1eebng b\u01b0\u1edbc.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">a = 56<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">b = 98<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3: GCD c\u1ee7a <\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">98<\/span><span style=\"font-weight: 400;\"> l\u00e0 <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Trong khi <\/span><span style=\"font-weight: 400;\">b<\/span><span style=\"font-weight: 400;\"> kh\u00e1c <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, th\u1ef1c hi\u1ec7n:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">G\u00e1n <\/span><span style=\"font-weight: 400;\">a<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng <\/span><span style=\"font-weight: 400;\">b<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">b<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng <\/span><span style=\"font-weight: 400;\">a % b<\/span><span style=\"font-weight: 400;\"> (ph\u1ea7n d\u01b0 c\u1ee7a <\/span><span style=\"font-weight: 400;\">a<\/span><span style=\"font-weight: 400;\"> chia cho <\/span><span style=\"font-weight: 400;\">b<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi <\/span><span style=\"font-weight: 400;\">b<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">a<\/span><span style=\"font-weight: 400;\"> l\u00e0 GCD.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> euclidean_algorithm<\/span><span style=\"font-weight: 400;\">(a, b)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"T\u00ecm GCD c\u1ee7a <\/span><span style=\"font-weight: 400;\">{a}<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">{b}<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng Thu\u1eadt to\u00e1n Euclid:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 step = <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> b != <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">{step}<\/span><span style=\"font-weight: 400;\">: a = <\/span><span style=\"font-weight: 400;\">{a}<\/span><span style=\"font-weight: 400;\">, b = <\/span><span style=\"font-weight: 400;\">{b}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 a, b = b, a % b<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 step += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"GCD l\u00e0 <\/span><span style=\"font-weight: 400;\">{a}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> a<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">a = <\/span><span style=\"font-weight: 400;\">56<\/span>\r\n<span style=\"font-weight: 400;\">b = <\/span><span style=\"font-weight: 400;\">98<\/span>\r\n<span style=\"font-weight: 400;\">euclidean_algorithm(a, b)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">T\u00ecm GCD c\u1ee7a <\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">98<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng Thu\u1eadt to\u00e1n Euclid:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">: a = <\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\">, b = <\/span><span style=\"font-weight: 400;\">98<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">: a = <\/span><span style=\"font-weight: 400;\">98<\/span><span style=\"font-weight: 400;\">, b = <\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">: a = <\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\">, b = <\/span><span style=\"font-weight: 400;\">42<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">: a = <\/span><span style=\"font-weight: 400;\">42<\/span><span style=\"font-weight: 400;\">, b = <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">B\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">: a = <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\">, b = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">GCD l\u00e0 <\/span><span style=\"font-weight: 400;\">14<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 1:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">a = 56<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">b = 98<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">a % b = 56 % 98 = 56<\/span><span style=\"font-weight: 400;\"> (v\u00ec <\/span><span style=\"font-weight: 400;\">56 &lt; 98<\/span><span style=\"font-weight: 400;\">)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt <\/span><span style=\"font-weight: 400;\">a = 98<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">b = 56<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 2:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">a = 98<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">b = 56<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">a % b = 98 % 56 = 42<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt <\/span><span style=\"font-weight: 400;\">a = 56<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">b = 42<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 3:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">a = 56<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">b = 42<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">a % b = 56 % 42 = 14<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt <\/span><span style=\"font-weight: 400;\">a = 42<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">b = 14<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 4:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">a = 42<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">b = 14<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">a % b = 42 % 14 = 0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt <\/span><span style=\"font-weight: 400;\">a = 14<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">b = 0<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>K\u1ebft lu\u1eadn:<\/b>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Khi <\/span><span style=\"font-weight: 400;\">b = 0<\/span><span style=\"font-weight: 400;\">, GCD l\u00e0 <\/span><span style=\"font-weight: 400;\">a = 14<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><b>H\u00ecnh \u1ea3nh minh h\u1ecda:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">98<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u2193<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">98<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\"> (v\u00ec <\/span><span style=\"font-weight: 400;\">98<\/span><span style=\"font-weight: 400;\"> % <\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">42<\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u2193<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">42<\/span><span style=\"font-weight: 400;\"> (v\u00ec <\/span><span style=\"font-weight: 400;\">56<\/span><span style=\"font-weight: 400;\"> % <\/span><span style=\"font-weight: 400;\">42<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u2193<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">42<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\"> (v\u00ec <\/span><span style=\"font-weight: 400;\">42<\/span><span style=\"font-weight: 400;\"> % <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\"> = <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u2192 GCD l\u00e0 <\/span><span style=\"font-weight: 400;\">14<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u00e2u h\u1ecfi v\u1ec1\u00a0S\u00e0ng Eratosthenes (Sieve of Eratosthenes)<\/b><\/h3>\n<p><b>S\u00e0ng Eratosthenes<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t thu\u1eadt to\u00e1n c\u1ed5 \u0111i\u1ec3n v\u00e0 hi\u1ec7u qu\u1ea3 \u0111\u1ec3 t\u00ecm t\u1ea5t c\u1ea3 c\u00e1c s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng m\u1ed9t s\u1ed1 nguy\u00ean d\u01b0\u01a1ng cho tr\u01b0\u1edbc. Thu\u1eadt to\u00e1n ho\u1ea1t \u0111\u1ed9ng b\u1eb1ng c\u00e1ch lo\u1ea1i b\u1ecf c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a m\u1ed7i s\u1ed1 nguy\u00ean t\u1ed1 b\u1eaft \u0111\u1ea7u t\u1eeb <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>B\u00e0i to\u00e1n:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Cho m\u1ed9t s\u1ed1 nguy\u00ean d\u01b0\u01a1ng <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\">. H\u00e3y li\u1ec7t k\u00ea t\u1ea5t c\u1ea3 c\u00e1c s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <\/span><b>Thu\u1eadt to\u00e1n S\u00e0ng Eratosthenes<\/b><span style=\"font-weight: 400;\">. Hi\u1ec3n th\u1ecb qu\u00e1 tr\u00ecnh s\u00e0ng l\u1ecdc t\u1eebng b\u01b0\u1edbc.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">n = 30<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3: C\u00e1c s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng <\/span><span style=\"font-weight: 400;\">30<\/span><span style=\"font-weight: 400;\"> l\u00e0: <\/span><span style=\"font-weight: 400;\">2, 3, 5, 7, 11, 13, 17, 19, 23, 29<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><b>\u00dd t\u01b0\u1edfng:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ea1o m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 t\u1eeb <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1eaft \u0111\u1ea7u t\u1eeb <\/span><span style=\"font-weight: 400;\">p = 2<\/span><span style=\"font-weight: 400;\">, s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf nh\u1ea5t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u t\u1ea5t c\u1ea3 c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">p<\/span><span style=\"font-weight: 400;\"> (t\u1eeb <\/span><span style=\"font-weight: 400;\">p*p<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\">) l\u00e0 kh\u00f4ng ph\u1ea3i s\u1ed1 nguy\u00ean t\u1ed1.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ecm s\u1ed1 nguy\u00ean t\u1ed1 ti\u1ebfp theo ch\u01b0a \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u v\u00e0 l\u1eb7p l\u1ea1i b\u01b0\u1edbc 3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u1eb7p l\u1ea1i cho \u0111\u1ebfn khi <\/span><span style=\"font-weight: 400;\">p*p &gt; n<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400;\">def<\/span><span style=\"font-weight: 400;\"> sieve_of_eratosthenes<\/span><span style=\"font-weight: 400;\">(n)<\/span><span style=\"font-weight: 400;\">:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 prime = [<\/span><span style=\"font-weight: 400;\">True<\/span> <span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> _ <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(n+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">)]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 p = <\/span><span style=\"font-weight: 400;\">2<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"T\u00ecm c\u00e1c s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng <\/span><span style=\"font-weight: 400;\">{n}<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng S\u00e0ng Eratosthenes:\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">while<\/span><span style=\"font-weight: 400;\"> p * p &lt;= n:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> prime[p]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\\nS\u1ed1 <\/span><span style=\"font-weight: 400;\">{p}<\/span><span style=\"font-weight: 400;\"> l\u00e0 s\u1ed1 nguy\u00ean t\u1ed1, \u0111\u00e1nh d\u1ea5u c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">{p}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> i <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(p * p, n+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">, p):<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> prime[i]:<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">{i}<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 prime[i] = <\/span><span style=\"font-weight: 400;\">False<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 \u00a0 \u00a0 p += <\/span><span style=\"font-weight: 400;\">1<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 primes = [p <\/span><span style=\"font-weight: 400;\">for<\/span><span style=\"font-weight: 400;\"> p <\/span><span style=\"font-weight: 400;\">in<\/span><span style=\"font-weight: 400;\"> range(<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, n+<\/span><span style=\"font-weight: 400;\">1<\/span><span style=\"font-weight: 400;\">) <\/span><span style=\"font-weight: 400;\">if<\/span><span style=\"font-weight: 400;\"> prime[p]]<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 print(<\/span><span style=\"font-weight: 400;\">f\"\\nC\u00e1c s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng <\/span><span style=\"font-weight: 400;\">{n}<\/span><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">{primes}<\/span><span style=\"font-weight: 400;\">\"<\/span><span style=\"font-weight: 400;\">)<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0 \u00a0 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> primes<\/span>\r\n\r\n<span style=\"font-weight: 400;\"># V\u00ed d\u1ee5<\/span>\r\n<span style=\"font-weight: 400;\">n = <\/span><span style=\"font-weight: 400;\">30<\/span>\r\n<span style=\"font-weight: 400;\">sieve_of_eratosthenes(n)<\/span><\/pre>\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">T\u00ecm c\u00e1c s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng <\/span><span style=\"font-weight: 400;\">30<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng S\u00e0ng Eratosthenes:<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1ed1 <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"> l\u00e0 s\u1ed1 nguy\u00ean t\u1ed1, \u0111\u00e1nh d\u1ea5u c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">6<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">8<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">10<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">14<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">16<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">18<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">20<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">22<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">24<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">26<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">28<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">30<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1ed1 <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\"> l\u00e0 s\u1ed1 nguy\u00ean t\u1ed1, \u0111\u00e1nh d\u1ea5u c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">12<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">15<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">18<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">21<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">24<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">27<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">30<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">S\u1ed1 <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> l\u00e0 s\u1ed1 nguy\u00ean t\u1ed1, \u0111\u00e1nh d\u1ea5u c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">30<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng nguy\u00ean t\u1ed1<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><span style=\"font-weight: 400;\">C\u00e1c s\u1ed1 nguy\u00ean t\u1ed1 nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng <\/span><span style=\"font-weight: 400;\">30<\/span><span style=\"font-weight: 400;\">: [<\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">7<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">11<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">13<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">17<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">19<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">23<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">29<\/span><span style=\"font-weight: 400;\">]<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 1:<\/b><span style=\"font-weight: 400;\"> Kh\u1edfi t\u1ea1o danh s\u00e1ch <\/span><span style=\"font-weight: 400;\">prime[0..n]<\/span><span style=\"font-weight: 400;\"> v\u1edbi t\u1ea5t c\u1ea3 c\u00e1c gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">True<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 2:<\/b><span style=\"font-weight: 400;\"> B\u1eaft \u0111\u1ea7u v\u1edbi <\/span><span style=\"font-weight: 400;\">p = 2<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">2<\/span><span style=\"font-weight: 400;\"> (t\u1eeb <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\">) l\u00e0 <\/span><span style=\"font-weight: 400;\">False<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 3:<\/b><span style=\"font-weight: 400;\"> T\u0103ng <\/span><span style=\"font-weight: 400;\">p<\/span><span style=\"font-weight: 400;\"> l\u00ean <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">3<\/span><span style=\"font-weight: 400;\"> (t\u1eeb <\/span><span style=\"font-weight: 400;\">9<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\">) l\u00e0 <\/span><span style=\"font-weight: 400;\">False<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 4:<\/b><span style=\"font-weight: 400;\"> T\u0103ng <\/span><span style=\"font-weight: 400;\">p<\/span><span style=\"font-weight: 400;\"> l\u00ean <\/span><span style=\"font-weight: 400;\">4<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">prime[4]<\/span><span style=\"font-weight: 400;\"> l\u00e0 <\/span><span style=\"font-weight: 400;\">False<\/span><span style=\"font-weight: 400;\"> n\u00ean b\u1ecf qua.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 5:<\/b><span style=\"font-weight: 400;\"> T\u0103ng <\/span><span style=\"font-weight: 400;\">p<\/span><span style=\"font-weight: 400;\"> l\u00ean <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\">.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u c\u00e1c b\u1ed9i s\u1ed1 c\u1ee7a <\/span><span style=\"font-weight: 400;\">5<\/span><span style=\"font-weight: 400;\"> (t\u1eeb <\/span><span style=\"font-weight: 400;\">25<\/span><span style=\"font-weight: 400;\"> \u0111\u1ebfn <\/span><span style=\"font-weight: 400;\">n<\/span><span style=\"font-weight: 400;\">) l\u00e0 <\/span><span style=\"font-weight: 400;\">False<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u01b0\u1edbc 6:<\/b><span style=\"font-weight: 400;\"> Ti\u1ebfp t\u1ee5c cho \u0111\u1ebfn khi <\/span><span style=\"font-weight: 400;\">p * p &gt; n<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<p><b>H\u00ecnh \u1ea3nh minh h\u1ecda:<\/b><\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">[2, 3, 4, 5, 6, 7, 8, 9, 10, &#8230;, 30]<\/p>\n<p>&#8211; B\u1eaft \u0111\u1ea7u v\u1edbi 2:<br \/>\n\u0110\u00e1nh d\u1ea5u 4, 6, 8, &#8230;, 30<\/p>\n<p>&#8211; Ti\u1ebfp t\u1ee5c v\u1edbi 3:<br \/>\n\u0110\u00e1nh d\u1ea5u 9, 12, 15, &#8230;, 30<\/p>\n<p>&#8211; Ti\u1ebfp t\u1ee5c v\u1edbi 5:<br \/>\n\u0110\u00e1nh d\u1ea5u 25, 30<\/p>\n<p>&#8211; K\u1ebft qu\u1ea3:<br \/>\nC\u00e1c s\u1ed1 kh\u00f4ng b\u1ecb \u0111\u00e1nh d\u1ea5u l\u00e0 s\u1ed1 nguy\u00ean t\u1ed1.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Cac_chu_de_pho_bien_khac_trong_cac_cau_hoi_phong_van_Python_Developer\"><\/span><span style=\"font-weight: 400;\"><strong>C\u00e1c ch\u1ee7 \u0111\u1ec1 ph\u1ed5 bi\u1ebfn kh\u00e1c trong c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python Developer<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i nh\u1eefng c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python chuy\u00ean m\u00f4n v\u1ec1 thu\u1eadt to\u00e1n v\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u, \u0111\u1ec3 v\u01b0\u1ee3t qua bu\u1ed5i ph\u1ecfng v\u1ea5n Python Developer, \u1ee9ng vi\u00ean c\u1ea7n n\u1eafm v\u1eefng c\u00e1c ch\u1ee7 \u0111\u1ec1 d\u01b0\u1edbi \u0111\u00e2y:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\"><strong>Ki\u1ebfn th\u1ee9c n\u1ec1n t\u1ea3ng v\u1ec1 Python<\/strong>:<\/span><span style=\"font-weight: 400;\"> C\u00e1c ki\u1ebfn th\u1ee9c c\u01a1 b\u1ea3n nh\u01b0 c\u00fa ph\u00e1p, x\u1eed l\u00fd file, v\u00e0 c\u00e1c lo\u1ea1i d\u1eef li\u1ec7u ph\u1ed5 bi\u1ebfn nh\u01b0 list, tuple, set, dictionary,&#8230;<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>C\u00e1c kh\u00e1i ni\u1ec7m OOP (Object-Oriented Programming) trong Python<\/strong>: <\/span><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng c\u00e1c class, inheritance, polymorphism, encapsulation trong l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>X\u1eed l\u00fd d\u1eef li\u1ec7u v\u00e0 c\u00e1c th\u01b0 vi\u1ec7n Python ph\u1ed5 bi\u1ebfn<\/strong>: <\/span><span style=\"font-weight: 400;\">Th\u01b0 vi\u1ec7n nh\u01b0 Pandas, NumPy \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u, t\u1eeb vi\u1ec7c \u0111\u1ecdc file CSV \u0111\u1ebfn c\u00e1c ph\u00e9p t\u00ednh ma tr\u1eadn ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>Frameworks ph\u1ed5 bi\u1ebfn nh\u01b0 Django, Flask<\/strong>: <\/span><span style=\"font-weight: 400;\">Django v\u00e0 Flask l\u00e0 hai framework ph\u1ed5 bi\u1ebfn cho ph\u00e1t tri\u1ec3n web b\u1eb1ng Python, c\u1ea7n hi\u1ec3u r\u00f5 v\u1ec1 routing, middleware, v\u00e0 qu\u1ea3n l\u00fd database.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>Gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1 v\u00e0 t\u1ed1i \u01b0u h\u00f3a m\u00e3 ngu\u1ed3n Python<\/strong>: <\/span><span style=\"font-weight: 400;\">Kh\u1ea3 n\u0103ng t\u1ed1i \u01b0u h\u00f3a m\u00e3 ngu\u1ed3n l\u00e0 y\u00eau c\u1ea7u quan tr\u1ecdng, \u0111\u1eb7c bi\u1ec7t khi ph\u1ea3i l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c \u1ee9ng d\u1ee5ng l\u1edbn ho\u1eb7c c\u00f3 y\u00eau c\u1ea7u v\u1ec1 t\u1ed1c \u0111\u1ed9.<\/span><\/li>\n<\/ul>\n<p>B\u1ea1n c\u00f3 th\u1ec3 &#8220;\u00f4n t\u1eadp&#8221; c\u00e1c ch\u1ee7 \u0111\u1ec1 tr\u00ean qua c\u00e1c b\u00e0i vi\u1ebft sau:<\/p>\n<ul>\n<li><a href=\"https:\/\/itviec.com\/blog\/code-python-co-ban\/\" target=\"_blank\" rel=\"noopener\">Code Python c\u01a1 b\u1ea3n: H\u01b0\u1edbng d\u1eabn chi ti\u1ebft c\u00e1c l\u1ec7nh Python c\u01a1 b\u1ea3n<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-oop-phan-1\/\" target=\"_blank\" rel=\"noopener\">Top 50+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n OOP v\u00e0 tr\u1ea3 l\u1eddi m\u1edbi nh\u1ea5t Ph\u1ea7n 1<\/a> v\u00e0 <a href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-oop-phan-2\/\" target=\"_blank\" rel=\"noopener\">Ph\u1ea7n 2<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/lap-trinh-web-voi-python\/\" target=\"_blank\" rel=\"noopener\">C\u00e1ch t\u1ed1i \u01b0u hi\u1ec7u su\u1ea5t khi l\u1eadp tr\u00ecnh web v\u1edbi Python<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/ung-dung-python\/\" target=\"_blank\" rel=\"noopener\">\u1ee8ng d\u1ee5ng Python v\u00e0 c\u00e1c Python framework \u00e1p d\u1ee5ng trong t\u1eebng \u1ee9ng d\u1ee5ng<\/a><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_cau_hoi_phong_van_Python\"><\/span><strong>T\u1ed5ng k\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">V\u1eady l\u00e0 ch\u00fang ta \u0111\u00e3 \u0111i qua c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python quan tr\u1ecdng th\u01b0\u1eddng xu\u1ea5t hi\u1ec7n trong c\u00e1c bu\u1ed5i ph\u1ecfng v\u1ea5n Python Developer, t\u1eeb c\u00e1c thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp v\u00e0 t\u00ecm ki\u1ebfm \u0111\u1ebfn c\u1ea5u tr\u00fac d\u1eef li\u1ec7u c\u01a1 b\u1ea3n v\u00e0 c\u00e1c thu\u1eadt to\u00e1n n\u00e2ng cao.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c chu\u1ea9n b\u1ecb k\u1ef9 l\u01b0\u1ee1ng cho ph\u1ecfng v\u1ea5n v\u1ecb tr\u00ed Python Developer kh\u00f4ng ch\u1ec9 gi\u00fap b\u1ea1n t\u1ef1 tin \u0111\u1ed1i m\u1eb7t v\u1edbi c\u00e1c c\u00e2u h\u1ecfi, m\u00e0 c\u00f2n r\u00e8n luy\u1ec7n kh\u1ea3 n\u0103ng gi\u1ea3i th\u00edch nh\u1eefng kh\u00e1i ni\u1ec7m ph\u1ee9c t\u1ea1p m\u1ed9t c\u00e1ch d\u1ec5 hi\u1ec3u. H\u00e3y \u0111\u1ea7u t\u01b0 th\u1eddi gian \u0111\u1ec3 nghi\u00ean c\u1ee9u, th\u1ef1c h\u00e0nh v\u00e0 n\u00e2ng cao k\u1ef9 n\u0103ng c\u1ee7a m\u00ecnh, qua \u0111\u00f3 t\u0103ng c\u01a1 h\u1ed9i th\u00e0nh c\u00f4ng trong bu\u1ed5i ph\u1ecfng v\u1ea5n.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0110\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c th\u00e0nh c\u00f4ng trong bu\u1ed5i ph\u1ecfng v\u1ea5n cho v\u1ecb tr\u00ed Python Developer, \u1ee9ng vi\u00ean kh\u00f4ng ch\u1ec9 c\u1ea7n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c n\u1ec1n t\u1ea3ng m\u00e0 c\u00f2n ph\u1ea3i \u0111\u1ed1i m\u1eb7t v\u1edbi nh\u1eefng c\u00e2u h\u1ecfi chuy\u00ean s\u00e2u, \u0111\u1eb7c bi\u1ec7t v\u1ec1 thu\u1eadt to\u00e1n v\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u. Sau \u0111\u00e2y l\u00e0 t\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng [&hellip;]<\/p>\n","protected":false},"author":222,"featured_media":81963,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-81416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>T\u1ed5ng h\u1ee3p 25+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn - ITviec Blog<\/title>\n<meta name=\"description\" content=\"T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python t\u1eadp trung v\u00e0o c\u00e1c c\u00e2u h\u1ecfi thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn, k\u00e8m theo c\u00e2u tr\u1ea3 l\u1eddi c\u1ef1c k\u1ef3 chi ti\u1ebft (c\u00f3 code).\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"T\u1ed5ng h\u1ee3p 25+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn\" \/>\n<meta property=\"og:description\" content=\"\u0110\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c th\u00e0nh c\u00f4ng trong bu\u1ed5i ph\u1ecfng v\u1ea5n cho v\u1ecb tr\u00ed Python Developer, \u1ee9ng vi\u00ean kh\u00f4ng ch\u1ec9 c\u1ea7n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c n\u1ec1n t\u1ea3ng m\u00e0 c\u00f2n ph\u1ea3i \u0111\u1ed1i m\u1eb7t v\u1edbi nh\u1eefng\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-25T09:00:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Python-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"790\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Nguy\u1ec5n H\u1eefu V\u0103n\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nguy\u1ec5n H\u1eefu V\u0103n\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"54 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"T\u1ed5ng h\u1ee3p 25+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn - ITviec Blog","description":"T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python t\u1eadp trung v\u00e0o c\u00e1c c\u00e2u h\u1ecfi thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn, k\u00e8m theo c\u00e2u tr\u1ea3 l\u1eddi c\u1ef1c k\u1ef3 chi ti\u1ebft (c\u00f3 code).","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/","og_locale":"vi_VN","og_type":"article","og_title":"T\u1ed5ng h\u1ee3p 25+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn","og_description":"\u0110\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c th\u00e0nh c\u00f4ng trong bu\u1ed5i ph\u1ecfng v\u1ea5n cho v\u1ecb tr\u00ed Python Developer, \u1ee9ng vi\u00ean kh\u00f4ng ch\u1ec9 c\u1ea7n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c n\u1ec1n t\u1ea3ng m\u00e0 c\u00f2n ph\u1ea3i \u0111\u1ed1i m\u1eb7t v\u1edbi nh\u1eefng","og_url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-11-25T09:00:36+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Python-vippro.jpg","type":"image\/jpeg"}],"author":"Nguy\u1ec5n H\u1eefu V\u0103n","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"Nguy\u1ec5n H\u1eefu V\u0103n","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"54 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/"},"author":{"name":"Nguy\u1ec5n H\u1eefu V\u0103n","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/a77cc13f89eaa58f59d8772448febe5f"},"headline":"T\u1ed5ng h\u1ee3p 25+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn","datePublished":"2024-11-25T09:00:36+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/"},"wordCount":13256,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Python-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/","url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/","name":"T\u1ed5ng h\u1ee3p 25+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Python-vippro.jpg","datePublished":"2024-11-25T09:00:36+00:00","description":"T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python t\u1eadp trung v\u00e0o c\u00e1c c\u00e2u h\u1ecfi thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn, k\u00e8m theo c\u00e2u tr\u1ea3 l\u1eddi c\u1ef1c k\u1ef3 chi ti\u1ebft (c\u00f3 code).","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Python-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Python-vippro.jpg","width":1500,"height":790,"caption":"c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"T\u1ed5ng h\u1ee3p 25+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Python v\u1ec1 thu\u1eadt to\u00e1n ph\u1ed5 bi\u1ebfn"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/a77cc13f89eaa58f59d8772448febe5f","name":"Nguy\u1ec5n H\u1eefu V\u0103n","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/TR-Nguyen-Huu-Van-vippro-e1712136004193-100x100.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/TR-Nguyen-Huu-Van-vippro-e1712136004193-100x100.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/TR-Nguyen-Huu-Van-vippro-e1712136004193-100x100.jpg","caption":"Nguy\u1ec5n H\u1eefu V\u0103n"},"url":"https:\/\/itviec.com\/blog\/author\/nguyen-huu-van-2\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/81416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/222"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=81416"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/81416\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/81963"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=81416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=81416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=81416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}