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

  • Home
  • SEARCH
  • 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 896799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:47:13+00:00 2026-05-15T14:47:13+00:00

While optimizing performance of an app of mine, I ran across a huge performance

  • 0

While optimizing performance of an app of mine, I ran across a huge performance bottleneck in few lines of (Python) code.

I have N tokens. each token has a value assigned to it. Some of the tokens contradict (e.g. tokens 8 and 12 cannot “live together”). My job is to find the k-best token-groups. The value of a group of tokens is simply the sum of the values of the tokens in it.

Naïve algorithm (which I have implemented…):

  1. find all 2^N token-group permutations of the tokens
  2. Eliminate the token-groups that have contradictions in them
  3. Calculate the value of all remaining token-groups
  4. Sort token-groups by value
  5. Choose top K token-groups

Real world numbers – I need top 10 token groups from a group of 20 tokens (for which I calculated the 1,000,000 permutations (!)), narrowed down to 3500 non-contradicting token groups. This took 5 seconds on my laptop…

I’m sure I can optimize steps 1+2 somehow by generating just the non-contradicting token-groups.

I’m also pretty sure I can somehow magically find the best token-group in a single search and find a way to traverse the token-groups by diminishing value, thus finding just the 10-best I am looking for….

my actual code:

all_possibilities = sum((list(itertools.combinations(token_list, i)) for i in xrange(len(token_list)+1)), [])
all_possibilities = [list(option) for option in all_possibilities if self._no_contradiction(option)] 
all_possibilities = [(option, self._probability(option)) for option in all_possibilities]
all_possibilities.sort(key = lambda result: -result[1]) # sort by descending probability

Please help?

Tal.

  • 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-15T14:47:14+00:00Added an answer on May 15, 2026 at 2:47 pm

    A really simple way to get all the non-contradicting token-groups:

    #!/usr/bin/env python
    
    token_list = ['a', 'b', 'c']
    
    contradictions = {
        'a': set(['b']),
        'b': set(['a']),
        'c': set()
    }
    
    result = []
    
    while token_list:
        token = token_list.pop()
        new = [set([token])]
        for r in result:
            if token not in contradictions or not r & contradictions[token]:
                new.append(r | set([token]))
        result.extend(new)
    
    print result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a chart that displays financial data. Performance was good while I
While debugging and keep pressing F5, if the source code does not exist, eclipse
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
While refactoring my code base I found a piece of code which I'd like
While optimizing a function in PHP, I changed if(is_array($obj)) foreach($obj as $key=>$value { [snip]
A while ago I posted a message about optimizing a query in MySQL. I
What are some good tips and/or techniques for optimizing and improving the performance of
In the process of optimizing queries in my app I noticed something strange. In
I am currently in the process of optimizing a numerical analysis code. Within the
I've two pieces of code: A do while loop: do { errorflag=0; ... if(cond1)

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.