Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
void project::step_nine()
{
	int **faltu_matrix;
	faltu_matrix = new int *[temp_tasks];
	for( int i=0; i<temp_tasks; i++)
	{
		faltu_matrix[i] = new int[temp_processors];
	}
	for(int i=0; i<temp_tasks;i++)
	{
		for(int j=0; j<temp_processors;j++)
		{
			faltu_matrix[i][j] = 0;
		}
	}

	for(int i=0; i<temp_tasks;i++)
	{
		for(int j=0; j<temp_processors;j++)
		{
			if(temp_Matrix[i][j] == 0)
			{
				faltu_matrix[i][j] =1;
			}
			
		}
	}

	for(int i=0; i<temp_tasks;i++)
	{
		for(int j=0; j<temp_processors;j++)
		{
			cout<<faltu_matrix[i][j]<<"    ";
		}
		cout<<endl;
	}	
}


suppose i got a matrix

0 1 0 0 2
0 0 0 4 0
5 0 2 1 3
4 4 5 0 0
0 3 2 0 2
Select a matching by choosing a set of zeros so that each row or column has only one selected. It means that i should have only one zero selected from each row and column!
i implemented several of my logics in mind but there comes a problem in every one! kindly help me! my project is badly stuck at this point just because of this stupid step!
i have to make a new matrix 2d in which the zeros from rows and columns should be selected in a way that they are unique!!

like in this case output should be
1 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 0 0 0 1
0 0 0 1 0
PROBLEM IAM FACING:

the implementation i did had the problem that if in the 2nd row i select the second element
1 0 0 0 0
0 1 0 0 0
then for row three the rule was violated as there was no zero in the original matrix to maintain uniqueness
Posted
Updated 4-May-12 16:56pm
v3
Comments
Sergey Alexandrovich Kryukov 3-May-12 17:19pm    
It is a question? What is the problem? What does "selected" means?
Where is your code and what's not working in it?
--SA
Haris Riaz 3-May-12 17:25pm    
well i hope you get some sense out of the question!! read my post!! problem is tricky!! ive been stuck on it all day sir!
Haris Riaz 3-May-12 17:27pm    
i have now a matrix of all 1 at places where there 0 in the original matrix!! tried different dry runs for logic to solve the uniqueness thing!! but none logic worked currectly!! problem with logic mentioned in post now!!
CPallini 3-May-12 17:37pm    
Your task is not clear (at least to me). You have an input matrix. What kind of manipulations may you perform? What is the target (I cannot understand it from your post, so, please, elaborate)?
Haris Riaz 3-May-12 17:47pm    
http://www.wikihow.com/Use-the-Hungarian-Algorithm
everyone please have a look at the link! am using this method in my project! step 9 now look at that!! that is where am stuck!! hope you all get it now!

How about this, there're probably much better solutions though:
You iterate through the rows, in every row you select the very first zero, after you reach the last row, you have this:
1. first zero selected
2. first zero selected
...
N. first zero selected
Then you check if this selection fits the requirement, if yes, then you are done, if not, then you select the second zero (if any) in the last row:
1. first zero
2. first zero
...
N. second zero
and check again, if it fits now, you are done, if not, then you select the next zero and so on until you run out of zeroes. Then you will select the second zero on the previous row and the first in the last row:
1. first zero
2. first zero
...
(N-1). second zero
N. first zero
And check again if it fits, if it does, you are done, if it doesn't, you move on to the next zero:
1. first zero
2. first zero
...
(N-1). second zero
N. second zero
and so on until you either find a setup that works or you run out of combinations. I think this is called a "trackback algorithm". Can be relatively easily implemented with recursion.
 
Share this answer
 
Comments
Haris Riaz 3-May-12 17:49pm    
am not good at recursion buddy! i got an idea from your post! working on it right now!!
Code-o-mat 4-May-12 6:33am    
You don't need to do it with recurstion, it's just one possible way of doing it...
Haris Riaz 3-May-12 17:51pm    
i request you to kindly give me a recursive algo for this one and i can implement it!
nv3 3-May-12 18:28pm    
The algorithm of Code-o-mat might not be the fastest, but it is definitely a solution. Allocate an int array with size equal to the number of rows and set each element to the index of the first zero. When the check for valid solution fails change the according cells and enumerate all possible solutions.

The name of the algorithm described by Code-o-mat is by the way "Backtracking" and you will certainly find a lot of materials in the internet about it.
Code-o-mat 4-May-12 6:34am    
"Backtracking" then, thanks, i was close. :)
The above solution could also be implemented like a (n) digit number.

1. Start with each digit at the location of the 0 for the row.

2. Test to see if this a solution. Each digit should be unique.

3. Then increment it like you would a base (n) number. But if the digit doesn't fall on a zero, then keep incrementing. If it wraps, carry the '10' and increment the next column.

4. Is this the solution?
a. True -> Quit
b. False -> goto 3.
c. Wrapped on zero digit? -> Quit, no solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900