Good afternoon,
I have to create a program to only be used as a tool (no main function). Below is the background information related to task:
Your goal for this assignment is to create a tool that manages an equivalence relation that can be changed by the program. The equivalence relation is always over a set of integers {1, 2, 3, …, n} for some n.
This tool is not a complete program. It is intended to be part of a larger program. It must not have a main function.
For this assignment, an equivalence relation has type ER. A module that uses this tool can create an equivalence relation called e by saying
- ER R = newER(n);
where n is an integer. Initially, each number is in its own equivalence class; the equivalence classes are {1}, {2}, …, {n}. There are two operations that can be performed.
equivalent(R, x, y) yields true if x and y are currently in the same equivalence class in equivalence relation R.
- merge(R, x, y) modifies equivalence relation R by making x and y equivalent. It combines the equivalence class that contains x with the equivalence class that contains y. The merge function does not yield an answer.
You will not store the equivalence classes directly. Instead, you will store them implicitly, using the following ideas. You are required to implement an equivalence manager in this way. You will receive no credit for a module that does not follow this algorithm.
Each equivalence class has a leader, which is one of the members of that equivalence class. You will create a function leader(R, x) that yields the current leader of the equivalence class that contains x in equivalence relation R. Two values are equivalent if they have the same leader.
There is another idea that is similar to a leader, but not exactly the same. Each value has a boss, which is a value in its equivalence class. Let's write boss[x] for x's boss.
If x is the leader of its equivalence class then boss[x] = 0, indicating that x does not have a boss. If x is not the leader then boss[x] ≠ x and boss[x] is closer to the leader. If you look at the values x, boss[x], boss[boss[x]], boss[boss[boss[x]]], … then you will eventually encounter x's leader.
Use an array to store the bosses. Write the following functions.
- newER(n) returns an array of n+1 integers. Allocate the array in the heap. This array will be used to store the bosses. So, if R has type ER then R[x] is x's boss. (If you prefer, you can call an equivalence relation boss. Then boss[x] is x's boss.)
In C++, arrays start at index 0. You will use indices 1, … n, so you need to allocate n+1 slots. (Index 0 will not be used.)
Initialize the array so that each value is a leader of its own group. That is, boss[x] = 0 for x = 1, …, n.
-
leader(R, x) returns the leader of x in equivalence relation R. To compute x's leader, just follow the bosses up to the leader. You are required to use this function any time you want to find the leader of a value.
-
equivalent(R, x, y) returns true if x and y are currently in the same equivalence class in equivalence relation R. (They are in the same equivalence class if they have the same leader.)
-
merge(R, x, y) merges the equivalence classes of x and y in R as follows. First, it finds the leaders x′ and y′ of x and y. If x′ and y′ are different (so x and y are not already in the same equivalence class) then y′ becomes the new boss of x′. (How can you changes x′'s boss?) So y′ becomes the leader of the combined equivalence class.
-
destroyER(R) deallocates R.
- showER(R, n) prints the entire contents of array R (of size n) in a readable form for debugging.
Important note. It is crucial that your program never change the boss of a value that is not a leader.
The professor provided the following header (I'm glad he did, gives me a better understanding on how he likes his contracts. I do know he doesn't want any body comments):
// These #ifndef and #define lines make it so that, if this file is
// read more than once by the compiler, its body is skipped on all
// but the first time it is read.
#ifndef EQUIV_H
#define EQUIV_H
// An equivalence relation is an array of integers.
// So ER abbreviates int*.
typedef int* ER;
// Public functions
ER newER (int n);
void destroyER (ER e);
bool equivalent (ER e, int x, int y);
void merge (ER e, int x, int y);
// The following is advertised here solely for debugging. These must
// only be used for debugging.
void showER(ER e, int n);
int leader(ER e, int x);
#endif
Below is what I have so far. I'm looking to change ER e to R, but not sure if that is possible. Based off of what I provided, am I at least on the right path? https://code.sololearn.com/c5850jWvbHRj/#cpp
// These #ifndef and #define lines make it so that, if this file is
// read more than once by the compiler, its body is skipped on all
// but the first time it is read.
#ifndef EQUIV_H
#define EQUIV_H
// An equivalence relation is an array of integers.
// So ER abbreviates int*.
typedef int* ER;
// Public functions ///////////////////////////////////////////////////
/*
// ER new ER(int n)
// Returns an array of 'n' + 1 integers
*/
ER newER(int n)
{
int* R = new int[n];
for (int index = 1; index < n; index++)
{
R[index] = (n + 1);
}
return R;
}
/*
// destroyER(Er)
// Deallocates R.
*/
void destroyER(ER e)
{
// Changed ER e to R.
delete R;
int* R = NULL;
// Is this considered a dangling pointer?
}
/*
// equivalent(ER e, int x, int y)
// Returns true if x and y are currently in the same equivalence class in equivalence relation R.
*/
bool equivalent (ER e, int x, int y);
{
}
/*
// merge(ER e, int x, int y)
// Merges the equivalnece classes of x and y in R.
*/
void merge(ER e, int x, int y);
{
}
////////////////////////////////////////////////////////////////////
// The following is advertised here solely for debugging. These must
// only be used for debugging.
///////////////////////////////////////////////////////////////////
/*
// showER(ER e, int n)
// Prints the entire contents of array R (of size n) in a readable form for debugging.
*/
void showER(ER e, int n);
{
}
/*
// leader(ER e, int x)
// Returns the leader of x in equivalence relation R
*/
int leader(ER e, int x);
{
// I changed ER e to R
if (R[x] == 0)
{
return x;
}
else
{
return leader(R, R[x])
}
}
#endif