Search Results

Search found 5119 results on 205 pages for 'genetic algorithm'.

Page 17/205 | < Previous Page | 13 14 15 16 17 18 19 20 21 22 23 24  | Next Page >

  • Making an efficient algorithm

    - by James P.
    Here's my recent submission for the FB programming contest (qualifying round only requires to upload program output so source code doesn't matter). The objective is to find two squares that add up to a given value. I've left it as it is as an example. It does the job but is too slow for my liking. Here's the points that are obviously eating up time: List of squares is being recalculated for each call of getNumOfDoubleSquares(). This could be precalculated or extended when needed. Both squares are being checked for when it is only necessary to check for one (complements). There might be a more efficient way than a double-nested loop to find pairs. Other suggestions? Besides this particular problem, what do you look for when optimizing an algorithm? public static int getNumOfDoubleSquares( Integer target ){ int num = 0; ArrayList<Integer> squares = new ArrayList<Integer>(); ArrayList<Integer> found = new ArrayList<Integer>(); int squareValue = 0; for( int j=0; squareValue<=target; j++ ){ squares.add(j, squareValue); squareValue = (int)Math.pow(j+1,2); } int squareSum = 0; System.out.println( "Target=" + target ); for( int i = 0; i < squares.size(); i++ ){ int square1 = squares.get(i); for( int j = 0; j < squares.size(); j++ ){ int square2 = squares.get(j); squareSum = square1 + square2; if( squareSum == target && !found.contains( square1 ) && !found.contains( square2 ) ){ found.add(square1); found.add(square2); System.out.println( "Found !" + square1 +"+"+ square2 +"="+ squareSum); num++; } } } return num; }

    Read the article

  • Optimizing Jaro-Winkler algorithm

    - by Pentium10
    I have this code for Jaro-Winkler algorithm taken from this website. I need to run 150,000 times to get distance between differences. It takes a long time, as I run on an Android mobile device. Can it be optimized more? public class Jaro { /** * gets the similarity of the two strings using Jaro distance. * * @param string1 the first input string * @param string2 the second input string * @return a value between 0-1 of the similarity */ public float getSimilarity(final String string1, final String string2) { //get half the length of the string rounded up - (this is the distance used for acceptable transpositions) final int halflen = ((Math.min(string1.length(), string2.length())) / 2) + ((Math.min(string1.length(), string2.length())) % 2); //get common characters final StringBuffer common1 = getCommonCharacters(string1, string2, halflen); final StringBuffer common2 = getCommonCharacters(string2, string1, halflen); //check for zero in common if (common1.length() == 0 || common2.length() == 0) { return 0.0f; } //check for same length common strings returning 0.0f is not the same if (common1.length() != common2.length()) { return 0.0f; } //get the number of transpositions int transpositions = 0; int n=common1.length(); for (int i = 0; i < n; i++) { if (common1.charAt(i) != common2.charAt(i)) transpositions++; } transpositions /= 2.0f; //calculate jaro metric return (common1.length() / ((float) string1.length()) + common2.length() / ((float) string2.length()) + (common1.length() - transpositions) / ((float) common1.length())) / 3.0f; } /** * returns a string buffer of characters from string1 within string2 if they are of a given * distance seperation from the position in string1. * * @param string1 * @param string2 * @param distanceSep * @return a string buffer of characters from string1 within string2 if they are of a given * distance seperation from the position in string1 */ private static StringBuffer getCommonCharacters(final String string1, final String string2, final int distanceSep) { //create a return buffer of characters final StringBuffer returnCommons = new StringBuffer(); //create a copy of string2 for processing final StringBuffer copy = new StringBuffer(string2); //iterate over string1 int n=string1.length(); int m=string2.length(); for (int i = 0; i < n; i++) { final char ch = string1.charAt(i); //set boolean for quick loop exit if found boolean foundIt = false; //compare char with range of characters to either side for (int j = Math.max(0, i - distanceSep); !foundIt && j < Math.min(i + distanceSep, m - 1); j++) { //check if found if (copy.charAt(j) == ch) { foundIt = true; //append character found returnCommons.append(ch); //alter copied string2 for processing copy.setCharAt(j, (char)0); } } } return returnCommons; } } I mention that in the whole process I make just instance of the script, so only once jaro= new Jaro(); If you are going to test and need examples so not break the script, you will find it here, in another thread for python optimization.

    Read the article

  • Algorithm to select groups of similar items in 2d array

    - by mafutrct
    There is a 2d array of items (in my case they are called Intersections). A certain item is given as a start. The task is to find all items directly or indirectly connected to this item that satisfy a certain function. So the basic algorithm is like this: Add the start to the result list. Repeat until no modification: Add each item in the array that satisfies the function and touches any item in the result list to the result list. My current implementation looks like this: private IList<Intersection> SelectGroup ( Intersection start, Func<Intersection, Intersection, bool> select) { List<Intersection> result = new List<Intersection> (); Queue<Intersection> source = new Queue<Intersection> (); source.Enqueue (start); while (source.Any ()) { var s = source.Dequeue (); result.Add (s); foreach (var neighbour in Neighbours (s)) { if (select (start, neighbour) && !result.Contains (neighbour) && !source.Contains (neighbour)) { source.Enqueue (neighbour); } } } Debug.Assert (result.Distinct ().Count () == result.Count ()); Debug.Assert (result.All (x => select (x, result.First ()))); return result; } private List<Intersection> Neighbours (IIntersection intersection) { int x = intersection.X; int y = intersection.Y; List<Intersection> list = new List<Intersection> (); if (x > 1) { list.Add (GetIntersection (x - 1, y)); } if (y > 1) { list.Add (GetIntersection (x, y - 1)); } if (x < Size) { list.Add (GetIntersection (x + 1, y)); } if (y < Size) { list.Add (GetIntersection (x, y + 1)); } return list; } (The select function takes a start item and returns true iff the second item satisfies.) This does its job and turned out to be reasonable fast for the usual array sizes (about 20*20). However, I'm interested in further improvements. Any ideas? Example (X satisfies in relation to other Xs, . does never satisfy): .... XX.. .XX. X... In this case, there are 2 groups: a central group of 4 items and a group of a single item in the lower left. Selecting the group (for instance by starting item [2, 2]) returns the former, while the latter can be selected using the starting item and sole return value [0, 3]. Example 2: .A.. ..BB A.AA This time there are 4 groups. The 3 A groups are not connected, so they are returned as separate groups. The bigger A and B groups are connected, but A does not related to B so they are returned as separate groups.

    Read the article

  • Problem Solving: Algorithm Required Urgently, Plz Help

    - by user616417
    Problem Solving: I've been working on something since last week. I am stuck at a point, where I want to find the minimum number of airplanes required to carry out a flight schedule given below. Plz, try out the brainstorming, i need the algorithm really badly, i'm also short of time. Thank u in advance. The Schedule---- Flight #,From,To,Departure,Arrival,Days,Via 6E 204,Agartala,Delhi,10:15:00,13:55:00,Daily,Kolkata 6E 360,Agartala,Imphal,13:50:00,14:35:00,Mo Th Sa, 6E 204,Agartala,Kolkata,10:15:00,11:00:00,Daily, 6E 360,Agartala,Kolkata,13:50:00,16:15:00,Mo Th Sa,Imphal 6E 362,Agartala,Kolkata,15:25:00,16:15:00,Tu We Fr Su, 6E 153,Ahmedabad,Bangalore,17:00:00,19:00:00,Daily, 6E 212,Ahmedabad,Chennai,9:00:00,12:55:00,Daily,Mumbai 6E 154,Ahmedabad,Delhi,12:30:00,14:00:00,Daily, 6E 211,Ahmedabad,Jaipur,19:10:00,20:20:00,Daily, 6E 410,Ahmedabad,Kolkata,15:00:00,17:30:00,Daily, 6E 212,Ahmedabad,Mumbai,9:00:00,10:10:00,Daily, 6E 409,Ahmedabad,Pune,14:25:00,15:40:00,Ex Sat, 6E 154,Bangalore,Ahmedabad,10:00:00,12:00:00,Daily, 6E 277,Bangalore,Chennai,15:35:00,16:25:00,Daily, 6E 132,Bangalore,Delhi,6:00:00,8:25:00,Daily, 6E 102,Bangalore,Delhi,9:50:00,13:45:00,Ex Sat,Pune 6E 154,Bangalore,Delhi,10:00:00,14:00:00,Daily,Ahmedabad 6E 104,Bangalore,Delhi,11:30:00,14:10:00,Sat, 6E 122,Bangalore,Delhi,17:20:00,20:00:00,Daily, 6E 108,Bangalore,Delhi,19:20:00,23:10:00,Sat,Pune 6E 106,Bangalore,Delhi,19:30:00,22:00:00,Ex Sat, 6E 275,Bangalore,Goa,12:15:00,13:15:00,Daily, 6E 351,Bangalore,Hyderabad,8:25:00,9:25:00,Daily, 6E 152,Bangalore,Hyderabad,19:10:00,20:10:00,Ex Sat, 6E 152,Bangalore,Hyderabad,19:30:00,20:35:00,Sat, 6E 152,Bangalore,Jaipur,19:10:00,22:30:00,Ex Sat,Hyderabad 6E 152,Bangalore,Jaipur,19:30:00,22:30:00,Sat,Hyderabad 6E 351,Bangalore,Kolkata,8:25:00,11:55:00,Daily,Hyderabad 6E 277,Bangalore,Kolkata,15:35:00,19:15:00,Daily,Chennai 6E 402,Bangalore,Mumbai,6:05:00,7:45:00,Daily, 6E 275,Bangalore,Mumbai,12:15:00,14:45:00,Daily,Goa 6E 414,Bangalore,Mumbai,12:45:00,14:20:00,Daily, 6E 412,Bangalore,Mumbai,21:20:00,23:20:00,Daily, 6E 102,Bangalore,Pune,9:50:00,11:10:00,Ex Sat, 6E 108,Bangalore,Pune,19:20:00,20:40:00,Sat, 6E 258,Bhubaneshwar,Delhi,18:55:00,20:55:00,Daily, 6E 257,Bhubaneshwar,Hyderabad,10:40:00,12:05:00,Daily, 6E 257,Bhubaneshwar,Mumbai,10:40:00,13:50:00,Daily,Hyderabad 6E 211,Chennai,Ahmedabad,15:10:00,18:40:00,Daily,Mumbai 6E 275,Chennai,Bangalore,10:50:00,11:40:00,Daily, 6E 302,Chennai,Delhi,11:35:00,15:20:00,Daily,Hyderabad 6E 282,Chennai,Delhi,19:45:00,22:30:00,Daily, 6E 275,Chennai,Goa,10:50:00,13:15:00,Daily,Bangalore 6E 302,Chennai,Hyderabad,11:35:00,12:40:00,Daily, 6E 211,Chennai,Jaipur,15:10:00,20:20:00,Daily,Mumbai/Ahmedabad 6E 523,Chennai,Kolkata,8:20:00,10:30:00,Daily, 6E 277,Chennai,Kolkata,16:55:00,19:15:00,Daily, 6E 211,Chennai,Mumbai,15:10:00,16:50:00,Daily, 6E 524,Chennai,Pune,21:15:00,23:00:00,Daily, 6E 273,Delhi,Agartala,6:15:00,9:45:00,Daily,Kolkata 6E 153,Delhi,Ahmedabad,14:45:00,16:30:00,Daily, 6E 101,Delhi,Bangalore,6:30:00,9:10:00,Ex Sat, 6E 103,Delhi,Bangalore,6:45:00,10:40:00,Sat,Pune 6E 121,Delhi,Bangalore,9:30:00,12:10:00,Daily, 6E 105,Delhi,Bangalore,14:20:00,18:30:00,Ex Sat,Pune 6E 153,Delhi,Bangalore,14:45:00,19:00:00,Daily,Ahmedabad 6E 107,Delhi,Bangalore,15:55:00,18:40:00,Sat, 6E 131,Delhi,Bangalore,20:45:00,23:15:00,Daily, 6E 257,Delhi,Bhubaneshwar,8:10:00,10:10:00,Daily, 6E 301,Delhi,Chennai,7:00:00,11:05:00,Daily,Hyderabad 6E 283,Delhi,Chennai,16:30:00,19:05:00,Daily, 6E 181,Delhi,Goa,9:15:00,13:35:00,Daily,Mumbai 6E 333,Delhi,Goa,11:45:00,14:15:00,Daily, 6E 201,Delhi,Guwahati,5:30:00,7:50:00,Daily, 6E 301,Delhi,Hyderabad,7:00:00,9:00:00,Daily, 6E 257,Delhi,Hyderabad,8:10:00,12:05:00,Daily,Bhubaneshwar 6E 305,Delhi,Hyderabad,14:00:00,15:55:00,Daily, 6E 307,Delhi,Hyderabad,21:00:00,22:55:00,Daily, 6E 201,Delhi,Imphal,5:30:00,9:10:00,Daily,Guwahati 6E 305,Delhi,Kochi,14:00:00,18:25:00,Daily,Hyderabad 6E 273,Delhi,Kolkata,6:15:00,8:20:00,Daily, 6E 203,Delhi,Kolkata,15:00:00,17:05:00,Daily, 6E 209,Delhi,Kolkata,18:30:00,20:45:00,Daily, 6E 183,Delhi,Mumbai,6:45:00,8:35:00,Daily, 6E 181,Delhi,Mumbai,9:15:00,11:35:00,Daily, 6E 481,Delhi,Mumbai,10:50:00,13:50:00,Daily,Vadodara 6E 189,Delhi,Mumbai,14:45:00,16:50:00,Daily, 6E 187,Delhi,Mumbai,17:50:00,19:50:00,Daily, 6E 185,Delhi,Mumbai,20:15:00,22:20:00,Daily, 6E 135,Delhi,Nagpur,8:55:00,10:40:00,Ex Sat, 6E 103,Delhi,Pune,6:45:00,8:45:00,Sat, 6E 135,Delhi,Pune,8:55:00,12:30:00,Ex Sat,Nagpur 6E 105,Delhi,Pune,14:20:00,16:30:00,Ex Sat, 6E 481,Delhi,Vadodara,10:50:00,12:20:00,Daily, 6E 277,Goa,Bangalore,14:05:00,15:00:00,Daily, 6E 277,Goa,Chennai,14:05:00,16:25:00,Daily,Bangalore 6E 334,Goa,Delhi,14:45:00,17:10:00,Daily, 6E 277,Goa,Kolkata,14:05:00,19:15:00,Daily,Bangalore/Chennai 6E 275,Goa,Mumbai,13:45:00,14:45:00,Daily, 6E 202,Guwahati,Delhi,11:00:00,13:25:00,Daily, 6E 201,Guwahati,Imphal,8:25:00,9:10:00,Daily, 6E 208,Guwahati,Jaipur,12:40:00,16:55:00,Daily,Kolkata 6E 208,Guwahati,Kolkata,12:40:00,14:00:00,Daily, 6E 322,Guwahati,Kolkata,15:30:00,16:50:00,Daily, 6E 322,Guwahati,Mumbai,15:30:00,20:20:00,Daily,Kolkata 6E 151,Hyderabad,Bangalore,8:20:00,9:20:00,Daily, 6E 352,Hyderabad,Bangalore,19:40:00,20:40:00,Daily, 6E 258,Hyderabad,Bhubaneshwar,16:40:00,18:20:00,Daily, 6E 301,Hyderabad,Chennai,9:50:00,11:05:00,Daily, 6E 308,Hyderabad,Delhi,6:10:00,8:00:00,Daily, 6E 302,Hyderabad,Delhi,13:10:00,15:20:00,Daily, 6E 258,Hyderabad,Delhi,16:40:00,20:55:00,Daily,Bhubaneshwar 6E 306,Hyderabad,Delhi,21:00:00,23:05:00,Daily, 6E 152,Hyderabad,Jaipur,20:50:00,22:30:00,Ex Sat, 6E 152,Hyderabad,Jaipur,21:10:00,22:30:00,Sat, 6E 305,Hyderabad,Kochi,16:45:00,18:25:00,Daily, 6E 351,Hyderabad,Kolkata,9:55:00,11:55:00,Daily, 6E 257,Hyderabad,Mumbai,12:35:00,13:50:00,Daily, 6E 362,Imphal,Agartala,14:15:00,14:55:00,Tu We Fr Su, 6E 202,Imphal,Delhi,9:40:00,13:25:00,Daily,Guwahati 6E 202,Imphal,Guwahati,9:40:00,10:25:00,Daily, 6E 362,Imphal,Kolkata,14:15:00,16:15:00,Tu We Fr Su,Agartala 6E 360,Imphal,Kolkata,15:05:00,16:15:00,Mo Th Sa, 6E 212,Jaipur,Ahmedabad,7:30:00,8:35:00,Daily, 6E 151,Jaipur,Bangalore,6:00:00,9:20:00,Daily,Hyderabad 6E 212,Jaipur,Chennai,7:30:00,12:55:00,Daily,Mumbai/Ahmedabad 6E 207,Jaipur,Guwahati,8:20:00,12:10:00,Daily,Kolkata 6E 151,Jaipur,Hyderabad,6:00:00,7:40:00,Daily, 6E 207,Jaipur,Kolkata,8:20:00,10:10:00,Daily, 6E 323,Jaipur,Kolkata,17:35:00,23:00:00,Daily,Mumbai 6E 212,Jaipur,Mumbai,7:30:00,10:10:00,Daily,Ahmedabad 6E 323,Jaipur,Mumbai,17:35:00,19:15:00,Daily, 6E 306,Kochi,Delhi,19:00:00,23:05:00,Daily,Hyderabad 6E 306,Kochi,Hyderabad,19:00:00,20:30:00,Daily, 6E 273,Kolkata,Agartala,8:50:00,9:45:00,Daily, 6E 360,Kolkata,Agartala,12:30:00,13:20:00,Mo Th Sa, 6E 362,Kolkata,Agartala,12:30:00,14:55:00,TuWeFrSu,Imphal 6E 409,Kolkata,Ahmedabad,11:10:00,13:50:00,Daily, 6E 275,Kolkata,Bangalore,7:30:00,11:40:00,Daily,Chennai 6E 352,Kolkata,Bangalore,16:50:00,20:40:00,Daily,Hyderabad 6E 275,Kolkata,Chennai,7:30:00,9:50:00,Daily, 6E 524,Kolkata,Chennai,18:15:00,20:25:00,Daily, 6E 210,Kolkata,Delhi,7:45:00,10:05:00,Daily, 6E 204,Kolkata,Delhi,11:40:00,13:55:00,Daily, 6E 274,Kolkata,Delhi,19:45:00,22:10:00,Daily, 6E 275,Kolkata,Goa,7:30:00,13:15:00,Daily,Chennai/Bangalore 6E 207,Kolkata,Guwahati,10:50:00,12:10:00,Daily, 6E 321,Kolkata,Guwahati,13:00:00,14:20:00,Daily, 6E 352,Kolkata,Hyderabad,16:50:00,19:00:00,Daily, 6E 362,Kolkata,Imphal,12:30:00,13:45:00,Tu We Fr Su, 6E 360,Kolkata,Imphal,12:30:00,14:35:00,MoThSa,Agartala 6E 208,Kolkata,Jaipur,14:35:00,16:55:00,Daily, 6E 320,Kolkata,Mumbai,6:00:00,8:30:00,Daily, 6E 322,Kolkata,Mumbai,17:35:00,20:20:00,Daily, 6E 404,Kolkata,Mumbai,18:35:00,21:55:00,Daily,Nagpur 6E 404,Kolkata,Nagpur,18:35:00,20:05:00,Daily, 6E 409,Kolkata,Pune,11:10:00,15:40:00,Ex Sat,Ahmedabad 6E 524,Kolkata,Pune,18:15:00,23:00:00,Daily,Chennai 6E 211,Mumbai,Ahmedabad,17:40:00,18:40:00,Daily, 6E 411,Mumbai,Bangalore,6:20:00,7:50:00,Daily, 6E 413,Mumbai,Bangalore,15:00:00,16:40:00,Daily, 6E 415,Mumbai,Bangalore,21:05:00,22:40:00,Daily, 6E 258,Mumbai,Bhubaneshwar,14:30:00,18:20:00,Daily,Hyderabad 6E 212,Mumbai,Chennai,11:00:00,12:55:00,Daily, 6E 184,Mumbai,Delhi,6:15:00,8:15:00,Daily, 6E 180,Mumbai,Delhi,8:25:00,10:35:00,Daily, 6E 482,Mumbai,Delhi,9:25:00,12:35:00,Daily,Vadodara 6E 188,Mumbai,Delhi,14:25:00,16:35:00,Daily, 6E 186,Mumbai,Delhi,17:50:00,19:55:00,Daily, 6E 182,Mumbai,Delhi,21:15:00,23:20:00,Daily, 6E 181,Mumbai,Goa,12:35:00,13:35:00,Daily, 6E 321,Mumbai,Guwahati,9:20:00,14:20:00,Daily,Kolkata 6E 258,Mumbai,Hyderabad,14:30:00,16:00:00,Daily, 6E 207,Mumbai,Jaipur,5:55:00,7:40:00,Daily, 6E 211,Mumbai,Jaipur,17:40:00,20:20:00,Daily,Ahmedabad 6E 207,Mumbai,Kolkata,5:55:00,10:10:00,Daily,Jaipur 6E 321,Mumbai,Kolkata,9:20:00,12:00:00,Daily, 6E 403,Mumbai,Kolkata,15:35:00,18:50:00,Daily,Nagpur 6E 323,Mumbai,Kolkata,20:05:00,23:00:00,Daily, 6E 403,Mumbai,Nagpur,15:35:00,16:50:00,Daily, 6E 482,Mumbai,Vadodara,9:25:00,10:25:00,Daily, 6E 136,Nagpur,Delhi,18:10:00,19:40:00,Ex Sat, 6E 403,Nagpur,Kolkata,17:20:00,18:50:00,Daily, 6E 404,Nagpur,Mumbai,20:35:00,21:55:00,Daily, 6E 135,Nagpur,Pune,11:20:00,12:30:00,Ex Sat, 6E 410,Pune,Ahmedabad,13:10:00,14:30:00,Ex Sat, 6E 103,Pune,Bangalore,9:15:00,10:40:00,Sat, 6E 105,Pune,Bangalore,17:00:00,18:30:00,Ex Sat, 6E 523,Pune,Chennai,5:55:00,7:40:00,Daily, 6E 102,Pune,Delhi,11:45:00,13:45:00,Ex Sat, 6E 136,Pune,Delhi,16:15:00,19:40:00,Ex Sat,Nagpur 6E 108,Pune,Delhi,21:10:00,23:10:00,Sat, 6E 523,Pune,Kolkata,5:55:00,10:30:00,Daily,Chennai 6E 410,Pune,Kolkata,13:10:00,17:30:00,Ex Sat,Ahmedabad 6E 136,Pune,Nagpur,16:15:00,17:40:00,Ex Sat, 6E 482,Vadodara,Delhi,10:55:00,12:35:00,Daily, 6E 481,Vadodara,Mumbai,12:50:00,13:50:00,Daily,

    Read the article

  • Problems with with A* algorithm

    - by V_Programmer
    I'm trying to implement the A* algorithm in Java. I followed this tutorial,in particular, this pseudocode: http://theory.stanford.edu/~amitp/GameProgramming/ImplementationNotes.html The problem is my code doesn't work. It goes into an infinite loop. I really don't know why this happens... I suspect that the problem are in F = G + H function implemented in Graph constructors. I suspect I am not calculate the neighbor F correclty. Here's my code: List<Graph> open; List<Graph> close; private void createRouteAStar(Unit u) { open = new ArrayList<Graph>(); close = new ArrayList<Graph>(); u.ai_route_endX = 11; u.ai_route_endY = 5; List<Graph> neigh; int index; int i; boolean finish = false; Graph current; int cost; Graph start = new Graph(u.xMap, u.yMap, 0, ManhattanDistance(u.xMap, u.yMap, u.ai_route_endX, u.ai_route_endY)); open.add(start); current = start; while(!finish) { index = findLowerF(); current = new Graph(open, index); System.out.println(current.x); System.out.println(current.y); if (current.x == u.ai_route_endX && current.y == u.ai_route_endY) { finish = true; } else { close.add(current); neigh = current.getNeighbors(); for (i = 0; i < neigh.size(); i++) { cost = current.g + ManhattanDistance(current.x, current.y, neigh.get(i).x, neigh.get(i).y); if (open.contains(neigh.get(i)) && cost < neigh.get(i).g) { open.remove(open.indexOf(neigh)); } else if (close.contains(neigh.get(i)) && cost < neigh.get(i).g) { close.remove(close.indexOf(neigh)); } else if (!open.contains(neigh.get(i)) && !close.contains(neigh.get(i))) { neigh.get(i).g = cost; neigh.get(i).f = cost + ManhattanDistance(neigh.get(i).x, neigh.get(i).y, u.ai_route_endX, u.ai_route_endY); neigh.get(i).setParent(current); open.add(neigh.get(i)); } } } } System.out.println("step"); for (i=0; i < close.size(); i++) { if (close.get(i).parent != null) { System.out.println(i); System.out.println(close.get(i).parent.x); System.out.println(close.get(i).parent.y); } } } private int findLowerF() { int i; int min = 10000; int minIndex = -1; for (i=0; i < open.size(); i++) { if (open.get(i).f < min) { min = open.get(i).f; minIndex = i; System.out.println("min"); System.out.println(min); } } return minIndex; } private int ManhattanDistance(int ax, int ay, int bx, int by) { return Math.abs(ax-bx) + Math.abs(ay-by); } And, as I've said. I suspect that the Graph class has the main problem. However I've not been able to detect and fix it. public class Graph { int x, y; int f,g,h; Graph parent; public Graph(int x, int y, int g, int h) { this.x = x; this.y = y; this.g = g; this.h = h; this.f = g + h; } public Graph(List<Graph> list, int index) { this.x = list.get(index).x; this.y = list.get(index).y; this.g = list.get(index).g; this.h = list.get(index).h; this.f = list.get(index).f; this.parent = list.get(index).parent; } public Graph(Graph gp) { this.x = gp.x; this.y = gp.y; this.g = gp.g; this.h = gp.h; this.f = gp.f; } public Graph(Graph gp, Graph parent) { this.x = gp.x; this.y = gp.y; this.g = gp.g; this.h = gp.h; this.f = g + h; this.parent = parent; } public List<Graph> getNeighbors() { List<Graph> aux = new ArrayList<Graph>(); aux.add(new Graph(x+1, y, g,h)); aux.add(new Graph(x-1, y, g,h)); aux.add(new Graph(x, y+1, g,h)); aux.add(new Graph(x, y-1, g,h)); return aux; } public void setParent(Graph g) { parent = g; } } Little Edit: Using the System.out and the Debugger I discovered that the program ALWAYS is check the same "current" graph, (15,8) which is the (u.xMap, u.yMap) position. Looks like it keeps forever in the first step.

    Read the article

  • Tetris Piece Rotation Algorithm

    - by coppercoder
    What are the best algorithms (and explanations) for representing and rotating the pieces of a tetris game? I always find the piece rotation and representation schemes confusing. Most tetris games seem to use a naive "remake the array of blocks" at each rotation: http://www.codeplex.com/Project/ProjectDirectory.aspx?ProjectSearchText=tetris However, some use pre-built encoded numbers and bit shifting to represent each piece: http://www.codeplex.com/wintris Is there a method to do this using mathematics (not sure that would work on a cell based board)?

    Read the article

  • What is an Efficient algorithm to find Area of Overlapping Rectangles

    - by namenlos
    My situation Input: a set of rectangles each rect is comprised of 4 doubles like this: (x0,y0,x1,y1) they are not "rotated" at any angle, all they are "normal" rectangles that go "up/down" and "left/right" with respect to the screen they are randomly placed - they may be touching at the edges, overlapping , or not have any contact I will have several hundred rectangles this is implemented in C# I need to find The area that is formed by their overlap - all the area in the canvas that more than one rectangle "covers" (for example with two rectangles, it would be the intersection) I don't need the geometry of the overlap - just the area (example: 4 sq inches) Overlaps shouldn't be counted multiple times - so for example imagine 3 rects that have the same size and position - they are right on top of each other - this area should be counted once (not three times) Example The image below contains thre rectangles: A,B,C A and B overlap (as indicated by dashes) B and C overlap (as indicated by dashes) What I am looking for is the area where the dashes are shown - AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA--------------BBB AAAAAAAAAAAAAAAA--------------BBB AAAAAAAAAAAAAAAA--------------BBB AAAAAAAAAAAAAAAA--------------BBB BBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBB BBBBBB-----------CCCCCCCC BBBBBB-----------CCCCCCCC BBBBBB-----------CCCCCCCC CCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCC

    Read the article

  • Probability algorithm: Finding probable correct item in a list (e.g John, John, Jon)

    - by Andrew White
    Hi, Take for example the list (L): John, John, John, John, Jon We are to presume one item is to be correct (e.g. John in this case), and give a probability it is correct. First (and good!) attempt: MostFrequentItem(L).Count / L.Count (e.g. 4/5 or 80% likelihood) But consider the cases: John, John, Jon, Jonny John, John, Jon, Jon I want to consider the likelihood of the correct item being John to be higher in the first list! I know I have to count the SecondMostFrequent Item and compare them. Any ideas? This is really busting my brain! Thx, Andrew

    Read the article

  • Mysql Algorithm for Determining Closest Colour Match

    - by buggedcom
    I'm attempting to create a true mosaic application. At the moment I have one mosaic image, ie the one the mosaic is based on and about 4000 images from my iPhoto library that act as the image library. I have already done my research and analysed the mosaic image. I've converted it into 64x64 slices each of 8 pixels. I've calculated the average colour for each slice and assertain the r, g, b and brightness (Luminance (perceived option 1) = (0.299*R + 0.587*G + 0.114*B)) value. I have done the same for each of the image library photos. The mosaic slices table looks like so. slice_id, slice_image_id, slice_slice_id, slice_image_column, slice_image_row, slice_colour_hex, slice_rgb_red, slice_rgb_blue, slice_rgb_green, slice_rgb_brightness The image library table looks like so. upload_id, upload_file, upload_colour_hex, upload_rgb_red, upload_rgb_green, upload_rgb_blue, upload_rgb_brightness So basically I'm reading the image slices from the slices table into PHP and then pulling out the appropriate images from the library table based on the colour hexs. My trouble is that I've been on this too long and probably had too many energy drinks so am not concentrating properly, I can't figure out the way to pick out the nearest colour neighbor if the appropriate hex code doesn't exist. Any ideas on the perfect query? NB: I know pulling out the slices one by one is not ideal however the mosaic is only rebuilt periodically so a sudden burst in the mysql load doesn't really bother me, however if there us a way to pull the images out all at once that would also be a massive bonus.

    Read the article

  • Al Zimmermann's Son of Darts

    - by polygenelubricants
    There's about 2 months left in Al Zimmermann's Son of Darts programming contest, and I'd like to improve my standing (currently in the 60s) to something more respectable. I'd like to get some ideas from the great community of stackoverflow on how best to approach this problem. The contest problem is known as the Global Postage Stamp Problem in literatures. I don't have much experience with optimization algorithms (I know of hillclimbing and simulated annealing in concept only from college), and in fact the program that I have right now is basically sheer brute force, which of course isn't feasible for the larger search spaces. Here are some papers on the subject: A Postage Stamp Problem (Alter & Barnett, 1980) Algorithms for Computing the h-Range of the Postage Stamp Problem (Mossige, 1981) A Postage Stamp Problem (Lunnon, 1986) Two New Techniques for Computing Extremal h-bases Ak (Challis, 1992) Any hints and suggestions are welcome. Also, feel free to direct me to the proper site if stackoverflow isn't it.

    Read the article

  • Working out global tab order algorithmically?

    - by Mrgreen
    We have a proprietry system where we can configure fields on indiviual forms. However these fields have a global tab order (we cannot specify for a specific form). We have a bunch of forms (35 in total) which share a lot of different fields. Each form has a specific tab/edit order that needs to be configured. Example: Form 1 has fields A,B,C,D in that order. Form 2 has fields E,F,G,A in that order. Form 3 has fields E,B,H,I in that order. The global tab orders would be E,F,G,A,B,C,D,H,I Notice how A needs to come before B yet after G. Is there any easy way to work this out using the tab order lists for each form? I need to merge this tab order information into a single global tab order list. I have over 200 fields in total and it is near impossible to do by hand.

    Read the article

  • Algorithm shortest path between all points

    - by Jeroen
    Hi, suppose I have 10 points. I know the distance between each point. I need to find the shortest possible route passing trough all points. I have tried a couple of algorithms (Dijkstra, Floyd Warshall,...) and the all give me the shortest path between start and end, but they don't make a route with all points on it. Permutations work fine, but they are to resource expensive. What algorithms can you advise me to look into for this problem? Or is there a documented way to do this with the above mentioned algorithms? Tnx Jeroen

    Read the article

  • algorithm analysis - orders of growth question

    - by cchampion
    I'm studing orders of growth "big oh", "big omega", and "big theta". Since I can't type the little symbols for these I will denote them as follows: ORDER = big oh OMEGA = big omega THETA = big theta For example I'll say n = ORDER(n^2) to mean that the function n is in the order of n^2 (n grows at most as fast n^2). Ok for the most part I understand these: n = ORDER(n^2) //n grows at most as fast as n^2 n^2 = OMEGA(n) //n^2 grows atleast as fast as n 8n^2 + 1000 = THETA(n^2) //same order of growth Ok here comes the example that confuses me: what is n(n+1) vs n^2 I realize that n(n+1) = n^2 + n; I would say it has the same order of growth as n^2; therefore I would say n(n+1) = THETA(n^2) but my question is, would it also be correct to say: n(n+1) = ORDER(n^2) please help because this is confusing to me. thanks.

    Read the article

  • What is an efficient way to write password cracking algorithm (python)

    - by Luminance
    This problem might be relatively simple, but I'm given two text files. One text file contains all encrypted passwords encrypted via crypt.crypt in python. The other list contains over 400k+ normal dictionary words. The assignment is that given 3 different functions which transform strings from their normal case to all different permutations of capitalizations, transforms a letter to a number (if it looks alike, e.g. G - 6, B - 8), and reverses a string. The thing is that given the 10 - 20 encrypted passwords in the password file, what is the most efficient way to get the fastest running solution in python to run those functions on dictionary word in the words file? It is given that all those words, when transformed in whatever way, will encrypt to a password in the password file. Here is the function which checks if a given string, when encrypted, is the same as the encrypted password passed in: def check_pass(plaintext,encrypted): crypted_pass = crypt.crypt(plaintext,encrypted) if crypted_pass == encrypted: return True else: return False Thanks in advance.

    Read the article

  • Looking for ideas for a simple pattern matching algorithm to run on a microcontroller

    - by pic_audio
    I'm working on a project to recognize simple audio patterns. I have two data sets, each made up of between 4 and 32 note/duration pairs. One set is predefined, the other is from an incoming data stream. The length of the two strongly correlated data sets is often different, but roughly the same "shape". My goal is to come up with some sort of ranking as to how well the two data sets correlate/match. I have converted the incoming frequencies to pitch and shifted the incoming data stream's pitch so that it's average pitch matches that of the predefined data set. I also stretch/compress the incoming data set's durations to match the overall duration of the predefined set. Here are two graphical examples of data that should be ranked as strongly correlated: http://s2.postimage.org/FVeG0-ee3c23ecc094a55b15e538c3a0d83dd5.gif (Sorry, as a new user I couldn't directly post images) I'm doing this on a 8-bit microcontroller so resources are minimal. Speed is less an issue, a second or two of processing isn't a deal breaker. It wouldn't surprise me if there is an obvious solution, I've just been staring at the problem too long. Any ideas? Thanks in advance...

    Read the article

  • Algorithm for converting hierarchical flat data (w/ ParentID) into sorted flat list w/ indentation l

    - by eagle
    I have the following structure: MyClass { guid ID guid ParentID string Name } I'd like to create an array which contains the elements in the order they should be displayed in a hierarchy (e.g. according to their "left" values), as well as a hash which maps the guid to the indentation level. For example: ID Name ParentID ------------------------ 1 Cats 2 2 Animal NULL 3 Tiger 1 4 Book NULL 5 Airplane NULL This would essentially produce the following objects: // Array is an array of all the elements sorted by the way you would see them in a fully expanded tree Array[0] = "Airplane" Array[1] = "Animal" Array[2] = "Cats" Array[3] = "Tiger" Array[4] = "Book" // IndentationLevel is a hash of GUIDs to IndentationLevels. IndentationLevel["1"] = 1 IndentationLevel["2"] = 0 IndentationLevel["3"] = 2 IndentationLevel["4"] = 0 IndentationLevel["5"] = 0 For clarity, this is what the hierarchy looks like: Airplane Animal Cats Tiger Book I'd like to iterate through the items the least amount of times possible. I also don't want to create a hierarchical data structure. I'd prefer to use arrays, hashes, stacks, or queues. The two objectives are: Store a hash of the ID to the indentation level. Sort the list that holds all the objects according to their left values. When I get the list of elements, they are in no particular order. Siblings should be ordered by their Name property. Update: This may seem like I haven't tried coming up with a solution myself and simply want others to do the work for me. However, I have tried coming up with three different solutions, and I've gotten stuck on each. One reason might be that I've tried to avoid recursion (maybe wrongly so). I'm not posting the partial solutions I have so far since they are incorrect and may badly influence the solutions of others.

    Read the article

  • Algorithm for performing decentralized search in social networks

    - by Jack
    I want to find out all the existing decentralized algorithms that exploit the structural properties of social networks. So far I know the following algorithms - 1) Best connected search - Adamic et al 2) Random Walk (does not exploit any structural property but still it is decentralized) 3) Hamming distance search 4) Weak/Strong tie search Any help would be appreciated

    Read the article

  • Fast file search algorithm for IP addresses

    - by Dave Jarvis
    Question What is the fastest way to find if an IP address exists in a file that contains IP addresses sorted as: 219.93.88.62 219.94.181.87 219.94.193.96 220.1.72.201 220.110.162.50 220.126.52.187 220.126.52.247 Constraints No database (e.g., MySQL, PostgreSQL, Oracle, etc.). Infrequent pre-processing is allowed (see possibilities section) Would be nice not to have to load the file each query (131Kb) Uses under 5 megabytes of disk space File Details One IP address per line 9500+ lines Possible Solutions Create a directory hierarchy (radix tree?) then use is_dir() (sadly, this uses 87 megabytes)

    Read the article

  • box stacking in graph theory

    - by mozhdeh
    Please help me find a good solution for this problem. We have n boxes with 3 dimensions. We can orient them and we want to put them on top of another to have a maximun height. We can put a box on top of an other box, if 2 dimensions (width and lenght) are lower than the dimensions of the box below. For exapmle we have 3 dimensions w*D*h, we can show it in to (h*d,d*h,w*d,d*W,h*w,w*h) please help me to solve it in graph theory.

    Read the article

  • Algorithm and data structure learning resources for dynamic programming

    - by Pranav
    Im learning dynamic programming now, and while I know the theory well, designing DP algorithms for new problems is still difficult. This is what i would really like now- A book or a website, which poses a problem which can be solved by dynamic programming. Also there is the solution with an explanation available, which i would like to see if i cant solve the problem even after butting my head at it for a few hours. Is there some resource that provides this sort of a thing for several categories of algorithms- like graph algorithms, dynamic programming, etc? P.S. I considered Topcoder, but the solutions there are not really appropriate for learning to implement efficient solutions.

    Read the article

  • trouble with algorithm

    - by rebel_UA
    David likes number of estimates with base "k" and not a multiple(a%2!=0) of the number of zeros at the end. Set system and the number of the order and print it I need to optimi this algoritm: class David{ private: int k; public: David(); David(int); int operator[] (int); }; David::David(){ k=10; }; David::David(int k){ this->k=k; } int David::operator[] (int n){ int q; int p; int i=1; for(int r=0;r<n;i++){ q=0; p=i; for(;;){ if(p%k) break; if(p==0) break; ++q; p/=k; } if(q%2){ r++; } } return i-1; }

    Read the article

  • Stack and Hash joint

    - by Alexandru
    I'm trying to write a data structure which is a combination of Stack and HashSet with fast push/pop/membership (I'm looking for constant time operations). Think of Python's OrderedDict. I tried a few things and I came up with the following code: HashInt and SetInt. I need to add some documentation to the source, but basically I use a hash with linear probing to store indices in a vector of the keys. Since linear probing always puts the last element at the end of a continuous range of already filled cells, pop() can be implemented very easy without a sophisticated remove operation. I have the following problems: the data structure consumes a lot of memory (some improvement is obvious: stackKeys is larger than needed). some operations are slower than if I have used fastutil (eg: pop(), even push() in some scenarios). I tried rewriting the classes using fastutil and trove4j, but the overall speed of my application halved. What performance improvements would you suggest for my code? What open-source library/code do you know that I can try?

    Read the article

< Previous Page | 13 14 15 16 17 18 19 20 21 22 23 24  | Next Page >