I am surprised why the following code that calculates all pairs shortest pairs does not show me any output.
Here is the code:
#include <iostream>
#include <conio.h>
using namespace std;
int Min(int a,int b){
return a<=b? a:b;
}
int cost[10][10],a[10][10],i,j,k,c;
int main(){
int n,m;
cout<<"enter number of vertices "<<endl;
cin>>n;
cout<<"enter number of edges "<<endl;
cin>>m;
for (k=1;k<=m;k++)
{
cin>>i>>j>>c;
a[i][j]=cost[i][j]=c;
}
for ( i=1;i<=n;i++){
for ( j=1;j<m;j++){
if (a[i][j]==0 && i!=j)
a[i][j]=40000;
}
}
for (k=1;k<=n;k++)
for (i=1;i<=n;i++)
for( j=1;j<=n;j++)
a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
cout<<" resultant adj matrix \n";
for (i=1;j<=n;j++){
for (j=1;i<=n;i++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
You have some typos:
The last loops should look like this:
for (i=1;i<=n;i++){
for (j=1;j<=n;j++){
Just fix the typos in your loops, especially here:
cout<<" resultant adj matrix \n";
for (i=1;i<=n;i++){
for (j=1;j<=m;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
import java.util.*;
class Main{
static int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
public static void main(String args[])
{
int n,i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of nodes :- ");
n=sc.nextInt();
int t=n;
int mat[][]=new int[n+1][n+1];
System.out.println("Consider 5000 as infinity :- ");
System.out.println("Enter the values of adjacency matrix :- ");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
mat[i][j]=sc.nextInt();
}
}
System.out.println("MAT0"+" = ");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(mat[i][j]+" ");
}
System.out.print("\n");
}
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
mat[i][j]=min(mat[i][j],mat[i][k]+mat[k][j]);
}
}
System.out.println("MAT"+k+" = ");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(mat[i][j]+" ");
}
System.out.print("\n");
}
}
}
}
Related
I want to print the number of connected components in a Graph.
For this, I am using bfs traversal. I have used an map to store the adjacency list.
My logic is that if vis[itr]! = true then I am incrementing the count.
But the count is always coming out to be zero.
Why is this so? Can anyone help me out?
#include<bits/stdc++.h>
using namespace std;
void addEdge(map<int,vector<int>>& adj, int u , int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int>bfsOfGraph(int n, map<int,vector<int>>adj, int &count)
{
vector<int>vis(n,0);
vis[0]=1;
queue<int>q;
q.push(0);
vector<int>bfs;
while(!q.empty())
{
auto node= q.front();
q.pop();
bfs.push_back(node);
for(auto it: adj[node])
{
if(!vis[it]){
count++;
vis[it]=1;0.
q.push(it);
}
}
}
cout<<"\n"<<count<<"\n";
return bfs;
}
void printGraph(int n, map<int,vector<int>>adj)
{
for(auto i : adj)
{
cout<<i.first<<" -> ";
for(auto j:i.second)
{
cout<<j<<" ";
}
cout<<"\n";
}
}
int main()
{
int n,e;
cout<<"Enter number of nodes : ";
cin>>n;
cout<<"\nEnter number of edges : ";
cin>>e;
cout<<"\nAdd edges :\n";
map<int,vector<int>>adj;
for(int i=0;i<e;i++)
{
int u,v;
cin>>u>>v;
addEdge(adj,u,v);
}
cout<<"\nPrinting Graph \n";
printGraph(n,adj);
int count=0;
bfsOfGraph(n,adj,count);
cout<<"\n"<<count;
return 0;
}
I want to determine the intersection of 2 bi-dimensional arrays.
Here is my code:
#include < iostream >
using namespace std;
void type(int mat[][10],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>mat[i][j];
}
void inter(int a[][10], int n,int b[][10],int m)
{
int k1=0,p, x,ok,j, c[50],i;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
p=0; x=0;
while(p<m)
{
ok=0;
while(x<m)
{
if(a[i][j]==b[p][x]) ok=1;
if(ok) c[k1++]=a[i][j];
x++;
}
p++;
}
for(i=0;i<n;i++)
{
cout<<c[i]<<" ";
cout<<endl;}
}
}
}
} // !!! added in edit!!!
int main()
{
int n,m,mat[10][10],c[30],mat2[10][10];
cout<<"n= ";cin>>n;
type(mat,n);
cout<<"m= "; cin>>m;
type(mat2,m);
inter(mat,n,mat2,m);
return 0;
}
It doesn't show me the expected answer. It shoes me numbers like these : 1
4758024
1
4758024
Can somebody help me?
I have written a program to print the matrix in the spiral form, but it only works for 3*3. how to make it useful for all the dimensions.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k=1,l=0;
int n;
clrscr();
cout<<"Enter the number of row : ";
cin>>n;
int a[3][3];
cout<<"Matrix Form : "<<"\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=i*n+(j+1);
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"Spiral form"<<"\n";
for(i=k;i<n;i++)
{
cout<<a[k][i]<<"\t";
}
k++;
for(i=k;i>=0;i--)
{
cout<<a[k][i]<<"\t";
}
k--;
for(i=k;i>=0;i--)
{
cout<<a[i][l]<<"\t";
}
for(i=k;i<n;i++)
{
cout<<a[l][i]<<"\t";
}
getch();
}
Assuming the code is working, you need to act on this row:
int a[3][3];
The easiest way is using C++ std::vector:
std::vector<std::vector<int> > a(n, std::vector<int>(n));
Remember to #include <vector> as well.
I tried to generate 3*3 spiral matrix using c by using increment and decrement of rows and columns in an array,then found out sum of main and opposite diagonals of the matrix.
My code is as follows
#include<stdio.h>
#define n 3
int main()
{
int m=n/2,i,j,a[10][10],c=1,sum=0,s=0;
for(i=m,j=m;j<n;j++)
{
a[i][j]=c;
c++;
}
j--;
i++;
while(j>=0)
{
a[i][j]=c;
c++;
j--;
}
i--;
j++;
a:
if(i>=0)
{
a[i][j]=c;
c++;
i--;
goto a;
}
i++;
j++;
b:
if(j<n)
{
a[i][j]=c;
c++;
j++;
goto b;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}printf("\n");
}
for (i = 0; i <n; i++)
{
sum = sum + a[i][i];
s=s+a[i][n-i-1];
}
printf("sum of diagonals is d1: %d d2: %d d1+d2: %d",sum,s,s+sum);
return 0;
}
output:
click here to view output
# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.
I am using a simple approach to convert an array into heap in bottom up manner. I could not find the mistake. Can some one please have a look and let me know.
Basically I am making out of a random array in bottom up manner.
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
There is some issue with heapify procedure ,which sometimes give correct result and most of times wrong.
`#include<iostream>
#include<conio.h>
using namespace std;
int min(i
nt x,int y,int z)
{
return ((x>y?x:y)>z?(x>y?x:y):z);
}
int left(int i)
{
return (2*i);enter code here
}
int right(int i)
{
return (2*i+1);
}
int parent(int i)
{
return (i/2);
}
int heapify(int i,int n,int arr[])
{
int temp;
int l=left(i);
int r=right(i);
temp=i;
if(l<=n&&arr[l]<arr[i])
temp=l;
else if(r<=n&&arr[temp]>arr[r])
temp=r;
if(temp!=i)
{
int x=arr[temp];
arr[temp]=arr[i];
arr[i]=x;
heapify(temp,n,arr);
}
//else return 0;
}
int deletemin(int n,int arr[])
{
cout<<arr[1]<<"\ndeLeted\n";
arr[1]=arr[n];
}
int insert(int x,int size,int arr[])
{
arr[size]=x;
int i=(size);
do
{
i=parent(i);
if(arr[i]!=min(arr[i],arr[left(i)],arr[right(i)]))
heapify(i,(size),arr);
else
return 0;
}while(i!=1);
}
int main()
{
int n,t,arr[100];
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>arr[i];
for(int i=n/2;i>=1;i--)
{
heapify(i,n,arr);
}
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
deletemin(n,arr);
n--;
heapify(1,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
int y;
cout<<"what value do you want to insert\n";
cin>>y;
n++;
insert(y,n,arr);
for(int i=1;i<=n;i++)
cout<<arr[i]<<" ";
}
getch();
return 0;
}
`
One simple observation is that your min function is wrong! It doesn't return the min of 3 numbers. Do it with ifs for the moment!