#include <bits/stdc++.h>
#include <cstdlib>
#include <ctime>
using namespace std;
class Graph
{
public:
int time = 0;
vector<int> traversal_array;
int v;
int e;
vector<vector<int>> graph_list;
vector<vector<int>> graph_matrix;
Graph(int v)
{
this->v = v;
this->e = rand() % (45 - 9 + 1) + 9;
this->graph_list.resize(v);
this->graph_matrix.resize(v, vector<int>(v, 0));
}
void create_random_graph()
{
for (int i = 0; i < this->e; i++)
{
int src = rand() % this->v;
int dest = rand() % this->v;
while (src == dest && this->graph_matrix[src][dest] == 1)
{
src = rand() % this->v;
dest = rand() % this->v;
}
this->graph_list[src].push_back(dest);
this->graph_matrix[src][dest] = 1;
}
}
void print_graph_list()
{
cout << "Adjacency List Representation:" << endl;
for (int i = 0; i < this->v; i++)
{
cout << i << "-->";
for (int j = 0; j < this->graph_list[i].size(); j++)
{
cout << this->graph_list[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void print_graph_matrix()
{
cout << "Adjacency Matrix Representation:" << endl;
for (int i = 0; i < this->graph_matrix.size(); i++)
{
for (int j = 0; j < this->graph_matrix[i].size(); j++)
{
cout << this->graph_matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int number_of_edges()
{
return this->e;
}
void dfs()
{
this->visited.resize(this->v);
this->start_time.resize(this->v);
this->end_time.resize(this->v);
fill(this->visited.begin(), this->visited.end(), false);
for (int node = 0; node < this->v; node++)
{
if (!this->visited[node])
{
this->traverse_dfs(node);
}
}
cout << endl;
cout << "DFS Traversal: ";
for (int i = 0; i < this->traversal_array.size(); i++)
{
cout << this->traversal_array[i] << " ";
}
cout << endl;
}
void traverse_dfs(int node)
{
this->visited[node] = true;
this->traversal_array.push_back(node);
this->start_time[node] = this->time;
this->time++;
for (int neighbour = 0; neighbour < this->graph_list[node].size(); neighbour++)
{
if (!this->visited[this->graph_list[node][neighbour]])
{
cout << "Tree Edge:" << node << "-->" << this->graph_list[node][neighbour] << endl;
this->traverse_dfs(this->graph_list[node][neighbour]);
}
else
{
if (this->start_time[node] > this->start_time[this->graph_list[node][neighbour]] && this->end_time[node] < this->end_time[this->graph_list[node][neighbour]])
{
cout << "Back Edge:" << node << "-->" << this->graph_list[node][neighbour] << endl;
}
else if (this->start_time[node] < this->start_time[this->graph_list[node][neighbour]] && this->end_time[node] > this->end_time[this->graph_list[node][neighbour]])
{
cout << "Forward Edge:" << node << "-->" << this->graph_list[node][neighbour] << endl;
}
else if (this->start_time[node] > this->start_time[this->graph_list[node][neighbour]] && this->end_time[node] > this->end_time[this->graph_list[node][neighbour]])
{
cout << "Cross Edge:" << node << "-->" << this->graph_list[node][neighbour] << endl;
}
}
this->end_time[node] = this->time;
this->time++;
}
}
private:
vector<bool> visited;
vector<int> start_time;
vector<int> end_time;
};
int main()
{
srand(time(NULL));
int n = 10;
Graph g(n);
g.create_random_graph();
g.print_graph_list();
g.print_graph_matrix();
g.dfs();
return 0;
}