Graph 1
Graph 1
#include <iostream>
#include <fstream> #include <iostream>
using namespace std; #include <conio.h>
#define max 20 #include <fstream>
int arr[max][max]; #define MAX 10
int n; using namespace std;
char vertext[100]; char vertex[MAX];
void init() { int n, a[MAX][MAX], c[MAX], nbfs(0), bfs[MAX];
n = 0;
} void input();
void inputFile() { void output();
fstream file;
string nameFile; struct Node
int x; {
do { int info;
cout << "Nhap ten file: "; Node* link;
cin >> nameFile; };
file.open(nameFile); Node* front, * rear;
if (!file.is_open()) { void init_queue();
cout << "Ten file sai."; void init();
} void enqueue(int x);
} while (!file.is_open()); void dequeue(int& x);
file >> n; void BFS(int v);
for (int i = 0; i < n; i++) { void search_by_BFS(char t, int v);
file >> vertext[i]; void output_BFS();
} void menu();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { int main()
file >> x; {
arr[i][j] = x; menu();
} system("pause");
} return 0;
} }
void outPut() { void menu() {
cout << "\t"; int select;
for (int i = 0; i < n; i++) { do
cout << vertext[i] << "\t"; {
} system("cls");
cout << endl; cout << "Nhap su lua chon: " << endl;
for (int i = 0; i < n; i++) { cout << "1. Nhap ma tran ke tu file " <<
cout << vertext[i] << "\t"; endl;
for (int j = 0; j < n; j++) { cout << "2. Xuat ma tran ke" << endl;
cout << arr[i][j] << "\t"; cout << "3. Duyet do thi theo chieu rong
} (BFS)" << endl;
cout << endl; cout << "4. Kiem tra x ton tai trong do
} thi khong" << endl;
} cout << "0. Thoat" << endl;
int main() { cin >> select;
init(); switch (select)
inputFile(); {
outPut(); case 1:
system("pause"); input();
} system("pause");
break;
case 2: }
output(); }
system("pause"); void init_queue()
break; {
case 3: front = NULL;
BFS(0); rear = NULL;
output_BFS(); }
system("pause"); void init()
break; {
case 4: for (int i = 0; i < n; i++)
char x; c[i] = 1;
cout << "Nhap ten dinh can kiem tra: init_queue();
"; }
cin >> x; void enqueue(int x)
search_by_BFS(x,0); {
system("pause"); Node* p = new Node;
break; p->info = x;
default: p->link = NULL;
break; if (rear == NULL)
} front = p;
} while (select != 0); else
} rear->link = p;
void input() rear = p;
{ }
ifstream iFile; void dequeue(int& x)
iFile.open("Data.txt"); {
if (iFile.is_open()) if (front != NULL)
{ {
iFile >> n; Node* p = front;
for (int i = 0; i < n; i++) x = p->info;
iFile >> vertex[i]; front = front->link;
for (int i = 0; i < n; i++) if (front == NULL)
{ rear = NULL;
for (int j = 0; j < n; j++) delete p;
iFile >> a[i][j]; return;
} }
cout << "Them du lieu thanh cong!" << return;
endl; }
iFile.close(); void BFS(int v)
} {
else int w, p;
cout << "Khong the mo File!" << endl; init();
} init_queue();
void output() enqueue(v);
{ c[v] = 0;
cout << " "; while (front != NULL)
for (int i = 0; i < n; i++) {
cout << vertex[i] << " "; dequeue(p);
cout << endl; bfs[nbfs] = p;
for (int i = 0; i < n; i++) nbfs++;
{ for (w = 0; w < n; w++)
cout << vertex[i] << " "; if (c[w] && a[p][w] == 1)
for (int j = 0; j < n; j++) {
cout << a[i][j] << " "; enqueue(w);
cout << endl; c[w] = 0;
}
} //b
} int c[MAX], dfs[MAX], ndfs(0);
void search_by_BFS(char t, int v) void init();
{ struct Node
int w, p; {
int x = -1 ; int info;
init(); Node* next;
init_queue(); };
enqueue(v); Node* top;
c[v] = 0; void init_stack();
while (front != NULL) void push(int x);
{ void pop(int& x);
dequeue(p); void DFS(int s);
for (int i = 0; i < n; i++) { void search_by_dfs(char t, int s);
if (t == vertex[i]) { void output_DFS();
x = i; void menu();
}
} int main()
if (x == p) {
{ menu();
cout << vertex[x] << " co ton tai!" system("pause");
<< endl; return 0;
return; }
}
for (w = 0; w < n; w++) void menu() {
{ int select;
if (c[w] && a[p][w] == 1) do
{ {
enqueue(w); system("cls");
c[w] = 0; cout << "Nhap su lua chon: " << endl;
} cout << "1. Nhap ma tran ke tu file " <<
} endl;
} cout << "2. Xuat ma tran ke" << endl;
cout << "Dinh khong ton tai!" << endl; cout << "3. Duyet do thi theo chieu rong
} (BFS)" << endl;
void output_BFS() cout << "4. Kiem tra x ton tai trong do
{ thi khong" << endl;
for (int i = 0; i < nbfs; i++) cout << "0. Thoat" << endl;
cout << vertex[bfs[i]] << " "; cin >> select;
} switch (select)
{
case 1:
//DFS input();
#include <iostream> system("pause");
#include <conio.h> break;
#include <fstream> case 2:
#define MAX 20 cout << "============= DO THI
using namespace std; =============" << endl;
char vertex[MAX]; output();
int a[MAX][MAX], n; system("pause");
break;
//a case 3:
void input(); DFS(0);
void output(); cout << "Duyet DFS: " << endl;
output_DFS(); void init()
system("pause"); {
break; for (int i = 0; i < n; i++)
case 4: c[i] = 1;
char x; }
cout << "Tim kiem theo DFS: "; void init_stack()
cin >> x; {
search_by_dfs(x, 0); top = NULL;
system("pause"); }
break; void push(int x)
default: {
cout << "Ban da chon thoat!" << Node* p = new Node;
endl; p->info = x;
} p->next = top;
} while (select != 0); top = p;
} }
void init_DFS() { void pop(int& x)
for (int i = 0; i < n; i++) { {
if (top != NULL)
} {
} Node* p = top;
void input() x = p->info;
{ top = top->next;
ifstream iFile; delete p;
iFile.open("Data.txt"); }
if (iFile.is_open()) else
{ return;
iFile >> n; }
for (int i = 0; i < n; i++) void DFS(int s)
iFile >> vertex[i]; {
for (int i = 0; i < n; i++) init();
{ init_stack();
for (int j = 0; j < n; j++) push(s);
iFile >> a[i][j]; dfs[ndfs] = s;
} ndfs++;
cout << "Doc File thanh cong!" << endl; c[s] = 0;
iFile.close(); int v = -1, u = s;
} while (top != NULL)
else {
cout << "Khong the mo File!" << endl; if (v == n)
} pop(u);
void output() for (v = 0; v < n; v++)
{ if (a[u][v] != 0 && c[v] == 1)
cout << " "; {
for (int i = 0; i < n; i++) push(u);
cout << vertex[i] << " "; push(v);
cout << endl; dfs[ndfs] = v;
for (int i = 0; i < n; i++) ndfs++;
{ c[v] = 0;
cout << vertex[i] << " "; u = v;
for (int j = 0; j < n; j++) break;
cout << a[i][j] << " "; }
cout << endl; }
} }
} void search_by_dfs(char t, int s)
{ int n;
int w, p(0); // khai bao TapE
int x = -1; int E1[max];
init(); int E2[max];
init_stack(); int wE[max];
push(s); int nE = 0; // so phan tu tap E
c[s] = 0; // Tap T
int v = -1, u = s; int T1[max];
while (top != NULL) int T2[max];
{ int wT[max];
for (int i = 0; i < n; i++) { int nT = 0; // so phan tap T
if (t == vertex[i]) { // Ma tran ke
x = i; char vertext[max];
}
} // Kiem tra ton tai
if (x == p) int TonTai(int d, int D[], int nD) {
{ for (int i = 0; i < nD; i++) {
cout << "Dinh " << vertex[x] << " co if (D[i] == d)
ton tai!" << endl; return 1;
return; }
} return 0;
int found(0); }
for (v = 0; v < n; v++)
if (a[u][v] != 0 && c[v] == 1) void XoaViTriE(int i) {
{ for (int j = i; j < nE; j++) {
push(u); E1[j] = E1[j + 1];
push(v); E2[j] = E2[j + 1];
dfs[ndfs] = v; wE[j] = wE[j + 1];
ndfs++; }
c[v] = 0; nE--;
p = v; }
u = v; void XoaCanhE(int u, int v) {
found = 1; for (int i = 0; i < nE; i++) {
break; if (E1[i] == u && E2[i] == v) {
} XoaViTriE(i);
if (!found) break;
pop(u); }
} }
cout << "Dinh khong ton tai!" << endl; }
}
void output_DFS() void prim(int s)
{ {
for (int i = 0; i < ndfs; i++) int u = s, min, i, d1, d2;
cout << vertex[dfs[i]] << " "; while (nT < n - 1)
} {
for (int v = 0; v < n; v++)
if (a[u][v] != 0)
//Prim cực tiểu if (TonTai(v, T2, nT)
#include <iostream> == 0)
#include <fstream> {
E1[nE] = u;
using namespace std; E2[nE] = v;
wE[nE] =
#define max 100 a[u][v];
int a[max][max]; nE++;
} file.open(fileName);
for (i = 0; i < nE; i++) if (!file.is_open()) {
if (TonTai(E2[i], T2, nT) == cout << "File sai. Nhap lai:
0) \t";
{ }
min = wE[i]; } while (!file.is_open());
d1 = E1[i]; file >> n;
d2 = E2[i]; for (int i = 0; i < n; i++) {
break; file >> name;
} vertext[i] = name;
for (; i < nE; i++) }
if (TonTai(E2[i], T2, nT) == for (int i = 0; i < n; i++) {
0) for (int j = 0; j < n; j++) {
if (min > wE[i]) file >> x;
{ a[i][j] = x;
min = wE[i]; }
d1 = E1[i]; }
d2 = E2[i]; file.close();
} cout << "Da nhap tu file thanh cong" <<
T1[nT] = d1; endl;
T2[nT] = d2; }
wT[nT] = a[d1][d2]; int main() {
a[d1][d2] = 0; inputFile();
a[d2][d1] = 0; prim(0);
nT++; output();
XoaCanhE(d1, d2); cout << endl;
u = d2; system("pause");
} return 0;
} }
int main() {
init();
inputFile();
output();
return 0;
}