Reading adjacency list from file
I need to read from file adjecancy list for Kruskal's algorith. Input file is
9 17
2 3 8 4 9 8
1 3 3 6 4 5 9 6
2 6 4 4 9 1
2 5 3 4 5 3 9 2
4 3 6 1 9 5
5 1 7 5 9 2
6 5 8 4 9 2
1 4 7 4 9 2
1 8 2 6 3 1 4 2 5 5 6 2 7 2 8 2
Let me explain how to look on this. First row is obviously number of vertices and edges. Every next row means one vertex so secound row is first vertex and it contains vertex with which it will connect and weight of the edge. And below is example on first row.
Vertex 1[Source]: 2[Destination] 3[Weight of the edge] 8[Destination] 4[Weight] 9[Destination] 8[Weight]
And I need to store edges in array of
typedef struct Edge{
int weight;
int beg, end;
}*PEdge;
And the problem is I have no clue how to read it into array. I came up with something like that but obviously it doesn't work.
in.open("In0303.txt");
out.open("Out0303.txt");
if(!in || !out)
return 0;
in>>n>>m;
Edge E[34];
while (! in.eof() ){
getline( in, line );
istringstream is( line );
while( is >> number ) {
edge++;
E[edge].beg=row;
E[edge].end=number;
E[edge].weight=number;
}
row++;
}
c++ algorithm file adjacency-list kruskals-algorithm
add a comment |
I need to read from file adjecancy list for Kruskal's algorith. Input file is
9 17
2 3 8 4 9 8
1 3 3 6 4 5 9 6
2 6 4 4 9 1
2 5 3 4 5 3 9 2
4 3 6 1 9 5
5 1 7 5 9 2
6 5 8 4 9 2
1 4 7 4 9 2
1 8 2 6 3 1 4 2 5 5 6 2 7 2 8 2
Let me explain how to look on this. First row is obviously number of vertices and edges. Every next row means one vertex so secound row is first vertex and it contains vertex with which it will connect and weight of the edge. And below is example on first row.
Vertex 1[Source]: 2[Destination] 3[Weight of the edge] 8[Destination] 4[Weight] 9[Destination] 8[Weight]
And I need to store edges in array of
typedef struct Edge{
int weight;
int beg, end;
}*PEdge;
And the problem is I have no clue how to read it into array. I came up with something like that but obviously it doesn't work.
in.open("In0303.txt");
out.open("Out0303.txt");
if(!in || !out)
return 0;
in>>n>>m;
Edge E[34];
while (! in.eof() ){
getline( in, line );
istringstream is( line );
while( is >> number ) {
edge++;
E[edge].beg=row;
E[edge].end=number;
E[edge].weight=number;
}
row++;
}
c++ algorithm file adjacency-list kruskals-algorithm
1
Why does your code "obviously" not work? What does it do and what were you expecting?
– scohe001
Nov 20 '18 at 19:52
In inner while loop I can read only one number at the time, but I need to read destination and weight save them in the array in right index and then in the next iteration move to next pair and increment edge
– szybkikon
Nov 20 '18 at 19:59
add a comment |
I need to read from file adjecancy list for Kruskal's algorith. Input file is
9 17
2 3 8 4 9 8
1 3 3 6 4 5 9 6
2 6 4 4 9 1
2 5 3 4 5 3 9 2
4 3 6 1 9 5
5 1 7 5 9 2
6 5 8 4 9 2
1 4 7 4 9 2
1 8 2 6 3 1 4 2 5 5 6 2 7 2 8 2
Let me explain how to look on this. First row is obviously number of vertices and edges. Every next row means one vertex so secound row is first vertex and it contains vertex with which it will connect and weight of the edge. And below is example on first row.
Vertex 1[Source]: 2[Destination] 3[Weight of the edge] 8[Destination] 4[Weight] 9[Destination] 8[Weight]
And I need to store edges in array of
typedef struct Edge{
int weight;
int beg, end;
}*PEdge;
And the problem is I have no clue how to read it into array. I came up with something like that but obviously it doesn't work.
in.open("In0303.txt");
out.open("Out0303.txt");
if(!in || !out)
return 0;
in>>n>>m;
Edge E[34];
while (! in.eof() ){
getline( in, line );
istringstream is( line );
while( is >> number ) {
edge++;
E[edge].beg=row;
E[edge].end=number;
E[edge].weight=number;
}
row++;
}
c++ algorithm file adjacency-list kruskals-algorithm
I need to read from file adjecancy list for Kruskal's algorith. Input file is
9 17
2 3 8 4 9 8
1 3 3 6 4 5 9 6
2 6 4 4 9 1
2 5 3 4 5 3 9 2
4 3 6 1 9 5
5 1 7 5 9 2
6 5 8 4 9 2
1 4 7 4 9 2
1 8 2 6 3 1 4 2 5 5 6 2 7 2 8 2
Let me explain how to look on this. First row is obviously number of vertices and edges. Every next row means one vertex so secound row is first vertex and it contains vertex with which it will connect and weight of the edge. And below is example on first row.
Vertex 1[Source]: 2[Destination] 3[Weight of the edge] 8[Destination] 4[Weight] 9[Destination] 8[Weight]
And I need to store edges in array of
typedef struct Edge{
int weight;
int beg, end;
}*PEdge;
And the problem is I have no clue how to read it into array. I came up with something like that but obviously it doesn't work.
in.open("In0303.txt");
out.open("Out0303.txt");
if(!in || !out)
return 0;
in>>n>>m;
Edge E[34];
while (! in.eof() ){
getline( in, line );
istringstream is( line );
while( is >> number ) {
edge++;
E[edge].beg=row;
E[edge].end=number;
E[edge].weight=number;
}
row++;
}
c++ algorithm file adjacency-list kruskals-algorithm
c++ algorithm file adjacency-list kruskals-algorithm
edited Nov 20 '18 at 19:46
szybkikon
asked Nov 20 '18 at 19:44
szybkikonszybkikon
63
63
1
Why does your code "obviously" not work? What does it do and what were you expecting?
– scohe001
Nov 20 '18 at 19:52
In inner while loop I can read only one number at the time, but I need to read destination and weight save them in the array in right index and then in the next iteration move to next pair and increment edge
– szybkikon
Nov 20 '18 at 19:59
add a comment |
1
Why does your code "obviously" not work? What does it do and what were you expecting?
– scohe001
Nov 20 '18 at 19:52
In inner while loop I can read only one number at the time, but I need to read destination and weight save them in the array in right index and then in the next iteration move to next pair and increment edge
– szybkikon
Nov 20 '18 at 19:59
1
1
Why does your code "obviously" not work? What does it do and what were you expecting?
– scohe001
Nov 20 '18 at 19:52
Why does your code "obviously" not work? What does it do and what were you expecting?
– scohe001
Nov 20 '18 at 19:52
In inner while loop I can read only one number at the time, but I need to read destination and weight save them in the array in right index and then in the next iteration move to next pair and increment edge
– szybkikon
Nov 20 '18 at 19:59
In inner while loop I can read only one number at the time, but I need to read destination and weight save them in the array in right index and then in the next iteration move to next pair and increment edge
– szybkikon
Nov 20 '18 at 19:59
add a comment |
1 Answer
1
active
oldest
votes
It sounds like you're having trouble reading two numbers in at once (the weight and destination). Are you ready to have your mind blown? You can chain the input operator!
That means that you can do:
int dest, weight;
while( is >> dest >> weight ) {
edge++;
E[edge].beg = row;
E[edge].end = dest;
E[edge].weight = weight;
}
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%2f53400458%2freading-adjacency-list-from-file%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
It sounds like you're having trouble reading two numbers in at once (the weight and destination). Are you ready to have your mind blown? You can chain the input operator!
That means that you can do:
int dest, weight;
while( is >> dest >> weight ) {
edge++;
E[edge].beg = row;
E[edge].end = dest;
E[edge].weight = weight;
}
add a comment |
It sounds like you're having trouble reading two numbers in at once (the weight and destination). Are you ready to have your mind blown? You can chain the input operator!
That means that you can do:
int dest, weight;
while( is >> dest >> weight ) {
edge++;
E[edge].beg = row;
E[edge].end = dest;
E[edge].weight = weight;
}
add a comment |
It sounds like you're having trouble reading two numbers in at once (the weight and destination). Are you ready to have your mind blown? You can chain the input operator!
That means that you can do:
int dest, weight;
while( is >> dest >> weight ) {
edge++;
E[edge].beg = row;
E[edge].end = dest;
E[edge].weight = weight;
}
It sounds like you're having trouble reading two numbers in at once (the weight and destination). Are you ready to have your mind blown? You can chain the input operator!
That means that you can do:
int dest, weight;
while( is >> dest >> weight ) {
edge++;
E[edge].beg = row;
E[edge].end = dest;
E[edge].weight = weight;
}
answered Nov 20 '18 at 20:02
scohe001scohe001
8,13312342
8,13312342
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%2f53400458%2freading-adjacency-list-from-file%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
1
Why does your code "obviously" not work? What does it do and what were you expecting?
– scohe001
Nov 20 '18 at 19:52
In inner while loop I can read only one number at the time, but I need to read destination and weight save them in the array in right index and then in the next iteration move to next pair and increment edge
– szybkikon
Nov 20 '18 at 19:59