Coloring a Tree - algorithm

There is a coloring a tree problem in one of contest which i could not solve.Problem Statement:We have to Color a tree such that the no two adjacent vertex is having the same color and the cost of coloring the tree is minimum
Problem Link
I have no idea what algorithm should i used in such type of problemsPLease the algorithm behind this.

This problem was more than just coloring a tree since there was a minimization problem. You can read the editorial here: https://www.facebook.com/notes/1047761065239794/

Related

Solving The House Drawing Problem With Graph Theory Algorithms

I was recently given the challenge of drawing a house with an x in the middle without lifting my pen, and without retracing any lines. Link to problem
The link above begins to dive into some of the graph theory related to the problem, however there is no mention of how one might go about solving this problem using graph theory algorithms.
What algorithms could be used here, and what would be the correct way to formulate this problem using graph theory language?
Two specific algorithms for constructing an Eulerian path are mentioned in the Wikipedia article on Eulerian paths. These are Fleury's algorithm and Hierholzer's algorithm.
Note that an algorithm that only finds an Euler cycle can also find an Euler trail by augmenting the graph with another edge that joins the 2 vertices that have an odd degree, then rotating the solution so that the added edge is first or last, then removing this edge from the solution found.

Is there a 2D-layout algorithm for DAGs that allows the positions on one axis to be fixed?

I've got a DAG of around 3.300 vertices which can be laid out quite successfully by dot as a more or less simple tree (things get complicated because vertices can have more than one predecessor from a whole different rank, so crossovers are frequent). Each vertex in the graph came into being at a specific time in the original process and I want one axis in the layout to represent time: An edge relation like a -> v, b -> v means that a and b came into being at some specific time before v.
Is there a layout algorithm for DAGs which would allow me to specify the positions (or at least the distances) on one axis and come up with an optimal layout regarding edge crossovers on the other?
You can make a topological sorting of the DAG to have the vertices sorted in a way that for every edge x->y, vertex x comes before than y.
Therefore, if you have a -> v, b -> v, you will get something like a, b, v or b, a, v.
Using this you can easily represents DAGs like this:
Yes, as #Arturo-Menchaca said a topological sorting may help to reduce overlapping count of edges. But it may be not optimal. There is no good algorithm for edge crossing minimization. Problem for crossing minimization is NP-complete. The heuristics are applied for solving this problem.
This StackOverflow link may help you: Drawing Directed Acyclic Graphs: Minimizing edge crossing?
I suppose your problem is related to an aesthetically pleasing way of the graph layout. Some heuristics are described in the articles Overview of algorithms for graph drawing, Force-Directed Drawing Algorithms. May be information about planar graph or almost planar graph can help you also.
Some review of the algorithms for checking and drawing planar graphs are described in the Wiki pages Planar graph, Crossing number (graph theory). The libraries and algorithms for planar graph drawing are described in the StackOverflow question How to check if a Graph is a Planar Graph or not? For example the author in the article GA for straight-line grid drawings of maximal planar graphs uses genetic algorithms for straight-line grid drawing.
Good descriptions for almost planar graphs are given in the articles Straight-Line Drawability of a Planar Graph Plus an Edge, On the Crossing Number of Almost Planar Graphs.
Try to modify the original algoritms using your condition with one axis alignment.
If I understood you correctly then you want to minimize the number of edge-crossings in your graph layout. If so, then the answer is "No", because this problem is proved to be NP-complete in the general case. See this, "Crossing Number is NP-Complete, Garey, Johnson".
If you need a not an optimal but just good enough solution, there are multiple articles on this topic because it is heavily related with circuit layouts. Probably googling "crossing number heuristics" and looking through the abstracts of some papers will solve your task better then me trying to guess blindly your requirements.

What is a Steiner point in poly2tri?

The poly2tri Readme talks about Steiner points, what are they? (Is it related to triangle Steiner points?)
Why would one add Steiner points?
Poly2tri has the ability to add something called Steiner Points. You can add these inside a polygon to get a triangulation with shorter edges
Here are a few resources from what you have posted: 1.Triangulation of spline to mesh, questions and results (read the comments)
2.Youtube Video with concept: Triangle Tribualtions
Conceptually, i believe these are linked with the famous NP-complete Steiner Tree Problem
From Wikipedia:
The Steiner tree problem is superficially similar to the minimum spanning tree problem: given a set V of points (vertices), interconnect them by a network (graph) of shortest length, where the length is the sum of the lengths of all edges. The difference between the Steiner tree problem and the minimum spanning tree problem is that, in the Steiner tree problem, extra intermediate vertices and edges may be added to the graph in order to reduce the length of the spanning tree. These new vertices introduced to decrease the total length of connection are known as Steiner points or Steiner vertices. It has been proved that the resulting connection is a tree, known as the Steiner tree. There may be several Steiner trees for a given set of initial vertices.
Also, you might want to look at Euclidean Steiner Tree on the wiki page. Seems relevant to your problem
Short answer: it enables to add custom points in the resulting mesh ; like in this video.

