Bài toán tô màu đồ thị:2... static void Mainstring[] args 12.. StreamReader objReader = new StreamReader"D:\\baitap.txt"; 15.
Trang 11 Bài toán tô màu đồ thị:
2 #include <iostream>
3 #include <iomanip>
4 #include <fstream>
5 using namespace std;
7 int n,a[MAX][MAX+3];
8 void swap(int& a,int& b){
9 int tmp=a;
10 a=b;
11 b=tmp;
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]){
32 int kt=1;
33 for(int m=1;m<=n;m++){
34 if(a<IMG border=0 src="images/nhom/
smod.gif"><FONT color=violet><B>quangiahopan</B></FONT>[n+2]==l && a<IMG bord er=0 src="images/nhom/smod.gif"><FONT color=violet><B>quangiahopan</B></ FONT>[n+3]==mau) kt=0;
35 }
36 if(kt) dem++;
37 }
38 }
39 if(dem==a[k][n+1] && !a[k][n+3]) a[k][n+3]=mau;
40 }
41 pop=1;
42 for(int i=1;i<=n;i++){
43 pop*=a[i][n+3];
44 }
45 }
47 int main(){
48 ifstream filein("D:\\baitap.txt");
49 filein>>n;
50 for(int i=1;i<=n;i++){
51 int sum=0;
52 for(int j=1;j<=n;j++){
53 filein>>a[i][j];
54 sum+=a[i][j];
55 }
56 a[i][n+1]=sum;
57 a[i][n+2]=i;
58 a[i][n+3]=0;
59 }
61 ToMau();
63 cout<<"/XXX/";
64 for(int i=1;i<=n+3;i++){
65 cout<<setw(5)<<i;
66 }
Trang 267 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 }
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;
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();
14 q.pop();
15 cout<<u<<" ";
16 for(int i=1;i<=n;i++){
17 if(aKhách[i] && chuaxet[i]){//phan tu hang u, cot i
18 q.push(i);
19 chuaxet[i]=0;
20 }
21 }
22 }while(!q.empty());
25 int main(){
26 ifstream filein("D:\\baitap.txt");
27 filein>>n;
28 for(int i=1;i<=n;i++){
29 for(int j=1;j<=n;j++){
30 filein>>a[i][j];
31 }
32 }
33 cout<<"/XXX/";
34 for(int i=1;i<=n;i++){
35 cout<<setw(5)<<i;
36 chuaxet[i]=1;
37 }
38 cout<<endl;
39 for(int i=1;i<=n;i++){
40 cout<<setw(5)<<i;
41 for(int j=1;j<=n;j++){
42 cout<<setw(5)<<a[i][j];
43 }
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 }
b) Lý thuyết đồ thị Euler:
Trang 31 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;
23 int sc = 0;
24 for (i = 0; i < n; i++)
25 {
26 textLine = objReader.ReadLine() + "\r\n";
27 arrChuoi = textLine.Split(' ');
28 int sum = 0;
29 for (j = 0; j < n; j++)
30 {
31 arrSo[i, j] = int.Parse(arrChuoi[j]);
32 arrSoV[i, j] = arrSo[i, j];
33 sum += arrSo[i, j];
34 }
35 Deg[i] = sum;
36 DegV[i] = sum;
37 sc += sum;
38 if (Deg[i] > Deg[VecM]) VecM = i;
39 }
40 sc = sc / 2;
41 objReader.Close();
42 //In du lieu da doc duoc
43 Console.Write(" ");
44 for (i = 0; i < n; i++) Console.Write((i + 1) + " ");
45 Console.WriteLine();
46 for (i = 0; i < n; i++)
47 {
48 Console.Write((i + 1) + " ");
49 for (j = 0; j < n; j++) Console.Write(arrSo[i, j] + " ");
50 Console.WriteLine();
51 }
52 //Di thoi
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)
Trang 468 {
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;
82 }
83 Console.Write(VecM + " ");
84 Console.ReadKey();
85 }
86 }
87 }