{"id":19405834,"url":"https://github.com/michaelwehar/fourcornersproblem","last_synced_at":"2026-02-02T04:09:13.926Z","repository":{"id":83330407,"uuid":"184497201","full_name":"MichaelWehar/FourCornersProblem","owner":"MichaelWehar","description":"Finding a rectangle whose four corners are 1's in a given Boolean matrix.","archived":false,"fork":false,"pushed_at":"2022-08-16T11:30:59.000Z","size":155,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-03T02:01:46.575Z","etag":null,"topics":["algorithms","boolean","java","matrices","problem","programming","python","rectangle","search","submatrix"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MichaelWehar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-02T00:00:26.000Z","updated_at":"2022-08-29T21:22:01.000Z","dependencies_parsed_at":"2023-03-01T10:46:15.531Z","dependency_job_id":null,"html_url":"https://github.com/MichaelWehar/FourCornersProblem","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichaelWehar%2FFourCornersProblem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichaelWehar%2FFourCornersProblem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichaelWehar%2FFourCornersProblem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MichaelWehar%2FFourCornersProblem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MichaelWehar","download_url":"https://codeload.github.com/MichaelWehar/FourCornersProblem/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250600614,"owners_count":21456995,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","boolean","java","matrices","problem","programming","python","rectangle","search","submatrix"],"created_at":"2024-11-10T11:39:43.197Z","updated_at":"2026-02-02T04:09:08.891Z","avatar_url":"https://github.com/MichaelWehar.png","language":"Python","readme":"# The Four Corners Problem\n\nThe *four corners problem* is a 2D pattern matching problem that has frequently appeared in online coding challenges and on interview practice websites.\n\nIt is related to several well studied problems such as [frequent itemset mining](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.210.7727) and [4-cycle](https://www.sciencedirect.com/science/article/pii/S0304020808730196) as well as problems related to [computational geometry](http://dspace.library.uu.nl/handle/1874/16599).\n\n## Problem Statement\n\nInput: A Boolean matrix M.\n\nQuestion: Does there exist a rectangle within M whose four corners are 1's?\n\nAnswer: True / False\n\n**Example Instance**: 5 x 8 matrix\n\n|        |  c1  |  c2  |  c3  |  c4  |  c5   |  c6  |  c7  |  c8   |\n|   ---  | ---  | ---  | ---  | ---  | ---   | ---  | ---  | ---   |\n| **r1** | 0    | 0    | 1    | 0    | 0     | 0    | 0    | 1     |\n| **r2** | 1    | 0    | 0    | 0    | **1** | 1    | 0    | **1** |\n| **r3** | 0    | 0    | 1    | 0    | 0     | 1    | 0    | 0     |\n| **r4** | 0    | 0    | 0    | 0    | **1** | 0    | 1    | **1** |\n| **r5** | 0    | 0    | 1    | 0    | 0     | 0    | 1    | 1     |\n\n**Example Solution**\n\nRows 2 \u0026 4, Cols 5 \u0026 8\n\n# Our Algorithm - Runtime O(m \\* n)\n\n**Informal Explanation**\n\nGiven an m by n Boolean matrix.\n\nFirst, we want the number of columns to be at most the number of rows. If this is not the case, then simply transpose the matrix and swap the values of m and n. Now, we have n \u003c= m.\n\nNext, create a set of pairs of column indexes. Initially, the set is empty.\n\nGo through entries in the matrix row by row. For each row, add all pairs of column indexes where there is a 1 to the set.\n\n*Note: A common inefficiency is to traverse through every pair of column indexes from each row.  Instead, we convert each row into a set containing all column indexes where there is a 1 so that we can efficiently traverse through pairs of 1's (skipping any pairs with 0).*\n\nIf we attempt to add a pair to the set that is already in the set, then we've found a rectangle whose corners are 1's.\n\nOtherwise, if we reach the end, then there is no rectangle whose corners are 1's.\n\n**Runtime Analysis**\n\nThis takes O(m \\* n + n^2) time because we go through each of the m \\* n entries at most once and we go through each of the (n choose 2) = O(n^2) pairs of column indexes at most once before finding a rectangle whose corners are 1's.\n\nSince n \u003c= m, this takes O(m \\* n) time.\n\n# Code\n\n- C++ code by Niteesh Kumar and Michael Wehar.\n\n- Java code by Michael Wehar (based on papers [1] and [2]).\n\n- Python code primary implementation by Ari Liloia and Michael Wehar (based on papers [1] and [2]).  Alternative implementation by Joseph Swernofsky.\n\n- Additional contributions by Yvo Meeres and Chen Xu.\n\n# License\n\n- MIT\n\n# Related Resources\n\n**The algorithm above is discussed in the following papers**\n\n- [1] F. Mráz, D. Prusa, and M. Wehar. Two-dimensional Pattern Matching against Basic Picture Languages. CIAA 2019.\n\n- [2] D. Prusa and M. Wehar. Complexity of Searching for 2 by 2 Submatrices in Boolean Matrices. DLT 2020.\n\n**The problem is discussed on the following coding websites**\n\n- https://www.geeksforgeeks.org/find-rectangle-binary-matrix-corners-1/\n\n- https://www.tutorialspoint.com/find-if-there-is-a-rectangle-in-binary-matrix-with-corners-as-1-in-cplusplus\n\n- https://www.interviewbit.com/problems/find-rectangle-in-binary-matrix/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaelwehar%2Ffourcornersproblem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichaelwehar%2Ffourcornersproblem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaelwehar%2Ffourcornersproblem/lists"}