99// BFS
1010#include < queue>
1111
12+ template <typename T> class Graph ;
13+
1214template <typename T>
1315struct Edge
1416{
@@ -29,6 +31,23 @@ struct Edge
2931 }
3032};
3133
34+ template <typename T>
35+ std::ostream& operator <<(std::ostream& os, const Graph<T>& G)
36+ {
37+ for (auto i = 1 ; i < G.vertices (); i++)
38+ {
39+ os << i << " :\t " ;
40+
41+ auto edges = G.outgoing_edges (i);
42+ for (auto & e : edges)
43+ os << " {" << e.dest << " : " << e.weight << " }, " ;
44+
45+ os << std::endl;
46+ }
47+
48+ return os;
49+ }
50+
3251template <typename T>
3352class Graph
3453{
@@ -49,7 +68,7 @@ class Graph
4968 return edge_list;
5069 }
5170
52- void add_edge (Edge<T>& e)
71+ void add_edge (Edge<T>&& e)
5372 {
5473 // Check if the source and destination vertices are within range
5574 if (e.src >= 1 && e.src <= V &&
@@ -73,31 +92,13 @@ class Graph
7392
7493 // Overloads the << operator so a graph be written directly to a stream
7594 // Can be used as std::cout << obj << std::endl;
76- template <typename T>
77- friend std::ostream& operator <<(std::ostream& os, const Graph<T>& G);
95+ friend std::ostream& operator << <>(std::ostream& os, const Graph<T>& G);
7896
7997private:
8098 size_t V; // Stores number of vertices in graph
8199 std::vector<Edge<T>> edge_list;
82100};
83101
84- template <typename T>
85- std::ostream& operator <<(std::ostream& os, const Graph<T>& G)
86- {
87- for (auto i = 1 ; i < G.vertices (); i++)
88- {
89- os << i << " :\t " ;
90-
91- auto edges = G.outgoing_edges (i);
92- for (auto & e : edges)
93- os << " {" << e.dest << " : " << e.weight << " }, " ;
94-
95- os << std::endl;
96- }
97-
98- return os;
99- }
100-
101102template <typename T>
102103auto create_reference_graph ()
103104{
@@ -169,4 +170,4 @@ int main()
169170 test_BFS<T>();
170171
171172 return 0 ;
172- }
173+ }
0 commit comments