-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
89 lines (62 loc) · 2.6 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
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <fstream>
#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{
public:
// 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;
//number of number variables, determined in getVariable function
int numCount = 0;
~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,long status);
// 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(char argv[]);
vector<int> topologicalSort();
// checks whether the graph is cyclic or not.
bool isCyclic();
// performs forward pass for the graph and returns the output value.
double forwardPass(vector<string> inputNames,vector<string> inputValues);
// performs backward pass for the graph and returns the derivative values.
void backwardPass();
};
#endif //GRAPH