8649 graph traversal breadth

Original link: http://www.cnblogs.com/arcfat/archive/2012/12/11/2813700.html

8649 graph traversal breadth

Time limit: 1000MS Memory Limit: 1000K

Questions: Programming language title: Unlimited

 

Description

FIG adjacent table storage structure and basic operations to achieve a function of depth of traversal, FIG implemented on the basis of the breadth of the traversal algorithm and tested. Note queue storage structure used correctly.

Input

The first line: an integer from 0 to 3 (digraph: 0, there are the network: the network 2, No:: 1, undirected FIG. 3); 
second line: the number of vertices and edges inputs; 
a third line: the input value of each vertex (string, length <3); (a vertex traversal starts from the first input) 
of the fourth row: the input of each arc (edge) and arc arc tail head (as a space intervals), If the network is also input weights;

Output

FIG output breadth traversal results

 

Sample Input

0
3 3
a b c
a b
b c
c b

 

Sample Output

a b c
 
   



// The following is the code
#include "string.h" 
#include "malloc.h" / * the malloc (), etc. * / 
#include "stdio.h" / * the EOF (or the Z = ^ F6), NULL * / 
#include "stdlib.h" / * Exit () * / 
typedef int the InfoType; / * type of vertex weights * / 
#define MAX_NAME. 3 / * maximum string length + 1 vertices * / 
typedef char VertexType [MAX_NAME]; / * string type * / 
/ * adjacency graph showing table storage * / 
#define MAX_VERTEX_NUM 20 is 
typedef enum {DG, the DN, AG, the AN GraphKind}; / * {directed graph, directed net, undirected graphs, undirected network *} / 
typedef struct ArcNode 
{ 
	int adjvex; / * position of the vertex of the arc points to * / 
	struct ArcNode * nextarc; / * point to the next arc pointer * / 
	the InfoType * info; / * weight pointers network) * / 
} ArcNode; / * tABLE node * / 

typedef struct 
{  
	VertexType Data; / * vertex information * /
	ArcNode * firstarc; / * table address of the first node, the first attachment point to the arc of the vertex pointer * /
} VNode, AdjList [MAX_VERTEX_NUM]; / * first node * / 

typedef struct 
{ 
	AdjList the vertices; 
	int vexnum, arcnum; / * current number of vertices and the arcs number graph * / 
	int kind; / * type flag map * / 
} ALGraph; 

typedef struct 
{ 
	VertexType Base *; 
	int Front; 
	int REAR; 
} Queue; 

void InitQueue (Queue & Q) 
{ 
	Q.base = (VertexType *) the malloc (* MAX_VERTEX_NUM the sizeof (VertexType)); 
	IF (! Q.base) Exit (0); 
	Q.front = Q.rear = 0; 
} 

int LocateVex (ALGraph G, U VertexType) 
{/ * initial conditions: the presence of G, u, and vertex G have the same characteristics * / 
/ * result: If there is a vertex u G, the top position in the drawing is returned; otherwise * -1 /  
	int i;
	for (I = 0; I <G.vexnum; I ++) 
		IF (strcmp (U, G.vertices [I] .data) == 0)
			I return; 
	return -1; 
} 

void CreateGraph (ALGraph * G) 
{/ * use of the adjacent table storage structure, no configuration information associated graph G (FIG. 4 kinds configured with a function) * / 
	int I, J, K; 
	int w; / * weight * / 
	VertexType VA, VB; 
	ArcNode * P; 
	// the printf ( "The type of the Enter Map: (~ 0. 3):"); 
	Scanf ( "% D", & (G *). kind); 
	// the printf ( "Vertex the Enter Number, the Arc Number:"); 
	Scanf ( "% D% D", & (G *) .vexnum, & (G *) .arcnum); 
	// the printf ( "the Enter vertex D%: \ n-", (G *) .vexnum); 
	for (I = 0; I <(G *) .vexnum; I ++) / * configuration vertex vector * / 
	{ 
		Scanf ("% S ", (G *) .vertices [I] .data); 
		(G *) .vertices [I] .firstarc = NULL; 
	}  
	// IF ((* G).kind == 1 || (* G) .kind == 3) / * * net /
	// the printf ( "ARC Every Order the Enter weight,head and tail (Takes the gap by the blank space ):\n"); 
	// else / * FIG * / 
	// the printf ( "ARC Every Order the Enter head and tail (Takes The GAP The blank by Space): \ n-"); 
	for (K = 0; K <(G *) .arcnum; ++ k) / * list node structure table * / 
	{ 
		IF ((G *). 1 || .kind == (* G) == .kind. 3) / * network * / 
		Scanf ( "% D% S% S ", & W, VA, VB); 
		the else / * FIG * / 
		Scanf ("% S% S ", VA, VB); 
		I = LocateVex (* G, VA); / * arc End * / 
		J = LocateVex ( * G, vb); / * arc header * / 
		
		P = (* ArcNode) the malloc (the sizeof (ArcNode)); 
		p-> J = adjvex; 
		
		IF ((G *). 1 || .kind == (* G) .kind == 3) / * network * / 
		{ 
			p-> info = (int *) the malloc (the sizeof (int)); 
			* (p-> info) = W; 
		} 
		else 
		p-> info = NULL; / * in FIG. * / 
		p-> nextarc = (G *) .vertices [I] .firstarc; / * * is inserted in the header / 
		(G *) .vertices [I] = P .firstarc; 
		
		IF ((G *) .kind> = 2) / * undirected graph or network, a second generation tABLE node * / 
		{ 
			P = (* ArcNode) the malloc (the sizeof (ArcNode)); 
			p-> adjvex = I; 
			IF ((G *) == .kind. 3) / * undirected network * / 
			{ 
				p-> = info (int *) the malloc (the sizeof (int)); 
				* (p-> info) = W; 
			} 
			the else 
			p-> info = NULL; / * undirected graph * / 
			p-> nextarc = (G *). vertices [j] .firstarc; / * is inserted in the header * / 
			(G *) .vertices [J] = P .firstarc; 
		} 
	}	  
} 

