How can I remove values from a ranking system if they are a positive value k apart?
<p>Suppose I have the following code:</p>

<pre><code>def compute_ranks(graph, k):
    d = .8 #dampening factor
    loops = 10
    ranks = {}
    npages = len(graph)
    for page in graph:
        ranks[page] = 1.0 / npages
    for c in range(0, loops):
        newranks = {}
        for page in graph:
            newrank = (1-d) / npages
            for node in graph:
                if page in graph[node]:
                    newrank = newrank + d * (ranks[node]/len(graph[node]))
            newranks[page] = newrank
        ranks = newranks
     return ranks
</code></pre>

<p>Alright so now suppose I want to not allow any items that can collude with each other. If I have an item dictionary </p>

<pre><code>g = {'a': ['a', 'b', 'c'], 'b':['a'], 'c':['d'], 'd':['a']}
</code></pre>

<p>For any path A==>B, I don't want to allow paths from B==>A that are at a distance at or below my number k.</p>

<p>For example if k = 0, then the only path I would not allow is A==>A.</p>

<p>However if k = 2, then I would not allow the links A==>A as before but also links such as D==>A, B==>A, or A==>C.</p>

<p>I know this is very confusing and a majority of my problem comes from not understanding exactly what this means. </p>

<p>Here's a transcript of the question:</p>

<pre><code># Question 2: Combatting Link Spam

# One of the problems with our page ranking system is pages can 
# collude with each other to improve their page ranks.  We consider 
# A-&gt;B a reciprocal link if there is a link path from B to A of length 
# equal to or below the collusion level, k.  The length of a link path 
# is the number of links which are taken to travel from one page to the 
# other.

# If k = 0, then a link from A to A is a reciprocal link for node A, 
# since no links needs to be taken to get from A to A.

# If k=1, B-&gt;A would count as a reciprocal link  if there is a link 
# A-&gt;B, which includes one link and so is of length 1. (it requires 
# two parties, A and B, to collude to increase each others page rank).

# If k=2, B-&gt;A would count as a reciprocal link for node A if there is
# a path A-&gt;C-&gt;B, for some page C, (link path of length 2),
# or a direct link A-&gt; B (link path of length 1).

# Modify the compute_ranks code to 
#   - take an extra input k, which is a non-negative integer, and 
#   - exclude reciprocal links of length up to and including k from 
#     helping the page rank.
</code></pre>
