Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:42:39+00:00 2026-05-26T12:42:39+00:00

i have following code from graph theory topics,kruskal algorithm for minimal spanning tree #include<iostream>

  • 0

i have following code from graph theory topics,kruskal algorithm for minimal spanning tree

#include<iostream>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p;
int main(){
    int dup1,dup2;
    cout<<" enter number of vertices "<<endl;
    cin>>n;
    cout<<"enter number of edges "<<endl;
    cin>>m;
    cout<<" EDGES  Cost "<<endl;
    for(k=1;k<=m;k++){
        cin>>i>>j>>c;
        cost[i][j]=c;
        cost[j][i]=c;

    }
    for(i=1;i<=n;i++)

        for(j=1;j<=m;j++)

            if(cost[i][j]==0)
                cost[i][j]=31999;
            visit=1;
            while(visit<n){
                v=31999;
                for(i=1;i<=n;i++)
                    for(j=1;j<=n;j++)
    if(cost[i][j]!=31999 && cost[i][j]<v && cost[i][j]!=-1)
    {
        int count=0;
        for(p=1;p<=n;p++){


            if(visited[p]==i || visited[p]==j)
                count++;
        }
        if(count>=2){

            for(p=1;p<=n;p++)
                if(cost[i][p]!=31999 && p!=j)
                    dup1=p;
             for(p=1;p<=n;p++)
                 if(cost[j][p]!=31999 && p!=i)
                     dup2=p;
            if(cost[dup1][dup2]==-1)
                continue;
        }
         l=i;
         k=j;
         v=cost[i][j];

        }

        cout<<" edgde from "<<l<<" --> "<<k;
        cost[l][k]=-1;
        cost[k][l]=-1;
        visit++;
        int count=0;
        count1=0;
        for(i=1;i<=n;i++)
        {

            if(visited[i]==1)
                count++;
            if(visited[i]==k)
                count1++;

        }
        if(count==0)
            visited[++vst]=1;
         if(count1==0)
             visited[++vst]=k;

        }


return 0;
}

which works and for following input

1 2 1
2 3 2
3 4 3
1 3 3

where number of vertices and number of edges is 4,gives me output like this

edge from 1–>2edge from 2–>3edge from 1–>3

my question is is there any semantical error?also i have used arrays and many variables,can i make shorted this code?my (programming)language is c++

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:42:40+00:00Added an answer on May 26, 2026 at 12:42 pm

    User input can easily break this code: If the user gives values equal to or greater than 10 for n or m, you could run off the end of your array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <iostream> class Grandma { public: virtual void foo()
I have the following code: from selenium import selenium selenium = selenium(localhost, 4444, *chrome,
I have got the following code from here to read an Excel file using
I have the following code (inherited from someone): <div class=feedback>&nbsp;</div> <form onsubmit=return false autocomplete=off>
So I have the following code: return from a in DBContext.Acts join artist in
I have the following code import smtplib from email.mime.text import MIMEText smtpserver = 'smtp.gmail.com'
I have the following piece of code from a book. There is this function
I have the following code for adding to/extracting from Zip. I'm trying to refactor
I have the following piece of code taken from the PHP manual on the
Hi I have following XAML code which is the output from XamlWriter.Save(): <StackPanel Name=itemStack

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.