Rearranging a graph so that certain nodes are not adjacent?

EDIT: Precisely, I am trying to find two disjoint independent sets of known size in a graph shaped like a triangular grid, which may have holes and has a variable perimeter shape.
I'm not very well versed in graph theory, so I'm not sure if there exists an efficient solution for this problem. Consider the following graphs:
The colors of any two nodes can be swapped. The goal is to ensure that no two red nodes are adjacent, and no two green nodes are adjacent. The edges marked with exclamation points are invalid. Basically, I need to write two algorithms:
Determine that the nodes in a given graph can be arranged so that red and green nodes are not adjacent to nodes of the same color.
Actually rearrange the nodes.
I'm a little lost on how to implement this. It's not too difficult to separate the nodes of one color, but repeating the process for the second color may mess up the first color. Without a way to determine whether the graph can actually be arranged properly, this process could loop forever.
Is there some kind of algorithm that I can use/write for this? I'm mainly interested in the first image's graph (a triangular grid), but a generic algorithm would work as well.
First, let's note that the problem is a variant of graph coloring.
Now, if you only dealing with 2 colors (red,green) - coloring a graph with 2 colors is fairly easy, and is basically done by finding out if the graph is bipartite, and coloring each "side" of the graph in one color. Finding if a graph is bipartite is fairly simple.
However, if you want more than two colors, the problem becomes NP-Complete, and is actually a variant of the Graph Coloring Problem.
Graph Coloring Problem:
Given a graph G=(V,E) and a number k determine if there is a
function c:V->{1,2.,,,.k} such that c(v) = v(u) -> (v,u) is not an
edge.
Informally, you can color the graph in k colors, and you need to determine if there is some coloring such that you never color 2 nodes that share an edge with the same color.
Note that while it seems your problem is slightly easier, since you already know what is the number of nodes in each color, it doesn't really make a difference.
Assume you have a polynomial time algorithm A that solves your problem.
Now, given an instance (G,k) of graph coloring - there are only O(n^3) possibilities to #color1,#color2,#color3 - so by examining each of these and invoking A on it, you can find a polynomial time solution to Graph-Coloring. This will mean P=NP, which is most likely (according to most CS researchers) not the case.
tl;dr:
For 2 colors: find out if the graph is bipartite - and give one color to each side of the graph.
For 3 or more colors: There is no known efficient solution, and the general belief is one does not exist.
I thought this problem would be easier for planar graph, but unfortunately it's not the case. Best match for this problem I was able to find is minimum sum coloring and largest bipartite subgraph.
For largest bipartite subgraph, assume that number of reds + number of greens exactly match the size of largest bipartite subgraph. Then this problem is equivalent to your. Paper claims that it's still NP-hard even for planar graphs.
For minimum sum coloring, assume that red color has weight 1, green color has color 2, and we have infinitely many blue* colors with some weight of >graph size. Then if answer is exactly minimal sum coloring, there is no polynomial algorithm to find it (although paper referes to such algorithm for chordal graphs).
Anyway, it seems that the closer your red+green count to the 'optimal' in some sense subgraph, the more difficult problem is.
If you can afford inexact solution, or relaxed solution then you only spearate, say, reds, you have an option. As I said in comment, approximate solution of maximum independent set problem for planar graph. Then color this set into red and blue colors, if it is large enough.
If you know that red+green is much less than total number of vertices, another approximation can work. Look at introduction chapter of this article. It claims that:
For graphs which are promised to have small chromatic number, a better
guarantee is known: given a k-colorable graph, an algorithm due to
Karger et al. [12] uses semidefinite programming (SDP) to find an
independent set of size about Ω(n/∆^(1−2/k)).
Your graph is for sure 4-colorable, so you can count on large enough independent set. The same article states that greedy solution already can find large enough independent set.

Is there a graph coloring algorithm where limits can be placed on number of vertices per color

I understand that graph coloring is a NP-complete problem. I was wondering if adding a restriction on the number of vertices that can have a given color makes the problem simpler? I can't seem to find any algorithm which does this. For example if I have a graph, I'd like to say "what is the smallest coloring of this graph such that each color has at most 3 vertices", or if it simplifies the problem "is there a way to color this graph with 4 colors such that each color has at most 3 vertices"?
Thanks!
This problem is still NP-complete by a simple reduction from the original graph coloring problem: a graph with n nodes is k-colorable if and only if the graph can be colored with k colors and no color is assigned to more than n nodes. In other words, the general version of the problem you're phrasing has graph coloring as a special case, and so it will still be NP-hard.
Hope this helps!
I would say that looking for the chromatic number of a graph given the restriction that a k-coloring exists can be easily added to an exact DSATUR based algorithm [Randall-Brown 72] [San Segundo 11] and prune the search space when one vertex has to be assgined color k. In any case the problem remains in NP.

Resources