-
Notifications
You must be signed in to change notification settings - Fork 0
/
directed_graph.h
72 lines (50 loc) · 1.51 KB
/
directed_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
#ifndef DIRECTED_GRAPH_H
#define DIRECTED_GRAPH_H
#include <iostream>
#include <utility>
#include <vector>
#include <string>
class Graph {
public:
Graph();
Graph(std::string &);
~Graph();
// static void Init(v8::Handle<v8::Object> target);
void print();
void trim();
void add_node(const std::string &name);
void add_edge(const std::string &, const std::string &, double);
void update_edge(const std::string &, const std::string &, double);
bool search(const std::string &name);
bool bellman_ford(const std::string &name, std::vector<std::vector<std::string>> &path);
private:
/* Structs */
struct GEdge;
struct GNode {
std::string name;
std::vector<GEdge> edges;
int key;
GNode(std::string name, int k) : name(std::move(name)), key(k) {}
};
struct GEdge {
double weight;
GNode *node; // node that the edge is pointed towards
GEdge(double weight, GNode *&n) : weight(weight), node(n) {}
};
/* Variables */
GNode *source;
std::vector<GNode> nodes;
/* Functions */
static void add_edge(GNode *&origin, GNode *&destination, double weight);
bool bellman_ford(GNode *&origin, std::vector<std::vector<std::string> > &);
GNode *get_node(const std::string &name) {
for (auto &node : nodes) {
if (name == node.name) {
return &node;
}
}
// this should be fixed to an empty node
return &nodes.at(0);
}
};
#endif