Skip to content

Instantly share code, notes, and snippets.

@babetoduarte
Forked from sramana/graph_coloring.py
Created March 9, 2021 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babetoduarte/46c0e136a6913ce2566300dbdbc9e3f0 to your computer and use it in GitHub Desktop.
Save babetoduarte/46c0e136a6913ce2566300dbdbc9e3f0 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment