In: Computer Science
In python this structure can be represented by a set of tuples, where each tuple has two elements. The following two lines would build the set given above and the print it. >>> L = [(’dog’,’white’), (’cat’,’black’),(’mouse’,’black’)] >>> f = set(L) >>> print(f) {(’cat’, ’black’), (’dog’, ’white’), (’mouse’, ’black’)} In the example, first we store the tuples into a list, and then we create a set with those tuples. There are obviously countless other ways to initialize f and get the same result. The following example builds a similar structure, but has a problem: 1 >>> L = L = [(’dog’,’white’), (’cat’,’black’),(’dog’,’black’)] >>> f = set(L) >>> print(f) {(’dog’, ’black’), (’cat’, ’black’), (’dog’, ’white’)} The problem is that in this case f is not the graph of a function because there are two entries where dog is the first element, and this is contrary to the definition of a function (every element in the domain must be associated with exactly one element in the codomain.)
With the above assumptions we then represents functions by giving their graph. Write two functions in python that satisfy the following specifications:
1. Write a function is_a_graph(A,B,f)
that receives three parameters. The first is the domain, the second is the codomain, and the third is a subset of A × B. The function returns True if f represents the graph of a function f : A → B and False otherwise.
2. Write a function is_surjective(A,B,f)
that receives three parameters. The first is the domain, the second is the codomain, and the third is the graph of a function represented as above. The function returns True if f is the graph of a surjective function and False otherwise. You can assume that in this case f is the graph of a function, i.e., you can ignore the case when f is not the graph of a function.
Suggestions: to solve this exercise it is necessary to analyze all elements in f. To this end, you can re-use the code provided in the file functionsexamples.py that is linked from the lab assignment. Therein we give the code for a function is injective that can be modified to answer the questions given above.
___________________________use this code__in python_________________________________________________________________________________________________
# creates the domain
A = set(['dog','cat','fish','frog'])
# creates the codomain
B = set([1,2,3,4,5,6])
# creates the graph of a function
f = set([('dog',1),('cat',1),('frog',5),('fish',4)])
# This corresponds to the following function
# f(dog) = 1
# f(cat) = 1
# f(frog) = 5
# f(fish) = 4
# prints domain and codomain as sets
print('Domain A')
print(A)
print('Codomain B')
print(B)
# prints the graph of the function as a set
print('Graph of the function')
print(f)
# iterates over all the tuples in f and prints them to the
screen one per row
for element in f:
print(element)
# prints all the first elements in the tuples in f; observe that
[0] accesses the first element in the tuple
for element in f:
print(element[0])
# prints all the second elements in the tuples in f; observe that
[1] accesses the second element in the tuple
for element in f:
print(element[1])
def is_injective(A,B,f):
"""
Determines if f is the graph of an injective function.
We assume that f is a valid graph.
Note: in this case parameters are not necessary and not even
used.
However they are included for uniformity with the questions you
have to
answer in the lab.
Parameters
----------
A: set
A is the domain of the function
B: set
B is the codomain of the function
f : set of tuples
f is the graph of a function.
Returns
-------
bool
True if f is the graph of an injective function and False if it is
not.
"""
for element in f: # analyze all elements in the graph
a = element[0] # first element in the tuple (not needed -- just for
clarity)
b = element[1] # second element: b = f(a)
for other in f: # consider other elements in the graph
if other != element:
bprime = other[1] # Same as above
if b == bprime: # if two elements have the same image, the function
is not injective
return False
# if this point is reached, all elements have been analyzed and it
has not
# found two elements with the same image; so the function is
injective
return True
check1 = is_injective(A,B,f)
if check1 == True:
print('Function f is injective')
else:
print('Function f is not injective')
# change f to make it injective
f = set([('dog',1),('cat',2),('frog',5),('fish',4)])
check2 = is_injective(A,B,f)
if check2 == True:
print('Function f is injective')
else:
print('Function f is not injective')
#determines if f is a valid graph
def is_a_graph(A,B,f):
for element in f:
#if first element of tuple is not in domain and/or second
element of tuple is not in codomain
#it means the f is not a graph
#thus, return false
if(element[0] not in A or element[1] not in B):
return False
#if every element of f is ok, it is a graph
return True
#determines if f is a surjective function
def is_surjective(A,B,f):
y = []
#create a list with every codomain in f
for element in f:
y.append(element[1])
#if every element of actual codomain is not in codomain in
f,
#it means the function is not surjective
for codomains in B:
if codomains not in y:
return False
return True
As the function simply returns True and False, I am not showing any sample execution. But the functions work.
In case of any doubt, drop a comment and I'll surely get back to you.
Please give a like if you're satisfied with the answer. Thank you.