My job is to complete the isCyclic() function in following program. I can handle this job with visited array and passing parent to dfs() function but I am trying another solution, So I use a height array to keep track of distance of nodes in DFS tree for determining parents and cycle. this height array initial by zero. If distance of two node be exactly one these two node are parent child.
But I cannot understand why my solution doesn't work. can someone tell me why output of this code is not correct?
thanks for your help.
An example of graph and height value of nodes:
/*
.1
/ \
.2 .2
/ \
.3 .3
*/
#include<bits/stdc++.h>
using namespace std;
class Graph
{
int V;
list<int> *adj;
public :
Graph(int V);
void addEdge(int v,int w);
bool isCyclic();
};
vector<int> g[100001];
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v,int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
int main()
{
int T;
cin>>T;
while(T--)
{
int _size,N;
cin>>_size>>N;
Graph *g = new Graph(_size);
for(int i=0;i<N;i++)
{
int u,v;
cin>>u>>v;
g->addEdge(u,v);
}
cout<<g->isCyclic()<<endl;
}
}
/*Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function is mentioned above.*/
/*The structure of the class is as follows
which contains an integer V denoting the no
of vertices and a list of adjacency vertices.
class Graph
{
int V;
list<int> *adj;
public :
Graph(int V);
void addEdge(int v,int w);
bool isCyclic();
};*/
/*You are required to complete this method*/
bool dfs(int v, int hi, list<int> *adj, int *h) {
h[v] = hi;
list<int>::iterator it;
for(it = adj[v].begin(); it != adj[v].end(); it++) {
int u = *it;
//cycle detect
if (h[u]>0 && hi - h[u] > 1)
return true;
if (h[u]==0 && dfs(u, hi+1, adj, h))
return true;
}
return false;
}
/*You are required to complete this method*/
bool Graph :: isCyclic()
{
//Your code here
int h[V];
for(int i = 0; i < V; i++) h[i] = 0;
for(int i = 0; i< V; i++)
if ( h[i] == 0 && dfs(i, 1, adj, h) ) return true;
return false;
}
A input that make my answer incorrect:
Input:
85 59
0 34 32 54 6 16 45 44 82 52 57 15 20 60 52 44 75 77 48 18 53 75 14 40 39 46 24 26 32 0 39 74 34 29 43 41 45 45 0 42 54 14 58 75 31 67 34 63 16 39 81 69 29 52 67 26 14 6 52 3 48 49 77 83 78 78 81 38 38 38 38 8 53 53 40 84 77 31 63 9 70 16 78 57 69 60 83 83 23 7 43 72 56 35 27 6 70 23 2 24 61 40 74 71 50 29 28 42 60 48 51 2 64 2 59 48 19 57
Its Correct output is:
1
And Your Code's output is:
0
Related
The values in my input testcase files were such that at some point in the code, values would exceed the capacity of int, so I figured I'd change the datatype of this particular array holding this value greater than INT_MAX from int to long long and change maximum values in the code to LLONG_MAX from INT_MAX so that comparisons during runtime don't yield a wrong answer.
However, now the code seems to get stuck with a runtime error even before arriving at the mentioned testcase. It now fails at a case that it used to pass when the values were all int oriented. I don't understand how this is possible.
The testcase which passes with int but fails with ll is:
100 50
1 23 133
1 87 16
2 9 78
3 12 117
3 39 19
5 25 219
5 47 130
5 97 157
6 50 114
9 11 25
9 39 227
10 45 187
10 77 120
12 19 85
13 43 247
14 16 4
15 33 223
16 33 1
19 69 204
20 35 119
20 43 213
20 86 19
22 40 233
23 33 61
23 79 152
26 89 213
27 57 129
28 42 220
31 68 84
31 69 183
32 39 145
32 100 117
33 49 198
34 48 78
37 66 200
37 91 77
39 44 235
41 70 109
42 92 33
44 74 196
48 73 26
51 57 216
53 70 158
63 98 220
66 72 148
80 93 150
81 99 54
83 84 129
83 89 177
95 100 16
Below is the code that gives an error at this tc.
#include<bits/stdc++.h>
using namespace std;
# define ll long long int
ll update, previous;
set<pair<ll, int>> dist;
auto it=dist.begin();
int ind=0, n, i, j;
pair<ll, int>p;
void dij(vector<pair<int, ll>> tree[], bool decided[], ll d[], int path[]) {
ind=0;
while(!dist.empty()) {
it=dist.begin();
if(it==dist.end()) return;
ind=it->second;
dist.erase(it);
decided[ind]=1;
for(j=0; j<tree[ind].size(); j++) {
update=d[ind]+tree[ind][j].second;
previous=d[tree[ind][j].first];
if(update<previous) {
p=make_pair(previous, tree[ind][j].first);
dist.erase(dist.find(p));
p=make_pair(update, tree[ind][j].first);
dist.insert(p);
path[tree[ind][j].first]=ind;
}
d[tree[ind][j].first]=min(update, previous);
}
}
}
int main()
{
ll edges;
ll x, y, weight;
cin>>n>>edges;
vector<pair<int, ll>> graph[n];
for(i=0; i<edges; i++) {
cin>>x>>y>>weight;
x--; y--;
graph[x].push_back({y, weight});
graph[y].push_back({x, weight});
}
int src=1;
src--;
ll d[n];
for(i=0; i<n; i++) {
if(src==i) {
dist.insert({0, i});
d[i]=0;
}
else {
dist.insert({LLONG_MAX, i});
d[i]=LLONG_MAX;
}
}
bool decided[n]={0};
int path[n]={-1};
for(int i=1; i<n; i++) path[i]=-2;
dij(graph, decided, d, path);
if(path[n-1]==-2) cout<<-1;
else {
vector<int> s;
int final=n-1;
while (final!=-1) {
s.push_back(final);
final=path[final];
}
reverse(s.begin(), s.end());
for(auto pi:s) cout<<pi+1<<" ";
}
cout<<endl;
}
Below is the code that produces a correct output for this tc.
#include<bits/stdc++.h>
using namespace std;
# define ll long long int
ll update, previous;
set<pair<ll, int>> dist;
auto it=dist.begin();
int ind=0, n, i, j;
pair<ll, int>p;
void dij(vector<pair<int, ll>> tree[], bool decided[], int d[], int path[]) {
ind=0;
while(!dist.empty()) {
it=dist.begin();
if(it==dist.end()) return;
ind=it->second;
dist.erase(it);
decided[ind]=1;
for(j=0; j<tree[ind].size(); j++) {
update=d[ind]+tree[ind][j].second;
previous=d[tree[ind][j].first];
if(update<previous) {
p=make_pair(previous, tree[ind][j].first);
dist.erase(dist.find(p));
p=make_pair(update, tree[ind][j].first);
dist.insert(p);
path[tree[ind][j].first]=ind;
}
d[tree[ind][j].first]=min(update, previous);
}
}
}
int main()
{
ll edges;
ll x, y, weight;
cin>>n>>edges;
vector<pair<int, ll>> graph[n];
for(i=0; i<edges; i++) {
cin>>x>>y>>weight;
x--; y--;
graph[x].push_back({y, weight});
graph[y].push_back({x, weight});
}
int src=1;
src--;
int d[n];
for(i=0; i<n; i++) {
if(src==i) {
dist.insert({0, i});
d[i]=0;
}
else {
dist.insert({INT_MAX, i});
d[i]=INT_MAX;
}
}
bool decided[n]={0};
int path[n]={-1};
for(int i=1; i<n; i++) path[i]=-2;
dij(graph, decided, d, path);
if(path[n-1]==-2) cout<<-1;
else {
vector<int> s;
int final=n-1;
while (final!=-1) {
s.push_back(final);
final=path[final];
}
reverse(s.begin(), s.end());
for(auto pi:s) cout<<pi+1<<" ";
}
cout<<endl;
}
The only difference in the 2 codes are the following lines:
void dij(vector<pair<int, ll>> tree[], bool decided[], ll d[], int path[])
void dij(vector<pair<int, ll>> tree[], bool decided[], int d[], int path[])
ll d[n];
int d[n];
dist.insert({LLONG_MAX, i})
dist.insert({INT_MAX, i})
d[i]=LLONG_MAX
d[i]=INT_MAX
Could someone please point out how is this creating the following error which I read is related to "allocating memory where I should not" or "attempting to execute delete with a pointer value that was not obtained from new". What is causing this problem and how should I resolve it?
free(): invalid pointer
Aborted (core dumped)
The problem was indeed related to long long and that is why the code with int was running fine, because the fact that update would create an overflow, as the variable is a sum of two long long type variables which would have had max value LLONG_MAX assigned in main() was overlooked.
As long long can not accommodate 2*LLONG_MAX it was neither holding nor finding that value in the set of pairs used as the min heap. Thus iterator pointed to end of the set and erasing set.end() would generate undefined behavior in the long long datatype oriented code whereas it wouldn't in the int oriented one.
Replacing LLONG_MAX with 1e18 instead, in the code solved the problem and the code runs for all test files smoothly.
Additionally to clarify all the reasons that were pointed out through the comments, I thought I should clarify that not checking if dist.find(p) exists and doesn't point to end of set before performing dist.erase(dist.find(p)) would not create any problems. This is because it is Dijkstra's Algorithm and as many times as update is found to be less than previous, the node that this updated distance is being calculated for, from the source will always be present in the set paired with the distance previous. This is because all the nodes are initially entered with a value of 10e8 and are being updated as the values are found in successive iterations of the while loop.
Below is the working code, the only difference is that instead of LLONG_MAX I have used 1e18 and it runs fine on all test files including the one I had mentioned in the question as being problematic.
#include<bits/stdc++.h>
using namespace std;
# define ll long long int
ll update, previous;
set<pair<ll, int>> dist;
auto it=dist.begin();
int ind=0, n, i, j;
pair<ll, int>p;
void dij(vector<pair<int, ll>> tree[], bool decided[], ll d[], int path[]) {
ind=0;
while(!dist.empty()) {
it=dist.begin();
if(it==dist.end()) return;
ind=it->second;
dist.erase(it);
decided[ind]=1;
for(j=0; j<tree[ind].size(); j++) {
update=d[ind]+tree[ind][j].second;
previous=d[tree[ind][j].first];
if(update<previous) {
p=make_pair(previous, tree[ind][j].first);
//cout<<p.first<<" intermediate "<<p.second<<endl;
dist.erase(dist.find(p));
p=make_pair(update, tree[ind][j].first);
dist.insert(p);
path[tree[ind][j].first]=ind;
}
d[tree[ind][j].first]=min(update, previous);
}
}
}
int main()
{
ll edges;
ll x, y, weight;
cin>>n>>edges;
vector<pair<int, ll>> graph[n];
for(i=0; i<edges; i++) {
cin>>x>>y>>weight;
x--; y--;
graph[x].push_back({y, weight});
graph[y].push_back({x, weight});
}
int src=1;
src--;
ll d[n];
for(i=0; i<n; i++) {
if(src==i) {
dist.insert({0, i});
d[i]=0;
}
else {
dist.insert({1e18, i});
d[i]=1e18;
}
}
bool decided[n]={0};
int path[n]={-1};
for(int i=1; i<n; i++) path[i]=-2;
dij(graph, decided, d, path);
if(path[n-1]==-2) cout<<-1;
else {
vector<int> s;
int final=n-1;
while (final!=-1) {
s.push_back(final);
final=path[final];
}
reverse(s.begin(), s.end());
for(auto pi:s) cout<<pi+1<<" ";
}
cout<<endl;
}
Here is the output for the test file in the question:
Input
100 50
1 23 133
1 87 16
2 9 78
3 12 117
3 39 19
5 25 219
5 47 130
5 97 157
6 50 114
9 11 25
9 39 227
10 45 187
10 77 120
12 19 85
13 43 247
14 16 4
15 33 223
16 33 1
19 69 204
20 35 119
20 43 213
20 86 19
22 40 233
23 33 61
23 79 152
26 89 213
27 57 129
28 42 220
31 68 84
31 69 183
32 39 145
32 100 117
33 49 198
34 48 78
37 66 200
37 91 77
39 44 235
41 70 109
42 92 33
44 74 196
48 73 26
51 57 216
53 70 158
63 98 220
66 72 148
80 93 150
81 99 54
83 84 129
83 89 177
95 100 16
Participant's output
-1
Jury's answer
-1
The link to submission - Dijkstra
I am getting a infinite loop when I try and run my solution for the Knights Tour problem using Backtracking
My Solution Code:
Link: https://ideone.com/Ud92vF
code:
#include <bits/stdc++.h>
using namespace std;
bool valid(int arr[8][8],int r,int c)
{
if(r>=0 and r<8 and c>=0 and c<8 and arr[r][c]== -1)
return true;
return false;
}
void fun(int arr[8][8],int r,int c,int x)
{
if(x==64){
cout<<"***********************ARRAY FOUND***********************\n";
for(int i=0;i<8;i++){
for(int j=0;j<8;j++)
cout<<arr[i][j]<<" ";
cout<<"\n";
}
return;
}
if(!valid(arr,r,c))
return;
arr[r][c] = x;
fun(arr,r-2,c+1,x+1); fun(arr,r-2,c-1,x+1);
fun(arr,r-2,c+2,x+1); fun(arr,r-2,c-2,x+1);
fun(arr,r+2,c+1,x+1); fun(arr,r+2,c-1,x+1);
fun(arr,r+1,c+2,x+1); fun(arr,r+1,c-2,x+1);
arr[r][c] = -1;
}
int main()
{
int arr[8][8] ;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++)
arr[i][j] = -1;
}
int r=0,c=0,x=0; fun(arr,r,c,x);
}
Make sure your move array is correct:
fun(arr,r-2,c-1,x+1); fun(arr,r-2,c+1,x+1);
fun(arr,r-1,c-2,x+1); fun(arr,r-1,c+2,x+1);
fun(arr,r+1,c-2,x+1); fun(arr,r+1,c+2,x+1);
fun(arr,r+2,c-1,x+1); fun(arr,r+2,c+1,x+1);
With this I get a right answer:
***********************ARRAY FOUND***********************
0 11 8 5 2 13 16 19
9 6 1 12 17 20 3 14
30 27 10 7 4 15 18 21
63 24 31 28 35 22 47 44
32 29 26 23 48 45 36 57
25 62 51 34 39 56 43 46
52 33 60 49 54 41 58 37
61 50 53 40 59 38 55 42
Note that as you use the 65th move to validate you answer, you'll get 8 of the same correct answers in a row. And then another 8. Etc. You can fix this by printing after your 64th move:
void fun(int arr[8][8],int r,int c,int x)
{
if(!valid(arr,r,c))
return;
arr[r][c] = x;
if(x==63){
cout<<"***********************ARRAY FOUND***********************\n";
for(int i=0;i<8;i++){
for(int j=0;j<8;j++)
cout<<arr[i][j]<<" ";
cout<<"\n";
}
}
else
{
fun(arr,r-2,c-1,x+1); fun(arr,r-2,c+1,x+1);
fun(arr,r-1,c-2,x+1); fun(arr,r-1,c+2,x+1);
fun(arr,r+1,c-2,x+1); fun(arr,r+1,c+2,x+1);
fun(arr,r+2,c-1,x+1); fun(arr,r+2,c+1,x+1);
}
arr[r][c] = -1;
}
And one last issue is that you only ever start at {0,0} so you'll only find knights tours which start on that square. You really want to start from every square to find all possible knights tours. Or if you're feeling clever you only need to check a subset of the starting squares and use symmetry to generate the others.
I need to reverse and print out the contents of the following array using recursion:
int myArray[] = { 65, 89, 74, 25, 21, 54, 38, 64, 11, 45, 19, 48, 46, 83 };
The call to the function is:
printArray(myArray, arraySize, 0, arraySize - 1);
And my function is:
void printArray(int values[], int sizeOfArray, int low, int high) {
if (low < high) {
int tempValue = values[low];
values[low] = values[high];
values[high] = tempValue;
printArray(values,sizeOfArray, low+1 ,high-1);
}
for (int i = 0; i < sizeOfArray; i++) {
cout << values[i]<<' ';
}
}
The output that I am getting is the reversed array a total of 8 times.
83 46 48 19 45 11 64 38 54 21 25 74 89 65 83 46 48 19 45 11 64 38 54 21 25 74 89 65 83 46 48 19 45 11 64 38 54 21 25 74 89 65 83 46 48 19 45 11 64 38 54 21 25 74 89 65 83 46 48 19 45 11 64 38 54 21 25 74 89 65 83 46 48 19 45 11 64 38 54 21 25 74 89 65 83 46 48 19 45 11 64 38 54 21 25 74 89 65 83 46 48 19 45 11 64 38 54 21 25 74 89 65
I can not seem to figure out what is wrong here. I am also new to arrays and recursion so any help is greatly appreciated.
If the same function is supposed reverse, then print, the sequence, you can do it by surrounding your for-loop with a condition that is only true on the outer-most invoke of the recursive call stack.
void printArray(int values[], int sizeOfArray, int low, int high)
{
if (low < high)
{
int tempValue = values[low];
values[low] = values[high];
values[high] = tempValue;
printArray(values, sizeOfArray, low+1 ,high-1);
}
if (low == 0)
{
for (int i = 0; i < sizeOfArray; i++)
std::cout << values[i]<<' ';
std::cout << '\n';
}
}
Worth noting. sizeofArray, or high, are pointless in this code. You don't need both. You can just as easily do this, for example:
void printArray(int values[], int low, int high)
{
if (low < high)
{
int tempValue = values[low];
values[low] = values[high];
values[high] = tempValue;
printArray(values, low+1 ,high-1);
}
if (low == 0)
{
for (int i = 0; i <= high; i++)
std::cout << values[i]<<' ';
std::cout << '\n';
}
}
Or
Likewise, you can use a template to do this to gather the native array size at compile time (which I leave as a future exercise, as right now it would probably be more a Rube Goldberg solution to you than anything else).
I am trying to recursively implement the merge sort algorithm by only passing in a vector value to the function (no left or right index). The while loop in the following code works when the list to be sorted is passed as a pointer void merge_sort_array(int* v, int l, int r) or reference void merge_sort_ref(vector<int>& v, int l, int r) but I cannot for the life of me understand why the following code will not properly sort my list. I have a feeling it is something to do with either the starting values of i, j, k or the bounds within my while loop but I've tried anything that makes sense to me and can't figure it out.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> merge_sort_value(vector<int> v) {
int n = v.size();
if(n == 1){
return v;
}
else{
int m = n/2;
vector<int> v1(v.begin(), v.begin()+m);
vector<int> v2(v.begin()+m, v.begin()+n);
merge_sort_value(v1);
merge_sort_value(v2);
vector<int> tmp(v.begin(), v.begin()+m);
int i = 0;
int j = m;
int k = 0;
while((i < m) or (j < n)){
if(i == m){
v[k] = v[j];
j +=1;
}
else if((j == n) or (tmp[i] < v[j])){
v[k] = tmp[i];
i+=1;
}
else{
v[k] = v[j];
j+=1;
}
k+=1;
# print output for debugging
for(auto x = v.begin(); x != v.end(); ++x)
cout << *x << " ";
cout << "" << endl;
cout << i << "\t"<< j << "\t" << k << endl;
}
return v;
}
}
int main(int argc, char** argv) {
vector<int> v(10);
for(int i=0; i < 10; ++i)
v[i] = rand() % 100;
v = merge_sort_value(v);
return 0;
}
I have included a sample output for reference below:
28 28
0 2 1
28 80
1 2 2
21 21
0 2 1
21 92
1 2 2
14 92 21
1 1 1
14 92 21
1 2 2
14 92 21
1 3 3
14 28 14 92 21
0 3 1
14 80 14 92 21
1 3 2
14 80 28 92 21
2 3 3
14 80 28 92 21
2 4 4
14 80 28 92 21
2 5 5
21 57
1 1 1
21 57
1 2 2
78 83
1 1 1
78 83
1 2 2
78 78 83
0 2 1
78 83 83
0 3 2
78 83 96
1 3 3
21 57 96 78 83
1 2 1
21 57 96 78 83
2 2 2
21 57 96 78 83
2 3 3
21 57 96 78 83
2 4 4
21 57 96 78 83
2 5 5
21 28 14 92 21 21 57 96 78 83
0 6 1
21 57 14 92 21 21 57 96 78 83
0 7 2
21 57 80 92 21 21 57 96 78 83
1 7 3
21 57 80 28 21 21 57 96 78 83
2 7 4
21 57 80 28 14 21 57 96 78 83
3 7 5
21 57 80 28 14 92 57 96 78 83
4 7 6
21 57 80 28 14 92 21 96 78 83
5 7 7
21 57 80 28 14 92 21 96 78 83
5 8 8
21 57 80 28 14 92 21 96 78 83
5 9 9
21 57 80 28 14 92 21 96 78 83
5 10 10
Thank you, any help is greatly appreciated!
after reviewing you code it seems you're making mistakes in the algorithm it self and in C++ as language so i've edited your algorithm to be more neat and more readable algorithm i will explain some part of the code
Code
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> merge_sort_value(vector<int> v) {
int n = v.size();
if(n == 1){
return v;
}
else{
int m = n/2;
vector<int> v1(v.begin(), v.begin()+m);
vector<int> v2(v.begin()+m, v.begin()+n);
v1 = merge_sort_value(v1); /* passing by value will left v1 with no sorting so you need to copy from the returned
object */
v2 = merge_sort_value(v2);
int i = 0;
int j = 0;
int k = 0;
const size_t left_vecS = v1.size();
const size_t right_vecS = v2.size();
while (i<left_vecS&&j<right_vecS) { // we must keep i (AND) j valid
if (v1[i] < v2[j])
v[k++] = v1[i++];
else
v[k++] = v2[j++];
}
while(i<left_vecS) // if we sorted v2 then what insert the rest of v1 in v as what kept from v1 will be sorted
v[k++] = v1[i++];
while(j<right_vecS)
v[k++] = v2[j++];
}
return v;
}
int main(int argc, char** argv) {
vector<int> v(10);
std::vector<int> x;
for(int i=0; i < 10; ++i)
v[i] = rand() % 100;
v = merge_sort_value(v);
for(auto&i:v)
std::cout << i << std::endl;
return 0;
}
1- I get rid of the printing inside the sorting function so we keep the code clean
2-
the first error you've did at the language level is you didn't copy the returned sorted vector object from merge_sort_value to the vectors.(i've mentioned that in the code in a comment) so that's the first thing to keep in mind
3- the logic part of the algorithm wasn't clear to me because i didn't see how you're sorting specially that part else if ((j == n) or (tmp[i] < v[j])) {
v[k] = tmp[i];
i += 1;
}
like you're comparing unsorted sub vector to another unsorted vector and you're giving it unsorted value again (you must compare v1 against v2)
the whole logic is missed up i think you need to review it
anyway i hope that helped
I'm having a little trouble utilizing a map, and I have never really used them before so I am really struggling here
My Code is as follows:
MSTapp.h
5 #ifndef MSTAPP_H
6 #define MSTAPP_H
7
8 #include"Graph.h"
9
10 #include<iostream>
11 #include<map>
12 #include<vector>
13 #include<string>
14
15 using namespace std;
16
17 class MSTapp
18 {
19 public:
20 void read_graph();
21 void print_v();
22 // void print_e();
23
24 private:
25 Graph my_graph;
26 };
27
28 #endif
29
MSTapp.cpp
5 #include"MSTapp.h"
6 #include"Graph.h"
7
8 #include<iostream>
9 #include<map>
10 #include<vector>
11 #include<stdlib.h>
12 #include<sstream>
13 #include<string>
14
15 using namespace std;
16
17 void MSTapp::read_graph()
18 {
19 string s;
20 int count = 0;
21
22 while(getline(cin, s))
23 {
24 if(count == 0)
25 {
26 istringstream my_words(s);
27 string word;
28 while(my_words >> word)
29 {
30 my_graph.add_vertex(word);
31 }
32 }
33 else
34 {
35 string first;
36 string last;
37 int key;
38
39 first = s.substr(0, s.find(" "));
40 s.erase(0,s.find(" ")+1);
41 last = s.substr(0, s.find(" "));
42 s.erase(0,s.find(" ")+1);
43 key = atoi(s.c_str());
44
45 my_graph.add_edge(first, last, key);
46 }
47 }
48 }
49
50 void MSTapp::print_v()
51 {
52 my_graph.print_vertices();
53 }
Graph.h
5 #include"MSTapp.h"
6 #include<map>
7 #include<iostream>
8 #include<string>
9 #include<vector>
10 #include<list>
11
12 using namespace std;
13
14 #ifndef GRAPH_H
15 #define GRAPH_H
16
17 class Graph
18 {
19 public:
20 // Graph();
21 // ~Graph();
22 void add_vertex(string name);
23 void print_vertices();
24 void add_edge(string from, string to, int weight);
25 // void print_edges();
26 // void min_span_tree(string start);
27
28 private:
29 // MinPriority min_queue;
30
31 class Vertex
32 {
33 public:
34 string name;
35 string pi;
36 int key;
37 };
38 class Neighbor
39 {
40 public:
41 string name;
42 int weight;
43 };
44
45 vector<Vertex> vertices;
46 map <string name, list<Neighbor> > adj_list;
4
48 };
49
50 #endif
Graph.cpp
5 #include"MSTapp.h"
6 #include"Graph.h"
7
8 #include<iostream>
9 #include<map>
10 #include<algorithm> // sort
11 #include<string>
12 #include<vector>
13 #include<list>
14
15 using namespace std;
35 void Graph::add_vertex(string name)
36 {
37 Vertex v1;
38 v1.name = name;
39 v1.pi = "NIL";
40 v1.key = 100;
41 bool check;
42
43 for(int i = 0; i < vertices.size(); i++)
44 {
45 if(vertices[i].name == v1.name)
46 {
47 check = true;
48 }
49 }
50
51 if(check == false)
52 {
53 vertices.push_back(v1);
54 }
55 }
56
57 void Graph::print_vertices()
58 {
59 for(int i = 0; i < vertices.size(); i++)
60 {
61 cout << vertices[i].name << " " << vertices[i].pi << " " << vertices[i].key << endl;
62 }
63 }
64
65 void Graph::add_edge(string from, string to, int weight)
66 {
67 string v_from = from;
68 string v_to = to;
69 int v_weight = weight;
70
71 Neighbor n1;
72 n1.name = v_to;
73 n1.weight = v_weight;
74
75 adj_list[v_from];
76 adj_list[v_from].push_back(n1);
77 adj_list[v_from].sort();
78 }
All the functions for the Vertices are working so just ignore those. The main problem I am having is the add_edge function in the Graph. For example my input would be something as follows:
A B C D E //Vertices, this is working
A B 3
so, with the A B 3, I would like to add the B and 3 the the neighbor, and do something like
adj_list[A].push_back(Neighbor)
The errors I am getting are regarding the map, it is saying I have the incorrect number of template arguments for the map in Graph.h.
Am I declaring my map incorrectly on line 46 of Graph.h?
If anyone could provide any insight as to what I am doing wrong or how I can get this working it would be GREATLY appreciated. If any additional clarifcation is needed, just ask. Thanks!
you should declare the map as:
map <string, list<Neighbor> > adj_list;