Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in case of multiple edges in Finding Bridges offline #1273

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/graph/bridge-searching.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ The implementation needs to distinguish three cases: when we go down the edge in

To implement this, we need a depth first search function which accepts the parent vertex of the current node.

```cpp
For the cases of multiple edges, we need to be careful when ignoring the edge from the parent. To solve this issue, we can add a flag `parent_skipped` which will ensure we only skip the parent once.

```{.cpp file=bridge_searching_offline}
void IS_BRIDGE(int v,int to); // some function to process the found bridge
int n; // number of nodes
vector<vector<int>> adj; // adjacency list of graph

Expand All @@ -51,8 +54,12 @@ int timer;
void dfs(int v, int p = -1) {
visited[v] = true;
tin[v] = low[v] = timer++;
bool parent_skipped = false;
for (int to : adj[v]) {
if (to == p) continue;
if (to == p && !parent_skipped) {
parent_skipped = true;
continue;
}
if (visited[to]) {
low[v] = min(low[v], tin[to]);
} else {
Expand Down
84 changes: 84 additions & 0 deletions test/test_bridge_searching_offline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <cassert>
#include <vector>
#include <set>
using namespace std;

#include "bridge_searching_offline.h"

vector<pair<int,int>> bridges;
void IS_BRIDGE(int v,int to){
bridges.push_back({v, to});
}

void normalize(vector<pair<int,int>> &v){
for(auto &p : v){
if(p.first > p.second)swap(p.first, p.second);
}
sort(v.begin(), v.end());
}

void test_suite(
int _n,
vector<vector<int>> _adj,
vector<pair<int,int> > expected_bridges){
bridges.clear();
n = _n;
adj = _adj;
find_bridges();
normalize(expected_bridges);
normalize(bridges);
assert(bridges == expected_bridges);
}

int main() {
vector<vector<int>> adj;
auto add_edge = [&](int a,int b){
adj[a].push_back(b);
adj[b].push_back(a);
};
{
int n = 5;
adj.resize(n);
add_edge(0, 1);
add_edge(0, 1);

add_edge(2, 3);
add_edge(3, 4);
vector<pair<int,int> > expected_bridges;
expected_bridges.push_back({2,3});
expected_bridges.push_back({3,4});
// note that 0-1 is not a bridge due to duplicate edges!
test_suite(n, adj, expected_bridges);
adj.clear();
}
{
int n = 7;
adj.resize(n);
add_edge(0, 1);
add_edge(1, 2);
add_edge(2, 3);
add_edge(3, 1);
add_edge(3, 4);
add_edge(0, 4);
add_edge(4, 5);
add_edge(1, 6);
vector<pair<int,int> > expected_bridges;
expected_bridges.push_back({4, 5});
expected_bridges.push_back({1, 6});
test_suite(n, adj, expected_bridges);
adj.clear();
}
{
int n = 4;
adj.resize(n);
add_edge(0, 1);
add_edge(0, 1);
add_edge(1, 2);
add_edge(2, 3);
add_edge(2, 3);
vector<pair<int,int> > expected_bridges;
expected_bridges.push_back({1, 2});
test_suite(n, adj, expected_bridges);
adj.clear();
}
}