0% found this document useful (0 votes)
35 views3 pages

19BCS2626 Crab Graph

The document describes finding the maximum number of vertex-disjoint crab subgraphs that can be selected from an undirected graph. A crab subgraph has one head vertex connected to K foot vertices by K edges. The code implements a maximum flow algorithm to find the maximum number of crab subgraphs that can be selected from the given graph.

Uploaded by

White Wolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

19BCS2626 Crab Graph

The document describes finding the maximum number of vertex-disjoint crab subgraphs that can be selected from an undirected graph. A crab subgraph has one head vertex connected to K foot vertices by K edges. The code implements a maximum flow algorithm to find the maximum number of crab subgraphs that can be selected from the given graph.

Uploaded by

White Wolf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

AIM – A crab is an undirected graph which has two kinds of vertices: 1 head, and K feet , and

exactly K edges which join the head to each of the feet.( 1 <= K <= T, where T is given)
Given an undirected graph, you have to find in it some vertex-disjoint subgraphs where each one
is a crab . The goal is to select those crabs in such a way that the total number of vertices covered
by them is maximized.

CODE-
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define source (0)
#define target (1)
int capacity[300][300];
int flow[300][300];

int max_flow(int n, int s, int t) {


memset(flow, 0, sizeof(flow));
int max_flow = 0;

while (true) {
bool visited[300];
memset(visited, 0 , sizeof(visited));
int parent[300];

queue<int> bfs;
bfs.push(s);
visited[s] = true;
parent[s] = -1;

while (!bfs.empty()) {
int curr = bfs.front();
bfs.pop();
for (int i = 0; i < n; ++i) {
if (!visited[i] && capacity[curr][i] > flow[curr][i] -
flow[i][curr]) {
bfs.push(i);
parent[i] = curr;
visited[i] = true;
}
}
}

if (!visited[t]) { break; }

int path_flow = 0x7FFFFFFF;


for (int v = t; v != s; v = parent[v])
{
int u = parent[v];
path_flow = min(path_flow, capacity[u][v] - flow[u][v] + fl
ow[v][u]);
}

// update residual capacities of the edges and reverse edges


// along the path

for (int v = t; v != s; v = parent[v])


{
int u = parent[v];
flow[u][v] += path_flow;
}

// Add path flow to overall flow


max_flow += path_flow;
}
return max_flow;
}

#define ODD(x) (2 * (x) + 1)


#define EVEN(x) (2 * (x) + 2)

int main() {
/* Enter your code here. Read input from STDIN. Print output to STD
OUT */
int C;
cin >> C;
while (C--) {
memset(capacity, 0, sizeof(capacity));
int N, T, M;
cin >> N >> T >> M;

for (int i = 0; i < M; ++i) {


int v, w;
cin >> v >> w;
capacity[EVEN(v)][ODD(w)] = 1;
capacity[EVEN(w)][ODD(v)] = 1;
}

for (int v = 1; v <= N; ++v) {


capacity[source][EVEN(v)] = T;
capacity[ODD(v)][target] = 1;
}

cout << max_flow(2 * N + 3, source, target) << endl;

}
return 0;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy