Skip to content

Commit 8ef8406

Browse files
v0.6
1 parent 85ce78d commit 8ef8406

File tree

4 files changed

+70
-61
lines changed

4 files changed

+70
-61
lines changed

Lesson05/Activity02/welsh_powell_solution.cpp

+24-19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <algorithm>
77
#include <iostream>
88

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

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

49-
void add_edge(Edge<T>& e)
69+
void add_edge(Edge<T>&& e)
5070
{
5171
// Check if the source and destination vertices are within range
5272
if (e.src >= 1 && e.src <= V &&
@@ -70,30 +90,14 @@ class Graph
7090

7191
// Overloads the << operator so a graph be written directly to a stream
7292
// 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);
93+
// template <typename T>
94+
friend std::ostream& operator<< <>(std::ostream& os, const Graph<T>& G);
7595

7696
private:
7797
size_t V; // Stores number of vertices in graph
7898
std::vector<Edge<T>> edge_list;
7999
};
80100

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-
}
97101

98102

99103
// Initialize the colors that will be used to color the vertices
@@ -196,4 +200,5 @@ int main()
196200
auto colors = welsh_powell_coloring<T>(G);
197201
std::cout << "Vertex Colors: " << std::endl;
198202
print_colors(colors);
203+
return 0;
199204
}

Lesson05/Exercise02/fractional_knapsack.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void test_fractional_knapsack(unsigned num_objects, unsigned knapsack_capacity)
109109
auto weight = uniform_dist(rand);
110110
auto value = uniform_dist(rand);
111111
auto obj = Object<weight_type, value_type, fractional_type> {
112-
weight,
112+
static_cast<weight_type>(weight),
113113
static_cast<value_type>(value),
114114
static_cast<fractional_type>(value) / weight
115115
};

Lesson05/Exercise03/kruskal.cpp

+23-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include<queue>
55
#include<map>
66

7+
template <typename T> class Graph;
8+
79
template<typename T>
810
class SimpleDisjointSet
911
{
@@ -105,6 +107,23 @@ struct Edge
105107
}
106108
};
107109

110+
template <typename T>
111+
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
112+
{
113+
for (auto i = 1; i < G.vertices(); i++)
114+
{
115+
os << i <<":\t";
116+
117+
auto edges = G.edges(i);
118+
for (auto& e : edges)
119+
os << "{" << e.dest << ": " << e.weight << "}, ";
120+
121+
os << std::endl;
122+
}
123+
124+
return os;
125+
}
126+
108127
template<typename T>
109128
class Graph
110129
{
@@ -125,7 +144,7 @@ class Graph
125144
return edge_list;
126145
}
127146

128-
void add_edge(Edge<T>& e)
147+
void add_edge(Edge<T>&& e)
129148
{
130149
// Check if the source and destination vertices are within range
131150
if (e.src >= 1 && e.src <= V &&
@@ -149,30 +168,15 @@ class Graph
149168

150169
// Overloads the << operator so a graph be written directly to a stream
151170
// Can be used as std::cout << obj << std::endl;
152-
template <typename T>
153-
friend std::ostream& operator<<(std::ostream& os, const Graph<T>& G);
171+
// template <typename T>
172+
friend std::ostream& operator<< <>(std::ostream& os, const Graph<T>& G);
154173

155174
private:
156175
size_t V; // Stores number of vertices in graph
157176
std::vector<Edge<T>> edge_list;
158177
};
159178

160-
template <typename T>
161-
std::ostream& operator<<(std::ostream& os, const Graph<T>& G)
162-
{
163-
for (auto i = 1; i < G.vertices(); i++)
164-
{
165-
os << i <<":\t";
166179

167-
auto edges = G.edges(i);
168-
for (auto& e : edges)
169-
os << "{" << e.dest << ": " << e.weight << "}, ";
170-
171-
os << std::endl;
172-
}
173-
174-
return os;
175-
}
176180

177181

178182
// Since a tree is also a graph, we can reuse the Graph class
@@ -208,7 +212,7 @@ Graph<T> minimum_spanning_tree(const Graph<T>& G)
208212
// in the MST
209213
if (dset.find(e.src) != dset.find(e.dest))
210214
{
211-
MST.add_edge(e);
215+
MST.add_edge(Edge <T>{e.src, e.dest, e.weight});
212216
dset.union_sets(e.src, e.dest);
213217
}
214218
}

Lesson05/Exercise04/graph_coloring.cpp

+22-22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <vector>
66
#include <iostream>
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,33 +88,14 @@ 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+
// template <typename T>
92+
friend std::ostream& operator<< <> (std::ostream& os, const Graph<T>& G);
7493

7594
private:
7695
size_t V; // Stores number of vertices in graph
7796
std::vector<Edge<T>> edge_list;
7897
};
7998

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-
97-
98-
9999
// Initialize the colors that will be used to color the vertices
100100
std::unordered_map<size_t, std::string> color_map = {
101101
{1, "Red"},

0 commit comments

Comments
 (0)