def transition_model(corpus, page, damping_factor):
    """
    Return a probability distribution over which page to visit next,
    given a current page.

    With probability `damping_factor`, choose a link at random
    linked to by `page`. With probability `1 - damping_factor`, choose
    a link at random chosen from all pages in the corpus.
    """
    probability_distribution = dict()

    if page is None or len(corpus.get(page)) == 0:
        #probabilities are all the same
        probability = float(1) / len(corpus.keys())
        for key in corpus.keys():
            probability_distribution.update({key : probability})
    
    else:
        #probabilities are weighted
        probability = float(1 - damping_factor) / len(corpus.keys())
        linked_pages = corpus.get(page)
        linked_probability = float(damping_factor) / len(linked_pages)
        for key in corpus.keys():
            if linked_pages.issuperset({key}):
                probability_distribution.update({key : probability + linked_probability})
            else:
                probability_distribution.update({key : probability})

    return probability_distribution