0% found this document useful (0 votes)
6 views

Experiment - 7: Aim: WAP To Implement Graph Coloring Using Greedy Algorithm. Theory

The document describes implementing graph coloring using a greedy algorithm. It explains the greedy algorithm, which assigns colors to vertices in a way that tries to minimize the number of colors used. It does not guarantee the minimum number but provides an upper bound. The algorithm is implemented in Python code that colors sample graphs.

Uploaded by

akshat sharma
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)
6 views

Experiment - 7: Aim: WAP To Implement Graph Coloring Using Greedy Algorithm. Theory

The document describes implementing graph coloring using a greedy algorithm. It explains the greedy algorithm, which assigns colors to vertices in a way that tries to minimize the number of colors used. It does not guarantee the minimum number but provides an upper bound. The algorithm is implemented in Python code that colors sample graphs.

Uploaded by

akshat sharma
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/ 3

EXPERIMENT -7

Aim: WAP to implement Graph coloring using greedy algorithm.


Theory:
Unfortunately, there is no efficient algorithm available for coloring a graph with minimum
number of colors as the problem is a NP complete problem. There are approximate algorithms
to solve the problem though. Following is the basic Greedy Algorithm to assign colors. It
doesn’t guarantee to use minimum colors, but it guarantees an upper bound on the number of
colors. The basic algorithm never uses more than d+1 colors where d is the maximum degree
of a vertex in the given graph.
Basic Greedy Coloring Algorithm: 
1. Color first vertex with first color. 
2. Do following for remaining V-1 vertices. 
….. a) Consider the currently picked vertex and color it with the 
lowest numbered color that has not been used on any previously 
colored vertices adjacent to it. If all previously used colors 
appear on vertices adjacent to v, assign a new color to it.

CODING:
def addEdge(adj, v, w):
adj[v].append(w)
adj[w].append(v)
return adj
def greedyColoring(adj, V):
result = [-1] * V
result[0] = 0;
available = [False] * V
for u in range(1, V):
for i in adj[u]:
if (result[i] != -1):
available[result[i]] = True
cr = 0
while cr < V:
if (available[cr] == False):
break
cr += 1
result[u] = cr
for i in adj[u]:
if (result[i] != -1):
available[result[i]] = False
for u in range(V):
print("Vertex", u, " ---> Color", result[u])
if __name__ == '__main__':
g1 = [[] for i in range(5)]
g1 = addEdge(g1, 0, 1)
g1 = addEdge(g1, 0, 2)
g1 = addEdge(g1, 1, 2)
g1 = addEdge(g1, 1, 3)
g1 = addEdge(g1, 2, 3)
g1 = addEdge(g1, 3, 4)
print("Coloring of graph 1 ")
greedyColoring(g1, 5)
g2 = [[] for i in range(5)]
g2 = addEdge(g2, 0, 1)
g2 = addEdge(g2, 0, 2)
g2 = addEdge(g2, 1, 2)
g2 = addEdge(g2, 1, 4)
g2 = addEdge(g2, 2, 4)
g2 = addEdge(g2, 4, 3)
print("\nColoring of graph 2")
greedyColoring(g2, 5)
OUTPUT:
RESULT:
The program is successfully done and compiled in python.

You might also like