C++ graph classunw_graph class (unweighted graph)Review/rate my new graph classImplementing graph searchGraph vertex class implementation with adjacency listsImplementing Directed and Undirected Graph in C++C++ Graph Class Implementation (adjacency list)Path Finding using Dynamic ProgrammingGraph represented by adjacency listJava Node class for a GraphGraph implementation from adjacency list
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
Today is the Center
Why is an old chain unsafe?
When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?
Why Is Death Allowed In the Matrix?
How is this relation reflexive?
I probably found a bug with the sudo apt install function
Has the BBC provided arguments for saying Brexit being cancelled is unlikely?
The Meaning of "Simply a child of her times"
How old can references or sources in a thesis be?
Why not use SQL instead of GraphQL?
declaring a variable twice in IIFE
How can bays and straits be determined in a procedurally generated map?
If Manufacturer spice model and Datasheet give different values which should I use?
Why has Russell's definition of numbers using equivalence classes been finally abandoned? ( If it has actually been abandoned).
GPS Rollover on Android Smartphones
Possibly bubble sort algorithm
Why was the small council so happy for Tyrion to become the Master of Coin?
Japan - Plan around max visa duration
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
How do I create uniquely male characters?
"You are your self first supporter", a more proper way to say it
Email Account under attack (really) - anything I can do?
Infinite past with a beginning?
C++ graph class
unw_graph class (unweighted graph)Review/rate my new graph classImplementing graph searchGraph vertex class implementation with adjacency listsImplementing Directed and Undirected Graph in C++C++ Graph Class Implementation (adjacency list)Path Finding using Dynamic ProgrammingGraph represented by adjacency listJava Node class for a GraphGraph implementation from adjacency list
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
What can be done better? My future plans is to implement algorithms such as: DFS, BFS, Prims, Dijkstra, Kruskal, ...
Faults I know of:
- Not implementing rule of five
Vertex.h:
#pragma once
#include <iostream>
#include <unordered_map>
template<typename T>
class Vertex
public:
Vertex(const T& value);
~Vertex();
const T& value() const;
void add_edge_in(Vertex<T>* dest, int weight);
void add_edge_out(Vertex<T>* dest, int weight);
void remove_edge_in(Vertex<T>* dest);
void remove_edge_out(Vertex<T>* dest);
private:
template<typename T> friend std::ostream& operator<<(std::ostream& out, const Vertex<T>& rhs);
const T value_;
std::unordered_map<Vertex<T>*, int> edges_in_;
std::unordered_map<Vertex<T>*, int> edges_out_;
;
template<typename T>
Vertex<T>::Vertex(const T& value)
: value_(value)
/*
Removes all edges associated with this vertex
*/
template<typename T>
Vertex<T>::~Vertex<T>()
for (auto edge : edges_in_)
Vertex<T>* dest = edge.first;
dest->remove_edge_in(this);
for (auto edge : edges_out_)
Vertex<T>* dest = edge.first;
dest->remove_edge_out(this);
template<typename T>
const T& Vertex<T>::value() const
return value_;
template<typename T>
void Vertex<T>::add_edge_in(Vertex<T>* dest, int weight)
edges_in_.insert( dest, weight );
template<typename T>
void Vertex<T>::add_edge_out(Vertex<T>* dest, int weight)
edges_out_.insert( dest, weight );
template<typename T>
void Vertex<T>::remove_edge_in(Vertex<T>* dest)
edges_in_.erase(dest);
template<typename T>
void Vertex<T>::remove_edge_out(Vertex<T>* dest)
edges_out_.erase(dest);
template<typename T>
std::ostream& operator<<(std::ostream& out, const Vertex<T>& rhs)
out << "[" << rhs.value_ << "]:";
out << "rn";
out << "rnt EDGES_IN = ";
for (auto edge : rhs.edges_in_)
out << " (" << edge.first->value_ << ", " << edge.second << ")";
out << " ,";
out << "rnt EDGES_OUT = ";
for (auto edge : rhs.edges_out_)
out << " (" << edge.first->value_ << ", " << edge.second << ")";
out << " ";
out << "rn;";
return out;
Graph.h:
#pragma once
#include "Vertex.h"
#include <iostream>
#include <vector>
template<typename T>
class Graph
public:
Graph(const bool directed);
~Graph();
void print() const;
bool is_directed() const;
void add_vertex(const T&);
void remove_vertex(const T&);
void add_edge(const T&, const T&, int weight);
void remove_edge(const T&, const T&);
private:
Vertex<T>* get_vertex(const T&);
std::vector<Vertex<T>*> graph_;
const bool directed_;
;
template<typename T>
Graph<T>::Graph(const bool directed)
: directed_(directed)
template<typename T>
Graph<T>::~Graph()
for (Vertex<T>* v : graph_)
delete v;
template<typename T>
void Graph<T>::print() const
for (Vertex<T>* v : graph_)
std::cout << *v << std::endl;
template<typename T>
bool Graph<T>::is_directed() const
return directed_;
template<typename T>
void Graph<T>::add_vertex(const T& value)
graph_.push_back(new Vertex<T>(value));
return;
template<typename T>
void Graph<T>::remove_vertex(const T& value)
for (auto it = graph_.begin(); it != graph_.end(); ++it)
if ((*it)->value() == value)
delete *it;
graph_.erase(it);
return;
template<typename T>
void Graph<T>::add_edge(const T& src_value, const T& dest_value, int weight)
Vertex<T>* src = get_vertex(src_value);
Vertex<T>* dest = get_vertex(dest_value);
src->add_edge_out(dest, weight);
dest->add_edge_in(src, weight);
if (!directed_)
src->add_edge_in(dest, weight);
dest->add_edge_out(src, weight);
template<typename T>
void Graph<T>::remove_edge(const T& src_value, const T& dest_value)
Vertex<T>* src = get_vertex(src_value);
Vertex<T>* dest = get_vertex(dest_value);
src->remove_edge_out(dest);
dest->remove_edge_in(src);
if (!directed_)
src->remove_edge_in(dest);
dest->remove_edge_out(src);
template<typename T>
Vertex<T>* Graph<T>::get_vertex(const T& value)
for (Vertex<T>* v : graph_)
if (v->value() == value)
return v;
return nullptr;
c++ beginner graph
$endgroup$
add a comment |
$begingroup$
What can be done better? My future plans is to implement algorithms such as: DFS, BFS, Prims, Dijkstra, Kruskal, ...
Faults I know of:
- Not implementing rule of five
Vertex.h:
#pragma once
#include <iostream>
#include <unordered_map>
template<typename T>
class Vertex
public:
Vertex(const T& value);
~Vertex();
const T& value() const;
void add_edge_in(Vertex<T>* dest, int weight);
void add_edge_out(Vertex<T>* dest, int weight);
void remove_edge_in(Vertex<T>* dest);
void remove_edge_out(Vertex<T>* dest);
private:
template<typename T> friend std::ostream& operator<<(std::ostream& out, const Vertex<T>& rhs);
const T value_;
std::unordered_map<Vertex<T>*, int> edges_in_;
std::unordered_map<Vertex<T>*, int> edges_out_;
;
template<typename T>
Vertex<T>::Vertex(const T& value)
: value_(value)
/*
Removes all edges associated with this vertex
*/
template<typename T>
Vertex<T>::~Vertex<T>()
for (auto edge : edges_in_)
Vertex<T>* dest = edge.first;
dest->remove_edge_in(this);
for (auto edge : edges_out_)
Vertex<T>* dest = edge.first;
dest->remove_edge_out(this);
template<typename T>
const T& Vertex<T>::value() const
return value_;
template<typename T>
void Vertex<T>::add_edge_in(Vertex<T>* dest, int weight)
edges_in_.insert( dest, weight );
template<typename T>
void Vertex<T>::add_edge_out(Vertex<T>* dest, int weight)
edges_out_.insert( dest, weight );
template<typename T>
void Vertex<T>::remove_edge_in(Vertex<T>* dest)
edges_in_.erase(dest);
template<typename T>
void Vertex<T>::remove_edge_out(Vertex<T>* dest)
edges_out_.erase(dest);
template<typename T>
std::ostream& operator<<(std::ostream& out, const Vertex<T>& rhs)
out << "[" << rhs.value_ << "]:";
out << "rn";
out << "rnt EDGES_IN = ";
for (auto edge : rhs.edges_in_)
out << " (" << edge.first->value_ << ", " << edge.second << ")";
out << " ,";
out << "rnt EDGES_OUT = ";
for (auto edge : rhs.edges_out_)
out << " (" << edge.first->value_ << ", " << edge.second << ")";
out << " ";
out << "rn;";
return out;
Graph.h:
#pragma once
#include "Vertex.h"
#include <iostream>
#include <vector>
template<typename T>
class Graph
public:
Graph(const bool directed);
~Graph();
void print() const;
bool is_directed() const;
void add_vertex(const T&);
void remove_vertex(const T&);
void add_edge(const T&, const T&, int weight);
void remove_edge(const T&, const T&);
private:
Vertex<T>* get_vertex(const T&);
std::vector<Vertex<T>*> graph_;
const bool directed_;
;
template<typename T>
Graph<T>::Graph(const bool directed)
: directed_(directed)
template<typename T>
Graph<T>::~Graph()
for (Vertex<T>* v : graph_)
delete v;
template<typename T>
void Graph<T>::print() const
for (Vertex<T>* v : graph_)
std::cout << *v << std::endl;
template<typename T>
bool Graph<T>::is_directed() const
return directed_;
template<typename T>
void Graph<T>::add_vertex(const T& value)
graph_.push_back(new Vertex<T>(value));
return;
template<typename T>
void Graph<T>::remove_vertex(const T& value)
for (auto it = graph_.begin(); it != graph_.end(); ++it)
if ((*it)->value() == value)
delete *it;
graph_.erase(it);
return;
template<typename T>
void Graph<T>::add_edge(const T& src_value, const T& dest_value, int weight)
Vertex<T>* src = get_vertex(src_value);
Vertex<T>* dest = get_vertex(dest_value);
src->add_edge_out(dest, weight);
dest->add_edge_in(src, weight);
if (!directed_)
src->add_edge_in(dest, weight);
dest->add_edge_out(src, weight);
template<typename T>
void Graph<T>::remove_edge(const T& src_value, const T& dest_value)
Vertex<T>* src = get_vertex(src_value);
Vertex<T>* dest = get_vertex(dest_value);
src->remove_edge_out(dest);
dest->remove_edge_in(src);
if (!directed_)
src->remove_edge_in(dest);
dest->remove_edge_out(src);
template<typename T>
Vertex<T>* Graph<T>::get_vertex(const T& value)
for (Vertex<T>* v : graph_)
if (v->value() == value)
return v;
return nullptr;
c++ beginner graph
$endgroup$
add a comment |
$begingroup$
What can be done better? My future plans is to implement algorithms such as: DFS, BFS, Prims, Dijkstra, Kruskal, ...
Faults I know of:
- Not implementing rule of five
Vertex.h:
#pragma once
#include <iostream>
#include <unordered_map>
template<typename T>
class Vertex
public:
Vertex(const T& value);
~Vertex();
const T& value() const;
void add_edge_in(Vertex<T>* dest, int weight);
void add_edge_out(Vertex<T>* dest, int weight);
void remove_edge_in(Vertex<T>* dest);
void remove_edge_out(Vertex<T>* dest);
private:
template<typename T> friend std::ostream& operator<<(std::ostream& out, const Vertex<T>& rhs);
const T value_;
std::unordered_map<Vertex<T>*, int> edges_in_;
std::unordered_map<Vertex<T>*, int> edges_out_;
;
template<typename T>
Vertex<T>::Vertex(const T& value)
: value_(value)
/*
Removes all edges associated with this vertex
*/
template<typename T>
Vertex<T>::~Vertex<T>()
for (auto edge : edges_in_)
Vertex<T>* dest = edge.first;
dest->remove_edge_in(this);
for (auto edge : edges_out_)
Vertex<T>* dest = edge.first;
dest->remove_edge_out(this);
template<typename T>
const T& Vertex<T>::value() const
return value_;
template<typename T>
void Vertex<T>::add_edge_in(Vertex<T>* dest, int weight)
edges_in_.insert( dest, weight );
template<typename T>
void Vertex<T>::add_edge_out(Vertex<T>* dest, int weight)
edges_out_.insert( dest, weight );
template<typename T>
void Vertex<T>::remove_edge_in(Vertex<T>* dest)
edges_in_.erase(dest);
template<typename T>
void Vertex<T>::remove_edge_out(Vertex<T>* dest)
edges_out_.erase(dest);
template<typename T>
std::ostream& operator<<(std::ostream& out, const Vertex<T>& rhs)
out << "[" << rhs.value_ << "]:";
out << "rn";
out << "rnt EDGES_IN = ";
for (auto edge : rhs.edges_in_)
out << " (" << edge.first->value_ << ", " << edge.second << ")";
out << " ,";
out << "rnt EDGES_OUT = ";
for (auto edge : rhs.edges_out_)
out << " (" << edge.first->value_ << ", " << edge.second << ")";
out << " ";
out << "rn;";
return out;
Graph.h:
#pragma once
#include "Vertex.h"
#include <iostream>
#include <vector>
template<typename T>
class Graph
public:
Graph(const bool directed);
~Graph();
void print() const;
bool is_directed() const;
void add_vertex(const T&);
void remove_vertex(const T&);
void add_edge(const T&, const T&, int weight);
void remove_edge(const T&, const T&);
private:
Vertex<T>* get_vertex(const T&);
std::vector<Vertex<T>*> graph_;
const bool directed_;
;
template<typename T>
Graph<T>::Graph(const bool directed)
: directed_(directed)
template<typename T>
Graph<T>::~Graph()
for (Vertex<T>* v : graph_)
delete v;
template<typename T>
void Graph<T>::print() const
for (Vertex<T>* v : graph_)
std::cout << *v << std::endl;
template<typename T>
bool Graph<T>::is_directed() const
return directed_;
template<typename T>
void Graph<T>::add_vertex(const T& value)
graph_.push_back(new Vertex<T>(value));
return;
template<typename T>
void Graph<T>::remove_vertex(const T& value)
for (auto it = graph_.begin(); it != graph_.end(); ++it)
if ((*it)->value() == value)
delete *it;
graph_.erase(it);
return;
template<typename T>
void Graph<T>::add_edge(const T& src_value, const T& dest_value, int weight)
Vertex<T>* src = get_vertex(src_value);
Vertex<T>* dest = get_vertex(dest_value);
src->add_edge_out(dest, weight);
dest->add_edge_in(src, weight);
if (!directed_)
src->add_edge_in(dest, weight);
dest->add_edge_out(src, weight);
template<typename T>
void Graph<T>::remove_edge(const T& src_value, const T& dest_value)
Vertex<T>* src = get_vertex(src_value);
Vertex<T>* dest = get_vertex(dest_value);
src->remove_edge_out(dest);
dest->remove_edge_in(src);
if (!directed_)
src->remove_edge_in(dest);
dest->remove_edge_out(src);
template<typename T>
Vertex<T>* Graph<T>::get_vertex(const T& value)
for (Vertex<T>* v : graph_)
if (v->value() == value)
return v;
return nullptr;
c++ beginner graph
$endgroup$
What can be done better? My future plans is to implement algorithms such as: DFS, BFS, Prims, Dijkstra, Kruskal, ...
Faults I know of:
- Not implementing rule of five
Vertex.h:
#pragma once
#include <iostream>
#include <unordered_map>
template<typename T>
class Vertex
public:
Vertex(const T& value);
~Vertex();
const T& value() const;
void add_edge_in(Vertex<T>* dest, int weight);
void add_edge_out(Vertex<T>* dest, int weight);
void remove_edge_in(Vertex<T>* dest);
void remove_edge_out(Vertex<T>* dest);
private:
template<typename T> friend std::ostream& operator<<(std::ostream& out, const Vertex<T>& rhs);
const T value_;
std::unordered_map<Vertex<T>*, int> edges_in_;
std::unordered_map<Vertex<T>*, int> edges_out_;
;
template<typename T>
Vertex<T>::Vertex(const T& value)
: value_(value)
/*
Removes all edges associated with this vertex
*/
template<typename T>
Vertex<T>::~Vertex<T>()
for (auto edge : edges_in_)
Vertex<T>* dest = edge.first;
dest->remove_edge_in(this);
for (auto edge : edges_out_)
Vertex<T>* dest = edge.first;
dest->remove_edge_out(this);
template<typename T>
const T& Vertex<T>::value() const
return value_;
template<typename T>
void Vertex<T>::add_edge_in(Vertex<T>* dest, int weight)
edges_in_.insert( dest, weight );
template<typename T>
void Vertex<T>::add_edge_out(Vertex<T>* dest, int weight)
edges_out_.insert( dest, weight );
template<typename T>
void Vertex<T>::remove_edge_in(Vertex<T>* dest)
edges_in_.erase(dest);
template<typename T>
void Vertex<T>::remove_edge_out(Vertex<T>* dest)
edges_out_.erase(dest);
template<typename T>
std::ostream& operator<<(std::ostream& out, const Vertex<T>& rhs)
out << "[" << rhs.value_ << "]:";
out << "rn";
out << "rnt EDGES_IN = ";
for (auto edge : rhs.edges_in_)
out << " (" << edge.first->value_ << ", " << edge.second << ")";
out << " ,";
out << "rnt EDGES_OUT = ";
for (auto edge : rhs.edges_out_)
out << " (" << edge.first->value_ << ", " << edge.second << ")";
out << " ";
out << "rn;";
return out;
Graph.h:
#pragma once
#include "Vertex.h"
#include <iostream>
#include <vector>
template<typename T>
class Graph
public:
Graph(const bool directed);
~Graph();
void print() const;
bool is_directed() const;
void add_vertex(const T&);
void remove_vertex(const T&);
void add_edge(const T&, const T&, int weight);
void remove_edge(const T&, const T&);
private:
Vertex<T>* get_vertex(const T&);
std::vector<Vertex<T>*> graph_;
const bool directed_;
;
template<typename T>
Graph<T>::Graph(const bool directed)
: directed_(directed)
template<typename T>
Graph<T>::~Graph()
for (Vertex<T>* v : graph_)
delete v;
template<typename T>
void Graph<T>::print() const
for (Vertex<T>* v : graph_)
std::cout << *v << std::endl;
template<typename T>
bool Graph<T>::is_directed() const
return directed_;
template<typename T>
void Graph<T>::add_vertex(const T& value)
graph_.push_back(new Vertex<T>(value));
return;
template<typename T>
void Graph<T>::remove_vertex(const T& value)
for (auto it = graph_.begin(); it != graph_.end(); ++it)
if ((*it)->value() == value)
delete *it;
graph_.erase(it);
return;
template<typename T>
void Graph<T>::add_edge(const T& src_value, const T& dest_value, int weight)
Vertex<T>* src = get_vertex(src_value);
Vertex<T>* dest = get_vertex(dest_value);
src->add_edge_out(dest, weight);
dest->add_edge_in(src, weight);
if (!directed_)
src->add_edge_in(dest, weight);
dest->add_edge_out(src, weight);
template<typename T>
void Graph<T>::remove_edge(const T& src_value, const T& dest_value)
Vertex<T>* src = get_vertex(src_value);
Vertex<T>* dest = get_vertex(dest_value);
src->remove_edge_out(dest);
dest->remove_edge_in(src);
if (!directed_)
src->remove_edge_in(dest);
dest->remove_edge_out(src);
template<typename T>
Vertex<T>* Graph<T>::get_vertex(const T& value)
for (Vertex<T>* v : graph_)
if (v->value() == value)
return v;
return nullptr;
c++ beginner graph
c++ beginner graph
asked 2 mins ago
user644361user644361
774
774
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217032%2fc-graph-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217032%2fc-graph-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown