Ch4-2: find if 2 given vertices are connected in a directed map,BFS + JUST fix the DFS recursion

Matlab实现使用遗传算法的混合能源系统优化.rar 立即下载

In this problem, there are 2 ways: BFS or DFS.

Hawstein used BFS:

#include 
#include 
#include 
#include 
using namespace std;

const int maxn = 100;
bool g[maxn][maxn], visited[maxn];
int n;
queue q;

void init(){
    memset(g, false, sizeof(g));
    memset(visited, false, sizeof(visited));
}
bool route(int src, int dst){
    q.push(src);
    visited[src] = true;
    while(!q.empty()){
        int t = q.front();
        q.pop();
        if(t == dst) return true;
        for(int i=0; i>n>>m;
    for(int i=0; i>u>>v;
        g[u][v] = true;
        if(i==0){cout<< "start of connections--" << endl;}
        cout<"<

input:

8 11
0 1
1 0
1 5
2 3
2 4
4 7
5 2
5 7
6 2
6 4
7 4

Output:

start of connections-
0->1
1->0
1->5
2->3
2->4
4->7
5->2
5->7
6->2
6->4
7->4
end of connections--
1
1
0
1

Here is the DFS solution but have errors in somecases, such as 0->3. The reason is that the case for v[i] didn't considered visited condition, then go to return false directly.

/*Given a directed graph, design an algorithm to find out whether there is a route between two nodes
*/
#include 
#define N 8
using namespace std;

int nextNbr(int g[][N], int i, int j){
    while((-1"<"<output:
1st case: 1
0: 0
1: 1
2: 0
3: 0
4: 0
5: 1
6: 0
7: 0
0
0: 0
1: 1
2: 0
3: 0
4: 1
5: 1
6: 0
7: 1


After I learned from zyli's code, The problem is fixed.

The reason is that the previous one fall into narrow path without go back so failed in some corner cases: for example: 0->3

Here is the final version of DFS with recursion.

/*Given a directed graph, design an algorithm to find out whether there is a route between two nodes
*/
#include 
#define N 8
using namespace std;

int nextNbr(int g[][N], int i, int j){
    while((-1"<"<J:  0, tim: 3
v[0]=0 
v[2]=0 
J:  1, tim: 5
v[5]=0 
J:  1, tim: 5
v[1]=0 
J:  1, tim: 5
1


Here is the sample analysis of the recursion for routeDFS(0,3)=1


基于斜坡补偿的Buck电路峰值电流+电压电流双闭环控制【附说明文档】(Simulink仿真实现)优快云首页发布文章优快云同步助手LLC谐振变换器开环+闭环软启动仿真设计(Simulink 内容概要:本文围绕三电平VIENNA整流器的双闭环控制与中点电位平衡问题展开仿真研究,提出了一种基于上下电容电压差值补偿的中点电位平衡策略。该策略在传统电压电流双闭环PI控制的基础上,引入电容电压差值作为反馈信号,通过SVPWM调制中冗余小矢量的时间分配实现中点电荷的定向调节,从而在不改变主控制结构的前提下有效抑制中点电位偏差。仿真结果表明,该方法不仅确保直流母线电压稳定跟踪800V给定值,还能显著提升上下电容电压的均衡度,同时保持输入电流低谐波水平,验证了控制策略的可行性和有效性。; 适合人群:电力电子、电气自动化及相关专业的研究生、科研人员及工程技术人员,具备一定的MATLAB/Simulink仿真基础和电力电子拓扑知识背景者更佳。; 使用场景及目标:①掌握三电平VIENNA整流器的工作原理与中点电位波动的成因;②理解电压电流双闭环PI控制的设计要点与参数整定原则;③学习基于差值补偿的中点电位平衡控制策略及其与SVPWM的协同机制;④应用于充电桩、航空电源、通信电源等中大功率单向整流场合的控制系统设计与优化。; 阅读建议:建议结合文中提供的仿真模型与参数设置进行动手实践,重点关注中点电位平衡投入前后电容电压波形的变化,对比分析不同工况下的系统动态响应,深入理解控制策略的作用机理。同时可进一步探索在电网不平衡、轻载等复杂工况下的控制性能,以拓展应用场景。 立即下载

相关推荐

VSCODE编辑+EIDE插件调试+STM8_Debug插件调试+IAR_STM8工具链编译开发项目

1. 安装 在VS CODE安装EIDE、STM8-Debug插件 2. 使用 EIDE 创建一个 8/16 Bit MCU 项目 新建虚拟文件夹,把工程外部的文件添加进虚拟文件夹,实现工程和源文件之间的关系,而不用实际把源文件复制到工程目录下。 EIDE工程和源文件实现分开放置,EIDE(EIDE工程)、EWSTM8(IAR STM8工程)、Project(ST Visual Develop工程) 选择工具链 烧录工具选择 头文件包含目录 添加宏定义 3. 编译 4. 使用STM8

TrueLink的博客 5502

存储结构和cache的基本原理

cache,高速缓冲存储器,位于CPU和主存储器之间DRAM,规模较小,速度较快,通常由SRAM构成。 简单的cache基本原理:每次请求一个字,每个块也由一个单独的字组成。如果cache中没有要访问的数据导致缺失就从主存调入cache。 cache:在cache中为主存中的每个字分配一个位置 ...

Travor Philip的blog 1446

黏菌算法(Slime Mould Algorithm,SMA)

这是一篇关于黏菌算法的总结博客,包含算法思想,算法步骤,求函数最值(Python实现),算法改进等,持续更新ing

weixin_46838605的博客 1万+

Direct Mapped Cache

Introduction Lets assume, as we did for fully associate caches that we have: 128 slots 32 bytes per slotParking Lot Analogy Suppose we have 1000 parking spots.

老梁的专栏 4573

MQTT(一)C#使用 MQTTnet 快速实现 MQTT 通信(文末有完整Demo下载)

目录 MQTT(一)C#使用 MQTTnet 快速实现 MQTT 通信(文末有完整Demo下载) MQTT(二)在windows64位上安装Python环境 MQTT(三)Python客户端+net客户端+net服务端 简单通信 MQTT(四)树莓派开机自动运行Python客户端 MQTT(五)EMQ开源MQTT消息服务器 1 什么是 MQTT ? MQTT(Message Que...

pw的博客 10万+

Arcmap计算土方量教程

1. 创建现状TIN 结果数据: 2. 创建设计TIN 结果数据: 注意:现状TIN和设计TIN构建时候的坐标系不同,设置坐标系见最后的注意事项第1点,就是import(导入)那步骤,现状TIN构建时选择高程点数据或者等高线数据,设计TIN构建时候选择洱海面数据 3. TIN转栅格 改成你要存的数据库,注意中间不要有汉字 结果数据(设计TIN转栅格的同理) 4. 裁剪现状TIN转成的栅格 结果数据: 5. 计算土方量 结果数据: ...

欢迎关注,共同进步! 5017

__direct_map 函数解析之影子页表的构建

阅读前,请先阅读前两篇文章 《tdp_page_fault 函数解析之level,gfn变量的含义》 《KVM的vMMU相关数据结构及其影子页表关系分析》 感谢Intel OTC的 wufeng、OenHan、chenhe、ruanshua、社区的@heart2011给予的帮助和支持  本文分析__direct_map函数中关于构建影子页表的过程,着重关心gfn和lev

六六哥的博客 2628

《Long-CLIP: Unlocking the Long-Text Capability of CLIP》

总的来说,Long-CLIP是一个改进的视觉-语言预训练模型,它通过知识保留的位置上插值和主要成分匹配策略,有效解决了原始CLIP模型在处理长文本输入时的限制。这一模型不仅在长文本图像检索任务中表现出色,而且在零样本图像分类任务中保持了与原始CLIP相当的性能,同时还能以即插即用的方式增强图像生成任务中的长文本处理能力。

Angelina_Jolie的博客 1381

cc150做题计划

计划:12.29.2013 - 1.29.2013 做完第一遍题目,然后2月做leetcode。 2月底面试前完成200题C++。 这里记录一下ctci学习的参考资料。 1. hawstein的blog 2. https://github.com/ChengTian/CtCI 3. absolute C++ 5th 4. Primer C++ 5. learncpp:http

纯黑老白的专栏 2909

数字盲水印嵌入和提取的实现基于小波变换DWT算法

数字盲水印是指在不知道原始图像的情况下,在其上嵌入水印并在后续的提取过程中能够恢复出原始水印的一种技术。本文介绍的数字盲水印嵌入和提取的实现方法基于小波变换DWT算法。具体来讲,DWT将信号或图像进行二次下采样和卷积滤波,使得高频分量的图像信息被集中在一个小区域内,方便进行嵌入和提取操作。其中 a 表示嵌入强度,而 watermark 则是用于嵌入的水印图像,这里我们嵌入的是一个全灰的图像。通过以上的代码,我们可以实现数字盲水印的嵌入和提取,并在一定程度上保护数字图片的版权安全。

HackWhisper的博客 516

量化 insider 爆料! 散户打板必亏?(打板必看!量化资金如何“隔日砸盘”收割散户)

量化开发 insider 爆料💥 散户追的涨停板📉,可能是机构故意做的陷阱!本文从技术视角拆解“涨停陷阱”,帮你看清市场真实逻辑🧠。

XiaoqiangClub的博客 459

ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods

In this problem, it is important to fully understand the definition of least common ancestor of 2 nodes: it means: (from leetcode blog: 点击打开链接) _______3______ / \

纯黑老白的专栏 1330

最详细的 SAP ABAP Web Service 创建和消费步骤讲解

本文原版发表于 SAP 官方社区,当时用的英文版写作,标题为:Step by step to create, consume and trace web service in ABAP system https://blogs.sap.com/2014/05/20/step-by-step-to-create-consume-and-trace-web-service-in-abap-system/ 总共阅读量超过 18 万。 How to create web service provider in A

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 3154

Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)

这个有点像DSP FPGA Chang Choo的题目,一般是放大插值,DFT,IIR之类的Verilog题目。 题目在这:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees.

纯黑老白的专栏 1295

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Ch2-2: return the nth to the last node data of a singly linked list

In this problem, I need to find the nth to the last node data of a singly linked list.  In order to do so, one good way is to use recursion, since it is automatically put recursion part into stack an

纯黑老白的专栏 1290

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

1/22/2014: 纪念第一个C++ programmer电面--F家

经过20天的CC刷题(其实是学习),才刚刚做到第四章。看看其他非ACM牛人,都是leetcode,CC,EPI滚瓜烂熟才去面。而且onsite确实是靠实力,要有做过大project之后才能真正融会贯通C++精神。 Anyway,谢谢郭志给的refer,不然连电面的机会都没有。 开头是C++基础题: 1. Fib的2种写法:recursive和DP 2. Struct和Class的区别;在C

纯黑老白的专栏 1274

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Ch1-7: clear the row i and column j iff a[i][j]==0

in this problem, both monish001 and hawstein use O(n^2) solution which first tranverse the char** a to find the a[i][j]==0 and mark that row/col to be: TRUE. Then the send loop make the TURE row/col

纯黑老白的专栏 1207

复习一下dfs和bfs 用java

dfs 和 bfs都是用来找从source 到 target的path的算法。 dfs用的是recursion,bfs则用queue

纯黑老白的专栏 1191

Ch3-1: use a single array to implement three stacks.

Here it explains why Hawstein use this in his constructor, because he use size a parameter in his constructor while "size" is also a member variable(it's private), so he use this->size = size, explici

纯黑老白的专栏 1097

ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one

// Ch4.7: You have two very large binary trees: T1, with millions of nodes, and T2, // with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 #include #include #includ

纯黑老白的专栏 1030

复习recursion

1. 如果这样 private void lop(int x, int size, double[] jerry){ if(x==size-1){ jerry[x] = -1; return; } if(jerry[x+1]==0){ jerry[x+1] = jerry[x]- 1; //if so, lop(x+1, size, jerry); }

纯黑老白的专栏 1009

Ch2-5: find the beginning of loop in a circular linked list---two solutions

Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. DEFINITION Circular linked list: A (corrupt) linked list in which a node’s next pointer poin

纯黑老白的专栏 986

Ch3-2: implement min() for a stack with O(1)

Here will show the thinking flow from bad to good. idea 1: const int MAX_INT = ~(1<<31);//2147483647 struct node{ int val, min; }; class StackWithMin{ public: StackWithMin(int size){

纯黑老白的专栏 956

Ch8.8: 8 queen problem

In order to solve this problem efficiently, need to use backtracking method instead of iterations. Aka: Backtracking is a general algorithm for finding all (or some) solutions to some computational p

纯黑老白的专栏 949

Ch3-3: popat(int idx) of setofstack

Continue to use class to design solution, also, start to use STL, such as container (vector, stack, set, ...). save time and to be more professional. The design without popat method is similar to the

纯黑老白的专栏 948
上一篇: Ch4-1: identify if a tree is "balanced" (注:CTCI 第四版和第五版的定义不一样)
下一篇: Ch4.3: transform an ordered array into a balanced Tree
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值