0% found this document useful (0 votes)
643 views40 pages

MAT668-assignment

This document outlines a Python project focused on graph theory, specifically on vertex coloring and scheduling exams for mathematics students at University Teknologi MARA. It includes an introduction to graph coloring, methodologies for creating conflict graphs, and the application of graph coloring techniques to determine optimal exam scheduling. The project aims to minimize scheduling conflicts by utilizing graph coloring to assign time slots for exams based on student course enrollments.

Uploaded by

natasyaalia365
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)
643 views40 pages

MAT668-assignment

This document outlines a Python project focused on graph theory, specifically on vertex coloring and scheduling exams for mathematics students at University Teknologi MARA. It includes an introduction to graph coloring, methodologies for creating conflict graphs, and the application of graph coloring techniques to determine optimal exam scheduling. The project aims to minimize scheduling conflicts by utilizing graph coloring to assign time slots for exams based on student course enrollments.

Uploaded by

natasyaalia365
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/ 40

PYTHON PROJECT

GRAPH THEORY (MAT668)

SEMESTER: OCT 2023 - FEB 2024

GROUP: 2

CS2496B

NAME STUDENT ID

NORALIA NATASYA BINTI ZAMRI 2021822976

SHAHIDATUL ATIRAH BINTI EDRUS 2021839344

SITI NUR ALEEZA BINTI MOHD KAMAL 2022908017

MITCHELL BRYCE BOE 2021829772

LECTURER BY:
ASSOC. PROF. TS. DR. ADIBAH SHUIB
TABLE OF CONTENT
TABLE OF CONTENT 2
QUESTION 1 3
1.0 INTRODUCTION 3
1.1 OBJECTIVES 3
1.2 LITERATURE REVIEW 4
Graph Coloring 4
Vertex Coloring 4
Edge Coloring 5
Graph Initialization 5
2.0 METHODOLOGY 7
2.1 Conflict graph 7
2.2 Graph Coloring 11
2.3 Graph Coloring using Python 13
3.0 CONCLUSION 23
QUESTION 2 23
1.0 INTRODUCTION 23
1.1 OBJECTIVES 24
1.2 LITERATURE REVIEW 24
Graph Theory 24
Four Color Theory 25
Graph Coloring 25
Greedy Theorem 26
Euler’s Theorem 26
2.0 METHODOLOGY 27
2.1 Data and Descriptions of data 27
2.2 Explanation of steps using Python 28
2.3 Python coding and output 30
2.4 Vertex coloring 32
2.5 Graph Coloring 35
3.0 RESULTS 35
4.0 CONCLUSION 36
5.0 REFERENCES 37
QUESTION 1
1.0 INTRODUCTION

Vertex coloring in graph theory entails assigning colors to the vertices of a graph in such
a way that adjacent vertices are not assigned the same color. The act of assigning colors in the
graph is referred to as vertex coloring in formal terms. Generally, numerical values are employed
to represent various hues. Furthermore, we assign colors to the vertices in a manner that
guarantees that neighboring vertices are never filled with the identical color. The chromatic
number represents the smallest possible number of colors required to color the vertices of a
graph. Usually, this value continuously exceeds one. The chromatic number of a graph will be
equal to one only when the graph consists of a single vertex.

Vertex coloring is widely utilized in various domains of computer science and


mathematics. Moreover, the vertex coloring problem allows us to model complex real-world
difficulties. Computing the chromatic number of a graph is a formidable undertaking and comes
inside the NP-complete classification. Hence, there is a lack of certainty on the existence of a
feasible approach to solve it for all graphs. However, there are efficient techniques that can be
used for specific types of graphs with special characteristics.

1.1 OBJECTIVES

a. Prepare a list of 20 student names and complete Table 1 by marking 'X' for courses taken
by each student.
b. To draw a conflict graph manually illustrating shared students between exams and
analyze its structure and implications
c. To determine the minimum number of time slots for scheduling exams using manual
graph coloring, implement the solution in Python, and compare the results.
1.2 LITERATURE REVIEW

Graph Coloring

Ann et al.,(2022) stated that graph coloring, a dynamic sub-branch of graph theory, has
seen significant advancements and applications across various fields such as chemistry and
computer science. Originating from Leonhard Euler's 1736 paper on the Königsberg bridge
problem, which laid the foundation for graph theory, this field has evolved substantially. Graph
coloring began with the four-color conjecture proposed by Francis Guthrie, stating that any map
can be colored using no more than four colors such that no adjacent regions share the same color.
This conjecture, which spurred extensive research by scientists including Augustus De Morgan
and William Hamilton, was eventually proven in 1976 by Kenneth Appel and Wolfgang Haken
with the aid of computer assistance, marking the first major computer-assisted proof. Since then,
concepts like chromatic polynomials have emerged, maintaining graph coloring as an active area
of research with numerous practical applications in everyday life.

Vertex Coloring

Ann et al., (2022) also stated that graph coloring typically refers to vertex coloring, which
is a method of assigning colors to the vertices of a graph in such a way that no two adjacent
vertices (vertices connected directly by an edge) share the same color. This ensures that each pair
of connected vertices is distinguishable by their colors, helping to solve problems where conflict
or overlap must be avoided. For example, in scheduling problems, each vertex could represent a
task, and an edge between two vertices indicates that the tasks cannot occur simultaneously. By
coloring the graph, we ensure that no two conflicting tasks are assigned in the same time slot.
This principle can be applied to various real-world scenarios, such as map coloring where
regions sharing a border must be colored differently to distinguish them, or in network design to
minimize interference.
Edge Coloring

