Question
python 3
Write a function that receives an array S representing a disjoint set forest and an integer k and determines if k is a root i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Read comments for explanation

#a root will be a node which is a parent of itslf
#in forest S check if S[k]=k or not if yes then k is root

#time complexity of this part is O(1)
def isRoot(S,k):
   if S[k]==k:
       return True
   return False
#leaf is node which is not a parent of itself
#traverse S if a node is found whose parent is k return false
#else return true

#time complexity of this part is O(n) where n is number of nodes
def isLeaf(S,k):
   for i in forest:
       if i==k:
           return False
   return True;
#test
forest=[2,0,2,4,4,5]
#parent of 0 is node 2
print(isRoot(forest,0))
#node 2 is a root
print(isRoot(forest,2))
#node0 is parent of node 1
print(isLeaf(forest,0))
#node 1 is a leaf
print(isLeaf(forest,1))

Terminal t : (95%) ) 22:35 * C Python 3| Chegg.com + ← → https://www.HomeworkLib.com/homework-help/expertquestion Ξ Chegg. Study TE
HomeworkLib.py ()-gedit t : ) (9696) 22:37 * OpenH Save 1#a root will be a node which is a parent of itslf 2 #tn forest s check if

Add a comment
Know the answer?
Add Answer to:
python 3 Write a function that receives an array S representing a disjoint set forest and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Write a function that receives an integer array with its size and determines whether...

    In C++ Write a function that receives an integer array with its size and determines whether the array is already sorted in increasing order. Write a test program (main function) that prompts the use to enter a list and displays whether the list is sorted or not. The first number in the input indicates the size of the list. Assume the maximum list size is 20.

  • In C++ Write a function that receives an integer array with its size and determines whether...

    In C++ Write a function that receives an integer array with its size and determines whether the array is already sorted in increasing order. Write a test program (main function) that prompts the use to enter a list and displays whether the list is sorted or not. The first number in the input indicates the size of the list. Assume the maximum list size is 20.

  • Python: Write a function "contrast_adjust", which takes a 2D NumPy array A and an integer c,...

    Python: Write a function "contrast_adjust", which takes a 2D NumPy array A and an integer c, and returns another NumPy array of the same size, representing the contrast-adjusted image. You can assume that −127 ≤ c ≤ 127.

  • Write a Python function cardinality() that takes in three Python set objects, representing sets of between...

    Write a Python function cardinality() that takes in three Python set objects, representing sets of between 0 and 50 integers, AA, BB, and UU. Your function should return a single non-negative integer value for the cardinality of the set below. AA and BB are subsets (not necessarily proper) of the universal set UU. |P(A¯¯¯¯∩B)||P(A¯∩B)| Note 1: You can copy-paste the code declaring the various visible test cases below. We strongly encourage you to do this to test your code. Note...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • 1. Write a C function named find that receives a one-dimensional array of type integer named...

    1. Write a C function named find that receives a one-dimensional array of type integer named arr and its size of type integer. The function fills the array with values that are the power of four (4^n) where n is the index. After that, the function must select a random index from the array and move the array elements around the element stored in the randomly selected index Example arr = [1, 4, 16, 64, 256) if the randomly selected...

  • Write a function makePoly(x,y,n,l) that receives a coordinate point (x,y), an integer n representing the desired...

    Write a function makePoly(x,y,n,l) that receives a coordinate point (x,y), an integer n representing the desired number of sides (3 to 20) , and an integer l between 10 and 200 pixels, inclusive, representing the length of each side. The function will then use the turtle to draw the requested n-sided polygon at the designated (x,y) coordinate, having sides of length l. Be sure to reject arguments that do not conform to the restrictions given. (Hint: to get a turtle...

  • USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array...

    USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array of integers (and the size of the array in the C/C++ version). The function will reverse the array of integers and return the reversed array. Print the array that is returned from the function. How you "return" the array is up to you, but do not overwrite the original array. Note: in Python, the term list is more appropriate, see https://docs.python.org/3.5/tutorial/datastructures.html a) Example input:...

  • 1. a. Function Description: The Python function is "clearSecurity". It receives 2 parameters (dat...

    1. a. Function Description: The Python function is "clearSecurity". It receives 2 parameters (databaseScore, obsScore). The first is a float; the second is an integer. The function's purpose is to determine if the person can enter the secure zone. (databasescore-80.5 and obScore-5). The function returns a message saying whether the person is eligible to enter or needs further scrutiny. Fill in the following table. Expected result Function NamePurpose Input (s) and Parameter(s) clearSecurity0 1.b. Write the function code: 1.c. Write...

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT