Question

Please write a python function for :

greedy: This function takes two inputs: one a graph, and the other an ordering of the vertices as a list, and returns the pro

Assumptions you may make You may assume the user of your code will call your functions with their own print statements (i.e.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def greedy(g,l):
vertex = sorted(list(g.keys()), key=lambda x: len(g[x]), reverse=True) # sort in decending order
color = {}

for n in vertex:
Available_Colors= [True] * len(vertex)
for n_g in g[n]:
if n_g in color:
c = color[n_g]
Available_Colors[c] = False
for c, av in enumerate(Available_Colors):
if av:
color[n] = c
break

return color
g={'A':['B'],'B':['A','C'],'C':['B','D'],'D':['C']}
l=['A','B','C','D']
x=greedy(g,l)
print(x)

RESTART C:/Users/Sahil Chopra/AppData/Local/Programs/Python/Python37/jh.py >>> RESTART C:/Users/Sahil Chopra/AppData/Local/Prjh.py - C/Users/Sahil Chopra/AppData/Local/Programs/Python/Python37/jh.py (3.7.0) File Edit Format Run Options Window Help de

Add a comment
Know the answer?
Add Answer to:
Please write a python function for : greedy: This function takes two inputs: one a graph,...
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
  • The language is python Write the function largest_edge_group(vertices) that consumes vertices, a list of list of...

    The language is python Write the function largest_edge_group(vertices) that consumes vertices, a list of list of integer containing the coordinates of the consecutive vertices of a polygon. The function largest_edge_group returns the size of the largest group of same length edges. Your function must run in O(n), where n is the length of vertices. Example largest_edge_group([[0,0],[1,1],[0,2],[-2,3],[-1,2]]) => 3 Hint Remember, it is not possible to compare Floats for strict equality, but since the coordinates are integers, the squared distance is...

  • IN PYTHON CODE Question #1 Write a function capital that has one argument: strlist that is...

    IN PYTHON CODE Question #1 Write a function capital that has one argument: strlist that is a list of non-empty strings. If each string in the list starts with a capital letter, then the function should return the value True. If some string in the list does not start with a capital letter, then the function should return the value False You may use any of the string functions that are available in Python in your solution, so you might...

  • IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...

    IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a character list and the other is the corresponding frequencies, and sorts the characters in ascending order of frequencies. The function should return a list consististing of two lists: the first list is the list of characters and the second is the list of corresponding frequencies, consistent with the rearranged characters. Name this function sortCharacters(characterList, frequencyList). For example, >>>sortCharacters(["a", "b", "c", "d"], [5, 8, 3,...

  • Python Write a function that takes in two numbers as parameters. The function will then return...

    Python Write a function that takes in two numbers as parameters. The function will then return the value of the first parameters squared by the second parameter. Print the value of the return statement. Take a screenshot of both your code and your properly executed code after you have run it. Submit the screenshots here along with your .txt file.

  • Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A),...

    Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A), the power set of A. The output should be only in the form of list objects. Note: Make sure you are familiar with some of Python's basic built-in list functions and operators, as they will make completing this problem far easier. For example: Test Result A = [0, 1, 2] output = set([frozenset(a) for a in powerSet(A)]) expected = [[], [0], [0, 1], [2],...

  • PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give...

    PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define new functions or helper...

  • Using Python 3.7 and higher, implement function egypt(numerator, denominator) that uses the greedy strategy to determine...

    Using Python 3.7 and higher, implement function egypt(numerator, denominator) that uses the greedy strategy to determine a set of distinct Egyptian fractions that sum to numerator/denominator. You cannot use any sort of division, including the floor and ceiling methods, nor the division, remainder, or modulus operators. 1. Implement function egypt(numerator, denominator) that uses the greedy strategy presented in slides 7 and 8 of Lecture 30 (April 13) to determine a set of distinct (i.e. all different) Egyptian fractions that sum...

  • PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP!...

    PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP! THANK YOU! RECURSIVE! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define...

  • 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...

  • In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and...

    In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and n2. Using a single list comprehension to make a list of all of the numbers in the range 1..N that are either divisible by n1 or n2, where n1 and n2 can be any positive integers. After creating the list, your code should print out the following 2 lines: “We are printing all numbers from 1 to that are divisible by or ” “These...

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