0% found this document useful (0 votes)
24 views5 pages

AI Lab Report 5 by Sakib Hossain (212002099)

The lab report details the implementation of a graph coloring algorithm using a greedy approach, which reads a graph definition from user input and constructs the graph accordingly. The algorithm ensures that no two adjacent vertices share the same color, making it applicable for various real-world problems. The report concludes that the program is flexible and user-friendly, allowing for easy customization and testing of different graph structures.

Uploaded by

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

AI Lab Report 5 by Sakib Hossain (212002099)

The lab report details the implementation of a graph coloring algorithm using a greedy approach, which reads a graph definition from user input and constructs the graph accordingly. The algorithm ensures that no two adjacent vertices share the same color, making it applicable for various real-world problems. The report concludes that the program is flexible and user-friendly, allowing for easy customization and testing of different graph structures.

Uploaded by

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

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Spring, Year:2024), B.Sc. in CSE (Day)

Lab Report 5

Course Title: Artificial Intelligence Lab


Course Code: CSE-316 Section:212-D6

Project Title: Graph Coloring Algorithm based on lab manual 5.


Student Details

Name ID
1. Sakib Hossain 212002099

Submission Date : 20-05- 2024


Course Teacher’s Name : Wahia Tasnim

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
Graph Coloring Algorithm with Text Input

❖ Objective:
To implement a graph coloring algorithm that reads a graph definition from text input,
constructs the graph, and performs the greedy graph coloring algorithm.

❖ Theory:
Graph coloring is an assignment of labels, commonly called "colors", to elements of a graph
subject to certain constraints. In its simplest form, it is a way of coloring the vertices of a
graph such that no two adjacent vertices share the same color. The greedy coloring
algorithm is a heuristic that colors each vertex in a graph by assigning the smallest possible
color that has not been used by its adjacent vertices.

❖ Algorithm:
1. **Input Parsing:**
- Read the input graph from the user.
- Construct the adjacency list from the input text.

2. **Greedy Graph Coloring:**


- Initialize an array to store colors of vertices, initially setting all values to -1.
- Assign the first color (0) to the first vertex.
- For each subsequent vertex, mark colors of its adjacent vertices as unavailable.
- Find the first available color and assign it to the current vertex.
- Reset the availability of colors for the next iteration.
- Print the color assigned to each vertex.

❖ Implementation:
```python
def parse_input(input_text):
lines = input_text.strip().split('\n')
graph = {}
for line in lines:
parts = line.split(':')
node = int(parts[0].strip())
neighbors = list(map(int, parts[1].strip().strip('[]').split(','))) if parts[1].strip() else []
graph[node] = neighbors
return graph

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 not available[cr]:
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])

def main():
input_text = """
0: [1, 2]
1: [2, 3, 0]
2: [3, 1, 0]
3: [1, 2, 4]
4: [3]

print("Enter the graph in the format 'node: [neighbor1, neighbor2, ...]'.")


print("Enter an empty line to finish input.")
print("Example input:")
print(input_text.strip())

input_lines = []
while True:
line = input().strip()
if line == "":
break
input_lines.append(line)

input_text = "\n".join(input_lines)

graph = parse_input(input_text)
V = len(graph)

print("Coloring of the input graph:")


greedyColoring(graph, V)

if __name__ == "__main__":
main()

❖ Results:
The program prompts the user to enter a graph definition, constructs the graph, and then
performs the greedy graph coloring algorithm. For example, given the input:

0: [1, 2]
1: [2, 3, 0]
2: [3, 1, 0]
3: [1, 2, 4]
4: [3]

The program outputs:

❖ Conclusion:
This lab exercise demonstrates the implementation of a greedy graph coloring algorithm
that reads user input to define the graph structure. The program successfully constructs the
graph and assigns colors to each vertex such that no two adjacent vertices share the same
color. This approach is useful in scenarios where graph coloring is required, such as
scheduling problems, map coloring, and frequency assignment.

By leveraging text input for graph definition, the program is flexible and user-friendly,
allowing for easy customization and testing with various graph structures.

You might also like