Object | +---DGraph
The graph collection stores objects in directed graph with nodes and edges. Both the nodes as the edges can store objects. The graph collection can also be used as graph data structure. Using the toDot method, the graph structure can be exported to a dot-file which can be visualized with the software of www.graphviz.org. IMPORTANT: as long as a node or edge object are part of a graph, the node or edge object is owned by the graph object. Do not free them outside the scope of the graph object.
#include <stdio.h> #include "ofc/DGraph.h" #include "ofc/DInt.h" #include "ofc/DFile.h" int main(int argc, char *argv[]) { DGraph *graph = [DGraph new]; DFile *file = [DFile new]; DList *path; DGraphNode *node1,*node2,*node3; DGraphEdge *edge1,*edge2,*edge3; DInt *nr; double weight; [graph attributes :"rotate=90"]; // Set the graphviz attributes for the graph itself printf("Add the nodes to the graph.\n"); nr = [DInt new]; [nr set :7]; // Create and add node to graph with label:start, attributes and object:7 node1 = [graph addNode :"start" :"shape=diamond" :nr]; nr = [DInt new]; [nr set :3]; // Create and add node to graph with label:between, attributes and object:3 node2 = [graph addNode :"between" :"color=green" :nr]; nr = [DInt new]; [nr set :9]; // Create and add node to graph with label:start, attributes and object:7 node3 = [graph addNode :"end" :"shape=circle" :nr]; printf("Add the edges to the graph.\n"); nr = [DInt new]; [nr set :-4]; // Create and add edge to graph from node1 to node2 edge1 = [graph addEdge :"edge1" :"color=blue" :1.0 :nr :node1 :node2]; nr = [DInt new]; [nr set :-5]; // Create and add edge to graph from node2 to node3 edge2 = [graph addEdge :"edge2" :"style=dashed" :2.0 :nr :node2 :node3]; nr = [DInt new]; [nr set :-3]; // Create and add edge to graph from node1 to node3 edge3 = [graph addEdge :"edge3" :"arrowhead=crow" :3.5 :nr :node1 :node3]; path = [graph shortestPath :&weight :node1 :node3]; printf("Number of nodes in shortestpath:%ld with weight:%.1f\n", [path length], weight); [path shallowFree]; printf("Ingoing degree of node3:%lu\n", [node3 ingoingDegree]); printf("Outgoing degree of node2:%lu\n", [node2 outgoingDegree]); nr = [node2 object]; printf("Object of node2:%d\n", [nr get]); if ([file open :"example.dot" :"w"]) { if ([graph toDot :file]) printf("Graph is succesfully exported to \"example.dot\".\n"); else printf("Graph could not be exported to \"example.dot\".\n"); [file close]; } else printf("Could not open the file \"example.dot\".\n"); [graph free]; // Cleanup [file free]; return 0; }