Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# Python program for solution of M Coloring # problem using backtracking class Graph(): def __init__(self, vertices): self.V = vertices self.graph = [[0 for column

# Python program for solution of M Coloring

# problem using backtracking

class Graph():

def __init__(self, vertices):

self.V = vertices

self.graph = [[0 for column in range(vertices)]\

for row in range(vertices)]

# A utility function to check

# if the current color assignment

# is safe for vertex v

def isSafe(self, v, colour, c):

for i in range(self.V):

if self.graph[v][i] == 1 and colour[i] == c:

return False

return True

# A recursive utility function to solve m

# coloring problem

def graphColourUtil(self, m, colour, v):

if v == self.V:

return True

for c in range(1, m + 1):

if self.isSafe(v, colour, c) == True:

colour[v] = c

if self.graphColourUtil(m, colour, v + 1) == True:

return True

colour[v] = 0

def graphColouring(self, m):

colour = [0] * self.V

if self.graphColourUtil(m, colour, 0) == None:

return False

# Print the solution

print "Solution exist and Following

are the assigned colours:"

for c in colour:

print c,

return True

# Driver Code

g = Graph(4)

g.graph = [[0, 1, 1, 1], [1, 0, 1, 0], [1, 1, 0, 1], [1, 0, 1, 0]]

m = 3

g.graphColouring(m)

# This code is contributed by Divyanshu Mehta

Convert the above Python code into pseudocode.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

=+j Describe the various support services delivered by IHR.

Answered: 1 week ago