Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Resolve Transitivity in Dictionary

Python – Resolve Transitivity in Dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to resolve the dangling transitivity, i.e remove keys that have a relation of kind a->b, b->c = a->c, removing unwanted “b” in this relation. This kind of problem can occur in many core CS subjects and also in web development domains or with Graphs. Let us discuss certain ways in which this task can be performed. 

Method 1: Using reverse dictionary + loop 

This is one way in which this problem can be solved. In this, we construct the reverse dictionary and then run loop to check for duplicates and perform removal. 

Python3




# Python3 code to demonstrate working of
# Resolve Transitivity in Dictionary
# Using reverse dictionary + loop
 
# Initializing dictionary
test_dict = {'a': 3, 'b': 4, 3: 5, 'l': 7, 4: 'd', 7: 'k'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Resolve Transitivity in Dictionary
# using reverse dictionary + loop
temp = {val: key for key, val in test_dict.items()}
for val in temp:
   
    if val in test_dict:
    test_dict.pop(temp[val])
    test_dict[temp[val]] = test_dict[val]
    test_dict.pop(val)
 
# Printing result
print("The resolved dictionary : " + str(test_dict))


Output : 

The original dictionary : {3: 5, 4: 'd', 7: 'k', 'b': 4, 'l': 7, 'a': 3}
The resolved dictionary : {'b': 'd', 'l': 'k', 'a': 5}

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method 2: using a union-find data structure. 

Steps: 

  1. Define a function find that takes a dictionary parent and a key x and returns the ultimate parent of x.
  2. Define a function union that takes a dictionary parent, two keys x and y, and makes the ultimate parent of x the same as the ultimate parent of y.
  3. Create an empty dictionary parent.
  4. For each key k and value v in the input dictionary, add k and v to parent as keys with their own values as the initial parents.
  5. For each key k and value v in the input dictionary, perform union(parent, k, v).
  6. Traverse parent and update the values of all keys that are not their own ultimate parent.
  7. Return the updated dictionary.

Python3




# Python3 code to demonstrate working of
# Resolve Transitivity in Dictionary
# Using Union-Find
def find(parent, x):
    if parent[x] == x:
        return x
    parent[x] = find(parent, parent[x])
    return parent[x]
 
 
def union(parent, x, y):
    parent[find(parent, x)] = find(parent, y)
 
 
def resolve_transitivity(test_dict):
    parent = {}
    for k, v in test_dict.items():
        parent[k] = k
        parent[v] = v
    for k, v in test_dict.items():
        union(parent, k, v)
    for k in parent:
        parent[k] = find(parent, k)
    for k, v in test_dict.items():
        if v != parent[k]:
            test_dict[k] = test_dict[v]
    return test_dict
 
 
# Initializing dictionary
test_dict = {'a': 3, 'b': 4, 3: 5, 'l': 7, 4: 'd', 7: 'k'}
 
print("The original dictionary: " + str(test_dict))
 
updated_dict = resolve_transitivity(test_dict)
 
print("The resolved dictionary: " + str(updated_dict))


Output

The original dictionary: {'a': 3, 'b': 4, 3: 5, 'l': 7, 4: 'd', 7: 'k'}
The resolved dictionary: {'a': 5, 'b': 'd', 3: 5, 'l': 'k', 4: 'd', 7: 'k'}

Time complexity: O(N α(N)), where N is the number of elements in the dictionary and α(N) is the inverse Ackermann function, which grows extremely slowly and is essentially a constant for all practical purposes. 
Auxiliary space: O(N) to store the parent dictionary.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments