1 /*-------------------------------------------------------------------------
3 graph.h - header file for graph.c
5 Written By - Raphael Neider <rneider AT web.de> (2005)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 -------------------------------------------------------------------------*/
22 /* $Id: graph.h 4781 2007-04-29 20:33:44Z borutr $ */
27 #include "../common.h"
29 typedef unsigned int hash_t;
33 typedef struct GraphEdge {
34 struct GraphNode *src; // starting node of this edge
35 struct GraphNode *node; // other end of this edge
36 unsigned int weight; // weight assigned to this edge
37 struct GraphEdge *prev; // link to previous edge
38 struct GraphEdge *next; // link to next edge
41 typedef struct GraphNode {
42 void *data; // data stored in this node
43 hash_t hash; // hash value for "data"
45 GraphEdge *edge; // first edge leaving this node
46 struct GraphNode *prev; // link to previous node
47 struct GraphNode *next; // link to next edge
50 // compare function, returns 0 for different items and 1 for equal items
51 typedef int Graph_compareData(void *item1, void *item2);
54 GraphNode *node; // first node in this graph
55 Graph_compareData *compare; // function used to compare two data items
58 /* Create a new edge from src to dest.
59 * Returns a pointer to the new edge. */
60 GraphEdge *newGEdge (GraphNode *src, GraphNode *dest, unsigned int weight);
62 /* Delete an edge and remove it from the containing list.
63 * Returns a pointer to the previous edge or (if there is NULL) to its successor. */
64 GraphEdge *deleteGEdge (GraphEdge *edge);
68 /* Create a new node. */
69 GraphNode *newGNode (void *data, hash_t hash);
71 /* Delete a node and all its edges. this also removes the node
72 * from its containing list.
73 * Returns the previous node in the list or (if there is NULL)
75 GraphNode *deleteGNode (GraphNode *node);
77 /* Adds an edge with the given weight. If the edge already exists,
78 * its weight its increased instead! */
79 GraphEdge *addGEdge (GraphNode *from, GraphNode *to, unsigned int weight);
81 /* Adds the edges (from,to) and (to,from) with the specified weights. */
82 void addGEdge2 (GraphNode *from, GraphNode *to, unsigned int weight, unsigned int weight_back);
84 /* Remove an edge from the node. This deletes the edge and updates the
85 * list of edges attached to the "from" node. */
86 void remGEdge (GraphNode *from, GraphNode *to);
88 /* Returns the edge (from,to) or NULL if no such edge exists. */
89 GraphEdge *getGEdge (GraphNode *from, GraphNode *to);
93 /* Create a new graph which uses the given compare function to test
94 * its nodes' data for equality. */
95 Graph *newGraph (Graph_compareData *compare);
97 /* Delete a graph, all its contained nodes and their edges. */
98 void deleteGraph (Graph *graph);
100 /* Add a node to the graph. */
101 GraphNode *addGNode (Graph *graph, void *data, hash_t hash);
103 /* Remove a node from the graph. This also deletes the node and all
104 * its associated (outbound) edges. */
105 void remGNode (Graph *graph, void *data, hash_t hash);
107 /* Returns the specified node or NULL if no such node exists. */
108 GraphNode *getGNode (Graph *graph, void *data, hash_t hash);
110 /* Returns the specified node (after creating it if neccessary). */
111 GraphNode *getOrAddGNode (Graph *graph, void *data, hash_t hash);