0% found this document useful (0 votes)
161 views25 pages

BACKTRACKING

The document discusses the backtracking algorithm and its application to several combinatorial problems including the n-queens problem, sum of subsets problem, and m-coloring problem. It provides pseudocode for the general backtracking approach, which starts with an empty partial solution, iteratively adds options while checking constraints, and backtracks when partial solutions cannot be extended to a full solution. For each problem, it gives the algorithm, time and space complexity analysis, and example code implementing the backtracking approach.

Uploaded by

Amisha 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)
161 views25 pages

BACKTRACKING

The document discusses the backtracking algorithm and its application to several combinatorial problems including the n-queens problem, sum of subsets problem, and m-coloring problem. It provides pseudocode for the general backtracking approach, which starts with an empty partial solution, iteratively adds options while checking constraints, and backtracks when partial solutions cannot be extended to a full solution. For each problem, it gives the algorithm, time and space complexity analysis, and example code implementing the backtracking approach.

Uploaded by

Amisha 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/ 25

Experiment No.

3: BACKTRACKING Date:18/04/2023

Aim:Write a C program to implement the following program using Dynamic programming


a. n queens problem
b. sum of subsets
c. m colouring problem
d. Hamiltonian cycle
e. 0/1 knapsack problem

THEORY:
Backtracking is a general algorithmic technique used to systematically search for solutions to
combinatorial problems. It is based on the idea of incrementally building a solution by
exploring all possible options and backtracking when a partial solution cannot be extended to
a complete solution.
The backtracking algorithm works as follows:
1. Start with an empty partial solution.
2. Choose the next element or option that can be added to the partial solution.
3. Check if the chosen element violates any constraints or conditions. If it does, discard it and
go back to step 2 (backtrack).
4. If the chosen element is valid, add it to the partial solution.
5. If the partial solution is complete (meets all the requirements), record it as a valid solution.
6. Recursively repeat steps 2-5 with the updated partial solution.
7. If all options have been explored, backtrack to the previous step and continue exploring
other options.
Backtracking is particularly useful for solving problems with a large search space where a
systematic exploration of all possible solutions is necessary. By eliminating invalid options
early in the process, backtracking reduces the number of unnecessary computations, making
it more efficient than exhaustive enumeration.
One of the key components of a backtracking algorithm is the decision-making process at
each step. The selection of the next option to consider can greatly affect the efficiency and
correctness of the algorithm. Careful consideration of constraints and heuristics can guide the
decision-making process and lead to more effective backtracking.
Backtracking is commonly used to solve various combinatorial problems, such as the n-
queens problem, Sudoku puzzles, graph coloring, subset sum problem, and many other
constraint satisfaction problems. It is a fundamental technique in algorithm design and is
often combined with other techniques, such as pruning or memoization, to further optimize
the search process.
However, it is important to note that backtracking can have an exponential time complexity
in the worst case, as it may need to explore all possible combinations. In such cases,
additional optimizations or alternative algorithms may be required to improve the
performance.

RollNo. 211105036 |Page


a) N QUEENS PROBLEM
Date:18/04/2023
Problem Statement:

Write a c program to implement N-QUEENS algorithm for 4 queens .

Algorithm:

TIME AND SPACE COMPLEXITY :

Time complexity :
The time complexity of the N-Queens problem when solved using backtracking depends on
the specific implementation and the size of the chessboard (N).

In the N-Queens problem, the goal is to place N queens on an N×N chessboard in such a way
that no two queens threaten each other. Backtracking is a common approach to solve this

RollNo. 211105036 |Page


problem, where the algorithm explores different configurations of queen placements,
backtracking when conflicts are encountered.

In the worst case, the backtracking algorithm for the N-Queens problem has an exponential
time complexity. This is because the algorithm explores all possible configurations of queen
placements, resulting in a branching factor of N at each level of the recursion. The worst-
case scenario occurs when there are no restrictions on the placements or symmetries in the
chessboard.

Therefore, the worst-case time complexity of the backtracking algorithm for the N-Queens
problem is O(N!), where N is the size of the chessboard. This exponential time complexity
arises from the exponential growth in the number of recursive calls and the branching factor
at each level.

SPACE COMPLEXITY :
The space complexity of the N-Queens problem when solved using backtracking depends on
the specific implementation and how the algorithm stores and tracks data during the
recursion.

In a basic backtracking implementation, the space complexity is determined by the maximum


depth of the recursion, which corresponds to the number of rows or queens in the chessboard
(N). This is because each recursive call creates a new stack frame, which includes local
variables, parameters, and return addresses.

Additionally, the algorithm may use additional space to store the current state of the queen
placements and track the valid positions. The space required for this state tracking typically
grows linearly with the number of queens or the size of the chessboard, so it contributes
O(N) space complexity.

