Skip to content

Commit ece43f1

Browse files
committed
split Bipartite into bfs
1 parent 4eadc13 commit ece43f1

File tree

4 files changed

+50
-28
lines changed

4 files changed

+50
-28
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
*.exe
3131
*.out
3232
*.app
33+
.vscode/settings.json

Algorithms.cpp

+36-27
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,14 @@ namespace ariel{
7979
// sort of BFS on the graph
8080
for (size_t ind = 0; ind < graph.getSize(); ++ind) {
8181
if (color[ind] == "WHITE") {
82-
queue<size_t> queue;
83-
color[ind] = "BLUE";
84-
setA.push_back(ind); // Add the starting vertex to setA
85-
queue.push(ind); // push starting vertex to queue
86-
87-
while (!queue.empty()) {
88-
size_t vertice = queue.front();
89-
queue.pop();
90-
91-
for (size_t neighbor = 0; neighbor < graph.getSize(); ++neighbor) {
92-
if (graph.getWeight(vertice, neighbor) != 0) {
93-
if (color[neighbor] == "WHITE") {
94-
color[neighbor] = (color[vertice] == "BLUE") ? "RED" : "BLUE";
95-
queue.push(neighbor);
96-
97-
// Add the vertex to its set based on its color
98-
if (color[neighbor] == "RED") {
99-
setB.push_back(neighbor);
100-
} else {
101-
setA.push_back(neighbor);
102-
}
103-
} else if (color[neighbor] == color[vertice]) {
104-
// If adjacent vertices have the same color, the graph is not bipartite
105-
return "0";
106-
}
107-
}
108-
}
82+
if (!bfsBipartite(graph, color, setA, setB, ind)){
83+
return "0"; // The graph is not bipartite
10984
}
11085
}
11186
}
11287

88+
// if we reached here, the graph is bipartite
89+
11390
// Build the result string
11491
result += "The graph is bipartite: A={";
11592
for (size_t ind = 0; ind < setA.size(); ++ind) {
@@ -215,6 +192,38 @@ namespace ariel{
215192
return false; // No cycle found
216193
}
217194

195+
bool Algorithms::bfsBipartite(Graph& graph, vector<string>& color, vector<size_t>& setA, vector<size_t>& setB, size_t start) {
196+
queue<size_t> queue;
197+
color[start] = "BLUE";
198+
setA.push_back(start); // Add the starting vertex to setA
199+
queue.push(start); // push starting vertex to queue
200+
201+
while (!queue.empty()) {
202+
size_t vertice = queue.front();
203+
queue.pop();
204+
205+
for (size_t neighbor = 0; neighbor < graph.getSize(); ++neighbor) {
206+
if (graph.getWeight(vertice, neighbor) != 0) {
207+
if (color[neighbor] == "WHITE") {
208+
color[neighbor] = (color[vertice] == "BLUE") ? "RED" : "BLUE";
209+
queue.push(neighbor);
210+
211+
// Add the vertex to its set based on its color
212+
if (color[neighbor] == "RED") {
213+
setB.push_back(neighbor);
214+
} else {
215+
setA.push_back(neighbor);
216+
}
217+
} else if (color[neighbor] == color[vertice]) {
218+
// If adjacent vertices have the same color, the graph is not bipartite
219+
return false;
220+
}
221+
}
222+
}
223+
}
224+
return true; // The graph is bipartite
225+
}
226+
218227
string Algorithms::isContainsCycle(Graph graph) {
219228
vector<bool> visited(graph.getSize(), false);
220229
vector<size_t> parent(graph.getSize(), INT32_MAX);

Algorithms.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ namespace ariel{
6363
*/
6464
static int relaxEdges(Graph, vector<int>&, vector<size_t>&);
6565

66+
/*
67+
* Gets a graph and a vertice, and runs a DFS visit on it.
68+
* Returns true if a cycle was found, false otherwise.
69+
* Updates visited and path vectors, allowing us to track the cycle.
70+
*/
6671
static bool dfsVisit(Graph, size_t, vector<bool>&, vector<size_t>&, vector<size_t>&);
72+
73+
/*
74+
* Gets a graph and a start vertice, and runs a BFS on it.
75+
* Used to check if the graph is bipartite - Updates setA and setB with the vertices.
76+
* Returns "1" if the graph is bipartite, "0" otherwise.
77+
*/
78+
static bool bfsBipartite(Graph&, vector<string>&, vector<size_t>&, vector<size_t>&, size_t);
6779
};
6880
}

personal_test/visualize_graph.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def visualize_graph(adj_matrix, directed=False):
4242
# ]
4343

4444

45-
adj_matrix = [ [0, 1, 2, 0, 0], [1, 0, 3, 0, 0], [2, 3, 0, 4, 0], [0, 0, 4, 0, 5], [0, 0, 0, 5, 0] ]
45+
adj_matrix = [ [0, 1, 0, 0, 0], [1, 0, 3, 0, 0], [0, 3, 0, 4, 0], [0, 0, 4, 0, 5], [0, 0, 0, 5, 0] ]
4646

4747
# print(is_bipartite(adj_matrix))
4848
# visualize_graph(adj_matrix, False)

0 commit comments

Comments
 (0)