Как уменьшить сложность рисования графа в следующем алгоритме? - PullRequest
0 голосов
/ 12 августа 2011
I am drawing a graph which keep on refreshing

Algo
Pmin = starting point of drawing graph line
pmax = ending point of drawing graph line

THpixel = Total Horizontal pixel;
TVpixel = Total Vertical pixel;

pseudo code
loop hpixel <= THpixel
{
    loop Vpixel < pmin
    {
        draw Vpixel with background color
        Vpixel = Vpixel  + 1;
    }

    //drawing min to max line
    loop Vpixel < pmax
    {
        draw Vpixel  with graph color
        Vpixel = Vpixel  + 1;
    }
    loop Vpixel < TVpixel
    {
        draw Vpixel with backround color
        Vpixel = Vpixel  + 1;
    }

    hpixel = hpixel + 1;

}

its complexity will be O(nxm);
THpixel = n;
TVpixel = m

is there any wother ay to reduce the complexity?

Ответы [ 2 ]

1 голос
/ 12 августа 2011

Вы можете удалить 2 цикла из этого алгоритма

Алго

Pmin = starting point of drawing graph line
pmax = ending point of drawing graph line

THpixel = Total Horizontal pixel;
TVpixel = Total Vertical pixel;

псевдокод

loop hpixel <= THpixel
{
    // use this method if there is a way to draw this without walking through Box pixels
    // the walking will bring complexity of algorithm again to O(n)
    drawBox(THpixel,TVpixel,backgroungColor);// this is drawing box with background color
    // if there is no way to draw Box, you can remember the last data of graph and when
    //you want to refresh just redraw last graph with background color and draw new one with graph color, so the first step(when 1st time drawing) will took O(mxn) time and the following steps will took O(n) time

    //drawing min to max line
    loop min < Vpixel < pmax
    {
        draw Vpixel  with graph color
        Vpixel = Vpixel  + 1;
    }

    hpixel = hpixel + 1;

}

его сложность будет O (nx (pmax-pmin)) = O (N);

0 голосов
/ 03 января 2014

в вашем алгоритме вы хотите сбросить цвет всего графика (включая линии и фон), это вызвало O (n * m).

Instead, before drawing new line (or graph in general), you just need to set the pixel belonging to previous graph to background-color 
<=> reset whole window into background-color

Next, you just need to draw our new graph on. 
Now your complexity depends on the number of pixel lying on your graph. 

Пример: если вам просто нужно обновить строку: для строки требуется максимум THpixel + TVpixel (диагональ) => сложность O (THpixel + TVpixel) , а не O (THpixel * TVpixel)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...