graph_algo.h File Reference
#include "../containers/graph.h"
#include "../containers/vertex.h"
#include "../containers/edge.h"
#include <unordered_map>
#include <iostream>
#include <vector>
#include <iterator>
#include <queue>
#include <stack>

Go to the source code of this file.

Functions

template<typename OutputIter >
OutputIter topological_sort (UndirectedGraph &G, OutputIter dest)
 
template<typename OutputIter >
OutputIter connected_components (UndirectedGraph &G, OutputIter dest)
 

Function Documentation

template<typename OutputIter >
OutputIter connected_components ( UndirectedGraph &  G,
OutputIter  dest 
)
40  {
41  // dest must be an iterator to an associative container.
42  // The output will be a mapping of a vertex to its componenet id (an int)
43 
44 
45  std::unordered_map<Vertex*, bool, UndirectedGraph::Hasher> visited;
46  std::vector<Vertex*> pvertex_list(G.no_of_vertices());
47  G.get_vertices(pvertex_list.begin());
48 
49  for(auto& pvertex : pvertex_list)
50  visited[pvertex] = false; // Initially set all the vertices to unvisited
51 
52 
53  std::vector<Vertex*> dfs_list(G.no_of_vertices());
54 
55  std::vector<Vertex*>::iterator dfs_it = dfs_list.begin();
56  int component_id = 0;
57 
58  for(auto& pvertex : pvertex_list) {
59  auto prev_it = dfs_it;
60  if(!visited[pvertex])
61  dfs(G, pvertex, visited, dfs_it);
62  for(auto it = prev_it; it != dfs_it; it++) {
63  // somehow insert at the place where dest points
64  }
65 
66  }
67  return dest;
68 }
template<typename OutputIter >
OutputIter topological_sort ( UndirectedGraph &  G,
OutputIter  dest 
)

Fills the input container with the topological order.

22  {
27  typedef typename std::iterator_traits<OutputIter>::value_type val_type;
28 
29  std::vector<val_type> postorder(G.no_of_vertices());
30 
31  dfs_order(G, postorder.begin());
32 
33  return copy(postorder.rbegin(), postorder.rend(), dest);
34 
35 }