Therefore, the overall space complexity of the basic backtracking implementation for the N-
Queens problem is O(N) in the worst case.

CODE :
/*code for N queen’s problem*/

#include <stdio.h>
#define N 20
int M[N],i,j;

int Place(int k, int i)


{
for ( j = 0; j < k; j++)
{
if (M[j] == i)
return 0;
else if (abs(i - M[j]) == abs(k - j))
return 0;

RollNo. 211105036 |Page


}
return 1;
}

void print(int n)
{int j;
printf("\n\n");
for ( i = 1; i <= n; i++)
{
printf("\t%d", i);
}
for ( i = 1; i <= n; i++)
{
printf("\n\n%d", i);
for (j = 1; j <= n; j++)
{
if (M[i] == j)
printf("\tQ");
else
printf("\t_");
}
};
printf("\n\n");
}

void nqueen(int k, int n)


{int i;
for ( i= 1; i<= n; i++)
{
if (Place(k, i))
{
M[k] = i;
if (k == n)
{
print(n);
return;
}
else
nqueen(k + 1, n);
}
}
}

int main()
{
int n;
printf("Enter the size(n):");
scanf("%d", &n);
nqueen(1, n);
return 0;
}

OUTPUT :

C:\Users\Nidhi Shanbhag \Downloads\madf CODES\NQueens_problem.exe


Enter the size(n):4

RollNo. 211105036 |Page


1 2 3 4

1 _ Q _ _

2 _ _ _ Q

3 Q _ _ _

4 _ _ Q _

1 2 3 4

1 _ _ Q _

2 Q _ _ _

3 _ _ _ Q

4 _ Q _ _

--------------------------------
Process exited after 1.314 seconds with return value 0
Press any key to continue . . .

RollNo. 211105036 |Page


b) SUM OF SUBSETS
Date:18/04/2023
Problem Statement:

Write a c program to implement SUM OF SUBSETS algorithm for the following problem:
Given ‘n’ weights, find the combinations of these weights such that the total weight of the
subset equal to ‘m’ (where n and m are given)

W = {5, 10, 12, 13, 15, 18} m = 30

Algorithm

Time and space complexity :

Time complexity :
The time complexity of the Subset Sum problem depends on the specific algorithm used to
solve it. There are several approaches to solving Subset Sum, and the time complexity can
vary depending on the chosen method.
The backtracking approach explores different combinations of elements to construct subsets
and backtracks when the sum exceeds the target or all elements have been considered. In the
worst case, the backtracking approach has an exponential time complexity of O(2^n), where
n is the number of elements in the input set. However, the actual running time can be
improved with pruning techniques and heuristics.

RollNo. 211105036 |Page


It's important to note that the Subset Sum problem is NP-complete, which means that no
known efficient polynomial-time algorithm exists for solving it in the general case.
Therefore, the time complexity is typically exponential, unless there are specific constraints
or structures in the input that allow for more efficient solutions.
Space complexity :
The space complexity of the Subset Sum problem when solved using backtracking depends
on the specific implementation and how the algorithm stores and tracks data during the
recursion.
In a basic backtracking implementation, the space complexity is determined by the maximum
depth of the recursion, which corresponds to the number of elements in the input set. This is
because each recursive call creates a new stack frame, which includes local variables,
parameters, and return addresses.
Additionally, the algorithm may use additional space to store the current state of the subset
being constructed during the recursion. The space required for this subset tracking typically
grows linearly with the number of elements, so it contributes O(n) space complexity, where n
is the number of elements in the input set.
Therefore, the overall space complexity of the basic backtracking implementation for the
Subset Sum problem is O(n) in the worst case.

Code

/*code for sum of subsets Algorithm */


#include <stdio.h>
#define N 20

int n, m,i;

void print(int w[N], int k, int x[N])


{
printf("( ");
for ( i = 1; i <= k; i++)
printf("%d, ", x[i]);
printf(") ie. sum of the subset ( ");
for (i = 1; i <= k; i++)
{
if (x[i] == 1)
printf("%d, ", w[i]);
}
printf(")\n");
}

void sumofsub(int s, int k, int r, int w[], int x[])


{
if (k > n)
return;
x[k] = 1;

if (s + w[k] == m)
print(w, k, x);
else if ((s + w[k] + w[k + 1]) <= m)
sumofsub(s + w[k], k + 1, r, w, x);
if (((s + r - w[k]) >= m) && (s + w[k + 1] <= m))

RollNo. 211105036 |Page


{
x[k] = 0;
sumofsub(s, k + 1, r, w, x);
}
}

int main()
{
int r = 0;

printf("Enter the no. of elements: ");


scanf("%d", &n);
int w[n + 1], x[n + 1];

printf("Enter the elements:\n");


for ( i = 1; i <= n; i++)
{
x[i] = 0;
scanf("%d", &w[i]);
r += w[i];
}

printf("Enter max weight: ");


scanf("%d", &m);
sumofsub(0, 1, r, w, x);
return 0;
}

OUTPUT :
C:\Users\ Nidhi Shanbhag \Downloads\madf CODES\sumofsubsets.exe
Enter the number of elements: 6
Enter the elements
5
10
12
13
15
18
Enter max weight: 30
( 1, 1, 0, 0, 1, ) ie. sum of the subset ( 5, 10, 15, )
( 1, 0, 1, 1, ) ie. sum of the subset ( 5, 12, 13, )
( 0, 0, 1, 0, 0, 1, ) ie. sum of the subset ( 12, 18, )
--------------------------------
Process exited after 6.7 seconds with return value 0
Press any key to continue . . .

RollNo. 211105036 |Page


c) M colouring problem
Date:28/04/2023
Problem Statement:

Write a c program to implement M-COLOURING ALGORITHM on following problem:

Given below are the time slots of taxis. Only one taxi is available for the service at a time.
Find the most efficient way to schedule the appointment:

10

ALGORITHM :

RollNo. 211105036 |Page


Time and space complexity :

Time complexity :
The time complexity of the m-coloring problem when solved using backtracking depends on
the specific implementation and characteristics of the graph being considered.
In the m-coloring problem, the goal is to color the vertices of a graph with at most m colors,
such that no adjacent vertices have the same color. The backtracking algorithm explores
different color assignments for each vertex and backtracks when it encounters a conflict.
In the worst case, the backtracking algorithm for the m-coloring problem has an exponential
time complexity. This is because the algorithm explores all possible color assignments for
each vertex, resulting in a branching factor of m at each level of the recursion. The worst-
case scenario occurs when every vertex has m possible colors to choose from.
Therefore, the worst-case time complexity of the backtracking algorithm for the m-coloring
problem is O(m^n), where n is the number of vertices in the graph. This exponential time
complexity arises from the exponential growth in the number of recursive calls and the
branching factor at each level.
Space complexity :
The space complexity of the m-coloring problem when solved using backtracking depends on
the specific implementation and how the algorithm stores and tracks data during the
recursion.
In a basic backtracking implementation, the space complexity is determined by the maximum
depth of the recursion, which corresponds to the number of vertices in the graph. This is
because each recursive call creates a new stack frame, which includes local variables,
parameters, and return addresses.
Additionally, the algorithm may use additional space to store the current state of the coloring
and track the colors assigned to each vertex. The space required for this coloring state
tracking typically grows linearly with the number of vertices, so it contributes O(n) space
complexity.

RollNo. 211105036 |Page


Therefore, the overall space complexity of the basic backtracking implementation for the m-
coloring problem is O(n) in the worst case.

Code:

/*code for M colouring Algorithm */

#include <stdio.h>
#define N 20

int G[N][N], count = 0,i,j;

int c[7] = {'V', 'I', 'B', 'G', 'Y', 'O', 'R'};

void nextval(int x[N], int nodes, int colors, int k)


{
do
{
x[k] = (x[k] + 1) % (colors + 1);
if (!x[k])
return;
int j;
for (j = 1; j <= nodes; j++)
{
if ((G[k][j] != 0) && (x[k] == x[j]))
break;
}
if (j == nodes + 1)
return;
} while (1);
}

void show(int x[N], int nodes)


{
printf("%d.\t", ++count);
for (i = 1; i <= nodes; i++)
printf("%c ", c[x[i]]);
printf("\n");
}

void color(int x[N], int nodes, int colors, int k)


{
do
{
nextval(x, nodes, colors, k);
if (!x[k])
return;
if (k == nodes)
show(x, nodes);
else
color(x, nodes, colors, k + 1);

} while (1);
}

RollNo. 211105036 |Page


int main()
{
int nodes, edges, colors, o, d, x[N];
printf("Enter the number of nodes: "), scanf("%d",&nodes);

for ( i = 1; i <= nodes; i++)


for ( j = 1; j <= nodes; j++)
G[i][j] = 0;

printf("Enter the number of edges: "),scanf("%d",&edges) ;

printf("Enter origin and destination:\n");


for (i = 0; i < edges; i++)
scanf("%d",&o) ,scanf("%d",&d), G[o][d] = G[d][o] = 1;

printf("Enter the number of colors: "), scanf("%d",&colors);


for ( i = 1; i <= nodes; i++)
x[i] = 0;
printf("\n\n");
printf("The following are the possible ways to color the nodes of
the graph:");
printf("\n\n");
color(x, nodes, colors, 1);
return 0;
}

OUTPUT :

C:\Users\ Nidhi Shanbhag \Downloads\madf CODES\mcolouring.exe


Enter the number of nodes: 10 15. I B G B I G B I G I
Enter the number of edges: 10 16. I B G B I G B I G B
Enter origin and destination 17. I B G B I G B B I B
1 2 18. I B G B I G B B I G
1 3 19. I B G B I G B B G I
1 4 20. I B G B I G B B G B
2 3 21. I B G B I G B G I B
3 4 22. I B G B I G B G I G
5 6 23. I B G B I G B G B I
5 7 24. I B G B I G B G B G
6 7 25. I B G B B I G I B I
8 9 26. I B G B B I G I B G
9 10 27. I B G B B I G I G I
Enter the number of colors: 3 28. I B G B B I G I G B
29. I B G B B I G B I B
30. I B G B B I G B I G
The possible ways to color the 31. I B G B B I G B G I
nodes of the graph are 32. I B G B B I G B G B
33. I B G B B I G G I B
34. I B G B B I G G I G
1. I B G B I B G I B I 35. I B G B B I G G B I
2. I B G B I B G I B G 36. I B G B B I G G B G
3. I B G B I B G I G I 37. I B G B B G I I B I
4. I B G B I B G I G B 38. I B G B B G I I B G
5. I B G B I B G B I B 39. I B G B B G I I G I
6. I B G B I B G B I G 40. I B G B B G I I G B
7. I B G B I B G B G I 41. I B G B B G I B I B
8. I B G B I B G B G B 42. I B G B B G I B I G
9. I B G B I B G G I B 43. I B G B B G I B G I
10. I B G B I B G G I G 44. I B G B B G I B G B
11. I B G B I B G G B I 45. I B G B B G I G I B
12. I B G B I B G G B G 46. I B G B B G I G I G
13. I B G B I G B I B I 47. I B G B B G I G B I
14. I B G B I G B I B G 48. I B G B B G I G B G

RollNo. 211105036 |Page


49. I B G B G I B I B I 118. I G B G B G I G I G
50. I B G B G I B I B G 119. I G B G B G I G B I
51. I B G B G I B I G I 120. I G B G B G I G B G
52. I B G B G I B I G B 121. I G B G G I B I B I
53. I B G B G I B B I B 122. I G B G G I B I B G
54. I B G B G I B B I G 123. I G B G G I B I G I
55. I B G B G I B B G I 124. I G B G G I B I G B
56. I B G B G I B B G B 125. I G B G G I B B I B
57. I B G B G I B G I B 126. I G B G G I B B I G
58. I B G B G I B G I G 127. I G B G G I B B G I
59. I B G B G I B G B I 128. I G B G G I B B G B
60. I B G B G I B G B G 129. I G B G G I B G I B
61. I B G B G B I I B I 130. I G B G G I B G I G
62. I B G B G B I I B G 131. I G B G G I B G B I
63. I B G B G B I I G I 132. I G B G G I B G B G
64. I B G B G B I I G B 133. I G B G G B I I B I
65. I B G B G B I B I B 134. I G B G G B I I B G
66. I B G B G B I B I G 135. I G B G G B I I G I
67. I B G B G B I B G I 136. I G B G G B I I G B
68. I B G B G B I B G B 137. I G B G G B I B I B
69. I B G B G B I G I B 138. I G B G G B I B I G
70. I B G B G B I G I G 139. I G B G G B I B G I
71. I B G B G B I G B I 140. I G B G G B I B G B
72. I B G B G B I G B G 141. I G B G G B I G I B
73. I G B G I B G I B I 142. I G B G G B I G I G
74. I G B G I B G I B G 143. I G B G G B I G B I
75. I G B G I B G I G I 144. I G B G G B I G B G
76. I G B G I B G I G B 145. B I G I I B G I B I
77. I G B G I B G B I B 146. B I G I I B G I B G
78. I G B G I B G B I G 147. B I G I I B G I G I
79. I G B G I B G B G I 148. B I G I I B G I G B
80. I G B G I B G B G B 149. B I G I I B G B I B
81. I G B G I B G G I B 150. B I G I I B G B I G
82. I G B G I B G G I G 151. B I G I I B G B G I
83. I G B G I B G G B I 152. B I G I I B G B G B
84. I G B G I B G G B G 153. B I G I I B G G I B
85. I G B G I G B I B I 154. B I G I I B G G I G
86. I G B G I G B I B G 155. B I G I I B G G B I
87. I G B G I G B I G I 156. B I G I I B G G B G
88. I G B G I G B I G B 157. B I G I I G B I B I
89. I G B G I G B B I B 158. B I G I I G B I B G
90. I G B G I G B B I G 159. B I G I I G B I G I
91. I G B G I G B B G I 160. B I G I I G B I G B
92. I G B G I G B B G B 161. B I G I I G B B I B
93. I G B G I G B G I B 162. B I G I I G B B I G
94. I G B G I G B G I G 163. B I G I I G B B G I
95. I G B G I G B G B I 164. B I G I I G B B G B
96. I G B G I G B G B G 165. B I G I I G B G I B
97. I G B G B I G I B I 166. B I G I I G B G I G
98. I G B G B I G I B G 167. B I G I I G B G B I
99. I G B G B I G I G I 168. B I G I I G B G B G
100. I G B G B I G I G B 169. B I G I B I G I B I
101. I G B G B I G B I B 170. B I G I B I G I B G
102. I G B G B I G B I G 171. B I G I B I G I G I
103. I G B G B I G B G I 172. B I G I B I G I G B
104. I G B G B I G B G B 173. B I G I B I G B I B
105. I G B G B I G G I B 174. B I G I B I G B I G
106. I G B G B I G G I G 175. B I G I B I G B G I
107. I G B G B I G G B I 176. B I G I B I G B G B
108. I G B G B I G G B G 177. B I G I B I G G I B
109. I G B G B G I I B I 178. B I G I B I G G I G
110. I G B G B G I I B G 179. B I G I B I G G B I
111. I G B G B G I I G I 180. B I G I B I G G B G
112. I G B G B G I I G B 181. B I G I B G I I B I
113. I G B G B G I B I B 182. B I G I B G I I B G
114. I G B G B G I B I G 183. B I G I B G I I G I
115. I G B G B G I B G I 184. B I G I B G I I G B
116. I G B G B G I B G B 185. B I G I B G I B I B
117. I G B G B G I G I B 186. B I G I B G I B I G

RollNo. 211105036 |Page


187. B I G I B G I B G I 256. B G I G B G I I G B
188. B I G I B G I B G B 257. B G I G B G I B I B
189. B I G I B G I G I B 258. B G I G B G I B I G
190. B I G I B G I G I G 259. B G I G B G I B G I
191. B I G I B G I G B I 260. B G I G B G I B G B
192. B I G I B G I G B G 261. B G I G B G I G I B
193. B I G I G I B I B I 262. B G I G B G I G I G
194. B I G I G I B I B G 263. B G I G B G I G B I
195. B I G I G I B I G I 264. B G I G B G I G B G
196. B I G I G I B I G B 265. B G I G G I B I B I
197. B I G I G I B B I B 266. B G I G G I B I B G
198. B I G I G I B B I G 267. B G I G G I B I G I
199. B I G I G I B B G I 268. B G I G G I B I G B
200. B I G I G I B B G B 269. B G I G G I B B I B
201. B I G I G I B G I B 270. B G I G G I B B I G
202. B I G I G I B G I G 271. B G I G G I B B G I
203. B I G I G I B G B I 272. B G I G G I B B G B
204. B I G I G I B G B G 273. B G I G G I B G I B
205. B I G I G B I I B I 274. B G I G G I B G I G
206. B I G I G B I I B G 275. B G I G G I B G B I
207. B I G I G B I I G I 276. B G I G G I B G B G
208. B I G I G B I I G B 277. B G I G G B I I B I
209. B I G I G B I B I B 278. B G I G G B I I B G
210. B I G I G B I B I G 279. B G I G G B I I G I
211. B I G I G B I B G I 280. B G I G G B I I G B
212. B I G I G B I B G B 281. B G I G G B I B I B
213. B I G I G B I G I B 282. B G I G G B I B I G
214. B I G I G B I G I G 283. B G I G G B I B G I
215. B I G I G B I G B I 284. B G I G G B I B G B
216. B I G I G B I G B G 285. B G I G G B I G I B
217. B G I G I B G I B I 286. B G I G G B I G I G
218. B G I G I B G I B G 287. B G I G G B I G B I
219. B G I G I B G I G I 288. B G I G G B I G B G
220. B G I G I B G I G B 289. G I B I I B G I B I
221. B G I G I B G B I B 290. G I B I I B G I B G
222. B G I G I B G B I G 291. G I B I I B G I G I
223. B G I G I B G B G I 292. G I B I I B G I G B
224. B G I G I B G B G B 293. G I B I I B G B I B
225. B G I G I B G G I B 294. G I B I I B G B I G
226. B G I G I B G G I G 295. G I B I I B G B G I
227. B G I G I B G G B I 296. G I B I I B G B G B
228. B G I G I B G G B G 297. G I B I I B G G I B
229. B G I G I G B I B I 298. G I B I I B G G I G
230. B G I G I G B I B G 299. G I B I I B G G B I
231. B G I G I G B I G I 300. G I B I I B G G B G
232. B G I G I G B I G B 301. G I B I I G B I B I
233. B G I G I G B B I B 302. G I B I I G B I B G
234. B G I G I G B B I G 303. G I B I I G B I G I
235. B G I G I G B B G I 304. G I B I I G B I G B
236. B G I G I G B B G B 305. G I B I I G B B I B
237. B G I G I G B G I B 306. G I B I I G B B I G
238. B G I G I G B G I G 307. G I B I I G B B G I
239. B G I G I G B G B I 308. G I B I I G B B G B
240. B G I G I G B G B G 309. G I B I I G B G I B
241. B G I G B I G I B I 310. G I B I I G B G I G
242. B G I G B I G I B G 311. G I B I I G B G B I
243. B G I G B I G I G I 312. G I B I I G B G B G
244. B G I G B I G I G B 313. G I B I B I G I B I
245. B G I G B I G B I B 314. G I B I B I G I B G
246. B G I G B I G B I G 315. G I B I B I G I G I
247. B G I G B I G B G I 316. G I B I B I G I G B
248. B G I G B I G B G B 317. G I B I B I G B I B
249. B G I G B I G G I B 318. G I B I B I G B I G
250. B G I G B I G G I G 319. G I B I B I G B G I
251. B G I G B I G G B I 320. G I B I B I G B G B
252. B G I G B I G G B G 321. G I B I B I G G I B
253. B G I G B G I I B I 322. G I B I B I G G I G
254. B G I G B G I I B G 323. G I B I B I G G B I
255. B G I G B G I I G I 324. G I B I B I G G B G

RollNo. 211105036 |Page


325. G I B I B G I I B I 386. G B I B B I G I B G
326. G I B I B G I I B G 387. G B I B B I G I G I
327. G I B I B G I I G I 388. G B I B B I G I G B
328. G I B I B G I I G B 389. G B I B B I G B I B
329. G I B I B G I B I B 390. G B I B B I G B I G
330. G I B I B G I B I G 391. G B I B B I G B G I
331. G I B I B G I B G I 392. G B I B B I G B G B
332. G I B I B G I B G B 393. G B I B B I G G I B
333. G I B I B G I G I B 394. G B I B B I G G I G
334. G I B I B G I G I G 395. G B I B B I G G B I
335. G I B I B G I G B I 396. G B I B B I G G B G
336. G I B I B G I G B G 397. G B I B B G I I B I
337. G I B I G I B I B I 398. G B I B B G I I B G
338. G I B I G I B I B G 399. G B I B B G I I G I
339. G I B I G I B I G I 400. G B I B B G I I G B
340. G I B I G I B I G B 401. G B I B B G I B I B
341. G I B I G I B B I B 402. G B I B B G I B I G
342. G I B I G I B B I G 403. G B I B B G I B G I
343. G I B I G I B B G I 404. G B I B B G I B G B
344. G I B I G I B B G B 405. G B I B B G I G I B
345. G I B I G I B G I B 406. G B I B B G I G I G
346. G I B I G I B G I G 407. G B I B B G I G B I
347. G I B I G I B G B I 408. G B I B B G I G B G
348. G I B I G I B G B G 409. G B I B G I B I B I
349. G I B I G B I I B I 410. G B I B G I B I B G
350. G I B I G B I I B G 411. G B I B G I B I G I
351. G I B I G B I I G I 412. G B I B G I B I G B
352. G I B I G B I I G B 413. G B I B G I B B I B
353. G I B I G B I B I B 414. G B I B G I B B I G
354. G I B I G B I B I G 415. G B I B G I B B G I
355. G I B I G B I B G I 416. G B I B G I B B G B
356. G I B I G B I B G B 417. G B I B G I B G I B
357. G I B I G B I G I B 418. G B I B G I B G I G
358. G I B I G B I G I G 419. G B I B G I B G B I
359. G I B I G B I G B I 420. G B I B G I B G B G
360. G I B I G B I G B G 421. G B I B G B I I B I
361. G B I B I B G I B I 422. G B I B G B I I B G
362. G B I B I B G I B G 423. G B I B G B I I G I
363. G B I B I B G I G I 424. G B I B G B I I G B
364. G B I B I B G I G B 425. G B I B G B I B I B
365. G B I B I B G B I B 426. G B I B G B I B I G
366. G B I B I B G B I G 427. G B I B G B I B G I
367. G B I B I B G B G I 428. G B I B G B I B G B
368. G B I B I B G B G B 429. G B I B G B I G I B
369. G B I B I B G G I B 430. G B I B G B I G I G
370. G B I B I B G G I G 431. G B I B G B I G B I
371. G B I B I B G G B I 432. G B I B G B I G B G
372. G B I B I B G G B G
373. G B I B I G B I B I --------------------------------
374. G B I B I G B I B G Process exited after 6.661 seconds
375. G B I B I G B I G I with return value 0
376. G B I B I G B I G B Press any key to continue . . .
377. G B I B I G B B I B
378. G B I B I G B B I G
379. G B I B I G B B G I
380. G B I B I G B B G B
381. G B I B I G B G I B
382. G B I B I G B G I G
383. G B I B I G B G B I
384. G B I B I G B G B G
385. G B I B B I G I B I

RollNo. 211105036 |Page


d) HAMILTONIAN CYCLE
Date:28/04/2023
Problem Statement:
Write a c program to calculate all possible Hamiltonian cycle in the following graph:

