diff options
| author | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-13 23:04:26 +0100 | 
|---|---|---|
| committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-13 23:04:26 +0100 | 
| commit | a80b82ef6c4214d16b5c6df76c90fb88f23329f7 (patch) | |
| tree | c1c336c3c0691df3cf2969a5a286e20509833a6f /03-algorithms_on_graphs/02-decomposition/03-strongly_connected | |
| parent | ce845d792479e9c153274c2ad5a199a7f7c17a41 (diff) | |
| download | coursera-a80b82ef6c4214d16b5c6df76c90fb88f23329f7.zip coursera-a80b82ef6c4214d16b5c6df76c90fb88f23329f7.tar.gz | |
Algorithms : complete 03-algorithms_on_graphs 02-decomposition
Diffstat (limited to '03-algorithms_on_graphs/02-decomposition/03-strongly_connected')
| -rw-r--r-- | 03-algorithms_on_graphs/02-decomposition/03-strongly_connected/strongly_connected.cpp | 56 | 
1 files changed, 51 insertions, 5 deletions
| diff --git a/03-algorithms_on_graphs/02-decomposition/03-strongly_connected/strongly_connected.cpp b/03-algorithms_on_graphs/02-decomposition/03-strongly_connected/strongly_connected.cpp index 0ad7b62..558c597 100644 --- a/03-algorithms_on_graphs/02-decomposition/03-strongly_connected/strongly_connected.cpp +++ b/03-algorithms_on_graphs/02-decomposition/03-strongly_connected/strongly_connected.cpp @@ -1,14 +1,60 @@  #include <algorithm>  #include <iostream>  #include <vector> +#include <stack>  using std::vector; -using std::pair; +using std::stack; + +void dfs(vector<vector<int> > &adj, vector<bool> &visited, stack<int> &order, int v) { +    visited[v] = true; +    int s = adj[v].size(); +    for (int j = 0; j < s; j++) { +        int w = adj[v][j]; +        if (!visited[w]) +            dfs(adj, visited, order, w); +    } +    order.push(v); +} + +void dfs(vector<vector<int> > &adj, vector<bool> &visited, int v) { +    visited[v] = true; +    int s = adj[v].size(); +    for (int j = 0; j < s; j++) { +        int w = adj[v][j]; +        if (!visited[w]) +            dfs(adj, visited, w); +    } +}  int number_of_strongly_connected_components(vector<vector<int> > adj) { -  int result = 0; -  //write your code here -  return result; +    int result = 0; + +    vector<vector<int> > radj(adj.size(), vector<int>()); +    for (size_t i = 0; i < adj.size(); i++) +        for (size_t j = 0; j < adj[i].size(); j++) +            radj[adj[i][j]].push_back(i); + +    stack<int> order; +    vector<bool> visited(radj.size(), 0); + +    for (size_t i = 0; i < adj.size(); i++) +        if (!visited[i]) +            dfs(adj, visited, order, i); + +    for (size_t i = 0; i < visited.size(); i++) +        visited[i] = false; + +    while(!order.empty()) { +        int v = order.top(); +        order.pop(); +        if (!visited[v]) { +            result += 1; +            dfs(radj, visited, v); +        } +    } + +    return result;  }  int main() { @@ -20,5 +66,5 @@ int main() {      std::cin >> x >> y;      adj[x - 1].push_back(y - 1);    } -  std::cout << number_of_strongly_connected_components(adj); +  std::cout << number_of_strongly_connected_components(adj) << '\n';  } | 
