-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAA_eval.cpp
1339 lines (1175 loc) · 38.9 KB
/
DAA_eval.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// C++ code for solving the Longest Common Subsequence (LCS) problem using Dynamic Programming:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int lcs(string s1, string s2)
{ // function to find LCS using DP
int m = s1.length();
int n = s2.length();
int dp[m + 1][n + 1]; // 2D array to store the DP table
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{ // base case
dp[i][j] = 0;
}
else if (s1[i - 1] == s2[j - 1])
{ // when the characters match
dp[i][j] = dp[i - 1][j - 1] + 1; // increment the LCS length
}
else
{ // when the characters don't match
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // choose the maximum LCS length so far
}
}
}
return dp[m][n]; // return the final LCS length
}
int main()
{
string s1, s2;
cin >> s1 >> s2; // input two strings
int ans = lcs(s1, s2); // function call to find LCS using DP
cout << ans << endl; // print the length of LCS
return 0;
}
// C++ code for solving the Matrix Chain Multiplication problem using Dynamic Programming:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int matrixChainOrder(int p[], int n)
{ // function to find the minimum number of scalar multiplications using DP
int m[n][n]; // 2D array to store the DP table
for (int i = 1; i < n; i++)
{
m[i][i] = 0; // setting the diagonal elements to 0
}
for (int L = 2; L < n; L++)
{
for (int i = 1; i < n - L + 1; i++)
{
int j = i + L - 1;
m[i][j] = INT_MAX;
for (int k = i; k < j; k++)
{
int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j])
{
m[i][j] = q;
}
}
}
}
return m[1][n - 1]; // return the minimum number of scalar multiplications
}
int main()
{
int n;
cin >> n; // input the number of matrices
int p[n + 1];
for (int i = 0; i < n + 1; i++)
{
cin >> p[i]; // input the dimensions of matrices
}
int ans = matrixChainOrder(p, n + 1); // function call to find the minimum number of scalar multiplications using DP
cout << ans << endl; // print the minimum number of scalar multiplications
return 0;
}
// C++ code for solving the 0/1 Knapsack problem using Dynamic Programming:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int knapSack(int W, int wt[], int val[], int n)
{ // function to find the maximum value that can be obtained using DP
int K[n + 1][W + 1]; // 2D array to store the DP table
for (int i = 0; i <= n; i++)
{
for (int w = 0; w <= W; w++)
{
if (i == 0 || w == 0)
{
K[i][w] = 0; // setting the base case values to 0
}
else if (wt[i - 1] <= w)
{
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]); // calculating the maximum value
}
else
{
K[i][w] = K[i - 1][w]; // copying the value from the previous row
}
}
}
return K[n][W]; // return the maximum value that can be obtained
}
int main()
{
int n, W;
cin >> n >> W; // input the number of items and the maximum weight of knapsack
int val[n], wt[n];
for (int i = 0; i < n; i++)
{
cin >> val[i] >> wt[i]; // input the values and weights of items
}
int ans = knapSack(W, wt, val, n); // function call to find the maximum value that can be obtained using DP
cout << ans << endl; // print the maximum value that can be obtained
return 0;
}
// C++ code for solving the Optimal Binary Search Tree problem using Dynamic Programming:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
float optimalBST(float keys[], float freq[], int n)
{ // function to find the cost of optimal BST using DP
float cost[n + 1][n + 1]; // 2D array to store the DP table
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
cost[i][j] = 0; // initializing all the elements of the DP table to 0
}
}
for (int i = 0; i < n; i++)
{
cost[i][i] = freq[i]; // setting the diagonal elements to frequency of ith key
}
for (int L = 2; L <= n; L++)
{
for (int i = 0; i <= n - L + 1; i++)
{
int j = i + L - 1;
cost[i][j] = FLT_MAX; // setting the initial value to infinity
for (int r = i; r <= j; r++)
{
float c = ((r > i) ? cost[i][r - 1] : 0) + ((r < j) ? cost[r + 1][j] : 0) + accumulate(freq + i, freq + j + 1, 0);
if (c < cost[i][j])
{
cost[i][j] = c;
}
}
}
}
return cost[0][n - 1]; // return the cost of optimal BST
}
int main()
{
int n;
cin >> n; // input the number of keys
float keys[n], freq[n];
for (int i = 0; i < n; i++)
{
cin >> keys[i] >> freq[i]; // input the keys and their frequencies
}
float ans = optimalBST(keys, freq, n); // function call to find the cost of optimal BST using DP
cout << ans << endl; // print the cost of optimal BST
return 0;
}
// C++ code for solving the Coin Exchange problem using Dynamic Programming:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int coinExchange(int coins[], int n, int sum)
{ // function to find the minimum number of coins required to make sum using DP
int dp[n + 1][sum + 1]; // 2D array to store the DP table
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= sum; j++)
{
if (j == 0)
{
dp[i][j] = 0; // initializing the first column to 0
}
else if (i == 0)
{
dp[i][j] = INT_MAX; // initializing the first row to infinity
}
else if (coins[i - 1] <= j)
{
dp[i][j] = min(dp[i][j - coins[i - 1]] + 1, dp[i - 1][j]); // filling the DP table using the recurrence relation
}
else
{
dp[i][j] = dp[i - 1][j]; // copying the value from the previous row if the coin value is greater than the sum
}
}
}
if (dp[n][sum] == INT_MAX)
{
return -1; // if it is not possible to make sum using the given coins, return -1
}
return dp[n][sum]; // return the minimum number of coins required to make sum
}
int main()
{
int n, sum;
cin >> n >> sum; // input the number of coins and the sum to be made
int coins[n];
for (int i = 0; i < n; i++)
{
cin >> coins[i]; // input the values of the coins
}
int ans = coinExchange(coins, n, sum); // function call to find the minimum number of coins required to make sum using DP
cout << ans << endl; // print the minimum number of coins required to make sum
return 0;
}
// C++ code for solving the Rod Cutting problem using Dynamic Programming:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int rodCutting(int price[], int n)
{ // function to find the maximum profit using DP
int dp[n + 1]; // 1D array to store the DP table
dp[0] = 0; // initializing the first element to 0
for (int i = 1; i <= n; i++)
{
int max_val = INT_MIN;
for (int j = 0; j < i; j++)
{
max_val = max(max_val, price[j] + dp[i - j - 1]); // filling the DP table using the recurrence relation
}
dp[i] = max_val; // storing the maximum profit in the DP table
}
return dp[n]; // return the maximum profit
}
int main()
{
int n;
cin >> n; // input the length of the rod
int price[n];
for (int i = 0; i < n; i++)
{
cin >> price[i]; // input the price of each piece of the rod of length i+1
}
int ans = rodCutting(price, n); // function call to find the maximum profit using DP
cout << ans << endl; // print the maximum profit
return 0;
}
// C++ code for solving the Weighted Job Sequencing problem using Dynamic Programming:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
struct Job
{ // struct to store the job details
int start, finish, profit;
};
bool compare(Job a, Job b)
{ // function to compare jobs based on finish time
return a.finish < b.finish;
}
int findLastNonConflictingJob(Job arr[], int n, int index)
{ // function to find the last non-conflicting job
for (int i = index - 1; i >= 0; i--)
{
if (arr[i].finish <= arr[index].start)
{
return i;
}
}
return -1;
}
int weightedJobSequencing(Job arr[], int n)
{ // function to find the maximum profit using DP
sort(arr, arr + n, compare); // sort the jobs based on finish time
int dp[n]; // 1D array to store the DP table
dp[0] = arr[0].profit; // initialize the first element of the array
for (int i = 1; i < n; i++)
{
int inclProfit = arr[i].profit; // profit if the current job is included
int l = findLastNonConflictingJob(arr, n, i); // find the last non-conflicting job
if (l != -1)
{
inclProfit += dp[l]; // add the profit of the last non-conflicting job
}
dp[i] = max(inclProfit, dp[i - 1]); // store the maximum profit in the DP table
}
return dp[n - 1]; // return the maximum profit
}
int main()
{
int n;
cin >> n; // input the number of jobs
Job arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i].start >> arr[i].finish >> arr[i].profit; // input the start time, finish time and profit of each job
}
int ans = weightedJobSequencing(arr, n); // function call to find the maximum profit using DP
cout << ans << endl; // print the maximum profit
return 0;
}
// C++ code for solving the N-Queens problem using backtracking:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int board[10][10]; // 2D array to store the state of the chess board
int n; // size of the chess board
void printSolution()
{ // function to print the solution
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << board[i][j] << " "; // print the state of each cell of the board
}
cout << endl;
}
}
bool isSafe(int row, int col)
{ // function to check if it is safe to place a queen at the given position
for (int i = 0; i < row; i++)
{ // check if there is any queen in the same column
if (board[i][col])
{
return false;
}
}
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
{ // check if there is any queen in the diagonal to the left
if (board[i][j])
{
return false;
}
}
for (int i = row, j = col; i >= 0 && j < n; i--, j++)
{ // check if there is any queen in the diagonal to the right
if (board[i][j])
{
return false;
}
}
return true; // return true if it is safe to place a queen
}
bool solveNQueen(int row)
{ // function to solve the N-Queens problem using backtracking
if (row == n)
{ // if all the queens are placed successfully, return true
return true;
}
for (int col = 0; col < n; col++)
{ // try placing a queen in each column of the current row
if (isSafe(row, col))
{ // check if it is safe to place a queen at the current position
board[row][col] = 1; // place the queen at the current position
if (solveNQueen(row + 1))
{ // recursively solve the N-Queens problem for the next row
return true;
}
board[row][col] = 0; // backtrack and remove the queen from the current position
}
}
return false; // if a queen cannot be placed in any column of the current row, return false
}
int main()
{
cin >> n; // input the size of the chess board
if (solveNQueen(0))
{ // function call to solve the N-Queens problem using backtracking
printSolution(); // print the solution if it exists
}
else
{
cout << "Solution does not exist." << endl; // print message if the solution does not exist
}
return 0;
}
// C++ code for solving the Sum of Subsets problem using backtracking:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int arr[10]; // array to store the input elements
int n; // size of the input array
int sum; // target sum
bool solutionExists = false; // variable to check if a solution exists
void printSolution(vector<int> &v)
{ // function to print the solution
cout << "{ ";
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " "; // print the elements that form the subset
}
cout << "}" << endl;
}
void solveSubsetSum(int index, int currentSum, vector<int> &subset)
{ // function to solve the Sum of Subsets problem using backtracking
if (currentSum == sum)
{ // if the target sum is reached, print the subset and return
printSolution(subset);
solutionExists = true;
return;
}
for (int i = index; i < n; i++)
{ // try adding each element of the input array one by one
if (currentSum + arr[i] <= sum)
{ // check if adding the current element to the sum does not exceed the target sum
subset.push_back(arr[i]); // add the current element to the subset
solveSubsetSum(i + 1, currentSum + arr[i], subset); // recursively solve the problem for the next element
subset.pop_back(); // backtrack and remove the current element from the subset
}
}
}
int main()
{
cin >> n; // input the size of the input array
for (int i = 0; i < n; i++)
{
cin >> arr[i]; // input the elements of the input array
}
cin >> sum; // input the target sum
vector<int> subset; // vector to store the elements that form the subset
solveSubsetSum(0, 0, subset); // function call to solve the Sum of Subsets problem using backtracking
if (!solutionExists)
{
cout << "Solution does not exist." << endl; // print message if the solution does not exist
}
return 0;
}
// C++ code for solving the Graph Coloring problem using backtracking:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int graph[10][10]; // adjacency matrix to represent the graph
int n; // number of vertices in the graph
int m; // number of colors available for coloring the vertices
int color[10]; // array to store the color assigned to each vertex
bool isSafe(int v, int c)
{ // function to check if it is safe to assign color c to vertex v
for (int i = 0; i < n; i++)
{
if (graph[v][i] && c == color[i])
{ // if there exists an edge between vertex v and i and the color assigned to vertex i is c, return false
return false;
}
}
return true; // otherwise, return true
}
void printColors()
{ // function to print the colors assigned to each vertex
cout << "Colors assigned to vertices:" << endl;
for (int i = 0; i < n; i++)
{
cout << "Vertex " << i << ": Color " << color[i] << endl;
}
}
bool graphColoring(int v)
{ // function to solve the Graph Coloring problem using backtracking
if (v == n)
{ // if all the vertices have been colored, print the colors and return true
printColors();
return true;
}
for (int c = 1; c <= m; c++)
{ // try assigning each color one by one to the current vertex
if (isSafe(v, c))
{ // check if it is safe to assign color c to vertex v
color[v] = c; // assign color c to vertex v
if (graphColoring(v + 1))
{ // recursively solve the problem for the next vertex
return true;
}
color[v] = 0; // backtrack and remove the color assigned to vertex v
}
}
return false; // if no color can be assigned to the current vertex, return false
}
int main()
{
cin >> n; // input the number of vertices in the graph
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> graph[i][j]; // input the adjacency matrix
}
}
cin >> m; // input the number of colors available for coloring the vertices
if (!graphColoring(0))
{
cout << "Solution does not exist." << endl; // print message if a solution does not exist
}
return 0;
}
// C++ code for solving the 0/1 Knapsack problem using backtracking:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int n; // number of items
int w[10]; // weight of each item
int p[10]; // profit of each item
int W; // maximum weight that the knapsack can hold
bool include[10]; // array to store whether an item is included in the knapsack or not
int maxProfit; // variable to store the maximum profit obtained
void knapsack(int i, int profit, int weight)
{ // function to solve the 0/1 Knapsack problem using backtracking
if (i == n)
{ // if all items have been considered
if (profit > maxProfit)
{ // if the current profit is greater than the maximum profit obtained so far
maxProfit = profit; // update the maximum profit
for (int j = 0; j < n; j++)
{
cout << include[j] << " "; // print the items included in the knapsack
}
cout << endl;
}
return;
}
if (weight + w[i] <= W)
{ // if the current item can be included in the knapsack
include[i] = true; // include the item
knapsack(i + 1, profit + p[i], weight + w[i]); // recursively solve the problem for the next item
include[i] = false; // backtrack and remove the item
}
knapsack(i + 1, profit, weight); // solve the problem by excluding the current item
}
int main()
{
cin >> n; // input the number of items
for (int i = 0; i < n; i++)
{
cin >> w[i] >> p[i]; // input the weight and profit of each item
}
cin >> W; // input the maximum weight that the knapsack can hold
maxProfit = 0; // initialize the maximum profit to 0
knapsack(0, 0, 0); // solve the 0/1 Knapsack problem using backtracking
cout << "Maximum profit: " << maxProfit << endl; // print the maximum profit obtained
return 0;
}
// C++ code for solving the Hamiltonian Cycle problem using backtracking:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int n; // number of vertices in the graph
int g[10][10]; // adjacency matrix to represent the graph
int path[10]; // array to store the current path
bool visited[10]; // array to store whether a vertex has been visited or not
bool hamiltonian; // variable to store whether a Hamiltonian Cycle exists or not
void hamiltonianCycle(int k)
{ // function to solve the Hamiltonian Cycle problem using backtracking
if (k == n)
{ // if all vertices have been included in the path
if (g[path[n - 1]][path[0]] == 1)
{ // if there is an edge from the last vertex to the first vertex
hamiltonian = true; // a Hamiltonian Cycle exists
for (int i = 0; i < n; i++)
{
cout << path[i] << " "; // print the Hamiltonian Cycle
}
cout << endl;
}
return;
}
for (int i = 0; i < n; i++)
{
if (g[path[k - 1]][i] == 1 && !visited[i])
{ // if there is an edge between the current vertex and the next vertex, and the next vertex has not been visited
path[k] = i; // add the next vertex to the path
visited[i] = true; // mark the next vertex as visited
hamiltonianCycle(k + 1); // recursively solve the problem for the next vertex
visited[i] = false; // backtrack and remove the next vertex from the path
}
}
}
int main()
{
cin >> n; // input the number of vertices in the graph
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> g[i][j]; // input the adjacency matrix of the graph
}
}
hamiltonian = false; // initialize the Hamiltonian Cycle to false
path[0] = 0; // add the first vertex to the path
visited[0] = true; // mark the first vertex as visited
hamiltonianCycle(1); // solve the Hamiltonian Cycle problem using backtracking
if (!hamiltonian)
{ // if a Hamiltonian Cycle does not exist
cout << "No Hamiltonian Cycle exists" << endl;
}
return 0;
}
// C++ code for solving the Printing N-Queen Solutions problem using backtracking:
#include <bits/stdc++.h> // header file to include all standard libraries
using namespace std;
int n; // number of queens
int col[10]; // array to store the column number of the queens in each row
int cnt; // variable to store the number of solutions found
bool isSafe(int row, int col)
{ // function to check whether it is safe to place a queen at a particular position
for (int i = 0; i < row; i++)
{
if (col == col[i] || abs(col - col[i]) == abs(row - i))
{ // if another queen is present in the same column or diagonal
return false; // it is not safe to place a queen at this position
}
}
return true; // it is safe to place a queen at this position
}
void nQueen(int row)
{ // function to solve the Printing N-Queen Solutions problem using backtracking
if (row == n)
{ // if all queens have been placed on the board
cnt++; // increment the number of solutions found
cout << "Solution " << cnt << ":" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (col[i] == j)
{
cout << "Q "; // print Q for queen
}
else
{
cout << ". "; // print . for empty cell
}
}
cout << endl;
}
cout << endl;
return;
}
for (int i = 0; i < n; i++)
{
if (isSafe(row, i))
{ // if it is safe to place a queen at this position
col[row] = i; // place the queen at this position
nQueen(row + 1); // recursively solve the problem for the next row
}
}
}
int main()
{
cin >> n; // input the number of queens
cnt = 0; // initialize the number of solutions found to zero
nQueen(0); // solve the Printing N-Queen Solutions problem using backtracking
if (cnt == 0)
{ // if no solutions were found
cout << "No solutions exist" << endl;
}
return 0;
}
// C++ code without vectors or any additional information to solve the Euler graph problem using the Hierholzer algorithm:
#include <iostream>
#include <stack>
#include <cstring>
#define MAXN 1001
#define MAXM 100001
using namespace std;
int head[MAXN], ver[MAXM], Next[MAXM];
bool vis[MAXM];
int cnt, m, n;
void add(int a, int b)
{
ver[++cnt] = b;
Next[cnt] = head[a];
head[a] = cnt;
}
void euler(int x)
{
for (int i = head[x]; i; i = Next[i])
{
if (!vis[i])
{
vis[i] = 1;
euler(ver[i]);
cout << x << "->" << ver[i] << " ";
}
}
}
int main()
{
memset(head, 0, sizeof(head));
memset(vis, 0, sizeof(vis));
cnt = 0;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
}
euler(1);
return 0;
}
// C++ code without vectors or any additional information to solve the Hamiltonian graph problem using backtracking:
#include <iostream>
#include <cstring>
#define MAXN 1001
using namespace std;
int n, m, ans = 0;
int graph[MAXN][MAXN];
int visited[MAXN];
void hamiltonian(int cur, int depth)
{
visited[cur] = depth;
if (depth == n)
{
ans = 1;
return;
}
for (int i = 1; i <= n; i++)
{
if (graph[cur][i] && !visited[i])
{
hamiltonian(i, depth + 1);
if (ans)
{
return;
}
}
}
visited[cur] = 0;
}
int main()
{
memset(graph, 0, sizeof(graph));
memset(visited, 0, sizeof(visited));
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
graph[u][v] = 1;
graph[v][u] = 1;
}
for (int i = 1; i <= n; i++)
{
hamiltonian(i, 1);
if (ans)
{
cout << "Hamiltonian path exists." << endl;
return 0;
}
}
cout << "Hamiltonian path does not exist." << endl;
return 0;
}
// C++ code for implementing topological sort using Kahn's algorithm:
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// Function to perform topological sorting
void topologicalSort(vector<int> adj[], int V)
{
// Create an array to store the in-degree of each vertex
vector<int> in_degree(V, 0);
// Traverse the graph and update the in-degree of each vertex
for (int u = 0; u < V; u++)
{
for (int v : adj[u])
{
in_degree[v]++;
}
}
// Create a queue and enqueue all vertices with in-degree 0
queue<int> q;
for (int u = 0; u < V; u++)
{
if (in_degree[u] == 0)
{
q.push(u);
}
}
// Initialize a counter to keep track of visited vertices
int count = 0;
// Create a vector to store the topological ordering of vertices
vector<int> top_order;
// Process vertices until the queue becomes empty
while (!q.empty())
{
// Dequeue a vertex from the queue
int u = q.front();
q.pop();
// Add the vertex to the topological ordering vector
top_order.push_back(u);
// Traverse all the adjacent vertices of u
for (int v : adj[u])
{
// Decrement the in-degree of each adjacent vertex
in_degree[v]--;
// If the in-degree of a vertex becomes 0, enqueue it
if (in_degree[v] == 0)
{
q.push(v);
}
}
// Increment the counter
count++;
}
// Check if there was a cycle in the graph
if (count != V)
{
cout << "There exists a cycle in the graph" << endl;
return;
}
// Print the topological ordering of vertices
for (int u : top_order)
{
cout << u << " ";
}
}
int main()
{
int V, E;
cout << "Enter the number of vertices in the graph: ";
cin >> V;
cout << "Enter the number of edges in the graph: ";
cin >> E;
// Create an adjacency list to represent the graph
vector<int> adj[V];
for (int i = 0; i < E; i++)
{
int u, v;
cout << "Enter the endpoints of edge " << i + 1 << ": ";
cin >> u >> v;
adj[u].push_back(v);
}
// Perform topological sorting
topologicalSort(adj, V);
return 0;
}
// C++ code for topological sort using Kahn's algorithm, without using vectors:
#include <iostream>
#include <queue>
#include <list>
#include <cstring>
#define MAX_NODES 10001 // maximum number of nodes
using namespace std;
int indegree[MAX_NODES]; // array to store indegrees of all nodes
list<int> adj[MAX_NODES]; // adjacency list to store graph
void topologicalSort(int n)
{
queue<int> q; // create a queue to store nodes with indegree 0
int visited[MAX_NODES]; // array to keep track of visited nodes
memset(visited, 0, sizeof visited);
for (int i = 1; i <= n; i++)
{ // loop through all nodes
if (indegree[i] == 0)
{ // if node has indegree 0
q.push(i); // add it to the queue
visited[i] = 1; // mark it as visited
}
}
while (!q.empty())
{ // while queue is not empty
int u = q.front(); // get the front node from the queue
q.pop(); // remove the front node from the queue
cout << u << " "; // print the node
for (auto v : adj[u])
{ // loop through all the neighbors of node u
indegree[v]--; // reduce the indegree of the neighbor
if (indegree[v] == 0 && !visited[v])
{ // if neighbor's indegree becomes 0 and it has not been visited before
q.push(v); // add it to the queue
visited[v] = 1; // mark it as visited
}
}
}
}
int main()
{
int n, m;
cin >> n >> m; // read number of nodes and edges
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v; // read edge (u, v)
adj[u].push_back(v); // add edge (u, v) to the adjacency list
indegree[v]++; // increment indegree of node v
}
topologicalSort(n); // call topological sort function
return 0;
}
// C++ code to perform topological sort on a given directed acyclic graph using Depth First Search (DFS) algorithm without using vectors:
#include <bits/stdc++.h>
using namespace std;