Question: Can you fill the following python code such that it passes the three tests I give you? Python code: from pulp import * def encode

Can you fill the following python code such that it passes the three tests I give you?
Python code:
from pulp import *
def encode_and_solve_three_coloring(n, edge_list):
assert n >=1, 'Graph must have at least one vertex'
assert all(0<= i and i < n and 0<= j and j < n and i != j for (i,j) in edge_list ), 'Edge list is not well formed'
prob = LpProblem('Three Color', LpMinimize)
#1. Formulate the decision variables
#2. Add the constraints for each vertex and edge in the graph.
#3. Solve and interpret the status of the solution.
#4. Return the result in the required form to pass the tests below.
# your code here
raise NotImplementedError
Test 1:
n =5
edge_list =[(1,2),(1,3),(1,4),(2,4),(3,4)]
(flag, color_assign)= encode_and_solve_three_coloring(n, edge_list)
assert flag == True, 'Error: Graph is three colorable but your code wrongly returns flag = False'
print(f'Three color assignment: {color_assign}')
def check_three_color_assign(n, edge_list, color_assign):
assert len(color_assign)== n, f'Error: The list of color assignments has {len(color_assign)} entries but must be same as number of vertices {n}'
assert all( col =='r' or col =='b' or col =='g' for col in color_assign), f'Error: Each entry in color assignment list must be r, g or b. Your code returned: {color_assign}'
for (i, j) in edge_list:
ci = color_assign[i]
cj = color_assign[j]
assert ci != cj, f' Error: For edge ({i,j}) we have same color assignment ({ci, cj})'
print('Success: Three coloring assignment checks out!!')
check_three_color_assign(n, edge_list, color_assign)
print('Passed: 10 points!')
Test 2:
n =5
edge_list =[(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
(flag, color_assign)= encode_and_solve_three_coloring(n, edge_list)
assert flag == False, 'Error: Graph is NOT three colorable but your code wrongly returns flag = True'
print('Passed: 5 points!')
Test 3:
n =10
edge_list =[(1,5),(1,7),(1,9),(2,4),(2,5),(2,9),(3,4),(3,6),(3,7),(3,8),(4,5),(4,6),(4,7),(4,9),(5,6),(5,7),(6,8),(7,9),(8,9)]
(flag, color_assign)= encode_and_solve_three_coloring(n, edge_list)
assert flag == True, 'Error: Graph is three colorable but your code wrongly returns flag = False'
print(f'Three color assignment: {color_assign}')
def check_three_color_assign(n, edge_list, color_assign):
assert len(color_assign)== n, f'Error: The list of color assignments has {len(color_assign)} entries but must be same as number of vertices {n}'
assert all( col =='r' or col =='b' or col =='g' for col in color_assign), f'Error: Each entry in color assignment list must be r, g or b. Your code returned: {color_assign}'
for (i, j) in edge_list:
ci = color_assign[i]
cj = color_assign[j]
assert ci != cj, f' Error: For edge ({i,j}) we have same color assignment ({ci, cj})'
print('Success: Three coloring assignment checks out!!')
check_three_color_assign(n, edge_list, color_assign)
print('Passed: 5 points!')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!