Hello ,i have problem with code bellow. every data i enter it show me the weight '0'!
Can you tell me how can i solve this problem?
you should enter number of nods and edges and weight of each edge.

#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <Windows.h>
using namespace std;

int perim (int set[],struct krus edge[],int n,int m);
void sort(struct krus ed[],int m);

struct krus{
	    char v1;
	    char v2;
	    int weight;
};

 
  
int main()
{
  system("cls");
  int n,m;

  cout<<"Input Num Vertex : ";
  cin>>n;
  int set[n];
  for (int i=0;i<n;i++)
     set[i]=i;

  cout<<"Input Num Yal : ";
  cin>>m;
  struct krus edge[20];

  for (int i=0;i<m;i++)
  {
   cout<<" Num V1 : ";  cin>>edge[i].v1;
   cout<<" Num V2 : ";  cin>>edge[i].v2;
   cout<<" Weight : ";  cin>>edge[i].weight;
  
  }
   cout<<"\nWeight Is : "<<perim(set,edge,n,m);
  getch();
}
//***********************************************
int perim(int set[],struct krus edge[],int n,int m)
{
 int fe=0;
 int p=0;
 struct krus e;
 while (fe<n-1)
 {
  
  int y=0;
  e.weight=0;
  for (int i=0;i<m;i++){
    if ((set[edge[i].v1]==0 && set[edge[i].v2]!=0) || (set[edge[i].v2]==0 && set[edge[i].v1]!=0))
       {
	 if(y==0)
	   {
	     e=edge[i];
	     y++;
	    }
	 else{
	    if (e.weight>edge[i].weight)
		e=edge[i];}
       }}
   //**********************************
  if (y!=0)
  {
   p+=e.weight;
   cout<<"("<<e.v1<<","<<e.v2<<") => W :"<<e.weight<<"\t";
   set[e.v1]=0;
   set[e.v2]=0;
   fe++;
  }
   else
      break;
  }
  
  return p;

}

What's a nod?
What's an edge?
What's a weight?
What the **** are you asking?

Treat us as if we aren't in your class and didn't hear the instructions...

What's a nod?
What's an edge?
What's a weight?

Nod is obviously a typo for node, and all of those terms are well known in graph theory, as is Prim's algorithm. I don't think the OP is expected to explain concepts that should be common knowledge, and I'm assuming you're intentionally acting dense, because your post history suggests a strong foundation in computer science.

I definitely agree with you that the OP is being lazy with this question, but I don't think it's due to not explaining what a graph is. ;)

What the **** are you asking?

He wants us to debug the program and tell him why perim() always returns 0. To which I'd ask what results he got from running the algorithm through a debugger. I'd also ask for a set of test data that produces the wrong result along with the expected result instead of just throwing the code out and hoping any helpers are interested enough to set up a realistic graph for testing.

On an amusing side note, it looks like this program is a translation of Kruskal's algorithm into Prim's algorithm, judging by the krus structure. So it's possible that the algorithm is a bastardization of both and unlikely to work at all, but I didn't look at it closely, that's just speculation. :)

commented: I'm not asking you... -4
commented: Well said, sir. +4

Nod is obviously a typo for node, and all of those terms are well known in graph theory, as is Prim's algorithm. I don't think the OP is expected to explain concepts that should be common knowledge, and I'm assuming you're intentionally acting dense, because your post history suggests a strong foundation in computer science.

I definitely agree with you that the OP is being lazy with this question, but I don't think it's due to not explaining what a graph is. ;)


He wants us to debug the program and tell him why perim() always returns 0. To which I'd ask what results he got from running the algorithm through a debugger. I'd also ask for a set of test data that produces the wrong result along with the expected result instead of just throwing the code out and hoping any helpers are interested enough to set up a realistic graph for testing.

On an amusing side note, it looks like this program is a translation of Kruskal's algorithm into Prim's algorithm, judging by the krus structure. So it's possible that the algorithm is a bastardization of both and unlikely to work at all, but I didn't look at it closely, that's just speculation. :)

these are inputs:
(1,2) weight:5
(1,4) weight:6
(1,3) weight:7
(2,4) weight:5
(2,7) weight:5
(2,6) weight:4
(3,4) weight:2
(3,5) weight:2
(4,6) weight:2
(4,5) weight:3
(6,7) weight:3
(5,6) weight:1
(5,7) weight:3

and the correct answer is 17!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.