-
Notifications
You must be signed in to change notification settings - Fork 1
/
spanningtree.cpp
61 lines (44 loc) · 1.38 KB
/
spanningtree.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
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <queue>
using namespace std;
/* simple bfs search to test reachability between any two nodes in the graph */
// also can use BFS to detect loop in graph, when neighour of a node is already visited
bool isConnected(int dim, int graph[dim][dim], int src, int dest){
unordered_set<int> visited;
queue<int> q;
visited.insert(src);
q.push(src); // the index for src
while (q.empty()) {
int size = q.size(); // get neighbourhood nodes
for(int i = 0; i < size; i++){
int row = q.front();
for(int j = 0; j < dim; j++) {
// node j in the list
if ( graph[i][j] > 0 && visited.find(j) != visited.end() ) {
q.push(j);
visited.insert(j);
}
if ( graph[i][j] > 0 && j == dest) return true;
}
}
}
return false; // iterates to all neighour nodes NOT reachable
}
int main(){
/* Let us create the following graph (take usage of geeksforgeeks matrix sample )
2 3
(0)--(1)--(2)
| / \ |
6| 8/ \5 |7
| / \ |
(3)-------(4)
9 */
int graph[5][5] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}, // undirected graph is symmetric around the diagonal
};
}