关闭

图的邻接表遍历

102人阅读 评论(0) 收藏 举报
分类:
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <string.h>
#define QueueSize 30
typedef struct _vexType{
	int flag;
	int x,y;
	char name[12];
	void *info;
}VexType;

typedef struct _arcType{
	int index;
	int weight;
	struct _arcType *next;
}ArcType;
typedef struct _adjList{
	VexType data;
	ArcType *first;
}AdjList;

typedef struct _ALGraph
{
	int kind;
	int vexNum;
	int arcNum;
	AdjList  *vertice;
}ALGraph;
typedef struct _Queue
{
    int front;
    int rear;
    int count;
    int data[QueueSize];
}Queue;
void InitGraph(ALGraph *g);
void CreatGraph(FILE *fp,ALGraph *g);
int GetIndex(ALGraph *g,char *name);
void InsertArc(AdjList *l,int index,int w);
void DispArc(AdjList *l);
void DFS(ALGraph *G, int i);
void DFSTraverseM(ALGraph *G);
void InitQueue(Queue *Q);
int QueueEmpty(Queue *Q);
int QueueFull(Queue *Q);
void EnQueue(Queue *Q);
void BFS(ALGraph*G, int k);
void BFSTraverseM(ALGraph *G);
void PrintfGraphAL(ALGraph *G);
/*-------------
在文件中写入
形如:
4,5
1,2,A
2,5,B
6,7,C
7,9,D
A B 10
A C 20
A D 30
B D 40
C D 50
-------------*/
int main()
{
	FILE *fp;
	
	ALGraph mygraph;
	InitGraph(&mygraph);
	
	fp=fopen("d:\\graph.txt","r");
	
	CreatGraph(fp,&mygraph);
	PrintfGraphAL(&mygraph);
	printf("深度优先遍历:\n");
    DFSTraverseM(&mygraph);
    printf("广度优先遍历:\n");
    BFSTraverseM(&mygraph);
	return 0;
}

void InitGraph(ALGraph *g)
{
	g->arcNum = 0;
	g->kind = 0;
	g->vertice = NULL;
	g->vexNum = 0;
}
void CreatGraph(FILE *fp,ALGraph *g)
{
	int vexNum,arcNum;
	int i,x,y,w,loc1,loc2;
	char name[12],n1[12],n2[12];
	
	fscanf(fp,"%d,%d",&vexNum,&arcNum);
	g->arcNum = arcNum;
	g->vexNum = vexNum;
	
	g->vertice = (AdjList *)malloc(sizeof(AdjList)*vexNum);
	
	for(i=0;i<vexNum;i++)
	{
		fscanf(fp,"%d,%d,%s",&x,&y,name);
		g->vertice[i].data.x = x;
		g->vertice[i].data.y = y;
		strcpy(g->vertice[i].data.name,name);
		g->vertice[i].first = NULL;
		g->vertice[i].data.flag=0;
	}
	
	for(i=0;i<g->arcNum;i++)
	{
		fscanf(fp,"%s %s %d",n1,n2,&w);
		loc1 = GetIndex(g,n1);
		loc2 = GetIndex(g,n2);
		InsertArc(g->vertice + loc1,loc2,w);
	}
	
	
}

int GetIndex(ALGraph *g,char *name)
{
	int i;
	for(i=0;i<g->vexNum;i++)
	{
		if(strcmp(g->vertice[i].data.name,name) ==0)
			return i;
	}
	return -1;
}


void InsertArc(AdjList *l,int index,int w)
{
	ArcType *p;
	
	p=(ArcType *)malloc(sizeof(ArcType));
	p->index = index;
	p->weight = w;
	p->next = l->first;
	l->first=p;
	
}
void DFS(ALGraph *G, int i)
{
    ArcType *p;
    printf("visit vertex:%s\n", G->vertice [i].data .name );  
    G->vertice[i].data .flag = 1;             
    p = G->vertice [i].first ;     
    while (p)
    {                               
        if (!G->vertice [p->index].data .flag )    
            DFS(G, p->index );      
        p = p->next;                    
    }
}
void DFSTraverseM(ALGraph *G)
{
    int i;
    for (i = 0; i < G->vexNum ; i++)
        G->vertice[i].data .flag = 0;
    for (i = 0; i < G->vexNum ; i++)
        if (!G->vertice[i].data .flag)
            DFS(G, i);
}
void InitQueue(Queue *Q)
{
    Q->front = Q->rear = 0;
    Q->count = 0;
}
int QueueEmpty(Queue *Q)
{
    return Q->count == 0;
}
int QueueFull(Queue *Q)
{
    return Q->count == QueueSize;
}
void EnQueue(Queue *Q, int x)
{
    if (QueueFull(Q))
        printf("Queue overflow");
    else
    {
        Q->count++;
        Q->data[Q->rear] = x;
        Q->rear = (Q->rear + 1) % QueueSize;
    }
}
int DeQueue(Queue *Q)
{
    int temp;
    if (QueueEmpty(Q))
    {
        printf("Queue underflow");
        return NULL;
    }
    else
    {
        temp = Q->data[Q->front];
        Q->count--;
        Q->front = (Q->front + 1) % QueueSize;
        return temp;
    }
}
void BFS(ALGraph*G, int k)
{   
    int i;
    Queue Q;             
    ArcType *p;
    InitQueue(&Q);         
    printf("visit vertex:%s\n", G->vertice [k].data .name);     
    G->vertice[k].data .flag = 1;
    EnQueue(&Q, k);         
    while (!QueueEmpty(&Q))
    {                                  
        i = DeQueue(&Q);                  
        p = G->vertice [i].first;       
        while (p)
        {                              
            if (!G->vertice [p->index ].data .flag )
            {                          
                printf("visit vertex:%s\n", G->vertice [p->index ].data.name );      //访问vj
                G->vertice[p->index ].data .flag = 1;
                EnQueue(&Q, p->index ); 
            }
            p = p->next;                   
        }
    }
}
void BFSTraverseM(ALGraph *G)
{
    int i;
    for (i = 0; i < G->vexNum ; i++)
		G->vertice[i].data .flag = 0;
    for (i = 0; i < G->vexNum ; i++)
        if (!G->vertice[i].data .flag)
            BFS(G, i);
}
void PrintfGraphAL(ALGraph *G)
{
    for (int i = 0; i < G->vexNum ; i++)
    {
        printf("vertex:%s",G->vertice [i].data .name);
        ArcType *p = G->vertice [i].first ;
        while (p)
        {
            printf("→:%d %d", p->index ,p->weight );
            p = p->next;
        }
        printf("\n");
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1508次
    • 积分:120
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档