Friday, May 24, 2024
HomeGolangIs Graph Bipartite Program in Go (Golang)

Is Graph Bipartite Program in Go (Golang)


An undirected graph is given. A  graph is claimed to be bipartite if the nodes of the graph may be partitioned into two subsets such that each edge connects one node within the first subset to another node within the second subset.

The graph incorporates n nodes numbered from 0 to n-1. Enter is a matrix named graph which is a 2D  matrix the place graph[i] incorporates the node to which ith node is linked. For eg if

graph[0] = [1,3]

this implies node 0 is linked to node 1 and node 3

Instance 1

Enter: [[1,3],[0,2],[1,3],[0,2]]
Output: true

Instance 2

Enter: [[1,4],[0,2],[1,3],[2,4],[0,3]
Output: false

Thought is to make use of DFS  right here. We are going to attempt to assign both crimson or black coloration to every of the nodes. If a node is coloured crimson then its neighbors should be coloured black.

  • We’re in a position to coloration  on this  means then the graph is bipartite
  • If whereas coloring we  discover  that two nodes linked by  an edge have the identical coloration then the graph is just not bipartite

Let’s see this system for a similar

Beneath is this system for a similar

package deal major

import "fmt"

func isBipartite(graph [][]int) bool {
	nodeMap := make(map[int][]int)

	numNodes := len(graph)

	if numNodes == 1 {
		return true
	for i := 0; i 



Observe: Take a look at our Golang Superior Tutorial. The tutorials on this sequence are elaborative and we’ve got tried to cowl all ideas with examples. This tutorial is for many who wish to achieve experience and a strong understanding of golang – Golang Advance Tutorial

Additionally in case you are fascinated about understanding how all design patterns may be applied in Golang. If sure, then this put up is for you – All Design Patterns Golang

Additionally, try our system design tutorial sequence right here – System Design Tutorial Sequence



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments