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 gr
https://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 arrayedge
https://blog.youkuaiyun.com/qq_15711195/article/details/126456032?spm=1001.2014.3001.5502