-
Notifications
You must be signed in to change notification settings - Fork 0
/
056.py
41 lines (31 loc) · 1.04 KB
/
056.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Problem:
Given an undirected graph represented as an adjacency matrix and an integer k, write a
function to determine whether each vertex in the graph can be colored such that no two
adjacent vertices share the same color using at most k colors.
"""
from typing import List
def can_color(adjacency_matrix: List[List[int]], k: int) -> bool:
minimum_colors_required = 0
vertices = len(adjacency_matrix)
# generating the minimum number of colors required to color the graph
for vertex in range(vertices):
colors_required_for_current_vertex = sum(adjacency_matrix[vertex]) + 1
minimum_colors_required = max(
minimum_colors_required, colors_required_for_current_vertex
)
return minimum_colors_required <= k
if __name__ == "__main__":
adjacency_matrix = [
[0, 1, 1, 1],
[1, 0, 1, 1],
[1, 1, 0, 1],
[1, 1, 1, 0],
]
print(can_color(adjacency_matrix, 4))
print(can_color(adjacency_matrix, 3))
"""
SPECS:
TIME COMPLEXITY: O(n ^ 2)
SPACE COMPLEXITY: O(1)
"""