Skip to content

Instantly share code, notes, and snippets.

@sramana
Created September 17, 2010 04:09
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save sramana/583681 to your computer and use it in GitHub Desktop.
Save sramana/583681 to your computer and use it in GitHub Desktop.
Python Program for Graph Coloring Problem
colors = ['Red', 'Blue', 'Green', 'Yellow', 'Black']
states = ['Andhra', 'Karnataka', 'TamilNadu', 'Kerala']
neighbors = {}
neighbors['Andhra'] = ['Karnataka', 'TamilNadu']
neighbors['Karnataka'] = ['Andhra', 'TamilNadu', 'Kerala']
neighbors['TamilNadu'] = ['Andhra', 'Karnataka', 'Kerala']
neighbors['Kerala'] = ['Karnataka', 'TamilNadu']
colors_of_states = {}
def promising(state, color):
for neighbor in neighbors.get(state):
color_of_neighbor = colors_of_states.get(neighbor)
if color_of_neighbor == color:
return False
return True
def get_color_for_state(state):
for color in colors:
if promising(state, color):
return color
def main():
for state in states:
colors_of_states[state] = get_color_for_state(state)
print colors_of_states
main()
@braska
Copy link

braska commented Mar 23, 2016

If I have understood correctly, it is greedy algorithm (not exact algorithm), isn't it?

@Toornaw
Copy link

Toornaw commented Apr 23, 2018

Extraordinary effort!

It saves huge amount of time for solving Super Graph Coloring problem for my algorithm graduate course project. I have modified this code for solving my problem. Big thanks for this code writer. I expect more contribution from him for solving different complex algorithmic problems, specially in python and share those solutions on GitHub.

@repdom
Copy link

repdom commented Jun 4, 2018

Thanks you bro!!! I consulted this algorithm for my new project. Thanks from Dominican Republic

@asad145
Copy link

asad145 commented May 9, 2019

It does not provide correct output all the time.

@toqeer618
Copy link

Extraordinary effort!

It saves huge amount of time for solving Super Graph Coloring problem for my algorithm graduate course project. I have modified this code for solving my problem. Big thanks for this code writer. I expect more contribution from him for solving different complex algorithmic problems, specially in python and share those solutions on GitHub.

can i get that code.

@easternmie
Copy link

how to relate this with hill climbing algo?

@mufassir-khan
Copy link

It does not provide correct output all the time.

agree, good but inefficient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment