C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Weighted LRU cache. Is there a name for this methodology?

By : DoodleNan
Date : November 20 2020, 03:01 PM
should help you out Say I have an app that's trying to relate a string with an int. There are many strings and I want to keep a list of the top N that have occurred. , I was looking for Least-Frequently Used
code :

Share :

## Combinations of weighted elements in a set where weighted sum equal to fixed integer (in python)

By : user3728308
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Looping through all n! permutations is much too expensive. Instead, generate all 2^n subsets.
code :
``````from itertools import chain, combinations

def weight(A):
return sum(weights[x] for x in A)

# Copied from example at http://docs.python.org/library/itertools.html
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in xrange(len(s) + 1))

[x for x in powerset({'A', 'B', 'C', 'D', 'E'}) if weight(x) == W]
``````
``````[('A', 'D'), ('C', 'B'), ('C', 'E'), ('A', 'B', 'E'), ('B', 'E', 'D')]
``````

## Efficently simulate rolling weighted dice (or traversing a weighted graph), with frequent updates

By : arpit pathak
Date : March 29 2020, 07:55 AM
it should still fix some issue I think you can do it with log(k) complexity where k is number of faces in the dice.
for one particular node let p1, p2, ..., pk be the relative probabilities. Let p1+p2,...,+pk = p.
code :
``````====Tree Example====
``````
``````          220
/       \
130         90
/   \      /    \
50    80    20    70
|    |     |      |
1    2     3      4
``````
``````==== Lazy update variation ====
``````

## Weighted, Undirected Adjacency List: Maximum Weighted Edge in a Single Cycle in O(n)

By : Yann
Date : March 29 2020, 07:55 AM
will be helpful for those in need You can return in the DFS which node was found in the cycle, then go back marking every node up in the DFS tree as part of the cycle (until the found node itself). Something like this:
code :
``````DFS(v):
mark v as visited
for edges (v, w) in E:
if w is not visited:
last_node = DFS(w)
if last_node != -1:
test (v, w) as maximum edge
if last_node != v:
return last_node
else:
return -1
else:
test (v, w) as maximum edge
return w
return -1
``````

## nw:weighted-path-to, nw:turtles-on-weighted-path-to and multiple equally weighted paths

By : user3572741
Date : March 29 2020, 07:55 AM
Does that help Good question! You can generate either list from the other, but I think turtle-path to link-path is easier:
code :
``````;; Construct the turtle path, putting the current turtle on the front:
let turtle-path fput self nw:turtles-on-weight-path-to turtle 3 "weight"

;; Iterate through pairs of turtles, getting the link connecting them
``````
``````reduce [ lput [[other-end] of ?2] of (last ?1) ?1 ] fput (list self) nw:weighted-path-to turtle 3 "weight"
``````
``````to-report add-link-to-turtle-path [ turtle-path next-link ]
let last-turtle last turtle-path
report lput [[other-end] of next-link] of last-turtle
end

;; turtle-procedure - Assumes the current turtle is the starting point of the path