头歌数据结构——图

第1关:实现图的宽度优先遍历 

//Graph
///
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Graph.h"
/

Graph* Graph_Create(int n)
{
	Graph* g=(Graph*)malloc(sizeof(Graph));
	g->n=n;
	g->vetex=(char**)malloc(sizeof(char*)*n);
	int i;
	for (i=0; i<n; i++) g->vetex[i] = NULL;
	g->adj=(int*)malloc(sizeof(int)*n*n);
	int j;
	for(i=0; i<n; i++) {
		for(j=0; j<n; j++) {
			g->adj[i*n+j]=0;
		}
	}
	return g;
}

void Graph_Free(Graph* g)
{
	free(g->adj);
	int i;
	for (i=0; i<g->n; i++) free(g->vetex[i]);
	free(g->vetex);
	free(g);
}

int Graph_WidthFirst(Graph*g, int start, Edge* tree)
//从start号顶点出发宽度优先遍历,(编号从0开始)
//返回访问到的顶点数,
//tree[]输出遍历树
//返回的tree[0]是(-1, start), 
//真正的遍历树保存在tree[1..return-1], return是返回值
//顶点的访问次序依次为tree[0].to, tree[1].to,  ..., tree[return-1].to
//输入时,tree[]的长度至少为顶点数
//返回值是从start出发访问到的顶点数
{
	const int MAX=1000;
	Edge queue[MAX];
	int head=0, tail=0;
#define In__(a,b)  {queue[tail].from=a; queue[tail].to=b; tail=(tail+1)%MAX;}/
#define Out__(a,b)  {a=queue[head].from; b=queue[head].to; head=(head+1)%MAX;}//
#define QueueNotEmpty (head!=tail?1:0)///
#define HasEdge(i,j)  (g->adj[(i)*g->n+(j)]==1)

	char* visited=(char*)malloc(sizeof(char)*g->n);
	memset(visited, 0, sizeof(char)*g->n);

	int parent=-1;  
	int curr=start;
	In__(parent, curr); 
	int k=0; //已经访问的结点数
	/*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/
    while(QueueNotEmpty)//队列不为空
    {
        Out__(parent,curr);//出队
        if(!visited[curr]) //现在结点未被访问
        {
            visited[curr]=1; //修改visit数组,curr结点现在被访问
            tree[k].from=parent; 
            tree[k].to=curr;
            k++;//已被访问结点数增加

            for(int j=0;j<g->n;j++)//对未被访问的邻接结点入队
            {
                if(!visited[j]&&HasEdge(curr,j))  //未被访问且与现在的结点之间存在边
                    In__(curr,j);
            }
        }  
        
    }
    /*****END*******/
	return k;
#undef In__//
#undef Out__///
#undef QueueNotEmpty
#undef HasEdge
}

第2关:实现图的深度优先遍历 

//Graph
///
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Graph.h"
Graph* Graph_Create(int n)
{
	Graph* g=(Graph*)malloc(sizeof(Graph));
	g->n=n;
	g->vetex=(char**)malloc(sizeof(char*)*n);
	int i;
	for (i=0; i<n; i++) g->vetex[i] = NULL;
	g->adj=(int*)malloc(sizeof(int)*n*n);
	int j;
	for(i=0; i<n; i++) {
		for(j=0; j<n; j++) {
			g->adj[i*n+j]=0;
		}
	}
	return g;
}

void Graph_Free(Graph* g)
{
	free(g->adj);
	int i;
	for (i=0; i<g->n; i++) free(g->vetex[i]);
	free(g->vetex);
	free(g);
}

int Graph_DepthFirst(Graph*g, int start, Edge* tree)
//从start号顶点出发深度优先遍历,(编号从开始)
//返回访问到的顶点数,
//tree[]输出遍历树
//返回的tree[0]是(-1, start), 
//真正的遍历树保存在tree[1..return-1], return是返回值
//顶点的访问次序依次为tree[0].to, tree[1].to, ..., tree[return-1].to
//输入时,tree[]的长度至少为顶点数
//返回值是从start出发访问到的顶点数
{
	/*请在BEGIN和END之间实现你的代码*/
    /*****BEGIN*****/   
    const int MAX=1000;
    Edge stack[MAX];//边存放在栈中
    int top=-1; //栈顶标记为-1
#define Push__(a,b)  {top++; stack[top].from=a; stack[top].to=b;}
#define Pop__(a,b)  {a=stack[top].from; b=stack[top].to; top--;}///
#define StakNotEmpty (top>=0?1:0)
#define HasEdge(i,j)  (g->adj[(i)*g->n+(j)]==1)
    char* visited=(char*)malloc(sizeof(char)*g->n);//为visit数组分配空间
    memset(visited, 0, sizeof(char)*g->n);//初始化一个visit数组
    int parent=-1; //parent从栈顶开始 
    int curr=start;//curr从起始位置出发
    Push__(parent, curr);//入栈
    int k=0; //用于存储已经被过访问的结点数
    while (StakNotEmpty) {
        Pop__(parent, curr); //出栈
        if (!visited[curr])//现在的结点若未被访问
        {
        visited[curr]=1;
        //tree中存放的为边
        tree[k].from=parent; 
        tree[k].to=curr;  
        k++;
        }
        for (int j=g->n-1; j>=0; j--)//编号大的开始入栈 
        {
            if (HasEdge(curr,j) && !visited[j]) //已被访问且跟curr之间存在节点
                Push__(curr,j);
        }
    }
    free(visited);
    return k;
#undef Push__
#undef Pop__///
#undef StakNotEmpty//
#undef HasEdge

    /*****END*******/
}

  • 17
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值