How to visualize a graph made with igraph in C?

落花浮王杯 提交于 2019-12-25 00:34:33

问题


I started to learn igraph in C and I was wondering how can I visualize a graph made with this library. I've seen that with igraph R one just use the plot function and the graph is plotted but if I use C, should I print the graph in a file and then use another program to visualize it or which is the usual way?

Thanks!

edit: This kind of graph.


回答1:


To plot directed graphs try GraphViz (https://www.graphviz.org).

Alternatively you could use a tool like Gephi (https://gephi.org) if you are willing to write the data into a file in a manner compliant with one of their supported formats (https://gephi.org/users/supported-graph-formats/). GML looks pretty straight forward.




回答2:


Follow the Unix philosophy, and have your program output the description of the graph (in text format, or in an easily processed form if no pure text format is easily available).

(Note that this also applies to image formats; the NetPBM (.pnm, or .pbm, .pgm, and .ppm) formats are easy to generate in pure C (to e.g. standard output), and if necessary, the NetPBM tools can be used to convert to any other image format you might wish.)

For example, if your program outputs

graph {
    rankdir=LR;
    "6" -- "4";
    "4" -- "5";
    "3" -- "4";
    "3" -- "2";
    "5" -- "2";
    "5" -- "1";
    "2" -- "1";
}

then redirecting the output to e.g. output.dot and running dot -Tx11 output.dot will output a graph similar to the one shown in the Wikipedia Graph article,

The DOT language is specified here, but it really is quite simple. -- denotes an undirected edge, and -> a directed edge. The rankdir=LR; line is a graph attribute, and tells DOT that it should try to order the nodes seen from left to right. The default is from top to bottom. You can add node attributes too, for example "6" [ label="Six" ]; would change the label of node "6" to Six. Edge attributes work exactly the same way; so using "2" -- "1" [ taillabel="Z" ]; adds "Z" near node "2" end of the edge between nodes "2" and "1". It is best to quote node names, even though the quotes are not necessary if the node name starts with a letter and does not match a graph attribute name.


Here is a useful hint, when printing trees or linked lists:

Use %p (a pointer to the node) as the node name, and label="value" to set the visible label of the node to value. For example, if you have

struct node {
    struct node  *left;
    struct node  *right;
    int           value;
};

then a simple function pair,

void print_tree_recursive(FILE *out, struct node *curr)
{
    fprintf(out, "    \"%p\" [ label=\"%d\" ];\n", (void *)curr, curr->value);
    if (curr->left) {
        print_tree_recursive(out, curr->left);
        fprintf(out, "    \"%p\" -> \"%p\" [ taillabel="L" ];\n", curr, curr->left);
    }
    if (curr->right) {
        print_tree_recursive(out, curr->right);
        fprintf(out, "    \"%p\" -> \"%p\" [ taillabel="R" ];\n", curr, curr->right);
    }
}

void print_tree(FILE *out, struct node *tree)
{
    fprintf(out, "digraph {\n");
    if (tree)
        print_tree_recursive(out, tree);
    fprintf(out, "}\n");
    fflush(out);
}

will print a nice directed graph of any tree. It is easy to modify to print linked lists (both singly and doubly linked). Note how the helper function describes the node first (the fprintf with label=), and the edges separately (the fprintfs with taillabel=).

If you print the graph to standard output, you can either redirect the output to a file and display or convert it using dot -Tformat filename, or you can pipe the output directly to | dot -Tx11 to see the generated graph.

I frequently use the Graphviz DOT format for checking whether my mental picture of data structure linkage matches the reality. I find it an extremely useful tool, and keep recommending it for anyone working with complex data structures.



来源:https://stackoverflow.com/questions/48628621/how-to-visualize-a-graph-made-with-igraph-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!