-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cc
More file actions
162 lines (135 loc) · 2.76 KB
/
bfs.cc
File metadata and controls
162 lines (135 loc) · 2.76 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <list>
using namespace std;
class Graph{
int V;
list<int> *A;
public:
Graph(int v);
void addEdge(int s, int d);
void printGraph();
friend void BFS(Graph G, int s);
};
Graph::Graph(int v){
this->V = v;
A = new list<int>[V];
}
void Graph::addEdge(int s, int d){
A[s].push_back(d);
}
void Graph::printGraph(){
for(int v=0;v<this->V;++v){
cout << "Adjacency List of vertex " << v << ":\n head";
for(list<int>::iterator i = (this->A[v].begin());i!=(this->A[v].end());i++){
cout << " -> " << *i;
}
cout << endl;
}
}
void BFS(Graph G, int s){
bool* visited = new bool[G.V];
for(int i=0;i<G.V;i++){
visited[i] = false;
}
int* spath=new int[G.V];
for(int i=0;i<G.V;i++){
spath[i]=0;
}
list<int> queue;
visited[s] = true;
queue.push_back(s);
int temp;
while(!queue.empty()){
temp = queue.front();
cout << temp << " ";
queue.pop_front();
for(list<int>::iterator i = G.A[temp].begin();i != G.A[temp].end();i++){
if(visited[*i]==false){
visited[*i]=true;
spath[*i] = spath[temp]+1;
queue.push_back(*i);
}
}
}
cout << endl;
for(int i=0;i<G.V;i++){
cout << spath[i] << " ";
}
cout << endl;
}
int main(){
Graph G(4);
G.addEdge(0,1);
G.addEdge(0,2);
G.addEdge(1,2);
G.addEdge(2,0);
//G.addEdge(1,4);
G.addEdge(2,3);
G.addEdge(3,3);
G.printGraph();
BFS(G,0);
}
/*#include <iostream>
#include <cstdlib>
using namespace std;
struct node{
int dest;
struct node* next;
};
struct list{
struct node* head;
};
struct graph{
int V;
struct list* G;
};
struct node* newNode(int dest){
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
struct graph* createGraph(int V){
struct graph* Graph = (struct graph*) malloc(sizeof(struct graph));
Graph->V = V;
Graph->G = (struct list*) malloc(V * sizeof(struct list));
int i;
for(i=0;i<V;i++){
Graph->G[i].head=NULL;
}
return Graph;
}
void addEdge(struct graph* Graph, int src, int dst){
struct node* Node = newNode(dst);
Node->next = Graph->G[src].head;
Graph->G[src].head = Node;
Node = newNode(src);
Node->next = Graph->G[dst].head;
Graph->G[dst].head = Node;
}
void printGraph(struct graph* Graph){
int v;
for(v=0;v<Graph->V;++v){
struct node* N = Graph->G[v].head;
cout << "Adjacency List of vertex " << v << ":\n head";
while(N){
cout << " -> " << N->dest;
N = N->next;
}
cout << endl;
}
}
int main(){
int V = 5;
struct graph* Graph = createGraph(V);
addEdge(Graph, 0, 1);
addEdge(Graph, 0, 4);
addEdge(Graph, 1, 2);
addEdge(Graph, 1, 3);
addEdge(Graph, 1, 4);
addEdge(Graph, 2, 3);
addEdge(Graph, 3, 4);
// print the adjacency list representation of the above graph
printGraph(Graph);
return 0;
}*/