Flow123d  JS_before_hm-1575-ga41e096
dfs_topo_sort.hh
Go to the documentation of this file.
1 /*!
2  *
3 ï»¿ * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file dfs_topo_sort.hh
15  * @brief
16  */
17 
18 #ifndef DFS_TOPO_SORT_HH_
19 #define DFS_TOPO_SORT_HH_
20 
21 #include <list>
22 #include <stack>
23 #include <vector>
24 
25 using namespace std;
26 
27 /**
28  * Class to represent a graph and allows to perform sorting.
29  *
30  * DFS topological sorting is used.
31  */
32 class DfsTopoSort {
33  /// Number of vertices
34  unsigned int nv_;
35 
36  /// Pointer to an array containing adjacency lists
38 
39  /// A function used by topological_sort
40  void topological_sort_util(unsigned int v, bool visited[],
41  stack<unsigned int>& stc);
42 
43 public:
44  /// Constructor
45  DfsTopoSort(unsigned int nv)
46  : nv_(nv)
47  {
48  adj_ = new list<unsigned int>[nv];
49  }
50 
51  /// Destructor
53  {
54  delete[] adj_;
55  }
56 
57  /// Clear content of adjacency lists
58  inline void clear()
59  {
60  for (uint i=0; i<nv_; ++i)
61  adj_[i].clear();
62  }
63 
64  /**
65  * Add an edge to graph
66  *
67  * Vertex 'w' is descendant of 'v'
68  */
69  inline void add_edge(unsigned int v, unsigned int w)
70  {
71  // Add w to v’s list.
72  adj_[v].push_back(w);
73  }
74 
75  /**
76  * Makes a DFS Topological Sort of the complete graph and returns result vector of sorting algorithm.
77  */
78  vector<unsigned int> topological_sort();
79 };
80 
81 
82 // A recursive function used by topological_sort
83 void DfsTopoSort::topological_sort_util(unsigned int v, bool visited[],
84  stack<unsigned int>& stc)
85 {
86  // Mark the current node as visited.
87  visited[v] = true;
88 
89  // Recur for all the vertices
90  // adjacent to this vertex
92  for (i = adj_[v].begin(); i != adj_[v].end(); ++i)
93  if (!visited[*i])
94  topological_sort_util(*i, visited, stc);
95 
96  // Push current vertex to stack
97  // which stores result
98  stc.push(v);
99 }
100 
101 // The function to do Topological Sort.
102 // It uses recursive topological_sort_util()
104 {
105  stack<unsigned int> stc;
106 
107  // Mark all the vertices as not visited
108  bool* visited = new bool[nv_];
109  for (unsigned int i = 0; i < nv_; i++)
110  visited[i] = false;
111 
112  // Call the recursive helper function
113  // to store Topological
114  // Sort starting from all
115  // vertices one by one
116  for (unsigned int i = 0; i < nv_; i++)
117  if (visited[i] == false)
118  topological_sort_util(i, visited, stc);
119 
120  // Move contents of stack to vector
122  vec.resize(stc.size());
123  while (!stc.empty()) {
124  vec[stc.size()-1] = stc.top();
125  stc.pop();
126  }
127 
128  return vec;
129 }
130 
131 #endif /* DFS_TOPO_SORT_HH_ */
ArmaVec< double, N > vec
Definition: armor.hh:885
unsigned int uint
void add_edge(unsigned int v, unsigned int w)
void topological_sort_util(unsigned int v, bool visited[], stack< unsigned int > &stc)
A function used by topological_sort.
vector< unsigned int > topological_sort()
unsigned int nv_
Number of vertices.
void clear()
Clear content of adjacency lists.
~DfsTopoSort()
Destructor.
DfsTopoSort(unsigned int nv)
Constructor.
list< unsigned int > * adj_
Pointer to an array containing adjacency lists.