简体   繁体   中英

How to analyse graph-data to find tops?

I need to find severel tops from some givin data (data from a graph). The data is put into an array.

A top is defined by having lower or equal elements in a certain interval before the top and lower or equal elements in a certain interval after the top. I would like to keep it simple as im not an expert.

Im working on analysing some stock graphs with some different tools. I hope u can help me or maybe come with some other input how to deal with this :)

Here's a working example:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Test
{
  static final int[] graph = new int[100]; static { randomGraph(); }
  static final int window = 3;

  public static void main(String[] args) {
    final List<Integer> tops = new ArrayList<Integer>();
    for (int i = 0; i < graph.length; i++)
      if (max(i-window, i) <= graph[i] && graph[i] >= max(i+1, i+1+window))
        tops.add(i);
    System.out.println(tops);
  }
  static int max(int start, int end) {
    int max = Integer.MIN_VALUE;
    for (int i = Math.max(start, 0); i < Math.min(end, graph.length); i++)
      max = Math.max(max, graph[i]);
    return max;
  }

  private static void randomGraph() {
    final Random r = new Random();
    for (int i = 1; i < graph.length; i++)
      graph[i] = graph[i-1] + r.nextInt(10) - 5;
    System.out.println(Arrays.toString(graph));
  }
}

A brute force solution could fit if the array is not too big. You can just iterate the elements, and checks the predicate you describe for each element:

pseudo code:

for (int i = 0; i < array.length; i++) { 
   boolean isPick = true;
   for (int j = max(0,i-interval; j < min(array.length,i+interval; j++) 
       if (array[j] > array[i]) isPick=false;
   //do something with the pick, isPick indicates if it is a pick or not.

An heuristical solution you might want to consider if the array is very large is hill-climbing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM