Code learned this week (depth-of-the-span-first traversal)

Source: Internet
Author: User

Depth-First traversal code:

#include "stdafx.h"

#include <stdio.h>

#include <string.h>

#define MVNUM// maximum number of vertices

typedef int INFOTYPE; the weight type of the edge

typedef char* VEXTYPE; vertex data type

typedef struct linknode{// Edge junction

int Adjvex; The position of the vertex to which the edge is pointing

struct Linknode * NEXT; pointer to next edge

InfoType info; and Edge-related information

}linknode;

typedef struct vexnode{

Vextype data; Vertex Information

Linknode * FIRSTADJ; Pointer to the first edge attached to the vertex

}vexnode; Vexnode represents the adjacency table type

typedef struct{

Vexnode Adjlist[mvnum]; adjacency Table

int Vexnum, arcnum; the current number of vertices and the number of edges of the graph

} adjlist;//,adjgraph;

int visited[mvnum];// is initially zero, indicating that this vertex has not been accessed

void DFS (Adjlist *pg,int i) {

Int J;

visited[i]=1;// setting vertex access flags

printf ("%s", pg->adjlist[i].data);// output vertex data

Linknode *pnode=pg->adjlist[i].firstadj;//The edge of the connection to which the vertex is taken

while (Pnode!=null)

{j=pnode->adjvex;// the vertex adjacent to the vertex

pnode=pnode->next;//the next connected edge of the vertex

if (visited[j]==1)// Whether the vertex has been accessed

continue;// has visited

DFS (PG,J);// not accessed, recursive call accesses its adjacency vertex

}

}

int main (int argc, char* argv[])
{printf ("depth-first search algorithm for adjacency table storage structure \ n");
Adjlist ag;//non-direction diagram
ag.vexnum=9;
Ag.adjlist[0].data= "V0";
Ag.adjlist[1].data= "V1"; Ag.adjlist[2].data= "V2";
Ag.adjlist[3].data= "V3"; Ag.adjlist[4].data= "V4";
Ag.adjlist[5].data= "V5"; Ag.adjlist[6].data= "V6";
Ag.adjlist[7].data= "V7"; Ag.adjlist[8].data= "V8";
int v0=0,v1=1,v2=2,v3=3,v4=4,v5=5,v6=6,v7=7,v8=8;
Linknode V12={v2,null},v21={v1,null},v13={v3,null},v31={v1,null},
V24={v4,null},v42={v2,null},v25={v5,null},v52={v2,null},
V37={v7,null},v73={v3,null},v36={v6,null},v63={v3,null},
V48={v8,null},v84={v4,null},v58={v8,null},v85={v5,null},
V67={v7,null},v76={v6,null};
Ag.adjlist[v0].firstadj=null; ag.adjlist[v1].firstadj=&v12;
ag.adjlist[v2].firstadj=&v21;ag.adjlist[v3].firstadj=&v31;
ag.adjlist[v4].firstadj=&v42;ag.adjlist[v5].firstadj=&v52;
ag.adjlist[v6].firstadj=&v63;ag.adjlist[v7].firstadj=&v73;
ag.adjlist[v8].firstadj=&v84;
v12.next=&v13;
v21.next=&v24; v24.next=&v25;
v31.next=&v36; v36.next=&v37;
v42.next=&v48;v52.next=&v58;
v63.next=&v67;v73.next=&v76;
v84.next=&v85;
Bfstraverse (&ag,4); Dfstraverse (&ag,4);

Adjlist ag2;//Map
ag2.vexnum=9;
Ag2.adjlist[0].data= "V0";
Ag2.adjlist[1].data= "V1"; Ag2.adjlist[2].data= "V2";
Ag2.adjlist[3].data= "V3"; Ag2.adjlist[4].data= "V4";
Ag2.adjlist[5].data= "V5"; Ag2.adjlist[6].data= "V6";
Ag2.adjlist[7].data= "V7"; Ag2.adjlist[8].data= "V8";
Linknode vv12={v2,null},vv13={v3,null}, Vv24={v4,null}, Vv36={v6,null},
Vv48={v8,null}, Vv67={v7,null}, Vv85={v5,null};
Ag2.adjlist[v0].firstadj=null;
ag2.adjlist[v1].firstadj=&vv12;ag2.adjlist[v2].firstadj=&vv24;
ag2.adjlist[v3].firstadj=&vv36;ag2.adjlist[v4].firstadj=&vv48;
ag2.adjlist[v5].firstadj=null;ag2.adjlist[v6].firstadj=&vv67;
ag2.adjlist[v7].firstadj=null;ag2.adjlist[v8].firstadj=&vv85;
vv12.next=&vv13;
Bfstraverse (&ag2,4);//dfstraverse (&ag2,4);
return 0;
}

void Dfstraverse (Adjlist *pg,int k) {// start traversal from k -vertex

int i;

for (i=0;i<pg->vexnum;i++)

visited[i]=0; initial setup Each vertex has not been accessed

I=k;

while (1) {

if (visited[i]==0)// i Vertex visited no

DFS (Pg,i); is not accessed, this vertex is accessed

i++; accessing the next vertex

if (i>=pg->vexnum)// arrive at the end of the array , start from zero and continue accessing

{i=0;

}

if (i==k) break; return to the original vertex position, the end of the visit

}

printf ("\ n");

}

Breadth-First Traversal code:

Graph Breadth _ adjacency table . Cpp:defines the entry point for the console application.

//

#include "stdafx.h"

#include "stdafx.h"

#include <stdio.h>

#define MVNUM// maximum number of vertices

typedef int INFOTYPE; the weight type of the edge

typedef char* VEXTYPE; vertex data type

#define CHAR[10] Vextype; vertex data type

typedef struct linknode{// Edge junction

int Adjvex; The position of the vertex to which the edge is pointing

struct Linknode * NEXT; pointer to next edge

InfoType info; and Edge-related information

}linknode;

typedef struct vexnode{

Vextype data; Vertex Information

Linknode * FIRSTADJ; Pointer to the first edge attached to the vertex

}vexnode; Vexnode represents the adjacency table type

typedef struct{

Vexnode Adjlist[mvnum]; adjacency Table

int Vexnum, arcnum; the current number of vertices and the number of edges of the graph

} adjlist;//,adjgraph;

/****************************************/

#define MAX_QUEUE mvnum/* maximum number of data elements in the queue * /

typedef int ELEMTYPE;

typedef struct QUEUE/* assumes that the queue is full when there is only one unit left in the array * /

{

Elemtype Elem[max_queue]; /* storage unit for storing data elements in the queue * /

int front,rear; /* Team head hands, tail hands * /

}queue;

1) Initialize queue Q

void Initqueue (QUEUE *q)

{q->front=0;

q->rear=0;

}

5) determine if the queue Q is empty

int Queueempty (QUEUE Q)

{

if (q.front==q.rear) return 1;// header pointer is the same, indicating that the queue is empty

else return 0;

}

2) Queue up

void EnQueue (QUEUE *q,elemtype elem)

{if (q->rear+1)%max_queue==q->front)// Determine if the queue is full,

printf ("Queue is full.\n");// full, you cannot queue, launch

else {q->elem[q->rear]=elem;// not full, data queued

Q->rear= (q->rear+1)%max_queue; The tail of the team plus a

}

}

3) out of the team

Elemtype DeQueue (QUEUE*Q)

{Elemtype e=-1;

if (Queueempty (*q)) printf ("Queue is empty.\n");// queue is empty, cannot be out,

Else

{

e=q->elem[q->front]; queue is not empty, the data to be deleted is saved to Elem

Q->front= (q->front+1)%max_queue;// team head pin plus one

}

return e;

}

int Visited[mvnum];

void BFS (Adjlist *pg,int i) {

int j,k;

QUEUE Q;

Initqueue (&q);// Initialize queue

visited[i]=1;// Setting this vertex has been accessed

printf ("%s", pg->adjlist[i].data);// Output this vertex data

EnQueue (&q,i);// this vertex into the pair

Linknode *pnode;

while (! Queueempty (q)) {// queue is empty then end

K=dequeue (&Q); vertex data out of

pnode=pg->adjlist[k].firstadj;// Gets the edge of this vertex

while (Pnode!=null)

{j=pnode->adjvex;// gets the adjacent vertices of the current vertex

if (visited[j]==0)// This neighboring vertex has access no

{visited[j]=1;// not visited, set as visited

printf ("%s", Pg->adjlist[j]);// output data for adjacent vertices

EnQueue (&Q,J);// queue of adjacent vertices

}

pnode=pnode->next;// gets the edge connected to the current vertex

}

}

}

void Bfstraverse (Adjlist *pg,int k) {// start Access from k -vertex

int i;

for (i=0;i<pg->vexnum;i++)

visited[i]=0;//initial setup Each vertex has not been accessed

I=k;

while (1) {

if (visited[i]==0)//first Vertex visited no

BFS (pg,i);// access to this vertex is not accessed

i++;// accessing the next vertex

if (i>=pg->vexnum)// arrive at the end of the array , start from zero and continue accessing

{

i=0;

}

if (i==k) break; return to the original vertex position, the end of the visit

}

printf ("\ n");

}

int main (int argc, char* argv[])
{printf ("The breadth-first search algorithm for adjacency table storage structure \ n");
Adjlist ag;//non-direction diagram
ag.vexnum=9;
Ag.adjlist[0].data= "V0";
Ag.adjlist[1].data= "V1"; Ag.adjlist[2].data= "V2";
Ag.adjlist[3].data= "V3"; Ag.adjlist[4].data= "V4";
Ag.adjlist[5].data= "V5"; Ag.adjlist[6].data= "V6";
Ag.adjlist[7].data= "V7"; Ag.adjlist[8].data= "V8";
int v0=0,v1=1,v2=2,v3=3,v4=4,v5=5,v6=6,v7=7,v8=8;
Linknode V12={v2,null},v21={v1,null},v13={v3,null},v31={v1,null},
V24={v4,null},v42={v2,null},v25={v5,null},v52={v2,null},
V37={v7,null},v73={v3,null},v36={v6,null},v63={v3,null},
V48={v8,null},v84={v4,null},v58={v8,null},v85={v5,null},
V67={v7,null},v76={v6,null};
Ag.adjlist[v0].firstadj=null; ag.adjlist[v1].firstadj=&v12;
ag.adjlist[v2].firstadj=&v21;ag.adjlist[v3].firstadj=&v31;
ag.adjlist[v4].firstadj=&v42;ag.adjlist[v5].firstadj=&v52;
ag.adjlist[v6].firstadj=&v63;ag.adjlist[v7].firstadj=&v73;
ag.adjlist[v8].firstadj=&v84;
v12.next=&v13;
v21.next=&v24; v24.next=&v25;
v31.next=&v36; v36.next=&v37;
v42.next=&v48;v52.next=&v58;
v63.next=&v67;v73.next=&v76;
v84.next=&v85;
Dfstraverse (&ag,4); Dfstraverse (&ag,4);

Adjlist ag2;//Map
ag2.vexnum=9;
Ag2.adjlist[0].data= "V0";
Ag2.adjlist[1].data= "V1"; Ag2.adjlist[2].data= "V2";
Ag2.adjlist[3].data= "V3"; Ag2.adjlist[4].data= "V4";
Ag2.adjlist[5].data= "V5"; Ag2.adjlist[6].data= "V6";
Ag2.adjlist[7].data= "V7"; Ag2.adjlist[8].data= "V8";
Linknode vv12={v2,null},vv13={v3,null}, Vv24={v4,null}, Vv36={v6,null},
Vv48={v8,null}, Vv67={v7,null}, Vv85={v5,null};
Ag2.adjlist[v0].firstadj=null;
ag2.adjlist[v1].firstadj=&vv12;ag2.adjlist[v2].firstadj=&vv24;
ag2.adjlist[v3].firstadj=&vv36;ag2.adjlist[v4].firstadj=&vv48;
ag2.adjlist[v5].firstadj=null;ag2.adjlist[v6].firstadj=&vv67;
ag2.adjlist[v7].firstadj=null;ag2.adjlist[v8].firstadj=&vv85;
vv12.next=&vv13;
Dfstraverse (&ag2,4);//dfstraverse (&ag2,4);
return 0;
}

I really have to say that sometimes the code will be dizzy, pointing to the machine class today, just did not hit the code to complete

Code learned this week (depth-of-the-span-first traversal)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.