| Conditions | 17 |
| Total Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like dsatur.process() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """! |
||
| 50 | def process(self): |
||
| 51 | """! |
||
| 52 | @brief Perform graph coloring using DSATUR algorithm. |
||
| 53 | |||
| 54 | @see get_colors() |
||
| 55 | |||
| 56 | """ |
||
| 57 | color_counter = 1; |
||
| 58 | |||
| 59 | degrees = list(); |
||
| 60 | saturation_degrees = [0] * len(self.__data_pointer); |
||
| 61 | |||
| 62 | self.__coloring = [0] * len(self.__data_pointer); |
||
| 63 | uncolored_vertices = set(range(len(self.__data_pointer))); |
||
| 64 | |||
| 65 | index_maximum_degree = 0; |
||
| 66 | maximum_degree = 0; |
||
| 67 | for index_node in range(len(self.__data_pointer)): |
||
| 68 | # Fill degree of nodes in the input graph |
||
| 69 | degrees.append( ( sum(self.__data_pointer[index_node]), index_node ) ); |
||
| 70 | |||
| 71 | # And find node with maximal degree at the same time. |
||
| 72 | if (degrees[index_node][0] > maximum_degree): |
||
| 73 | (maximum_degree, node_index) = degrees[index_node]; |
||
| 74 | index_maximum_degree = index_node; |
||
| 75 | |||
| 76 | # Update saturation |
||
| 77 | neighbors = self.__get_neighbors(index_maximum_degree); |
||
| 78 | for index_neighbor in neighbors: |
||
| 79 | saturation_degrees[index_neighbor] += 1; |
||
| 80 | |||
| 81 | # Coloring the first node |
||
| 82 | self.__coloring[index_maximum_degree] = color_counter; |
||
| 83 | uncolored_vertices.remove(index_maximum_degree); |
||
| 84 | |||
| 85 | while(len(uncolored_vertices) > 0): |
||
| 86 | # Get maximum saturation degree |
||
| 87 | maximum_satur_degree = -1; |
||
| 88 | for index in uncolored_vertices: |
||
| 89 | if (saturation_degrees[index] > maximum_satur_degree): |
||
| 90 | maximum_satur_degree = saturation_degrees[index]; |
||
| 91 | |||
| 92 | # Get list of indexes with maximum saturation degree |
||
| 93 | indexes_maximum_satur_degree = [index for index in uncolored_vertices if saturation_degrees[index] == maximum_satur_degree]; |
||
| 94 | |||
| 95 | coloring_index = indexes_maximum_satur_degree[0]; |
||
| 96 | if (len(indexes_maximum_satur_degree) > 1): # There are more then one node with maximum saturation |
||
| 97 | # Find node with maximum degree |
||
| 98 | maximum_degree = -1; |
||
| 99 | for index in indexes_maximum_satur_degree: |
||
| 100 | (degree, node_index) = degrees[index]; |
||
| 101 | if (degree > maximum_degree): |
||
| 102 | coloring_index = node_index; |
||
| 103 | maximum_degree = degree; |
||
| 104 | |||
| 105 | # Coloring |
||
| 106 | node_index_neighbors = self.__get_neighbors(coloring_index); |
||
| 107 | for number_color in range(1, color_counter + 1, 1): |
||
| 108 | if (self.__get_amount_color(node_index_neighbors, number_color) == 0): |
||
| 109 | self.__coloring[coloring_index] = number_color; |
||
| 110 | break; |
||
| 111 | |||
| 112 | # If it has not been colored then |
||
| 113 | if (self.__coloring[coloring_index] == 0): |
||
| 114 | color_counter += 1; # Add new color |
||
| 115 | self.__coloring[coloring_index] = color_counter; |
||
| 116 | |||
| 117 | # Remove node from uncolored set |
||
| 118 | uncolored_vertices.remove(coloring_index); |
||
| 119 | |||
| 120 | |||
| 121 | # Update degree of saturation |
||
| 122 | for index_neighbor in node_index_neighbors: |
||
| 123 | subneighbors = self.__get_neighbors(index_neighbor); |
||
| 124 | |||
| 125 | if (self.__get_amount_color(subneighbors, self.__coloring[coloring_index]) == 1): |
||
| 126 | saturation_degrees[index_neighbor] += 1; |
||
| 127 | |||
| 171 |