Skip to content
Snippets Groups Projects
Commit 5093c739 authored by Stefan Braß's avatar Stefan Braß
Browse files

New graph data structures in graph.cpp

parent ec7fcf74
No related branches found
No related tags found
No related merge requests found
// Old implementation, maybe superior for some graphs.
//=============================================================================
// Class for Graph Nodes (one line of the adjacency matrix):
//=============================================================================
class Node {
public:
// Constructor:
Node(int graph_num_nodes)
{
num_nodes_ = graph_num_nodes;
int bytes = (num_nodes_ + BYTE_SIZE - 1) / BYTE_SIZE;
connections_ = new char[bytes];
char *p = connections_;
for(int i = bytes; i > 0; i--)
*p++ = '\0';
}
// Accessor Functions:
inline int num_nodes() const { return num_nodes_; }
// Store edge in adjacency matrix:
void store_connection(int to) {
// Check nodes:
if(to <= 0 || to > num_nodes_) {
std::cerr << "Invalid to node: " << to << "\n";
exit(3);
}
// Determine position in adjacency matrix:
long index = to - 1;
int bit = index % 8;
index = index / 8;
// Set bit:
connections_[index] |= (0x1) << bit;
}
// Check whether edge exists:
bool connection_exists(int to) const {
// Check node:
if(to <= 0 || to > num_nodes_) {
std::cerr << "Invalid to node: " << to << "\n";
exit(4);
}
// Determine position in adjacency matrix:
long index = to - 1;
int bit = index % 8;
index = index / 8;
// Check bit:
return (connections_[index] & ((0x1) << bit)) != 0;
}
// Copy other node into this node:
void copy(Node *node) {
if(node->num_nodes_ != num_nodes_) {
std::cerr << "Can copy only node of same size!\n";
exit(5);
}
int bytes = (num_nodes_ + BYTE_SIZE - 1) / BYTE_SIZE;
char *to = connections_;
char *from = node->connections_;
while(bytes-- > 0)
*to++ = *from++;
}
// Attributes:
private:
int num_nodes_;
char *connections_;
};
//-----------------------------------------------------------------------------
// Pointer Type for Node Data:
//-----------------------------------------------------------------------------
typedef Node *node_t;
#define NODE_NULL (static_cast<node_t>(0))
//=============================================================================
// Class for Graphs (contains adjacency matrix):
//=============================================================================
class Graph {
public:
// Constructor:
Graph(int graph_num_nodes)
{
num_nodes_ = graph_num_nodes;
nodes_ = new node_t[num_nodes_];
for(int i = 0; i < graph_num_nodes; i++)
nodes_[i] = new Node(graph_num_nodes);
}
// Accessor Functions:
inline int num_nodes() const { return num_nodes_; }
inline node_t node(int i) const { return nodes_[i-1]; }
// Store edge in adjacency matrix:
void store_edge(int from, int to) {
// Check nodes:
if(from <= 0 || from > num_nodes_) {
std::cerr << "Invalid from node: " << from << "\n";
exit(6);
}
if(to <= 0 || to > num_nodes_) {
std::cerr << "Invalid to node: " << to << "\n";
exit(7);
}
// Store edge in adjacency matrix:
long index = from - 1;
nodes_[index]->store_connection(to);
}
// Check whether edge exists:
bool edge_exists(int from, int to) const {
// Check nodes:
if(from <= 0 || from > num_nodes_) {
std::cerr << "Invalid from node: " << from << "\n";
exit(8);
}
if(to <= 0 || to > num_nodes_) {
std::cerr << "Invalid to node: " << to << "\n";
exit(9);
}
// Look up edge in adjacency matrix:
long index = from - 1;
return nodes_[index]->connection_exists(to);
}
// Copy a graph (of the same size) into this graph:
void copy(Graph *g)
{
if(g->num_nodes_ != num_nodes_) {
std::cerr << "Can copy only graph of same size!\n";
exit(10);
}
for(int i = 0; i < num_nodes_; i++)
nodes_[i]->copy(g->nodes_[i]);
}
// Attributes:
private:
int num_nodes_;
node_t *nodes_;
};
//-----------------------------------------------------------------------------
// Pointer Type for Graph Data:
//-----------------------------------------------------------------------------
typedef Graph *graph_t;
#define GRAPH_NULL (static_cast<graph_t>(0))
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment