Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

triangular fuzzy set #11050

Closed
wants to merge 13 commits into from
67 changes: 67 additions & 0 deletions divide_and_conquer/graphcolor.py
Original file line number Diff line number Diff line change
@@ -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()
39 changes: 39 additions & 0 deletions dynamic_programming/spiralmatrix.py
Original file line number Diff line number Diff line change
@@ -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)))
39 changes: 39 additions & 0 deletions dynamic_programming/triangularfuzz.py
Original file line number Diff line number Diff line change
@@ -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}")