Optimal skincare partition finder using graph theory

Related tags

Algorithmspigment
Overview

Pigment

License: ISC CC BY-SA 4.0

The problem of partitioning up a skincare regime into parts such that each part does not interfere with itself is equivalent to the minimal clique cover problem, which can be transformed into the vertex colouring of a graph, both of which are NP-hard and thus computationally infeasible to find optimal solutions for. This project is a brute-force proof-of-concept that exhaustively solves the problem of good skincare product grouping!

Usage

  1. Modify the ingredient conflict dictionary (named conflicts in the pigment.py mainline) to reflect your skincare products. If you say A conflicts with B, you don't have to also write the rule that B conflicts with A. The script handles the reflexivity.

  2. Run the program (you need Python 3):

    python3 pigment.py

Algorithm

This algorithm takes in an adjacency list for a conflict graph where each edge between two nodes represents an instance of two ingredients conflicting.

It then exhaustively generates every possible partition using a recursive backtracking depth-first-search algorithm where for each ingredient, it explores every sub-tree consisting of adding the ingredient to every existing part before finally creating a new part. Each terminal/leaf node represents a generated partition, which we exhaustively check: for each part in the partition, we check to see if any pair exists as an edge in the conflict dictionary. If no such pairs exist among any part, the partition is valid.

partition tree

The algorithm looks for the valid partition with the least amount of parts.

The number of partitions that are brute-force generated is equivalent to the nth Bell number and it is sequence A000110 in the OEIS.

It runs in O(a fuckton of time). If you have a lot of stuff in your skincare routine, this algorithm may take forever to run. It is recommended that you do not add vanity elements (aka adding an element just for it to show up in the final result) such as:

CONFLICTS = OrderedDict((
    ("A", ["B", "C"])
    ("D", [])
))

In this case, "D" is a vanity element; it contributes nothing to conflict data but bloats the state space (which, in a brute-force algorithm like this, is not good). If an element doesn't conflict with anything, then use it as liberally as you like without restriction.

You have been warned.

Modelling

Say, for the purposes of illustration (as these opinions are still hotly debated in the skincare community today), we have the following ingredients:

  • Retinol
  • AHAs/BHAs
  • Copper peptides
  • Ferrulic acid

and the following interactions:

  • Retinol and AHAs/BHAs conflict with each other
  • Copper peptides interfere with AHAs/BHAs
  • Ferrulic acid interferes with copper peptides

We can therefore model compatible products as an undirected graph where each node represents a skincare ingredient and each edge between node a and node b represents the sentence "ingredient a is compatible with ingredient b". We can represent the relation above as such:

compatibility graph

The ideal here is that we want to take all four of these ingredients at once, however as noted by the conflicts above, that isn't possible. The next best solution, if we can't create 1 part, is to try to create 2 part. We know that in our model, retinol is compatible with copper peptides, and ferrulic acid is compatible with AHAs/BHAs, but we discard the possibility of using retinol with ferrulic acid though, as its part contains AHAs/BHAs, which are not compatible with retinol (as shown by the lack of edge).

minimum clique

This is the optimal solution. In one skincare session, we take retinol with the copper peptides, and another session we take AHAs/BHAs and ferrulic acid.

Our major goal, therefore, is to partition the ingredients list into as few parts as possible such that each parts's ingredients represents a clique, where a clique is an induced subgraph that is complete. In layperson's terms, we are looking to create subgraphs of ingredients such that each ingredient has an edge connected to every other ingredient node in the subgraph. Such complete subgraphs are known as cliques. As shown below, when two ingredients are compatible with each other, the resultant clique has a single edge between two nodes (as shown by K2: 1). For four ingredients, the resultant clique has six edges between the four nodes (as shown by K2:6). To see ten ingredients compatible with each other is somewhat uncommon.

complete graphs These images are taken from Wikipedia.org and are by koko90. See attribution for details

Minimal Clique Cover

In formal terms, a "clique cover" or "partition into cliques" of an undirected graph is a partition (or splitting of the graph into groups) into constituent cliques. Our problem is to find the "minimal" clique cover—aka—doing it in the least number of cliques—or splits—possible. As shown in the figure above, the trivial case is K1: 0 as each individual ingredient is its own clique, but that's the worst-case scenario we are trying to avoid. It would mean that no skincare ingredient is compatible with anything else e.g. you may have to take each 10 skincare ingredient on separate days, which would be a scheduling nightmare.

Graph Colouring

We can make things more readable by looking at an equivalent problem.

Given a graph G, the complement of the graph, let's call it G2, is a graph with the same nodes as G, but every edge in the original graph is missing, and every midding edge in the original graph is now an edge. In layperson's terms, a complement graph G2 for graph G contains only the edges necessary to turn G into a complete graph, as shown by this diagram:

complement of the Petersen graph Image edited by Claudio Rocchini; derived from David Eppstein. See attribution for details

We can invert the "maximal clique" problem by not mapping whether two skincare products are compatible with each other, but rather if they conflict. This makes specifications a whole lot easier to make, as now we can assume anything that isn't connected by an edge is compatible. If we change our first graph to model conflicts instead of synergies, we get the following:

conflict graph

Our problem is now to induce subgraphs such that none of the nodes have any edges between them. Each subgraph is its own group. In this example, we induce the subgraphs for the nodes {Retinol, Copper peptides} as well as for {Ferrulic acid, AHAs/BHAs}, as each graph has no nodes:

coloured conflict graph

Those with a background in CS will immediately notice that this is actually the well-studied graph colouring sub-problem known as "vertex colouring": colouring a graph such that no two colours are adjacent to each other. In this case, each colour group represents a partition, like from earlier. Again, the optimization problem is NP-hard and is intractable. Which is why the algorithm solves the colouring problem in the ugliest, most brute force way possible.

Bibliography

Attribution

  • Graphs made by me using Dreampuf's Dot Grapher and they are licensed as CC BY-SA 4.0 as the project is
  • Complete graphs K1, K2, and K3 are simple geometry and thus are in the public domain (author is David Benbennick).
  • Simplex graphs 4, 5, 6, 7, 8, 9, 10, 11, were released by Koko90 under GFDL and CC BY-SA 3.0 and will be coalesced into the license of this project, thus making them CC BY-SA 4.0
  • The Petersen graph complement image was edited by Claudio Rocchini whose original author was David Eppstein, also released under GFDL and CC BY-SA 3.0. CC BY-SA 4.0 as per the project.
Owner
Jason Nguyen
CS @ University of Guelph
Jason Nguyen
Path finding algorithm visualizer with python

path-finding-algorithm-visualizer ~ click on the grid to place the starting block and then click elsewhere to add the end block ~ click again to place

izumi 1 Oct 31, 2021
With this algorithm you can see all best positions for a Team.

Best Positions Imagine that you have a favorite team, and you want to know until wich position your team can reach With this algorithm you can see all

darlyn 4 Jan 28, 2022
sudoku solver using CSP forward-tracking algorithms.

Sudoku sudoku solver using CSP forward-tracking algorithms. Description Sudoku is a logic-based game that consists of 9 3x3 grids that create one larg

Cindy 0 Dec 27, 2021
This is a demo for AAD algorithm.

Asynchronous-Anisotropic-Diffusion-Algorithm This is a demo for AAD algorithm. The subroutine of the anisotropic diffusion algorithm is modified from

3 Mar 21, 2022
An NUS timetable generator which uses a genetic algorithm to optimise timetables to suit the needs of NUS students.

A timetable optimiser for NUS which uses an evolutionary algorithm to "breed" a timetable suited to your needs.

Nicholas Lee 3 Jan 09, 2022
Genetic algorithms are heuristic search algorithms inspired by the process that supports the evolution of life.

Genetic algorithms are heuristic search algorithms inspired by the process that supports the evolution of life. The algorithm is designed to replicate the natural selection process to carry generatio

Mahdi Hassanzadeh 4 Dec 24, 2022
A minimal implementation of the IQRM interference flagging algorithm for radio pulsar and transient searches

A minimal implementation of the IQRM interference flagging algorithm for radio pulsar and transient searches. This module only provides the algorithm that infers a channel mask from some spectral sta

Vincent Morello 6 Nov 29, 2022
Algoritmos de busca:

Algoritmos-de-Buscas Algoritmos de busca: Abaixo está a interface da aplicação: Ao selecionar o tipo de busca e o caminho, então será realizado o cálc

Elielson Barbosa 5 Oct 04, 2021
8 Puzzle with A* , Greedy & BFS Search in Python

8_Puzzle 8 Puzzle with A* , Greedy & BFS Search in Python Python Install Python from here. Pip Install pip from here. How to run? 🚀 Install 8_Puzzle

I3L4CK H4CK3l2 1 Jan 30, 2022
Algorithms-in-Python - Programs related to DSA in Python for placement practice

Algorithms-in-Python Programs related to DSA in Python for placement practice CO

MAINAK CHAUDHURI 2 Feb 02, 2022
A Python library for simulating finite automata, pushdown automata, and Turing machines

Automata Copyright 2016-2021 Caleb Evans Released under the MIT license Automata is a Python 3 library which implements the structures and algorithms

Caleb Evans 219 Dec 12, 2022
A Python description of the Kinematic Bicycle Model with an animated example.

Kinematic Bicycle Model Abstract A python library for the Kinematic Bicycle model. The Kinematic Bicycle is a compromise between the non-linear and li

Winston H. 36 Dec 23, 2022
Nature-inspired algorithms are a very popular tool for solving optimization problems.

Nature-inspired algorithms are a very popular tool for solving optimization problems. Numerous variants of nature-inspired algorithms have been develo

NiaOrg 215 Dec 28, 2022
SortingAlgorithmVisualization - A place for me to learn about sorting algorithms

SortingAlgorithmVisualization A place for me to learn about sorting algorithms.

1 Jan 15, 2022
CLI Eight Puzzle mini-game featuring BFS, DFS, Greedy and A* searches as solver algorithms.

🕹 Eight Puzzle CLI Jogo do quebra-cabeças de 8 peças em linha de comando desenvolvido para a disciplina de Inteligência Artificial. Escrito em python

Lucas Nakahara 1 Jun 30, 2021
Zipline, a Pythonic Algorithmic Trading Library

Zipline, a Pythonic Algorithmic Trading Library

Stefan Jansen 463 Jan 08, 2023
The DarkRift2 networking framework written in Python 3

DarkRiftPy is Darkrift2 written in Python 3. The implementation is fully compatible with the original version. So you can write a client side on Python that connects to a Darkrift2 server written in

Anton Dobryakov 6 May 23, 2022
A tictactoe where you never win, implemented using minimax algorithm

Unbeatable_TicTacToe A tictactoe where you never win, implemented using minimax algorithm Requirements Make sure you have the pygame module along with

Jessica Jolly 3 Jul 28, 2022
Machine Learning algorithms implementation.

Machine Learning Algorithms Machine Learning algorithms implementation. What can I find here? ML Algorithms KNN K-Means-Clustering SVM (MultiClass) Pe

David Levin 1 Dec 10, 2021
Evol is clear dsl for composable evolutionary algorithms that optimised for joy.

Evol is clear dsl for composable evolutionary algorithms that optimised for joy. Installation We currently support python3.6 and python3.7 and you can

GoDataDriven 178 Dec 27, 2022