Abdulg

Python greedy graph colourings

Nov 22nd, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import os.path
  2.  
  3. def checkFile(filename):
  4.     return os.path.isfile(filename)
  5.  
  6. def checkConsistency(filename):
  7.     inFile = open(filename, 'r')
  8.     character = inFile.read()
  9.     validcharacters = 0
  10.     while not (character == ''):
  11.         if character == '0' or character == '1':
  12.             validcharacters += 1
  13.     size = sqrt(validcharacters)
  14.     if size.is_integer():
  15.         return True, size
  16.     else:
  17.         return False, 0
  18.    
  19. def adjMatrixToLists(inMat):
  20.     adjlist = []
  21.     for i in range(len(inMat)):
  22.         ilist = []
  23.         for j in range(len(inMat[i])):
  24.             if inMat[i][j] == 1:
  25.                 ilist.append(j)
  26.         adjlist.append(ilist)
  27.     return adjlist
  28.  
  29. def adjListToMatrix(inList):
  30.     matrix = [[0 for x in range(len(inList))] for x in range(len(inList))]
  31.     for i in range(0, len(inList)):
  32.         for j in inList[i]:
  33.             matrix[i][j] = 1
  34.     return matrix
  35.  
  36. def readGraphIntoMatrix(filename, n):
  37.     inFile = open(filename, 'r')
  38.     matrix = [['3' for x in range(n)] for x in range(n)]
  39.    
  40.     for i in range(0,n):
  41.         for j in range(0,n):
  42.             matrix[i][j] = int(inFile.read(1))
  43.    
  44.     return matrix
  45.    
  46. def printMatrix(matrix):
  47.     for i in matrix:
  48.         for j in i:
  49.             print(j, end="")
  50.         print();
  51.            
  52.  
  53. def graphFacts(adjMatrix):
  54.     numVerticies = len(adjMatrix)
  55.     numEdges = 0
  56.    
  57.     Degrees = [0] * numVerticies
  58.    
  59.     iIndex = 0
  60.     jIndex = 0
  61.    
  62.     for i in adjMatrix:
  63.         for j in i:
  64.             if iIndex == jIndex:
  65.                 break
  66.                
  67.             if j == 1:
  68.                 numEdges += 1
  69.                 Degrees[iIndex] += 1
  70.                 Degrees[jIndex] += 1
  71.             jIndex += 1
  72.         jIndex = 0;
  73.         iIndex += 1
  74.                
  75.     print("Number of verticies:\t", numVerticies);
  76.     print("Number of edges:\t",  numEdges);
  77.     print("\nVertex\tDegree")
  78.    
  79.     for i in range(0,numVerticies):
  80.         print(i, "\t", Degrees[i])
  81.  
  82. def greedyColouring1(inList):
  83.     colours = [0] * len(inList)
  84.     for i in range(0, len(inList)):
  85.         adjColours = []
  86.         for j in inList[i]:
  87.             adjColours.append(colours[j])
  88.         colours[i] = max(adjColours) + 1
  89.     print("Coloured using", max(colours), "colours.")
  90.     return colours
  91.    
  92. def greedyColouring2(inList):
  93.     colours = [0] * len(inList)
  94.     for x in range(0, len(inList)):
  95.         lowestColourDict = {}
  96.         for i in range(0, len(inList)):
  97.             if colours[i] != 0:
  98.                 continue
  99.             adjColours = []
  100.             for j in inList[i]:
  101.                 adjColours.append(colours[j])
  102.             lowestColourDict[i] = max(adjColours) + 1
  103.         minVert = min(lowestColourDict, key = lowestColourDict.get)
  104.         colours[minVert] = lowestColourDict[minVert]
  105.     print("Coloured using", max(colours), "colours.")
  106.     return colours
  107.  
  108. def checkColouring(adjList, colours):
  109.     iIndex = 0
  110.     for i in adjList:
  111.         for j in i:
  112.             if colours[j] == colours[iIndex]:
  113.                 return False
  114.         iIndex += 1
  115.     return True        
  116.  
  117. matrix = readGraphIntoMatrix("SampleGraphs/CTexampledigraph17.txt", 17)
  118. lists = adjMatrixToLists(matrix)
  119. colours = greedyColouring1(lists)
  120. print("Legit colouring?", checkColouring(lists, colours))
  121. colours = greedyColouring2(lists)
  122. print("Legit colouring?", checkColouring(lists, colours))
Advertisement
Add Comment
Please, Sign In to add comment