VertexType * GetVex (ALGraph G, V int) 
{/ * initial conditions: G in FIG. exists, v G is the number of a vertex. Result: Returns the value of v * / 
	IF (v> = G.vexnum || v <0) 
		Exit (0); 
	return & G.vertices [v] .data; 
}

FirstAdjVex int (ALGraph G, VertexType v) 
{/ * initial conditions: the presence of G, v is a vertex * / G in 
/ * Result: a first number of adjacent vertices v returned. If the vertex is not adjacent to the vertex in G -1 * / return 
	ArcNode P *; 
	int V1; 
	V1 = LocateVex (G, v); / * number V1 as a vertex v in the graph G * / 
	P = G.vertices [V1] .firstarc; 
	IF (P) 
		return p-> adjvex; 
	the else 
		return -1; 
} 

int NextAdjVex (ALGraph G, VertexType V, W VertexType) 
{/ * initial conditions: the presence of G, v G is in a vertices, w is adjacent to vertex v * / 
/ * result: (relative to w) of the next adjacent vertex number v is returned. * / 
/ * If w is the last neighbor of v, then -1 * / return 
	ArcNode P *; 
	int V1, W1; 
	V1 = LocateVex (G, v); / * number V1 as a vertex v in the graph G * / 
	W1 = LocateVex (G, w); / * W1 is the vertex w number in graph G * / 
	P = G.vertices [V1] .firstarc;
	while (! p && p-> adjvex = w1) / * pointer p is not empty and is not referred to in the table node * W / 
	InitQueue (Q);
		= p-P> nextarc; 
	IF (!! P || p-> nextarc) / * find w or w is not the last neighbor * / 
		return -1; 
	the else / * p-> adjvex w == * / 
		return p-> nextarc-> adjvex; / * return v (relative to w) of the next adjacent vertex number * / 
} 

/ * depth traversal * / 
int visited [MAX_VERTEX_NUM]; / * access flag array (overall volume), not accessed flag 0, access tag. 1 * / 
void (* VisitFunc) (char * V); / * function variable (overall volume) * / 

void BFSTraverse (ALGraph G, void (* visit) (char *)) 
{/ * FIG G for depth-first traversal. Algorithm 7.4 * / 
/ * global variables VisitFunc, so that DFS is not necessary provided the function pointer parameters * / 
/ * access flag array initialization * / 
/ * calls DFS vertices not yet visited * / 
	int V, W; 
	VertexType U; 
	Queue Q; 
	for (V = 0; V <G.vexnum; ++ V) visited [V] = 0; 
	for (V = 0; V <G.vexnum; V ++) 
	{ 
		IF (visited [V]!) 
		{
			visited[v] = 1; Visit(G.vertices[v].data);
			strcpy(Q.base[Q.rear++],G.vertices[v].data);//G.vertices[v].data入队 
			while(Q.front != Q.rear)
			{
				strcpy(u,Q.base[--Q.rear]);
				for(w = FirstAdjVex(G,u); w >= 0; w = NextAdjVex(G,G.vertices[v].data,G.vertices[w].data))
				{
					if(!visited[w])
					{
						visited[w] = 1;
						Visit(G.vertices[w].data);
						strcpy(Q.base[Q.rear++],G.vertices[w].data);//G.vertices[w].data入队
					}
				}
			}
		}
	}	 
} 

void print(char *i) 
{ 
	printf("%s ",i);
} 

int main() 
{ 
	ALGraph G; 
	CreateGraph(&G); 
	BFSTraverse(G,print); 
	printf("\n");
	return 0;
}

Reproduced in: https: //www.cnblogs.com/arcfat/archive/2012/12/11/2813700.html

Guess you like

Origin blog.csdn.net/weixin_30802171/article/details/94789596