-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
71 lines (58 loc) · 1.72 KB
/
Graph.cpp
File metadata and controls
71 lines (58 loc) · 1.72 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Graph.cpp
* Author: Raell
*
* Created on May 18, 2018, 6:31 PM
*/
#include "Graph.h"
#include <algorithm>
using namespace std;
Graph::Graph() {
}
// Add nodes to graph from 0 to n-1
void Graph::addNodes(unsigned n) {
for(int i = 0; i < n; i++) {
nodes.push_back(Node(i));
}
}
// Return reference to specified node
Node* Graph::getNode(unsigned index) {
return &nodes[index];
}
// Returns all nodes
vector<Node> Graph::getNodes() {
return nodes;
}
// Adds an Edge to graph
void Graph::addEdge(const Edge& edge) {
edges.push_back(edge);
}
// Return all edges
vector<Edge> Graph::getEdges() {
return edges;
}
// Return all edges with source specified
vector<Edge> Graph::getEdgesFrom(unsigned source) {
vector<Edge> edgesFrom;
auto lambda = [source](Edge e){return e.getSource() == source;};
auto it = copy_if(edges.begin(), edges.end(),
back_inserter(edgesFrom),
lambda
);
return edgesFrom;
}
// Return all edges with destination specified
vector<Edge> Graph::getEdgesTo(unsigned destination) {
vector<Edge> edgesTo;
auto lambda = [destination](Edge e){return e.getDestination() == destination;};
auto it = copy_if(edges.begin(), edges.end(),
back_inserter(edgesTo),
lambda
);
return edgesTo;
}