Hi I need help on a problem implementing a reduction of edges.
bool reduction();
If a vertex u has only one outgoing arc towards another vertex s and at least one incoming arc, then we will replace all the arcs of the form (p,u) if they exist, by (p,s), then we delete the vertex u and all the arcs which are adjacent to it.
We perform the symmetric operation where a vertex u has only one incoming arc and at least one outgoing arc.
here s a brief description of my class :
template <class T>
class Digraph
{
public:
Digraph();
~Digraph();
bool acyclic(T u, T v) const;
bool reduction();
private:
std::map<T, std::set<T>> graphe;
}
/*
Here s an exemple of adjacency list
0 : 5
1 : 0, 2, 6, 7
2 :
3 : 2, 4, 8, 9
4 : 10, 13
5 : 1
6 : 11
7 : 3
8 : 13
9 : 13
10 : 4
11 : 5, 7, 11, 12
12 : 7, 13
13 :
*/
Can someone help me with the implementation ? some hints, or anything ? Thanks a lot
EDIT :
How many iterations of reductions should be done? Until no more can be done?
- Perform reductions until none are possible.
Which vertex should reduction be started from?
- Start from any vertex (changing this constraint, for example, to vertex with greatest outgoing/ingoing degree is straightforward)
CodePudding user response:
The question has ambiguities, such as:
- How many iterations of reductions should be done? Until no more can be done?
- Which vertex should reduction be started from?
- What is the significance of declaring
bool acyclic(T u, T v) constin yourDigraphclass? (Aside: since yourDigraphis templated,Tshould have a well-defined==operator overloaded).
To be able to answer your question, let us make the following assumptions:
- Perform reductions until none are possible.
- Start from any vertex (changing this constraint, for example, to vertex with greatest outgoing/ingoing degree is straightforward)
A solution could be:
- Maintain a data structure
map<T, DegreeCount> degreeCountMapwhereDegreeCountis a defined as follows (note: degree definition)
struct DegreeCount
{
DegreeCount() : d_inDegrees(0), d_outDegrees(0){}
int d_inDegrees;
int d_outDegrees;
}
- Iterate through the vertices in your
graph(amap<T, set<T>>), and for each vertex over its set of adjacent vertices. This gives you a directed edgeu -> v. For each such edge, updatedegreeCountMapby incrementing thed_inDegreesofvand incrementing thed_outDegreesofuby1. - If
degreeCountMapis none empty, pick the first vertex that has and_outDegreesof exactly one, and and_inDegreesof at least1. If none exist, try the symmetric operation. That is, pick the first vertex that has and_inDegreesof exactly one, and and_outDegreesof at least1. If none exist, try the symmetric operation. If none exist, no more reductions are possible and we are done. - If a vertex was picked in
step 3, let's call itw, we can remove it. To do so, assuming the "first case", iterate overgraphexcludingw, if awis a member ofgraph[x], removewfromgraph[x]and decrementdegreeCountMap[x].d_outDegreesby1. Then deletewkey from the mapgraphand the mapdegreeCountMap. Similar logic for "symmetric case". Then repeatstep 3.
When Step 3 above can no longer be performed, no more reductions are possible and the algorithm is done.
Rough Algorithm Analysis:
- Runtime complexity:
O(|V|^3)where|V|is the number of unique vertices. Intuitively, this is because we preprocess the edge set instep 2takingO(|V|^2)there, and process the continually decreasing edge set at most|V|instep 3 & 4, thus that piece results inO(|V|^3)operations, and leads the total runtime. - Space complexity:
O(|V|)since only store a struct (constant size) for each vertex.
CodePudding user response:
We don't know what your method (bool acyclic(T u, T v)const;) do, we need to see your code so we ll see how to use it because u said " I don't have right to add structures to my Class".


