leetcode图论刷题总结

LeetCode Hot100 刷题笔记(4)—— 二叉树、图论 一、二叉树:二叉树的中序遍历,二叉树的最大深度,翻转二叉树,对称二叉树,二叉树的直径,二叉树的层序遍历,将有序数组转换为二叉搜索树,验证二叉搜索树,二叉搜索树中第 K 小的元素,二叉树的右视图,二叉树展开为链表,从前序与中序遍历序列构造二叉树,路径总和 III,二叉树的最近公共祖先,二叉树中的最大路径和。 二、图论:岛屿数量,腐烂的橘子,课程表,实现 Trie (前缀树)。 阅读详情

理论

1. 基本概念

1.1 顶点

图中的数据元素,我们称之为顶点,图至少有一个顶点(非空有穷集合)

1.2 边

顶点之间的关系用边表示。

1.3 度

度表示一个顶点包含多少条边,在有向图中,还分为出度和入度,出度表示从该顶点出去的边的条数,入度表示进入该顶点的边的条数。

1.4 无向图和有向图

边表示的是顶点之间的关系,有的关系是双向的,比如同学关系,A是B的同学,那么B也肯定是A的同学,那么在表示A和B的关系时,就不用关注方向,用不带箭头的边表示,这样的图就是无向图。有的关系是有方向的,比如父子关系,师生关系,微博的关注关系,A是B的爸爸,但B肯定不是A的爸爸,A关注B,B不一定关注A。在这种情况下,我们就用带箭头的边表示二者的关系,这样的图就是有向图。

1.5 无权图和带权图

对于一个关系,如果我们只关心关系的有无,而不关心关系有多强,那么就可以用无权图表示二者的关系。对于一个关系,如果我们既关心关系的有无,也关心关系的强度,比如描述地图上两个城市的关系,需要用到距离,那么就用带权图来表示,带权图中的每一条边一个数值表示权值,代表关系的强度。

2. 图的存储

2.1 邻接矩阵存储

在这里插入图片描述

2.2 邻接表存储

在这里插入图片描述

3. 图的搜索

3.1 深度优先 DFS

深度优先搜索就是“一条路走到黑”,从源顶点开始,一直走到没有后继节点,才回溯到上一顶点,然后继续“一条路走到黑”

3.2 广度优先 BFS

广度优先搜索就像水面上的波纹一样一层一层向外扩展

T841. 钥匙和房间 (有向图找一条路径、DFS) **

在这里插入图片描述

  • 思路分析
    有向图
    首先必须从0号房间开始搜索,一直搜,不需要回溯(只要找到一条路径就可以),深度为len返回true
class Solution {
    //不需要回溯:只要找到一条符合的路径即可
    boolean[] visited;//记录是否访问过
    int deep;//记录访问深度
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        int len = rooms.size();
        visited = new boolean[len];//false
        dfs(rooms,0);//从0号房间开始
        if(deep == len){return true;}
        return false;
    }
    public void dfs(List<List<Integer>> rooms,int index){
        visited[index] = true;
        deep++;//遍历依次深度就加1,由于设置的是没访问过的才会递归dfs,因此只要递归次数达到len,就说明满足条件
        for(int i:rooms.get(index)){//从该房间往可能的房间方向遍历
            if(visited[i]!=true){//访问的不需要再访问
                dfs(rooms,i);
            }
        }
    }
}

T797. 所有可能的路径(有向图找所有路径:DFS、回溯) **

在这里插入图片描述
所有路径需要回溯

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        path.add(0);//0号房间需要手动加进去
        dfs(graph,0);
        return res;
    }
    public void dfs(int[][] graph, int index){
        //终止条件
        if(index == graph.length-1){//审题:终点必然是n-1
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i=0;i<graph[index].length;i++){//遍历graph每个元素
            path.add(graph[index][i]);
            dfs(graph,graph[index][i]);
            path.removeLast();//回溯
        }
        return;
    }
}

T127. 单词接龙 (无向图求最短路径、BFS) ***

在这里插入图片描述

  • 思路分析
    在这里插入图片描述
    简历单词之间的联系
    广度优先搜索找到的必然是最短路径
class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        HashSet<String> wordSet = new HashSet<>(wordList);//把wordlist的单词放入hash表中
        if(!wordSet.contains(endWord)){return 0;}//特殊情况:不存在目标单词
        Queue<String> que = new LinkedList<>();
        que.offer(beginWord);
        Map<String,Integer> map = new HashMap<>();//单词:路径长度
        map.put(beginWord,1);

        while(!que.isEmpty()){
            String word = que.poll();
            int path = map.get(word);//取出第一个单词和其路径长度
            //对单词的每个字符进行修改遍历
            for(int i=0;i<word.length();i++){
                //修改字符
                char[] chars = word.toCharArray();
                for(char c='a';c<='z';c++){
                    chars[i] = c;
                    String newWord = String.valueOf(chars);//char变成string
                    if(newWord.equals(endWord)){
                        return path+1;//如果新单词与目标一致,返回结果
                    }
                    if(wordSet.contains(newWord) && !map.containsKey(newWord)){//存在该单词,且未使用过
                        map.put(newWord,path+1);
                        que.offer(newWord);
                    }
                }
            }
        }
        return 0;
    }

}

T200. 岛屿数量(无向图:DFS/BFS)*

在这里插入图片描述

- 思路分析
遇到陆地就开始向四个方向搜索,直到遇到海洋就停止,搜索过的就做个标记;
岛屿数量就是dfs了多少次

class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        for(int m=0;m<grid.length;m++){
            for(int n=0;n<grid[0].length;n++){
                if(grid[m][n] == '1'){
                    dfs(grid,m,n);
                    count++;
                }
            }
        }
        return count;
    }
    void dfs(char[][] grid,int r,int c){
        //判断是否在表格中
        if(r<0||c<0||r>=grid.length||c>=grid[0].length){
            return;
        }
        //遇到非岛屿的
        if(grid[r][c] != '1') return;
        //记录遍历过的
        grid[r][c] = '2';
        //遍历四个方向
        dfs(grid,r-1,c);
        dfs(grid,r+1,c);
        dfs(grid,r,c-1);
        dfs(grid,r,c+1);
    }
}
class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j] == '1'){
                    bfs(grid,i,j);
                    count++;
                }
            }
        }
        return count;
    }
    public void bfs(char[][] grid,int i,int j){
        Queue<int[]> que = new LinkedList<>();
        que.offer(new int[]{i,j});
        while(!que.isEmpty()){
            int[] temp = que.poll();
            i = temp[0];
            j = temp[1];
            if(i>=0&&i<grid.length&&j>=0&&j<grid[0].length && grid[i][j] == '1'){
                grid[i][j] = '0';
                bfs(grid,i+1,j);
                bfs(grid,i-1,j);
                bfs(grid,i,j+1);
                bfs(grid,i,j-1);
            }
        }
    }
}

T417. 太平洋大西洋水流问题(无向图:多源BFS)****

在这里插入图片描述

  • 思路分析
    题目翻译: 某个点出发,能从四个方向到达比它小的点,求整个集合中能够到达左上边界(太平洋) 右下边界(大西洋)的有哪些点
    思路解决: 遍历所有点太麻烦,直接从边界点出发,能够到达比它大的点,能够到达的就标记为true;分别从太平洋和大西洋出发,进行两次bfs,最终看结果res中两次标记为true的点就是所求。
    从结果开始遍历!
    BFS思路:以队列为基础,队列存放点坐标。传入点坐标,只要队列非空,就取点坐标,进行四个方向移动,更新,符合条件的入队。
  • 代码实现
class Solution {
    static int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};//移动的四个方向
    static int m;
    static int n;
    static int[][] nums;
    //反向推导:从边界出发,水往高处流,能到达就记为true
    public List<List<Integer>> pacificAtlantic(int[][] heights) {
        m = heights.length;
        n = heights[0].length;//行和列
        nums = heights;
        boolean[][] pacific = new boolean[m][n];//能否到达太平洋
        boolean[][] atlantic = new boolean[m][n];//记录能否到达大西洋
        //记录能否到达太平洋
        for(int i=0;i<m;i++){
            bfs(i,0,pacific);
        }
        for(int j=1;j<n;j++){
            bfs(0,j,pacific);
        }
        //记录能否到达大西洋
        for(int i=0;i<m;i++){
            bfs(i,n-1,atlantic);
        }
        for(int j=0;j<n-1;j++){
            bfs(m-1,j,atlantic);
        }
        //算结果:遍历所有的节点,能够同时到达太平洋和大西洋的就add
        List<List<Integer>> res = new ArrayList<>();
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(pacific[i][j] && atlantic[i][j]){
                    List<Integer> path = new ArrayList<>();
                    path.add(i);
                    path.add(j);
                    res.add(path);
                }
            }
        }
        return res;
    }
    //广度优先搜索的逻辑
    //取出其中一个点
    //往四个方向移动,判断条件是否符合
    //入队
    public static void bfs(int row,int col,boolean[][] ocean){
        if(ocean[row][col]){return;}
        ocean[row][col] = true;
        Queue<int[]> que = new LinkedList<>();//x,y坐标
        que.offer(new int[]{row,col});
        while(!que.isEmpty()){
            int[] temp = que.poll();
            for(int[] dir:dirs){//往四个方向移动
                //计算新坐标
                int newRow = temp[0] + dir[0];
                int newCol = temp[1] + dir[1];
                //判断是否符合条件:边界条件+高度+未处理过
                if(newRow>=0&&newRow<m&&newCol>=0&&newCol<n&& nums[temp[0]][temp[1]]<=nums[newRow][newCol]&& !ocean[newRow][newCol]){
                    ocean[newRow][newCol] = true;
                    que.offer(new int[]{newRow,newCol});
                }
            }
        }
    }
}

T1020. 飞地的数量 (无向图:DFS/多源BFS) ***

在这里插入图片描述

  • 解法1:DFS搜索
class Solution {
    //从边界搜,如果遇到1,就设置为true;最终grid==1,false的就是飞地
    public static int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};
    public static int m,n;
    public static boolean[][] visited;
    public int numEnclaves(int[][] grid) {
        int count = 0;
        m = grid.length;
        n = grid[0].length;
        visited = new boolean[m][n];
        //从边界开始搜
        //左右两个边界
        for(int i=0;i<m;i++){
            dfs(grid,i,0);
            dfs(grid,i,n-1);
        }
        //上下两个边界
        for(int j=0;j<n;j++){
            dfs(grid,0,j);
            dfs(grid,m-1,j);
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(!visited[i][j] && grid[i][j]==1){
                    count++;
                }
            }
        }
        return count;
    }
    public static void dfs(int[][] grid,int i,int j){
        if(i<0 || i>=m || j<0 || j>=n || grid[i][j] == 0 || visited[i][j] ){
            return;//终止条件
        }
        visited[i][j] = true;
        for(int[] dir:dirs){
            dfs(grid,i+dir[0],j+dir[1]);
        }
    }
}
  • 解法2:多源BFS
class Solution {
    //从边界搜,如果遇到1,就设置为true;最终grid==1,false的就是飞地
    public static int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};
    public static int m,n;
    public static boolean[][] visited;
    public int numEnclaves(int[][] grid) {
        int count = 0;
        m = grid.length;
        n = grid[0].length;
        visited = new boolean[m][n];
        Queue<int[]> que = new LinkedList<>();

        //从边界开始搜
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(i==0||i==m-1||j==0||j==n-1){
                    if(grid[i][j]==0) continue;
                    visited[i][j] = true;
                    que.offer(new int[]{i,j});
                }
            }
        }
        while(!que.isEmpty()){
            int[] temp = que.poll();
            for(int[] dir:dirs){
                int newX = temp[0] + dir[0];
                int newY = temp[1] + dir[1];
                if(newX>=0 && newX<m && newY>=0 && newY<n && !visited[newX][newY] && grid[newX][newY]==1){
                    visited[newX][newY] = true;
                    que.offer(new int[]{newX,newY});
                }
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(!visited[i][j] && grid[i][j]==1){
                    count++;
                }
            }
        }
        return count;
    }

}

剑指 Offer II 105. 岛屿的最大面积(无向图:DFS) **

在这里插入图片描述
在岛屿数量题目基础上,dfs递归函数返回岛屿面积的大小

class Solution {
    public int maxAreaOfIsland(int[][] grid) {
        int res  = 0;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j] == 1){
                    res = Math.max(dfs(grid,i,j),res);
                }
            }
        }
        return res;
    }
    public int dfs(int[][] grid,int row,int col){
        if(row<0 || col<0 || row>=grid.length || col >= grid[0].length||grid[row][col]!=1){
            return 0;
        }
        grid[row][col] = 0;
        int res = 1;
        res = res + dfs(grid,row+1,col) + dfs(grid,row-1,col) +
            dfs(grid,row,col+1) + dfs(grid,row,col-1);
        return res;

    }
}

