TheAlgorithms/Go · error

coloring: not all vertices of graph are colored

Error message

coloring: not all vertices of graph are colored

What it means

ValidateColorsOfVertex compares the graph's vertex count with len(colors); a mismatch means the colors map doesn't cover every vertex (or covers extra/unknown vertex ids), so validity can't be assessed.

Source

Thrown at graph/coloring/graph.go:46

		g.vertices++
		g.edges[v] = make(map[int]struct{})
	}
}

// AddEdge will add a new edge between the provided vertices in the graph
func (g *Graph) AddEdge(one, two int) {
	// Add vertices: one and two to the graph if they are not present
	g.AddVertex(one)
	g.AddVertex(two)

	// and finally add the edges: one->two and two->one for undirected graph
	g.edges[one][two] = struct{}{}
	g.edges[two][one] = struct{}{}
}

func (g *Graph) ValidateColorsOfVertex(colors map[int]Color) error {
	if g.vertices != len(colors) {
		return errors.New("coloring: not all vertices of graph are colored")
	}
	// check colors
	for vertex, neighbours := range g.edges {
		for nb := range neighbours {
			if colors[vertex] == colors[nb] {
				return errors.New("coloring: same colors of neighbouring vertex")
			}
		}
	}
	return nil
}

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Build the colors map by iterating all graph vertices before validating
  2. Ensure vertices are added via AddVertex/AddEdge before assigning colors
  3. Call BipartiteCheck/ValidateColorsOfVertex only after the graph is fully constructed
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at graph/coloring/graph.go:46 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/76f5d56ff4f9012d. Report an issue: GitHub.