Toplogical Sort 拓扑排序

Toplogical Sort

      拓扑排序是对有向图的顶点的一种排序,它使得如果存在一条从Vm到Vn的路径,那么在排序中Vn出现在Vm后面。

      如果图含有圈,或者初始入度没有为0的节点,那么拓扑排序是不可能完成的。

理论介绍去看<DSAA>或者《算法导论》,老话,这里还是介绍如何实现。

tls.h

/************************************************************
code file	: tls.h
code writer	: EOF
code date	: 2014.11.22
e-mail		: [email protected]

code description:

	Header file for toplogistic sort.

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

#ifndef _TLS_H_
#define _TLS_H_

	#include <stdio.h>
	#include <stdlib.h>

	#define CONNECTED    1
	#define DISCONNECTED 0

	#define SUCCESS  0
	#define FAILED  -1

	#define EMPTY_QUEUE   -1
	#define UNEMPTY_QUEUE  0 

	/*
	**	We use these macro as index for struct table
	*/
	#define KNOW_OFFSET 0 //vertex that have been found
	#define DIST_OFFSET 1 //distance of vertex
	#define PATH_OFFSET 2 //parent vertex of current vertex

	#define FOUND	    1
	#define NOT_FOUND   0

	#define ENTRY_POINT 3

	#define INFINITE -1

	struct node
	{
		struct node* previous;
		struct node* next;
		int data;
	};

	struct vertex
	{
		int value;
		int indegree;
		struct vertex* next;
		struct vertex* end;
	};

	struct graph
	{
		int num_vertex;
		int num_edge;
		struct vertex adjacent[0];
	};

	struct table
	{
		int height;
		int width;
		int msg[0];//we store message of table in this array.
	};

	/*
	**	API for ADT--graph
	*/
	struct graph* init_graph(int vertex,int edge);
	void   release_graph(struct graph* p_graph);

	int add_edge(struct graph* p_graph,char from_v,char to_v);
	int print_graph(struct graph* p_graph);

	/*
	**	API for ADT--table
	*/
	struct table* init_table(int row,int col);
	void release_table(struct table* p_table);

	/*
	**	I implement some API to use ADT-Queue
	*/
	int queue_destory(struct node* p_queue_tail);

	int queue_enter(struct node** pp_queue_header,
			struct node** pp_queue_tail,
			int number);

	int queue_init(struct node** pp_queue_header,
 		       struct node** pp_queue_tail);

	int queue_out(struct node** pp_queue_header,
		      struct node** pp_queue_tail);

	void queue_print(struct node* p_queue_tail);

#endif

测试主程序test_graph.c

/****************************************************************
code file	: test_graph.c
code writer	: EOF
code date	: 2014.11.22
e-mail		: [email protected]

code description:

	This test program is used for testing toplogistic sort.

*****************************************************************/
#include "tls.h"

int main()
{
	struct graph* p_graph = NULL;

	FILE* fp = fopen("./text.txt","r+");

	if(!fp)
	{
		printf("fopen() failed!\n");
		return 0;
	}

	int ret    = 0;
	int vertex = 0;
	int edge   = 0;

	int from_v = 0;
	int to_v   = 0;

	fscanf(fp,"%d",&vertex);
	fscanf(fp,"%d",&edge);

	p_graph = init_graph(vertex,edge);

	int temp = 0;
	for(temp;temp < edge;temp++)
	{
		/*
		**	I think it's necessary to check the returned value
		** of scanf() family.
		*/
		ret = fscanf(fp,"%d %d",&from_v,&to_v);
		if(ret != 2)
		{
			break;
		}

		add_edge(p_graph,from_v,to_v);
	}

	struct table* p_table =  init_table(vertex,3);

	print_graph(p_graph);

	//----------------------
	topsort(p_graph);
	//---------------------

	release_table(p_table);
	release_graph(p_graph);
	fclose(fp);
	return 0;
}

init_graph.c

/************************************************************
code file	: init_graph.c
code writer	: EOF
code date	: 2014.11.22
e-mail		: [email protected]

code description:
	This function is used for initializing the graph
with inputed parameter @vertex and @edge.

************************************************************/
#include "tls.h"

struct graph* init_graph(int num_vertex,int num_edge)
{

	if(num_vertex <= 0 || num_edge <= 0)
	{
		return NULL;
	}

	struct graph* p_graph = NULL;

        p_graph = (struct graph*)malloc(sizeof(struct graph) +		  num_vertex*sizeof(struct vertex));

	if(!p_graph)
	{
		printf("malloc failed in function %s()\n",__FUNCTION__);
		return NULL;
	}

	p_graph->num_vertex = num_vertex;
	p_graph->num_edge   = num_edge;

	int temp = 0;
	//initialize the adjacent relationship
	for(temp = 0;temp < num_vertex;temp++)
	{
		p_graph->adjacent[temp].value = temp;
		p_graph->adjacent[temp].next  = NULL;
		p_graph->adjacent[temp].end   = NULL;
		p_graph->adjacent[temp].indegree   = 0;
	}

	return p_graph;
}

init_table.c

/************************************************************
code file	: init_table.c
code writer	: EOF
code date	: 2014.11.23
e-mail		: [email protected]

code description:

	This function will help us to create a table which's
size is @row multiply @col. And the size of each unit int
this table is sizeof(int).

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

#include "tls.h"

struct table* init_table(int row,int col)
{
	if(row <= 0 || col <= 0)
	{
		return NULL;
	}

	struct table* p_table = (struct table*)malloc(sizeof(struct table) + row*col*sizeof(int));

	if(!p_table)
	{
		return NULL;
	}

	p_table->height = row;
	p_table->width  = col;

	int num_row = row;
	int num_col = col;

	//We must initialize the table !
	for(row = 0;row < num_row;row++)
	{
		*((int*)(p_table->msg) + row*num_col + KNOW_OFFSET) = NOT_FOUND;
		if(row != ENTRY_POINT)
		{
			*((int*)(p_table->msg) + row*num_col + DIST_OFFSET) = INFINITE;
		}
		else
		{
			*((int*)(p_table->msg) + row*num_col + DIST_OFFSET) = 0;
		}
		*((int*)(p_table->msg) + row*num_col + PATH_OFFSET) = 0;

	}

	return p_table;
}

print_graph.c

/************************************************************
code file	: print_graph.c
code writer	: EOF
code date	: 2014.11.22
e-mail		: [email protected]

code description:
	This function will print out the connection of graph
which @p_graph point to.

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

#include "tls.h"

int print_graph(struct graph* p_graph)
{
	if(!p_graph)
	{
		return FAILED;
	}

	int from_v = 0;
	int to_v = 0;

	int num_vertex = p_graph->num_vertex;

	struct vertex* p_vertex = NULL;

	for(from_v = 0;from_v < num_vertex;from_v++)
	{
		p_vertex = &(p_graph->adjacent[from_v]);
		while(p_vertex)
		{
			printf("\t%d",p_vertex->value);
			p_vertex = p_vertex->next;
		}
		printf("\n");
	}

	return SUCCESS;
}

release_graph.c

/************************************************************
code file	: release_graph.c
code writer	: EOF
code date	: 2014.11.22
e-mail		: [email protected]

code description:
	It's easy and convenient for us to call this API once
and free all the graph.

*************************************************************/
#include "tls.h"

void release_graph(struct graph* p_graph)
{
	if(!p_graph)
	{
		return ;
	}

	int temp = 0;
	int num_vertex = p_graph->num_vertex;
	struct vertex* p_temp = NULL;

	for(temp = 0;temp < num_vertex;temp++)
	{
		if(p_graph->adjacent[temp].next)
		{
			p_temp = (p_graph->adjacent[temp].next->next);
			while(p_temp)
			{
				free(p_graph->adjacent[temp].next);
				p_graph->adjacent[temp].next = p_temp;
				p_temp = p_temp->next;
			}
			free(p_graph->adjacent[temp].next);
		}
	}

	free(p_graph);
}

release_table.c

#include "tls.h"

void release_table(struct table* p_table)
{
	if(!p_table)
	{
		return;
	}

	free(p_table);
}
#include "tls.h"

void table_print(struct table* p_table)
{
	if(!p_table)
	{
		return;
	}

	int row_num = p_table->height;
	int col_num = p_table->width;

	int row = 0;

	int know = 0;
	int dist = 0;
	int path = 0;

	printf("\tknow\tdist\tpath\n");

	for(row = 0;row < row_num;row++)
	{
		know = p_table->msg[row*col_num + KNOW_OFFSET];

		switch(know)
		{
			case FOUND:
			{
				printf("\tFound");
				break;
			}

			case NOT_FOUND:
			{
				printf("\tNFond");
				break;
			}

			default:
				printf("error value of varible @know\n");
				return;
		}

		dist = p_table->msg[row*col_num + DIST_OFFSET];

		switch(dist)
		{
			case INFINITE:
			{
				printf("\tINF");
				break;
			}

			default:
				printf("\t%d",dist);
		}

		path = p_table->msg[row*col_num + PATH_OFFSET];

		switch(path)
		{
			case 0:
			{
				printf("\t0");
				break;
			}

			default:
				printf("\tv%d",path);
		}

		printf("\n");
	}

	printf("\n\n\n");
}

add_edge.c

/************************************************************
code file	: add_edge.c
code writer	: EOF
code date	: 2014.11.22
e-mail		: [email protected]

code description:
	This function will help us to add a new connection
between different vertex which is in the graph.

*************************************************************/
#include "tls.h"

int add_edge(struct graph* p_graph,char from_v,char to_v)
{
	if(!p_graph || from_v < 0 || to_v < 0)
	{
		return FAILED;
	}

	struct vertex* p_to_v    = (struct vertex*)malloc(sizeof(struct vertex));

	if(!p_to_v)
	{
		printf("malloc failed in function %s()\n",__FUNCTION__);

		return FAILED;
	}

	if(!(p_graph->adjacent[from_v].end))
	{
		p_graph->adjacent[from_v].next  = p_to_v;
		p_graph->adjacent[from_v].end   = p_to_v;
		p_to_v->next  = NULL;
		p_to_v->value = to_v;
	}
	else
	{
		p_graph->adjacent[from_v].end->next = p_to_v;
		p_graph->adjacent[from_v].end       = p_to_v;//update the new end node.
		p_to_v->next  = NULL;
		p_to_v->value = to_v;

	}

	/*
	**	Caculate the indegree of each vertex
	*/
	(p_graph->adjacent[to_v].indegree)++;

	return SUCCESS;
}

这里实现拓扑排序的核心函数topsort.c

/****************************************************************
code file	: topsort.c
code writer	: EOF
code date	: 2014.11.22
e-mail		: [email protected]

code description:
	A implementation of topsort() which is used for
toplogistic sort.

***************************************************************/
#include "tls.h"

void topsort(struct graph* p_graph)
{
	if(!p_graph)
	{
		return;
	}

	int counter = 0;

	struct vertex* p_vertex = NULL;

	struct node* p_queue_header = NULL;
	struct node* p_queue_tail   = NULL;

	queue_init(&p_queue_header,&p_queue_tail);

	int temp = 0;
	//Attention! Here we start from point 1 but not temp = 0
	for(temp = 1;temp < p_graph->num_vertex;temp++)
	{
		if(p_graph->adjacent[temp].indegree == 0)
		{
			queue_enter(&p_queue_header,
		    		    &p_queue_tail,
				    temp);
		}
	}

	int vertex_index = 0;

	while(!!p_queue_header)
	{
		vertex_index = queue_out(&p_queue_header,&p_queue_tail);

		printf("\t%d",vertex_index);

		p_vertex = (struct vertex*)(&p_graph->adjacent[vertex_index]);
		p_vertex = p_vertex->next;
		for(;!!p_vertex;p_vertex = p_vertex->next)
		{
			if(--(p_graph->adjacent[p_vertex->value].indegree) == 0)
			{
				queue_enter(&p_queue_header,
					    &p_queue_tail,
					    p_vertex->value);
			}
		}
	}

	printf("\n");
	queue_destory(p_queue_tail);
}

测试文本: text.txt

8
12
3 6
1 4
1 2
1 3
4 3
4 6
4 7
7 6
2 4
2 5
5 7
5 4

