HW04
Source codes for forth assignment
graph.h
Go to the documentation of this file.
1 /**
2  * Interface for creating graph.
3  *
4  * This file declares functions and types for creating graphs.
5  *
6  * @file graph.h
7  * @author Lubomir Sedlar
8  */
9 #ifndef GRAPH_H
10 #define GRAPH_H
11 
12 #include <stdio.h>
13 #include <stdbool.h>
14 
15 /**
16  * Node is an opaque type. You can have a pointer to it, but the only thing
17  * you can do with that pointer is pass it given functions.
18  */
19 typedef struct node Node;
20 
21 /**
22  * Graph is an opaque type. To work with it, use provided functions, but do
23  * not try to access its members directly.
24  */
25 typedef struct graph Graph;
26 
27 /**
28  * Representation of an edge.
29  *
30  * Since this is a simple struct (and not a type), direct access to its
31  * members is possible. However, make sure not to change the values.
32  */
33 struct edge {
34  /** Pointer to destination node of this edge. */
36  /** Minimum delay of this edge. */
37  int mindelay;
38 };
39 
40 /**
41  * Get id of a node.
42  *
43  * @param n node to query
44  * @return node id
45  */
46 unsigned int node_get_id(Node *n);
47 
48 /**
49  * Get number of outgoing edges from a node.
50  *
51  * @param n node to query
52  * @return number of outgoing edges
53  */
54 unsigned short node_get_n_outgoing(Node *n);
55 
56 /**
57  * Get array of outgoing edges.
58  * The number of elements in this array is returned by node_get_n_outgoing().
59  *
60  * @param n node to query
61  * @return array of outgoing edges
62  */
63 struct edge * node_get_edges(Node *n);
64 
65 /**
66  * Get distance of this node from starting node.
67  * Infinity is signalled by `UINT_MAX`.
68  *
69  * @param n node to query
70  * @return total distance
71  */
72 unsigned int node_get_distance(Node *n);
73 
74 /**
75  * Get node on (already found) shortest path from which queried node was
76  * visited. For starting and unreachable nodes this function returns NULL.
77  *
78  * @param n node to query
79  * @return previous node or NULL
80  */
82 
83 /**
84  * Create a new graph.
85  *
86  * @return new graph or NULL if memory is exhausted
87  */
88 Graph * graph_new(void);
89 
90 /**
91  * Insert node into the graph.
92  * This function fails only if memory runs out.
93  *
94  * @param g graph to insert into
95  * @param id identifier of the new node
96  * @return true if successful, false otherwise
97  */
98 bool graph_insert_node(Graph *g, unsigned int id);
99 
100 /**
101  * Insert an edge into the graph.
102  * Both nodes need to be present in the graph, otherwise the function will
103  * fail. Other reason of failure is malloc failure.
104  *
105  * @param g graph to insert into
106  * @param source starting node of the edge
107  * @param dest ending node of the edge
108  * @param mindelay minimum delay on this edge
109  * @return true if successful, false otherwise
110  */
111 bool graph_insert_edge(Graph *g, unsigned int source, unsigned int dest,
112  int mindelay);
113 
114 /**
115  * Retrieve a node.
116  * This function performs binary search, therefore needs the nodes to be
117  * sorted. Sorting is done automatically if necessary. Inserting a node will
118  * break the sorted property, so first node retrieval after insertion is going
119  * to be slow.
120  *
121  * @param g graph to be searched
122  * @param id identifier of the node
123  * @return pointer to node or NULL if such node does not exist
124  */
125 Node * graph_get_node(Graph *g, unsigned int id);
126 
127 /**
128  * Free memory used by nodes and edges.
129  *
130  * @param g graph to be freed
131  */
132 void graph_free(Graph *g);
133 
134 #endif /* end of include guard: GRAPH_H */
Node * graph_get_node(Graph *g, unsigned int id)
Retrieve a node.
Definition: graph.c:132
Node * destination
Pointer to destination node of this edge.
Definition: graph.h:35
void graph_free(Graph *g)
Free memory used by nodes and edges.
Definition: graph.c:176
Representation of an edge.
Definition: graph.h:33
struct edge * node_get_edges(Node *n)
Get array of outgoing edges.
Definition: graph.c:37
struct node Node
Node is an opaque type.
Definition: graph.h:19
unsigned int node_get_id(Node *n)
Get id of a node.
Definition: graph.c:25
bool graph_insert_edge(Graph *g, unsigned int source, unsigned int dest, int mindelay)
Insert an edge into the graph.
Definition: graph.c:103
int mindelay
Minimum delay of this edge.
Definition: graph.h:37
Graph * graph_new(void)
Create a new graph.
Definition: graph.c:60
unsigned int node_get_distance(Node *n)
Get distance of this node from starting node.
Definition: graph.c:43
bool graph_insert_node(Graph *g, unsigned int id)
Insert node into the graph.
Definition: graph.c:75
unsigned short node_get_n_outgoing(Node *n)
Get number of outgoing edges from a node.
Definition: graph.c:31
Node * node_get_previous(Node *n)
Get node on (already found) shortest path from which queried node was visited.
Definition: graph.c:49
struct graph Graph
Graph is an opaque type.
Definition: graph.h:25