Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3872826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:58:05+00:00 2026-05-19T21:58:05+00:00

A generalized diagonal in a NXN matrix is a selection of N cells, such

  • 0

A “generalized diagonal” in a NXN matrix is a selection of N cells, such that:

  1. Exactly one cell is selected from each row and from each column
  2. Every selected cell contains a non-zero value

I am looking for an algorithm to find a generalized diagonal in O(n^3). It would seem to me that the following dynamic programming algorithm is “good enough”, but I’m not sure how to analyze its complexity.

Set<Set<Integer>> failedCache = new HashSet<Set<Integer>>();

List<Integer> find(int[][] matrix, Set<Integer> used, int row) {
    int N = matrix.length;
    if (failedCache.contains(used))
        return null;

    if (row == N) return new ArrayList<Integer>();

    for (int col = 0; col < N; ++col) {
        if (matrix[row][col] == 0)
            continue;

        if (used.contains(col))
            continue;

        Set<Integer> newUsed = new HashSet<Integer>(used);
        newUsed.add(col);
        List<Integer> answer = find(matrix, newUsed, row + 1);
        if (answer != null) {
            answer.add(col);
            return answer;
        }
    }

    failedCache.add(used);
    return null;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T21:58:05+00:00Added an answer on May 19, 2026 at 9:58 pm

    The algorithm runs in worst-case exponential time because on the following matrix

     11111
     11111
     11111
     11111
     00000
    

    it will try about n! possible combinations.

    For polynomial time solution, create a bipartite graph using the matrix, and find perfect matching.

    For example, with matrix

     011
     101
     001
    

    you create the graph

     A    X
     B    Y
     C    Z
    

    with edges A->Y, A->Z, B->X, B->Z, C->Z.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize for asking such a generalized question, but it's something that can prove
Based on chapter 5 (Generalized Functors) from the book Modern C++ Design, I'm trying
This problem actually deals with roll-overs, I'll just generalized below as such: I have
The ISO definition of generalized markup states: Markup should be rigorous so that the
I have a generalized media player web app that I wish to be able
My generalized question is this: how do you write asynchronous code that is still
I recently generalized a type class away from a constraint MonadError GenError m to
What is a generalized way to create a bash script from another script. For
I'm trying to create a generalized HTML parser that works well on Blog Posts.
I have a date that is in a format called 'String(Generalized-Time)', see MSDN linked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.