-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraph_Node.h
174 lines (138 loc) · 3.6 KB
/
Graph_Node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Created by Shubi & Eyal on 21/12/2019.
//
#ifndef DALGO_GRAPH_NODE_H
#define DALGO_GRAPH_NODE_H
#include <cstddef>
#include "Graph_Edge.h"
#define ZERO 0
/*
* Each instance represents a graph node
*/
class Graph_Node {
public:
/*
* Constructor
*
*/
Graph_Node(unsigned int key,
Graph_Node *prevNode,
Graph_Node *nextNode):
_key(key), _prev_node(prevNode),
_next_node(nextNode), _num_of_incoming_edges(ZERO), _num_of_outgoing_edges(ZERO),
_out_edges(NULL), _reverse_edges(NULL)
{}
~Graph_Node(){
}
/*
* Returns the number of edges going into this node
*/
unsigned Get_out_Degree() const {
return this->_num_of_outgoing_edges;
}
/*
* Returns the number of edges going out from this node
*/
unsigned Get_in_Degree() const {
return this->_num_of_incoming_edges;
}
/*
* Returns the Key of this node
*/
unsigned Get_key() const{
return _key;
}
/*
* Previous Node Getter
*/
Graph_Node *getPrevNode() const {
return _prev_node;
}
/*
* Previous Node Setter
*/
void setPrevNode(Graph_Node *prevNode) {
_prev_node = prevNode;
}
/*
* Next Node Getter
*/
Graph_Node *getNextNode() const {
return _next_node;
}
/*
* Next Node Setter
*/
void setNextNode(Graph_Node *nextNode) {
_next_node = nextNode;
}
/*
* Returns true if node has any incoming or outgoing edges
* False otherwise
*/
bool has_incoming_outgoing_edges(){
return (_num_of_outgoing_edges != 0) || (_num_of_incoming_edges != 0);
}
/*
* Adds an edge-(this node, target)-'new_first_edge'
* to linked list maintained by the node
* Adds it at the beginning to comply with requirements
*/
void add_edge_to_edges(Graph_Edge* new_first_edge, bool is_reverse) {
Graph_Edge* edge_list = NULL;
if(is_reverse){
edge_list = _reverse_edges;
}else{
edge_list = _out_edges;
}
Graph_Edge* current_first_edge = edge_list;
new_first_edge->setNextEdge(current_first_edge);
if(current_first_edge != NULL) {
current_first_edge->setPrevEdge(new_first_edge);
}
if(is_reverse){
_reverse_edges = new_first_edge;
}else{
_out_edges = new_first_edge;
}
}
/*
* Color Getter
*/
unsigned int getColor() const {
return _color;
}
/*
* Color Setter
*/
void setColor(unsigned int color) {
_color = color;
}
/*
* Distance Getter
*/
unsigned int getDistance() const {
return _distance;
}
/*
* Distance Setter
*/
void setDistance(unsigned int distance) {
_distance = distance;
}
unsigned _key; // Unique id of the node
// for storing graph implementation data structure
Graph_Node* _prev_node;
Graph_Node* _next_node;
unsigned _num_of_incoming_edges;
unsigned _num_of_outgoing_edges;
// Pointer to first edge in adjacency list of outgoing edges from the node
Graph_Edge* _out_edges;
// Pointer to first edge in adjacency list of incoming edges to the node
Graph_Edge* _reverse_edges;
// Attributes for BFS & DFS Algorithms
unsigned _color; // the discovery status
// Attributes for BFS Algorithm
unsigned _distance; // the distance from source to this node in G
};
#endif //DALGO_GRAPH_NODE_H