Ask Experts Questions for FREE Help !
Ask
    prasert2014's Avatar
    prasert2014 Posts: 1, Reputation: 1
    New Member
     
    #1

    May 18, 2014, 07:39 PM
    How can we get all unique row and column paths in M*N Matrix
    Now I implemented a easy source code to find all possible paths in matrix.Then selected the unique row and column.But due to time complexity how can we can get directly unique path.this is my source code.
    Code:
    import java.util.*;    public class MyAllPath {
        	public static ArrayList<String> allPaths;
        	public static void printpaths(double[][] matrix) {	
        		String[] path = new String[matrix.length];
    		
    		for (int i = 0; i < matrix[0].length; i++)
    		{
    			printpaths(matrix, path, 0, 0, i);
    		}
    	}
    	
    	private static void printpaths(double[][] matrix, String[] path, int index, int row, int column)
    	{
    		path[index++] = Integer.toString(column)+"|"+Double.toString(matrix[row][column])+"\t";
    		row++;
    		if (row == matrix.length)
    			{
    				print(path);
    			}
    		else if (row < matrix.length) 
    		{
    			int boundary = matrix[0].length-1;
    			for (int i = column - boundary; i <= column + boundary; i++)
    			{
    				if (i > -1 && i < matrix[0].length)
    				{
    					printpaths(matrix, path, index, row, i);
    				}
    			}
    		}
    	}
    	
    	private static void print(String[] path) 
    	{
    		String myPath="";
    		for (int i = 0; i < path.length; i++)
    		{
    			myPath+=path[i]+" ";
    			System.out.print(path[i] + " ");
    		}
    		System.out.println();
    		allPaths.add(myPath);
    	}
    	
    	
    	public static void main(String args[]) 
    	{
    		allPaths =new ArrayList<String>();
    		double[][] matrix = {{1, 2, 3},
    							{4, 5, 6}, 
    						    {7, 8, 9}};
    		printpaths(matrix);
    		System.out.println("-------------------------------------------------------");
    		for(int i=0;i<allPaths.size();i++)
    		{
    			String[] path= allPaths.get(i).split(" ");
    			TreeSet<Integer> duplicateColumn = new TreeSet<Integer>();
    			for(int j= 0;j<path.length;j++)
    			{
    				String[] word=path[j].split("\\|");
    				
    				for(int k=0;k<word.length;k++)
    				{
    					duplicateColumn.add(Integer.parseInt(word[0]));
    				}
    			}
    			if(duplicateColumn.size()==path.length)
    				System.out.println(allPaths.get(i));
    			
    		}
    	}
        }

Check out some similar questions!

how can we pass a value from a column of first row to a column of second row [ 0 Answers ]

how can we pass a value from a column of first row to a column of second row my code is like this but it is not working for(int i=model.getRowCount();i<model.getRowCount();i++) { for(int j=0;j<model.getColumnCount();j++) { ...

How to calculate row and column reference in access? [ 1 Answers ]

Dear Sir, I have am facing problem in access programme development if you can do so please solve it at the earliest. I have to created 5 tables for the determination of sales individually and then merged all the tables to each other to find out the actual transaction in the whole day. Fields of...

Column and row strategies for matrixes [ 0 Answers ]

M= 1 -3 convert M to a positive matrix M* by adding 4 to each entry in M. a. Determine the optimal row and column strategies for matrix M b. Determine the optimal row and column strategies for matrix M* c. Determine the respective values of each matrix M, M*, how do these values...

Excel - Copying a row to a column [ 3 Answers ]

I have a list of data in Row 1 that I would like to be displayed down Column A instead. Is there an easy way to transfer it from one to the other?

How to find unique values in an entire column [ 10 Answers ]

hi all please tell me how can I findout the unique values in an entire coloumn. for example I have 3 coloumn in an sheet. Name Prod. Value a 24 100 b 26 240 a 33 120 a 21 200


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.