forked from singhmayank980/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS_Directed.cpp
147 lines (131 loc) · 3.09 KB
/
BFS_Directed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<iostream>
#include<stdlib.h>
#include<queue>
#include<bits/stdc++.h>
#include<vector>
using namespace std;
struct Edge
{
int start,end;
Edge():start(0),end(0){};
Edge(int st,int en):start(st),end(en){};
};
struct Node
{
int data,level,pre;
Node():data(0),level(-1),pre(-1){};
Node(int x):data(x),level(-1),pre(-1){};
Node(Node &temp):data(temp.data),level(temp.level),pre(temp.pre){};
};
vector<Node *> q;
class Graph
{
int nodes,edges,*visited,**adj_mat;
vector<Edge *> edges_arr;
vector<Node *> nodes_arr;
public:
Graph(){};
Graph(int node,int edge):nodes(node),edges(edge)
{
visited = new int[nodes]{0};
adj_mat = new int * [nodes];
for(int i=0;i<nodes;i++)
adj_mat[i] = new int[nodes]{0};
}
void get_data(void);
void display_tree(void);
Graph * converter(void);
void display_adj_mat(void);
};
void Graph::display_adj_mat(void)
{
cout<<endl;
for(int i=0;i<nodes;i++)
{
for(int j=0;j<nodes;j++)
cout<<adj_mat[i][j];
cout<<endl;
}
}
void Graph::display_tree(void)
{
for(auto it = edges_arr.begin();it!=edges_arr.end();it++)
cout<<endl<<(*it)->start<<" "<<(*it)->end;
}
Graph * Graph::converter(void)
{
Graph * ptr = new Graph(nodes,nodes-1);
for(int j=0;j<nodes_arr.size();j++)
if(!ptr->visited[j])
{
Node *temp = new Node(*nodes_arr[j]);
temp->level=0;
ptr->visited[temp->data]=1;
q.push_back(temp);
while(!q.empty())
{
Node * dequeue = q[0];
q.erase(q.begin());
for(int i=0;i<nodes;i++)
{
if((adj_mat[dequeue->data][i])&&(!(ptr->visited[i])))
{
temp = new Node(*nodes_arr[i]);
temp->level = dequeue->level+1;
temp->pre = dequeue->data;
ptr->visited[temp->data]=1;
q.push_back(temp);
}
}
ptr->visited[dequeue->data]=2;
cout<<dequeue->data<<" ";
ptr->nodes_arr.push_back(dequeue);
if(dequeue->pre!=-1)
{
ptr->adj_mat[dequeue->pre][dequeue->data]=1;
Edge * temp_edge = new Edge(dequeue->pre,dequeue->data);
ptr->edges_arr.push_back(temp_edge);
}
}
}
return ptr;
}
void Graph::get_data(void)
{
cout<<"\nEnter number of nodes ";
cin>>nodes;
cout<<"\nEnter number of edges ";
cin>>edges;
adj_mat = new int * [nodes];
for(int i=0;i<nodes;i++)
{
adj_mat[i] = new int[nodes]{0};
Node * temp = new Node(i);
nodes_arr.push_back(temp);
}
for(int i=0;i<edges;i++)
{
cout<<"\nEnter edge "<<i+1<<": ";
int st,en;
cin>>st>>en;
if(st>=nodes||en>=nodes)
{
cout<<"\nInvalid input ";
i--;
}
else
{
Edge *temp = new Edge(st,en);
edges_arr.push_back(temp);
adj_mat[st][en] = 1;
}
}
}
main()
{
Graph G;
G.get_data();
Graph * T = G.converter();
T->display_adj_mat();
T->display_tree();
}