1. Bài toán tô màu đồ thị:
2. #include <iostream>
3. #include <iomanip>
4. #include <fstream>
5. using namespace std;
6. #define MAX 100
7. int n,a[MAX][MAX+3];
8. void swap(int& a,int& b){
9. int tmp=a;
10. a=b;
11. b=tmp;
12. }
13. void ToMau(){
14. for(int i=1;i<n;i++){
15. for(int j=i+1;j<=n;j++){
16. if(a[j][n+1]>a[i][n+1]){
17. swap(a[i][n+1],a[j][n+1]);
18. swap(a[i][n+2],a[j][n+2]);
19. }
20. }
21. }
22. int mau=0;
23. int pop;
24. for(int i=1;!pop || i==1;i++){
25. int j;
26. for(j=1;j<=n;j++) if (!a[j][n+3]) break;
27. a[j][n+3]=++mau;
28. for(int k=j+1;k<=n;k++){
29. int dem=0;
30. for(int l=1;l<=n;l++){
31. if (a[a[k][n+2]][l]){
60.
61. ToMau();
62.
63. cout<<"/XXX/";
64. for(int i=1;i<=n+3;i++){
65. cout<<setw(5)<<i;
66. }
67. cout<<endl;
68. for(int i=1;i<=n;i++){
69. cout<<setw(5)<<i;
70. for(int j=1;j<=n+3;j++){
71. cout<<setw(5)<<a[i][j];
72. }
73. cout<<endl;
74. }
75. }
Thuật toán Depth First Search với ngăn xếp
1. #include <iostream>
2. #include <iomanip>
3. #include <fstream>
4. #include <stack>
5. using namespace std;
6. #define MAX 100
7. int n,a[MAX][MAX],chuaxet[MAX];
8. void DepthFirstSearch(int v){
9. stack<int> q;
10. q.push(v);
11. chuaxet[v]=0;
12. do{
13. int u=q.top();
44. cout<<endl;
45. }
46. cout<<endl<<"Duyet DFS:"<<endl;
47. for(int i=1;i<=n;i++){
48. if(chuaxet[i]) DepthFirstSearch(i);
49. }
50. }
b) Lý thuyết đồ thị Euler:
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Text;
5. using System.IO;
6. namespace VD1
7. {
8. class Program
9. {
10. public const int MAX = 100;
11. static void Main(string[] args)
12. {
13. //Doc du lieu va chuan bi so lieu
14. StreamReader objReader = new StreamReader("D:\\baitap.txt");
15. int n = int .Parse(objReader.ReadLine());
16. string[] arrChuoi = new string[MAX];
17. int[,] arrSo = new int[MAX, MAX];
18. int[,] arrSoV = new int[MAX, MAX];
19. int[] Deg = new int[MAX];
20. int[] DegV = new int[MAX];
21. string textLine = "";
22. int i, j, VecM = 0;
53. Console.Write(VecM + " ");
54. int Vec = VecM;
55. for (i = 0; i < sc; i++)
56. {
57. int DegG=0;
58. int Good = 0;
59. for (j = 0; j < n; j++)
60. if (arrSoV[Vec, j] == 1)
61. {
62. DegG = DegV[j];
63. Good = j;
64. break;
65. }
66. for (j = 0; j < n; j++)
67. if (arrSoV[Vec, j] == 1 && DegV[j] > DegG)
68. {
69. DegG = DegV[j];
70. Good = j;
71. }
72. // Tranh dinh xuat phat, ket thuc
73. if (Good == VecM)
74. for (j = 0; j < n; j++)
75. if (arrSoV[Vec, j] == 1 && DegV[j] == DegV[Good] && j!=Good)
76. Good = j;
77. Console.Write(Good + " ");
78. DegV[Good] ;
79. DegV[Vec] ;
80. arrSoV[Vec, Good] = 0;
81. Vec = Good;