diff --git a/divide_and_conquer/graphcolor.py b/divide_and_conquer/graphcolor.py new file mode 100644 index 000000000000..428a8ee74848 --- /dev/null +++ b/divide_and_conquer/graphcolor.py @@ -0,0 +1,67 @@ +from collections import defaultdict +from typing import dict, list + + +class Graph: + def __init__(self, subjects: list[str]) -> None: + """ + Initialize a Graph instance with a list of subjects. + + :param subjects: A list of subjects to be represented in the graph. + """ + self.subjects = subjects + self.graph = defaultdict(list) + + def add_edge(self, subject1: str, subject2: str) -> None: + """ + Add an edge between two subjects in the graph. + + :param subject1: The first subject to connect. + :param subject2: The second subject to connect. + """ + self.graph[subject1].append(subject2) + self.graph[subject2].append(subject1) + + def graph_coloring(self) -> dict[str, int]: + color_map = {} + available_colors = set(range(1, len(self.subjects) + 1)) + for subject in self.subjects: + used_colors = set() + + for neighbor in self.graph[subject]: + if neighbor in color_map: + used_colors.add(color_map[neighbor]) + + available_color = available_colors - used_colors + if available_color: + color_map[subject] = min(available_color) + else: + # If no available color, assign a new color + color_map[subject] = len(available_colors) + 1 + available_colors.add(color_map[subject]) + return color_map + + def get_minimum_time_slots(self) -> int: + color_map = self.graph_coloring() + return max(color_map.values()) + + +# Example usage +subjects = ["Math", "Physics", "Chemistry", "Biology"] +students = { + "Math": ["Alice", "Bob", "Charlie"], + "Physics": ["Alice", "Charlie", "David"], + "Chemistry": ["Bob", "Charlie", "Eve"], + "Biology": ["Alice", "David", "Eve"], +} +graph = Graph(subjects) +graph.add_edge("Math", "Physics") +graph.add_edge("Math", "Chemistry") +graph.add_edge("Physics", "Chemistry") +graph.add_edge("Physics", "Biology") + +# Example doctest for add_edge method +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/spiralmatrix.py b/dynamic_programming/spiralmatrix.py new file mode 100644 index 000000000000..6dfbc9399a65 --- /dev/null +++ b/dynamic_programming/spiralmatrix.py @@ -0,0 +1,39 @@ +def spiral_order(matrix): + result = [] + if not matrix: + return result + + rows, cols = len(matrix), len(matrix[0]) + top, bottom, left, right = 0, rows - 1, 0, cols - 1 + + while top <= bottom and left <= right: + # Traverse from left to right + for i in range(left, right + 1): + result.append(matrix[top][i]) + top += 1 + + # Traverse from top to bottom + for i in range(top, bottom + 1): + result.append(matrix[i][right]) + right -= 1 + + if top <= bottom: + # Traverse from right to left + for i in range(right, left - 1, -1): + result.append(matrix[bottom][i]) + bottom -= 1 + + if left <= right: + # Traverse from bottom to top + for i in range(bottom, top - 1, -1): + result.append(matrix[i][left]) + left += 1 + + return result + + +# Example usage: +matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] + +result = spiral_order(matrix) +print(" ".join(map(str, result))) diff --git a/dynamic_programming/triangularfuzz.py b/dynamic_programming/triangularfuzz.py new file mode 100644 index 000000000000..d4a5e59a5090 --- /dev/null +++ b/dynamic_programming/triangularfuzz.py @@ -0,0 +1,39 @@ +class TriangularFuzzySet: + def __init__(self, a, b, c): + """ + Initializes a triangular fuzzy set with the given parameters. + + :param a: Left point of the fuzzy set. + :param b: Peak point of the fuzzy set. + :param c: Right point of the fuzzy set. + """ + self.a = a + self.b = b + self.c = c + + def membership(self, x): + """ + Calculate the membership degree of a value 'x' in the fuzzy set. + + :param x: The input value to calculate the membership for. + :return: The membership degree of 'x' in the fuzzy set. + """ + if x < self.a or x > self.c: + return 0.0 + elif self.a <= x < self.b: + return (x - self.a) / (self.b - self.a) + elif self.b <= x <= self.c: + return (self.c - x) / (self.c - self.b) + else: + return 1.0 + +# Example usage: +if __name__ == "__main__": + # Create a triangular fuzzy set with parameters (a=2, b=4, c=6) + fuzzy_set = TriangularFuzzySet(2, 4, 6) + + # Calculate the membership degree of values in the set + values = [3, 4, 5, 6, 7] + for value in values: + membership_degree = fuzzy_set.membership(value) + print(f"Membership of {value} in the fuzzy set: {membership_degree}")