0% found this document useful (0 votes)
12 views6 pages

GG 2

The document is a C++ program that implements various graph algorithms, including BFS, DFS, Kruskal's and Prim's algorithms. It reads a graph from a file, constructs an adjacency list, and provides functionalities to display the graph, find paths, and calculate the degree of each vertex. The program demonstrates the use of data structures like queues, stacks, and unordered maps for efficient graph traversal and manipulation.

Uploaded by

cutevlss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

GG 2

The document is a C++ program that implements various graph algorithms, including BFS, DFS, Kruskal's and Prim's algorithms. It reads a graph from a file, constructs an adjacency list, and provides functionalities to display the graph, find paths, and calculate the degree of each vertex. The program demonstrates the use of data structures like queues, stacks, and unordered maps for efficient graph traversal and manipulation.

Uploaded by

cutevlss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<iostream>

#include<fstream>
#include<queue>
#include<stack>
#include<unordered_map>
#include<vector>
#define max 50
int e1[max], e2[max], we[max], ne = 1,n;
using namespace std;
unordered_map<int, vector<pair<int, int>>> adj;
void doc()
{
int a[max][max];
ifstream file("test.txt");
if (file.is_open())
{
file >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
file>>a[i][j];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (a[i][j]!=0)
{
e1[ne] = i;
e2[ne] = j;
we[ne] = a[i][j];
a[i][j] = a[j][i] = 0;
ne++;
}
}
}
/*file >> n>>ne;
for (int i = 1; i <=n ; i++)
{
int u, v, w;
file >> u >> v >> w;
e1[i] = u, e2[i] = v, we[i] = w;
}*/
for (int i = 1; i <= ne; i++)
{
adj[e1[i]].push_back({ e2[i],we[i] });
adj[e2[i]].push_back({ e1[i],we[i] });

}
file.close();
}
}
void xuat()
{
for (int i = 1; i <= ne-1; i++)
{
cout << e1[i] << " " << e2[i] << " " << we[i] << endl;
}
}

void bfs(int start)


{
vector<bool>v(max, false);
queue<int>q;

q.push(start);
v[start] = true;
cout << "BFS:";
while (!q.empty())
{
int u = q.front();
q.pop();
cout << u << " ";
for (auto &nei:adj[u])
{
int nexte = nei.first;
if (!v[nexte])
{
q.push(nexte);
v[nexte] = true;
}
}
}
}
void bfsext(int s,int e)
{
queue<int>q;
vector<int> parent(max, -1);
vector<bool>v(max, false);
q.push(s);
v[s] = true;
while (!q.empty())
{
int u = q.front();
q.pop();
if (u==e)
{
break;
}
for (auto& nei : adj[u])
{
int nexte = nei.first;
if (!v[nexte])
{
q.push(nexte);
parent[nexte] = u;
v[nexte] = true;
}
}
}
cout << endl;
if (v[e])
{
vector<int>path;
for (int i = e; i !=-1; i=parent[i])
{
path.push_back(i);
}
reverse(path.begin(), path.end());
for (int v:path)
{
cout << v << " ";
}
}
}
void dfs(int start)
{
stack<int>s;
vector<bool>v(max, false);
s.push(start);
v[start] = true;
cout <<endl<< "DFS:";

while (!s.empty())
{
int u = s.top();s.pop();
cout << u << " ";
for (auto& nei : adj[u])
{
int nexte = nei.first;
if (!v[nexte])
{
v[nexte] = true;
s.push(nexte);
}
}
}
}
void dfsext(int start,int e)
{
stack<int>s;
vector<int>parent(max, -1);
vector<bool>v(max, false);
s.push(start);
v[start] = true;

while (!s.empty())
{
int u = s.top();s.pop();
if (e == u)break;
for (auto& nei : adj[u])
{
int nexte = nei.first;
if (!v[nexte])
{
v[nexte] = true;
parent[nexte] = u;
s.push(nexte);
}
}
}
cout << endl;
if (v[e])
{
vector<int>path;
for (int i = e; i != -1; i = parent[i])
{
path.push_back(i);
}
reverse(path.begin(), path.end());
for (int v : path)
{
cout << v << " ";
}
}
}
void calc()
{
vector<int> deg(ne);
for (int i = 1; i <= ne; i++)
{

deg[e1[i]]++;
deg[e2[i]]++;

}
cout << endl<<"Degree of each vertex:" << endl;
for (int i = 1; i <= deg.size(); i++)
{
cout << i << ":" << deg[i] << endl;
if (i==n)
{
break;
}
}
}
vector<int> parents(max), sz(max, 1);
struct edge {
int u, v, w;
} e;
void swap(int a, int b)
{
int c = a;
a = b;
b = c;
}
int find(int v)
{
if (v==parents[v])
{
return v;
}
return parents[v] = find(parents[v]);
}
bool cmp(edge a, edge b)
{
return a.w < b.w;
}
bool Union(int a, int b)
{
a = find(a);
b = find(b);
if (a==b)
{
return false;
}
if (sz[a]<sz[b])
swap(a, b);
parents[b] = a;
sz[a] += sz[b];
return true;
}
void kruskal()
{
for (int i = 1; i <=ne ; i++)
{
parents[i] = i;
}
vector<edge>canh, mst;
for (int i = 1; i <= ne; i++)
{
e.u = e1[i];
e.v = e2[i];
e.w = we[i];
canh.push_back(e);
}int d = 0;
sort(canh.begin(), canh.end(),cmp);
for (int i = 0; i < ne; i++)
{
if (mst.size()==n)
{
break;
}
edge e = canh[i];
if (Union(e.u,e.v))
{
mst.push_back(e);
d += e.w;
}
}
cout << endl << d<<endl;
for (auto&m :mst)
{
cout << m.u <<"-"<< m.v << "-"<<m.w << endl;
}
}

void prim(int start)


{
vector<bool>v(max, false);
int d = 0;
v[start] = true;
vector<edge>MST;
while (MST.size()< n-1)
{
int min_w = INT_MAX;
int x, y;
for (int i = 1; i <= n; i++)
{
if (v[i])
{
for (auto& it : adj[i]) {
int j = it.first, trongso = it.second;
if (!v[j]&& trongso<min_w)
{
min_w = trongso;
x = j, y = i;
}
}
}
}
MST.push_back({ x,y,min_w });
d += min_w;
v[x] = true;
}
cout << d<<endl;
for (auto& m : MST)
{
cout << m.u << " " << m.v << " " << m.w << endl;
}

}
int main()
{
doc();
xuat();
bfs(1);
bfsext(1, 6);
dfs(1);
dfsext(2, 6);
calc();
kruskal();
prim(1);
system("pause");
return 0;
}

You might also like