-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindVertexInDAGWithZeroInDegree.cpp
More file actions
90 lines (75 loc) · 2.31 KB
/
FindVertexInDAGWithZeroInDegree.cpp
File metadata and controls
90 lines (75 loc) · 2.31 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
#include "CppUnitTest.h"
#include <cassert>
#include <list>
#include <vector>
#include <limits>
#include <set>
#include <functional>
// Find a vertex with zero in-degree in a directed acyclic graph
// Time Complexity: O(V + E)
// Space Complexity: O(V)
namespace FindVertexInDAGWithZeroInDegree {
class Graph {
public:
Graph(int v) {
adj.resize(v);
}
void insert(int v, int u) {
assert(v >= 0 && v < adj.size());
assert(u >= 0 && u < adj.size());
adj[v].emplace_back(u);
}
// Traverses entire graph to find a node with zero in-degree.
// Returns int::min if no node with zero in-degree found.
int findNodeWithZeroInDegree() {
std::set<int> indegrees;
for (const std::list<int>& vadj : adj) {
for (const int& v : vadj) {
if (indegrees.find(v) == indegrees.end())
indegrees.insert(v);
}
}
// If no nodes in graph
if (indegrees.size() == 0)
return std::numeric_limits<int>::min();
// If all of the nodes have incoming edges then there is cycle in the graph.
if (indegrees.size() >= adj.size())
throw std::exception("Cycle in DAG");
for (auto i = 0; i < adj.size(); i++) {
if (indegrees.find(i) == indegrees.end()) {
return i;
}
}
return std::numeric_limits<int>::min();
}
private:
std::vector<std::list<int>> adj;
};
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
TEST_CLASS(FindVertexInDAGWithZeroInDegreeTests) {
public:
TEST_METHOD(WhenEmpty_ExpectMinReturned) {
Graph graph(0);
auto result = graph.findNodeWithZeroInDegree();
Assert::AreEqual(std::numeric_limits<int>::min(), result);
}
TEST_METHOD(WhenLinear_ExpectTopReturned) {
// 4 -> 3 -> 2 -> 1 -> 0
Graph graph(5);
graph.insert(3, 2);
graph.insert(1, 0);
graph.insert(2, 1);
graph.insert(4, 3);
auto result = graph.findNodeWithZeroInDegree();;
Assert::AreEqual(4, result);
}
TEST_METHOD(WhenCycle_ExpectException) {
Graph graph(3);
graph.insert(2, 1);
graph.insert(1, 0);
graph.insert(0, 2);
std::function<int(void)> f = std::bind(&Graph::findNodeWithZeroInDegree, graph);
Assert::ExpectException<std::exception>(f);
}
};
}