T207. 课程表(拓扑排序判断是否为有向无环图:邻接表+BFS)

在这里插入图片描述
在这里插入图片描述

  • 入度表BFS
class Solution {
    //BFS 构造一个有向图
    //1.统计每个点的入度
    //2.度为0的入队
    //3.出队:课程数-1,相连的节点的度减去1,若度为0就入度
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        int[] indegree = new int[numCourses];//统计入度
        List<List<Integer>> joinList = new ArrayList<>();//记录相连的课程,索引代表当前课程
        for(int i=0;i<numCourses;i++){
            joinList.add(new ArrayList<>());
        }
        for(int[] item:prerequisites){
            indegree[item[0]]++;//入度数
            joinList.get(item[1]).add(item[0]);//相连的节点
        }
        Queue<Integer> que = new LinkedList<>();
        for(int i=0;i<indegree.length;i++){
            if(indegree[i]==0){
                que.add(i);//度为0 入队
            }
        }
        while(!que.isEmpty()){
            int temp = que.poll();
            numCourses--;
            for(int item:joinList.get(temp)){
                indegree[item]--;
                if(indegree[item]==0){
                    que.add(item);
                }
            }
        }
        return numCourses==0;
    }
}

T684. 冗余连接(无向图中找环:邻接表BFS/并查集)

在这里插入图片描述

  • 解法1:邻接表,入度表,BFS

参考题:课程表,不同的是度数为1入队。每次把1的出队,并且删除掉与其相连的边,最终剩下的结果中,两边入度都大于1的为所求

class Solution {
    public int[] findRedundantConnection(int[][] edges) {
        int size = edges.length;
        int[] indegree = new int[size];//构造入度表 下标0表示1
        List<List<Integer>> joinList = new ArrayList<>();//邻接表
        for(int i=0;i<size;i++){
            joinList.add(new ArrayList<>());
        }
        for(int[] item:edges){//下标统一做减1处理
            indegree[item[0]-1]++;
            indegree[item[1]-1]++;
            joinList.get(item[0]-1).add(item[1]-1);
            joinList.get(item[1]-1).add(item[0]-1);
        }
        Queue<Integer> que = new LinkedList<>();
        for(int i=0;i<size;i++){
            if(indegree[i]==1){
                que.offer(i);//度为1 就入队
            }
        }
        while(!que.isEmpty()){
            int item = que.poll();
            for(int i:joinList.get(item)){
                indegree[i]--;
                if(indegree[i]==1){
                    que.offer(i);
                }
            }
        }
        //前边是找环的思路 同题 课程表
        //从后往前遍历:如果两个相连的点度数都大于1
        for(int i=size-1;i>=0;i--){
            if(indegree[edges[i][0]-1]>1&&indegree[edges[i][1]-1]>1){
                return edges[i];
            }
        }
        return new int[0];
    }
}

华为机考题:循环依赖(有向图中找环并输出节点)

在这里插入图片描述

为了方便对原题修改了一下
//输入:
8 (模块名称:模块里的函数)
A a1 a2 a3
B b1 b2 b3
C c1 c2 c3
D d1 d2 d3
E e1 e2 e3
F f1 f2 f3
H h1 h2
G g1 g2 g3
5 (主函数:需要用到的函数,即a1依赖b1,b2)
a1 b1 b2
c2 a3
d2 e1
e1 b1 f2 g3
b2 b1 c2 d1 h1
//输出:
A B C D E
package HuaWei;
/**
 * 循环依赖 思路不算难 但是过程很繁琐 java构建输入太过于复杂
 * 考察的是:在一个有向图中找环,并输出环的节点。
 * 解决方法:构建一个邻接表,利用dfs搜索,用res记录路径
 * https://blog.youkuaiyun.com/zhilamou7549/article/details/107092998思路见此
 * 为了简化,此题不再做起点要求,对整个图进行搜索找环,并且规定起始点为A
 */

import java.util.*;

