DFS Traversal in graph [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to do dfs traversal using recursive call, My Graph, and vector<int>visited are global variables. num_v variable corresponds to number of vertices,num_e corresponds to number of edges I tried calling two DFS calls in main, but my output matches to the correct DFS order for the first case. and it gives incorrect out for the other, kindly help where I'm getting wrong in the implementation of it.
sample input: first line is a number of vertices and number of edges. next, each line is directed nodes between two-point.
5 5
1 2
2 4
4 5
5 3
1 3
expected output:
1 2 4 5 3
2 4 5 3
my wrong output
1 2 4 5 3
2
code:
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int mx=1e5;
//int num_v;
vector<vector<int>>Graph(mx);
vector<int>visited;
void dfs(int v,vector<int>&vec){
visited[v]=1;
vec.push_back(v);
for(int neigh:Graph[v]){
if(visited[neigh]!=1){
dfs(neigh,vec);
}
}
}
int main(){
int num_v;int num_e;
cin>>num_v>>num_e;
visited.resize(num_v + 1);
for(int i=0;i<num_e;i++){
int u,v;
cin>>u>>v;
Graph[u].push_back(v);
}
vector<int>vec;
dfs(1,vec);
for(auto x:vec){
cout<<x<<" ";
}
visited.clear();
cout<<endl;
vector<int>tt;
dfs(2,tt);
for(auto y:tt){
cout<<y<<" ";
}
}

Related

Absolute difference of sum of two diagonals of a 2d array in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to obtain the absolute difference of the sum of left and right diagonal of the given 2d array.
I have written the following function-
int diagonalDifference(vector<vector<int>> arr) {
int n=arr.size();
int summ1=0,summ2=0,result=0;
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
{
if(i==j)
{ summ1=summ1+arr[i][j];}
else if((i+j)==(n-1))
{ summ2=summ2+arr[i][j];}
}
}
result=abs(summ1-summ2);
return result;
}
input array
11 2 4
4 5 6
10 8 -12
Explanation- summ1=11+5+(-12)
summ2=4++5+10 result=|4-19|=|-15|=15
expected output: 15
The output I am getting is 10
Here is a solution in O(n) complexity. reference
for (int i = 0; i < n; i++)
{
summ1 += arr[i][i];
summ2 += arr[i][n-i-1];
}
The else-if should be an if by itself.
The else-if prevents the 5 from being added to summ2. Which means the final calculation is: (11+5-12)-(4+10) = -10. Whose absolute value is 10.

