Repository navigation
Expand file tree
/
Copy pathmy_graph.cpp
More file actions
58 lines (56 loc) · 1.66 KB
/
Copy pathmy_graph.cpp
File metadata and controls
58 lines (56 loc) · 1.66 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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct Graph {
map<int, vector<int>> edges;
//Specifications:
//Requires: Two vectors vectors of type int including the edges start and end points.
//Effects: Initializes the graph with the given edges.
//Test cases:
//1- If the sizes of the vectors are not equal, we throw an invalid argument error.
Graph(const vector<int> start, const vector<int>& end) {
if (start.size() != end.size()) {
throw invalid_argument("Sizes of start and end don't match!");
}
else {
for (int i = 0; i < start.size(); i++) {
int k = start.at(i);
if (edges.count(k)) {
edges[k].push_back(end.at(i));
}
else {
edges.insert(pair<int, vector<int>>(k, { end.at(i) }));
}
}
}
}
//Specifications:
//Requires: An integer that is the nodeID
//Effects: Returns the number of outgoing edges from the given nodeID.
//Test cases:
//1- If the nodeID is not in the graph, we print "The node is not in the graph" and return -1.
int numOutgoing(const int nodeID) {
if (edges.count(nodeID)) {
return(edges[nodeID].size());
}
else {
cout << "The node is not in the graph";
return -1;
}
}
//Specifications:
//Requires: An integer that is the nodeID
//Effects: Returns a reference to the list of nodes which nodeID has outgoing edges
//Test cases:
//1- If the nodeID is not in the graph, we print "The node is not in the graph" and return -1.
const vector<int>& adjacent(const int NodeID) {
if (edges.count(NodeID)) {
return edges[NodeID];
}
else {
cout << "The node is not in the graph";
return {};
}
}
};