public class Solution7 {
    static List<List<String>> res = new ArrayList<>();
    static List<String> path = new LinkedList<>();
    static String[]  numToStr;
    static boolean result;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = Integer.parseInt(sc.nextLine());
        numToStr = new String[m];
        int tempSize = m;
        int index = 0;
        TreeMap<String,List<String>> map = new TreeMap<>();//模块以及其对应的函数
        while(m>0){
            String[] line = sc.nextLine().split(" ");
            numToStr[index] = line[0];
            index++;
            List<String> list = new LinkedList<>();
            for(int i=1;i<line.length;i++){
                list.add(line[i]);
            }
            map.put(line[0],list);//模块名:对应函数的名字
            m--;
        }
        int n = Integer.parseInt(sc.nextLine());
        TreeMap<String,Set<String>> relation = new TreeMap<>();//各个模块之间的依赖关系
        while(n>0) {
            String[] parts = sc.nextLine().split(" ");
            Set<String> value = new HashSet<>();
            String key = null;
            for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                List<String> temp = entry.getValue();
                for (String str : temp) {
                    if (parts[0].equals(str)) {//找到和parts[0]对应的函数 对应的模块 作为key
                        key = entry.getKey();
                    }
                }
            }
            for (int i = 1; i < parts.length; i++) {//寻找value
                for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                    List<String> temp = entry.getValue();
                    for (String str : temp) {
                        if (parts[i].equals(str)) {//找到和parts[0]对应的函数 对应的模块 作为key
                            if(!entry.getKey().equals(key)){
                                value.add(entry.getKey());//给value赋值 记得去重
                            }
                        }
                    }
                }
            }
            relation.put(key, value);
            n--;
        }
//        System.out.println(map);
//        System.out.println(relation);
//        String target = sc.nextLine();//起点模块
        //处理逻辑:默认模块名从A开始排序了
        //1.构建邻接表
        //行表示当前模块,列表示对应模块,若行依赖于列,值为1
        
        int[][] matrix = new int[tempSize][tempSize];
        for(Map.Entry<String,Set<String>> entry: relation.entrySet()){
            int row = entry.getKey().charAt(0) - 'A';
            for(String str: entry.getValue()){
                int col = str.charAt(0) - 'A';
                matrix[row][col] = 1;
            }
        }
//        System.out.println(matrix);
        //2.dfs处理
        boolean[] visited = new boolean[matrix.length];
       for(int i=0;i<matrix.length;i++){
           for (int j = 0; j < matrix.length; j++) {
               //遇到值为1就进行搜索. 如果两次出现visited都为true说明出现了环
               if(matrix[i][j]!=0){
                   Arrays.fill(visited,false);
                   visited[j] = true;
                   dfs(matrix,visited,i,j);
               }
           }
       }
        if(result){
            System.out.println("yes");
            Set<String> set = new HashSet<>();
            for(List<String> list:res){
                for(String str:list){
                    set.add(str);
                }
            }
            String[] strResult = new String[set.size()];
            int i = 0;
            for(String str:set){
                strResult[i] = str;
                i++;
            }
            Arrays.sort(strResult);
            for(int j=0;j<strResult.length;j++){
                System.out.print(strResult[j]+" ");
            }
        }else{
            System.out.println("No!");
        }
//        System.out.println(res);

    }
    public static void dfs(int[][] matrix,boolean[] visited,int start,int cur_node){
        for(int col=0;col<matrix.length;col++){
            if(matrix[cur_node][col]!=0&&!visited[col]){
                if(col == start){//找到环了
                    res.add(new ArrayList<>(path));
                    result = true;
                    return;
                }
                visited[col] = true;
                path.add(numToStr[col]);
                dfs(matrix,visited,start,col);
                visited[col] = false;
                path.remove(numToStr[col]);
            }
        }
    }
}

T130. 被围绕的区域

【Unity】入门教程第一期-下载安装Unity编辑器(下) 讲解如何在Unity Hub中下载Unity编辑器以及IED环境 阅读详情

相关推荐

Python自动化监控:实时捕捉LOF基金溢价套利机会并微信预警

本文详细介绍了如何使用Python构建一个自动化监控系统,实时捕捉LOF基金的溢价套利机会。通过AKShare获取基金数据,结合雪球API计算实时溢价率,并利用企业微信API实现微信推送预警,帮助投资者高效、自动地发现并抓住转瞬即逝的套利机会。

weixin_28327051的博客 522

LeetCode刷题笔记之图论

LeetCode刷题笔记之图论

abtgu的博客 1081

固定翼飞行器建模(一)——六自由度动力学

前两期文章讲完了气动力和气动力矩的模型,本期将来讲解一下六自由度部分的动力学与运动学方程。

2302_80148937的博客 1万+

【刷题(12)】图论

【刷题(12)】图论

m0_51579041的博客 1222

在F1C100s上跑GBA游戏:手把手教你用Buildroot配置SDL和移植gpsp模拟器

