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 7643135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:22:17+00:00 2026-05-31T09:22:17+00:00

I have an array of pairs like this: [[a, b], [b, d], [a, c],

  • 0

I have an array of pairs like this:

[["a", "b"], ["b", "d"], ["a", "c"], ["e", "d"], ["a", "d"], ..., ["s", "f"]]
  1. What is an efficient way to check if the given array can express a partial ordering? That is, there is no “loop” in the given array like ["a", "b"], ["b", "c"], ["c", "a"].

  2. If it is confirmed that the array expresses a partial order, I want to normalize this by removing all of the pairs that can be derived by reflexivity or transitivity. For example, in the above, since there is ["a", "b"] and ["b", "d"], the pair ["a", "d"] is redundant, and should be removed.

The order between 1 and 2 does not matter. If 2 should be done before or within the process of 1, then, that is fine.

Preferably I want it in Ruby 1.9.3, but just pseudo-code will suffice.

  • 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-31T09:22:18+00:00Added an answer on May 31, 2026 at 9:22 am

    For the first part of the question, I came up with my own answer here with the help of an answer at a mathematics site.

    For the second part of the question, after following the suggestions given in the other answers, I implemented in Ruby (i) Floyd-Warshall algorithm to calculate the transitive closure, (ii) composition, and (iii) transitive reduction using the formula R^- = R – R \cdot R^+.

    module Digraph; module_function
        def vertices graph; graph.flatten(1).uniq end
        ## Floyd-Warshall algorithm
        def transitive_closure graph
            vs = vertices(graph)
            path = graph.inject({}){|path, e| path[e] = true; path}
            vs.each{|k| vs.each{|i| vs.each{|j| path[[i, j]] ||= true if path[[i, k]] && path[[k, j]]}}}
            path.keys
        end
        def compose graph1, graph2
            vs = (vertices(graph1) + vertices(graph2)).uniq
            path1 = graph1.inject({}){|path, e| path[e] = true; path}
            path2 = graph2.inject({}){|path, e| path[e] = true; path}
            path = {}
            vs.each{|k| vs.each{|i| vs.each{|j| path[[i, j]] ||= true if path1[[i, k]] && path2[[k, j]]}}}
            path.keys
        end
        def transitive_reduction graph
                graph - compose(graph, transitive_closure(graph))
        end
    end
    

    Usage examples:

    Digraph.transitive_closure([[1, 2], [2, 3], [3, 4]])
    #=> [[1, 2], [2, 3], [3, 4], [1, 3], [1, 4], [2, 4]]
    
    Digraph.compose([[1, 2], [2, 3]], [[2, 4], [3, 5]])
    #=> [[1, 4], [2, 5]]
    
    Digraph.transitive_reduction([[1, 2], [2, 3], [3, 4], [1, 3], [1, 4], [2, 4]])
    #=> [[1, 2], [2, 3], [3, 4]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that looks like this: Array([5258]=>5274, [5261]=>5281, [5264]=>5287, [5271]=>5289 ); I
I have an array of objects like this - [{name:admission[year],value:2011-12},{name:admission[class],value:Nursery}] How can i access
I have an array that looks something like this array( 'cars' => 10, 'trucks'
I have an array that looks like this: $array = Array ( [0] =>
I have a comment block that can look like this; /** * variable1: value
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
Have an array of chars like char members[255]. How can I empty it completely
Now I currently have an array that contains 120 pairs. For example: [[1, 12],
So we have a real-estate-related site that has controller/action pairs like homes/view, realtors/edit, and
Say I have an array of Strings declared like this: String[] strings = new

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.