-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
99 lines (71 loc) · 2.82 KB
/
graph.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
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <vector>
#include <string>
#include <unordered_map>
#include <queue>
#include <iterator>
#include "variable.h"
#include "function.h"
using namespace std;
// enum that has VARIABLE and FUNCTION with values of 0 and 1.
enum TYPES{VARIABLE,FUNCTION};
/*
Graph class is created in order to construct directed graph structure
*/
class Graph{
// map storing the name, id pairs as key and value for each node.
unordered_map<string, int> id;
// map storing the id, name pairs as key and value for each node.
unordered_map<int, string> name;
// map storing the id, type pairs as key and value for each node.
unordered_map<int, TYPES> type;
// map storing the id, variable* pairs as key and value for each variable.
// in other words, it points the allocated memory of variable with given id.
unordered_map<int, Variable *> vars;
// map storing the id, function* pairs as key and value for each function.
// in other words, it points the allocated memory of function with given id.
unordered_map<int, Function *> fncs;
// stores the ids of input nodes.
vector<int> inputNodes;
// stores the id of ouput node
int outputNode;
// helps to count ids.
int idCount=0;
//stores ids of nodes in topological order
vector<int> topological_order;
public:
~Graph(); // destructor
// returns the id of variable with given variable name
// if the given name is new then it creates an instance of variable and
// updates the described mappings above and returns the id of it.
int getVariable(string name);
// it creates an instance of function and
// updates the described mappings above and returns the id of it.
int getFunction(string name);
// its a helper function to add assignment to graph.
void addAssignment(string lvalue, string rvalue);
// its a helper function to add unary function to graph
void addUnaryFunction(string fnc, string inp, string out);
// its a helper function to add binary function to graph
void addBinaryFunction(string fnc, string inp1, string inp2, string out);
// it reads the graph description and constructs it.
void readGraph(string filename);
// performs forward pass for the graph and returns the output value.
double forwardPass(vector<double> inputValues);
// performs backward pass for the graph and returns the derivative values.
vector<double> backwardPass();
//performs topological sort
void initTopologicalOrder();
//returns name of the output variable
string getOutputName();
//returns names of the derivative expressions
string getDerivativeName();
//shows whether graph is cyclic or not
bool isCyclic = false;
};
#endif //GRAPH