Search Results

Search found 5070 results on 203 pages for 'algorithm'.

Page 95/203 | < Previous Page | 91 92 93 94 95 96 97 98 99 100 101 102  | Next Page >

  • Collision free hash function for a specific data structure

    - by Max
    Is it possible to create collision free hash function for a data structure with specific properties. The datastructure is int[][][] It contains no duplicates The range of integers that are contained in it is defined. Let's say it's 0..1000, the maximal integer is definitely not greater than 10000. Big problem is that this hash function should also be very fast. Is there a way to create such a hash function? Maybe at run time depending on the integer range?

    Read the article

  • Find a common element within N arrays

    - by kunjaan
    If I have N arrays, what is the best(Time complexity. Space is not important) way to find the common elements. You could just find 1 element and stop. Edit: The elements are all Numbers. Edit: These are unsorted. Please do not sort and scan. This is not a homework problem. Somebody asked me this question a long time ago. He was using a hash to solve the problem. I was thinking if SO has solved similar problems.

    Read the article

  • question about permutation problem

    - by davit-datuashvili
    i have posted similar problem here http://stackoverflow.com/questions/2920315/permutation-of-array but i want following we know that with length n there is n! possible permutation from which one such that all element are in order they are in sorted variant so i want break permutation when array is in order and print result but something is wrong i think that problem is repeated of permutation here is my code import java.util.*; public class permut{ public static Random r=new Random(); public static void display(int a[],int n){ for (int i=0;i<n;i++){ System.out.println(a[i]); } } public static void Permut(int a[],int n){ int j=0; int k=0; while (j<fact(n)){ int s=r.nextInt(n); for (int i=0;i<n;i++){ k=a[i]; a[i]=a[s]; a[s]=k; } j++; if (sorted(a,n)) display(a,n); break; } } public static void main(String[]args){ int a[]=new int[]{3,4,1,2}; int n=a.length; Permut(a,n); } public static int fact(int n){ if (n==0 || (n==1) ) return 1; return n*fact(n-1); } public static boolean sorted(int a[],int n ){ boolean flag=false; for (int i=0;i<n-1;i++){ if (a[i]<a[i+1]){ flag=true; } else{ flag=false; } } return flag; } } can anybody help me? result is nothing

    Read the article

  • How to perform a Depth First Search iteratively using async/parallel processing?

    - by Prabhu
    Here is a method that does a DFS search and returns a list of all items given a top level item id. How could I modify this to take advantage of parallel processing? Currently, the call to get the sub items is made one by one for each item in the stack. It would be nice if I could get the sub items for multiple items in the stack at the same time, and populate my return list faster. How could I do this (either using async/await or TPL, or anything else) in a thread safe manner? private async Task<IList<Item>> GetItemsAsync(string topItemId) { var items = new List<Item>(); var topItem = await GetItemAsync(topItemId); Stack<Item> stack = new Stack<Item>(); stack.Push(topItem); while (stack.Count > 0) { var item = stack.Pop(); items.Add(item); var subItems = await GetSubItemsAsync(item.SubId); foreach (var subItem in subItems) { stack.Push(subItem); } } return items; } EDIT: I was thinking of something along these lines, but it's not coming together: var tasks = stack.Select(async item => { items.Add(item); var subItems = await GetSubItemsAsync(item.SubId); foreach (var subItem in subItems) { stack.Push(subItem); } }).ToList(); if (tasks.Any()) await Task.WhenAll(tasks); UPDATE: If I wanted to chunk the tasks, would something like this work? foreach (var batch in items.BatchesOf(100)) { var tasks = batch.Select(async item => { await DoSomething(item); }).ToList(); if (tasks.Any()) { await Task.WhenAll(tasks); } } The language I'm using is C#.

    Read the article

  • Algorithms for finding the intersections of intervals

    - by tomwu
    I am wondering how I can find the number of intervals that intersect with the ones before it. for the intervals [2, 4], [1, 6], [5, 6], [0, 4], the output should be 2. from [2,4] [5,6] and [5,6] [0,4]. So now we have 1 set of intervals with size n all containing a point a, then we add another set of intervals size n as well, and all of the intervals are to the right of a. Can you do this in O(nlgn) and O(nlg^2n)?

    Read the article

  • Simple loop, which one I would get more performance and which one is recommended? defining a variable inside a loop or outside of it?

    - by Grego
    Variable outside of the loop int number = 0; for(int i = 0; i < 10000; i++){ number = 3 * i; printf("%d",number); } or Variable inside of the loop for(int i = 0; i < 10000; i++){ int number = 3 * i; printf("%d",number); } Which one is recommended and which one is better in performance? Edit: This is just an example to exhibit what I mean, All I wanna know is if defining a variable inside a loop and outside a loop means the same thing , or there's a difference.

    Read the article

  • Adapting pseudocode to java implementation for finding the longest word in a trie

    - by user1766888
    Referring to this question I asked: How to find the longest word in a trie? I'm having trouble implementing the pseudocode given in the answer. findLongest(trie): //first do a BFS and find the "last node" queue <- [] queue.add(trie.root) last <- nil map <- empty map while (not queue.empty()): curr <- queue.pop() for each son of curr: queue.add(son) map.put(son,curr) //marking curr as the parent of son last <- curr //in here, last indicate the leaf of the longest word //Now, go up the trie and find the actual path/string curr <- last str = "" while (curr != nil): str = curr + str //we go from end to start curr = map.get(curr) return str This is what I have for my method public static String longestWord (DTN d) { Queue<DTN> holding = new ArrayQueue<DTN>(); holding.add(d); DTN last = null; Map<DTN,DTN> test = new ArrayMap<DTN,DTN>(); DTN curr; while (!holding.isEmpty()) { curr = holding.remove(); for (Map.Entry<String, DTN> e : curr.children.entries()) { holding.add(curr.children.get(e)); test.put(curr.children.get(e), curr); } last = curr; } curr = last; String str = ""; while (curr != null) { str = curr + str; curr = test.get(curr); } return str; } I'm getting a NullPointerException at: for (Map.Entry<String, DTN> e : curr.children.entries()) How can I find and fix the cause of the NullPointerException of the method so that it returns the longest word in a trie?

    Read the article

  • question about siftdown operation on heap

    - by davit-datuashvili
    i have following pseudo code which execute siftdown operation on heap array suppose is x void siftdown(int n) pre heap(2,n) && n>=0 post heap(1,n) i=1; loop /*invariant heap(1,n) except perhaps between i and it's (0,1,or 2) children*/ c=2*i; if (c>n) break; // c is left child of i if (c+1)<=n /* c+1 is rigth child of i if (x[c+1]<x[c]) c++ /* c is lesser child of i if (x[i]<=x[c]) break; swap(c,i) i=c; i have wrote following code is it correct? public class siftdown{ public static void main(String[]args){ int c; int n=9; int a[]=new int[]{19,100,17,2,7,3,36,1,25}; int i=1; while (i<n){ c=2*i; if (c>n) break; //c is the left child of i if (c+1<=n) //c+1 ir rigth child of i if (a[c+1]<a[c]) c++; if (a[i]<=a[c]) break; int t=a[c]; a[c]=a[i]; a[i]=t; i=c; } for (int j=0;j<a.length;j++){ System.out.println(a[j]); } } } // result is 19 2 17 1 7 3 36 100 25

    Read the article

  • how to fast compute distance between high dimension vectors

    - by chyojn
    assume there are three group of high dimension vectors: {a_1, a_2, ..., a_N}, {b_1, b_2, ... , b_N}, {c_1, c_2, ..., c_N}. each of my vector can be represented as: x = a_i + b_j + c_k, where 1 <=i, j, k <= N. then the vector is encoded as (i, j, k) wich is then can be decoded as x = a_i + b_j + c_k. my question is, if there are two vector: x = (i_1, j_1, k_1), y = (i_2, j_2, k_2), is there a method to compute the euclidian distance of these two vector without decode x and y.

    Read the article

  • Help to learn Image Search algorithm

    - by R Manu
    I am a beginner in image processing. I want to write an application in C++ or in C# for Searching an image in a list of images Searching for a particular feature (for e.g. face) in a list of images. Can anybody suggest where should I start from? What all should I learn before doing this? Where can I find the correct information regarding this?

    Read the article

  • public (static) swap() method vs. redundant (non-static) private ones...

    - by Helper Method
    I'm revisiting data structures and algorithms to refresh my knowledge and from time to time I stumble across this problem: Often, several data structures do need to swap some elements on the underlying array. So I implement the swap() method in ADT1, ADT2 as a private non-static method. The good thing is, being a private method I don't need to check on the parameters, the bad thing is redundancy. But if I put the swap() method in a helper class as a public static method, I need to check the indices every time for validity, making the swap call very unefficient when many swaps are done. So what should I do? Neglect the performance degragation, or write small but redundant code?

    Read the article

  • Trie vs B+ tree

    - by Fakrudeen
    How does Trie and B+ tree compare for indexing lexicographically sorted strings [on the order some billions]? It should support range queries as well. From perf. as well as implementation complexity point of view.

    Read the article

  • F# insert/remove item from list

    - by Timothy
    How should I go about removing a given element from a list? As an example, say I have list ['A'; 'B'; 'C'; 'D'; 'E'] and want to remove the element at index 2 to produce the list ['A'; 'B'; 'D'; 'E']? I've already written the following code which accomplishes the task, but it seems rather inefficient to traverse the start of the list when I already know the index. let remove lst i = let rec remove lst lst' = match lst with | [] -> lst' | h::t -> if List.length lst = i then lst' @ t else remove t (lst' @ [h]) remove lst [] let myList = ['A'; 'B'; 'C'; 'D'; 'E'] let newList = remove myList 2 Alternatively, how should I insert an element at a given position? My code is similar to the above approach and most likely inefficient as well. let insert lst i x = let rec insert lst lst' = match lst with | [] -> lst' | h::t -> if List.length lst = i then lst' @ [x] @ lst else insert t (lst' @ [h]) insert lst [] let myList = ['A'; 'B'; 'D'; 'E'] let newList = insert myList 2 'C'

    Read the article

  • One Database Field to Hold Survey Answer

    - by yar
    Skipping the question of whether this is bad design (and the question of why I would want/need to do this), I'm just wondering if my 'math' is right... I have n things that need to be put in order (n is always less than 5): thing1 thing2 thing3 ... and I'd like to store these results in a single integer for thing. My initial thought is that (obviously the code will not look like this): thing = thing1 * 1 + thing2 * 2 + thing3 * 4 + thing5 * 8 will always give me a unique value, and that any value for thing will always translate back to its values for thing1...thingn. Is this correct?

    Read the article

  • question about in -place sort

    - by davit-datuashvili
    for example we have following array char data[]=new char[]{'A','S','O','R','T','I','N','G','E','X','A','M','P','L','E'}; and index array int a[]=new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}: void insitu(char data[],int a[],N){ for (int i=0;i<N;i++) { char v=data[i]; int j,int k; for (k=i;a[k]!=i;k=a[j];a[j]=j) { j=k;data[k]=data[a[k]; } data[k]=v; a[k]=k; } i have question what is initialize value of j? when i run this code it asks me to initialize j and what should do? }

    Read the article

  • Distributing players to tables

    - by IVlad
    Consider N = 4k players, k tables and a number of clans such that each member can belong to one clan. A clan can contain at most k players. We want to organize 3 rounds of a game such that, for each table that seats exactly 4 players, no 2 players sitting there are part of the same clan, and, for the later rounds, no 2 players sitting there have sat at the same table before. All players play all rounds. How can we do this efficiently if N can be about ~80 large? I thought of this: for each table T: repeat until 4 players have been seated at T: pick a random player X that is not currently seated anywhere if X has not sat at the same table as anyone currently at T AND X is not from the same clan as anyone currently at T seat X at T break I am not sure if this will always finish or if it can get stuck even if there is a valid assignment. Even if this works, is there a better way to do it?

    Read the article

  • Print number series in java

    - by user1898282
    I have to print the series shown below in java: ***1*** **2*2** *3*3*3* 4*4*4*4 My current implementation is: public static void printSeries(int number,int numberOfCharsinEachLine){ String s="*"; for(int i=1;i<=number;i++){ int countOfs=(numberOfCharsinEachLine-(i)-(i-1))/2; if(countOfs<0){ System.out.println("Can't be done"); break; } for(int j=0;j<countOfs;j++){ System.out.print(s); } System.out.print(i); for(int k=1;k<i;k++){ System.out.print(s); System.out.print(i); } for(int j=0;j<countOfs;j++){ System.out.print(s); } System.out.println(); } } But there are lot of for loops, so I'm wondering whether this can be done in a better way or not?

    Read the article

  • Interview question: C program to sort a binary array in O(n)

    - by Zacky112
    I've comeup with the following program to do it, but it does not seem to work and goes into infinite loop. Its working is similar to quicksort. int main() { int arr[] = {1,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1}; int N = 18; int *front, *last; front = arr; last = arr + N; while(front <= last) { while( (front < last) && (*front == 0) ) front++; while( (front < last) && (*last == 1) ) last--; if( front < last) { int temp = *front; *front = *last; *last = temp; front ++; last--; } } for(int i=0;i<N;i++) printf("%d ",arr[i]); return 0; }

    Read the article

  • Graph search problem with route restrictions

    - by Darcara
    I want to calculate the most profitable route and I think this is a type of traveling salesman problem. I have a set of nodes that I can visit and a function to calculate cost for traveling between nodes and points for reaching the nodes. The goal is to reach a fixed known score while minimizing the cost. This cost and rewards are not fixed and depend on the nodes visited before. The starting node is fixed. There are some restrictions on how nodes can be visited. Some simplified examples include: Node B can only be visited after A After node C has been visited, D or E can be visited. Visiting at least one is required, visiting both is permissible. Z can only be visited after at least 5 other nodes have been visited Once 50 nodes have been visited, the nodes A-M will no longer reward points Certain nodes can (and probably must) be visited multiple times Currently I can think of only two ways to solve this: a) Genetic Algorithms, with the fitness function calculating the cost/benefit of the generated route b) Dijkstra search through the graph, since the starting node is fixed, although the large number of nodes will probably make that not feasible memory wise. Are there any other ways to determine the best route through the graph? It doesn't need to be perfect, an approximated path is perfectly fine, as long as it's error acceptable. Would TSP-solvers be an option here?

    Read the article

< Previous Page | 91 92 93 94 95 96 97 98 99 100 101 102  | Next Page >