Lintcode 127. Topological Sorting

【Lintcode】127. Topological Sorting 题目地址: https://www.lintcode.com/problem/topological-sorting/description 对一个有向图进行拓扑排序,返回任意一个符合条件的排序。可以用Kahn’s algorithm。具体做法是,先求出所有顶点的入度,然后取所有入度等于000的顶点加入拓扑排序的结果里,接着进行BFS,将这些入度为000的顶点的前驱邻边依次删掉,一旦某个顶点的入度删为了000,就将其加入拓扑排序的结果里,并将其入队。如此重复操作下去即可。代码如下: import java.util.*; public class Solution { /* 立即下载

Given an directed graph, a topological order of the graph nodes is defined as follow:

  • For each directed edge A -> B in graph, A must before B in the order list.
  • The first node in the order can be any node in the graph with no nodes direct to it.

Find any topological order for the given graph.

 Notice

You can assume that there is at least one topological order in the graph.

Example

For graph as follow:

picture

The topological order can be:

[0, 1, 2, 3, 4, 5]
[0, 2, 3, 1, 5, 4]
...
题目思路:

这题就是topological sorting的算法之一:我使用dfs。

实现的是链接的算法--topological sorting

class Solution {
public:
    /*
     * @param graph: A list of Directed graph node
     * @return: Any topological order for the given graph.
     */
    vector<DirectedGraphNode*> topSort(vector<DirectedGraphNode*>& graph) {
        // write your code here
        vector<DirectedGraphNode*> res;
        set<int> visited;
    
        if(graph.size()==0)
            return graph;
        
        for(int i=0;i<graph.size();i++){
            if(visited.find(graph[i]->label) == visited.end())
                topSort(res, visited, graph[i]);
        }
        
        reverse(res.begin(), res.end());
        return res;
    }
    
private:
    void topSort(vector<DirectedGraphNode*>& res,
            set<int>& visited, DirectedGraphNode* node){
                
        if(visited.find(node->label) != visited.end())
            return;
           
        visited.insert(node->label);
        for(int i=0; i < node->neighbors.size(); i++){
            topSort(res, visited, node->neighbors[i]);
        }
        res.push_back(node);
    }

};

这种方法需要保证的是,第0个节点一定要是入度为零的节点,否则会出错。

第二种方法采用的是使用map统计所有节点的入度节点,然后使用queue保存入度为零的节点,最后使用bfs。

class Solution {
public:
    /**
     * @param graph: A list of Directed graph node
     * @return: Any topological order for the given graph.
     */
    vector<DirectedGraphNode*> topSort(vector<DirectedGraphNode*> graph) {
        // write your code here
        int size = graph.size(), i = 0;
        if(size <= 0) {
            return vector<DirectedGraphNode*>();
        }

        vector<DirectedGraphNode*> result;
        queue<DirectedGraphNode*> noPreNode;
        map<DirectedGraphNode*, int> nodeIndegree;

        getIndegree(graph, nodeIndegree);

        for(i=0; i<size; i++) {
            if(nodeIndegree[graph[i]] == 0) {
                noPreNode.push(graph[i]);
            }
        }

        while(!noPreNode.empty()) {
            result.push_back(noPreNode.front());
            for(i=0; i<noPreNode.front()->neighbors.size(); i++) {
                if(--nodeIndegree[noPreNode.front()->neighbors[i]] == 0) {
                    noPreNode.push(noPreNode.front()->neighbors[i]);
                }
            }
            noPreNode.pop();
        }
        return result;
    }

    void getIndegree(vector<DirectedGraphNode*> &graph, map<DirectedGraphNode*, int> &nodeIndegree) {
        int size = graph.size();
        for(int i=0; i<size; i++) {
            int size2 = graph[i]->neighbors.size();
            for(int j=0; j<size2; j++) {
                nodeIndegree[graph[i]->neighbors[j]]++;
            }
        }
    }
};


基于AIS动态数据与AI结合得经纬度标示算法 港口船舶智能标注系统 本系统通过结合摄像头视频流和AIS船舶动态数据,利用AI技术实现船舶实时标注功能。系统架构包括视频流处理、AIS数据解析、船舶检测匹配和坐标映射转换等模块。关键技术步骤: AIS数据获取:通过本地接收机或网络API获取船舶位置、航速等信息 AI目标检测:采用YOLOv8等模型识别视频中的船舶 船舶匹配算法:通过位置投影或运动轨迹匹配视频目标与AIS数据 坐标映射:使用相机标定技术将经纬度转换为图像坐标 实时标注:在视频画面上叠加船名、位置等关键信息 应用场景包括港口监控、智能海事管理和 阅读详情

相关推荐

2025-08-19利用opencv检测图片中文字及图片的坐标

