Data update
This commit is contained in:
parent
61b93a2cd1
commit
5af6d93694
858 changed files with 20572 additions and 2082 deletions
113
Task/Graph-colouring/C++/graph-colouring.cpp
Normal file
113
Task/Graph-colouring/C++/graph-colouring.cpp
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
const std::vector<std::string> all_colours = { "PINK", "ORANGE", "CYAN", "YELLOW", "RED", "GREEN", "BLUE" };
|
||||
|
||||
class Node {
|
||||
public:
|
||||
Node(const int32_t& aID, const int32_t& aSaturation, const std::string& aColour)
|
||||
: id(aID), saturation(aSaturation), colour(aColour) {}
|
||||
|
||||
Node() : id(0), saturation(0), colour("NO_COLOUR") {}
|
||||
|
||||
int32_t id, saturation;
|
||||
std::string colour;
|
||||
bool excluded_from_search = false;
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::map<int32_t, Node, decltype([](const int32_t& a, const int32_t& b){ return a < b; })> graph;
|
||||
|
||||
std::map<int32_t, std::set<int32_t, decltype([](const int32_t& a, const int32_t& b) { return a < b; })>,
|
||||
decltype([](const int32_t& a, const int32_t& b) { return a < b; })> neighbours;
|
||||
|
||||
const std::vector<std::string> graph_representations = { "0-1 1-2 2-0 3",
|
||||
"1-6 1-7 1-8 2-5 2-7 2-8 3-5 3-6 3-8 4-5 4-6 4-7",
|
||||
"1-4 1-6 1-8 3-2 3-6 3-8 5-2 5-4 5-8 7-2 7-4 7-6",
|
||||
"1-6 7-1 8-1 5-2 2-7 2-8 3-5 6-3 3-8 4-5 4-6 4-7" };
|
||||
|
||||
for ( const std::string& graph_representation : graph_representations ) {
|
||||
graph.clear();
|
||||
neighbours.clear();
|
||||
std::stringstream stream(graph_representation);
|
||||
std::string element;
|
||||
while ( stream >> element ) {
|
||||
if ( element.find("-") != std::string::npos ) {
|
||||
const int32_t id1 = element[0] - '0';
|
||||
const int32_t id2 = element[element.length() - 1] - '0';
|
||||
|
||||
if ( ! graph.contains(id1) ) {
|
||||
graph[id1] = Node(id1, 0, "NO_COLOUR");
|
||||
}
|
||||
Node node1 = graph[id1];
|
||||
|
||||
if ( ! graph.contains(id2) ) {
|
||||
graph[id2] = Node(id2, 0, "NO_COLOUR");
|
||||
}
|
||||
Node node2 = graph[id2];
|
||||
|
||||
neighbours[id1].emplace(id2);
|
||||
neighbours[id2].emplace(id1);
|
||||
} else {
|
||||
const int32_t id = element[0] - '0';
|
||||
if ( ! graph.contains(id) ) {
|
||||
graph[id] = Node(id, 0, "NO_COLOUR");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for ( uint64_t i = 0; i < graph.size(); ++i ) {
|
||||
int32_t max_node_id = -1;
|
||||
int32_t max_saturation = -1;
|
||||
for ( const auto& [key, value] : graph ) {
|
||||
if ( ! value.excluded_from_search && value.saturation > max_saturation ) {
|
||||
max_saturation = value.saturation;
|
||||
max_node_id = key;
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_set<std::string> colours_used;
|
||||
for ( const int32_t& neighbour : neighbours[max_node_id] ) {
|
||||
colours_used.emplace(graph[neighbour].colour);
|
||||
}
|
||||
|
||||
std::string min_colour;
|
||||
for ( const std::string& colour : all_colours ) {
|
||||
if ( ! colours_used.contains(colour) ) {
|
||||
min_colour = colour;
|
||||
}
|
||||
}
|
||||
|
||||
graph[max_node_id].excluded_from_search = true;
|
||||
graph[max_node_id].colour = min_colour;
|
||||
|
||||
for ( int32_t neighbour : neighbours[max_node_id] ) {
|
||||
if ( graph[neighbour].colour == "NO_COLOUR" ) {
|
||||
graph[neighbour].saturation = colours_used.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_set<std::string> graph_colours;
|
||||
for ( const auto& [key, value] : graph ) {
|
||||
graph_colours.emplace(value.colour);
|
||||
std::cout << "Node " << key << ": colour = " + value.colour;
|
||||
|
||||
if ( ! neighbours[key].empty() ) {
|
||||
std::cout << std::string(8 - value.colour.length(), ' ') << "neighbours = ";
|
||||
for ( const int32_t& neighbour : neighbours[key] ) {
|
||||
std::cout << neighbour << " ";
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << "Number of colours used: " << graph_colours.size() << std::endl << std::endl;
|
||||
}
|
||||
}
|
||||
109
Task/Graph-colouring/Java/graph-colouring.java
Normal file
109
Task/Graph-colouring/Java/graph-colouring.java
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public final class GraphColoring {
|
||||
|
||||
public static void main(String[] aArgs) {
|
||||
colourise("0-1 1-2 2-0 3");
|
||||
colourise("1-6 1-7 1-8 2-5 2-7 2-8 3-5 3-6 3-8 4-5 4-6 4-7");
|
||||
colourise("1-4 1-6 1-8 3-2 3-6 3-8 5-2 5-4 5-8 7-2 7-4 7-6");
|
||||
colourise("1-6 7-1 8-1 5-2 2-7 2-8 3-5 6-3 3-8 4-5 4-6 4-7");
|
||||
}
|
||||
|
||||
private static void colourise(String aGraphRepresentation) {
|
||||
List<Node> graph = initialiseGraph(aGraphRepresentation);
|
||||
List<Node> nodes = new ArrayList<Node>(graph);
|
||||
while ( ! nodes.isEmpty() ) {
|
||||
Node maxNode = nodes.stream().max( (one, two) -> Integer.compare(one.saturation, two.saturation) ).get();
|
||||
maxNode.colour = minColour(maxNode);
|
||||
updateSaturation(maxNode);
|
||||
nodes.remove(maxNode);
|
||||
}
|
||||
|
||||
System.out.println("Graph: " + aGraphRepresentation);
|
||||
display(graph);
|
||||
}
|
||||
|
||||
private static Colour minColour(Node aNode) {
|
||||
Set<Colour> coloursUsed = coloursUsed(aNode);
|
||||
for ( Colour colour : Colour.values() ) {
|
||||
if ( ! coloursUsed.contains(colour) ) {
|
||||
return colour;
|
||||
}
|
||||
}
|
||||
return Colour.NO_COLOUR;
|
||||
}
|
||||
|
||||
private static Set<Colour> coloursUsed(Node aNode) {
|
||||
Set<Colour> coloursUsed = new HashSet<Colour>();
|
||||
for ( Node neighbour : aNode.neighbours ) {
|
||||
coloursUsed.add(neighbour.colour);
|
||||
}
|
||||
return coloursUsed;
|
||||
}
|
||||
|
||||
private static void updateSaturation(Node aNode) {
|
||||
for ( Node neighbour : aNode.neighbours ) {
|
||||
if ( neighbour.colour == Colour.NO_COLOUR ) {
|
||||
neighbour.saturation = coloursUsed(aNode).size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void display(List<Node> aNodes) {
|
||||
Set<Colour> graphColours = new HashSet<Colour>();
|
||||
for ( Node node : aNodes ) {
|
||||
graphColours.add(node.colour);
|
||||
System.out.print("Node " + node.index + ": colour = " + node.colour);
|
||||
|
||||
if ( ! node.neighbours.isEmpty() ) {
|
||||
List<Integer> indexes = node.neighbours.stream().map( n -> n.index ).toList();
|
||||
System.out.print(" ".repeat(8 - String.valueOf(node.colour).length()) + "neighbours = " + indexes);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println("Number of colours used: " + graphColours.size());
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private static List<Node> initialiseGraph(String aGraphRepresentation) {
|
||||
Map<Integer, Node> map = new TreeMap<Integer, Node>();
|
||||
for ( String element : aGraphRepresentation.split(" ") ) {
|
||||
if ( element.contains("-") ) {
|
||||
final int index1 = Integer.valueOf(element.substring(0, 1));
|
||||
final int index2 = Integer.valueOf(element.substring(element.length() - 1));
|
||||
Node node1 = map.computeIfAbsent(index1, k -> new Node(index1, 0, Colour.NO_COLOUR) );
|
||||
Node node2 = map.computeIfAbsent(index2, k -> new Node(index2, 0, Colour.NO_COLOUR) );
|
||||
node1.neighbours.add(node2);
|
||||
node2.neighbours.add(node1);
|
||||
} else {
|
||||
final int index = Integer.valueOf(element);
|
||||
map.computeIfAbsent(index, k -> new Node(index, 0, Colour.NO_COLOUR));
|
||||
}
|
||||
}
|
||||
|
||||
List<Node> graph = new ArrayList<Node>(map.values());
|
||||
return graph;
|
||||
}
|
||||
|
||||
private enum Colour { BLUE, GREEN, RED, YELLOW, CYAN, ORANGE, NO_COLOUR }
|
||||
|
||||
private static class Node {
|
||||
|
||||
public Node(int aIndex, int aSaturation, Colour aColour) {
|
||||
index = aIndex; saturation = aSaturation; colour = aColour;
|
||||
}
|
||||
|
||||
private int index, saturation;
|
||||
private Colour colour;
|
||||
private Set<Node> neighbours = new TreeSet<Node>( (one, two) -> Integer.compare(one.index, two.index) );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue