Create particular node order for graph coloring problemHow to increase node spacing for networkx.spring_layoutPlotting the same graph repeatedly — how to get nodes in the same position?plotting integer output from Networkxplotting a dictionary made from a TAB2 fileScaling NetworkX nodes and edges proportional to adjacency matrixnetworkx (pyplot) node size in terms of window sizeHow do I update DESTINATION of arrows in matplotlib?Adjacency matrix python (graph coloring)

How do I organize members in a struct to waste the least space on alignment?

What will happen if I checked in for another room in the same hotel, but not for the booked one?

I just started should I accept a farewell lunch for a coworker I don't know?

Bin Packing with Relational Penalization

How Do I Know When I am in Private Mode?

Are all commands with an optional argument fragile?

Do home values typically rise and fall at a consistent percent?

What are good ways to spray paint a QR code on a footpath?

13th chords on guitar

What verb for taking advantage fits in "I don't want to ________ on the friendship"?

Handling a player (unintentionally) stealing the spotlight

Losing queen and then winning the game

Why were the first airplanes "backwards"?

How could a satellite follow earth around the sun while staying outside of earth's orbit?

Journal standards vs. personal standards

Closest Proximity of Oceans to Freshwater Springs

Are gliders susceptible to bird strikes?

Why is Japan trying to have a better relationship with Iran?

What do you call a notepad used to keep a record?

How to get a character's limb regrown at 3rd level?

How do I ensure my employees don't abuse my flexible work hours policy?

My colleague is constantly blaming me for his errors

What is "override advice"?

How can I deal with extreme temperatures in a hotel room?



Create particular node order for graph coloring problem


How to increase node spacing for networkx.spring_layoutPlotting the same graph repeatedly — how to get nodes in the same position?plotting integer output from Networkxplotting a dictionary made from a TAB2 fileScaling NetworkX nodes and edges proportional to adjacency matrixnetworkx (pyplot) node size in terms of window sizeHow do I update DESTINATION of arrows in matplotlib?Adjacency matrix python (graph coloring)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I struggle with the algorithm to create the order in which I will color a graph.
Let's consider the following graph:



Network graph



import networkx as nx
from matplotlib import pyplot as plt

nodes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (1, 5), (5, 6), (6, 10),
(6, 7), (4, 7), (3, 8), (7, 8), (8, 9), (8, 11)]

# Create the graph
G = nx.Graph()
# Add edges
G.add_edges_from(edges)
# Plot
nx.draw(G, with_labels=True, font_size = 16)
plt.show()


I want to have multiple starting point, called initial_nodes and to create the order around the adjacent nodes. For the graph above, let's consider the starting nodes as the node 2 and 7.



The order would be:



# Step 1: - Initial nodes
order = [2, 7]
# Step 2: - Nodes adjacent to the initial nodes
order = [2, 7, 1, 3, 4, 6, 8]
# Step 3: - Nodes adjacent to the nodes added in the previous step
# If they are not already present in the order...
order = [2, 7, 1, 3, 4, 6, 8, 5, 10, 9, 11]


I feel like a recursive approach should work nice, but I can't figure how to write it down. Any idea?



EDIT: All the problem described a bit further.



Current algorithm to create the order:



def create_node_order(graph, initial_nodes):
"""
Create the node order.
"""
# Initialization
order = initial_nodes
next_nodes = list()

for node in initial_nodes:
for adj_node in graph.adj[node].keys():
if adj_node not in order:
order.append(adj_node)
next_nodes.append(adj_node)

while len(next_nodes) != 0:

for node in next_nodes:
for adj_node in graph.adj[node].keys():
if adj_node not in order:
order.append(adj_node)
next_nodes.append(adj_node)

next_nodes.remove(node)

return order









share|improve this question
























  • Is the goal to arrive at any coloring or is it required to find a minimal coloring? I know of no proof that this approach would yield an optimal coloring. Usually the order of coloring is very much not trivial and some approaches I know require backtracking, meaning that "order" is meaningless with them.

    – Etienne Ott
    Mar 25 at 14:28











  • @EtienneOtt I am no expert in graph theory... from what I read, there is no algorithm to arrive efficiently (less than exponential time) to an optimal coloring. I don't know exactly yet what backtracking is. From what I saw, it is about going down a path, and going back up to correct the colors in order to achieve a better global coloring. At the moment, I do not want to use backtracking, and I'd like to try a greedy approach. I will not describe the entire problem, but the order I describe above is chosen wisely based on my data and the coloring I am trying to achieve.

    – Mathieu
    Mar 25 at 14:31











  • @EtienneOtt I just can't figure out an algorithm to create this order efficiently... and although I feel like a recursive approach is the right choice, I can' tfigure out how to write it down.

    – Mathieu
    Mar 25 at 14:32

















0















I struggle with the algorithm to create the order in which I will color a graph.
Let's consider the following graph:



Network graph



import networkx as nx
from matplotlib import pyplot as plt

nodes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (1, 5), (5, 6), (6, 10),
(6, 7), (4, 7), (3, 8), (7, 8), (8, 9), (8, 11)]

# Create the graph
G = nx.Graph()
# Add edges
G.add_edges_from(edges)
# Plot
nx.draw(G, with_labels=True, font_size = 16)
plt.show()


I want to have multiple starting point, called initial_nodes and to create the order around the adjacent nodes. For the graph above, let's consider the starting nodes as the node 2 and 7.



The order would be:



# Step 1: - Initial nodes
order = [2, 7]
# Step 2: - Nodes adjacent to the initial nodes
order = [2, 7, 1, 3, 4, 6, 8]
# Step 3: - Nodes adjacent to the nodes added in the previous step
# If they are not already present in the order...
order = [2, 7, 1, 3, 4, 6, 8, 5, 10, 9, 11]


I feel like a recursive approach should work nice, but I can't figure how to write it down. Any idea?



EDIT: All the problem described a bit further.



Current algorithm to create the order:



def create_node_order(graph, initial_nodes):
"""
Create the node order.
"""
# Initialization
order = initial_nodes
next_nodes = list()

for node in initial_nodes:
for adj_node in graph.adj[node].keys():
if adj_node not in order:
order.append(adj_node)
next_nodes.append(adj_node)

while len(next_nodes) != 0:

for node in next_nodes:
for adj_node in graph.adj[node].keys():
if adj_node not in order:
order.append(adj_node)
next_nodes.append(adj_node)

next_nodes.remove(node)

return order









share|improve this question
























  • Is the goal to arrive at any coloring or is it required to find a minimal coloring? I know of no proof that this approach would yield an optimal coloring. Usually the order of coloring is very much not trivial and some approaches I know require backtracking, meaning that "order" is meaningless with them.

    – Etienne Ott
    Mar 25 at 14:28











  • @EtienneOtt I am no expert in graph theory... from what I read, there is no algorithm to arrive efficiently (less than exponential time) to an optimal coloring. I don't know exactly yet what backtracking is. From what I saw, it is about going down a path, and going back up to correct the colors in order to achieve a better global coloring. At the moment, I do not want to use backtracking, and I'd like to try a greedy approach. I will not describe the entire problem, but the order I describe above is chosen wisely based on my data and the coloring I am trying to achieve.

    – Mathieu
    Mar 25 at 14:31











  • @EtienneOtt I just can't figure out an algorithm to create this order efficiently... and although I feel like a recursive approach is the right choice, I can' tfigure out how to write it down.

    – Mathieu
    Mar 25 at 14:32













0












0








0








I struggle with the algorithm to create the order in which I will color a graph.
Let's consider the following graph:



Network graph



import networkx as nx
from matplotlib import pyplot as plt

nodes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (1, 5), (5, 6), (6, 10),
(6, 7), (4, 7), (3, 8), (7, 8), (8, 9), (8, 11)]

# Create the graph
G = nx.Graph()
# Add edges
G.add_edges_from(edges)
# Plot
nx.draw(G, with_labels=True, font_size = 16)
plt.show()


I want to have multiple starting point, called initial_nodes and to create the order around the adjacent nodes. For the graph above, let's consider the starting nodes as the node 2 and 7.



The order would be:



# Step 1: - Initial nodes
order = [2, 7]
# Step 2: - Nodes adjacent to the initial nodes
order = [2, 7, 1, 3, 4, 6, 8]
# Step 3: - Nodes adjacent to the nodes added in the previous step
# If they are not already present in the order...
order = [2, 7, 1, 3, 4, 6, 8, 5, 10, 9, 11]


I feel like a recursive approach should work nice, but I can't figure how to write it down. Any idea?



EDIT: All the problem described a bit further.



Current algorithm to create the order:



def create_node_order(graph, initial_nodes):
"""
Create the node order.
"""
# Initialization
order = initial_nodes
next_nodes = list()

for node in initial_nodes:
for adj_node in graph.adj[node].keys():
if adj_node not in order:
order.append(adj_node)
next_nodes.append(adj_node)

while len(next_nodes) != 0:

for node in next_nodes:
for adj_node in graph.adj[node].keys():
if adj_node not in order:
order.append(adj_node)
next_nodes.append(adj_node)

next_nodes.remove(node)

return order









share|improve this question
















I struggle with the algorithm to create the order in which I will color a graph.
Let's consider the following graph:



Network graph



import networkx as nx
from matplotlib import pyplot as plt

nodes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
edges = [(1, 2), (2, 3), (3, 4), (4, 5), (1, 5), (5, 6), (6, 10),
(6, 7), (4, 7), (3, 8), (7, 8), (8, 9), (8, 11)]

# Create the graph
G = nx.Graph()
# Add edges
G.add_edges_from(edges)
# Plot
nx.draw(G, with_labels=True, font_size = 16)
plt.show()


I want to have multiple starting point, called initial_nodes and to create the order around the adjacent nodes. For the graph above, let's consider the starting nodes as the node 2 and 7.



The order would be:



# Step 1: - Initial nodes
order = [2, 7]
# Step 2: - Nodes adjacent to the initial nodes
order = [2, 7, 1, 3, 4, 6, 8]
# Step 3: - Nodes adjacent to the nodes added in the previous step
# If they are not already present in the order...
order = [2, 7, 1, 3, 4, 6, 8, 5, 10, 9, 11]


I feel like a recursive approach should work nice, but I can't figure how to write it down. Any idea?



EDIT: All the problem described a bit further.



Current algorithm to create the order:



def create_node_order(graph, initial_nodes):
"""
Create the node order.
"""
# Initialization
order = initial_nodes
next_nodes = list()

for node in initial_nodes:
for adj_node in graph.adj[node].keys():
if adj_node not in order:
order.append(adj_node)
next_nodes.append(adj_node)

while len(next_nodes) != 0:

for node in next_nodes:
for adj_node in graph.adj[node].keys():
if adj_node not in order:
order.append(adj_node)
next_nodes.append(adj_node)

next_nodes.remove(node)

return order






python networkx graph-theory graph-coloring






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 14:42







Mathieu

















asked Mar 25 at 14:16









MathieuMathieu

1,8141 gold badge8 silver badges30 bronze badges




1,8141 gold badge8 silver badges30 bronze badges












  • Is the goal to arrive at any coloring or is it required to find a minimal coloring? I know of no proof that this approach would yield an optimal coloring. Usually the order of coloring is very much not trivial and some approaches I know require backtracking, meaning that "order" is meaningless with them.

    – Etienne Ott
    Mar 25 at 14:28











  • @EtienneOtt I am no expert in graph theory... from what I read, there is no algorithm to arrive efficiently (less than exponential time) to an optimal coloring. I don't know exactly yet what backtracking is. From what I saw, it is about going down a path, and going back up to correct the colors in order to achieve a better global coloring. At the moment, I do not want to use backtracking, and I'd like to try a greedy approach. I will not describe the entire problem, but the order I describe above is chosen wisely based on my data and the coloring I am trying to achieve.

    – Mathieu
    Mar 25 at 14:31











  • @EtienneOtt I just can't figure out an algorithm to create this order efficiently... and although I feel like a recursive approach is the right choice, I can' tfigure out how to write it down.

    – Mathieu
    Mar 25 at 14:32

















  • Is the goal to arrive at any coloring or is it required to find a minimal coloring? I know of no proof that this approach would yield an optimal coloring. Usually the order of coloring is very much not trivial and some approaches I know require backtracking, meaning that "order" is meaningless with them.

    – Etienne Ott
    Mar 25 at 14:28











  • @EtienneOtt I am no expert in graph theory... from what I read, there is no algorithm to arrive efficiently (less than exponential time) to an optimal coloring. I don't know exactly yet what backtracking is. From what I saw, it is about going down a path, and going back up to correct the colors in order to achieve a better global coloring. At the moment, I do not want to use backtracking, and I'd like to try a greedy approach. I will not describe the entire problem, but the order I describe above is chosen wisely based on my data and the coloring I am trying to achieve.

    – Mathieu
    Mar 25 at 14:31











  • @EtienneOtt I just can't figure out an algorithm to create this order efficiently... and although I feel like a recursive approach is the right choice, I can' tfigure out how to write it down.

    – Mathieu
    Mar 25 at 14:32
















