Skip to content

Commit 7d53634

Browse files
committed
Refactor graph coloring algorithm for improved efficiency
2 parents bb8e257 + 52cc347 commit 7d53634

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

color.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,69 @@ def graph_coloring(self, m):
3939
[1, 0, 1, 0]]
4040
m = 3 # Number of colors
4141
g.graph_coloring(m)
42+
43+
44+
45+
46+
--------**
47+
g1={
48+
0:[1,2],
49+
1:[2,3,0],
50+
2:[3,1,0],
51+
3:[1,2,4],
52+
4:[3]
53+
}
54+
55+
# Assigns colors (starting from 0) to all
56+
# vertices and prints the assignment of colors
57+
def greedyColoring(adj, V):
58+
59+
result = [-1] * V
60+
print(result)
61+
62+
# Assign the first color to first vertex
63+
result[0] = 0;
64+
65+
66+
# A temporary array to store the available colors.
67+
# True value of available[cr] would mean that the
68+
# color cr is assigned to one of its adjacent vertices
69+
available = [False] * V
70+
print(available)
71+
72+
# Assign colors to remaining V-1 vertices
73+
for u in range(1, V): #1
74+
75+
# Process all adjacent vertices and
76+
# flag their colors as unavailable
77+
print(result)
78+
print(available)
79+
for i in adj[u]:
80+
if (result[i] != -1):
81+
available[result[i]] = True
82+
print(available)
83+
84+
# Find the first available color
85+
cr = 0
86+
while cr < V:
87+
if (available[cr] == False):
88+
break
89+
90+
cr += 1
91+
92+
# Assign the found color
93+
result[u] = cr
94+
95+
# Reset the values back to false
96+
# for the next iteration
97+
for i in adj[u]:
98+
if (result[i] != -1):
99+
available[result[i]] = False
100+
101+
# Print the result
102+
for u in range(V):
103+
print("Vertex", u, " ---> Color", result[u])
104+
105+
106+
print("Coloring of graph 1 ")
107+
greedyColoring(g1, 5)

idfs.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,60 @@ def iddfs(graph, start, target, max_depth):
3737
print(f"Found target {target} within depth {max_depth}")
3838
else:
3939
print(f"Target {target} not found within depth {max_depth}")
40+
41+
42+
43+
----------
44+
graph = {
45+
'A': ['B', 'C', 'D'],
46+
'B': ['A', 'E', 'F'],
47+
'C': ['A', 'G'],
48+
'D': ['A','H','J'],
49+
'E': ['B', 'K', 'L'],
50+
'F': ['B'],
51+
'G': ['C', 'M'],
52+
'H': ['D', 'N', 'O'],
53+
'J': ['D', 'P'],
54+
'K': ['E'],
55+
'L': ['E'],
56+
'M': ['G'],
57+
'N': ['H'],
58+
'O': ['H'],
59+
'P': ['J']
60+
}
61+
62+
63+
64+
65+
# here i am just call ids method by passing max level. then running a loop and call dls function.
66+
def ids(graph, start, max_limit):
67+
for i in range(max_limit):
68+
dls(graph, start, i)
69+
70+
71+
72+
73+
74+
75+
def dls(graph, start, limit):
76+
visited = []
77+
stack = [(start, 0)]
78+
79+
80+
while stack:
81+
current_node, current_depth = stack.pop()
82+
83+
84+
if current_depth <= limit:
85+
if current_node not in visited:
86+
visited.append(current_node)
87+
neighbors = graph[current_node]
88+
89+
90+
for neighbor in neighbors:
91+
stack.append((neighbor, current_depth + 1))
92+
print(visited)
93+
94+
95+
96+
print(ids(graph, 'A', 4))

quN.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,45 @@ def print_solution(board):
44
print()
55

66
def is_safe(board, row, col):
7+
# Check this row on the left side
78
for i in range(col):
89
if board[row][i] == 1:
910
return False
1011

11-
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
12+
# Check upper diagonal on the left side
13+
i, j = row, col
14+
while i >= 0 and j >= 0:
1215
if board[i][j] == 1:
1316
return False
17+
i -= 1
18+
j -= 1
1419

15-
for i, j in zip(range(row, len(board), 1), range(col, -1, -1)):
20+
# Check lower diagonal on the left side
21+
i, j = row, col
22+
while i < len(board) and j >= 0:
1623
if board[i][j] == 1:
1724
return False
25+
i += 1
26+
j -= 1
1827

1928
return True
2029

2130
def solve_nq_util(board, col):
2231
if col >= len(board):
32+
print("All queens successfully placed:")
33+
print_solution(board)
2334
return True
2435

2536
for i in range(len(board)):
2637
if is_safe(board, i, col):
2738
board[i][col] = 1
39+
print(f"Placed queen at ({i}, {col}):")
40+
print_solution(board)
2841
if solve_nq_util(board, col + 1):
2942
return True
3043
board[i][col] = 0
44+
print(f"Backtracked from ({i}, {col}):")
45+
print_solution(board)
3146

3247
return False
3348

@@ -40,4 +55,8 @@ def solve_nq(n):
4055
print_solution(board)
4156
return True
4257

58+
<<<<<<< HEAD
4359
solve_nq(8)
60+
=======
61+
solve_nq(8)
62+
>>>>>>> 52cc3477255fdcf42a9a7863d163d09ee1f7c3db

0 commit comments

Comments
 (0)