Repository navigation
Expand file tree
/
Copy pathgraph_cycles.cpp
More file actions
93 lines (89 loc) · 2.58 KB
/
Copy pathgraph_cycles.cpp
File metadata and controls
93 lines (89 loc) · 2.58 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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 {};
}
}
//Requires: nodeId is a valid node in the graph, a vector of type boolean, and a vector of type integer.
//Effects: returns a vector of the first non-trivial cycle
vector<int> hasCycle(int nodeid, vector<bool>& visited, vector<int>& path) {
visited[nodeid] = true;
vector<int> ourList;
path.push_back(nodeid);
for (int i = 0; i < adjacentvector[nodeid].size(); i++)
{
if (!visited[adjacentvector[nodeid][i]])
{
ourList = hasCycle(adjacentvector[nodeid][i], visited, path);
}
else
{
for (int j = 0; j < path.size(); j++)
{
if (path[j] == adjacentvector[nodeid][i])
{
for (int k = j; k < path.size(); k++)
{
ourList.push_back(path[k]);
}
ourList.push_back(adjacentvector[nodeid][i]);
return ourList;
}
}
}
}
path.pop_back();
return ourList;
}
private:
map<int, vector<int>> adjacentvector;
};