In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use leetcode to judge bipartite graph in golang. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Given an undirected graph graph, true is returned when the graph is bipartite.
If we can divide the node set of a graph into two independent subsets An and B, and make the two nodes of each edge of the graph one from the A set and the other from the B set, we call the graph bipartite graph.
Graph will be given as an adjacency table, and graph [I] represents all nodes connected to node I in the graph. Each node is an integer between 0 and graph.length-1. There is no self-loop and row edge in this figure: I does not exist in graph [I], and there are no duplicate values in graph [I].
Example 1:
Input: [1mem3], [0Magne2], [1recover3], [0recover2]] output: true explanation: undirected graph is as follows: 0 Musashi 1 | 3 Musashi Mui 2 We can divide the nodes into two groups: {0Query 2} and {1jue 3}.
Example 2:
Input: [[1Jing 2jue 3], [0Jue 2], [0JME 1p3], [0Jue 2]] output: false explanation: undirected graph is as follows: 0Mui Mui Mui 1 |\ | 3 Mustang Mustang 2
We cannot split the node into two separate subsets.
Note:
The length range of graph is [1,100].
The range of elements in graph [I] is [0, graph.length-1].
Graph [I] will not contain I or have duplicate values.
The graph is undirected: if j is in graph [I], then I will also be in graph [j].
Problem-solving ideas
Depth first search shading
1. If the node belongs to the first collection, color it blue, otherwise it will be red.
2, only in the case of bipartite graph, we can use greedy idea to color the graph: a node is blue, which means that all its adjacent points are red, all its adjacent points are blue, and so on.
3. Use an array (or hash table) to record the color of each node: color [node]. The color can be 1BI 2, or unshaded (0).
4. When searching nodes, we need to consider the situation that the graph is unconnected.
5, for each unshaded node, starting from that node, depth first searches for shading. Each adjacent point can be colored in the opposite color through the current node.
6, if there is a current point with the same color as the adjacent point, the shading fails.
7. Use the stack to complete the depth-first search, which is similar to the "todo list" of the node, storing the order of the next node to be accessed. In graph [node], for each unshaded adjacency, the node is shaded and placed on the stack.
Code implementation
Func isBipartite (graph [] int) bool {l:=len (graph)
If l
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.