Exemplo n.º 1
0
vector<SplitEdge> hookUpHelper(int s, const vector<SplitEdge> &g, const vector<SplitEdge> &toHookUp, historyIndex &h)
{
	vector<SplitEdge> out = g;
	for (unsigned i = 0; i < toHookUp.size(); i++)
	{
		int u = toHookUp[i].end0;
		int v = toHookUp[i].end1;
		int subtractInd = indexOfEdge(g, u, v);
		if (subtractInd == -1)
			throw logic_error("Trying to subtract weight from nonexistant edge in hookUpHelper");
		out[subtractInd].weight -= toHookUp[i].weight;
		int usInd = indexOfEdge(g, u, s);
		int vsInd = indexOfEdge(g, v, s);
		eraseSplit(h, s, u, v, toHookUp[i].weight);
		if (usInd == -1)
		{
			SplitEdge e(u, s, toHookUp[i].weight, u, s);
			//cout << "Creating edge between " << u << " and " << s << " with weight " << toHookUp[i].weight << " in hookUpHelper" << endl;
			out.push_back(e);
		}
		else
		{
			out[usInd].weight += toHookUp[i].weight;
			//cout << "Increasing the weight from " << u << " to " << s << " from " << out[usInd].weight << " to " << out[usInd].weight + toHookUp[i].weight << endl;
		}
		if (vsInd == -1)
		{
			SplitEdge e(v, s, toHookUp[i].weight, v, s);
			out.push_back(e);
			//cout << "Creating edge between " << v << " and " << s << " with weight " << toHookUp[i].weight << " in hookUpHelper" << endl;
		}
		else
		{ 
			out[vsInd].weight += toHookUp[i].weight;
			//cout << "Increasing the weight from " << v << " to " << s << " from " << out[vsInd].weight << " to " << out[vsInd].weight + toHookUp[i].weight << endl;
		}
	}
	return out;
}
Exemplo n.º 2
0
void DinicFlowSolver::solve(int source, int sink) {
    while (true) {
        std::vector<int> distanceToVertex;
        bfs(source, distanceToVertex);
        if (distanceToVertex[sink] == -1) {
            break;
        }
        long long MaxFlowPossible = 0LL;
        for (int i = 0; i < (int)graph.edge[source].size(); ++i) {
            FlowEdge *edge = (FlowEdge *)
                    graph.edge[source][i];
            MaxFlowPossible += edge->getCapacity() - edge->getFlow();
        }
        std::vector<int> indexOfEdge(graph.edge.size(), 0);
        long long pushed = dfs(graph, source, sink, MaxFlowPossible, distanceToVertex, indexOfEdge);
        graph.flow += pushed;
    }
}
Exemplo n.º 3
0
huReturn hookUp(const vector<SplitEdge> &g, int s, vector<SplitEdge> &bIn, int k, vector<vector<int>> &Y, historyIndex &h)
{
	if (cG(s, g) != 0)
	{
		cout << "cG(s) problem in hookup" << endl;
		throw logic_error("");
	}
	vector<SplitEdge> H = g;
	vector<SplitEdge> G1 = g; 
	vector<SplitEdge> B = bIn;
	vector<SplitEdge> B1;
	vector<vector<int>> XS;
	int maxNodeInd = getMaxNodeInd(G1);
	for (int i = 0; i < maxNodeInd; i++)
		XS.push_back(vector<int>());
	for (int i = 0; i < maxNodeInd; i++)
		XS[i].push_back(i);
	//cout << "About to enter while loop" << endl;
	while (getNumUsedNodes(H) >= 4)
	{
		vector<int> ma = maOrderingHeap(H, s);
		int v = ma[ma.size() - 2];
		int w = ma[ma.size() - 1];
		if (v == s || w == s)
			throw logic_error("SET WAS V - S, S FOUND");
		vector<int> X1;
		H = combineVertices(H, v, w);
		H = compress(H);
		XS[v] = setUnion(XS[v], XS[w]);
		if (XS[w].size() == 0)
		{
			cout << "Error: W, " << w << " was merged twice. Quitting" << endl;
			throw logic_error("");
		}
		XS[w] = vector<int>();
		if (cG(v, H) < k)
		{
			int numToGet = (int)ceil(.5*(double(k) - double(cG(G1, XS[v]))));
			vector<SplitEdge> GX = inducedSubgraph(G1, XS[v]);
			vector<SplitEdge> delB;
			int added = 0;
			for (unsigned i = 0; i < GX.size(); i++)
			{
				SplitEdge e = SplitEdge(GX[i].end0, GX[i].end1, GX[i].weight, GX[i].orig0, GX[i].orig1);
				if (isMem(e, B))
				{
					int bW = B[indexOfEdge(B, e.end0, e.end1)].weight;
					if (bW < e.weight)
						e.weight = bW;
					if (e.weight > (numToGet - added))
					{
						e.weight = numToGet - added;
					}
					added += e.weight;
					delB.push_back(e);
				}
				if (added == numToGet)
					break;
			}
			if (added != numToGet)
			{
				cout << "Error: GX did not contain " << numToGet << " entries in B. Quitting." << endl;
				throw logic_error("");
			}
			if (!isSubset(delB, B))
			{
				cout << "ERROR: delB is not a subset of B." << endl;
				cout << "B:" << endl;
				output(B);
				cout << "delB:" << endl;
				output(delB);
				cout << "This was the GX to choose from:" << endl;
				output(GX);
				cout << "V: " << v << endl;
				cout << "W: " << w << endl;
				cout << "S: " << s << endl;
				throw logic_error("");
			}
			B = setRemove(delB, B);
			B = removeZeroWeighted(B);
			B1 = setUnion(delB, B1);
			H = removeZeroWeighted(H);
			G1 = hookUpHelper(s, G1, delB, h);
			G1 = removeZeroWeighted(G1);
			H = removeZeroWeighted(H);
			bool addedFromXSinH = false;
			numToGet *= 2;
			for (unsigned i = 0; i < H.size(); i++)
			{
				SplitEdge tester = SplitEdge(s, v, 0, 0, 0);
				if (equals(tester, H[i]))
				{
					//cout << "Increasing weight in hookUp in H between " << H[i].end0 << " and " << H[i].end1 << "from " << H[i].weight << " to " << H[i].weight + numToGet << endl;
					H[i].weight += numToGet;
					addedFromXSinH = true;
					break;
				}
			}
			if (!addedFromXSinH && numToGet != 0)
			{
				//cout << "Creating edge in hookUp in H between " << s << " and " << v << " with weight " << numToGet << endl;
				SplitEdge e(s, v, numToGet, s, v);
				H.push_back(e);
			}
			vector<vector<int>> newY;
			for (unsigned i = 0; i < Y.size(); i++)
			{
				if (!isProperSubset(Y[i], XS[v]))
					newY.push_back(Y[i]);
			}
			bool foundX1inY = false;
			for (unsigned i = 0; i < newY.size(); i++)
			{
				if (setsEqual(newY[i], XS[v]))
					foundX1inY = true;
			}
			if (!foundX1inY)
				newY.push_back(XS[v]);
			Y = newY;
		}
	}
	huReturn ret;
	ret.BP = B1;
	ret.G1 = G1;
	ret.Y = Y;
	return ret;
}