Skip to content

Commit aa76662

Browse files
v0.6
1 parent a0ed3ba commit aa76662

File tree

6 files changed

+130
-105
lines changed

6 files changed

+130
-105
lines changed

Lesson06/Activity01/bipartite_check.cpp

+21-20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <map>
66
#include <stack>
77

8+
template<typename T> class Graph;
9+
810
template<typename T>
911
struct Edge
1012
{
@@ -25,6 +27,23 @@ struct Edge
2527
}
2628
};
2729

30+
template <typename T>
31+
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
32+
{
33+
for (auto i = 1; i < G.vertices(); i++)
34+
{
35+
os << i << ":\t";
36+
37+
auto edges = G.outgoing_edges(i);
38+
for (auto& e : edges)
39+
os << "{" << e.dest << ": " << e.weight << "}, ";
40+
41+
os << std::endl;
42+
}
43+
44+
return os;
45+
}
46+
2847
template<typename T>
2948
class Graph
3049
{
@@ -45,7 +64,7 @@ class Graph
4564
return edge_list;
4665
}
4766

48-
void add_edge(Edge<T>& e)
67+
void add_edge(Edge<T>&& e)
4968
{
5069
// Check if the source and destination vertices are within range
5170
if (e.src >= 1 && e.src <= V &&
@@ -69,31 +88,13 @@ class Graph
6988

7089
// Overloads the << operator so a graph be written directly to a stream
7190
// Can be used as std::cout << obj << std::endl;
72-
template <typename T>
73-
friend std::ostream& operator<<(std::ostream& os, const Graph<T>& G);
91+
friend std::ostream& operator<< <>(std::ostream& os, const Graph<T>& G);
7492

7593
private:
7694
size_t V; // Stores number of vertices in graph
7795
std::vector<Edge<T>> edge_list;
7896
};
7997

80-
template <typename T>
81-
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
82-
{
83-
for (auto i = 1; i < G.vertices(); i++)
84-
{
85-
os << i << ":\t";
86-
87-
auto edges = G.outgoing_edges(i);
88-
for (auto& e : edges)
89-
os << "{" << e.dest << ": " << e.weight << "}, ";
90-
91-
os << std::endl;
92-
}
93-
94-
return os;
95-
}
96-
9798
template <typename T>
9899
auto create_bipartite_reference_graph()
99100
{

Lesson06/Activity02/nyshortest_path.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <queue>
88
#include <fstream>
99
#include <sstream>
10+
#include <algorithm>
11+
12+
template<typename T> class Graph;
1013

1114
template<typename T>
1215
struct Edge
@@ -28,6 +31,23 @@ struct Edge
2831
}
2932
};
3033

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+
3151
template<typename T>
3252
class Graph
3353
{
@@ -48,7 +68,7 @@ class Graph
4868
return edge_list;
4969
}
5070

51-
void add_edge(Edge<T>& e)
71+
void add_edge(Edge<T>&& e)
5272
{
5373
// Check if the source and destination vertices are within range
5474
if (e.src >= 1 && e.src <= V &&
@@ -72,8 +92,7 @@ class Graph
7292

7393
// Overloads the << operator so a graph be written directly to a stream
7494
// Can be used as std::cout << obj << std::endl;
75-
template <typename T>
76-
friend std::ostream& operator<<(std::ostream& os, const Graph<T>& G);
95+
friend std::ostream& operator<< <>(std::ostream& os, const Graph<T>& G);
7796

7897
private:
7998
size_t V; // Stores number of vertices in graph

Lesson06/Exercise01/bfs.cpp

+22-21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// BFS
1010
#include<queue>
1111

12+
template<typename T> class Graph;
13+
1214
template<typename T>
1315
struct 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+
3251
template<typename T>
3352
class 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

7997
private:
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-
101102
template <typename T>
102103
auto create_reference_graph()
103104
{
@@ -169,4 +170,4 @@ int main()
169170
test_BFS<T>();
170171

171172
return 0;
172-
}
173+
}

Lesson06/Exercise02/dfs.cpp

+21-20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <map>
66
#include <stack>
77

8+
template<typename T> class Graph;
9+
810
template<typename T>
911
struct Edge
1012
{
@@ -25,6 +27,23 @@ struct Edge
2527
}
2628
};
2729

30+
template <typename T>
31+
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
32+
{
33+
for (auto i = 1; i < G.vertices(); i++)
34+
{
35+
os << i << ":\t";
36+
37+
auto edges = G.outgoing_edges(i);
38+
for (auto& e : edges)
39+
os << "{" << e.dest << ": " << e.weight << "}, ";
40+
41+
os << std::endl;
42+
}
43+
44+
return os;
45+
}
46+
2847
template<typename T>
2948
class Graph
3049
{
@@ -45,7 +64,7 @@ class Graph
4564
return edge_list;
4665
}
4766

48-
void add_edge(Edge<T>& e)
67+
void add_edge(Edge<T>&& e)
4968
{
5069
// Check if the source and destination vertices are within range
5170
if (e.src >= 1 && e.src <= V &&
@@ -69,31 +88,13 @@ class Graph
6988

7089
// Overloads the << operator so a graph be written directly to a stream
7190
// Can be used as std::cout << obj << std::endl;
72-
template <typename T>
73-
friend std::ostream& operator<<(std::ostream& os, const Graph<T>& G);
91+
friend std::ostream& operator<< <>(std::ostream& os, const Graph<T>& G);
7492

7593
private:
7694
size_t V; // Stores number of vertices in graph
7795
std::vector<Edge<T>> edge_list;
7896
};
7997

80-
template <typename T>
81-
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
82-
{
83-
for (auto i = 1; i < G.vertices(); i++)
84-
{
85-
os << i << ":\t";
86-
87-
auto edges = G.outgoing_edges(i);
88-
for (auto& e : edges)
89-
os << "{" << e.dest << ": " << e.weight << "}, ";
90-
91-
os << std::endl;
92-
}
93-
94-
return os;
95-
}
96-
9798
template <typename T>
9899
auto create_reference_graph()
99100
{

Lesson06/Exercise03/prim.cpp

+21-20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <vector>
77
#include <iostream>
88

9+
template<typename T> class Graph;
10+
911
template<typename T>
1012
struct Edge
1113
{
@@ -26,6 +28,23 @@ struct Edge
2628
}
2729
};
2830

31+
template <typename T>
32+
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
33+
{
34+
for (auto i = 1; i < G.vertices(); i++)
35+
{
36+
os << i << ":\t";
37+
38+
auto edges = G.outgoing_edges(i);
39+
for (auto& e : edges)
40+
os << "{" << e.dest << ": " << e.weight << "}, ";
41+
42+
os << std::endl;
43+
}
44+
45+
return os;
46+
}
47+
2948
template<typename T>
3049
class Graph
3150
{
@@ -46,7 +65,7 @@ class Graph
4665
return edge_list;
4766
}
4867

49-
void add_edge(Edge<T>& e)
68+
void add_edge(Edge<T>&& e)
5069
{
5170
// Check if the source and destination vertices are within range
5271
if (e.src >= 1 && e.src <= V &&
@@ -70,31 +89,13 @@ class Graph
7089

7190
// Overloads the << operator so a graph be written directly to a stream
7291
// Can be used as std::cout << obj << std::endl;
73-
template <typename T>
74-
friend std::ostream& operator<<(std::ostream& os, const Graph<T>& G);
92+
friend std::ostream& operator<< <>(std::ostream& os, const Graph<T>& G);
7593

7694
private:
7795
size_t V; // Stores number of vertices in graph
7896
std::vector<Edge<T>> edge_list;
7997
};
8098

81-
template <typename T>
82-
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
83-
{
84-
for (auto i = 1; i < G.vertices(); i++)
85-
{
86-
os << i << ":\t";
87-
88-
auto edges = G.outgoing_edges(i);
89-
for (auto& e : edges)
90-
os << "{" << e.dest << ": " << e.weight << "}, ";
91-
92-
os << std::endl;
93-
}
94-
95-
return os;
96-
}
97-
9899
template <typename T>
99100
auto create_reference_graph()
100101
{

0 commit comments

Comments
 (0)