本文详细介绍了在全志F1C100s开发板上配置Buildroot、移植SDL库和gpsp模拟器的完整流程,帮助开发者实现在ARM9平台上流畅运行GBA游戏。内容涵盖环境配置、交叉编译、常见问题解决及性能优化技巧,特别针对'段错误'和'无声音'等典型问题提供实用解决方案。

weixin_42566424的博客 397

# 多用户 JupyterHub 配置教程(通用 Linux 服务器版)

以上即是通用 Linux 服务器上的多用户 JupyterHub 实战配置。从安装认证与 Spawner 选择自动创建系统用户到反代与运维,覆盖了小团队到中等规模的主流场景。根据团队增长与合规要求,你可以进一步引入、统一身份源(LDAP/OAuth2/Keycloak)与资源配额治理,平滑演进到企业级部署。

数字化老司机 1288

Leetcode——图论

图论

qq_43332536的博客 477

【GPFS专题4】---IBM Spectrum Scale (GPFS) 原理详解

IBM Spectrum Scale(原名为 GPFS,General Parallel File System)是一种高性能、分布式的集群文件系统,专为大规模数据共享和并行访问设计。它允许多个客户端同时访问共享文件,提供高可用性、容错性和扩展性,广泛应用于高性能计算(HPC)、大数据分析、AI 训练和企业级存储场景。IBM Spectrum Scale 通过分布式架构、并行访问和智能数据管理,为企业提供了高性能、高可用、可扩展的存储解决方案。

jundao1997的专栏 777

leetcode(图论专题)

图论1.导论2.编程题2.1 127. 单词接龙 1.导论 2.编程题 2.1 127. 单词接龙 字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列: 序列中第一个单词是 beginWord 。 序列中最后一个单词是 endWord 。 每次转换只能改变一个字母。 转换过程中的中间单词必须是字典 wordList 中的单词。 给你两个单词 beginWord 和 endWord 和一个字典 wordList ,找到从 beginWord 到

Listen·Rain的博客 2155

人形机器人电源系统硬件开发:从原理图设计到PCB布局的完整流程

人形机器人是“移动的智能终端”,其电源系统如同“心脏+血管”:既要为大脑(主控)、肌肉(电机)、感官(传感器)提供稳定能量,又要在有限体积内应对高动态负载(如跳跃时电机瞬间大电流)。本文聚焦“硬件开发”维度,覆盖从需求分析到PCB制板的完整流程,适合硬件工程师、机器人爱好者快速掌握工程实战方法。

AI天才研究院 2173

(王道408考研操作系统)第三章内存管理-第一节1:内存基础知识、程序编译运行原理

注意:本节内容和《计算机组成原理》“存储器”那一部分内容相关性很强,组成原理是从硬件角度,操作系统是从软件角度。因此如果没有学习过这部分的同学,强烈建议先看这一部分 王道计算机组成原理笔记 本文主要会涉及以下必要的内存知识 文章目录一:内存及其作用 一:内存及其作用 内存:内存又称之为主存。是一种硬件设备,作为CPU和硬盘的缓冲区,用于解决其速度不匹配的问题,提高整机性能。程序执行前需要放入内存才能运行 ...

快乐江湖的博客 2790

【leetcode】图论刷题总结

深度优先搜索(dfs)是可一个方向去搜,直到遇到绝境了,搜不下去了,再换方向(换方向的过程就涉及到了回溯),所以用递归的方式来实现是最方便的。

zs16113的博客 789

OpenClaw实战-行政与HR必备:AI驱动的办公自动化

智能会议纪要自动生成会议时间智能协调自动提醒和跟进简历智能筛选和匹配度分析面试安排自动化入职办理流程化通知公告一键发布发票识别和报销辅助资产管理数字化让AI处理繁杂的事务性工作,让您有更多时间专注于更有价值的战略和管理任务。

gaoxiang_120的专栏 217

力扣100二刷——图论、回溯

第二次刷题不在idea写代码,而是直接在leetcode网站上写,“逼”自己掌握常用的函数。

weixin_49426115的博客 858

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

LeetCode HOT100刷题笔记(八):图论

ps:笔记和代码按本人理解整理,重思路

学习记录 1222

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Leetcode 刷题笔记1 图论part04

【代码】Leetcode 刷题笔记1 图论part04。

pinglejun的博客 464

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

leetcode刷题-图论01

本题本质还是对nums2采用单调栈的算法计算下一个最大元素,但是返回的result需要和nums1大小一致,顺序一致,因此对nums2中的元素计算完后需要映射回nums1,除此之外,本题的result数组应该初始化为-1。这里可以借助单调栈的特性,栈内的元素是单调递增的,因此在获得了某一个元素的右边第一个比它大的元素,那么栈里的下一个元素就是左边第一个比它大的元素。例如:nums = [1,2,1],扩容后:nums = [1,2,1,1,2,1]n*2 输出:[1, 2, 3, 1, 2, 3]

emmmmXxxy的博客 993

8.6-8.15力扣图论刷题

哈希表中的 key 是原始图中的节点,value 是克隆图中的对应节点。如果 edges[0][0] 和 edges[1] 的两个节点之一相同则 edges[0][0] 是星型图的中心节点,如果 edges[0][0] 和 edges[1] 的两个节点都不相同则 edges[0][1] 是星型图的中心节点。如果从点 u 到点 v 路径,能刷新 dis[v],则更新 dis[v],并将 ways[v] 更新为 ways[u],表示有多少条源到点 u 的最短路径,就有多少条源到点 v 的最短路径。

sprintzer的博客 1282

Leetcode 刷题笔记 图论part06

并查集的进一步应用。

pinglejun的博客 287

Leetcode刷题笔记1 图论part03

【代码】Leetcode刷题笔记1 图论part03

pinglejun的博客 587

Leetcode 刷题笔记1 图论part02

【代码】Leetcode 刷题笔记1 图论part02。

pinglejun的博客 282

Leetcode刷题 | Day49_图论01

给定一个有 n 个节点的有向无环图,节点编号从 1 到 n。请编写一个程序,找出并返回所有从节点 1 到节点 n 的路径。输出所有的可达路径,路径中所有节点的后面跟一个空格,每条路径独占一行,存在多条路径,路径输出的顺序可任意。后续 M 行,每行包含两个整数 s 和 t,表示图中的 s 节点与 t 节点中有一条路径。是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点。第一行包含两个整数 N,M,表示图中拥有 N 个节点,M 条边。,请你找出所有从节点 0 到节点 n-1 的路径并输出(

weixin_52152177的博客 726

Leetcode刷题 | Day59_图论05

如果存在,输出 1;判断两个节点是否在同一个集合,函数:isSame(int u, int v),就是判断两个节点是不是同一个根节点。将两个节点接入到同一个集合,函数:join(int u, int v),将两个节点连在同一个根节点上。后续 M 行,每行两个正整数 s 和 t,代表从节点 s 与节点 t 之间有一条边。寻找根节点,函数:find(int u),也就是判断这个节点的祖先节点是哪个。第一行包含两个正整数 N 和 M,N 代表节点的个数,M 代表边的个数。并查集是无向图,主要考察是否连通。

weixin_52152177的博客 417

力扣刷题总结【图论】

力扣图的相关题目

bigorange1的博客 655

Leetcode 刷题笔记1 图论part01

有向图(边有方向) 、 无向图(边无方向)、加权有向图(边有方向和权值)无向图中几条边连接该节点,该节点就有几度;有向图中每个节点有入度和出度在无向图中,任何两个节点都是可以到达的,称之为连通图,否则称之为非连通图在有向图中,热河两个节点是可以相互到达的,称之为强连通图在无向图中的连通子图称之为该图的一个连通分量有向图中强连通子图称之为强连通分量一般使用邻接表、邻接矩阵和朴素存储深度优先搜索(dfs)、广度优先搜索(bfs)

pinglejun的博客 432

Leetcode刷题 | Day64_图论09_dijkstra算法

然而,途中的各个车站之间的道路状况、交通拥堵程度以及可能的自然因素(如天气变化)等不同,这些因素都会影响每条路径的通行时间。接下来为 M 行,每行包括三个整数,S、E 和 V,代表了从 S 车站可以单向直达 E 车站,并且需要花费 V 单位的时间。接下来为 M 行,每行包括三个整数,S、E 和 V,代表了从 S 车站可以单向直达 E 车站,并且需要花费 V 单位的时间。本题就是求最短路,最短路是图论中的经典问题即:给出一个有向图,一个起点,一个终点,问起点到终点的最短路径。

weixin_52152177的博客 999
上一篇: leetcode单调栈刷题总结
下一篇: leetcode字符串刷题总结
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值