Algorithm:

RollNo. 211105036 |Page


TIME AND SPACE COMPLEXITY :

Time complexity :
The time complexity of the Hamiltonian cycle problem when solved using backtracking
depends on the specific implementation and characteristics of the graph being considered.
In the worst case, the backtracking algorithm for finding a Hamiltonian cycle has an
exponential time complexity. This is because the algorithm explores all possible
permutations of the vertices to construct a cycle and checks whether each permutation
satisfies the conditions of a Hamiltonian cycle.
The number of possible permutations of n vertices is n!, which grows factorially with the
number of vertices. Therefore, the worst-case time complexity of the backtracking algorithm
for finding a Hamiltonian cycle is O(n!).

Space complexity ;
The space complexity of the Hamiltonian cycle problem when solved using backtracking depends on
the specific implementation and how the algorithm stores and tracks data during the recursion.
In a basic backtracking implementation, the space complexity is determined by the maximum depth
of the recursion, which corresponds to the number of vertices in the graph. This is because each
recursive call creates a new stack frame, which includes local variables, parameters, and return
addresses.
Additionally, the algorithm may use additional space to maintain the current path or cycle being
constructed during the recursion. The space required for this path tracking typically grows linearly
with the number of vertices, so it contributes O(n) space complexity.
Therefore, the overall space complexity of the basic backtracking implementation for the
Hamiltonian cycle problem is O(n) in the worst case.

