DFS (Recursive, link list)

1. 定义 vertex 和 edge的数据结构:

struct edge;

struct vertex
{
	int visited;
	int value;
	struct vertex *next;
	struct edge * first_edge;
};

struct edge
{
	struct vertex *end_point;
	struct edge *next_edge;
};

struct Graph
{
	struct vertex *first_vertex;
	struct vertex *last_vertex;
};

2. 添加一个顶点的函数:

void AddVertex(struct Graph *graph, int vertexValue)
{
	struct vertex **head = &(graph->first_vertex);
	struct vertex **tail = &(graph->last_vertex);
	struct vertex *pVertex = (struct vertex *)malloc(sizeof(struct vertex));

	pVertex->visited = 0;
	pVertex->value = vertexValue;
	pVertex->next = NULL;
	pVertex->first_edge = NULL;

	if(*head == NULL)
	{
		*head = pVertex;
		*tail = pVertex;
	}
	else
	{
		(*tail)->next = pVertex;
		*tail = pVertex;
	}
}

3. 根据顶点的索引找到对应的顶点:

struct vertex * FindVertex(struct Graph *graph, int value)
{
	struct vertex *pVertex = graph->first_vertex;
	while(pVertex)
	{
		if(pVertex->value == value)
		{
			return pVertex;
		}
		pVertex = pVertex->next;
	}
	return NULL;
}

4. 增加一条边:

void AddEdge(struct Graph *graph, int E1, int E2)
{
	struct vertex *pV1 = FindVertex(graph, E1);
	struct vertex *pV2 = FindVertex(graph, E2);

	struct edge *pEdge = (struct edge *)malloc(sizeof(struct edge));
	pEdge->end_point = pV2;
	pEdge->next_edge = pV1->first_edge;
	pV1->first_edge = pEdge;
	
	pEdge = (struct edge *)malloc(sizeof(struct edge));
	pEdge->end_point = pV1;
	pEdge->next_edge = pV2->first_edge; 
	pV2->first_edge = pEdge;
}

5. 删除顶点和边:

void RemoveEdges(struct vertex * vertex)
{
	struct edge *pEdge = vertex->first_edge;
	while(pEdge)
	{
		struct edge *pTemp = pEdge;
		pEdge = pEdge->next_edge;
		free(pTemp);
	}
}
void DestroyGraph(struct Graph *graph)
{
	struct vertex *pVertex = graph->first_vertex;
	while(pVertex)
	{
		RemoveEdges(pVertex);
		struct vertex *pTemp = pVertex;
		pVertex = pVertex->next;
		free(pTemp);
	}
}

6. DFS:

void DFS(struct Graph *graph, int vertex)
{
	struct vertex *pVertex = FindVertex(graph, vertex);
	if(pVertex->visited == 1)
	{
		return;
	}
	pVertex->visited = 1;
	printf("%d ", vertex);
	struct edge *pEdge = pVertex->first_edge;

	while(pEdge && pEdge->end_point)
	{
		DFS(graph, pEdge->end_point->value);
		pEdge = pEdge->next_edge;
	} 	
}

7. 测试函数:

#define VERTEX_NUM 9
#define EDGE_NUM	10

int main(void)
{
	struct Graph graph;
	graph.first_vertex = graph.last_vertex = NULL;

	int V =  VERTEX_NUM;
	
	int E = EDGE_NUM;

	int edge[] ={1, 2, 1, 5, 1, 9, 2,3, 3, 4, 2, 5, 9, 6, 5, 6, 6, 7, 7, 8};
	
	int i;
	for(i = 0; i < V; i++)
	{
		AddVertex(&graph, i + 1);		
	}

	for(i = 0; i < 2 * E; i += 2)
	{
		AddEdge(&graph, edge[i], edge[i + 1]);		
	}

	DFS(&graph, 1);
	printf("\n");
	
	DestroyGraph(&graph);
	return 0;
}



输出如下:

1 9 6 7 8 5 2 3 4


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值