0% found this document useful (0 votes)
7 views

Graph 1

The document discusses graph algorithms including breadth-first search (BFS) and depth-first search (DFS). It includes code to represent graphs as adjacency matrices and code implementing functions for BFS like initializing a queue, enqueueing and dequeuing nodes. It also includes similar code for DFS using a stack. The code is in C++ with functions for inputting the graph, running the searches and outputting the results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Graph 1

The document discusses graph algorithms including breadth-first search (BFS) and depth-first search (DFS). It includes code to represent graphs as adjacency matrices and code implementing functions for BFS like initializing a queue, enqueueing and dequeuing nodes. It also includes similar code for DFS using a stack. The code is in C++ with functions for inputting the graph, running the searches and outputting the results.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

// Matrix array //BFS

#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;
} }

void output() //Prim cực đại


{
int tong = 0; void primMax(int s)
for (int i = 0; i < nT; i++) {
{ int u = s, maxWeight, i, d1, d2;
cout << endl << "(" << while (nT < n - 1)
vertext[T1[i]] << "," << vertext[T2[i]] << ") - {
Trong so: " << wT[i]; for (int v = 0; v < n; v++)
tong += wT[i]; if (a[u][v] != 0)
} if (TonTai(v, T2, nT)
cout << endl; == 0)
cout << "\nTong = " << tong; {
cout << endl; E1[nE] = u;
} E2[nE] = v;
wE[nE] =
// Input data from file a[u][v];
void inputFile() { nE++;
cout << "Nhap ten file: \t"; }
fstream file; for (i = 0; i < nE; i++)
int x; if (TonTai(E2[i], T2, nT) ==
char name; 0)
do {
{ maxWeight = wE[i];
string fileName; d1 = E1[i];
cin >> fileName; d2 = E2[i];
break;
} if (!file.is_open()) {
for (; i < nE; i++) cout << "Khong the mo file. Kiem tra lai
if (TonTai(E2[i], T2, nT) == ten file." << endl;
0) exit(1);
if (maxWeight < wE[i]) }
{
maxWeight = file >> n;
wE[i]; for (int i = 0; i < n; i++) {
d1 = E1[i]; file >> vertex[i];
d2 = E2[i]; }
} if (n <= 0 || n > MAX) {
T1[nT] = d1; cout << "So dinh khong hop le." << endl;
T2[nT] = d2; exit(1);
wT[nT] = a[d1][d2]; }
a[d1][d2] = 0;
a[d2][d1] = 0;
nT++; cout << "Ma tran ke cua do thi:" << endl;
XoaCanhE(d1, d2); for (int i = 0; i < n; i++) {
u = d2; for (int j = 0; j < n; j++) {
} file >> a[i][j];
} cout << a[i][j] << " ";
}
cout << endl;
//Kruskal cực tiểu }
#include <iostream>
#include <fstream> file.close();
}
const int MAX = 100;
void taoE() {
int a[MAX][MAX]; for (int i = 0; i < n; i++)
int n; for (int j = i + 1; j < n; j++)
if (a[i][j] != 0) {
int E1[MAX * MAX]; E1[nE] = i;
int E2[MAX * MAX]; E2[nE] = j;
int wE[MAX * MAX]; wE[nE] = a[i][j];
int nE = 0; nE++;
}
int T1[MAX]; }
int T2[MAX];
int wT[MAX]; void SapXepE() {
int nT = 0; for (int i = 0; i < nE; i++)
for (int j = i + 1; j < nE; j++)
char vertex[MAX]; if (wE[i] > wE[j]) {
Swap(wE[i], wE[j]);
using namespace std; Swap(E1[i], E1[j]);
Swap(E2[i], E2[j]);
void Swap(int& x, int& y) { }
int tam = x; }
x = y;
y = tam; int TonTai(int d, int D[], int nD) {
} for (int i = 0; i < nD; i++) {
if (D[i] == d)
void input() { return 1;
ifstream file("Prim.txt"); }
return 0; cin.get();
} return 0;
}
void krusal() {
int parent[MAX];
for (int i = 0; i < n; i++) {
parent[i] = i; // Khởi tạo mỗi đỉnh là //Kruskal cực đại
một cây con riêng lẻ void krusalMax() {
} int parent[MAX];
for (int i = 0; i < n; i++) {
for (int i = 0; i < nE; i++) { parent[i] = i; // Khởi tạo mỗi đỉnh là
int root1 = parent[E1[i]]; một cây con riêng lẻ
int root2 = parent[E2[i]]; }
if (root1 != root2) { for (int i = nE - 1; i >= 0; i--) {
T1[nT] = E1[i]; int root1 = parent[E1[i]];
T2[nT] = E2[i]; int root2 = parent[E2[i]];
wT[nT] = wE[i];
nT++; if (root1 != root2) {
T1[nT] = E1[i];
// Gộp hai cây con thành một cây T2[nT] = E2[i];
for (int j = 0; j < n; j++) { wT[nT] = wE[i];
if (parent[j] == root2) { nT++;
parent[j] = root1;
} // Gộp hai cây con thành một cây
} for (int j = 0; j < n; j++) {
if (parent[j] == root2) {
if (nT == n - 1) { parent[j] = root1;
break; }
} }
}
} if (nT == n - 1) {
} break;
}
}
void output() { }
cout << "Cay bao trum nho nhat cua do thi }
la:" << endl;
int tongTrongSo = 0;
for (int i = 0; i < nT; i++) {
cout << "(" << vertex[T1[i]] << ", " <<
PrimMST của GFG
vertex[T2[i]] << ") - Trong so: " << wT[i] << #include <iostream>
endl; #include <fstream>
tongTrongSo += wT[i];
} using namespace std;
cout << "Tong trong so cua cay bao trum nho
nhat: " << tongTrongSo << endl; #define max 100
} int n, graph[max][max];

int main() { int minKey(int key[], bool mstSet[]) {


input(); int min = INT_MAX, min_index;
taoE();
SapXepE(); for (int v = 0; v < n; v++)
krusal(); if (mstSet[v] == false && key[v] < min)
output(); min = key[v], min_index = v;
else {
return min_index; fileOpened = true;
} }
} while (!fileOpened);
void printMST(int parent[]) {
cout << "Edge \tWeight\n"; file >> n;
int totalWeight = 0; for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) { file >> graph[i][j];
if (parent[i] != -1) { }
cout << parent[i] << " - " << i << " }
\t" << graph[i][parent[i]] << " \n"; file.close();
totalWeight += graph[i][parent[i]]; cout << "Da nhap tu file thanh cong" << endl;
} }
}
int main() {
cout << "Total Weight of MST: " << totalWeight inputFile();
<< endl; primMST();
} cout << endl;
system("pause");
void primMST() { return 0;
int parent[max]; }
int key[max];
bool mstSet[max];
Prim Min đúng nhất
for (int i = 0; i < n; i++)
key[i] = INT_MAX, mstSet[i] = false; #include<iostream>
#include<fstream>
key[0] = 0; using namespace std;
parent[0] = -1; #define MAX 50
//Khai bao tap E
for (int count = 0; count < n - 1; count++) { int n;
int u = minKey(key, mstSet); int a[MAX][MAX];
mstSet[u] = true; // tai vi tri 0---->(a,b,5)// a b c d(1,2,3,4)
int E1[MAX];
for (int v = 0; v < n; v++) int E2[MAX];
if (graph[u][v] && mstSet[v] == false int wE[MAX];
&& graph[u][v] < key[v]) int nE = 0;
parent[v] = u, key[v] =
graph[u][v]; // Khai bao tap T
} int T1[MAX];
int T2[MAX];
printMST(parent); int wT[MAX];
} int nT = 0;

