cycleDetection

Detect cycles in graphs
Log | Files | Refs | README

Graph.h (1108B)


      1 #ifndef GRAPH_H
      2 #define GRAPH_H
      3 
      4 #include "LinkedList.h"
      5 
      6 typedef struct Vertex Vertex;
      7 typedef struct Graph Graph;
      8 struct Vertex {
      9 	int id; // a number in [0; numVertices[
     10 	LinkedList *outNeighbours; // A linked list of vertices.
     11 	LinkedList *inNeighbours; // A linked list of vertices
     12 };
     13 
     14 struct Graph {
     15 	int numVertices;
     16 	int numEdges;
     17 	Vertex *vertices; // An array of numVertices vertices
     18 };
     19 
     20 // Allocates and constructs a new graph with n vertices.
     21 // Returns a pointer to the new graph, or NULL on error.
     22 // Post: the caller owns the graph.
     23 Graph *Graph_new(int n);
     24 
     25 // Adds an edge from the i'th to the j'th vertex (0-indexed).
     26 void Graph_addEdge(Graph *g, int i, int j);
     27 
     28 // Reads a graph from the given file and returns a newly
     29 // constructed Graph representing it.
     30 // Returns a pointer to the read graph, or NULL on error.
     31 // Post: the caller owns the graph.
     32 Graph *Graph_read(const char *filename);
     33 
     34 // Deallocates the given graph and all its associated memory.
     35 void Graph_delete(Graph *g);
     36 
     37 // Prints some useful information about the given graph.
     38 void Graph_print(Graph *g);
     39 
     40 #endif // GRAPH_H