Is the goal to arrive at any coloring or is it required to find a minimal coloring? I know of no proof that this approach would yield an optimal coloring. Usually the order of coloring is very much not trivial and some approaches I know require backtracking, meaning that "order" is meaningless with them.

– Etienne Ott
Mar 25 at 14:28





Is the goal to arrive at any coloring or is it required to find a minimal coloring? I know of no proof that this approach would yield an optimal coloring. Usually the order of coloring is very much not trivial and some approaches I know require backtracking, meaning that "order" is meaningless with them.

– Etienne Ott
Mar 25 at 14:28













@EtienneOtt I am no expert in graph theory... from what I read, there is no algorithm to arrive efficiently (less than exponential time) to an optimal coloring. I don't know exactly yet what backtracking is. From what I saw, it is about going down a path, and going back up to correct the colors in order to achieve a better global coloring. At the moment, I do not want to use backtracking, and I'd like to try a greedy approach. I will not describe the entire problem, but the order I describe above is chosen wisely based on my data and the coloring I am trying to achieve.

– Mathieu
Mar 25 at 14:31





@EtienneOtt I am no expert in graph theory... from what I read, there is no algorithm to arrive efficiently (less than exponential time) to an optimal coloring. I don't know exactly yet what backtracking is. From what I saw, it is about going down a path, and going back up to correct the colors in order to achieve a better global coloring. At the moment, I do not want to use backtracking, and I'd like to try a greedy approach. I will not describe the entire problem, but the order I describe above is chosen wisely based on my data and the coloring I am trying to achieve.

– Mathieu
Mar 25 at 14:31













@EtienneOtt I just can't figure out an algorithm to create this order efficiently... and although I feel like a recursive approach is the right choice, I can' tfigure out how to write it down.

– Mathieu
Mar 25 at 14:32





@EtienneOtt I just can't figure out an algorithm to create this order efficiently... and although I feel like a recursive approach is the right choice, I can' tfigure out how to write it down.

– Mathieu
Mar 25 at 14:32












1 Answer
1






active

oldest

votes


















1














Note that is was not a requirement, or even proven to be possible, to result in an optimal coloring given the approach of iterating over "circles" "radiating" out from some start nodes. Given my understand of the algorithm described and what the requirements are, I would use something like this:



Pseudocode:



// no need for more than four colors IFF the algorithm is optimal and the graph is planar, otherwise extend
colors = [red, blue, green, yellow]
// initialize
graph = SomeSuitableDataStructure(data)
queue = [graph(2), graph(7)] // starting nodes
for node in graph.nodes:
node.visited = False
node.color = Undefined

while not queue.empty():
node = queue.pop()
node.visited = True
node.color = first_color_not_in([n.color for n in node.neighbors()])
for neighbor in node.neighbors():
if not neighbor.visited: queue.push_back(neighbor)


Implementing first_color_not_in() and handling the Undefined color is left as an excercise to the reader.






share|improve this answer

























  • This is doing the coloring and not just the order. I was simply looking in a way to produce the order in a more efficient way that the code I added in the Edit. My problem is actually a bit different. Each node has a list of available alternative values, and the goal is to select one for each node in such a way that the deviation between close-by nodes (linked) is minimized. On the contrary of a coloring problem, the goal is to have connected nodes with the same, or with the closest possible color... That's why the "circles" approach makes sense and that's why I was looking only for the order

    – Mathieu
    Mar 25 at 15:05












  • Anyway, indeed, with a pure coloring problem, your approach is great and indeed, the maximum number of required colors is d+1 with d the degree.

    – Mathieu
    Mar 25 at 15:06











  • Okay, I wasn't sure about the difference between the order of iteration for constructing a queue and the order of coloring, because with most algorithms it is the same, or more precisely, it is the crux of the problem and finding one is finding the other.

    – Etienne Ott
    Mar 25 at 15:12











  • And by the way, it's not maxed 4 colors if the algorithm is optimal. It's rather that the greedy algorithm will in worst case need 4 colors, depending on the order in which the coloring is performed.

    – Mathieu
    Mar 25 at 15:14











  • And I am not sure what you mean by order of the queue, since both in your algorithm, and on the one I am writing, the order of the queue is the order in which the nodes are colored.

    – Mathieu
    Mar 25 at 15:15










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55339866%2fcreate-particular-node-order-for-graph-coloring-problem%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Note that is was not a requirement, or even proven to be possible, to result in an optimal coloring given the approach of iterating over "circles" "radiating" out from some start nodes. Given my understand of the algorithm described and what the requirements are, I would use something like this:



Pseudocode:



// no need for more than four colors IFF the algorithm is optimal and the graph is planar, otherwise extend
colors = [red, blue, green, yellow]
// initialize
graph = SomeSuitableDataStructure(data)
queue = [graph(2), graph(7)] // starting nodes
for node in graph.nodes:
node.visited = False
node.color = Undefined

while not queue.empty():
node = queue.pop()
node.visited = True
node.color = first_color_not_in([n.color for n in node.neighbors()])
for neighbor in node.neighbors():
if not neighbor.visited: queue.push_back(neighbor)


Implementing first_color_not_in() and handling the Undefined color is left as an excercise to the reader.






share|improve this answer

























  • This is doing the coloring and not just the order. I was simply looking in a way to produce the order in a more efficient way that the code I added in the Edit. My problem is actually a bit different. Each node has a list of available alternative values, and the goal is to select one for each node in such a way that the deviation between close-by nodes (linked) is minimized. On the contrary of a coloring problem, the goal is to have connected nodes with the same, or with the closest possible color... That's why the "circles" approach makes sense and that's why I was looking only for the order

    – Mathieu
    Mar 25 at 15:05












  • Anyway, indeed, with a pure coloring problem, your approach is great and indeed, the maximum number of required colors is d+1 with d the degree.

    – Mathieu
    Mar 25 at 15:06











  • Okay, I wasn't sure about the difference between the order of iteration for constructing a queue and the order of coloring, because with most algorithms it is the same, or more precisely, it is the crux of the problem and finding one is finding the other.

    – Etienne Ott
    Mar 25 at 15:12











  • And by the way, it's not maxed 4 colors if the algorithm is optimal. It's rather that the greedy algorithm will in worst case need 4 colors, depending on the order in which the coloring is performed.

    – Mathieu
    Mar 25 at 15:14











  • And I am not sure what you mean by order of the queue, since both in your algorithm, and on the one I am writing, the order of the queue is the order in which the nodes are colored.

    – Mathieu
    Mar 25 at 15:15















1














Note that is was not a requirement, or even proven to be possible, to result in an optimal coloring given the approach of iterating over "circles" "radiating" out from some start nodes. Given my understand of the algorithm described and what the requirements are, I would use something like this:



Pseudocode:



// no need for more than four colors IFF the algorithm is optimal and the graph is planar, otherwise extend
colors = [red, blue, green, yellow]
// initialize
graph = SomeSuitableDataStructure(data)
queue = [graph(2), graph(7)] // starting nodes
for node in graph.nodes:
node.visited = False
node.color = Undefined

while not queue.empty():
node = queue.pop()
node.visited = True
node.color = first_color_not_in([n.color for n in node.neighbors()])
for neighbor in node.neighbors():
if not neighbor.visited: queue.push_back(neighbor)


Implementing first_color_not_in() and handling the Undefined color is left as an excercise to the reader.






share|improve this answer

























  • This is doing the coloring and not just the order. I was simply looking in a way to produce the order in a more efficient way that the code I added in the Edit. My problem is actually a bit different. Each node has a list of available alternative values, and the goal is to select one for each node in such a way that the deviation between close-by nodes (linked) is minimized. On the contrary of a coloring problem, the goal is to have connected nodes with the same, or with the closest possible color... That's why the "circles" approach makes sense and that's why I was looking only for the order

    – Mathieu
    Mar 25 at 15:05












  • Anyway, indeed, with a pure coloring problem, your approach is great and indeed, the maximum number of required colors is d+1 with d the degree.

    – Mathieu
    Mar 25 at 15:06











  • Okay, I wasn't sure about the difference between the order of iteration for constructing a queue and the order of coloring, because with most algorithms it is the same, or more precisely, it is the crux of the problem and finding one is finding the other.

    – Etienne Ott
    Mar 25 at 15:12











  • And by the way, it's not maxed 4 colors if the algorithm is optimal. It's rather that the greedy algorithm will in worst case need 4 colors, depending on the order in which the coloring is performed.

    – Mathieu
    Mar 25 at 15:14











  • And I am not sure what you mean by order of the queue, since both in your algorithm, and on the one I am writing, the order of the queue is the order in which the nodes are colored.

    – Mathieu
    Mar 25 at 15:15













1












1








1







Note that is was not a requirement, or even proven to be possible, to result in an optimal coloring given the approach of iterating over "circles" "radiating" out from some start nodes. Given my understand of the algorithm described and what the requirements are, I would use something like this:



Pseudocode:



// no need for more than four colors IFF the algorithm is optimal and the graph is planar, otherwise extend
colors = [red, blue, green, yellow]
// initialize
graph = SomeSuitableDataStructure(data)
queue = [graph(2), graph(7)] // starting nodes
for node in graph.nodes:
node.visited = False
node.color = Undefined

while not queue.empty():
node = queue.pop()
node.visited = True
node.color = first_color_not_in([n.color for n in node.neighbors()])
for neighbor in node.neighbors():
if not neighbor.visited: queue.push_back(neighbor)


Implementing first_color_not_in() and handling the Undefined color is left as an excercise to the reader.






share|improve this answer















Note that is was not a requirement, or even proven to be possible, to result in an optimal coloring given the approach of iterating over "circles" "radiating" out from some start nodes. Given my understand of the algorithm described and what the requirements are, I would use something like this:



Pseudocode:



// no need for more than four colors IFF the algorithm is optimal and the graph is planar, otherwise extend
colors = [red, blue, green, yellow]
// initialize
graph = SomeSuitableDataStructure(data)
queue = [graph(2), graph(7)] // starting nodes
for node in graph.nodes:
node.visited = False
node.color = Undefined

while not queue.empty():
node = queue.pop()
node.visited = True
node.color = first_color_not_in([n.color for n in node.neighbors()])
for neighbor in node.neighbors():
if not neighbor.visited: queue.push_back(neighbor)


Implementing first_color_not_in() and handling the Undefined color is left as an excercise to the reader.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 15:18

























answered Mar 25 at 14:56









Etienne OttEtienne Ott

3631 silver badge8 bronze badges




3631 silver badge8 bronze badges












  • This is doing the coloring and not just the order. I was simply looking in a way to produce the order in a more efficient way that the code I added in the Edit. My problem is actually a bit different. Each node has a list of available alternative values, and the goal is to select one for each node in such a way that the deviation between close-by nodes (linked) is minimized. On the contrary of a coloring problem, the goal is to have connected nodes with the same, or with the closest possible color... That's why the "circles" approach makes sense and that's why I was looking only for the order

    – Mathieu
    Mar 25 at 15:05












  • Anyway, indeed, with a pure coloring problem, your approach is great and indeed, the maximum number of required colors is d+1 with d the degree.

    – Mathieu
    Mar 25 at 15:06











  • Okay, I wasn't sure about the difference between the order of iteration for constructing a queue and the order of coloring, because with most algorithms it is the same, or more precisely, it is the crux of the problem and finding one is finding the other.

    – Etienne Ott
    Mar 25 at 15:12











  • And by the way, it's not maxed 4 colors if the algorithm is optimal. It's rather that the greedy algorithm will in worst case need 4 colors, depending on the order in which the coloring is performed.

    – Mathieu
    Mar 25 at 15:14











  • And I am not sure what you mean by order of the queue, since both in your algorithm, and on the one I am writing, the order of the queue is the order in which the nodes are colored.

    – Mathieu
    Mar 25 at 15:15

















  • This is doing the coloring and not just the order. I was simply looking in a way to produce the order in a more efficient way that the code I added in the Edit. My problem is actually a bit different. Each node has a list of available alternative values, and the goal is to select one for each node in such a way that the deviation between close-by nodes (linked) is minimized. On the contrary of a coloring problem, the goal is to have connected nodes with the same, or with the closest possible color... That's why the "circles" approach makes sense and that's why I was looking only for the order

    – Mathieu
    Mar 25 at 15:05












  • Anyway, indeed, with a pure coloring problem, your approach is great and indeed, the maximum number of required colors is d+1 with d the degree.

    – Mathieu
    Mar 25 at 15:06











  • Okay, I wasn't sure about the difference between the order of iteration for constructing a queue and the order of coloring, because with most algorithms it is the same, or more precisely, it is the crux of the problem and finding one is finding the other.

    – Etienne Ott
    Mar 25 at 15:12











  • And by the way, it's not maxed 4 colors if the algorithm is optimal. It's rather that the greedy algorithm will in worst case need 4 colors, depending on the order in which the coloring is performed.

    – Mathieu
    Mar 25 at 15:14











  • And I am not sure what you mean by order of the queue, since both in your algorithm, and on the one I am writing, the order of the queue is the order in which the nodes are colored.

    – Mathieu
    Mar 25 at 15:15
















This is doing the coloring and not just the order. I was simply looking in a way to produce the order in a more efficient way that the code I added in the Edit. My problem is actually a bit different. Each node has a list of available alternative values, and the goal is to select one for each node in such a way that the deviation between close-by nodes (linked) is minimized. On the contrary of a coloring problem, the goal is to have connected nodes with the same, or with the closest possible color... That's why the "circles" approach makes sense and that's why I was looking only for the order

– Mathieu
Mar 25 at 15:05






This is doing the coloring and not just the order. I was simply looking in a way to produce the order in a more efficient way that the code I added in the Edit. My problem is actually a bit different. Each node has a list of available alternative values, and the goal is to select one for each node in such a way that the deviation between close-by nodes (linked) is minimized. On the contrary of a coloring problem, the goal is to have connected nodes with the same, or with the closest possible color... That's why the "circles" approach makes sense and that's why I was looking only for the order

– Mathieu
Mar 25 at 15:05














Anyway, indeed, with a pure coloring problem, your approach is great and indeed, the maximum number of required colors is d+1 with d the degree.

– Mathieu
Mar 25 at 15:06





Anyway, indeed, with a pure coloring problem, your approach is great and indeed, the maximum number of required colors is d+1 with d the degree.

– Mathieu
Mar 25 at 15:06













Okay, I wasn't sure about the difference between the order of iteration for constructing a queue and the order of coloring, because with most algorithms it is the same, or more precisely, it is the crux of the problem and finding one is finding the other.

– Etienne Ott
Mar 25 at 15:12





Okay, I wasn't sure about the difference between the order of iteration for constructing a queue and the order of coloring, because with most algorithms it is the same, or more precisely, it is the crux of the problem and finding one is finding the other.

– Etienne Ott
Mar 25 at 15:12













And by the way, it's not maxed 4 colors if the algorithm is optimal. It's rather that the greedy algorithm will in worst case need 4 colors, depending on the order in which the coloring is performed.

– Mathieu
Mar 25 at 15:14





And by the way, it's not maxed 4 colors if the algorithm is optimal. It's rather that the greedy algorithm will in worst case need 4 colors, depending on the order in which the coloring is performed.

– Mathieu
Mar 25 at 15:14













And I am not sure what you mean by order of the queue, since both in your algorithm, and on the one I am writing, the order of the queue is the order in which the nodes are colored.

– Mathieu
Mar 25 at 15:15





And I am not sure what you mean by order of the queue, since both in your algorithm, and on the one I am writing, the order of the queue is the order in which the nodes are colored.

– Mathieu
Mar 25 at 15:15








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55339866%2fcreate-particular-node-order-for-graph-coloring-problem%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현