Professional Documents
Culture Documents
#include<iostream>
#include<vector>
HeapSort(a, n);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
}
Quick sort
#include<iostream>
#include<vector>
QuickSort(a,0, n-1);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
}
Counting sort
#include<iostream>
#include<vector>
Ma trận kề
#define MAX 20
int A[MAX][MAX]; // mảng hai chiều
int n; // số đỉnh của đồ thị
void init() {
n = 0;
}
void input() {
cout << "nhap so dinh do thi n: ";
cin >> n;
for
(int i = 0; i
< n; i++)
{
cout << "nhap vao dong thu " << i + 1 << ": ";
for
(int j = 0; j
< n; j++)
cin >> A[i][j];
}
}
void output() {
for
(int i = 0; i
< n; i++)
{
for
(int j = 0; j
< n; j++)
cout << A[i][j] << " ";
cout << endl
;
}
}
Danh sách kề
#define MAX 20
struct Node
{
int info;
Node* link;
};
Node* first[MAX]; // mảng danh sách
int n; // so dinh tren do thi
void init()
{
for (int i = 0; i < n; i++)
first[i] = NULL;
}
void insert_first
(Node * &f, int x)
{
Node* p;
p = new Node
;
p-> info = x;
p-> link = f;
f = p;
}
void input()
{
int d, x, m;
cout << "nhap so dinh do thi n: ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "\nnhap dinh thu"<<i+1<<" : ";
cin >> d;
insert_first(first[i], d);
cout << "nhap vao so dinh ke cua"<<d<<" : ";
cin >> m;
for (int j = 0; j < m; j++)
{
cin >> x;
insert_first(first[i], x);
}
}
}
void output_list
(Node* f)
{
if (f != NULL
)
{
Node* p = f;
while (p != NULL
)
{
cout << p-> info << " ";
p = p-> link
;
}
}
}
void output()
{
if (n > 0)
for (int i = 0; i < n; i++)
{
cout << endl << "Danh sach thu " << i + 1 << ": ";
output_list(first[i]);
}
else
cout << “rong”;
}
DFS
#include<iostream>
#include<fstream>
#include<stack>
#define hola 100
using namespace std;
class myStack {
private:
public:
int sArr[hola];
int r=0;
void pop(int& x) {
x = sArr[r - 1];
r--;
}
bool isEmpty() {
return r == 0;
}
void push(int x) {
sArr[r] = x;
r++;
}
};
int n;
int a[hola][hola];
int visited[hola];
int isOut[hola];
void inputFile()
{
ifstream file;
file.open("Text.txt");
file >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
file >> a[i][j];
}
}
file.close();
for (int i = 0; i < n; i++)
{
visited[i] = 0;
isOut[i] = 0;
}
}
void dfsMy2()
{
myStack st;
st.push(0);
visited[0] = 1;
isOut[0] = 0;
while (!st.isEmpty())
{
int x;
st.pop(x);
if (isOut[x] == 0)
{
cout << x + 1 << " ";
isOut[x] = 1;
}
visited[x] = 1;
for (int i = 0; i < n; i++)//(int i = n-1; i >=0; i--)
{
if (visited[i] == 0 && a[x][i] != 0)
{
st.push(i);
}
}
}
}
int main()
{
inputFile();
dfsMy2();
//DFS(0);
return 0;
}
BFS
#include<iostream>
#include<fstream>
#include<queue>
#define hola 100
using namespace std;
class myQueue {
private:
public:
int sArr[hola];
int r = 0;
int qSize = 0;
int pop() {
int x = sArr[0];
for (int i = 0; i < qSize; i++)
{
sArr[i] = sArr[i + 1];
}
qSize--;
return x;
}
bool isEmpty() {
return qSize == 0;
}
void push(int x) {
sArr[qSize] = x;
qSize++;
}
};
int n;
int a[hola][hola];
int visited[hola];
int isOut[hola];
void inputFile()
{
ifstream file;
file.open("Text.txt");
file >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
file >> a[i][j];
}
}
file.close();
for (int i = 0; i < n; i++)
{
visited[i] = 0;
isOut[i] = 0;
}
}
void bfsMy()
{
myQueue st;
st.push(0);
visited[0] = 1;
isOut[0] = 0;
while (!st.isEmpty())
{
int x = st.pop();
if (isOut[x] == 0)
{
cout << x + 1 << " ";
isOut[x] = 1;
}
visited[x] = 1;
for (int i = 0; i < n; i++)//(int i = n-1; i >=0; i--)
{
if (visited[i] == 0 && a[x][i] != 0)
{
st.push(i);
}
}
}
}
int main()
{
inputFile();
bfsMy();
//DFS(0);
return 0;
}
Prim
Kruskal