An edge coloring is the assignment of colors to the edges of a graph such that no two
incident edges (edges sharing a common vertex) share the same color. This method is essential
for avoiding conflicts in various applications where shared resources or connections must be
managed without overlap. In the scheduling context, edge coloring ensures that tasks represented
by edges do not share the same time slot if they are connected to a common resource or machine
represented by a vertex. For example, in a factory setting, each job (edge) that requires the same
machine (vertex) at the same time must be scheduled in different time slots, which can be
effectively managed using edge coloring techniques. Ann et al., (2022).

Graph Initialization

Graph initialization is a critical step in graph theory and its applications, laying the
groundwork for various algorithms and analyses. The process involves setting up a graph’s basic
structure, which includes defining vertices, edges, and their relationships. According to Cormen
et al. (2009), the initialization phase is crucial for ensuring the efficiency and accuracy of
subsequent computational processes. Initializing a graph correctly can significantly influence the
performance of algorithms such as those for shortest path, spanning tree, and network flow
problems. Techniques for graph initialization vary, from simple adjacency matrices and lists to
more sophisticated data structures like edge lists and incidence matrices, each offering distinct
advantages in terms of memory usage and access speed. Furthermore, in dynamic applications
such as social networks and communication systems, initialization must account for potential
changes in the graph’s structure over time, necessitating robust and adaptable initialization
methods (West, 2001). Overall, effective graph initialization forms the foundation for successful
implementation and analysis in graph-theoretical research and practical applications.

A graph is considered 𝑘 − 𝑐𝑜𝑙𝑜𝑟𝑎𝑏𝑙𝑒 if it is possible to assign one of 𝑘 unique colors to each of


its vertices in such a way that no two adjacent vertices share the same color. This ensures that
every pair of connected vertices is distinctly colored, preventing color clashes. If a graph can be
properly colored with 𝑘 colors but not with 𝑘 − 1 colors, it is termed 𝑘 − 𝑐ℎ𝑟𝑜𝑚𝑎𝑡𝑖𝑐. This
indicates that 𝑘 is the minimum number of colors required to achieve a valid vertex coloring for
that graph, reflecting the graph's chromatic number, which is a fundamental measure of its
complexity in terms of colorability (Idris et al., 2018).
2.0 METHODOLOGY
2.1 Conflict graph

University Teknologi MARA (UiTM) offered mathematical studies in the College of Computing,
Informatics, and Mathematics (KPPIM). The enrollment of mathematics students in a semester
has been done at the beginning of their undergraduate studies. In the current semester, a total of
20 mathematics students have been registered for their undergraduate studies.

Table 1 shows the ID and name of 20 students.

Table 1. List of students

STUDENT NAMES

S1 HADI IMAN BIN MOHD YASHAK

S2 ILME BALQIS BT. CHE ME

S3 KHAIRUN AISYA BT. ROSLI

S4 MOHAMAD KHAIRUL BIN AZAHAR MOHD FARHAN

S5 AIMAN B. AMZANI

S6 MUHAMMAD FAREZ BIN MOHD NAZRI MUHAMAD

S7 EILMAL HAFIZ BIN MAHAMAD

S8 FISOL MUHAMAD KHAIRUL NIZAM BIN BAHAROM

S9 MUHAMMAD AIDIL BIN MOHD NIZAM

S10 MUHAMMAD AIDIL HAIKAL B. MOHD. NASIR

S11 MUHAMMAD IMMAD BIN ALIS

S12 MUHAMMAD ISYRAQ IMAN BIN SHAHARUDIN

S13 MUHAMMAD LUKMAN B. JOHART

S14 MUHAMMAD NABIL BIN MD SHARIFF


S15 MUHAMMAD YUSUF BIN JALALUDDIN

S16 MUHAMMAD ZAIM B. MUHAMAD ZOHIR

S17 MUHAMMAD ZHARFAN SUFI BIN MUHAMAD ZALISHAM

S18 NUR ALIEYA SHAFIQAH BINTI AMRAN

S19 NUR FARHANA BT. ZAIDI

S20 NUR SYAHIRA ALYA BINTI NUROL NIZAM

Among the 20 enrolled students, there is a rich diversity of backgrounds and interests. Each
student has selected a unique combination of courses based on their academic goals and
requirements. The mathematics courses (MAT111, MAT112, MAT113) must be taken by each
student. Similarly, ICT courses (ICT114,ICT115), English courses (ENG116, ENG117), Science
courses (PHY118, BIO119, CHM120), and Social Science courses (PSY121,SOC122). Each
student takes exactly one course from each category. It is crucial to take note of the courses taken
by students to schedule examination plans at the end of the semester. They are required to sit for
the exam of each course they take. The university must make sure the exam time slots are
well-organized to make sure no students overlap their final exam.

Table 2 shows the courses taken by each student.


Table 2. Courses taken by each student

Course S1 S2 S3 S4 S5 S6 S7 S8 S9 S1 S1 S1 S1 S14 S1 S1 S1 S S19 S20


0 1 2 3 5 6 7 1
8

MAT111 X X X X X X X

MAT112 X X X X X X X

MAT113 X X X X X X

ICT114 X X X X X X X X X X

ICT115 X X X X X X X X X X

ENG116 X X X X X X X X X X

ENG117 X X X X X X X X X X

PHY118 X X X X X X X

BIO119 X X X X X X X

CHM120 X X X X X X

PSY121 X X X X X X X X X X

SOC122 X X X X X X X X X X