Code

RollNo. 211105036 |Page


/*code for Hamiltonian cycles Algorithm */
#include <stdio.h>
#define N 100

int G[N][N], count = 0,i,j;

void nextvalue(int x[N], int nodes, int k)


{
do
{
x[k] = (x[k] + 1) % (nodes + 1);
if (!x[k])
return;
if (G[x[k - 1]][x[k]] != 0)
{
int j;
for (j = 1; j <= k - 1; j++)
if (x[j] == x[k])
break;
if (j == k)
if ((k < nodes) || ((k == nodes && G[x[nodes]][x[1]] !=
0)))
return;
}
} while (1);
}

void show(int x[N], int nodes)


{
printf("%d.\t", ++count);
for (i = 1; i <= nodes; i++)
printf("%d -> ", x[i]);
printf("%d", x[1]);
printf("\n");
}

void hamiltonian(int x[N], int nodes, int k)


{
do
{
nextvalue(x, nodes, k);
if (!x[k])
return;
if (k == nodes)
show(x, nodes);
else
hamiltonian(x, nodes, k + 1);
} while (1);
}

int main()
{
int nodes, edges, o, d, x[N];
printf("Enter the number of nodes: "), scanf("%d",&nodes);

RollNo. 211105036 |Page


for (i = 1; i <= nodes; i++)
for ( j = 1; j <= nodes; j++)
G[i][j] = 0;

printf("Enter the number of edges: "), scanf("%d",&edges);

printf("Enter origin and destination\n");


for (i = 0; i < edges; i++)
scanf("%d %d",&o,&d), G[o][d] = G[d][o] = 1;
int k;
printf("Enter the starting vertex: "),scanf("%d",&k);
x[1] = k;
for (i = 2; i <= nodes; i++)
x[i] = 0;
printf("\n\n");
printf("The Hamiltonian Cycles starting from node %d are:", k);
printf("\n\n");
hamiltonian(x, nodes, 2);
return 0;
}

