I am trying to write a backtracking algorithm for harmonious graph coloring (no adjacent colors can be the same, and also each pair of colors can appear only once).
This is the backtracking function:
void Graph::colorBacktrack(int a, int b) {
print();
for (int i = 1; i <= colors; i++) //assigning colors until match
{
elems[a][b] = i; //color
if (check(a, b)) break; //if it's alright break
if (i == colors) // if all the colors have been used -
return; //return one stack back and look again
}
int nextA;
int nextB = b;
if (a < size - 1 ) nextA = a + 1;
else {
nextA = 0;
nextB = b + 1;
}
if (a == size && b == size - 1) //when array is complete - cout finished
{
cout << endl << endl << "Finished" << endl << endl;
}
colorBacktrack(nextA, nextB); //go to next node when everything is fine
}
checking is working properly and all the other things. The problem is that the output is wrong - at the end it shows something like this:
1 4 2 6
2 5 7 8
3 6 4 8
<--- this is wrong
1 7 3 0
<--- this is also wrong
So when it cannot find a solution in current tree it just ends everything instead of going up. Why is it happening?
Related
The CSES problem Josephus Problem I requires us to print the sequence of how people are chosen for n people and k = 2. I found an elegant solution to this here.
Basically, the code is similar to this:
void J(int n)
{
int a = 1, b = 0;
while (n > 0)
{
for (int i = 2; i <= n; i += 2)
{
cout << a * i + b << ' ';
}
if (n & 1)
cout << a + b << ' ', b += a;
else
b -= a;
a <<= 1;
n >>= 1;
}
}
Can someone explain why it works?
I made a few changes to the code to illustrate how it works.
void J(int n)
{
int a = 1, b = 0;
while (n > 0)
{
std::cout << "n=" << n << std::endl;
std::cout << "a=" << a << ", b=" << b << std::endl;
for (int i = 2; i <= n; i += 2)
{
std::cout << a * i + b << ' ';
}
if (n & 1) // equivalent to n % 2 == 1 (if n is odd)
std::cout << '*' << a + b << ' ', b += a;
else
b -= a;
std::cout << std::endl;
a <<= 1; // a = a * 2;
n >>= 1; // n = n / 2;
}
}
The first thing to note is that the variables a and b are initialized to values that allow us to compute the positions of people who will be eliminated as we go around the circle (also using the iterator i in the for loop). The initial values of a = 1, b = 0 are so we can eliminate every even numbered person on the first iteration.
Next, every time through the while(n) loop, half of the remaining people will be eliminated. If n is odd, we loop back to the beginning of the circle and also eliminate the person at the start of the current iteration.
I think the tricky part here is how the values of a and b change in the if statement. If n is odd we increment b by the value of a. If n is even we subtract the value of a from b. Then we double the value of a to get ready for the next iteration around the circle. The values of a and b are chosen to account for the gaps in the circle left by eliminating positions in the previous iterations.
Finally, we divide n by 2 before the next iteration, since there are half as many people left in the circle. Here's a sample run for n=19.
Output:
n=19
a=1, b=0
2 4 6 8 10 12 14 16 18 *1
n=9
a=2, b=1
5 9 13 17 *3
n=4
a=4, b=3
11 19
n=2
a=8, b=-1
15
n=1
a=16, b=-9
*7
I have vector of integers which is filled only by 1 or 0 values. What I am trying to make is that when the current value is 1 and previous/old is 0 or opposite if Current=0 and Previous=1, then to assign another variable(AvgCan) to 0.
I am trying to get from FOR condition previous value. However, if I try it the usual way I still get the same value all the time until the loop end. The issue is in the first if-statement.
int AvgCan = 0;
int OldAvgCan = 0;
int iteration = 0;
int iterationDecrease = 0;
for (int i = 0; i < resultINT.size(); i++)
{
//myFileO << to_string(resultINT.at(i)) + "\n";
cout << to_string((resultINT.at(i))) + " Current" + "\n";
cout << to_string((resultINT.at(i - iteration))) + " Old" + "\n" << endl;
cout << to_string(AvgCan) + "\n" << endl;
iteration = i;
iterationDecrease = i - 1;
if ((resultINT.at(i)) != (resultINT.at(iteration - iterationDecrease)))
{
AvgCan = 0;
}
if ((resultINT.at(i)) == 1)
{
/*if ((resultINT.at(i- iteration)) != 1)
{
AvgCan = 0;
}*/
AvgCan++;
}
if ((resultINT.at(i)) == 0)
{
/*if ((resultINT.at(i- iteration))!=0 )
{
AvgCan = 0;
}*/
AvgCan--;
}
myFileO << to_string(AvgCan) + "\n";
}
As you can see I assigned iterator i to iteration variable and i - 1 to iterationDecrease. (I also tried i-- and similar possible ways.)
I simulated the data so the results are 1,1,1,1,0,0,0,0. When it is changing from 1 to 0 and it gets to the if condition, but each next iteration it still returns 1 like old values, even when it's 0.
I am adding also screenshot for better understanding. On the right side is output in the console.
Here
iteration = i;
iterationDecrease = i - 1;
if ((resultINT.at(i)) != (resultINT.at(iteration - iterationDecrease)))
the iteration - iterationDecrease is equal to i - (i - 1) which is always 1. Meaning in effect that if the statement is equivalent to
if ((resultINT.at(i)) != (resultINT.at(1)))
You probably meant
if (resultINT.at(iteration) != resultINT.at(iterationDecrease))
which is still not correct, as when i==0, the iterationDecrease = -1 which will throw an exception, for the call .at(-1)
You need to start from i=1 in the loop therefore,
for (int i = 1; i < resultINT.size(); i++)
{
// ...code
if (resultINT[i] != resultINT[i-1])
{
// ...code
}
}
Think about the expression iteration - iterationDecrease. It has a constant value 1.
You wished probably resultINT.at(iterationDecrease), but it will cause an exception on the first iteration, since it will try to access -1 element.
Avoid indexing when it is possible. It is not final solution but it may show you right direction. Use adjacent_find to find pairs. I made this example for you here.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec{ 0,0,1,1,0,0,0,1,0,1,0,0,0,0,1,1 };
auto it = std::begin(vec);
while (1) {
it = std::adjacent_find(it, std::end(vec), [](int v1, int v2) {
return std::min(v1, v2) == 0 && std::max(v1, v2) == 1;
});
if (it != std::end(vec)) {
std::cout << "Pairs " << *it << " and " << *(it + 1) << " with indexes "
<< std::distance(std::begin(vec), it) << " and "
<< std::distance(std::begin(vec), it + 1) << std::endl;
++it;
}
else {
break;
}
}
return 0;
}
Output
Pairs 0 and 1 with indexes 1 and 2
Pairs 1 and 0 with indexes 3 and 4
Pairs 0 and 1 with indexes 6 and 7
Pairs 1 and 0 with indexes 7 and 8
Pairs 0 and 1 with indexes 8 and 9
Pairs 1 and 0 with indexes 9 and 10
Pairs 0 and 1 with indexes 13 and 14
I have an assignment in which I am supposed to
1.) Read in 2 char arrays of numbers
2.) add together the arrays, performing carry addition, carrying the tens place
3.) print out the newly added array.
I am also supposed to print an error if I need to carry on the last digit of the array (overflow)
so something like 99999999999999999999 +
1 =
________________________
ERROR
That's the part I'm having trouble with.
The above outputs something like "99999999999999999:0" so I have no idea what's going wrong.
I'll post my code, but please be nice :( I know it certainly isn't the most efficient, but I'm just trying to lay things out in a way that is easy for my brain to understand right now.
And yes, I HAVE to use char arrays. I guess it's to help us understand the ascii table.
#include <iostream>
using namespace std;
void InitNumber(char[]);
int AddArrays(char first[], char second[], char combined[]);
void OutputNumber (char[]);
const int LENGTH = 20; // global variable
int main()
{
char set1[LENGTH];
char set2[LENGTH];
char sum[LENGTH];
InitNumber (set1);
InitNumber (set2);
if(AddArrays (set1, set2, sum)) {
cout << "overflow!" << endl;
}
OutputNumber(sum);
}
void InitNumber (char list[])
{
int numberOfDigits;
cout << "Please enter the number of digits in the number: ";
cin >> numberOfDigits;
cout << "Please enter the digits in the number with the LEAST significant first: ";
for (int i = 0; i < numberOfDigits; i++) {
cin >> list [i];
}
for (int l=(numberOfDigits); l < LENGTH; l++) {
list [l] = '0';
}
}
int AddArrays(char first[], char second[], char combined[])
{
for (int h = 0; h < LENGTH; h++)
combined[h]= '0';
int overflow = 0;
for (int i = 0; i < LENGTH; i++) {
int currentSum = (first[i]-'0' + second[i]-'0');
cout << "currentSum = " << currentSum << endl;
if(currentSum / 10 == 0 )
combined[i] += currentSum;
else if (currentSum/10 !=0) {
if (i == LENGTH-1 && currentSum/10 !=0){
overflow = 1;
}
else{
combined [i] += currentSum%10;
cout << "current i: " << combined[i] << endl;
combined [i+1] += currentSum/10;
cout << "current i+1: " << combined[i+1] << endl;
}
}
}
return overflow;
}
void OutputNumber(char arrayOut[])
{
for (int l=LENGTH - 1; l >= 0; l--)
cout << arrayOut[l];
}
working input
input
6
1 2 3 4 5 6
7
1 2 3 4 5 6 7
output
00000000000008308642
Not-working output
input
20
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
1
1
output
999999999999999999:0
I'm going to reproduce small parts of your inner loop where you are carrying out the addition, in order to explain why your overflow detection is broken.
for (int i = 0; i < LENGTH; i++) {
int currentSum = (first[i]-'0' + second[i]-'0');
if(currentSum / 10 == 0 )
combined[i] += currentSum;
Here you're adding the corresponding pair of digits from the two numbers you're adding, and checking (in your particular style) if they overflowed. Try to remember this part, it's important. In order to check for overflow, you're only checking the result of adding the pair of digits from the two large numbers you're adding.
else if (currentSum/10 !=0) {
This is a completely useless check. You can only get here if the division is known to produce a non-zero result. This if() can be removed completely. Now, the relevant part of your code is
combined [i] += currentSum%10;
combined [i+1] += currentSum/10;
Do you see the problem yet?
Your approach is, once overflow is detected, is to increment the next higher order digit in the result.
Unfortunately, on the next loop iteration, in order to check for carry over, you're just checking the sum of the next corresponding digit pair, from the two large numbers you're adding. The carry-over you're saving here is going to get completely ignored.
Say your numbers are two digits long max, rather than 20, and you entered the numbers 99 and 1.
On the first iteration, you'll add 9 and 1, save 0 as the first digit, and add 1 to the second digit in the sum.
On the second iteration, you'll add 9 and 0, and, given your logic above, conclude that nothing overflowed.
I need your help with this problem. What I want to know is how to output of a loop based on the input.
Let's say we have a program that should measure if a triangle is right or not based on the inputs of the user. The input could be something like this:
6 8 10
25 52 60
5 12 13
Using the Pythagoras formula, we can determine if a triangle is or not right
C^2=a^2+b^2
Now, with the numbers provided, the output should be:
right
wrong
right
My question is..how can I do the calculation and check if it's right or not but format the output with the same order as the input?
This is what I've tried :
#include <iostream>
#include <cmath>
using namespace std;
int rightt;
int wrong;
int main()
{
double a = 0, b = 0, c = 0;
double formula = 0;
for (int i = 0; i < 1;)
{
cin >> a >> b >> c;
formula = pow(a, 2) + pow(b, 2);
if (formula == pow(c, 2) && formula != 0)
{
//cout << "Right";
rightt = rightt + 1;
}
if (formula != pow(c, 2) && formula != 0)
{
//cout << "Wrong";
wrong = wrong + 1;
}
if (a == 0 && b == 0 && c == 0)
{
i = 1;
cout << "\n";
while (rightt > 0)
{
cout << "\n" << "Right";
rightt = rightt - 1;
}
while (wrong > 0)
{
cout << "\n" << "Wrong";
wrong = wrong - 1;
}
}
}
system("pause");
}
But my output is not as I desired. The output is first set the right, and then the wrong ones. Thanks, and I hope you understand my problem.
EDIT:
I need to have the output after the 0 0 0 is reached and not before. So If I left the commented sections , the output will be Number-output-Number-output , and what I need is to allow users to enter all numbers and tell the software that he finishes when he enters 0 0 0 , and after that give the output based on the order.
Let's imagine this input :
6 8 10 >> this is right
25 52 60 >> This is wrong
5 12 13 >> This is right
0 0 0 >> This is the values used to end the inputs
Output should be
right
wrong
right
I think that rather than counting the number of right answers and wrong answers, you can STORE all of your answers IN ORDER, in an vector. Once you are done storing all your answers, you can just loop through the answers, and print them out one by one.
If you have not learned about vectors yet, the concept is simple... you have an array like collection of data. "push_back" always tacks the data to the end of the collection of data. So if your first answer was wrong, then right, then right, first you would push_back(wrong)...resulting in a collection of [wrong]. Then you would push_back(right) resulting in a collection of [wrong, right]. Again you would push_back(right) so your final vector would be a collection in the order of [wrong, right, right]
Now you just need to loop through your collection to print out the data. The "iter" is a pointer to each spot in your list. To get the "contents of each spot" you dereference, by saying (*iter) which will provide the string result values.
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
using namespace std;
int main()
{
double a = 0, b = 0, c = 0;
double formula = 0;
int numberOfResults = 0;
int currentIndex = 0;
vector<string> answers;
for (int i = 0; i < 1;)
{
cout << "Enter the number of attempts: " << "\n";
cin >> numberOfResults;
string results[numberOfResults];
cout << "Enter a b and c" << "\n";
cin >> a >> b >> c;
formula = pow(a, 2) + pow(b, 2);
if (formula == pow(c, 2) && formula != 0)
{
results[currentIndex] = "Right";
answers.push_back("Right");
}
if (formula != pow(c, 2) && formula != 0)
{
results[currentIndex] = "Wrong";
answers.push_back("Wrong");
}
if (a == 0 && b == 0 && c == 0 || currentIndex == numberOfResults-1)
{
for (int j = 0; j < numberOfResults; j++){
cout << "\n" << results[j];
}
for(auto iter = answers.begin(); iter != answers.end(); ++iter){
cout << "\n" << (*iter);
}
return 0;
}
}
system("pause");
}
Given a n*n matrix and a value k, how do we find all the neighbors for each element?
for example: in a 4*4 matrix, with k=2
say matrix is :
[ 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16]
where these values are the indexes of the location, the neighbors for 1 are 1,2,3,5,6,9 . The values 3,6 and 9 come only because k =2 and wouldnt be there if k was = 1.
similarly the neighbors of 6 will be 1 2 3 5 6 7 8 9 10 11 and 14
Can you please help me to write a c code to implement this in c++.
It is the problem of von Neumann neighborhood, please can some one implement it in c++. Thanks
Your neighbors will form a diamond pattern around your target element. The points of the diamond will be k hops away from the target element. So the top will be k rows up, the left will be k columns over, etc. The diamond expands uniformly as you go from level to level. If you start at the top point and go one row down (closer to the target node) then you go out 1 to each side. It's symmetric in the other directions. In other words, the difference in x coordinates between a neighbor and the target node plus the difference in y will be <= k.
So just make two nested for loops that iterate over this diamond. Outer loop iterates over the rows, inner loop over the columns. Start at the top then expand the diamond by 1 at each outer loop iteration until you reach the same row as the target element, then contract until you reach the bottom point.
Obviously you'll need to test boundary conditions for going outside the matrix.
This should do the trick for k=1. Make minor changes to make it work for all k
int width = 4;
int height = 4;
int k = 1;
int value = 2;
bool hasRight = (value % width != 0);
bool hasLeft = (value % width != 1);
bool hasTop = (value > 4);
bool hasBottom = (value < (height * width - width));
cout << value; // Always itself
if(hasRight == true) {
cout << value+1 << " "; // Right
if(hasTop == true) {
cout << value-width << " " << value-width+1 << " "; // Top and Top-right
}
if(hasBottom == true) {
cout << value+width << " " << value+width+1; // Bottom and Bottom-right
}
}
if(hasLeft == true) {
cout << value-1 << " "; // Left
if(hasTop == true) {
cout << value-width-1 << " "; // Top-left
}
if(hasBottom == true) {
cout << value+width-1 << " "; // Bottom-left
}
}