graph_algo.h
Go to the documentation of this file.
1 #ifndef _GRAPH_ALGO_H
2 #define _GRAPH_ALGO_H
3 
4 #include "../containers/graph.h"
5 #include "../containers/vertex.h"
6 #include "../containers/edge.h"
7 #include <unordered_map>
8 #include <iostream>
9 #include <vector>
10 #include <iterator>
11 #include <queue>
12 #include <stack>
13 
14 /*
15 The following function is templatized by vertex type (a subtype of Vertex class)
16 and a Sequence (linear).
17 */
18 
19 
20 
21 template<typename OutputIter>
22 OutputIter topological_sort(UndirectedGraph& G, OutputIter dest) {
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 }
36 
37 
38 
39 template<typename OutputIter>
40 OutputIter connected_components(UndirectedGraph& G, OutputIter dest) {
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 }
69 
70 
71 
72 
73 // Future work
74 /*
75 
76 template<typename InputIter>
77 OutputIter connected_components(const Graph& G, InputIter dfs_order) {
78 
79 
80 }
81 
82 
83 template<typename OutputIter>
84 OutputIter strongly_connected_components(const DirectedGraph& G, OutputIter dest) {
85  // dest must be an iterator to an associative container.
86  // The output will be a mapping of a vertex to its componenet id (an int)
87 
88 
89 }
90 
91 
92 // Helper function for shortest paths algorithm
93 static void relax(const Graph& G, Edge& e) {
94 
95 
96 }
97 
98 
99 void Bellman_Ford(const Graph& G, Vertex* source, OutputIter dest) {
100 
101 }
102 
103 
104 void dijskitra_shortest_path(const Graph& G, Vertex* source, OutputIter dest) {
105  // dest must be an iterator to an associative iterator.
106 
107 }
108 
109 */
110 
111 
112 
113 #endif
OutputIter connected_components(UndirectedGraph &G, OutputIter dest)
Definition: graph_algo.h:40
OutputIter topological_sort(UndirectedGraph &G, OutputIter dest)
Definition: graph_algo.h:22