0% found this document useful (0 votes)
13 views4 pages

Ai 3 Exp

efseffg

Uploaded by

dekitov522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Ai 3 Exp

efseffg

Uploaded by

dekitov522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Bharati Vidyapeeth (Deemed to be University)

Department of Engineering and Technology, Kharghar, Navi


Mumbai

Department of Computer Science and


Engineering

Experiment - 3
Name: Anoop Khude Roll No: 05

Subject: AI Class/Batch: TY/CSE-B1

Date of Performance: Date of Submission: /07/24

AIM
write a program to implement Graph Coloring

Theory/Procedure/Algorithm

Graph coloring refers to the problem of coloring vertices of a graph in such a way that no two
adjacent vertices have the same color. This is also called the vertex coloring problem. If coloring is
done using at most m colors, it is called m-coloring.

Time Complexity: O(mV). There is a total O(mV) combination of colors

Auxiliary Space: O(V). The Recursive Stack of graph coloring(…) function will require O(V) space.

Assign colors one by one to different vertices, starting from vertex 0. Before assigning a color, check
for safety by considering already assigned colors to the adjacent vertices i.e check if the adjacent
vertices have the same color or not. If there is any color assignment that does not violate the
conditions, mark the color assignment as part of the solution. If no assignment of color is possible
then backtrack and return false
Bharati Vidyapeeth (Deemed to be University)
Department of Engineering and Technology, Kharghar, Navi
Mumbai

Department of Computer Science and


Engineering

Code:
print("Anoop Khude")

class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = [[] for _ in range(vertices)]

def add_edge(self, u, v):


self.graph[u].append(v)
self.graph[v].append(u)

def greedy_coloring(self):
result = [-1] * self.V
result[0] = 0
available = [False] * self.V

for u in range(1, self.V):


for v in self.graph[u]:
if result[v] != -1:
available[result[v]] = True

for color in range(self.V):


if not available[color]:
break

result[u] = color

for v in self.graph[u]:
if result[v] != -1:
available[result[v]] = False

for u in range(self.V):
print(f"Vertex {u} --> Color {result[u]}")

if __name__ == "__main__":
graph = Graph(5)
Bharati Vidyapeeth (Deemed to be University)
Department of Engineering and Technology, Kharghar, Navi
Mumbai

Department of Computer Science and


Engineering
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(1, 3)
graph.add_edge(2, 3)
graph.add_edge(3, 4)

print("Coloring of vertices:")
graph.greedy_coloring()
Output:
Bharati Vidyapeeth (Deemed to be University)
Department of Engineering and Technology, Kharghar, Navi
Mumbai

Department of Computer Science and


Engineering

Conclusion

Thus we successfully implemented Graph Coloring.

Assessment

Timely Submission Presentation Understanding Total


Sign
(7) (06) (12) (25)

You might also like