OUTPUT :

C:\Users\ Nidhi Shanbhag \Downloads\madf CODES\hamiltonian.exe

Enter the number of nodes: 6


Enter the number of edges: 10
Enter origin and destination
1 2
2 3
1 3
1 4
4 3
4 5
2 5
3 5
4 6
5 6
Enter the starting vertex: 1

The Hamiltonian Cycles starting from node 1 are:

1. 1 -> 2 -> 3 -> 5 -> 6 -> 4 -> 1


2. 1 -> 2 -> 5 -> 6 -> 4 -> 3 -> 1
3. 1 -> 3 -> 2 -> 5 -> 6 -> 4 -> 1
4. 1 -> 3 -> 4 -> 6 -> 5 -> 2 -> 1
5. 1 -> 4 -> 6 -> 5 -> 2 -> 3 -> 1
6. 1 -> 4 -> 6 -> 5 -> 3 -> 2 -> 1

--------------------------------
Process exited after 47.15 seconds with return value 0
Press any key to continue . . .

RollNo. 211105036 |Page


e) 0/1 KNAPSACK PROBLEM
Date:02/05/2023
Problem Statement:
Write a c program to implement 0/1 knapsack using dynamic programming on the following knapsack :

N=4

M=8

p1,p2,p3,p4={3,5,6,10}

