How to represent a graph with dummy vertices using an adjacency list?
This graph contains dummy vertices. How to store the state information of the vertices using an adjacency list? The outgoing edges should be stored for each vertex.
I used simple adjacency list. But here, for example, v14 is having two different set of outgoing edges (one with no outgoing edge and another one with two outgoing). What data structure should I use to represent such dummy nodes.
c++ graph adjacency-matrix adjacency-list
|
show 3 more comments
This graph contains dummy vertices. How to store the state information of the vertices using an adjacency list? The outgoing edges should be stored for each vertex.
I used simple adjacency list. But here, for example, v14 is having two different set of outgoing edges (one with no outgoing edge and another one with two outgoing). What data structure should I use to represent such dummy nodes.
c++ graph adjacency-matrix adjacency-list
Only the graph is to be stored. Right? Are P1 and P2 relevant to you?
– Kunal Puri
Jan 20 at 7:08
Only the graph need to be stored. P1 and P2 are not relevant
– ptpkueqf
Jan 20 at 7:09
Can you replace the hyperlink by the content it refers to?
– JVApen
Jan 20 at 7:09
@ptpkueqf See this: theoryofprogramming.com/2018/01/14/… .This is in Java. But you will get the idea.
– Kunal Puri
Jan 20 at 7:11
1
@ptpkueqf I think you're going to have to explain what you mean by dummy vertices. I can see the graph above is a bit unusual, but exactly what a dummy vertex is, and how you want to process them is a bit unclear.
– john
Jan 20 at 7:32
|
show 3 more comments
This graph contains dummy vertices. How to store the state information of the vertices using an adjacency list? The outgoing edges should be stored for each vertex.
I used simple adjacency list. But here, for example, v14 is having two different set of outgoing edges (one with no outgoing edge and another one with two outgoing). What data structure should I use to represent such dummy nodes.
c++ graph adjacency-matrix adjacency-list
This graph contains dummy vertices. How to store the state information of the vertices using an adjacency list? The outgoing edges should be stored for each vertex.
I used simple adjacency list. But here, for example, v14 is having two different set of outgoing edges (one with no outgoing edge and another one with two outgoing). What data structure should I use to represent such dummy nodes.
c++ graph adjacency-matrix adjacency-list
c++ graph adjacency-matrix adjacency-list
edited Jan 20 at 7:19
ptpkueqf
asked Jan 20 at 7:04
ptpkueqfptpkueqf
63
63
Only the graph is to be stored. Right? Are P1 and P2 relevant to you?
– Kunal Puri
Jan 20 at 7:08
Only the graph need to be stored. P1 and P2 are not relevant
– ptpkueqf
Jan 20 at 7:09
Can you replace the hyperlink by the content it refers to?
– JVApen
Jan 20 at 7:09
@ptpkueqf See this: theoryofprogramming.com/2018/01/14/… .This is in Java. But you will get the idea.
– Kunal Puri
Jan 20 at 7:11
1
@ptpkueqf I think you're going to have to explain what you mean by dummy vertices. I can see the graph above is a bit unusual, but exactly what a dummy vertex is, and how you want to process them is a bit unclear.
– john
Jan 20 at 7:32
|
show 3 more comments
Only the graph is to be stored. Right? Are P1 and P2 relevant to you?
– Kunal Puri
Jan 20 at 7:08
Only the graph need to be stored. P1 and P2 are not relevant
– ptpkueqf
Jan 20 at 7:09
Can you replace the hyperlink by the content it refers to?
– JVApen
Jan 20 at 7:09
@ptpkueqf See this: theoryofprogramming.com/2018/01/14/… .This is in Java. But you will get the idea.
– Kunal Puri
Jan 20 at 7:11
1
@ptpkueqf I think you're going to have to explain what you mean by dummy vertices. I can see the graph above is a bit unusual, but exactly what a dummy vertex is, and how you want to process them is a bit unclear.
– john
Jan 20 at 7:32
Only the graph is to be stored. Right? Are P1 and P2 relevant to you?
– Kunal Puri
Jan 20 at 7:08
Only the graph is to be stored. Right? Are P1 and P2 relevant to you?
– Kunal Puri
Jan 20 at 7:08
Only the graph need to be stored. P1 and P2 are not relevant
– ptpkueqf
Jan 20 at 7:09
Only the graph need to be stored. P1 and P2 are not relevant
– ptpkueqf
Jan 20 at 7:09
Can you replace the hyperlink by the content it refers to?
– JVApen
Jan 20 at 7:09
Can you replace the hyperlink by the content it refers to?
– JVApen
Jan 20 at 7:09
@ptpkueqf See this: theoryofprogramming.com/2018/01/14/… .This is in Java. But you will get the idea.
– Kunal Puri
Jan 20 at 7:11
@ptpkueqf See this: theoryofprogramming.com/2018/01/14/… .This is in Java. But you will get the idea.
– Kunal Puri
Jan 20 at 7:11
1
1
@ptpkueqf I think you're going to have to explain what you mean by dummy vertices. I can see the graph above is a bit unusual, but exactly what a dummy vertex is, and how you want to process them is a bit unclear.
– john
Jan 20 at 7:32
@ptpkueqf I think you're going to have to explain what you mean by dummy vertices. I can see the graph above is a bit unusual, but exactly what a dummy vertex is, and how you want to process them is a bit unclear.
– john
Jan 20 at 7:32
|
show 3 more comments
1 Answer
1
active
oldest
votes
Since you don't have constraints on implementation, a simple class would do. Here, we store (shared) pointers to children in a vector. (Overloaded) addChild
methods return a reference to the added child, so it is easier to chain together addChild
s. Operator overloading used is nice to have, but not necessary, and you may remove them if desired. Here's the code:
#include <utility>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using std::vector;
using std::string;
using std::size_t;
using std::shared_ptr;
using std::make_shared;
using std::ostream;
using std::cout;
using std::endl;
class Node {
public:
using child_ptr_type = shared_ptr<Node>;
using contaner_type = vector<child_ptr_type >;
explicit Node(string lab = "Default", contaner_type ch = {})
: label(std::move(lab))
, children(std::move(ch))
{}
const string &getLabel() const
{ return label; }
void setLabel(const string &label)
{ Node::label = label; }
const contaner_type &getChildren() const
{ return children; }
const Node& getChild(size_t indx) const
{ return *children[indx]; }
Node& getChild(size_t indx)
{ return *children[indx]; }
Node& addChild(const string& lab = "Default", const contaner_type & ch = {})
{
children.push_back(make_shared<Node>(lab, ch));
return *children.back();
}
Node& addChild(const child_ptr_type &child)
{
children.push_back(child);
return *children.back();
}
Node& addChild(const Node& node)
{
children.push_back(make_shared<Node>(node));
return *children.back();
}
friend ostream& operator<<(ostream& os, const Node &node)
{
node.print(os);
return os;
}
Node&operator(size_t indx)
{
return getChild(indx);
}
const Node&operator(size_t indx) const
{
return getChild(indx);
}
private:
string label;
contaner_type children;
void print(ostream& os, size_t level = 0) const
{
for (size_t i = 0; i != level; ++i) {
os << "|----";
}
os << label << 'n';
for (const auto& child : children) {
child->print(os, level + 1);
}
}
};
int main()
{
Node V1("V1");
V1.addChild("V2").addChild("V5").addChild("V7").addChild("V10");
V1[0].addChild("V6").addChild("V8").addChild("V10");
V1[0][1][0].addChild("V11").addChild("V13");
V1[0][1][0].addChild("V14");
V1.addChild("V3").addChild("V12").addChild("V14").addChild("V6");
V1[1][0][0].addChild("V10");
V1.addChild("V4").addChild("V13").addChild("V8");
cout << V1 << endl;
return 0;
}
The tree in main
is your example in the picture. Here's the output:
V1
|----V2
|----|----V5
|----|----|----V7
|----|----|----|----V10
|----|----V6
|----|----|----V8
|----|----|----|----V10
|----|----|----|----V11
|----|----|----|----|----V13
|----|----|----|----V14
|----V3
|----|----V12
|----|----|----V14
|----|----|----|----V6
|----|----|----|----V10
|----V4
|----|----V13
|----|----|----V8
add a comment |
Your Answer
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f54274315%2fhow-to-represent-a-graph-with-dummy-vertices-using-an-adjacency-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you don't have constraints on implementation, a simple class would do. Here, we store (shared) pointers to children in a vector. (Overloaded) addChild
methods return a reference to the added child, so it is easier to chain together addChild
s. Operator overloading used is nice to have, but not necessary, and you may remove them if desired. Here's the code:
#include <utility>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using std::vector;
using std::string;
using std::size_t;
using std::shared_ptr;
using std::make_shared;
using std::ostream;
using std::cout;
using std::endl;
class Node {
public:
using child_ptr_type = shared_ptr<Node>;
using contaner_type = vector<child_ptr_type >;
explicit Node(string lab = "Default", contaner_type ch = {})
: label(std::move(lab))
, children(std::move(ch))
{}
const string &getLabel() const
{ return label; }
void setLabel(const string &label)
{ Node::label = label; }
const contaner_type &getChildren() const
{ return children; }
const Node& getChild(size_t indx) const
{ return *children[indx]; }
Node& getChild(size_t indx)
{ return *children[indx]; }
Node& addChild(const string& lab = "Default", const contaner_type & ch = {})
{
children.push_back(make_shared<Node>(lab, ch));
return *children.back();
}
Node& addChild(const child_ptr_type &child)
{
children.push_back(child);
return *children.back();
}
Node& addChild(const Node& node)
{
children.push_back(make_shared<Node>(node));
return *children.back();
}
friend ostream& operator<<(ostream& os, const Node &node)
{
node.print(os);
return os;
}
Node&operator(size_t indx)
{
return getChild(indx);
}
const Node&operator(size_t indx) const
{
return getChild(indx);
}
private:
string label;
contaner_type children;
void print(ostream& os, size_t level = 0) const
{
for (size_t i = 0; i != level; ++i) {
os << "|----";
}
os << label << 'n';
for (const auto& child : children) {
child->print(os, level + 1);
}
}
};
int main()
{
Node V1("V1");
V1.addChild("V2").addChild("V5").addChild("V7").addChild("V10");
V1[0].addChild("V6").addChild("V8").addChild("V10");
V1[0][1][0].addChild("V11").addChild("V13");
V1[0][1][0].addChild("V14");
V1.addChild("V3").addChild("V12").addChild("V14").addChild("V6");
V1[1][0][0].addChild("V10");
V1.addChild("V4").addChild("V13").addChild("V8");
cout << V1 << endl;
return 0;
}
The tree in main
is your example in the picture. Here's the output:
V1
|----V2
|----|----V5
|----|----|----V7
|----|----|----|----V10
|----|----V6
|----|----|----V8
|----|----|----|----V10
|----|----|----|----V11
|----|----|----|----|----V13
|----|----|----|----V14
|----V3
|----|----V12
|----|----|----V14
|----|----|----|----V6
|----|----|----|----V10
|----V4
|----|----V13
|----|----|----V8
add a comment |
Since you don't have constraints on implementation, a simple class would do. Here, we store (shared) pointers to children in a vector. (Overloaded) addChild
methods return a reference to the added child, so it is easier to chain together addChild
s. Operator overloading used is nice to have, but not necessary, and you may remove them if desired. Here's the code:
#include <utility>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using std::vector;
using std::string;
using std::size_t;
using std::shared_ptr;
using std::make_shared;
using std::ostream;
using std::cout;
using std::endl;
class Node {
public:
using child_ptr_type = shared_ptr<Node>;
using contaner_type = vector<child_ptr_type >;
explicit Node(string lab = "Default", contaner_type ch = {})
: label(std::move(lab))
, children(std::move(ch))
{}
const string &getLabel() const
{ return label; }
void setLabel(const string &label)
{ Node::label = label; }
const contaner_type &getChildren() const
{ return children; }
const Node& getChild(size_t indx) const
{ return *children[indx]; }
Node& getChild(size_t indx)
{ return *children[indx]; }
Node& addChild(const string& lab = "Default", const contaner_type & ch = {})
{
children.push_back(make_shared<Node>(lab, ch));
return *children.back();
}
Node& addChild(const child_ptr_type &child)
{
children.push_back(child);
return *children.back();
}
Node& addChild(const Node& node)
{
children.push_back(make_shared<Node>(node));
return *children.back();
}
friend ostream& operator<<(ostream& os, const Node &node)
{
node.print(os);
return os;
}
Node&operator(size_t indx)
{
return getChild(indx);
}
const Node&operator(size_t indx) const
{
return getChild(indx);
}
private:
string label;
contaner_type children;
void print(ostream& os, size_t level = 0) const
{
for (size_t i = 0; i != level; ++i) {
os << "|----";
}
os << label << 'n';
for (const auto& child : children) {
child->print(os, level + 1);
}
}
};
int main()
{
Node V1("V1");
V1.addChild("V2").addChild("V5").addChild("V7").addChild("V10");
V1[0].addChild("V6").addChild("V8").addChild("V10");
V1[0][1][0].addChild("V11").addChild("V13");
V1[0][1][0].addChild("V14");
V1.addChild("V3").addChild("V12").addChild("V14").addChild("V6");
V1[1][0][0].addChild("V10");
V1.addChild("V4").addChild("V13").addChild("V8");
cout << V1 << endl;
return 0;
}
The tree in main
is your example in the picture. Here's the output:
V1
|----V2
|----|----V5
|----|----|----V7
|----|----|----|----V10
|----|----V6
|----|----|----V8
|----|----|----|----V10
|----|----|----|----V11
|----|----|----|----|----V13
|----|----|----|----V14
|----V3
|----|----V12
|----|----|----V14
|----|----|----|----V6
|----|----|----|----V10
|----V4
|----|----V13
|----|----|----V8
add a comment |
Since you don't have constraints on implementation, a simple class would do. Here, we store (shared) pointers to children in a vector. (Overloaded) addChild
methods return a reference to the added child, so it is easier to chain together addChild
s. Operator overloading used is nice to have, but not necessary, and you may remove them if desired. Here's the code:
#include <utility>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using std::vector;
using std::string;
using std::size_t;
using std::shared_ptr;
using std::make_shared;
using std::ostream;
using std::cout;
using std::endl;
class Node {
public:
using child_ptr_type = shared_ptr<Node>;
using contaner_type = vector<child_ptr_type >;
explicit Node(string lab = "Default", contaner_type ch = {})
: label(std::move(lab))
, children(std::move(ch))
{}
const string &getLabel() const
{ return label; }
void setLabel(const string &label)
{ Node::label = label; }
const contaner_type &getChildren() const
{ return children; }
const Node& getChild(size_t indx) const
{ return *children[indx]; }
Node& getChild(size_t indx)
{ return *children[indx]; }
Node& addChild(const string& lab = "Default", const contaner_type & ch = {})
{
children.push_back(make_shared<Node>(lab, ch));
return *children.back();
}
Node& addChild(const child_ptr_type &child)
{
children.push_back(child);
return *children.back();
}
Node& addChild(const Node& node)
{
children.push_back(make_shared<Node>(node));
return *children.back();
}
friend ostream& operator<<(ostream& os, const Node &node)
{
node.print(os);
return os;
}
Node&operator(size_t indx)
{
return getChild(indx);
}
const Node&operator(size_t indx) const
{
return getChild(indx);
}
private:
string label;
contaner_type children;
void print(ostream& os, size_t level = 0) const
{
for (size_t i = 0; i != level; ++i) {
os << "|----";
}
os << label << 'n';
for (const auto& child : children) {
child->print(os, level + 1);
}
}
};
int main()
{
Node V1("V1");
V1.addChild("V2").addChild("V5").addChild("V7").addChild("V10");
V1[0].addChild("V6").addChild("V8").addChild("V10");
V1[0][1][0].addChild("V11").addChild("V13");
V1[0][1][0].addChild("V14");
V1.addChild("V3").addChild("V12").addChild("V14").addChild("V6");
V1[1][0][0].addChild("V10");
V1.addChild("V4").addChild("V13").addChild("V8");
cout << V1 << endl;
return 0;
}
The tree in main
is your example in the picture. Here's the output:
V1
|----V2
|----|----V5
|----|----|----V7
|----|----|----|----V10
|----|----V6
|----|----|----V8
|----|----|----|----V10
|----|----|----|----V11
|----|----|----|----|----V13
|----|----|----|----V14
|----V3
|----|----V12
|----|----|----V14
|----|----|----|----V6
|----|----|----|----V10
|----V4
|----|----V13
|----|----|----V8
Since you don't have constraints on implementation, a simple class would do. Here, we store (shared) pointers to children in a vector. (Overloaded) addChild
methods return a reference to the added child, so it is easier to chain together addChild
s. Operator overloading used is nice to have, but not necessary, and you may remove them if desired. Here's the code:
#include <utility>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using std::vector;
using std::string;
using std::size_t;
using std::shared_ptr;
using std::make_shared;
using std::ostream;
using std::cout;
using std::endl;
class Node {
public:
using child_ptr_type = shared_ptr<Node>;
using contaner_type = vector<child_ptr_type >;
explicit Node(string lab = "Default", contaner_type ch = {})
: label(std::move(lab))
, children(std::move(ch))
{}
const string &getLabel() const
{ return label; }
void setLabel(const string &label)
{ Node::label = label; }
const contaner_type &getChildren() const
{ return children; }
const Node& getChild(size_t indx) const
{ return *children[indx]; }
Node& getChild(size_t indx)
{ return *children[indx]; }
Node& addChild(const string& lab = "Default", const contaner_type & ch = {})
{
children.push_back(make_shared<Node>(lab, ch));
return *children.back();
}
Node& addChild(const child_ptr_type &child)
{
children.push_back(child);
return *children.back();
}
Node& addChild(const Node& node)
{
children.push_back(make_shared<Node>(node));
return *children.back();
}
friend ostream& operator<<(ostream& os, const Node &node)
{
node.print(os);
return os;
}
Node&operator(size_t indx)
{
return getChild(indx);
}
const Node&operator(size_t indx) const
{
return getChild(indx);
}
private:
string label;
contaner_type children;
void print(ostream& os, size_t level = 0) const
{
for (size_t i = 0; i != level; ++i) {
os << "|----";
}
os << label << 'n';
for (const auto& child : children) {
child->print(os, level + 1);
}
}
};
int main()
{
Node V1("V1");
V1.addChild("V2").addChild("V5").addChild("V7").addChild("V10");
V1[0].addChild("V6").addChild("V8").addChild("V10");
V1[0][1][0].addChild("V11").addChild("V13");
V1[0][1][0].addChild("V14");
V1.addChild("V3").addChild("V12").addChild("V14").addChild("V6");
V1[1][0][0].addChild("V10");
V1.addChild("V4").addChild("V13").addChild("V8");
cout << V1 << endl;
return 0;
}
The tree in main
is your example in the picture. Here's the output:
V1
|----V2
|----|----V5
|----|----|----V7
|----|----|----|----V10
|----|----V6
|----|----|----V8
|----|----|----|----V10
|----|----|----|----V11
|----|----|----|----|----V13
|----|----|----|----V14
|----V3
|----|----V12
|----|----|----V14
|----|----|----|----V6
|----|----|----|----V10
|----V4
|----|----V13
|----|----|----V8
edited Jan 20 at 11:14
answered Jan 20 at 8:23
AyxanAyxan
1,603316
1,603316
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f54274315%2fhow-to-represent-a-graph-with-dummy-vertices-using-an-adjacency-list%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
Only the graph is to be stored. Right? Are P1 and P2 relevant to you?
– Kunal Puri
Jan 20 at 7:08
Only the graph need to be stored. P1 and P2 are not relevant
– ptpkueqf
Jan 20 at 7:09
Can you replace the hyperlink by the content it refers to?
– JVApen
Jan 20 at 7:09
@ptpkueqf See this: theoryofprogramming.com/2018/01/14/… .This is in Java. But you will get the idea.
– Kunal Puri
Jan 20 at 7:11
1
@ptpkueqf I think you're going to have to explain what you mean by dummy vertices. I can see the graph above is a bit unusual, but exactly what a dummy vertex is, and how you want to process them is a bit unclear.
– john
Jan 20 at 7:32