使用sort进行拓扑排序的比较函数

C++等语言自带的sort函数使用的排序算法基本都是快速排序,快速排序是一种不稳定的排序算法,也就是说两个相等的元素排序前后的相对位置是有可能变化的。在使用sort对一个有偏序关系的序列进行拓扑排序时,比较函数的设计要特别注意。例如我们对格点(x,y)进行排序,使得如果有x0 <= x1 而且 y0 <= y1时,(x0,y0)在(x1,y1)前面。

 

代码大致如下:

 

const int N = 10;

struct Node {
	int x, y;
	void init(int _x, int _y) {
		x = _x;
		y = _y;
	}
}node[N * N];

bool cmp(const Node &a, const Node &b) {
}

int main() {
	int n = 0;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			node[n++].init(i, j);
		}
	}
	for (int i = 0; i < N * N; ++i) {
		swap(node[i], node[rand() % (N * N)]);
	}
	sort(node, node + n, cmp);
	for (int i = 0; i < N; ++i) {
		printf("%d %d\n", node[i].x, node[i].y);
	}
	return 0;
}

 非常直观地,也许我们会把比较函数设计成:

bool cmp(const Node &a, const Node &b) {
	return a.x <= b.x && a.y <= b.y;
}

 

但是这样的结果很可能不是我们所要的,我们确定了这么个比较关系之后,所有不满足的这个关系的两个元素进来比较,都会被认为第一个应该在第二个后面。

 

正确的,应该设计成这样:

bool cmp(const Node &a, const Node &b) {
	return a.x < b.x || a.x == b.x && a.y < b.y;
}

 

这其实是将一个偏序关系全序化,容易理解这个比较函数包含了前面那个比较函数的内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
拓扑排序是一种对有向无环图(DAG)进行排序的算法,它可以将图中的节点按照依赖关系进行排序,使得所有的依赖关系都得到满足。 在C语言中,可以使用邻接表来表示有向图,并通过深度优先搜索(DFS)来实现拓扑排序。下面是一个使用C语言实现拓扑排序的示例代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_VERTEX 100 // 邻接表节点 typedef struct Node { int vertex; struct Node* next; } Node; // 图结构 typedef struct Graph { int numVertices; Node* adjLists[MAX_VERTEX]; } Graph; // 创建节点 Node* createNode(int v) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->vertex = v; newNode->next = NULL; return newNode; } // 创建图 Graph* createGraph(int vertices) { Graph* graph = (Graph*)malloc(sizeof(Graph)); graph->numVertices = vertices; for (int i = 0; i < vertices; i++) { graph->adjLists[i] = NULL; } return graph; } // 添加边 void addEdge(Graph* graph, int src, int dest) { Node* newNode = createNode(dest); newNode->next = graph->adjLists[src]; graph->adjLists[src] = newNode; } // 拓扑排序辅助函数 void topologicalSortUtil(Graph* graph, int v, int visited[], int stack[]) { visited[v] = 1; Node* temp = graph->adjLists[v]; while (temp != NULL) { int adjVertex = temp->vertex; if (!visited[adjVertex]) { topologicalSortUtil(graph, adjVertex, visited, stack); } temp = temp->next; } stack[--stack[0]] = v; } // 拓扑排序 void topologicalSort(Graph* graph) { int visited[MAX_VERTEX] = {0}; int stack[MAX_VERTEX + 1] = {0}; for (int i = 0; i < graph->numVertices; i++) { if (!visited[i]) { topologicalSortUtil(graph, i, visited, stack); } } printf("拓扑排序结果:"); for (int i = 0; i < graph->numVertices; i++) { printf("%d ", stack[i]); } } int main() { int vertices, edges; printf("请输入图的顶点数和边数:"); scanf("%d %d", &vertices, &edges); Graph* graph = createGraph(vertices); printf("请输入边的起始点和终点:\n"); for (int i = 0; i < edges; i++) { int src, dest; scanf("%d %d", &src, &dest); addEdge(graph, src, dest); } topologicalSort(graph); return 0; } ``` 以上代码实现了一个简单的拓扑排序算法。用户需要输入图的顶点数和边数,然后输入每条边的起始点和终点。程序会输出拓扑排序的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值