CMR  1.3.0
graph.h
Go to the documentation of this file.
1 #ifndef CMR_GRAPH_H
2 #define CMR_GRAPH_H
3 
13 #include <cmr/env.h>
14 #include <cmr/element.h>
15 
16 #include <stdio.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
30 typedef int CMR_GRAPH_NODE;
31 typedef int CMR_GRAPH_EDGE;
32 typedef int CMR_GRAPH_ITER;
34 typedef struct
35 {
36  int prev;
37  int next;
38  int firstOut;
40 
41 typedef struct
42 {
43  int target;
44  int prev;
45  int next;
47 
48 typedef struct
49 {
50  size_t numNodes;
51  size_t memNodes;
53  int firstNode;
54  int freeNode;
56  size_t numEdges;
57  size_t memEdges;
59  int freeEdge;
60 } CMR_GRAPH;
61 
66 static inline
68  CMR_GRAPH* graph
69 )
70 {
71  assert(graph);
72 
73  return graph->memNodes;
74 }
75 
80 static inline
82  CMR_GRAPH* graph
83 )
84 {
85  assert(graph);
86 
87  return graph->numNodes;
88 }
89 
94 static inline
96  CMR_GRAPH* graph
97 )
98 {
99  assert(graph);
100 
101  return graph->memEdges;
102 }
103 
108 static inline
110  CMR_GRAPH* graph
111 )
112 {
113  assert(graph);
114 
115  return graph->numEdges;
116 }
117 
122 static inline
124  CMR_GRAPH* graph,
125  CMR_GRAPH_EDGE e
126 )
127 {
128  assert(graph);
129 
130  return graph->arcs[2*e+1].target;
131 }
132 
137 static inline
139  CMR_GRAPH* graph,
140  CMR_GRAPH_EDGE e
141 )
142 {
143  assert(graph);
144 
145  return graph->arcs[2*e].target;
146 }
147 
152 CMR_EXPORT
154  CMR* cmr,
155  CMR_GRAPH** pgraph,
156  int memNodes,
157  int memEdges
158 );
159 
164 CMR_EXPORT
166  CMR* cmr,
167  CMR_GRAPH** pgraph
168 );
169 
174 CMR_EXPORT
176  CMR* cmr,
177  CMR_GRAPH* graph
178 );
179 
188 CMR_EXPORT
190  CMR* cmr,
191  CMR_GRAPH* graph,
192  CMR_GRAPH_NODE* pnode
193 );
194 
203 CMR_EXPORT
205  CMR* cmr,
206  CMR_GRAPH* graph,
207  CMR_GRAPH_NODE u,
208  CMR_GRAPH_NODE v,
209  CMR_GRAPH_EDGE* pedge
210 );
211 
216 CMR_EXPORT
218  CMR* cmr,
219  CMR_GRAPH* graph,
220  CMR_GRAPH_NODE v
221 );
222 
227 CMR_EXPORT
229  CMR* cmr,
230  CMR_GRAPH* graph,
231  CMR_GRAPH_EDGE e
232 );
233 
238 static inline
240  CMR_GRAPH* graph
241 )
242 {
243  assert(graph);
244 
245  return graph->firstNode;
246 }
247 
252 static inline
254  CMR_GRAPH* graph,
255  CMR_GRAPH_NODE v
256 )
257 {
258  CMR_UNUSED(graph);
259 
260  assert(graph);
261 
262  return v >= 0;
263 }
264 
269 static inline
271  CMR_GRAPH* graph,
272  CMR_GRAPH_NODE v
273 )
274 {
275  assert(graph);
276 
277  return graph->nodes[v].next;
278 }
279 
284 static inline
286  CMR_GRAPH* graph,
287  CMR_GRAPH_NODE v
288 )
289 {
290  assert(graph);
291 
292  CMR_GRAPH_ITER i = graph->nodes[v].firstOut;
293  while (true)
294  {
295  if (i < 0)
296  return -1;
297  if ((graph->arcs[i].target != (graph)->arcs[i ^ 1].target) || !(i & 0x1))
298  return i;
299  i = graph->arcs[i].next;
300  }
301 }
302 
307 static inline
309  CMR_GRAPH* graph,
310  CMR_GRAPH_ITER i
311 )
312 {
313  CMR_UNUSED(graph);
314 
315  assert(graph);
316 
317  return i >= 0;
318 }
319 
324 static inline
326  CMR_GRAPH* graph,
327  CMR_GRAPH_ITER i
328 )
329 {
330  assert(graph);
331 
332  while (true)
333  {
334  i = graph->arcs[i].next;
335  if (i < 0)
336  return -1;
337  if (((graph)->arcs[i].target != (graph)->arcs[i ^ 1].target) || !(i & 0x1))
338  return i;
339  }
340 }
341 
346 static inline
348  CMR_GRAPH* graph,
349  CMR_GRAPH_ITER i
350 )
351 {
352  CMR_UNUSED(graph);
353 
354  assert(graph);
355 
356  return i/2;
357 }
358 
363 static inline
365  CMR_GRAPH* graph,
366  CMR_GRAPH_ITER i
367 )
368 {
369  return graph->arcs[i^1].target;
370 }
371 
376 static inline
378  CMR_GRAPH* graph,
379  CMR_GRAPH_ITER i
380 )
381 {
382  return graph->arcs[i].target;
383 }
384 
389 static inline
391  CMR_GRAPH* graph,
392  CMR_GRAPH_ITER i
393 )
394 {
395  CMR_GRAPH_NODE source = graph->arcs[i ^ 1].target;
396  CMR_GRAPH_ITER j = graph->arcs[i].next;
397  while (true)
398  {
399  while (j >= 0)
400  {
401  if (!(j & 0x1))
402  return j;
403  j = graph->arcs[j].next;
404  }
405  source = graph->nodes[source].next;
406  if (source < 0)
407  return -1;
408 
409  j = graph->nodes[source].firstOut;
410  }
411 }
412 
417 static inline
419  CMR_GRAPH* graph
420 )
421 {
422  assert(graph);
423 
424  for (CMR_GRAPH_NODE v = CMRgraphNodesFirst(graph); CMRgraphNodesValid(graph, v);
425  v = CMRgraphNodesNext(graph, v))
426  {
427  CMR_GRAPH_ITER i = graph->nodes[v].firstOut;
428  if (i >= 0)
429  {
430  /* We found some node with an incident edge. */
431  if (i & 0x1)
432  i = CMRgraphEdgesNext(graph, i);
433  return i;
434  }
435  }
436  return -1;
437 }
438 
443 static inline
445  CMR_GRAPH* graph,
446  CMR_GRAPH_ITER i
447 )
448 {
449  CMR_UNUSED(graph);
450 
451  assert(graph);
452 
453  return i >= 0;
454 }
455 
460 static inline
462  CMR_GRAPH* graph,
463  CMR_GRAPH_ITER i
464 )
465 {
466  CMR_UNUSED(graph);
467 
468  assert(graph);
469 
470  return i/2;
471 }
472 
477 CMR_EXPORT
479  CMR_GRAPH* graph,
480  FILE* stream
481 );
482 
487 CMR_EXPORT
489  CMR* cmr,
490  CMR_GRAPH* graph,
491  CMR_GRAPH_NODE u,
492  CMR_GRAPH_NODE v
493 );
494 
495 
496 CMR_EXPORT
498  CMR* cmr,
499  CMR_GRAPH** pgraph,
500  CMR_ELEMENT** pedgeElements,
501  char*** pnodeLabels,
502  FILE* stream
503 );
504 
505 CMR_EXPORT
507  CMR* cmr,
508  CMR_GRAPH* graph,
509  CMR_GRAPH** pcopy
510 );
511 
514 #ifdef __cplusplus
515 }
516 #endif
517 
518 #endif /* CMR_GRAPH_H */
Functionality for the row and column elements of a matrix.
int CMR_ELEMENT
Definition: element.h:20
Basic functionality of the software library.
#define CMR_UNUSED(x)
Definition: env.h:24
CMR_ERROR
Type for return codes of library functions.
Definition: env.h:32
int CMR_GRAPH_NODE
Reference to a node of CMR_GRAPH.
Definition: graph.h:30
static CMR_GRAPH_EDGE CMRgraphEdgesEdge(CMR_GRAPH *graph, CMR_GRAPH_ITER i)
Returns the actual edge, iterator i represents.
Definition: graph.h:461
CMR_EXPORT CMR_ERROR CMRgraphCreateEmpty(CMR *cmr, CMR_GRAPH **pgraph, int memNodes, int memEdges)
Creates an empty graph.
Definition: graph.c:93
static CMR_GRAPH_EDGE CMRgraphIncEdge(CMR_GRAPH *graph, CMR_GRAPH_ITER i)
Converts an iterator for edges incident to a node to the actual edge.
Definition: graph.h:347
static size_t CMRgraphNumNodes(CMR_GRAPH *graph)
Returns number of nodes.
Definition: graph.h:81
static size_t CMRgraphMemEdges(CMR_GRAPH *graph)
Returns number of edges for which memory is allocated.
Definition: graph.h:95
static size_t CMRgraphMemNodes(CMR_GRAPH *graph)
Returns number of nodes for which memory is allocated.
Definition: graph.h:67
static CMR_GRAPH_NODE CMRgraphIncTarget(CMR_GRAPH *graph, CMR_GRAPH_ITER i)
Returns the end node of the edge corresponding to this iterator i.
Definition: graph.h:377
CMR_EXPORT CMR_ERROR CMRgraphPrint(CMR_GRAPH *graph, FILE *stream)
Prints the graph, writing to stream.
Definition: graph.c:381
CMR_EXPORT CMR_ERROR CMRgraphMergeNodes(CMR *cmr, CMR_GRAPH *graph, CMR_GRAPH_NODE u, CMR_GRAPH_NODE v)
Merges two nodes u and v of graph.
Definition: graph.c:400
static bool CMRgraphEdgesValid(CMR_GRAPH *graph, CMR_GRAPH_ITER i)
Returns true if and only if iterator i for all edges is valid.
Definition: graph.h:444
static bool CMRgraphIncValid(CMR_GRAPH *graph, CMR_GRAPH_ITER i)
Returns true if iterator i for all incident edges of some node is valid.
Definition: graph.h:308
static CMR_GRAPH_NODE CMRgraphNodesNext(CMR_GRAPH *graph, CMR_GRAPH_NODE v)
Returns the next node after v.
Definition: graph.h:270
static CMR_GRAPH_ITER CMRgraphEdgesNext(CMR_GRAPH *graph, CMR_GRAPH_ITER i)
Returns iterator of next edge in list of all edges.
Definition: graph.h:390
static CMR_GRAPH_ITER CMRgraphEdgesFirst(CMR_GRAPH *graph)
Returns iterator for all edges of graph.
Definition: graph.h:418
CMR_EXPORT CMR_ERROR CMRgraphAddNode(CMR *cmr, CMR_GRAPH *graph, CMR_GRAPH_NODE *pnode)
Adds a node to a graph.
Definition: graph.c:181
static CMR_GRAPH_ITER CMRgraphIncNext(CMR_GRAPH *graph, CMR_GRAPH_ITER i)
Returns the iterator following iterator i for all edges incident to some node.
Definition: graph.h:325
CMR_EXPORT CMR_ERROR CMRgraphAddEdge(CMR *cmr, CMR_GRAPH *graph, CMR_GRAPH_NODE u, CMR_GRAPH_NODE v, CMR_GRAPH_EDGE *pedge)
Adds an edge to a graph.
Definition: graph.c:227
CMR_EXPORT CMR_ERROR CMRgraphDeleteEdge(CMR *cmr, CMR_GRAPH *graph, CMR_GRAPH_EDGE e)
Removes edge e from graph.
Definition: graph.c:329
static CMR_GRAPH_NODE CMRgraphEdgeU(CMR_GRAPH *graph, CMR_GRAPH_EDGE e)
Returns node u of edge e.
Definition: graph.h:123
static size_t CMRgraphNumEdges(CMR_GRAPH *graph)
Returns number of edges.
Definition: graph.h:109
CMR_EXPORT CMR_ERROR CMRgraphCopy(CMR *cmr, CMR_GRAPH *graph, CMR_GRAPH **pcopy)
Definition: graph.c:598
CMR_EXPORT CMR_ERROR CMRgraphCreateFromEdgeList(CMR *cmr, CMR_GRAPH **pgraph, CMR_ELEMENT **pedgeElements, char ***pnodeLabels, FILE *stream)
Definition: graph.c:430
static CMR_GRAPH_NODE CMRgraphNodesFirst(CMR_GRAPH *graph)
Returns a node iterator for iterating over all nodes.
Definition: graph.h:239
static CMR_GRAPH_NODE CMRgraphEdgeV(CMR_GRAPH *graph, CMR_GRAPH_EDGE e)
Returns node v of edge e.
Definition: graph.h:138
int CMR_GRAPH_ITER
Reference to an edge iterator of CMR_GRAPH.
Definition: graph.h:32
CMR_EXPORT CMR_ERROR CMRgraphClear(CMR *cmr, CMR_GRAPH *graph)
Removes all nodes and columns, keeping the memory.
Definition: graph.c:155
CMR_EXPORT CMR_ERROR CMRgraphDeleteNode(CMR *cmr, CMR_GRAPH *graph, CMR_GRAPH_NODE v)
Removes node v and all its incident edges from graph.
Definition: graph.c:292
static bool CMRgraphNodesValid(CMR_GRAPH *graph, CMR_GRAPH_NODE v)
Return true if and only if this v is not the last node in node iteration.
Definition: graph.h:253
int CMR_GRAPH_EDGE
Reference to an edge of CMR_GRAPH.
Definition: graph.h:31
CMR_EXPORT CMR_ERROR CMRgraphFree(CMR *cmr, CMR_GRAPH **pgraph)
Frees a graph.
Definition: graph.c:130
static CMR_GRAPH_ITER CMRgraphIncFirst(CMR_GRAPH *graph, CMR_GRAPH_NODE v)
Returns an iterator for all edges incident to node v.
Definition: graph.h:285
static CMR_GRAPH_NODE CMRgraphIncSource(CMR_GRAPH *graph, CMR_GRAPH_ITER i)
Returns the node of which iterator i traverses through incident edges.
Definition: graph.h:364
Definition: env_internal.h:45
Definition: graph.h:42
int prev
Next arc in out-arc list of source node.
Definition: graph.h:44
int next
Previous arc in out-arc list of source node.
Definition: graph.h:45
int target
Target node of this arc.
Definition: graph.h:43
Definition: graph.h:35
int prev
Next node in node list.
Definition: graph.h:36
int firstOut
First out-arc of this node.
Definition: graph.h:38
int next
Previous node in node list.
Definition: graph.h:37
Definition: graph.h:49
CMR_GRAPH_ARC_DATA * arcs
Array containing arc data.
Definition: graph.h:58
size_t memNodes
Number of nodes for which memory is allocated.
Definition: graph.h:51
size_t numNodes
Number of nodes.
Definition: graph.h:50
CMR_GRAPH_NODE_DATA * nodes
Array containing node data.
Definition: graph.h:52
size_t numEdges
Number of edges.
Definition: graph.h:56
size_t memEdges
Number of edges for which memory is allocated.
Definition: graph.h:57
int freeEdge
Beginning of free-list of arc.
Definition: graph.h:59
int freeNode
Beginning of free-list of nodes.
Definition: graph.h:54
int firstNode
Index of first node.
Definition: graph.h:53