该代码实现了图片中中文文本的检测与位置计算功能。主要使用CnOcr库进行OCR识别,结合OpenCV和PIL库处理图像。功能包括:1)检测图片中的中文文本及位置坐标;2)计算每个文本的像素宽度和高度;3)在原图上绘制文本边界框并标注识别结果。代码通过put_chinese_text函数解决中文显示问题,最终输出各文本内容、坐标位置及尺寸信息。适用于需要获取图片中文字精确位置的应用场景。

m0_58149406的博客 404

Lintcode 127. Topological Sorting (Medium) Python

题目:https://www.lintcode.com/problem/topological-sorting/description 思路:拓扑排序 1.统计所有点的入度 2.将入度为0的加入队列 3.bfs """ Definition for a Directed graph node class DirectedGraphNode: def __init__(self, x): ...

Amber F 的博客 396

解决rt-thread上使用SPI+DMA

首先阐述下遇到的问题 使用SPI读取icm20602数据,读取频率为1000hz,使用stm32f407主控,发现CPU占用率达到了70%,将此线程注释掉后CPU占用率掉到25%,看来这里要着重优化,便萌生了使用DMA的想法。 使用SPI+DMA要进行的配置 1.开启RTT设备驱动。点击自己的工程 ->RT-Thread Setting2.在board.hzhong3.在board.c文件里加入以下函数,此函数受设备框架调用 void HAL_SPI_MspInit(SPI_HandleTypeDef

响望的博客 8095

[leetcode] 127. Word Ladder 解题报告

题目链接: https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord toendWor

小榕流光的专栏 8175

豆包大模型API调用两种方法

ps: 因为是自用,所以这里图方便采用硬编码密钥的形式;如果有需求/安全风险,建议参考官方教程设置ARK_API_KEY环境变量的方式。坏处是:看别人的教程走了一大圈弯路hh;基础调用的话方法一就可以满足了,也比较方便快捷,容易上手。将 <Model> 替换为 Endpoint ID即可。创建就按照提示填写信息就行,需要实名认证,有免费额度。绿框框里就是Endpoint ID。

kkkkkkkkkasey的博客 1万+

别再用Excel画波特图了!用LTspice免费仿真软件,5分钟搞定电路传递函数分析

本文介绍了如何利用LTspice免费仿真软件高效绘制波特图,解决传统Excel绘图效率低、精度不足的问题。通过详细的操作步骤和实战案例,展示LTspice在电路传递函数分析中的自动化计算、专业级精度和完整特性展示等优势,帮助工程师快速完成频率响应分析。

weixin_42524226的博客 467

lintcode:Topological Sorting

Given an directed graph, a topological order of the graph nodes is defined as follow:For each directed edge A -> B in graph, A must before B in the order list. The first node in the order can be any n

知之可否 601

数字IC设计技能篇之如何在RTL级降低功耗?

以上就是本次降低芯片功耗的部分心得和收获,由于是在芯片设计阶段还没办法后端直接给出power的报告,前端一般使用spyglass和fsdb来进行功耗分析,后续会更新如何使用spyglass在前仿阶段进行power的分析,以及持续更新有关RTL级降低功耗的相关内容。

mzynk的博客 1208

LintCode 127-拓扑排序

本人电子系,只为一学生。心喜计算机,小编以怡情。 思路: 1、用字典存储每个节点的入度 2、用队列存储入度为零的节点 3、BFS static public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) { // write your code here

Jason__Liang的博客 498

九齐NY8A051F低功耗模式全解析:从待机到休眠的省电实战

本文全面解析台湾九齐NY8A051F MCU的低功耗模式,从待机到休眠的省电实战技巧。通过实测数据展示四种工作模式的电流消耗差异,提供模式切换的标准流程和唤醒时序优化策略,并分享外围电路省电设计和低电压检测的实战应用,帮助开发者实现物联网设备的超长续航。

weixin_30839881的博客 415

[RK3568 Android11] 教程之原厂SDK源码适配AIO-3568J板子跑起来

目录 一、前提 二、修改dts设备树 三、适配板子的dts 四、编译系统烧入固件验证 一、前提 因源码是直接使用原厂的SDK,没有使用firefly配套的SDK源码,所以手上这块firefly aio-3568j板子在硬件设计上与原厂使用的板子会有些差异;就是因为这些差异会导致新固件烧入后,debug串口打印许多错误信息,可能的话,系统还会崩溃掉,跑不起来; 主要差异在哪里呢?比如: ● DDR类型和品牌多样性; ...

~未来可期~ 1749

LintCode 127. Topological Sorting

题目 思路 1.找到所有结点的入度 2.找到所有入度为0的结点。注意,这里有坑,入度为0的结点有可能不止有一个。 3.BFS 代码 """ Definition for a Directed graph node class DirectedGraphNode: def __init__(self, x): self.label = x ...

SparkSnail 344

如何使用VNC对openEuler系统进行远程图形化操作

本文详细介绍了在openEuler系统中配置VNC远程图形化操作的方法。主要内容包括:1)服务端配置:安装GNOME图形界面和TigerVNC服务,创建VNC用户并设置密码,配置VNC服务启动脚本;2)客户端连接:使用VNC Viewer等工具连接服务器;3)常见问题解决方法。通过5901端口建立连接后,用户可远程操作openEuler图形界面,适用于需要图形化管理的场景。

2501_93651177的博客 322

[Lintcode]127. Topological Sorting

127. Topological Sorting 本题难度: Medium Topic: Search & Recursion Description Given an directed graph, a topological order of the graph nodes is defined as follow: For each directed edge A -> B...

weixin_30575309的博客 131

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

LintCode_127 Topological Sorting

Given an directed graph, a topological order of the graph nodes is defined as follow: For each directed edge A -> B in graph, A must before B in the order list.The first node in the order can be

Bernini @ buffalo 283

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

lintcode 127: Topological Sorting 拓扑排序

https://www.lintcode.com/problem/topological-sorting/my-submissions 问题描述 给定一个有向图,图节点的拓扑排序定义如下: 对于图中的每一条有向边 A -> B , 在拓扑排序中A一定在B之前. 拓扑排序中的第一个节点可以是图中的任何一个没有其他节点指向它的节点. 针对给定的有向图找到任意一种拓扑排序的顺序 The to...

zero 304

Lintcode127 Topological Sorting solution 题解

【题目描述】 Given an directed graph, a topological order of the graph nodes is defined as follow: ·For each directed edgeA -> Bin graph, A must before B in the order list. ·The first node in the ord

nekomilk的博客 280

lintcode127- Topological Sorting- medium

Given an directed graph, a topological order of the graph nodes is defined as follow: For each directed edgeA -> Bin graph, A must before B in the order list. The first node in the order ca...

weixin_30699443的博客 145

Topological Sorting 拓扑排序系列题目/课程表/宽度优先

LintCode 127. Topological Sorting 题目链接 给定一个有向图,图节点的拓扑排序定义如下: 对于图中的每一条有向边 A -> B , 在拓扑排序中A一定在B之前. 拓扑排序中的第一个节点可以是图中的任何一个没有其他节点指向它的节点. 针对给定的有向图找到任意一种拓扑排序的顺序. 思路: 首先要找到starting node,也就是in-degree = 0...

keke今天又学算法了 246

LintCode-127: Topological Soring 拓扑排序 (经典题)

Topological Soring可以用BFS和DFS实现。 解法1:BFS宽搜 基于queue。先做预处理,把统计所有节点的in-degree,用一个hashmap存起来。然后把in-degree为0的节点放入queue中(同时也放入结果中),然后进入while循环,每次把queue中的front元素取出来,遍历其邻居,把每个邻居的in-degree减一。若该邻居的in-degree降到...

roufoo的博客 736

127.Topological Sorting-拓扑排序(中等题)

拓扑排序 题目给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A–> B,则A必须排在B之前   拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点   找到给定图的任一拓扑排序注意事项 你可以假设图中至少存在一种拓扑排序 说明 样例对于下列图: 这个图的拓扑排序可能是: [0, 1, 2, 3, 4, 5] 或者 [0, 2, 3, 1, 5, 4] 或者

三色旗飞扬 464

127. Topological Sorting——拓扑排序(BFS)

DescriptionGiven an directed graph, a topological order of the graph nodes is defined as follow:For each directed edge A -&gt; B in graph, A must before B in the order list.The first node in the order...

lighthear的博客 1220

lintcode-拓扑排序-127

给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则A必须排在B之前   拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点   /** * Definition for Directed graph. * struct DirectedGraphNode { * int label; * vector neighbors;

我有一个梦想 1261

lintcode练习-127. 拓扑排序

描述 给定一个有向图,图节点的拓扑排序被定义为:   对于每条有向边A--&gt; B,则A必须排在B之前   拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点     找到给定图的任一拓扑排序   你可以假设图中至少存在一种拓扑排序 您在真实的面试中是否遇到过这个题?  是 说明 Learn more about representation of graphs ...

云中寻雾的博客 607

【精】领扣LintCode算法问题答案:127. 拓扑排序

领扣LintCode算法问题答案-127. 拓扑排序 目录127. 拓扑排序描述样例 1:挑战题解鸣谢 127. 拓扑排序 描述 给定一个有向图,图节点的拓扑排序定义如下: 对于图中的每一条有向边 A -> B , 在拓扑排序中A一定在B之前. 拓扑排序中的第一个节点可以是图中的任何一个没有其他节点指向它的节点. 针对给定的有向图找到任意一种拓扑排序的顺序. 你可以假设图中至少存在一种拓扑排序 样例 1: 例如以下的图: 拓扑排序可以为: [0, 1, 2, 3, 4, 5] [0, 2.

二当家的白帽子 291
上一篇: C++之STL中set的用法总结
下一篇: LeetCode四道Permutations问题详解
vanturman
博客等级 码龄9年 96粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值