I am trying to solve the problem using the adjacency list now. But the problem I am facing here is when I am trying to insert the element in an adjacency list, it stores the value in the sequence I have entered.
For example for the test case:
5 8 0 1 0 4 1 2 2 0 2 4 3 0 3 2 4 3
My output is:
0 1 4 2 3
Expected output is:
0 1 2 3 4.
It is because my adjacency list stores the value in the fashion it was not entered in a sorted manner. How would I store it in a sorted manner? If I sort it, it just increases the complexity of the code. Please help.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
void addEdge(vector<ll> edges[], ll u, ll v)
{
edges[u].push_back(v);
edges[v].push_back(u);
}
void BFS(vector<ll> edges[], ll v, bool * visited, queue<ll> q)
{
while(!q.empty())
{
ll s = q.front();
cout << s << " ";
q.pop();
for(ll i = 0; i < edges[s].size(); i++)
{
if(!visited[edges[s][i]])
{
visited[edges[s][i]] = true;
q.push(edges[s][i]);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll v, e;
cin >> v >> e;
vector<ll> edges[v];
for(ll i = 0 ; i < e; i++)
{
int x, y;
cin >> x >> y;
addEdge(edges, x, y);
}
bool * visited = new bool[v];
memset(visited, false, sizeof(visited));
queue<ll> q;
q.push(0);
visited[0] = true;
BFS(edges, v, visited, q);
return 0;
}
Yes, you were right, the behaviour is due to the order in the input.
You could try using a priority queue instead of a simple vector for your adjacency list, to keep your vertices in a specific order, but this does add complexity to your algorithm.
Related
I'm making an adjacency list to represent a graph of roads, there are two kinds of weights (length and capacity) and for some reason this code never exits the for loop. it gets stuck on the cin for the last line. input is in the form:
4 5 4
0 1 1 5
0 2 1 4
1 2 2 2
1 3 1 4
2 3 1 5
I'm a student starting out. Help is very much appreciated thank you.
#include <iostream>
#include <vector>
using namespace std;
int N, M, T; //#cities N, #highways M, time available T
struct Road {
int Y, L, C; //destination Y, length L and capacity C
};
void readNetwork(vector<vector<Road>> &adjList) {
cin >> N >> M >> T;
adjList.resize(N);
for (int i = 0; i < M; i++){
int x, y, l, c;
cin >> x >> y >> l >> c;
adjList[x].push_back({y,l,c});
}
}
int main() {
vector<vector<Road>> adjList;
readNetwork(adjList);
cout << adjList.size();
return 0;
};
So, I was trying to solve the below problem using the most basic method i.e. storing the paths and finding LCA.
My code is working fine on VSCode and giving the right output. But when submitting on SPOJ, it gives runtime error (SIGSEGV).
Problem Link: https://www.spoj.com/problems/LCA/
Problem Description:
A tree is an undirected graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. - Wikipedia
The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with N nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself). - Wikipedia
Your task in this problem is to find the LCA of any two given nodes v and w in a given tree T.
Sample Input:
1
7
3 2 3 4
0
3 5 6 7
0
0
0
0
2
5 7
2 7
Sample Output:
Case 1:
3
1
My Code:
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;
vector<vector<int>> edges;
bool storepath(int s, int d, vector<int>& path, vector<bool>& visited) {
if(s == d)
{
path.push_back(d);
return true;
}
else if(edges[s].size() == 1) {
if(s != d)
{
for(int i = 0; i < path.size(); i++)
if(path[i] == s) {
path.erase(path.begin() + i);
}
}
return false;
}
visited[s] = true;
path.push_back(s);
for(auto e: edges[s])
{
if(visited[e] == false)
{
bool ans = storepath(e, d, path, visited);
if(ans)
break;
}
}
}
int LCA(int a, int b)
{
if(a == b)
return a;
vector<int> path1, path2;
vector<bool> visited(edges.size(), false);
storepath(1, a, path1, visited);
visited.assign(edges.size(), false);
storepath(1, b, path2, visited);
int n = path1.size();
int m = path2.size();
int i = 0,j = 0;
while(i < n && j < m && path1[i] == path2[j]) {
i++;
j++;
}
return path1[i-1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin >> t;
int Case = 1;
while(t--)
{
int n;
cin >> n;
edges.resize(n+1);
for(int i = 1; i <= n; i++)
{
int size, val;
cin >> size;
while(size--)
{
cin >> val;
edges[i].push_back(val);
edges[val].push_back(i);
}
}
int q;
cin >> q;
cout << "Case "<< Case << ":" << endl;
while(q--)
{
int a, b;
cin >> a >> b;
cout << LCA(a, b) << endl;
}
Case++;
edges.clear(); //added after igor's comment (forgot to add before but used while submitting)
}
return 0;
}
I think I'm not accessing any out of scope element so SIGSEGV should not occur.
Please tell me how can I fix and improve my code.
Some bugs are easy to find, when you know how to find them. The tools every programmer should know about are valgrind and -fsanitize. Remember to always compile with warnings enabled and fix them. Compiling your code with:
g++ -Wall -Wextra -fsanitize=undefined 1.cpp && ./a.out </tmp/2
results in a helpful warning:
1.cpp:38:1: warning: control reaches end of non-void function [-Wreturn-type]
38 | }
| ^
and a runtime error:
1.cpp:9:6: runtime error: execution reached the end of a value-returning function without returning a value
Your storepath doesn't return value.
this function prints all triplets in an array whose product is a number k
input first line number of elements, second array elements, third target product .. passes parameters to recursive function f along with a vector that stores elements whose product might give k
thought process -> for each element we can include or exclude it to get the product k. If p > 24 or number elements multiplied > 3 we backtrack. once the prod = k, we print all numbers from vector v and pop them out and set number of elements count to 0 and product to 1 and continue
for an input of:
9
1 2 3 12 4 7 5 24 9
24
my output looks something like this :
12
2
1
9
9
9
| ->cursor justs stops here ..no further outputs...
naming scheme used :
count -> number of elements multiplied till now whose product is stored in -> p
n-> number of elements in array
k -> target pdt
i -> index of element in array currently at
code :
#include <iostream>
#include <vector>
using namespace std;
// all triplets whose product is a number k
void f(int i, int count, int p, int k, vector<int>&v, int *a, int n)
{
// success condition
if(count == 3 && p == k)
{
for(int i = 2; i >= 0; --i)
{
cout << v[i] << " " << endl;
v.pop_back();
}
p = 1;
count = 0;
}
if(count>=3 || i > n - 1 || p > k)
{
return;
}
v.push_back(a[i]);
f(i + 1, count + 1, p * a[i], k, v, a, n);
v.pop_back();
f(i + 1, count, p, k, v, a, n);
}
int main()
{
int n;
cin >> n;
int *a=new int[n];
for(int i = 0; i < n; ++i)
{
cin >> a[i];
}
int k;
cin >> k;
//int p = 1;
vector<int>v;
f(0, 0, 1, k, v, a, n);
delete[] a;
return 0;
}
Your “reset” of p and count on success is immediately strange: why does the function need to keep looking when its caller will already try the other possibilities? But that’s just a distraction from the real issue: the balanced push_back and pop_back around the first recursive call establish and rely on an invariant where every call leaves v the same length as when it started. But the success path clears the vector and leaves it shorter, so eventually you pop_back when it’s empty and—
The joys of undefined behavior (which happened to give an infinite loop here) aside, the fix is trivial: just return after printing without modifying v at all. (Then you may find some further simplifications.)
I've been struggling with a problem where based on a given number i have to find all the triplets that sum gives the given number.
For example if the given number is 5 conditions are : (x+y+z=5 and x<=y<=z) :
Input :
5
Output :
0 0 5
0 1 4
0 2 3
1 1 3
1 2 2
I've tried to find all the solutions starting with 0 but i can't figure out how to find solutions that start with 1, 2, 3, etc
What i have so far :
int x,y,z;
void showSolutions(int c) {
z=c;
while((x<=y) && (y<=z)) {
if((x<=y&&y<=z)&&(x+y+z)==c) {
cout<<x<<" "<<y<<" "<<z;
cout<<"\n";
}
y++;
z--;
}
}
Thanks for help!
Well you can think of this recursively, let me try to point a generic method which would work for two numbers or triplets or more.
class sum
{
public:
vector<vector<int>> triplets(int k, int target)
{
vector<vector<int>> result;
vector<int> holder;
getTriplets(k, target, holder, result);
return result;
}
void getTriplets(int k, int target, vector<int>& holder, vector<vector<int>>& result)
{
if(target < 0) return;
if(k < 0) return;
if(k == 0 && target == 0)
{
result.push_back(holder);
return;
}
for(int i = 0; i <= target; ++i)
{
holder.push_back(i);
getTriplets(k-1, target - i, holder, result);
holder.pop_back();
}
}
};
You can invoke this as:
sum s;
auto res = s.triplets(3,5);
for(auto row : res)
{
for(auto col : row) cout << col << " ";
cout << endl;
}
where the first argument to triplets is the what the size of set required and the latter is the garget value. The problem here is, it will produce duplicates, 0 0 5 and 5 0 0 I will leave that for you to figure out how.
Simply add results to a temporary holder and recursively try all combinations. until you reach the target value which is 0 in our case since we are subtracting, or hit a error condition. Backtrack and pop the value and exhausting all combinations. Store the result if we hit the target.
Live Demo
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int x,y,z;
for(z=n;z>=0;z--)
{
for(y=0;y<=z && z+y<=n;y++)
{
x=n-(y+z);
if(x<=y)
cout<<x<<y<<z<<endl;
}
}
}
for n=5,you get
005
014
113
023
122
I have written a graph structure as an edge list, and am trying to write Kruskal's MST algorithm for it.
Here's my code so far:
#include <bits/stdc++.h>
using namespace std;
struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); } } _;
//////////////////////////////////////////////////////////////////////////////////
#define endl '\n'
#define ll long long
#define pb push_back
#define mt make_tuple
#define in(a) for (auto& i: a)
//////////////////////////////////////////////////////////////////////////////////
#define edge tuple < ll, ll, ll >
bool func (edge a, edge b) { return get<2>(a) < get<2>(b); }
struct graph
{
ll v;
vector <edge> edgelist;
void addedge (edge x) { edgelist.pb(x); }
ll find (vector <ll> parent, ll i)
{ return parent[i]==-1 ? i : find (parent, parent[i]); }
bool cycle()
{
vector <ll> parent (v);
fill (parent.begin(), parent.end(), -1);
in (edgelist)
{
ll x = find (parent, get<0>(i));
ll y = find (parent, get<1>(i));
if (x==y) return true;
else parent[x]=y;
}
return false;
}
graph mst()
{
sort (edgelist.begin(), edgelist.end(), func);
graph tree;
in(edgelist)
{
graph temp = tree;
temp.addedge(i);
if (!temp.cycle()) tree=temp;
}
return tree;
}
};
int main()
{
graph g;
cin >> g.v;
ll e;
cin >> e;
for (ll i=1; i<=e; i++)
{
ll a, b, w;
cin >> a >> b >> w;
g.addedge(mt(a, b, w));
}
graph mstree = g.mst();
in(mstree.edgelist) cout << get<0>(i) << " " << get<1>(i) << " " << get<2>(i) << endl;
cout << endl;
}
/*
Sample Input
4 5
0 1 10
0 2 6
0 3 5
1 3 15
2 3 4
Sample Output
2 3 4
0 3 5
0 1 10
*/
My code takes a very long time to produce the output. Are there any problems in my implementation? Also, if I loop this task for multiple graphs, my program crashes in the middle of execution.
My code takes a very long time to produce the output. Are there any
problems in my implementation?
There are several problems:
First,
ll find (vector <ll> parent, ll i)
{ return parent[i]==-1 ? i : find (parent, parent[i]); }
You pass the parent by value, this means copying all the array. Pass by reference (and non-const, as you will need to modify it, see point 3).
Second, in cycle() you do not need to check all edges, you need to check only the edge that is under consideration in the main loop (in mst()). (And do not set parent in cycle(), you need to use the same array in all mst().)
Third, read on "enhancements" of disjoin-set structure (even the Wikipedia article has it all explained), namely union by rank and path compression. Only with those you will achieve expected performance from the disjoin-set.
Also, if I loop this task for multiple graphs, my program crashes in
the middle of execution.
It is impossible to tell not knowing how to you loop and what are the multiple graphs inputs you are using. However, I strongly suspect stack overflow on even medium-sized graphs as you pass parent by value, thus quickly consuming the stack.