w1,w2,w3,w4}={2,3,4,5,}

Algorithm;

RollNo. 211105036 |Page


Time and space complexity :
Time complexity ;
The time complexity of the 0/1 knapsack problem when solved using backtracking depends
on the specific implementation. In the worst case, the time complexity is exponential,
O(2^n), where n is the number of items to choose from.
In the backtracking approach, the algorithm explores all possible combinations of items by
recursively considering whether each item should be included in the knapsack or not. This
results in a binary tree with a depth of n, where each node represents an item and has two
branches (representing whether the item is included or excluded).
Since there are two choices at each level of the recursion (to include or exclude an item), the
number of nodes in the tree is 2^n. The algorithm visits each node to evaluate the
corresponding combination, resulting in a time complexity of O(2^n) in the worst case.
Space complexity :
The space complexity of the 0/1 knapsack problem when solved using backtracking depends
on the specific implementation and how the algorithm stores and tracks data during the
recursion.
In a basic backtracking implementation, the space complexity is determined by the maximum
depth of the recursion, which is equal to the number of items, n. This is because each
recursive call creates a new stack frame, which includes local variables, parameters, and
return addresses.
Additionally, the algorithm may use additional space to store the current state of the
knapsack and track the best solution found so far. The space required for this state tracking
typically grows linearly with the number of items, so it contributes O(n) space complexity.
Code

RollNo. 211105036 |Page


/*code for 0/1 knapsack Algorithm */
#include<stdio.h>
#include<stdlib.h>
int p[100], w[100];
int x[100],y[100];
int m, n,fp=0,fw=0;
void knapsack(int,int,int);
float bound(int,int,int);
void knapsack(int k,int cp,int cw)
{
if(cw+w[k]<=m)
{
y[k]=1;
if(k<n)knapsack(k+1,cp+p[k],cw+w[k]);
if((cp+p[k]>fp) && k==n)
{
fp=cp+p[k];
fw=cw+w[k];
for(int j=1;j<=k;j++)x[j]=y[j];
}
}
if(bound(cp,cw,k)>=fp)
{
y[k]=0;
if(k<n)
knapsack(k+1,cp,cw);
if((cp>fp)&& k==n)
{
fp=cp;
fw=cw;
for(int j=1;j<=k;j++)
x[j]=y[j];
}
}
}
float bound(int cp,int cw,int k)
{
int b=cp,c=cw;
for(int i=k+1;i<=n;i++)
{
c=c+w[i];
if(c<m)
b=b+p[i];
else
return b+(1-(c-m)/(float)w[i])*p[i];

}
return b;
}
int main()
{
int profit=0, weight=0;
printf("Enter number of objects[n]: ");
scanf("%d",&n);
printf("Enter sack size[m]: ");

RollNo. 211105036 |Page


scanf("%d",&m);
printf("\nEnter profits :\n");
for(int i=1; i<=n; i++)
{
printf("P[%d]: ",i);
scanf("%d",&p[i]);
}
printf("\nEnter weights : \n");
for(int i=1; i<=n; i++)
{
printf("W[%d]: ",i);
scanf("%d",&w[i]);
x[i] = 0;
}
knapsack(0,0,1);
printf("\nSolution Vector: (");
for(int i=1; i<=n; i++)
{
profit+=p[i]*x[i];
weight+=w[i]*x[i];
printf("%d",x[i]);
if(i<n)
{
printf(",");
}
}
printf(")\n");
printf("\nMAX Profit = %d \n",profit);
printf("Weight Occupied = %d ",weight);

return 0;
}
OUTPUT :
C:\Users\ Nidhi Shanbhag \Downloads\madf CODES\knapsack_bt.exe
Enter number of objects[n]: 4
Enter sack size[m]: 8
Enter profits :
P[1]: 3
P[2]: 5
P[3]: 6
P[4]: 10

Enter weights :
W[1]: 2
W[2]: 3
W[3]: 4
W[4]: 5

Solution Vector: (1,0,0,1)

MAX Profit = 13
Weight Occupied = 7
--------------------------------
Process exited after 19.19 seconds with return value 0
Press any key to continue . . .

RollNo. 211105036 |Page


CONCLUSION:
The Backtracking method was studied.

The programs for:

(a) N-QUEENS PROBLEM

(b) SUM OF SUBSETS

(c) M-COLOURING PROBLEM

(d) HAMILTONIAN CYCLE

(e) 0/1 KNAPSACK PROBLEM

were studied and implemented successfully

RollNo. 211105036 |Page


RollNo. 211105036 |Page

You might also like