Design your algorithm in a pseudocode! (PS: use greedy algorithm) ___________________________________________________________________________ #include #define max 6 int count = 0, countSpan = 0, cost = 0; struct ed{ int pq, zx; int value; }; ed list[max]; ed spanning[max]; int graph[max][max], n; int find(int *belong, int node) { return(belong[node]); } void sort(){ ed temp; for (int i = 1; i < count; i++) { for (int j = 0; j < count - 1; j++) { if (list[j].value > list[j+1].value) { temp = list[j]; list[j] = list[j+1]; list[j + 1] = temp; } } } } void kl(){ int belong[max], node1, node2; for(int i = 1; i < max; i++){ for(int j = 0; j < i; j++) { if(graph[i][j] != 0){ list[count].pq = i; list[count].zx = j; list[count].value = graph[i][j]; count++; } } } sort(); for(int i = 0; i < max; i++) { belong[i] = i; } for(int i = 0; i < max; i++){ node1 = find(belong, list[i].pq); node2 = find(belong, list[i].zx); if(node1 != node2){ spanning[countSpan] = list[i]; cost += spanning[countSpan].value; countSpan++; for(int i = 0; i < max; i++) { if(belong[i] == node2){ belong[i] = node1; } } } } } void print(){ printf("%d\n", cost); for (int i = 0; i < countSpan; i++){ printf("%c - %c : %d\n", spanning[i].zx+65, spanning[i].pq+65, spanning[i].value); cost = cost + spanning[i].value; } } int main() { char ot, ds; int value; for(int i = 0; i < 12; i++) { scanf("%c", &ot); getchar(); scanf("%c", &ds); getchar(); scanf("%d", &value); getchar(); list[i].value = value; graph[ot-'A'][ds-'A'] = graph[ds-65][ot-65] = value; } kl(); print(); return 0; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Design your algorithm in a pseudocode! (PS: use greedy algorithm)

___________________________________________________________________________

#include <stdio.h>
#define max 6

int count = 0, countSpan = 0, cost = 0;

struct ed{
    int pq, zx;
    int value;
};

ed list[max];
ed spanning[max];
int graph[max][max], n;

int find(int *belong, int node)
{
    return(belong[node]);
}

void sort(){
  ed temp;
  for (int i = 1; i < count; i++)
  {
    for (int j = 0; j < count - 1; j++)
    {
      if (list[j].value > list[j+1].value) 
      {
        temp = list[j];
        list[j] = list[j+1];
        list[j + 1] = temp;
      }
  }
  }
}

void kl(){
    int belong[max], node1, node2;
    for(int i = 1; i < max; i++){
        for(int j = 0; j < i; j++)
        {
      if(graph[i][j] != 0){
              list[count].pq = i;
              list[count].zx = j;
              list[count].value = graph[i][j];
              count++;
      }
        }
    }

    sort();

  for(int i = 0; i < max; i++)
  {
    belong[i] = i;
  }
  
    for(int i = 0; i < max; i++){
        node1 = find(belong, list[i].pq);
        node2 = find(belong, list[i].zx);
        if(node1 != node2){
            spanning[countSpan] = list[i];
            cost += spanning[countSpan].value;
            countSpan++;
            for(int i = 0; i < max; i++)
            {
            if(belong[i] == node2){
                belong[i] = node1;
            }
        } 
        }
    }
}

void print(){
  printf("%d\n", cost);
  for (int i = 0; i < countSpan; i++){
      printf("%c - %c : %d\n", spanning[i].zx+65, spanning[i].pq+65, spanning[i].value);
      cost = cost + spanning[i].value;
  }
}

int main()
{
  char ot, ds;
  int value;
  for(int i = 0; i < 12; i++)
  {
    scanf("%c", &ot);
    
    getchar();
    scanf("%c", &ds);
    
    getchar();
    scanf("%d", &value);
    
    getchar();
    list[i].value = value;
    
    graph[ot-'A'][ds-'A'] = graph[ds-65][ot-65] = value;
  }

  kl();
  print();
    return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY