find-eventual-safe-states 1.0.0
Find Eventual Safe States
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

vector< int > eventualSafeNodes (vector< vector< int > > &graph)
 

Private Member Functions

bool dfs (int node, vector< vector< int > > &graph, vector< int > &color)
 

Detailed Description

Definition at line 5 of file main.cpp.

Member Function Documentation

◆ dfs()

bool Solution::dfs ( int  node,
vector< vector< int > > &  graph,
vector< int > &  color 
)
inlineprivate

Definition at line 8 of file main.cpp.

9 {
10 if (color[node] > 0)
11 return color[node] == 2;
12
13 color[node] = 1; // mark as visiting
14 for (int neighbor : graph[node])
15 {
16 if (color[neighbor] == 2)
17 continue;
18 if (color[neighbor] == 1 || !dfs(neighbor, graph, color))
19 return false;
20 }
21 color[node] = 2; // mark as safe
22 return true;
23 }
bool dfs(int node, vector< vector< int > > &graph, vector< int > &color)
Definition main.cpp:8

References dfs().

Referenced by dfs(), and eventualSafeNodes().

◆ eventualSafeNodes()

vector< int > Solution::eventualSafeNodes ( vector< vector< int > > &  graph)
inline

Definition at line 26 of file main.cpp.

27 {
28 int n = (int)graph.size();
29 vector<int> color(n, 0); // 0: unvisited, 1: visiting, 2: safe
30
31 vector<int> result;
32 for (int i = 0; i < n; ++i)
33 {
34 if (dfs(i, graph, color))
35 result.push_back(i);
36 }
37 return result;
38 }

References dfs().

Referenced by main().


The documentation for this class was generated from the following file: