{"id":34366,"date":"2024-11-01T09:27:21","date_gmt":"2024-11-01T09:27:21","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34366"},"modified":"2024-11-01T10:57:30","modified_gmt":"2024-11-01T10:57:30","slug":"c-coding-test-course-finding-the-minimum-number-of-multiplications-for-matrix-multiplication","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34366\/","title":{"rendered":"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n    There is a problem of finding the minimum number of operations required to multiply a given set of matrices.<br \/>\n    If matrix A has a size of <code>pq<\/code> and matrix B has a size of <code>qr<\/code>, the number of operations required to multiply these two matrices is <code>p * q * r<\/code>.<br \/>\n    We need to determine the order of matrix multiplication to minimize the total number of multiplication operations.\n<\/p>\n<h2>Problem Definition<\/h2>\n<p>\n    Given <code>n<\/code> matrices with sizes as follows, find the minimum number of multiplication operations needed to multiply these matrices.\n<\/p>\n<h3>Input<\/h3>\n<ul>\n<li>The first line contains the number of matrices <code>n<\/code> (1 \u2264 n \u2264 100).<\/li>\n<li>The next <code>n<\/code> lines contain the size <code>p_i \u00d7 q_i<\/code> of each matrix.<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>\n    Print the minimum number of operations required to multiply the matrices.\n<\/p>\n<h2>Example<\/h2>\n<div class=\"example\">\n<strong>Input:<\/strong><\/p>\n<pre>\n    3\n    10 20\n    20 30\n    30 10\n    <\/pre>\n<\/div>\n<div class=\"example\">\n<strong>Output:<\/strong><\/p>\n<pre>\n    3000\n    <\/pre>\n<\/div>\n<h2>Solution Process<\/h2>\n<h3>1. Understanding the Basic Concept<\/h3>\n<p>\n    Matrix multiplication is determined by the dimensions of the two matrices. Multiplying matrix A (p\u00d7q) and matrix B (q\u00d7r) results in matrix C (p\u00d7r).<br \/>\n    The number of operations needed is <code>p * q * r<\/code>. If multiple matrices are provided, it is important to find the optimal multiplication order.\n<\/p>\n<h3>2. Utilizing Dynamic Programming<\/h3>\n<p>\n    This problem can be solved using dynamic programming. We can construct a DP table to determine the optimal order of matrix multiplication.<br \/>\n    Let <code>dp[i][j]<\/code> represent the minimum number of operations required to multiply from the <code>i<\/code>th matrix to the <code>j<\/code>th matrix.\n<\/p>\n<h3>3. Filling the DP Table<\/h3>\n<p>\n    1. <code>dp[i][i] = 0<\/code> (No operations are needed when multiplying a matrix with itself.)<br \/>\n    2. When multiplying two or more matrices, divide the calculation based on the intermediate matrix.<br \/>\n    3. The order of matrix multiplication is determined as follows.\n<\/p>\n<pre>\nfor length from 2 to n:\n    for i from 1 to n-length+1:\n        j = i + length - 1\n        dp[i][j] = \u221e\n        for k from i to j-1:\n            cost = dp[i][k] + dp[k+1][j] + dimensions[i-1]*dimensions[k]*dimensions[j]\n            dp[i][j] = min(dp[i][j], cost)\n<\/pre>\n<h3>4. Python Implementation and C++ Code<\/h3>\n<p>\n    Let&#8217;s implement the above algorithm in C++.\n<\/p>\n<pre>\n#include <iostream>\n#include <vector>\n#include <limits.h>\n\nusing namespace std;\n\nint main() {\n    int n;\n    cin &gt;&gt; n;\n    vector<int> dimensions(n + 1);\n    for (int i = 0; i &lt; n; i++) {\n        int p, q;\n        cin &gt;&gt; p &gt;&gt; q;\n        dimensions[i] = p;\n        if (i == n - 1) dimensions[i + 1] = q; \/\/ last matrix's number of columns\n    }\n\n    vector<vector<int>&gt; dp(n, vector<int>(n, 0));\n\n    for (int length = 2; length &lt;= n; length++) {\n        for (int i = 0; i &lt; n - length + 1; i++) {\n            int j = i + length - 1;\n            dp[i][j] = INT_MAX;\n            for (int k = i; k &lt; j; k++) {\n                int cost = dp[i][k] + dp[k + 1][j] + dimensions[i] * dimensions[k + 1] * dimensions[j + 1];\n                dp[i][j] = min(dp[i][j], cost);\n            }\n        }\n    }\n\n    cout &lt;&lt; dp[0][n - 1] &lt;&lt; endl;\n    return 0;\n}\n<\/int><\/vector<int><\/int><\/limits.h><\/vector><\/iostream><\/pre>\n<h2>5. Time Complexity<\/h2>\n<p>\n    The time complexity of this algorithm is O(n^3). This occurs because we need to consider all possible multiplication orders when there are n matrices.\n<\/p>\n<h2>6. Conclusion<\/h2>\n<p>\n    The problem of minimizing matrix multiplication operations can be efficiently solved using dynamic programming.<br \/>\n    This problem is very useful in developing algorithm problem-solving skills and is an important topic applicable in various fields.<br \/>\n    By implementing this problem in C++, you can gain a deeper understanding of the concepts of algorithms and enhance your abilities to apply them in actual coding tests.\n<\/p>\n<h2>7. References<\/h2>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Matrix_chain_multiplication\">Matrix Chain Multiplication &#8211; Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/matrix-chain-multiplication-dp-8\/\">Matrix Chain Multiplication &#8211; GeeksforGeeks<\/a><\/li>\n<li><a href=\"https:\/\/www.hackerrank.com\/challenges\/matrix-multiplication\/problem\">HackerRank &#8211; Matrix Multiplication<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description There is a problem of finding the minimum number of operations required to multiply a given set of matrices. If matrix A has a size of pq and matrix B has a size of qr, the number of operations required to multiply these two matrices is p * q * r. We need &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34366\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34366","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34366\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description There is a problem of finding the minimum number of operations required to multiply a given set of matrices. If matrix A has a size of pq and matrix B has a size of qr, the number of operations required to multiply these two matrices is p * q * r. We need &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34366\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:27:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:30+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34366\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34366\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication\",\"datePublished\":\"2024-11-01T09:27:21+00:00\",\"dateModified\":\"2024-11-01T10:57:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34366\/\"},\"wordCount\":380,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34366\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34366\/\",\"name\":\"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:27:21+00:00\",\"dateModified\":\"2024-11-01T10:57:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34366\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34366\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34366\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/34366\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description There is a problem of finding the minimum number of operations required to multiply a given set of matrices. If matrix A has a size of pq and matrix B has a size of qr, the number of operations required to multiply these two matrices is p * q * r. We need &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication\"","og_url":"https:\/\/atmokpo.com\/w\/34366\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:27:21+00:00","article_modified_time":"2024-11-01T10:57:30+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34366\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34366\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication","datePublished":"2024-11-01T09:27:21+00:00","dateModified":"2024-11-01T10:57:30+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34366\/"},"wordCount":380,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34366\/","url":"https:\/\/atmokpo.com\/w\/34366\/","name":"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:27:21+00:00","dateModified":"2024-11-01T10:57:30+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34366\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34366\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34366\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Finding the Minimum Number of Multiplications for Matrix Multiplication"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34366"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34366\/revisions"}],"predecessor-version":[{"id":34367,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34366\/revisions\/34367"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}