Results 1 to 4 of 4
  1. #1
    lkalaivanan is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Tell me efficient way

    In hardware topology , each cell will have x1 number of input lines ( called as fan-in ) and y1 number of output lines ( called as fan-out ).the requirement is to identify the nearest cell with maximum fan-out.The input given is X Y coordinate of new cell. Then we want to find nearest one from the csv file. then wants to produce it fan-out?

    For the problem definition , cell is defined by x-y co-ordinates.take a csv file as input.

    cell_no,cell_x,cell_y,fan-out
    1,4.6,30.23,56
    2,3.2,100.766,67
    3,3,2,98.766,23



    Java Code:
    import java.io.*;
    import java.util.regex.*;
    import java.util.Scanner;
    
    class showfile
    {
      public static void main(String args[]) throws Exception
      {
       int i=1,count=0,j=0,k,d=0;
       FileInputStream fin = new FileInputStream("empty");
       DataInputStream in=new DataInputStream(fin);
       BufferedReader br=new BufferedReader(new InputStreamReader(in));
       String strline=null;
       String[] dummy=null;
       String x=null;
       double y[]=new double[6];
       double x2,y2;
       double x1,y1;
       double dist[]=new double[4];
       double copy[]=new double[4];
       double fanout[]=new double[3];
    
       Scanner user=new Scanner(System.in);
       System.out.println("Enter x value::\n");
       x1=user.nextDouble();
      
       user=new Scanner(System.in);
       System.out.println("\nEnter y value::\n");
       y1=user.nextDouble();
    
       System.out.println("\n\n*******Content of CSV file**********\n\n");
    
       while ((strline=br.readLine())!= null)
       {
        if(i>1)
        {
         int c=1;
         System.out.println(strline);
         dummy=strline.split(",");
         fanout[d]=Double.parseDouble(dummy[3]);
         d++;
         try{
         while(c<3) 
         {
          x=dummy[c];
          y[j]=Double.parseDouble(x);
          c++;
          j++;
         }
         }catch(NumberFormatException e){
          System.out.println("Number format exception for input string");
          }
        }
        else
        {
         System.out.println(strline);
        }
        i++;
       }
       
      count=y.length;
      
      i=0;
      j=0;
      while(i<count)
      {
        x2=y[i];
        i++;
        y2=y[i];
        dist[j]=Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
        j++;
        i++;
      }
      j--;  
      for(i=0;i<=j;i++)
      {
       copy[i]=dist[i];
       
      }  
    
      for(i=0;i<=j;i++)
      {
       for(k=i+1;k<=j;k++)
       {
        if(dist[i]>dist[k])
        {
         x2=dist[k];
         dist[k]=dist[i];
         dist[i]=x2;
        }
       }    
      }
      
       
      for(i=0;i<=j;i++)
      {
       if(dist[0]==copy[i])
       {
        break;
       }
      }
     
       System.out.println("\n \nfan-out= "+fanout[i]);
     
    fin.close();
       }
    }
    The code given above is working correctly but i want to know is there is any other efficint way to do it?
    Last edited by pbrockway2; 08-11-2011 at 11:33 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    When posting code, use the code tags. Put [code] at the start of your code and [/code] at the end: that way the code will be properly formatted when it appears in the forum.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    What does "efficient" mean? In particular if it includes maintainability of the code, consider

    * many small methods rather than a long deeply nested main(). Document what each method does.
    * use descriptive variables. The extra key strokes make for self documenting code.
    * define and use classes. A cell (with id, location and fan characteristics) looks like an ideal candidate for being modelled by a Cell class. A network of cells might be another class with the important behaviour of reporting the closest cell to a given location.

    ------

    As far as finding the closest element of a point set to a given location is concerned, there are better ways than brute forcing your way through the set. But get the code clean first as efficiency in this narrow sense requires storing the collection of cells in a more sorted order than merely an array indexed by their position in the data file.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pbrockway2 View Post
    As far as finding the closest element of a point set to a given location is concerned, there are better ways than brute forcing your way through the set. But get the code clean first as efficiency in this narrow sense requires storing the collection of cells in a more sorted order than merely an array indexed by their position in the data file.
    I don't know, because the OPs requirement is:

    Quote Originally Posted by OP
    "the requirement is to identify the nearest cell with maximum fan-out."
    If I read it correctly the requirement is either: find the cell with the maximum fan-out or (this is another interpretation), in case more than one cell are 'nearest' to the current cell, select the cell with the maximum fan-out from that subset of cells. If the first interpretation is correct all cells need to be checked; if the second interpretation is correct, the problem reduces to finding the nearest cell(s); this can be done by checking all cells in an expanding circle with the current cell as the centre of the circle. Next a brute force needs to be applied over those nearest cell(s).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Efficient image loading
    By aburnett397 in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2011, 03:44 PM
  2. is there a more efficient way?
    By Yakg in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 06:32 PM
  3. Need help making program more efficient
    By cid in forum New To Java
    Replies: 4
    Last Post: 06-30-2010, 08:22 PM
  4. Replies: 1
    Last Post: 03-22-2010, 09:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •