Menu

Python Code Please Follow Question S Requirements Print Function Used Use Return Task 4 M Q43867147

PYTHON CODE

PLEASE FOLLOW THE QUESTION’S REQUIREMENTS!!!!!!!

NO PRINT FUNCTION CAN BE USED, ONLY CAN USERETURN

Task 4: m-Coloring problem

In this problem, an undirected graph is given. There is alsoprovided m colors. The problem is to find if it is possible toassign nodes with m different colors, such that no two adjacentvertices of the graph are of the same colors. If the solutionexists, then display which color is assigned on which vertex asshown in Figure 2. The graph coloring enjoys many practicalapplications as well as theoretical challenges. Some of thepractical applications of graph coloring are pattern matching,scheduling, designing seating plans, exam timetabling, radiofrequency assignment and solving Sudoku puzzles.

Figure 2: An example of graph that can be colored with 3 different colors.

There is two files, graph_A.txt andgraph_b.txt

Inside graph_A.txt there is :
0,1,1,1
1,0,1,0
1,1,0,1
1,0,1,0

Inside graph_B.txt there is :

0,1,0,0,1,1,0,0,0,0
1,0,1,0,0,0,1,0,0,0
0,1,0,1,0,0,0,1,0,0
0,0,1,0,1,0,0,0,1,0
1,0,0,1,0,0,0,0,0,1
1,0,0,0,0,0,0,1,1,0
0,1,0,0,0,0,0,0,1,1
0,0,1,0,0,1,0,0,0,1
0,0,0,1,0,1,1,0,0,0
0,0,0,0,1,0,1,1,0,0

Part A: Get graph from text-file

Write a function graph from file(file name) that reads in a filecontaining a graph and returns the contents of the file as aPython-readable table.

Input: a file name file name, where the file contains n lines,and each line contains n entries separated by commas.

Output: a table represented as a nested list, where each entryin the original file is an element in the table, and all numbershave been converted to integers.

Examples

a) Calling (graph from file(‘graph A.txt’)), returns: [[0, 1, 1,1], [1, 0, 1, 0], [1, 1, 0, 1], [1, 0, 1, 0]]

b) Calling (graph from file(‘graph B.txt’)), returns: [[0, 1, 0,0, 1, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 1,0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0,0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 1, 0], [0, 1, 0, 0, 0, 0,0, 0, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 1, 1,0, 0, 0], [0, 0, 0, 0, 1, 0, 1, 1, 0, 0]]

Part B: Valid entry

Write a function valid entry(vertex,graph,colored vertex,color)that determines whether a particular color can be entered at aparticular vertex in the graph, while maintaining validity.

Input: vertex is an integer that represents the vertex that tobe colored; graph is a nested list that represents the adjacencymatrix of the graph; colored vertex is a list that represents thatthe color of the vertices. For an example, a graph with 4 vertices,colored vertex can be represented by colored vertex =[1,2,3,0] (Wemay assume that red = 1, green = 2, blue = 3 and not colored = 0.Assignment of colors are arbitrary, however, we assign not coloredas 0). In this example, vertex 0 is colored by red, vertex 1 iscolored as green, vertex 2 is colored as blue and vertex 3 is notcolored; color is an integer represents the color to be added tovertex.

Output: return a Boolean is the coloring is valid; otherwiseFalse.

Examples

Given graph A= [[0, 1, 1, 1], [1, 0, 1, 0], [1, 1, 0, 1], [1, 0,1, 0]]

a) Calling valid entry(3,graph A,[1,2,3,0],2) returns True.

b) Calling valid entry(3,graph A,[1,2,3,0],1) returns False.

Part C: Assign colors to vertex

Write a function add color vertex(vertex,graph,colored vertex,m)that assigns all the possible valid colors to a particularvertex.

Input: vertex is an integer that represents the vertex that tobe colored; graph is a nested list that represents the adjacencymatrix of the graph; colored vertex is a list that represents thatthe color of the vertices; m represents the number of thecolors.

Output: a nested list that represents all the possible validcolor assignments to vertex. If no possible assignment of colors tothe vertex available, then return an empty list.

a) Calling add color vertex(1,graph A,[1,0,0,0],3) returns [[1,2, 0, 0], [1, 3, 0, 0]].

b) Calling add color vertex(2,graph A,[1, 2, 2, 0],2) returns[].

Part D: Solve m-coloring problem for the given graph

Write a function m color(file name,m) that finds the solutionfor the given graph.

Input: a file name, where the file contains n lines, and eachline contains n entries separated by commas. This file representsthe adjacency matrix of a graph.
Output: a list representing the colored graph. If there are morethan one possible solutions available, just return one possiblesolution (You may always return all possible solutions). If nopossible solution available, the function should return an emptylist.

Examples

a) Calling (m color(‘graph A.txt’,3)) returns [1, 2, 3, 2] (Oneof the possible valid solution).

b) Calling (m color(‘graph A.txt’,2)) returns [].

c) Calling (m color(‘graph B.txt’,3)) returns [1, 2, 1, 2, 3, 2,1, 3, 3, 2] (One of the possible valid solution).

Students are encouraged to validate the correctness oftheir functions with different graphs.

Marks are given for the correct behavior of thedifferent functions: (a) Task 4(A): 1 mark for graph from file. (b)Task 4(B): 1 mark for valid entry. (c) Task 4(C): 3 marks for addcolor vertex. (d) Task 4(D): 5 marks for m solve.

THE RETURN OUTPUT MUST BE THE SAME AS THE EXAMPLE GIVEN,NO PRINT FUNCTION CAN BE USED, ONLY RETURN

THIS IS THE WHOLE QUESTION, WHAT ELSE INFO DO YOU NEED??????????

Figure 2: An example of graph that can be colored with 3 different colors. Show transcribed image text Figure 2: An example of graph that can be colored with 3 different colors.

Expert Answer


Answer to PYTHON CODE PLEASE FOLLOW THE QUESTION’S REQUIREMENTS !!!!!!! NO PRINT FUNCTION CAN BE USED, ONLY CAN USE RETURN Task 4:…

OR