void inputFile() { int TonTai(int d, int D[], int nD) {


cout << "Nhap ten file: \t"; for (int i = 0; i < nD; i++) {
fstream file; if (D[i] == d) return 1;
bool fileOpened = false; }
return 0;
do { }
string fileName; void XoaViTriE(int i) {
cin >> fileName; for (int j = i; j < nE; j++) {
file.open(fileName); E1[j] = E1[j + 1];
E2[j] = E2[j + 1];
if (!file.is_open()) { wE[j] = wE[j + 1];
cout << "File sai. Nhap lai: \t"; }
} nE--;
} input >> a[i][j];
void XoaCanhE(int u, int v) { }
for (int i = 0; i < nE; i++) { }
if (E1[i] == u && E2[i] == v) { input.close();
XoaViTriE(i); }
break; else {
} cout << "File mo khong duoc" << endl;
} }
} input.close();
void Prim(int s) { }
int u = s, min, i, d1, d2; void print() {
for (int i = 0; i < nT; i++)
while (nT < n - 1) { cout << "(" << char(T1[i] + 65) << "," <<
for (int v = 0; v < n; v++) char(T2[i] + 65) << "," << wT[i] << ")\n";
if (a[u][v] != 0) int sum = 0;
for (int i : wT) sum += i;
if (TonTai(v, T2, nT) == 0) { cout << "Tong cay bao trum toi thieu: " <<
E1[nE] = u; sum;
E2[nE] = v; }
wE[nE] = a[u][v]; int main() {
nE++; InputByFile();
} Prim(3);
print();
for (i = 0; i < nE; i++) return 1;
if (TonTai(E2[i], T2, nT) == 0 && }
E2[i] != s) {
min = wE[i]; Kruskal đúng nhất
d1 = E1[i];
d2 = E2[i]; #include<iostream>
break; #include<fstream>
} using namespace std;
#define MAX 50
for (; i < nE; i++) //Khai bao tap E
if (TonTai(E2[i], T2, nT) == 0 && int n;
E2[i] != s) int a[MAX][MAX];
if (min > wE[i]) { // tai vi tri 0---->(a,b,5)// a b c d(1,2,3,4)
min = wE[i]; int E1[MAX];
d1 = E1[i]; int E2[MAX];
d2 = E2[i]; int wE[MAX];
} int nE = 0;
T1[nT] = d1;
T2[nT] = d2; // Khai bao tap T
wT[nT] = a[d1][d2]; int T1[MAX];
a[d1][d2] = 0; int T2[MAX];
a[d2][d1] = 0; int wT[MAX];
nT++; int nT = 0;
XoaCanhE(d1, d2);
u = d2; int parent[MAX];
}
} void make_set() {
for (int i = 0; i < n; i++) {
void InputByFile() { parent[i] = i;
fstream input; }
input.open("Input.txt"); }
if (input.is_open()) { int find(int v) {
input >> n; if (parent[v] == v) return v;
for (int i = 0; i < n; i++) { return find(parent[v]);
for (int j = 0; j < n; j++) { }
}
bool union_set(int a, int b) { if (nT != n - 1) cout << "Mang khong lien
a = find(a); thong";
b = find(b); }
if (a == b) return false; void print() {
parent[b] = a; for (int i = 0; i < nT; i++)
return true; cout << "(" << char(T1[i] + 65) << "," <<
} char(T2[i] + 65) << "," << wT[i] << ")\n";
void InputByFile() { int sum = 0;
fstream input; for (int i : wT) sum += i;
input.open("Input.txt"); cout << "Tong cay bao trum toi thieu: " <<
if (input.is_open()) { sum;
input >> n; }
for (int i = 0; i < n; i++) { int main() {
for (int j = 0; j < n; j++) { InputByFile();
input >> a[i][j]; krusal();
} print();
} return 0;
} }
else {
cout << "File mo khong duoc" << endl; // Graph using linked list
}
} #include <iostream>
void taoE() { #include <fstream>
for (int i = 0; i < n; i++) using namespace std;
for (int j = 0; j < n; j++)
if (a[i][j] != 0) { //Linked list
E1[nE] = i; struct Node
E2[nE] = j; {
wE[nE] = a[i][j]; int data;
a[i][j] = 0; Node* next;
a[j][i] = 0; };
nE++; Node* first[100];
} int n;
} void init() {
void sapXepE() { for (int i = 0; i < n; i++) {
for (int i = 0; i < nE - 1; i++) first[i] = NULL;
for (int j = i + 1; j < nE; j++) }
if (wE[i] > wE[j]) { }
swap(E1[i], E1[j]); void insertNode(Node*& first, int x) {
swap(E2[i], E2[j]); Node* p;
swap(wE[i], wE[j]); p = new Node;
} p->data = x;
} p->next = first;
first = p;
void krusal() { }
taoE();
sapXepE();
make_set(); void inputFile() {
for (int i = 0; i < nE; i++) { fstream file;
; int x;
if (nT == n - 1) break; file.open("Input2.txt");
if (union_set(E1[i], E2[i])) { if (file.is_open()) {
T1[nT] = E1[i]; file >> n;
T2[nT] = E2[i]; for (int i = 0; i < n; i++) {
wT[nT] = wE[i]; insertNode(first[i], i);
nT++; for (int j = 0; j < n; j++) {
} file >> x;
insertNode(first[i],
x);
}
}
cout << "Them du lieu tu file thanh
cong" << endl;
file.close();
}
else cout << "Khong the mo file! " << endl;
}
void output_list(Node* f) {
if (f != NULL) {
Node* p = f;
while (p->next != NULL) {
cout << p->data << "\t";
p = p->next;
}
}
}
void output() {
if (n > 0) {
for (int i = 0; i < n; i++) {
cout << "Danh sach thu " << i
+ 1 << ": ";
output_list(first[i]);
cout << endl;
}
}
else {
cout << "Rong";
}
}

int main() {
init();
inputFile();
output();
return 0;
}

You might also like