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]_贫道绝缘子的博客-优快云博客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.youkuaiyun.com/qq_15711195/article/details/126397821?spm=1001.2014.3001.5502LeetCode-207. Course Schedule [C++][Java]_贫道绝缘子的博客-优快云博客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.youkuaiyun.com/qq_15711195/article/details/126416977?spm=1001.2014.3001.5502LeetCode-210. Course Schedule II [C++][Java]_贫道绝缘子的博客-优快云博客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.youkuaiyun.com/qq_15711195/article/details/126416918?spm=1001.2014.3001.5502LeetCode-1059. All Paths from Source Lead to Destination [C++][Java]_贫道绝缘子的博客-优快云博客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.youkuaiyun.com/qq_15711195/article/details/126435369?spm=1001.2014.3001.5502LeetCode-1135. Connecting Cities With Minimum Cost [C++][Java]_贫道绝缘子的博客-优快云博客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.youkuaiyun.com/qq_15711195/article/details/126447763?spm=1001.2014.3001.5502LeetCode-882. Reachable Nodes In Subdivided Graph [C++][Java]_贫道绝缘子的博客-优快云博客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.youkuaiyun.com/qq_15711195/article/details/126455402?spm=1001.2014.3001.5502LeetCode-684. Redundant Connection [C++][Java]_贫道绝缘子的博客-优快云博客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.youkuaiyun.com/qq_15711195/article/details/126456032?spm=1001.2014.3001.5502

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值