In table 2, the courses taken by each student are marked with ‘X’. The adjacency matrix is
created by the provided table. The matrices explain the conflicts of the courses shared by
students. The matrix is filled with '1' signifying the conflicts and '0' means no conflict. Figure 1
below shows the adjacency matrix indicating the conflicts. The network conflict graph will be
drawn based on the adjacency matrix.
Figure 1. Adjacency matrix
The time slots for the final examination must be scheduled effectively. We utilized task
scheduling to help allocate the time slots. The conflict graph is a way to visualize the conflicts or
incompatibilities among a set of entities (courses). Each vertex in a conflict graph represents the
available courses, which are 12 courses. Based on table 2, each course is denoted as ‘A’,’B’, and
onwards. An edge between two vertices indicates that there is a conflict between the
corresponding courses. This means that the two courses cannot co-exist in the same time slots.
Figure 2 illustrates the network of the conflict graph based on figure 1 which was drawn
manually.
Figure 2. The conflict graph

Each vertex has conflicts. We created the conflict graph from the table of students' course
enrollments by representing each course as a vertex and connecting vertices that share students.
Since each student takes one course from each category (Mathematics, ICT, English, Science,
Social Science), we find conflicts by identifying pairs of courses taken by the same student. If a
student takes two courses, we draw an edge between those courses to show a scheduling conflict.
By repeating this for all students, we get a graph where edges highlight these conflicts. To
schedule exams without overlaps, we use graph coloring, assigning different colors (time slots)
to adjacent courses. The chromatic number of the graph tells us the minimum number of time
slots needed to avoid conflicts, helping us create an optimal schedule. In the next section, the
findings from graph coloring will be discussed.

2.2 Graph Coloring

The minimum number of time slots is required in scheduling. It can be solved by utilizing the
graph coloring method. Vertices(courses) that share the same color can conduct the exam in the
same time slots. We can figure out the total of time slots to be conducted during the final exam
session. Figure 3 shows the implementation of graph coloring (manually) to the conflict graph.
Figure 3. The graph coloring (manually)

By solving the graph coloring manually, we managed to get 5 colors without having the
neighboring vertices having the same color. The graph requires 5 colors instead of 4 because it is
non-planar. This means that it cannot be drawn on a plane without some edges crossing, so the
Four Color Theorem does not apply. According to Cho et al. (2022), they appeal to the
correlation of conflict-free graphs and four color theorems. The conflict-free graph will have the
proper coloring such that the non isolated vertex will have different color within its
neighborhood. This concludes that the conflict-free graph follows the four color theorem. The
conflict graph does not necessarily follow the four color theorem due to its representation of
conflicting entities. In non-planar graphs, more complex connectivity patterns may appear such
as complete subgraphs 𝐾5 or other complex structures that require additional colors. Based on
the graph, it becomes clear that an additional color is necessary to prevent adjacent vertices from
having the same color. Hence, a total of 5 colors is needed.
(they discussed and proved where a non-planar graph does not follow four color theorem due to
its conflicting entities unlike the planar graph. They make a conclusion, where conflict graph
may need additional colors to prevent adjacent vertices from having the same color.)
The 5 colors that we obtain represent 5 distinct time slots. We begin by assigning colors to each
vertex in the graph. The vertices A, B, and C are labeled with a dark red color, indicating that
they can be scheduled together. The vertices G and K are coloured yellow, representing another
time slot. The vertices D and L are pink, while H,I, and J are cyan. Lastly, the vertices E and F
are labeled with a purple color. Every color corresponds to a distinct time slot, ensuring that
exams within the same slot do not overlap. By grouping the vertices with the same color, we
form a table of lists on the time slot (vertices) for the exam schedule that can be conducted
simultaneously without any scheduling conflicts.

Table 3. The time schedule of the final exam

TIME SLOT COURSE

8:00 AM to 10:00 AM MAT111, MAT112, MAT113

10:00 AM to 12:00 PM ENG117, PSY121

12:00 PM to 2:00 PM ICT114, SOC122

2:00 PM to 4:00 PM PHY118, BIO119, CHM120

4:00 PM to 6:00 PM ICT115, ENG116

2.3 Graph Coloring using Python

We solve this graph colouring problem using Python to see if the answer we obtain aligns with
the graph colouring that we have done manually. Before we run the python coding, we list out
the graph edges which we obtain from the graph based on a line connecting two vertices. Then,
we run the python coding with the list of edges.

# A class to represent a graph object


class Graph:
def __init__(self, edges, n):
self.adjList = [[] for _ in range(n)]
# add edges to the undirected graph
for (src, dest) in edges:
self.adjList[src].append(dest)
self.adjList[dest].append(src)

# Function to assign colors to vertices of a graph


def colorGraph(graph, n):

# keep track of the color assigned to each vertex


result = {}

# assign a color to vertex one by one


for u in range(n):

# check colors of adjacent vertices of `u` and store them in a set


assigned = set([result.get(i) for i in graph.adjList[u] if i in
result])

# check for the first free color


color = 1
for c in assigned:
if color != c:
break
color = color + 1

# assign vertex `u` the first available color


result[u] = color

