Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when I have the Matrix below

          X1	 X2	 X3	 X4
Y1	1.00	0.11	0.07	0.50
Y2	0.22	0.07	0.30	0.14
Y3	0.00	0.06	0.06	0.08

how can I print all following path with java
X1Y1-> X2Y2-> X3Y3
            -> X4Y3
        X3Y2-> X2Y3
            -> X4Y3
        X4Y2-> X2Y3
            -> X3Y3
 X2Y1-> X1Y2-> X3Y3
            -> X4Y3
        X3Y2-> X1Y3
            -> X4Y3
        X4Y2-> X1Y3
            -> X3Y3
 X3Y1-> X1Y2-> X2Y3
            -> X4Y3
        X2Y2-> X1Y3
            -> X4Y3
        X4Y2-> X1Y3
            -> X2Y3

 X4Y1-> X1Y2-> X2Y3
            -> X3Y3
        X2Y2-> X1Y3
            -> X3Y3
        X1Y2-> X2Y3
            -> X3Y3


I try to do that by using Dijkstra's algorithm that can give me shortest path.
But for my work I need to look all possible path except duplicate column that show in above example. How can I do that???.Please help me!!! Due to my deadline.
But I have no idea :(
Posted
Updated 10-May-14 5:14am
v3
Comments
Maciej Los 9-May-14 15:52pm    
Does SearchBox is disabled?
Matrix operations in Java[^]
Emre Ataseven 10-May-14 17:24pm    
It is not related with matrix operations.
[no name] 10-May-14 11:30am    
What does that X1 X2.. Y1 Y2 and numbers in the matrix mean? I asked this because I cannot get the phrase "all possible path except duplicate column". In typical formulation of shortest path problems, both rows and columns are indexed with similar names.
Member 4065983 10-May-14 22:03pm    
Y.. are rows of Matrix
X.. are column of Matrix
and number are value in each XY
I define if X are the same with other XY mean duplicate column.

1 solution

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.
Java
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));
 
		}
	}
    }
 
Share this answer
 
v2

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