-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgraph.cpp
More file actions
99 lines (85 loc) · 1.58 KB
/
graph.cpp
File metadata and controls
99 lines (85 loc) · 1.58 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
//Author : Jayant Kumar, jayant@umiacs.umd.edu, UMD college park
#include "stdafx.h"
#include <iostream>
#include <vector>
#include "graph.h"
using namespace std;
Queue::Queue()
{
first = new node;
first->next = NULL;
last = first;
}
Queue::~Queue()
{
delete first;
}
bool Queue::isEmpty() {
return (first->next == NULL);
}
void Queue::add(int x) {
node *aux = new node;
aux->info = x;
aux->next = NULL;
last->next = aux;
last = aux;
}
int Queue::get() {
node *aux = first->next;
int value = aux->info;
first->next = aux->next;
if (last == aux) last = first;
delete aux;
return value;
}
Graph::Graph(int size) {
int i, j;
if (size < 2) n = 2;
else n = size;
A = new int*[n];
for (i = 0; i < n; ++i)
A[i] = new int[n];
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
A[i][j] = 0;
}
Graph::~Graph() {
for (int i = 0; i < n; ++i)
delete [] A[i];
delete [] A;
}
bool Graph::isConnected(int x, int y) {
return (A[x-1][y-1] == 1);
}
void Graph::addEdge(int x, int y) {
A[x-1][y-1] = A[y-1][x-1] = 1;
}
vector<int> Graph::BFS(int x) {
Queue Q;
bool *visited = new bool[n+1];
int i;
vector<int> conComp;
for (i = 1; i <= n; ++i)
visited[i] = false;
Q.add(x);
visited[x] = true;
//cout << "Breadth First Search starting from vertex ";
//cout << x << " : " << endl;
while (!Q.isEmpty())
{
int k = Q.get();
//cout << k << " ";
for (i = 1; i <= n; ++i)
{
if (isConnected(k, i) && !visited[i])
{
Q.add(i);
visited[i] = true;
conComp.push_back(i);
}
}
}
//cout << endl;
delete [] visited;
return conComp; // return connected components
}