Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Quote:
The weight is number of nodes in reference of the node

 1:   C+←  //vertex cover set which is empty initially.
 2:   L=List
 3:   L[w] = weight
 4:   (h, v) = highest weight of the list and respective vertex
 5:   if h≠ 0 thens
 6:     C+← C+ ∪ v
 7:     L[v] ← 0
 8:     for all vertices of List L[ref]   {v} do
 9:        L[v]← L[v]-1
 10:    end for
 11:    go to 4
12:    else 
13:     return C+
14:    end if


What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>
#define new_node (struct node*)malloc(sizeof(struct node))
struct node
{
    int vertex;
    struct node *next;
};

void main()
{
    int option;
    clrscr();
    do
    {
	printf("\n A Program to represent a Graph by using an Adjacency List \n ");
	printf("\n 1. Directed Graph ");
	printf("\n 2. Un-Directed Graph ");
	printf("\n 3. Exit ");
	printf("\n\n Select a proper option : ");
	scanf("%d", &option);
	switch(option)
	{
	    case 1 : dir_graph();
		     break;
	    case 2 : undir_graph();
		     break;
	    case 3 : exit(0);
	}
    }while(1);
}
int dir_graph()
{
    struct node *adj_list[10], *p;
    int n,w;
    int in_deg, out_deg, i, j;
    printf("\n How Many Vertices ? : ");
    scanf("%d", &n);
    for( i = 1 ; i <= n ; i++ )
	adj_list[i] = NULL;
    read_graph (adj_list, n);
    printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ");
    for (i = 1; i <= n ; i++ )
    {
	in_deg = out_deg = 0;
	p = adj_list[i];
	while( p != NULL )
	{
	    out_deg++;
	    p = p -> next;
	}
	for ( j = 1 ; j <= n ; j++ )
	{
	    p = adj_list[j];
	    while( p != NULL )
	    {
		if ( p -> vertex == i )
		   in_deg++;
		p = p -> next;
		}
			     }
	     printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n", i, in_deg, out_deg, in_deg + out_deg);
	  w = in_deg + out_deg;
	 
	}
    return;
}
int undir_graph()
{
    struct node *adj_list[10], *p;
    int deg, i, j, n,w[100],l;
    printf("\n How Many Vertices ? : ");
    scanf("%d", &n);
    for ( i = 1 ; i <= n ; i++ )
	adj_list[i] = NULL;
    read_graph(adj_list, n);
    printf("\n Vertex \t Degree ");
    for ( i = 1 ; i <= n ; i++ )
    {
	deg = 0;
	p = adj_list[i];
	while( p != NULL )
	{
	    deg++;
	    p = p -> next;
	}
	printf("\n\n %5d \t\t %d\n\n", i, deg);
	w[i]=deg;
    }
    for(i=1;i<=n;i++)
     printf("\nweight of node %d = %d",i,w[i]);
     l=0;
     for(i=1;i<=n;i++)
     {
       if(l<w[i])
	l=w[i];
	maxwt=i;
     }
     printf("\nhighest weight of list = %d",l);
    /* if(l!=0)
     {
	c=c+maxwt;
	w[i]=0;
	for(i=0;i<=n;i++)
     */	
 
    return;
}
int read_graph ( struct node *adj_list[10], int n )
{
    int i, j;
    int reply;
    struct node *p, *c;
    for ( i = 1 ; i <= n ; i++ )
    {
	for ( j = 1 ; j <= n ; j++ )
	{
	    if ( i == j )
		continue;
	    printf("\n Vertices %d & %d are Adjacent ? (1 for yes /2 for no) :", i, j);
	    scanf("%d", &reply);
	    if ( reply == 1)
	    {
		c = new_node;
		c -> vertex = j;
		c -> next = NULL;
		if ( adj_list[i] == NULL )
		    adj_list[i] = c;
		else
		{
		    p = adj_list[i];
		    while ( p -> next != NULL )
			p = p -> next;
		    p -> next = c;
		}
	    }
	}



    }
    return;
}
Posted
Updated 22-Mar-18 12:46pm
v4
Comments
Richard MacCutchan 18-Mar-18 12:13pm    
What is the question?
KarstenK 18-Mar-18 13:55pm    
You should learn to use the debugger. Download Visual Studio and watch some tutorial for details. No one will debug your coding homework.

If you want to store some data for later use the vector container is a good choice. Depending on your task you may use a class with sub vectors.

Providing a pointer is only half the job - you also need some memory for the data.

C++
struct node *p;//only a pointer
p = new node;//allocate buffer of node (use new is better)
memset( p, 0, sizeof(node) );//clean memory
// ...usage of p
p->vertext = 1;

delete p;//deallocate memory
Pay close attention to some clear memory managment!!! I isnt easy.

Here is a tutorial how to use the debugger.
 
Share this answer
 
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <stdio.h>
#include <stdlib.h>
#define new_node (struct node*)malloc(sizeof(struct node))
struct node
{
  int vertex;
  struct node *next;
};

void main()
{
  int option;
  clrscr();
  do
  {
    printf("\n A Program to represent a Graph by using an Adjacency List \n ");
    printf("\n 1. Directed Graph ");
    printf("\n 2. Un-Directed Graph ");
    printf("\n 3. Exit ");
    printf("\n\n Select a proper option : ");
    scanf("%d", &option);
    switch(option)
    {
    case 1 : dir_graph();
      break;
    case 2 : undir_graph();
      break;
    case 3 : exit(0);
    }
  }while(1);
}
int dir_graph()
{
  struct node *adj_list[10], *p;
  int n,w;
  int in_deg, out_deg, i, j;
  printf("\n How Many Vertices ? : ");
  scanf("%d", &n);
  for( i = 1 ; i <= n ; i++ )
    adj_list[i] = NULL;
  read_graph (adj_list, n);
  printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree ");
  for (i = 1; i <= n ; i++ )
  {
    in_deg = out_deg = 0;
    p = adj_list[i];
    while( p != NULL )
    {
      out_deg++;
      p = p -> next;
    }
    for ( j = 1 ; j <= n ; j++ )
    {
      p = adj_list[j];
      while( p != NULL )
      {
        if ( p -> vertex == i )
          in_deg++;
        p = p -> next;
      }
    }
    printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n", i, in_deg, out_deg, in_deg + out_deg);
    w = in_deg + out_deg;

  }
  return;
}
int undir_graph()
{
  struct node *adj_list[10], *p;
  int deg, i, j, n,w[100],l;
  printf("\n How Many Vertices ? : ");
  scanf("%d", &n);
  for ( i = 1 ; i <= n ; i++ )
    adj_list[i] = NULL;
  read_graph(adj_list, n);
  printf("\n Vertex \t Degree ");
  for ( i = 1 ; i <= n ; i++ )
  {
    deg = 0;
    p = adj_list[i];
    while( p != NULL )
    {
      deg++;
      p = p -> next;
    }
    printf("\n\n %5d \t\t %d\n\n", i, deg);
    w[i]=deg;
  }
  for(i=1;i<=n;i++)
    printf("\nweight of node %d = %d",i,w[i]);
  l=0;
  for(i=1;i<=n;i++)
  {
    if(l<w[i])
    l=w[i];
    maxwt=i;
  }
  printf("\nhighest weight of list = %d",l);
  /* if(l!=0)
  {
  c=c+maxwt;
  w[i]=0;
  for(i=0;i<=n;i++)
  */

  return;
}
int read_graph ( struct node *adj_list[10], int n )
{
  int i, j;
  int reply;
  struct node *p, *c;
  for ( i = 1 ; i <= n ; i++ )
  {
    for ( j = 1 ; j <= n ; j++ )
    {
      if ( i == j )
        continue;
      printf("\n Vertices %d & %d are Adjacent ? (1 for yes /2 for no) :", i, j);
      scanf("%d", &reply);
      if ( reply == 1)
      {
        c = new_node;
        c -> vertex = j;
        c -> next = NULL;
        if ( adj_list[i] == NULL )
          adj_list[i] = c;
        else
        {
          p = adj_list[i];
          while ( p -> next != NULL )
            p = p -> next;
          p -> next = c;
        }
      }
    }
  }
  return;
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

-----
Do you have a question with this code ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900