A Recipe for Online Demos of Graph Algorithms in GraphWin
The edit-and-run paradigm for demos
of graph algorithms requires
an explicit user action, namely clicking the done-button, to start
the graph algorithm to be demonstrated. Call-back
or handler functions allow us to write online demos which show
the result of a graph algorithm while the graph is edited.
If the edges of your graph have capacities or costs, have a look
at A Recipe for Online Demos
of Network Algorithms with GraphWin
|
We use a function run_and_display() that runs the
graph algorithm on the graph associated with GraphWin gw
and updates the display. We define post-handlers for the new_node,
new_edge, del_node, del_edge, and init_graph
operations; each handler simply calls run_and_display(gw) .
In the main() function we tell GraphWin which handlers
to use by calling the corresponding set_handler() functions,
display the GraphWin, and call gw.edit() .
|
|
We derive a specific demo from this framework by instantiating
the run_and_display() function with a call to STRONG_COMPONENTS()
and coloring the nodes according to their component.
The picture above shows a screenshot of the program after some
edit operations. Clicking on the pictures shows the Graphwin in
original size.
|
#include <LEDA/graphics/graphwin.h>
#include <LEDA/graph/graph_alg.h>
using namespace leda;
void run_and_display(GraphWin& gw);
void new_node_handler(GraphWin& gw, node) {run_and_display(gw);}
void new_edge_handler(GraphWin& gw, edge) {run_and_display(gw);}
void del_edge_handler(GraphWin& gw) {run_and_display(gw);}
void del_node_handler(GraphWin& gw) {run_and_display(gw);}
void init_graph_handler(GraphWin& gw) {run_and_display(gw);}
int main()
{
GraphWin gw;
gw.set_init_graph_handler(init_graph_handler);
gw.set_new_edge_handler(new_edge_handler);
gw.set_del_edge_handler(del_edge_handler);
gw.set_new_node_handler(new_node_handler);
gw.set_del_node_handler(del_node_handler);
gw.display();gw.edit();
return 0;
}
void run_and_display(GraphWin& gw)
{
graph& G=gw.get_graph();
node_array<int> comp_num(G);
STRONG_COMPONENTS(G,comp_num);
node v;forall_nodes(v,G)
gw.set_color(v,color(comp_num[v]));
}
|
|
See also:
GraphWin
Edit-and-Run Paradigm
Call-Back Functions
A Recipe for Online Demos of Network
Algorithms with GraphWin
Graph Algorithms
Strongly Connected
Components
Manual Pages:
Manual
Page GraphWin
|