码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode常见题型——图


    1. 算法思想

    图是树的升级版。图通常分为有向(directed)或无向(undirected),有循环(cyclic)或无循环(acyclic),所有节点相连(connected)或不相连(disconnected)。

    • 树即是一个相连的无向无环图。
    • 有向无环图(Directed Acyclic Graph,DAG)。

    图通常有两种表示方法。假设图中一共有n 个节点、m 条边。

    • 邻接矩阵(adjacency matrix):建立一个n× n 的矩阵G,如果第i 个节点连向第j 个节点,则G[i][j] = 1,反之为0;如果图是无向的,则这个矩阵一定是对称矩阵,即G[i][j] = G[j][i]。
    • 邻接链表(adjacency list):建立一个大小为n 的数组,每个位置i 储存一个数组或者链表,表示第i 个节点连向的其它节点。邻接矩阵空间开销比邻接链表大,但是邻接链表不支持快速查找i 和j 是否相连,因此两种表示方法可以根据题目需要适当选择。除此之外,我们也可以直接用一个m × 2 的矩阵储存所有的边。

    二分图算法也称为染色法,是一种广度优先搜索。如果可以用两种颜色对图中的节点进行着
    色,并且保证相邻的节点颜色不同,那么图为二分。

    2. 常见题型

    LeetCode-785. Is Graph Bipartite? [C++][Java]_贫道绝缘子的博客-CSDN博客There is anundirectedgraph withnnodes, where each node is numbered between0andn - 1. You are given a 2D arraygraph, wheregraph[u]is an array of nodes that nodeuis adjacent to. More formally, for eachvingraph[u], there is an undirected edge b...https://blog.csdn.net/qq_15711195/article/details/126397821?spm=1001.2014.3001.5502LeetCode-207. Course Schedule [C++][Java]_贫道绝缘子的博客-CSDN博客There are a total ofnumCoursescourses you have to take, labeled from0tonumCourses - 1. You are given an arrayprerequisiteswhereprerequisites[i] = [ai, bi]indicates that youmusttake coursebifirst if you want to take courseai. reuturn true .......https://blog.csdn.net/qq_15711195/article/details/126416977?spm=1001.2014.3001.5502LeetCode-210. Course Schedule II [C++][Java]_贫道绝缘子的博客-CSDN博客There are a total ofnumCoursescourses you have to take, labeled from0tonumCourses - 1. You are given an arrayprerequisiteswhereprerequisites[i] = [ai, bi]indicates that youmusttake coursebifirst if you want to take courseai.https://blog.csdn.net/qq_15711195/article/details/126416918?spm=1001.2014.3001.5502LeetCode-1059. All Paths from Source Lead to Destination [C++][Java]_贫道绝缘子的博客-CSDN博客Given the edges of a directed graph, and two nodes source and destination of this graph, determine whether or not all paths starting from source eventually end at destination,this problem is pretty straight forward.https://blog.csdn.net/qq_15711195/article/details/126435369?spm=1001.2014.3001.5502LeetCode-1135. Connecting Cities With Minimum Cost [C++][Java]_贫道绝缘子的博客-CSDN博客Return the minimum cost so that for every pair of cities, there exists a pathof connections (possibly of length 1) that connects those two cities together. The cost is the sum of the connection costs used. If the task is impossible, return -1.https://blog.csdn.net/qq_15711195/article/details/126447763?spm=1001.2014.3001.5502LeetCode-882. Reachable Nodes In Subdivided Graph [C++][Java]_贫道绝缘子的博客-CSDN博客In thisnew graph, you want to know how many nodes arereachablefrom the node0, where a node isreachableif the distance ismaxMovesor less.Given the original graph andmaxMoves, returnthe number of nodes that arereachablefrom node0in the new grhttps://blog.csdn.net/qq_15711195/article/details/126455402?spm=1001.2014.3001.5502LeetCode-684. Redundant Connection [C++][Java]_贫道绝缘子的博客-CSDN博客You are given a graph that started as a tree withnnodes labeled from1ton, with one additional edge added. The added edge has twodifferentvertices chosen from1ton, and was not an edge that already existed. The graph is represented as an arrayedgehttps://blog.csdn.net/qq_15711195/article/details/126456032?spm=1001.2014.3001.5502

  • 相关阅读:
    【游戏客户端】制作节奏大师Like音游(下)
    Linux服务器部署SpringBoot项目教程
    【Verilog】用双口RAM实现同步FIFO
    python-opencv图像的高通滤波和低通滤波
    newspringboot
    C语言实现句子中的单词颠倒排序
    Jenkins的一些其他操作
    多校联测12 树
    类和对象!
    Python接口自动化实战案例
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126458652
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号