| |

VerySource

 Forgot password?
 Register
Search
View: 737|Reply: 3

Boss, do me a favor, it can't be done ~~

[Copy link]

2

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-24 15:00:01
| Show all posts |Read mode
The graph tour in the data structure can be stored using adjacency matrix storage and adjacency list storage. You can use breadth-first and depth-first algorithms when traveling around. Thank you all for my urgent need.
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-27 00:00:01
| Show all posts
[code=C/C++]#define M 20
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
/*Definition map*/
typedef struct{
  int V[M];
  int R[M][M];
  int vexnum;
}Graph;

/*Create a picture*/
void creatgraph(Graph *g,int n)
{
  int i,j,r1,r2;
  g->vexnum=n;
  /*Vertex is represented by i*/
  for(i=1;i<=n;i++)
  {
    g->V[i]=i;
  }
  /*Initialize R*/
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    {
      g->R[i][j]=0;
    }
  /*Enter R*/
  printf("Please input R(0,0 END):\n");
  scanf("%d,%d",&r1,&r2);
  while(r1!=0&&r2!=0)
   {
    g->R[r1][r2]=1;
    g->R[r2][r1]=1;
    scanf("%d,%d",&r1,&r2);
   }
}

/*Print the adjacency matrix of the graph*/
void printgraph(Graph *g)
{
int i,j;
for(i=1;i<=g->vexnum;i++)
{for(j=1;j<=g->vexnum;j++)
   {
     printf("%2d ",g->R[i][j]);
   }
   printf("\n");
   }
}
/*Global variables: access to the flag array*/
int visited[M];
/*Visit Vertex*/
void visitvex(Graph *g,int vex)
{
printf("%d ",g->V[vex]);
}

/*Get the first unvisited adjacent node*/
int firstadjvex(Graph *g,int vex)
{
int w,i;
for(i=1;i<=g->vexnum;i++)
  {
   if(g->R[vex][i]==1&&visited[i]==0)
    {
      w=i;
      break;
    }
   else
    {
      w=0;
    }
  }
  return w;
}
/*Get the next unvisited adjacent node (deep traversal)*/
int nextadjvex(Graph *g,int vex,int w)
{
  int t;
  t=firstadjvex(g,w);
  return t;
}
/*Deep recursive traversal*/
  void dfs(Graph *g,int vex)
   {
    int w;
    visited[vex]=1;
    visitvex(g,vex);
    for(w=firstadjvex(g,vex);w>0;w=nextadjvex(g,vex,w))
      if(!visited[w])
       {
dfs(g,w);
       }
   }

   void dfstraverse(Graph *g)
   {
     int i;
     for(i=1;i<=g->vexnum;i++)
       visited[i]=0;
     for(i=1;i<=g->vexnum;i++)
       if(!visited[i])
         {dfs(g,i);}
   }
/*Define the queue*/
typedef struct{
int V[M];
int front;
int rear;
}Queue;
/*Initialize the queue*/
initqueue(Queue *q)
{
  q->front=0;
  q->rear=0;
}
/* Determine whether the queue is empty*/
int quempty(Queue *q)
{
  if(q->front==q->rear)
  {
   return 0;
  }
  else
  {
   return 1;
  }
}
/*Entry operation*/
enqueue(Queue *q,int e)
{
  if((q->rear+1)%M==q->front)
   {
    printf("The queue is overflow!\n");
    return 0;
   }
   else
   {
    q->V[q->rear]=e;
    q->rear=(q->rear+1)%M;
    return 1;
   }
}
/*Dequeue operation*/
dequeue(Queue *q)
{
  int t;
  if(q->front==q->rear)
   {
     printf("The queue is empty!\n");
     return 0;
   }
   else
   {
     t=q->V[q->front];
     q->front=(q->front+1)%M;
     return t;
   }
}
/*Breadth traversal*/
void BESTraverse(Graph *g)
{
  int i;
  Queue *q=(Queue *)malloc(sizeof(Queue));
  for(i=1;i<=g->vexnum;i++)
  {
   visited[i]=0;
  }
  initqueue(q);
  for(i=1;i<=g->vexnum;i++)
  {
    if(!visited[i])
     {
       visited[i]=1;
       visitvex(g,g->V[i]);
       enqueue(q,g->V[i]);
       while(!quempty(q))
        {
          int u,w;
          u=dequeue(q);
          for(w=firstadjvex(g,u);w>0;w=nextadjvex(g,u,w))
            {
              if(!visited[w])
               {
                 visited[w]=1;
   visitvex(g,w);
                 enqueue(q,w);
               }
            }
        }
     }
  }
}
/*Main program*/
main()
{
  int n;
  Graph *g=(Graph *)malloc(sizeof(Graph));
  char menu;
  printf("Please input the number of vertex:\n");
  scanf("%d",&n);
  creatgraph(g,n);
  printf("This is the linjiejuzhen of graph:\n");
  printgraph(g);
  input:
  printf("Please input b or d or q ,Breadth_first: b Depth_first: d quit: q\n");
  while((menu=getchar())=='\n');
  if(menu=='b')
    {
      printf("Breadth_first:\n");
      BESTraverse(g);
      printf("\n");
      goto input;
    }
  else if(menu=='d')
    {
      printf("Depth_first:\n");
      dfstraverse(g);
      printf("\n");
      goto input;
    }
  else if(menu=='q')
    {
     exit(0);
    }
  else
    {
      printf("Input error!Please input b or d!\n");
    }
}

[/code]
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-27 00:15:01
| Show all posts
I also got the code, ha ha. Not original.
See you in a hurry, get one for you.
I really don't want to use my brain today. Hehe.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-27 00:45:01
| Show all posts
Bangding
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list