Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Do you know how i can solve the following python code? There are n nodes in a network, initially empty. Your task is to implement

Do you know how i can solve the following python code?
There are n nodes in a network, initially empty. Your task is to implement a class that allows adding edges to the network and checking if the nodes can be colored with two colors such that each edge connects two nodes of different colors.
You can assume that there are at most 50 nodes and the methods of the class are called at most 100 times.
Implement a class named Coloring in python file with the following methods:
*Constructor that takes the number of nodes as input
*add_edge adds an edge between two nodes
*check checks if the network can be colored with two colors
class Coloring:
def __init__(self, n):
# TODO
def add_edge(self, a, b):
# TODO
def check(self):
# TODO
if __name__=="__main__":
c = Coloring(4)
c.add_edge(1,2)
c.add_edge(2,3)
c.add_edge(3,4)
c.add_edge(1,4)
print(c.check()) # True
c.add_edge(2,4)
print(c.check()) # False
c = Coloring(5)
print(c.check())
print(c.check())
c.add_edge(3,4)
c.add_edge(4,5)
c.add_edge(4,5)
print(c.check())
c.add_edge(4,5)
c.add_edge(3,5)
print(c.check())
print(c.check())
Expected output:
line 7: check returns True

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_2

Step: 3

blur-text-image_3

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

Databases Theory And Applications 27th Australasian Database Conference Adc 20 Sydney Nsw September 28 29 20 Proceedings Lncs 9877

Authors: Muhammad Aamir Cheema ,Wenjie Zhang ,Lijun Chang

1st Edition

3319469215, 978-3319469218

More Books

Students also viewed these Databases questions