Problem in Finding Prime Numbers between Two numbers [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have to find all prime numbers between two given numbers(given in ascending order i.e small, large) I made logic such that my program starts from the given least number till the given most numbers and find factors for each number in between, if factors count are 2 i.e 1 and itself(which is a condition for a prime number), hence it is printed as prime. However I am unable to print my desired output.. can't track why(P.S I am 19 years old newbie in Programming)
#include <iostream>
using namespace std;
int main(){
int start,end;
cin>>start,end;
for(int i=start+1;i<end;++i){
int count;
for(int j=1;j<=i;++j){
if(i%j==0 || i/2==0)count++;
}
if(count==2) cout<<i<<endl;
}
return 0;
}
Input: 1 10
Expected Output:
2
3
5
7
9
Output: (nothing)
Your program has several issues.
cin>>start,end; is not going to read in 2 numbers. You need cin >> start >> end;
You are not initializing count to anything, so you invoke undefined behavior when you do count++. You need to do int count = 0;
Also, when checking if n is prime, you don't need to check for divisibility by 1 or n since this is always true.

C++ STL-Inserter [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm a newbie in C++ STL. I have some problem in the below code with respect to the output.Why is this not providing the expected output?
#include<iostream>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
void show(vector<int> vect)
{
for(int i=0;i<vect.size();i++)
{
cout<<vect[i]<<" ";
}
}
int main()
{
int arr[5]={ 1, 2, 3, 4, 5 };
vector<int> vect1(arr,arr+5);
vector<int>::iterator it;
it=vect1.begin();
advance(it,3);
copy(vect1.begin(),vect1.end(),inserter(vect1,it));
show(vect1);
return 0;
}
Expected output:1 2 3 1 2 3 4 5 4 5
Actual output:1 2 3 1 0 3 4 5 4 5
Can anyone help me in finding out where it went wrong?
Replace the copy with this:
vect1.insert(it, std::begin(arr), std::end(arr));
where the arguments to insert are:
it is the position you want to insert into vector vect1;
std::begin(arr) is the start position of range to insert;
std::end(arr) is the end position of range to insert.

3n+1 uVa gives WA [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
My initial question was a bit vague, so here's the edited question.
The 3n+1 uVa problem is based on the Collatz Conjecture.
Consider any positive integer 'n'. If it is even, divide it by 2. If it is odd, multiply it by 3 and add 1. After 'x' such repeated operations, you will get 1. This has been proven for very large numbers by supercomputers.
UVa Online Judge is an online automated judge for programming problems hosted by University of Valladolid.
Here's a link to the problem statement:
uVa 3n+1
Please read the problem statement before looking at my code!
The uVa online judge runs your code against certain test-cases and tells you if your solution was right or wrong. It does not tell you why it failed (if your solution was wrong).
Here is the link again if you missed the first one:
The link to the problem description
I don't understand what I am missing out or skipping (since I am getting Wrong Answer) in my code logic. I have tried lots of test cases, and they seem to be working fine. I know the logic can be compressed to a large extent, but I just need to figure out where the flaw is for now.
Here is my code. I know bits/stdc++ shouldn't be used, but currently, it's not affecting my code. Please review it if you can and help me out.
#include<iostream>
#include<cstdlib>
#include<bits/stdc++.h>
using namespace std;
int main()
{
unsigned long int num;
int i,j,tempi, tempj,temp;
//Scanning until inputs don't stop
while(scanf("%d %d",&i,&j)!=EOF)
{
//Boolean variable to set if i is greater
bool iwasmore=false;
//Boolean variable for equal numbers
bool equalnums=false;
int cycles=1, maxcycles=0;
if(i>j)
{
//swapping for the for loop to follow
temp=i; i=j; j=temp;
iwasmore=true;
}
if(i==j)
{
equalnums=true;
}
tempi=i; tempj=j;
//Taking each number in the given range and running it in the algorithm.
//The maxcycles variable will have the value of the maximum number of cycles
//that one of the numbers in the range took (at the end of the for loop).
for(num=i;num<=j;num=(++tempi))
{
if(cycles>maxcycles)
{
maxcycles=cycles;
}
cycles=1;
//The actual algorithm
while(num!=1)
{
if(num%2==1)
{
num = (3*num)+1;
cycles++;
}
else
{
num=(num/2);
cycles++;
}
}
if(equalnums==true)
{
maxcycles=cycles;
equalnums=false;
}
//Resetting num
num=0;
}
if(!iwasmore)
cout<<i<<" "<<j<<" "<<maxcycles<<endl;
else
cout<<j<<" "<<i<<" "<<maxcycles<<endl;
}
return 0;
}
Here is the output I am getting for the following input:
Input:
1 1
10 1
210 201
113383 113383
999999 1
Output:
1 1 1
10 1 20
210 201 89
113383 113383 248
999999 1 525
Another testcase:
Input:
1 5
10 8
210 202
113383 113383
999999 999989
Output:
1 5 8
10 8 20
210 202 89
113383 113383 248
999999 999989 259
The cycle length of the last number checked is never compared against maximum.
For Input:
8 9
9 9
The program outputs
8 9 4
9 9 20
But the correct answer is
8 9 20
9 9 20
Put
if (cycles > maxcycles) {
maxcycles = cycles;
}
at the end of the for-loop, instead of at the beginning.

Reducing Run Time C or C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.