Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to visualize an algorithm without changing the code of that algorithm?

I want to visualize two different algorithms that decide if there's overlap in a collection of circles in a plane in Java:

  • an O(n²) algorithm that checks every combination of circles
  • an O(nlogn) algorithm using a sweep line

Is there a way to let an object of a vizualization class 'listen' to an object of the algorithm class in a way that it can for example see when the algorithm is performing an overlap check between a pair of circles and knows when to update the visualization?

other example: I can keep the list of active circles(those which intersect the sweep line) as a variable of the sweep line algorithm and let another class(visualization class) get that variable. But how will that class know when the list is updated and it has to update the visualization?

That's just the strategy I was thinking of. Maybe there are better ways...

like image 683
aerod Avatar asked Apr 05 '12 22:04

aerod


People also ask

What is algorithm Visualizer?

Algorithm Visualizer is an interactive online platform that visualizes algorithms from code. Learning an algorithm gets much easier with visualizing it.

What do we call visual version of algorithm?

It is called algorithm visualization and can be defined as the use of images to convey some useful information about algorithms. Algorithm Visualization. In addition to the mathematical and empirical analyses of algorithms, there is yet a third way to study algorithms.11-Jan-2017.


2 Answers

Perhaps reading up on the Observer pattern can help you: https://en.wikipedia.org/wiki/Observer_pattern

You can either implement java.util.Observer or give the algorithm a callback function / object.

You can hand the observer arbitrary data, enough to allow it do decide when the algorithm is performing an overlap check.

like image 182
j13r Avatar answered Oct 23 '22 00:10

j13r


I'm not sure whether or not this will help you or not, but if you can't change the code of the algorithm to support observers then one (interesting) option would be to look into aspect-oriented programming.

For example, in AspectJ (e.g. see http://en.wikipedia.org/wiki/AspectJ) you can specify (using things called 'pointcuts') places (called 'join points') where bits of extra code (called 'advice') should be run. You could use this to detect overlap checks being performed by the algorithm and respond to them as you see fit.

Of course, doing things this way would involve using AspectJ, so it wouldn't be possible with just normal Java - but it's something interesting you might want to look into.

like image 42
Stuart Golodetz Avatar answered Oct 23 '22 00:10

Stuart Golodetz