-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_traversal.cpp
More file actions
161 lines (140 loc) · 3.08 KB
/
Graph_traversal.cpp
File metadata and controls
161 lines (140 loc) · 3.08 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<vector>
#define NO_EDGE 0
#define IS_EDGE 1
//Graph_traversal "https://informatics.mccme.ru/mod/statements/view3.php?id=256&chapterid=164#1"
class Graph
{
public:
typedef size_t Vertex;
Graph(size_t vertex_count, bool is_directed)
{
vertex_count_ = vertex_count;
is_directed_ = is_directed;
}
size_t getVertexCount() const
{
return vertex_count_;
}
size_t geEdgeCount() const
{
return edge_count_;
}
virtual std::vector<Vertex> getNeighbors(const Vertex& v) const = 0;
virtual void addEdge(const Vertex& start, const Vertex& finish)
{
edge_count_++;
}
virtual size_t getNeighborsCount(const Vertex& v) const = 0;
protected:
bool is_directed_;
size_t vertex_count_ = 0,
edge_count_ = 0;
};
class GraphAdjMatrix : public Graph
{
public:
GraphAdjMatrix(const size_t vertex_count, bool is_directed) :
Graph(vertex_count, is_directed)
{
adj_matrix_ = std::vector<std::vector<int>>(vertex_count, std::vector<int>(vertex_count, NO_EDGE));
}
void addEdge(const Vertex& start, const Vertex& finish) override
{
Graph::addEdge(start, finish);
adj_matrix_[start][finish] = IS_EDGE;
if (is_directed_)
{
adj_matrix_[finish][start] = IS_EDGE;
}
}
std::vector<Vertex> getNeighbors(const Vertex& v) const override
{
std::vector<Vertex> neighbors;
for (Vertex i = 0; i < vertex_count_; i++)
{
if (adj_matrix_[v][i])
{
neighbors.push_back(i);
}
}
return neighbors;
}
size_t getNeighborsCount(const Vertex& v) const override
{
size_t count = 0;
for (Vertex i = 0; i < adj_matrix_[v].size(); i++)
{
if (adj_matrix_[v][i] == IS_EDGE)
{
count++;
}
}
return count;
}
void printGraph() const
{
std::cout << "Print\n";
std::cout << "Vertex_count = " << vertex_count_ << "\n";
for (auto i : adj_matrix_)
{
for (auto j : i)
std::cout << j << " ";
std::cout << std::endl;
}
for (size_t i = 0; i < vertex_count_; i++)
{
std::cout << "Neighbors for " << i << " : ";
for (auto j : this->getNeighbors(i))
std::cout << j << " ";
std::cout << "\n";
}
}
private:
std::vector<std::vector<int>> adj_matrix_;
};
namespace GraphProcessing
{
enum VertexMark
{
WHITE, GREY, BLACK
};
typedef size_t Vertex;
void dfs_visit(const Graph& g, const size_t& vertex, size_t& counter, std::vector<VertexMark>& vertex_marks)
{
counter++;
vertex_marks[vertex] = GREY;
for (Vertex neighbor : g.getNeighbors(vertex))
{
if (vertex_marks[neighbor] == WHITE)
{
dfs_visit(g, neighbor, counter, vertex_marks);
vertex_marks[neighbor] = BLACK;
}
}
}
size_t getComponentSize(const Graph& g, const size_t& vertex)
{
size_t counter = 0;
std::vector<VertexMark> vertex_marks(g.getVertexCount(), WHITE);
dfs_visit(g, vertex, counter, vertex_marks);
return counter;
}
}
int main()
{
size_t n, v;
std::cin >> n >> v;
GraphAdjMatrix g(n, false);
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n; j++)
{
size_t is_neighbor;
std::cin >> is_edge;
if (is_edge == IS_EDGE)
g.addEdge(i, j);
}
}
std::cout << GraphProcessing::getComponentSize(g, v - 1);
}