时间: 2024-08-30 14:18:06

Toplogical Sort 拓扑排序的相关文章

图论-拓扑排序

拓扑排序(Topological sort) 拓扑排序是对有向无环图(DAG)顶点的一种排序,它使得如果存在u, v的有向路径,那么满足序中u在v前.拓扑排序就是由一种偏序(partical order)得到的一个全序(称为拓扑有序).偏序满足自反性,反对称性,传递性的序. 拓扑排序的思路很简单,就是每次任意找一个入度为0的点输出,并把这个点以及与这个点相关的边删除,实际算法中,用一个队列实现. 算法: 1.把所有入度为0的点入队Q; 2.若队Q非空,则点u出队,输出u,否则转4; 3.把所有与

拓扑排序(Topological Sort)

Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap.c.heap.h 等等,且这些文件没有其他自定义库的依赖:另外还有一些基于上述自定义库的库:bfs.c.bfs.h.dfs.c.dfs.h.dijkstra.c.dijkstra.h.tcpSocket.c.tcpSocket.h 等等:基于以上的库,你开发了一些爬虫程序 scrawlYoutub

LeetCode编程训练 - 拓扑排序(Topological Sort)

拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序(被依赖的排在前面),或给出排序结果. 最常用解决拓扑排序问题的方法是Kahn算法,步骤可以概括为: 1. 根据依赖关系,构建邻接矩阵或邻接表.入度数组 2. 取入度为0的数据(即不依赖其他数据的数据),根据邻接矩阵/邻接表依次减小依赖其的数据的入度 3. 判断减小后是否有新的入度为0的数据,继续进

算法导论 第二十二章:拓扑排序

拓扑排序(针对有向无回路图DAG)是深度优先搜索的一个应用,其结果图中所有顶点的一个线性排列. 伪代码如下: EG: 拓扑排序完整代码如下: #include<iostream> #include<iomanip> #include<string> #include<algorithm> using namespace std; #define UDG 0 #define DG 1 #define WHITE 0 #define GRAY 1 #define

UVA - 10305 - Ordering Tasks (拓扑排序!)

UVA - 10305 Ordering Tasks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem F Ordering Tasks Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n task

hello大家好,我是拓扑排序

发几个以前写的拓扑排序,回顾一下. 拓扑排序,一般不会单独考,主要要求还是掌握好这个概念,有个感性的认识,以及能快速的写出求拓扑排序的程序,进而继续接下来对图的处理,或是比如dp之类的算法,又或者是判断有无环之类.求拓扑序主要就是运用队列,push入度为0的点,删掉它们出去的边,重复这个操作.像要是求字典序最小,就可以用优先队列. TOJ 3993 求字典序最小的拓扑排序 1 #include<cstdio> 2 #include<queue> 3 #include<cstr

【模拟题(63550802...)】解题报告【贪心】【拓扑排序】【找规律】【树相关】

目录: 1.A[树相关]    2.B[找规律]    3.C[贪心][拓扑排序] A. 描述(A 输入文件 : A.input 输出文件 : A.output)一个城市的构成是一颗n 个节点的树(2 ≤ n ≤ 200), 现在需要在树中找出两条不相交的路径(即两条路径不能有重边也不能有重点),使得路径的长度的乘积最大.输入描述第一行一个数n 表示这个城市一共有 n 个节点.接下来 n-1 行,每行两个数ai 和bi (1 ≤ ai,bi ≤ n ),分别表示从ai 到bi,有一条边,每条边的

poj 1094 拓扑排序

Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D.

CF 274D Lovely Matrix 拓扑排序,缩点 难度:2

http://codeforces.com/problemset/problem/274/D 这道题解题思路: 对每一行统计,以小值列作为弧尾,大值列作为弧头,(-1除外,不连弧),对得到的图做拓扑排序即可. 但本题数据较大,所以需要进行缩点,把相同数值的点缩在一起,成为一个新的大点,原先的小值列向大点连接,再由大点向大值列连接,可以减少边数 举例来说,原本取值为1的有4个点,取值为2的有5个点, 不缩点,就需要20条边 缩点,只需要4+1+5=10条边 (不过我还是觉得这个方法有点投机取巧??