-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTask3.cpp
108 lines (95 loc) · 1.93 KB
/
Task3.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
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
#include<windows.h>
#include <vector>
using namespace std;
// Declaring class to store all the data related to graphs
class Graph
{
public:
int vertice;
int vertices = 0;
int edges = 0;
vector <char> graph[100];
bool directed = false;
bool* present;
void addEdge(vector <char> graph[], char a, char b, bool directed);
void BFS(int startVertex);
};
//To add new edges in rhe graph
void Graph::addEdge(vector <char> graph[], char a, char b, bool directed) {
if (directed) //if graph is directed
graph[a].push_back(b);
else { //if it is undirected
graph[a].push_back(b);
graph[b].push_back(a);
}
}
// Breath First Search
void Graph::BFS(int start)
{
present = new bool[vertices];
for (int i = 0; i < vertices; i++){
present[i] = false;
}
vector<int> a;
present[start] = true;
a.push_back(start);
vector<int>::iterator visited;
cout << "The visited vertex are: ";
while (!a.empty())
{
int currentNode = a.front();
cout << currentNode << ", ";
a.pop_front();
for (visited = graph[currentNode].begin(); visited != graph[currentNode].end(); ++visited)
{
int neighVertex = *visited;
if (!present[neighVertex])
{
present[neighVertex] = true;
a.push_back(neighVertex);
}
}
}
}
int main()
{
Graph g;
int i, direct;
char spc, temp;
string name = "";
ifstream input;
input.open("data.txt"); //opening file
if (!input) {
cout << "Unable to open file " << endl;
exit(1);
}
else
{
input >> g.vertices;
input >> spc;
input >> direct;
if (direct == 0) {
g.directed = false;
}
else {
g.directed = true;
}
for (int i = 0; i < g.vertices; i++) {
input >> spc;
name += spc;
}
input >> g.edges;
for (int i = 0; i < g.edges; i++) {
input >> spc;
input >> temp;
g.addEdge(g.graph, spc, temp, g.directed); //creatig edges in the list
}
}
input.close(); //closing file
g.BFS(4);
return 0;
}