4. Write a function to get the number of vertices in an undirected graph and its edges. You may assume thatno edge is input twice. i.Use adjacency list representation of the graph and find runtime of the functionii.Use adjacency matrix representation of the graph and find runtime of the function

#include<iostream>
using namespace std;

class node 
{
public:
 char data;
 node *start = NULL;
 node *next;
 
 void list(char ch)
 {
  node *ptr = new node;
  node *temp = new node;
  ptr->data = ch;
  if(start == NULL)
  {
   start = ptr;
   start->next = NULL;
  }
  else
  {
   temp = start;
   while(temp->next != NULL)
   {
    temp = temp->next;
   }
   ptr->next = NULL;
   temp->next = ptr;
   
  }
 }
 void displaylist()
 {
  node *temp = new node;
  if(start == NULL)
  {
   cout<<"LIST IS EMPTY"<<endl;
  }
  else
  {
   temp = start;
   while(temp != NULL)
   {
    cout<<temp->data<<"->";
    temp = temp->next;
   }
   cout<<endl;
  }
 }
      
  
};
  
class graph
{
 int a[10][10];
 char arr[5] = {'A','B','C','D','E'};
 int vertices;
 node al[5];
public:
 graph()
 {
  cout<<"ENTER THE NUMBER OF VERTICES IN THE GRAPH \t::\t";
  cin>>vertices;
 }
 
 void getdata()
 {
  cout<<"ADJANCY MATRIX ::"<<endl;
  for(int i = 0 ; i < vertices ; i++)
  {
   for(int j = 0 ; j<vertices ; j++)
   {
    cout<<"CONNECTIVITY BETWEEN "<<arr[i]<<" and "<<arr[j]<<" :: ";
    cin>>a[i][j];
   }
  }
 }
 
 void display()
 {
  cout<<"::::ADJANCY MATRIX::::"<<endl;
  for(int i = 0 ; i < vertices ; i++)
  {
   for(int j = 0 ; j<vertices ; j++)
   {
    
    cout<<a[i][j]<<" ";
   }
   cout<<endl;
  }
 }
 
 void calllist()
 {
  node n[5];
  for(int i = 0 ; i<vertices ; i++)
  {
   n[i].list(arr[i]);
  }
  for(int i = 0 ; i < vertices ; i++)
  {
   for(int j = 0 ; j<vertices ; j++)
   {
    if(a[i][j] == 1 && i!=j)
    {
     n[i].list(arr[j]);
    }
   }
  }
  for(int i = 0 ; i<vertices ; i++)
  {
   n[i].displaylist();
  }
 }
 
};

int main()
{
 graph am;
 am.getdata();
 am.display();
 am.calllist();
 return 0;
 }
   

Comments

Popular posts from this blog

2. Beginning with an empty binary search tree, Construct binary search tree by inserting the values in the order given. After constructing a binary tree -i.Insert new nodeii.Find number of nodes in longest path iii.Minimum data value found inthe tree iv.Change a tree so that the roles of the left and right pointers are swapped at every node v.Search a value

1. A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method.

6. Implement all the functions of a dictionary (ADT) using hashing.Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must be uniqueStandard Operations: Insert(key, value), Find(key), Delete(key)