for v in range(n):
print(f'Color assigned to vertex {chr(v + ord("A"))} is
{colors[result[v]]}')

# Greedy coloring of a graph


if __name__ == '__main__':

# Add more colors for graphs with many more vertices


colors = ['', 'BLUE', 'GREEN', 'RED', 'YELLOW', 'ORANGE', 'PINK',
'BLACK', 'BROWN', 'WHITE', 'PURPLE', 'VOILET']

# List of graph edges with letters as vertex labels


edges = [
('A', 'D'), ('A', 'F'), ('A', 'H'), ('A', 'K'), ('A', 'E'), ('A',
'G'), ('A', 'L'),
('B', 'E'), ('B', 'G'), ('B', 'I'), ('B', 'L'), ('B', 'D'), ('B',
'F'), ('B', 'K'),
('C', 'D'), ('C', 'F'), ('C', 'J'), ('C', 'K'), ('C', 'E'), ('C',
'G'), ('C', 'L'),
('D', 'F'), ('D', 'H'), ('D', 'K'), ('D', 'I'), ('D', 'J'),
('E', 'G'), ('E', 'H'), ('E', 'I'), ('E', 'J'), ('E', 'L'),
('F', 'H'), ('F', 'I'), ('F', 'J'), ('F', 'K'),
('G', 'H'), ('G', 'I'), ('G', 'J'), ('G', 'L'),
('H', 'K'), ('H', 'L'),
('I', 'K'), ('I', 'L'),
('J', 'K'), ('J', 'L')
]

# Map letters to numbers


letter_to_index = {chr(i + ord('A')): i for i in range(12)}
edges = [(letter_to_index[src], letter_to_index[dest]) for (src, dest)
in edges]

# total number of nodes in the graph


n = 12

# build a graph from the given edges


graph = Graph(edges, n)

# color graph using the greedy algorithm


colorGraph(graph, n)

Flow of the python coding:

1. Graph Initialization:
○ A graph object is represented by a class named ‘Graph’.
○ Initialization of the adjacency list ‘adjList’ for the graph is done by the
constructor ‘__init__’. For every vertex in the list, there are empty lists.
○ The edges provided are added to the undirected graph by appending adjacent
vertices to each other’s adjacency lists.
2. Color Assignment:
○ The ‘colorGraph’ function assigns colors to the vertices of the graph.
○ A dictionary called ‘result’ is used to track the color assigned to each vertex.
○ For each vertex ‘u’, it determines the colors of every adjacent vertex and stores
them in a set called ‘assigned’.
○ Next, it selects the first color (starting from 1) that is not already used for a
vertex's neighbour.
○ The color assigned to vertex ‘u’ stored in the ‘result’ dictionary.
3. Greedy Coloring Algorithm:
○ The algorithm uses a greedy approach to color the vertices.
○ It applies color assignments based on adjacent vertices, iterating over all vertices.
○ The goal is to minimize the number of colors used while maintaining a difference
in color between adjacent.
4. Main Execution:
○ The function (if__name__==’__main__’ :) initiates entry point for execution.
○ The letters "A" through "L," which correspond to numerical indices, are used to
indicate the graph edges.
○ The ‘Graph’ class and the provided edges are used to construct the graph.
○ Using the greedy algorithm, the ‘colorGraph’ function is called to color the graph.

Output obtain from python coding:

Color assigned to vertex A is BLUE

Color assigned to vertex B is BLUE

Color assigned to vertex C is BLUE

Color assigned to vertex D is GREEN

Color assigned to vertex E is GREEN


Color assigned to vertex F is RED

Color assigned to vertex G is RED

Color assigned to vertex H is YELLOW

Color assigned to vertex I is YELLOW

Color assigned to vertex J is YELLOW

Color assigned to vertex K is ORANGE

Color assigned to vertex L is ORANGE

The Figure 4 below is colored according to the output of the python.

Figure 4. The vertex coloring according to python output

Based on the results from the Python script, we can see that the results are exactly the same as
the results from drawing the graph manually. Both methods conclude that 5 time slots are needed
to fit all 12 exams without overlapping. This ensures that all students are able to attend their
examinations in order to fulfill the requirement for passing the semester.
We modify the python coding from above so that the python color the graph by itself.

import networkx as nx
import matplotlib.pyplot as plt

# A class to represent a graph object


class Graph:
def __init__(self, edges):
# Initialize an adjacency list using a dictionary
self.adjList = {}
for edge in edges:
src, dest = edge
if src not in self.adjList:
self.adjList[src] = []
if dest not in self.adjList:
self.adjList[dest] = []
self.adjList[src].append(dest)
self.adjList[dest].append(src)

# Function to assign colors to vertices of a graph


def colorGraph(graph):
# keep track of the color assigned to each vertex
result = {}
# assign a color to vertex one by one
for u in graph.adjList.keys():
# check colors of adjacent vertices of `u` and store them in a set
assigned = set([result.get(i) for i in graph.adjList[u] if i in
result])
# check for the first free color
color = 1
for c in assigned:
if color != c:
break
color = color + 1
# assign vertex `u` the first available color
result[u] = color
return result

# Function to draw the graph with colored vertices


def drawColoredGraph(graph, colors):
G = nx.Graph()

# Add nodes with alphabetic labels


G.add_nodes_from(graph.adjList.keys())

# Add edges with alphabetic labels


for src, neighbors in graph.adjList.items():
for dest in neighbors:
if (src, dest) not in G.edges() and (dest, src) not in
G.edges(): # to avoid adding edges twice
G.add_edge(src, dest)

# Draw graph with colored nodes


pos = nx.spring_layout(G) # positions for all nodes
node_colors = [colors[node] for node in G.nodes()]
nx.draw(G, pos, with_labels=True, node_color=node_colors,
node_size=500, font_size=10, font_color='white',
cmap=plt.cm.tab10) # using tab10 colormap for consistent
coloring

# Draw node labels with assigned colors


node_labels = {node: f"{node}\n({colors[node]})" for node in
G.nodes()}
nx.draw_networkx_labels(G, pos, labels=node_labels, font_size=12)

# Display the graph


plt.title('Graph Coloring')
plt.show()

if __name__ == '__main__':
# List of graph edges with alphabetic labels
edges = [
('A', 'D'), ('A', 'F'), ('A', 'H'), ('A', 'K'), ('A', 'E'), ('A',
'G'), ('A', 'L'),
('B', 'E'), ('B', 'G'), ('B', 'I'), ('B', 'L'), ('B', 'D'), ('B',
'F'), ('B', 'K'),
('C', 'D'), ('C', 'F'), ('C', 'J'), ('C', 'K'), ('C', 'E'), ('C',
'G'), ('C', 'L'),
('D', 'F'), ('D', 'H'), ('D', 'K'), ('D', 'I'), ('D', 'J'),
('E', 'G'), ('E', 'H'), ('E', 'I'), ('E', 'J'), ('E', 'L'),
('F', 'H'), ('F', 'I'), ('F', 'J'), ('F', 'K'),
('G', 'H'), ('G', 'I'), ('G', 'J'), ('G', 'L'),
('H', 'K'), ('H', 'L'),
('I', 'K'), ('I', 'L'),
('J', 'K'), ('J', 'L')
]

# build a graph from the given edges


graph = Graph(edges)

# color graph using the greedy algorithm


colors = colorGraph(graph)

# print colors assigned to vertices


for v in sorted(colors.keys()):
print(f'Color assigned to vertex {v} is {colors[v]}')

# Define colors as per your specified output


color_map = {
1: 'BLUE',
2: 'GREEN',
3: 'RED',
4: 'YELLOW',
5: 'ORANGE',
}

# Map colors to alphabetic labels


color_labels = {node: color_map[colors[node]] for node in colors}

# draw colored graph


drawColoredGraph(graph, color_labels)

# draw colored graph


drawColoredGraph(graph, color_labels)
We use the ‘drawColoredGraph’ function to visualize the graph with colored vertices. It creates a
NetworkX graph (G) and adds vertices and edges based on the adjacency list. The spring layout
algorithm 'pos' is used to determine the layout. Then the vertices colors are assigned based on the
provided ‘colors’ dictionary. We assign the color based on the output we obtain from the
previous Python coding. We assign the values to colors as follows: Blue is assigned the value 1,
Green is assigned the value 2, Red is assigned the value 3, Yellow is assigned the value 4, and
Orange is assigned the value 5. Then, the graph is displayed using the Matplotlib function.

Output obtain from python coding:

Color assigned to vertex A is 1

Color assigned to vertex B is 1

Color assigned to vertex C is 1

Color assigned to vertex D is 2

Color assigned to vertex E is 2

Color assigned to vertex F is 3

Color assigned to vertex G is 3

Color assigned to vertex H is 4

Color assigned to vertex I is 4

Color assigned to vertex J is 4

Color assigned to vertex K is 5

Color assigned to vertex L is 5


Figure 5. The conflict graph and the vertex coloring by python

Table 4. The time schedule of the final exam based on python coding

TIME SLOT COURSE

8:00 AM to 10:00 AM MAT111, MAT112, MAT113

10:00 AM to 12:00 PM ICT114, ICT115

12:00 PM to 2:00 PM ENG116, ENG117

2:00 PM to 4:00 PM PHY118, BIO119, CHM120

4:00 PM to 6:00 PM PSY121, SOC122


3.0 CONCLUSION
In conclusion, graph coloring was a good way to model the problem of scheduling tests
so that they don't clash. Each course was shown as a vertex, and edges showed which students
were enrolled in more than one course. By giving each course a different color of time slot, the
main goal was to make sure that no student had tests at the same time. We used the greedy
method for vertex coloring to give colors to vertices in a way that made sure that adjacent
vertices (which show courses taken by the same student) got different colors. Both the manual
coloring and the Python version gave the same results, which confirmed the graph's chromatic
number, which is the smallest number of time slots needed to avoid any problems.

This activity shows how useful coloring graphs can be in academic scheduling by
showing how well it can solve difficult scheduling problems. Finding the chromatic number gave
us a clear way to solve the test scheduling problem, making sure that it was fair and possible for
all students. The implementation and analysis that went well show how important graph theory is
for solving problems in the real world. They also give us useful information about both the ideas
behind graph theory and how they can be used in real life. This project not only showed how
useful graph coloring can be in school, but it also showed how useful graph theory is in many
other areas, like logistics, network design, and resource allocation.
QUESTION 2
1.0 INTRODUCTION

Graph theory is a fundamental branch of mathematics that explores the properties and
applications of graphs, which are structures consisting of vertices (or nodes) and edges that
connect pairs of vertices. This theory is pivotal in various fields such as computer science,
biology, and social sciences due to its ability to model complex relationships and interactions.

The map coloring problem is one of graph theory's most exciting challenges. This challenge
requires coloring the regions of a map so that no two adjacent parts have the same color. The
answer to this problem introduces the concept of the chromatic number, which is the smallest
number of colors required to produce the desired coloring with no contiguous regions sharing the
same color. Planar graphs are very beneficial for tackling the map coloring problem. Planar
graphs can be drawn on a plane with no edges crossing, making them perfect for representing
maps.

This project focuses on planar graphs and their duals. A dual graph is created by
translating an initial planar graph's faces into vertices and shared borders into edges. This
transformation makes it easier to analyze and solve coloring problems by giving you a new
viewpoint on how areas interact. The project entails implementing the greedy method in Python
to color the vertices of the dual graph with the goal of using as few colors as feasible. This
practical application of graph theory will demonstrate the usefulness of heuristic algorithms in
tackling complicated coloring problems, as well as providing insights into system optimization.

1.1 OBJECTIVES
a. To determine the dual graph of a map with at least 20 regions by precisely mapping
common boundaries to edges and areas to vertices.
b. To implement the greedy algorithm in Python to color the vertices of the dual graph and
calculate the chromatic number, which is the smallest amount of colors required.
c. To apply vertex coloring to the original map to guarantee that adjacent regions are
different colors, and document the procedure and results in a thorough report.
1.2 LITERATURE REVIEW

Graph Theory

Graph theory is an important branch of mathematics that studies graphs as mathematical


structures that efficiently express relationships between objects. There has been a significant
amount of research contributions made to the field of graph theory, which has produced a large
body of literature. The Königsberg bridge problem was tackled by Leonhard Euler in 1735,
marking the beginning of graph theory. The task at hand was to locate a path that, without going
around twice, crossed each of Königsberg's seven bridges exactly once. The first theorem in
graph theory was established by Euler's proof, which depended on the placement of the bridges
rather than the actual terrain. This significant event set the stage for the growth of graph theory
as a field of study in mathematics. Graph theory is widely used in numerous fields.

Four Color Theory

Guo and Tang (2023) say that the four-color theorem has been an important part of graph
theory ever since it was first proposed in 1852. It says that any planar map can be colored with
no more than four colors, and no two nearby areas can share the same color. Compared to earlier
proofs, like the computer-assisted proof by Appel and Haken (1976), Guo and Tang's proof
stands out because it uses strict combinatorial methods and doesn't rely too much on computers.
Advanced combinatorial methods are used in Guo and Tang's approach. For example, planar
graphs are broken up into smaller subgraphs that are easier to handle and color. They take the
coloring strategy from these easier parts and apply it to more complicated graphs using a
systematic reduction method along with induction. This paper shows that the four-color theory is
true because the graph can't be drawn with five colors.
Graph Coloring

Graph coloring is a method of assigning labels, commonly known as colors, to elements


of a graph subject to certain constraints. The primary goal is to ensure that no two adjacent
vertices share the same color. This approach has been extensively used to solve scheduling
problems, including the task of scheduling exams. By representing exams as vertices and
conflicts between exams (e.g., students enrolled in both exams) as edges, the problem of
scheduling exams without conflicts can be transformed into a graph coloring problem. The
chromatic number of the graph, which is the smallest number of colors needed to color the graph,
corresponds to the minimum number of time slots required for the exams (Arifin et al., 2022).

Greedy Theorem

West (2021) explains the Greedy Theorem, which is commonly used in algorithms for
problems like graph coloring and minimum spanning trees. The theorem outlines how a greedy
approach—one that makes the locally optimal choice at each step—can lead to an overall
optimal solution in certain cases. West details various applications of the Greedy Theorem in
graph theory, including its role in efficiently solving problems by prioritizing immediate benefits,
which helps simplify complex tasks without compromising on the overall solution quality. This
resource is ideal for those looking to understand both the theoretical foundations and practical
implementations of greedy algorithms in graph theory.

Euler’s Theorem

Euler's Theorem provides a fundamental relationship in graph theory for planar graphs,
expressed by the Euler characteristic formula |𝑉| − |𝐸| + |𝐹| = 2 where |𝑉| represents the
number of vertices, |𝐸| the number of edges, and |𝐹| the number of faces in a graph 𝐺. This
formula holds true regardless of the specific arrangement of the planar graph, as long as the
edges and vertices are repositioned without crossing each other. This invariance underscores the
theorem's robustness and significance, ensuring that the relationship between vertices, edges, and
faces remains accurate despite any non-intersecting transformations of the graph (Pickard, 2022).
This principle is crucial in various applications such as network design and topology, where
maintaining planar characteristics is essential.
2.0 METHODOLOGY
2.1 Data and Descriptions of data
1. Type of graph:
- The graph used in this report is planar. Planar graphs can be drawn on a plane
without any edges crossing each other.
2. Sources of data:
- The graph is collected from:
https://www.researchgate.net/figure/A-maximal-outerplanar-graph-shown-in-blac
k-Its-dual-is-shown-in-red-Color-figure-online_fig1_366320943
- The graph comes from a selected map with 22 regions, it was chosen to illustrate
the map coloring process.
3. Vertices:
- The original graph has 22 vertices. Each vertex in the original graph represents a
region on the map.
4. Edges:
- The original graph has 30 edges. It can be calculated using Euler’s formula,
(𝑉 − 𝐸 + 𝐹 = 2).
- For edges, it represents the adjacency relationships between regions.
- The origin graph shows an edge between two vertices if the corresponding regions
share a boundary.
- In the dual graph, an edge exists between two vertices if the corresponding
regions in the original map are adjacent.
5. Dual graph:
- The dual graph is constructed from the original planar graph by converting each
face into a vertex and each shared boundary into an edge.
6. Chromatic number:
- (χ(𝐺)) the minimum number of colors needed to color the graph without two
adjacent vertices sharing the same color.
7. Greedy algorithm:
- They are used to color the dual graph’s vertices, aiming to use the fewest colors
possible.
8. Python implementation:
- The coloring process is automated using the Python code.

Figure 6. Original graph( Planar graph)

2.2 Explanation of steps using Python


➔ Step 1: Adjacent Matrix
From the original graph, a vertice is allocated at each of faces. The total of faces in the
original graph is 22. Then, the dual graph is created and an adjacent matrix of 22 × 22
is formed by counting the total of edges at each of the vertices.
Figure 7. Adjacency matrix of dual graph

➔ Step 2: Count the degree and define the possible color. Each item in the list "𝑑𝑒𝑔𝑟𝑒𝑒"
indicates the degree (number of edges) of the corresponding node. The numbers in each
row of the adjacency matrix are added up to get this number. This is a dictionary called
"colorDict" that gives each node a list of different colors. Initially, each node can be
colored “Blue”, “Pink”, “Green”, “Brown”.

➔ Step 3: Sort the node. The list "𝑠𝑜𝑟𝑡𝑒𝑑_𝑛𝑜𝑑𝑒𝑠" is made up of nodes that are arranged in
decreasing order by their degree. When coloring, nodes with higher degrees are looked at
first.

➔ Step 4: The main process. “theSolution” is a dictionary to store the final color of each
node. The outer loop goes through each of the 𝑛 nodes in 𝑠𝑜𝑟𝑡𝑒𝑑_𝑛𝑜𝑑𝑒𝑠. The greedy
method is used here by going through the nodes in order of their degrees, giving them the
first colour that is available, and then updating the possible colours of nodes that are next
to them.”setTheColor” retrieves the list of possible colors for node 𝑛. The first color in
the list is assigned to n (𝑠𝑒𝑡𝑇ℎ𝑒𝑐𝑜𝑙𝑜𝑟[0]). “adjacentNode” gets the adjacency list of node
𝑛. The inner loop iterates through each element in the adjacency list. If an edge exists
(𝑎𝑑𝑗𝑎𝑐𝑒𝑛𝑡𝑁𝑜𝑑𝑒[𝑖] == 1 or 𝑎𝑑𝑗𝑎𝑐𝑒𝑛𝑡𝑁𝑜𝑑𝑒[𝑖] == 2), it identifies the neighbor node.
To stop the same colour from being used for neighboring nodes, it removes the assigned
colour from the neighbor's list of available colours if it is present.

➔ Step 5: Print the Solution. The solution is presented with the node names listed
alphabetically. Every node is shown together with the colour it is assigned.

2.3 Python coding and output

# Adjacency Matrix
G = [
[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
[0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,2],
[0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,2],
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2],
[0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2],
[2,1,1,1,0,2,1,0,1,1,0,1,1,0,2,2,1,1,2,1,2,0]
]

# Initialize the name of nodes


nodes = "abcdefghijklmnopqrstuv"
t_ = {nodes[i]: i for i in range(len(G))}

# Count degree of all nodes


degree = [sum(row) for row in G]

# Initialize the possible colors


colorDict = {node: ["Blue", "Pink", "Green","Brown"] for node in nodes}

# Sort nodes based on their degrees (highest first)


sorted_nodes = sorted(nodes, key=lambda x: degree[t_[x]], reverse=True)

# The main process for assigning colors


theSolution = {}
for n in sorted_nodes:
setTheColor = colorDict[n]
theSolution[n] = setTheColor[0]
adjacentNode = G[t_[n]]
for j in range(len(adjacentNode)):
if adjacentNode[j] == 1 or adjacentNode[j] == 2:
neighbor = nodes[j]
if setTheColor[0] in colorDict[neighbor]:
colorDict[neighbor].remove(setTheColor[0])

# Print the solution


for node, color in sorted(theSolution.items()):
print(f"Node {node} = {color}")
Output:

Node a = Pink
Node b = Green
Node c = Pink
Node d = Green
Node e = Blue
Node f = Pink
Node g = Green
Node h = Pink
Node i = Green
Node j = Pink
Node k = Blue
Node l = Pink
Node m = Green
Node n = Blue
Node o = Pink
Node p = Pink
Node q = Pink
Node r = Green
Node s = Pink
Node t = Pink
Node u = Green
Node v = Blue

From the outputs, we can determined the chromatic number, 𝜒(𝐺) is 3. The chromatic number is
the minimum number of colors required to color the vertices of a graph where no two adjacent
vertices share the same color. In the following section, the graph chosen is colored according to
output in python.

2.4 Vertex coloring

We implemented the python output to color the vertex of the dual graph. The vertex coloring is a
method of assigning colors to the vertices of a graph such that no two neighboring vertices share
the same color. In planar graphs, the concept of using dual graphs aids in solving the vertex
coloring problem. The dual graph simplifies the coloring process of complex planar graphs. The
application of four color theorems in this problem describes the behavior of planar graphs. This
theorem states that any planar graph can only have four vertex colors at most. This is related to
the fact that the dual graph of a planar graph can be colored with four colors.
First, the faces or vertices of the original graph G (planar graph) are identified. We observed that
graph G contains 22 faces including the outer region. Each of face in the graph G becomes a
vertex in the dual graph, denoted as G*. We added the edges between the vertices of graph G*.
The two faces share a common edge in graph G corresponds to the vertices in graph G* to be
connected by an edge. Each vertex in graph G* must cross all the existing edges of the graph G.
The vertices of G* with the edges of graph G facing the outer region will be connected to the
vertices v. Figure 8 shows the dual graph of the planar graph that we chose.

Figure 8. Dual graph

The vertex coloring is done by ensuring there is no neighbor vertices of graph G* share the same
color. The vertex coloring of the graph G* corresponds to the faces of the graph G. Figure 9
shows the vertex coloring of the dual graph.
Figure 9. Vertex coloring of dual graph
2.5 Graph Coloring
Graph coloring is specific to planar graphs. The main objective is to color the faces of the graph
G such that no two faces sharing a common edge have the same color. In this report, we apply
Greedy Algorithms into the coding in Python. We use the vertex coloring information from the
dual graph to guide the graph coloring of the original graph. The presence of three colors
observed in the graph coloring corresponds to the vertex coloring in the previous figure. The
Figure 1 shows the result of our graph coloring.

Figure 10. Graph Coloring

In the next section, the results will be discussed in detail.


3.0 RESULTS

The following vertex coloring outcomes were attained by applying the greedy algorithm to the
map coloring problem: Green for nodes 𝑏, 𝑑, 𝑔, 𝑖, 𝑚, 𝑟, 𝑢; Blue for nodes 𝑒, 𝑘, 𝑛, 𝑣; and Pink
for nodes 𝑎, 𝑐, 𝑓, ℎ, 𝑗, 𝑙, 𝑜, 𝑝, 𝑞, 𝑠, 𝑡. According to these findings, the chromatic number, or
χ(𝐺), is three. This means that three different colors are enough to color the map so that no two
nearby locations have the same color. This result is consistent with the four-color theorem, which
states that a planar graph can only have four colors. The acquired coloring ensures that adjacent
sections have unique colors, proving the greedy algorithm's effectiveness in addressing this
problem for a planar graph with 22 regions. These findings enable the original map to be
suitably colored with the three identified colors, thus providing visual evidence of the algorithm's
efficacy. This accurate coloring of the map demonstrates the algorithm's usefulness in obtaining
an ideal and effective solution in addition to validating the algorithm.

Applying the greedy algorithm to the map coloring problem gave us the following vertex colors:
Green for nodes𝑏, 𝑑, 𝑔, 𝑖, 𝑚, 𝑟 and 𝑢; Blue for nodes 𝑒, 𝑘, 𝑛, 𝑣; and Pink for nodes
𝑎, 𝑐, 𝑓, ℎ, 𝑗, 𝑙, 𝑜, 𝑝, 𝑞, 𝑠, 𝑡. This shows that the chromatic number, χ(𝐺), is three, meaning we
only need three colors to ensure no two adjacent regions share the same color. This coloring
proves the algorithm's effectiveness for a planar graph with 22 regions. Using Euler's formula
(𝑉 − 𝐸 + 𝐹) = 2, we can verify the properties of the original and dual graphs. In the original
graph, vertices represent regions, and edges represent shared boundaries. In the dual graph, each
face in the original graph becomes a vertex, ensuring planarity.

These results allow us to color the original map accurately with the three identified colors,
visually confirming the algorithm's success. This proper coloring validates the algorithm and
demonstrates its practical application in solving map coloring problems efficiently and optimally.

The equation (𝑉 − 𝐸 + 𝐹) = 2

Data original:

22 − 42 + 22 = 2

Euler’s formula for the data original is verified.

Dual Graph:

22 − 42 + 22 = 2

Euler’s formula for dual graph is verified.


4.0 CONCLUSION

In conclusion, we have successfully verified the validity of the four color theorem by
demonstrating our ability to color the selected planar graph using a minimum of three colors
(chromatic number = 3), while ensuring that no two neighboring vertices have the same color for
graph coloring. The same principle applies to vertex coloring, where we ensure that no
consecutive vertices share the same color. Using the greedy technique to color maps is effective
and economical. We found that the chromatic number, 𝜒(𝐺), is three by assigning colors to each
node. This suggests that just three colors are needed to prevent nearby faces from sharing colors.
The 22-region planar graph coloring proves the algorithm's viability. We tested the characteristics
of the original and dual graphs using Euler's formula (V - E + F = 2), confirming our approach's
planarity and accuracy. In summary, the greedy algorithm solved the map coloring issue
optimally, demonstrating its real-world implications.
5.0 REFERENCES

Ann, M. J. (2022). Application of Vertex Coloring (Doctoral dissertation, St Teresa's College

(Autonomous), Ernakulam).

Arifin, S., Muktyas, I. B., Maki, W. F. A., & Aziz, M. K. B. M. (2022). Graph Coloring program

of exam scheduling modeling based on bitwise coloring algorithm using Python. Journal

of Computer Sciences/Journal of Computer Science, 18(1), 26–32.

https://doi.org/10.3844/jcssp.2022.26.32

Burke, E. K., Mareček, J., Parkes, A. J., & Rudová, H. (2010). A supernodal formulation of

vertex coloring with applications in course timetabling. Annals of Operations Research,

179, 105-130.

Cho, E. K., Choi, I., Kwon, H., & Park, B. (2022). Proper conflict-free coloring of sparse graphs.

arXiv preprint arXiv:2203.16390.

Ganguli, R., & Roy, S. (2017). A study on course timetable scheduling using graph coloring

approach. international journal of computational and applied mathematics, 12(2),

469-485.

Guo, T., & Tang, Z. (2023). A proof of Four-Color theorem. Journal of Applied Mathematics and

Physics, 11(04), 1194–1199. https://doi.org/10.4236/jamp.2023.114078

Havet, F. (2007). Graph colouring and applications. Habilitationa diriger des recherches,

Université de Nice-Sophia Antipolis.

Idris, A., Ismail, B., Jama’are, M. U., & Zaharaddeen, D. (2018). Vertex Colorings of Graph and

Some of Their Applications in Promoting Global Competitiveness for National Growth

and Productivity. Machine Learning Research, 3(2), 28-32.

https://www.sciencepublishinggroup.com/article/10.11648/j.mlr.20180302.13
Intro. (2003). Science, 302(5652), 1871e1871. https://doi.org/10.1126/science.302.5652.1871e

Pickard, J. (2022, February 12). Graph Theory: Euler’s Formula for Planar Graphs - Math

Simplified - Medium. Medium.

https://medium.com/math-simplified/graph-theory-eulers-theorem-for-planar-graphs-207

6bbdb59be

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy